xref: /openbmc/phosphor-mboxd/test/copy_flash.c (revision acdbdd14)
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 "mbox.h"
14 #include "mboxd_flash.h"
15 
16 #include "test/tmpf.h"
17 
18 #define TEST_SIZE 4096
19 
20 static struct tmpf tmp;
21 
cleanup(void)22 void cleanup(void)
23 {
24 	tmpf_destroy(&tmp);
25 }
26 
main(void)27 int 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 
38 	mbox_vlog = &mbox_log_console;
39 
40 	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 
53 	rc = tmpf_init(&tmp, "flash-store.XXXXXX");
54 	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 
74 free:
75 	free(src);
76 	free(dst);
77 
78 	return rc;
79 }
80