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" |
| 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 | |
| 22 | void cleanup(void) |
| 23 | { |
| 24 | tmpf_destroy(&tmp); |
| 25 | } |
| 26 | |
| 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 | |
Suraj Jitindar Singh | 6f3197d | 2017-05-03 16:57:25 +1000 | [diff] [blame] | 38 | mbox_vlog = &mbox_log_console; |
| 39 | |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 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 | |
Andrew Jeffery | c314404 | 2018-02-26 13:24:52 +1030 | [diff] [blame] | 53 | rc = tmpf_init(&tmp, "flash-store.XXXXXX"); |
Andrew Jeffery | 0484c75 | 2017-04-12 14:20:18 +0930 | [diff] [blame] | 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 | } |