blob: 3b88b76505f3d292a27756f8c235760e7090cfd9 [file] [log] [blame]
Andrew Jeffery4fe996c2018-02-27 12:16:48 +10301// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2018 IBM Corp.
Andrew Jeffery0484c752017-04-12 14:20:18 +09303
4#include <assert.h>
5#include <fcntl.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11
12#include "common.h"
Andrew Jeffery26558db2018-08-10 00:22:38 +093013#include "mboxd.h"
Andrew Jefferyeebc6bd2018-08-08 10:38:19 +093014#include "flash.h"
Andrew Jeffery0484c752017-04-12 14:20:18 +093015
Andrew Jeffery2dfc2a22019-03-14 16:42:15 +103016#include "test/system.h"
Andrew Jeffery0484c752017-04-12 14:20:18 +093017#include "test/tmpf.h"
18
19#define TEST_SIZE 4096
20
21static struct tmpf tmp;
22
23void cleanup(void)
24{
25 tmpf_destroy(&tmp);
26}
27
28int main(void)
29{
Andrew Jeffery2dfc2a22019-03-14 16:42:15 +103030 struct mbox_context context = {0};
Andrew Jeffery0484c752017-04-12 14:20:18 +093031 ssize_t processed;
32 int rand_fd;
33 char *src;
34 char *dst;
35 int rc;
36
37 atexit(cleanup);
38
Andrew Jeffery2dfc2a22019-03-14 16:42:15 +103039 system_set_mtd_sizes(TEST_SIZE, TEST_SIZE);
40
Suraj Jitindar Singh6f3197d2017-05-03 16:57:25 +100041 mbox_vlog = &mbox_log_console;
42
Andrew Jeffery0484c752017-04-12 14:20:18 +093043 src = malloc(TEST_SIZE);
44 dst = malloc(TEST_SIZE);
45 if (!(src && dst)) {
46 rc = -1;
47 goto free;
48 }
49
50 rand_fd = open("/dev/urandom", O_RDONLY);
51 if (rand_fd < 0) {
52 rc = rand_fd;
53 goto free;
54 }
55
Andrew Jefferyc3144042018-02-26 13:24:52 +103056 rc = tmpf_init(&tmp, "flash-store.XXXXXX");
Andrew Jeffery0484c752017-04-12 14:20:18 +093057 if (rc < 0)
58 goto free;
59
60 processed = read(rand_fd, src, TEST_SIZE);
61 if (processed != TEST_SIZE) {
62 rc = -1;
63 goto free;
64 }
65
66 processed = write(tmp.fd, src, TEST_SIZE);
67 if (processed != TEST_SIZE) {
68 rc = -1;
69 goto free;
70 }
71
72 context.fds[MTD_FD].fd = tmp.fd;
73
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +093074 flash_copy(&context, 0, dst, TEST_SIZE);
Andrew Jeffery0484c752017-04-12 14:20:18 +093075 assert(0 == memcmp(src, dst, TEST_SIZE));
76
77free:
78 free(src);
79 free(dst);
80
81 return rc;
82}