blob: b8beb21ce038e3cfde1a6a7b392df7864eeb613f [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"
13#include "mbox.h"
14#include "mboxd_flash.h"
15
16#include "test/tmpf.h"
17
18#define TEST_SIZE 4096
19
20static struct tmpf tmp;
21
22void cleanup(void)
23{
24 tmpf_destroy(&tmp);
25}
26
27int main(void)
28{
29 struct mbox_context context;
30 ssize_t processed;
31 int rand_fd;
32 char *src;
33 char *dst;
34 int rc;
35
36 atexit(cleanup);
37
Suraj Jitindar Singh6f3197d2017-05-03 16:57:25 +100038 mbox_vlog = &mbox_log_console;
39
Andrew Jeffery0484c752017-04-12 14:20:18 +093040 src = malloc(TEST_SIZE);
41 dst = malloc(TEST_SIZE);
42 if (!(src && dst)) {
43 rc = -1;
44 goto free;
45 }
46
47 rand_fd = open("/dev/urandom", O_RDONLY);
48 if (rand_fd < 0) {
49 rc = rand_fd;
50 goto free;
51 }
52
Andrew Jefferyc3144042018-02-26 13:24:52 +103053 rc = tmpf_init(&tmp, "flash-store.XXXXXX");
Andrew Jeffery0484c752017-04-12 14:20:18 +093054 if (rc < 0)
55 goto free;
56
57 processed = read(rand_fd, src, TEST_SIZE);
58 if (processed != TEST_SIZE) {
59 rc = -1;
60 goto free;
61 }
62
63 processed = write(tmp.fd, src, TEST_SIZE);
64 if (processed != TEST_SIZE) {
65 rc = -1;
66 goto free;
67 }
68
69 context.fds[MTD_FD].fd = tmp.fd;
70
71 copy_flash(&context, 0, dst, TEST_SIZE);
72 assert(0 == memcmp(src, dst, TEST_SIZE));
73
74free:
75 free(src);
76 free(dst);
77
78 return rc;
79}