xref: /openbmc/phosphor-mboxd/test/copy_flash.c (revision c3144042b9f050c3d880f4ae5464a2a21617fe7d)
1 /*
2  * MBox Daemon Test File
3  *
4  * Copyright 2017 IBM
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
20 #include <assert.h>
21 #include <fcntl.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 
28 #include "common.h"
29 #include "mbox.h"
30 #include "mboxd_flash.h"
31 
32 #include "test/tmpf.h"
33 
34 #define TEST_SIZE 4096
35 
36 static struct tmpf tmp;
37 
38 void cleanup(void)
39 {
40 	tmpf_destroy(&tmp);
41 }
42 
43 int main(void)
44 {
45 	struct mbox_context context;
46 	ssize_t processed;
47 	int rand_fd;
48 	char *src;
49 	char *dst;
50 	int rc;
51 
52 	atexit(cleanup);
53 
54 	mbox_vlog = &mbox_log_console;
55 
56 	src = malloc(TEST_SIZE);
57 	dst = malloc(TEST_SIZE);
58 	if (!(src && dst)) {
59 		rc = -1;
60 		goto free;
61 	}
62 
63 	rand_fd = open("/dev/urandom", O_RDONLY);
64 	if (rand_fd < 0) {
65 		rc = rand_fd;
66 		goto free;
67 	}
68 
69 	rc = tmpf_init(&tmp, "flash-store.XXXXXX");
70 	if (rc < 0)
71 		goto free;
72 
73 	processed = read(rand_fd, src, TEST_SIZE);
74 	if (processed != TEST_SIZE) {
75 		rc = -1;
76 		goto free;
77 	}
78 
79 	processed = write(tmp.fd, src, TEST_SIZE);
80 	if (processed != TEST_SIZE) {
81 		rc = -1;
82 		goto free;
83 	}
84 
85 	context.fds[MTD_FD].fd = tmp.fd;
86 
87 	copy_flash(&context, 0, dst, TEST_SIZE);
88 	assert(0 == memcmp(src, dst, TEST_SIZE));
89 
90 free:
91 	free(src);
92 	free(dst);
93 
94 	return rc;
95 }
96