blob: 5c40b53ffdb51f1a662749fa64b19ac89b12c83e [file] [log] [blame]
Andrew Jeffery0484c752017-04-12 14:20:18 +09301/*
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
36static struct tmpf tmp;
37
38void cleanup(void)
39{
40 tmpf_destroy(&tmp);
41}
42
43int 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
Suraj Jitindar Singh6f3197d2017-05-03 16:57:25 +100054 mbox_vlog = &mbox_log_console;
55
Andrew Jeffery0484c752017-04-12 14:20:18 +093056 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, "flashXXXXXX");
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
90free:
91 free(src);
92 free(dst);
93
94 return rc;
95}