xref: /openbmc/hiomapd/test/flash_copy.c (revision 0297e5b8)
1 // SPDX-License-Identifier: Apache-2.0
2 // Copyright (C) 2018 IBM Corp.
3 
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 "mboxd.h"
14 #include "backend.h"
15 
16 #include "test/system.h"
17 #include "test/tmpf.h"
18 
19 #define TEST_SIZE 4096
20 
21 static struct tmpf tmp;
22 
cleanup(void)23 void cleanup(void)
24 {
25 	tmpf_destroy(&tmp);
26 }
27 
main(void)28 int main(void)
29 {
30 	struct mbox_context context = {0};
31 	ssize_t processed;
32 	int rand_fd;
33 	char *src;
34 	char *dst;
35 	int rc;
36 
37 	atexit(cleanup);
38 
39 	system_set_mtd_sizes(TEST_SIZE, TEST_SIZE);
40 
41 	mbox_vlog = &mbox_log_console;
42 
43 	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 
56 	rc = tmpf_init(&tmp, "flash-store.XXXXXX");
57 	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 	assert(!backend_probe_mtd(&context.backend, tmp.path));
73 
74 	backend_copy(&context.backend, 0, dst, TEST_SIZE);
75 	assert(0 == memcmp(src, dst, TEST_SIZE));
76 
77 	backend_free(&context.backend);
78 
79 free:
80 
81 	free(src);
82 	free(dst);
83 
84 	return rc;
85 }
86