1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stlink.h>
#include <flash.h>
#if defined(_MSC_VER)
#include <malloc.h>
#endif
struct Test {
const char * cmd_line;
int res;
struct flash_opts opts;
};
static bool cmp_strings(const char * s1, const char * s2) {
if (s1 == NULL || s2 == NULL) {
return (s1 == s2);
} else {
return (0 == strcmp(s1, s2));
}
}
static bool cmp_mem(const uint8_t * s1, const uint8_t * s2, size_t size) {
if (s1 == NULL || s2 == NULL) {
return (s1 == s2);
} else {
return (0 == memcmp(s1, s2, size));
}
}
static bool execute_test(const struct Test * test) {
int ac = 0;
char* av[32];
/* parse (tokenize) the test command line */
#if defined(_MSC_VER)
char *cmd_line = alloca(strlen(test->cmd_line) + 1);
#else
char cmd_line[strlen(test->cmd_line) + 1];
#endif
strcpy(cmd_line, test->cmd_line);
for (char * tok = strtok(cmd_line, " "); tok; tok = strtok(NULL, " ")) {
if ((size_t)ac >= sizeof(av) / sizeof(av[0])) return(false);
av[ac] = tok;
++ac;
}
/* Call */
struct flash_opts opts;
int res = flash_get_opts(&opts, ac, av);
/* Compare results */
bool ret = (res == test->res);
if (ret && (res == 0)) {
ret &= (opts.cmd == test->opts.cmd);
ret &= cmp_mem(opts.serial, test->opts.serial, sizeof(opts.serial));
ret &= cmp_strings(opts.filename, test->opts.filename);
ret &= (opts.addr == test->opts.addr);
ret &= (opts.size == test->opts.size);
ret &= (opts.reset == test->opts.reset);
ret &= (opts.log_level == test->opts.log_level);
ret &= (opts.freq == test->opts.freq);
ret &= (opts.format == test->opts.format);
}
printf("[%s] (%d) %s\n", ret ? "OK" : "ERROR", res, test->cmd_line);
return(ret);
}
static struct Test tests[] = {
{ "", -1, FLASH_OPTS_INITIALIZER },
{ "--debug --reset read test.bin 0x80000000 0x1000", 0,
{ .cmd = FLASH_CMD_READ,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 0x1000,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --reset write test.bin 0x80000000", 0,
{ .cmd = FLASH_CMD_WRITE,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 0,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --freq 5k --reset write test.bin 0x80000000", 0,
{ .cmd = FLASH_CMD_WRITE,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 0,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 5,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --freq 15K --reset write test.bin 0x80000000", 0,
{ .cmd = FLASH_CMD_WRITE,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 0,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 15,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --freq=5k --reset write test.bin 0x80000000", 0,
{ .cmd = FLASH_CMD_WRITE,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 0,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 5,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --reset read test.bin 0x80000000 1000", 0,
{ .cmd = FLASH_CMD_READ,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 1000,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --reset read test.bin 0x80000000 1k", 0,
{ .cmd = FLASH_CMD_READ,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 1024,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --reset read test.bin 0x80000000 1M", 0,
{ .cmd = FLASH_CMD_READ,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 1048576,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --reset write test.bin 0x80000000", 0,
{ .cmd = FLASH_CMD_WRITE,
.serial = { 0 },
.filename = "test.bin",
.addr = 0x80000000,
.size = 0,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "erase", 0,
{ .cmd = FLASH_CMD_ERASE,
.serial = { 0 },
.filename = NULL,
.addr = 0,
.size = 0,
.reset = 0,
.log_level = STND_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--debug --reset --format=ihex write test.hex", 0,
{ .cmd = FLASH_CMD_WRITE,
.serial = { 0 },
.filename = "test.hex",
.addr = 0,
.size = 0,
.reset = 1,
.log_level = DEBUG_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_IHEX }
},
{ "--debug --reset --format=binary write test.hex", -1, FLASH_OPTS_INITIALIZER },
{ "--debug --reset --format=ihex write test.hex 0x80000000", -1, FLASH_OPTS_INITIALIZER },
{ "--debug --reset write test.hex sometext", -1, FLASH_OPTS_INITIALIZER },
{ "--serial ABCEFF544851717867216044 erase sometext", -1, FLASH_OPTS_INITIALIZER },
{ "--serial ABCEFF544851717867216044 erase", 0,
{ .cmd = FLASH_CMD_ERASE,
.serial = "ABCEFF544851717867216044",
.filename = NULL,
.addr = 0,
.size = 0,
.reset = 0,
.log_level = STND_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
{ "--serial=ABCEFF544851717867216044 erase", 0,
{ .cmd = FLASH_CMD_ERASE,
.serial = "ABCEFF544851717867216044",
.filename = NULL,
.addr = 0,
.size = 0,
.reset = 0,
.log_level = STND_LOG_LEVEL,
.freq = 0,
.format = FLASH_FORMAT_BINARY }
},
};
int main() {
bool allOk = true;
for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); ++i)
if (!execute_test(&tests[i]))
allOk = false;
return (allOk ? 0 : 1);
}
|