xref: /openbmc/phosphor-mboxd/test/copy_flash.c (revision faeb88c0)
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 	src = malloc(TEST_SIZE);
55 	dst = malloc(TEST_SIZE);
56 	if (!(src && dst)) {
57 		rc = -1;
58 		goto free;
59 	}
60 
61 	rand_fd = open("/dev/urandom", O_RDONLY);
62 	if (rand_fd < 0) {
63 		rc = rand_fd;
64 		goto free;
65 	}
66 
67 	rc = tmpf_init(&tmp, "flashXXXXXX");
68 	if (rc < 0)
69 		goto free;
70 
71 	processed = read(rand_fd, src, TEST_SIZE);
72 	if (processed != TEST_SIZE) {
73 		rc = -1;
74 		goto free;
75 	}
76 
77 	processed = write(tmp.fd, src, TEST_SIZE);
78 	if (processed != TEST_SIZE) {
79 		rc = -1;
80 		goto free;
81 	}
82 
83 	context.fds[MTD_FD].fd = tmp.fd;
84 
85 	copy_flash(&context, 0, dst, TEST_SIZE);
86 	assert(0 == memcmp(src, dst, TEST_SIZE));
87 
88 free:
89 	free(src);
90 	free(dst);
91 
92 	return rc;
93 }
94