Andrew Jeffery | 4fe996c | 2018-02-27 12:16:48 +1030 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright (C) 2018 IBM Corp. |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 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" |
Andrew Jeffery | 26558db | 2018-08-10 00:22:38 +0930 | [diff] [blame] | 13 | #include "mboxd.h" |
Andrew Jeffery | eebc6bd | 2018-08-08 10:38:19 +0930 | [diff] [blame] | 14 | #include "flash.h" |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 15 | |
Andrew Jeffery | 2dfc2a2 | 2019-03-14 16:42:15 +1030 | [diff] [blame] | 16 | #include "test/system.h" |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 17 | #include "test/tmpf.h" |
| 18 | |
| 19 | #define TEST_SIZE 4096 |
| 20 | |
| 21 | static struct tmpf tmp; |
| 22 | |
| 23 | void cleanup(void) |
| 24 | { |
| 25 | tmpf_destroy(&tmp); |
| 26 | } |
| 27 | |
| 28 | int main(void) |
| 29 | { |
Andrew Jeffery | 2dfc2a2 | 2019-03-14 16:42:15 +1030 | [diff] [blame] | 30 | struct mbox_context context = {0}; |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 31 | ssize_t processed; |
| 32 | int rand_fd; |
| 33 | char *src; |
| 34 | char *dst; |
| 35 | int rc; |
| 36 | |
| 37 | atexit(cleanup); |
| 38 | |
Andrew Jeffery | 2dfc2a2 | 2019-03-14 16:42:15 +1030 | [diff] [blame] | 39 | system_set_mtd_sizes(TEST_SIZE, TEST_SIZE); |
| 40 | |
Suraj Jitindar Singh | 6f3197d | 2017-05-03 16:57:25 +1000 | [diff] [blame] | 41 | mbox_vlog = &mbox_log_console; |
| 42 | |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 56 | rc = tmpf_init(&tmp, "flash-store.XXXXXX"); |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 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 | context.fds[MTD_FD].fd = tmp.fd; |
| 73 | |
Andrew Jeffery | e0cdd3e | 2018-08-08 16:51:44 +0930 | [diff] [blame] | 74 | flash_copy(&context, 0, dst, TEST_SIZE); |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 75 | assert(0 == memcmp(src, dst, TEST_SIZE)); |
| 76 | |
| 77 | free: |
| 78 | free(src); |
| 79 | free(dst); |
| 80 | |
| 81 | return rc; |
| 82 | } |