blob: 08dfc1a55c2356a17f7aa8398cc6b2cf77166a65 [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
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
Andrew Jefferye0cdd3e2018-08-08 16:51:44 +093071 flash_copy(&context, 0, dst, TEST_SIZE);
Andrew Jeffery0484c752017-04-12 14:20:18 +093072 assert(0 == memcmp(src, dst, TEST_SIZE));
73
74free:
75 free(src);
76 free(dst);
77
78 return rc;
79}