|
| 1 | +#include <assert.h> |
| 2 | +#include <setjmp.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <string.h> |
| 6 | +#include <sys/mman.h> |
| 7 | +#include <sys/stat.h> |
| 8 | + |
| 9 | +static jmp_buf exit_jmp; |
| 10 | +static int exit_code = -1; |
| 11 | +static int mmap_calls = 0; |
| 12 | +static int patch_init_called = 0; |
| 13 | +static int perror_called = 0; |
| 14 | + |
| 15 | +static int mock_stat(const char *path, struct stat *st) |
| 16 | +{ |
| 17 | + memset(st, 0, sizeof(*st)); |
| 18 | + if (strcmp(path, "source.bin") == 0) { |
| 19 | + st->st_size = 16; |
| 20 | + return 0; |
| 21 | + } |
| 22 | + if (strcmp(path, "patch.bin") == 0) { |
| 23 | + st->st_size = 8; |
| 24 | + return 0; |
| 25 | + } |
| 26 | + return -1; |
| 27 | +} |
| 28 | + |
| 29 | +static int mock_open(const char *path, int flags, ...) |
| 30 | +{ |
| 31 | + (void)flags; |
| 32 | + if ((strcmp(path, "source.bin") == 0) || (strcmp(path, "patch.bin") == 0)) |
| 33 | + return 3; |
| 34 | + return -1; |
| 35 | +} |
| 36 | + |
| 37 | +static void *mock_mmap(void *addr, size_t len, int prot, int flags, int fd, |
| 38 | + off_t offset) |
| 39 | +{ |
| 40 | + static uint8_t source_map[16]; |
| 41 | + |
| 42 | + (void)addr; |
| 43 | + (void)len; |
| 44 | + (void)prot; |
| 45 | + (void)flags; |
| 46 | + (void)fd; |
| 47 | + (void)offset; |
| 48 | + |
| 49 | + mmap_calls++; |
| 50 | + if (mmap_calls == 1) |
| 51 | + return source_map; |
| 52 | + |
| 53 | + return MAP_FAILED; |
| 54 | +} |
| 55 | + |
| 56 | +static void mock_perror(const char *s) |
| 57 | +{ |
| 58 | + (void)s; |
| 59 | + perror_called = 1; |
| 60 | +} |
| 61 | + |
| 62 | +static void mock_exit(int code) |
| 63 | +{ |
| 64 | + exit_code = code; |
| 65 | + longjmp(exit_jmp, 1); |
| 66 | +} |
| 67 | + |
| 68 | +#define WOLFBOOT_SECTOR_SIZE 1024 |
| 69 | +#define stat(path, st) mock_stat(path, st) |
| 70 | +#define open(path, flags, ...) mock_open(path, flags, ##__VA_ARGS__) |
| 71 | +#define mmap(addr, len, prot, flags, fd, offset) \ |
| 72 | + mock_mmap(addr, len, prot, flags, fd, offset) |
| 73 | +#define perror(s) mock_perror(s) |
| 74 | +#define exit(code) mock_exit(code) |
| 75 | +#define main bmdiff_tool_main |
| 76 | +#include "bmdiff.c" |
| 77 | +#undef main |
| 78 | +#undef exit |
| 79 | +#undef perror |
| 80 | +#undef mmap |
| 81 | +#undef open |
| 82 | +#undef stat |
| 83 | + |
| 84 | +int wb_patch_init(WB_PATCH_CTX *bm, uint8_t *src, uint32_t ssz, uint8_t *patch, |
| 85 | + uint32_t psz) |
| 86 | +{ |
| 87 | + (void)bm; |
| 88 | + (void)src; |
| 89 | + (void)ssz; |
| 90 | + (void)patch; |
| 91 | + (void)psz; |
| 92 | + |
| 93 | + patch_init_called = 1; |
| 94 | + mock_exit(99); |
| 95 | + return -1; |
| 96 | +} |
| 97 | + |
| 98 | +int wb_patch(WB_PATCH_CTX *ctx, uint8_t *dst, uint32_t len) |
| 99 | +{ |
| 100 | + (void)ctx; |
| 101 | + (void)dst; |
| 102 | + (void)len; |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +int wb_diff_init(WB_DIFF_CTX *ctx, uint8_t *src_a, uint32_t len_a, |
| 107 | + uint8_t *src_b, uint32_t len_b) |
| 108 | +{ |
| 109 | + (void)ctx; |
| 110 | + (void)src_a; |
| 111 | + (void)len_a; |
| 112 | + (void)src_b; |
| 113 | + (void)len_b; |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +int wb_diff(WB_DIFF_CTX *ctx, uint8_t *patch, uint32_t len) |
| 118 | +{ |
| 119 | + (void)ctx; |
| 120 | + (void)patch; |
| 121 | + (void)len; |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | +int main(void) |
| 126 | +{ |
| 127 | + char *argv[] = { (char *)"bmpatch", (char *)"source.bin", (char *)"patch.bin" }; |
| 128 | + |
| 129 | + if (setjmp(exit_jmp) == 0) |
| 130 | + bmdiff_tool_main(3, argv); |
| 131 | + |
| 132 | + assert(exit_code == 3); |
| 133 | + assert(perror_called == 1); |
| 134 | + assert(patch_init_called == 0); |
| 135 | + return 0; |
| 136 | +} |
0 commit comments