Add afl_test test. Not run by default

This commit is contained in:
philip
2016-08-24 08:32:41 -04:00
parent 4d89d9b190
commit bae70314d3
6 changed files with 219 additions and 18 deletions
+2 -1
View File
@@ -16,7 +16,8 @@ builddir = build
#
#############
CC = gcc $(COMPILEROPTIONS)
CC ?= gcc
CC += $(COMPILEROPTIONS)
LD = ld
GDB = gdb
OBJCOPY = objcopy
+1 -1
View File
@@ -137,7 +137,7 @@ typedef void (*spiffs_file_callback)(struct spiffs_t *fs, spiffs_fileop_type op,
#ifndef SPIFFS_DBG
#define SPIFFS_DBG(...) \
print(__VA_ARGS__)
printf(__VA_ARGS__)
#endif
#ifndef SPIFFS_GC_DBG
#define SPIFFS_GC_DBG(...) printf(__VA_ARGS__)
+186
View File
@@ -597,6 +597,111 @@ TEST(temporal_fd_cache) {
return TEST_RES_OK;
} TEST_END
TEST(afl_test) {
fs_reset_specific(0, 0, 32*1024, 4096, 2*4096, 256);
int res;
(FS)->fd_count = 4;
FILE * f = stdin;
if (!f) {
return TEST_RES_OK;
}
int c;
spiffs_file fd[4];
memset(fd, -1, sizeof(fd));
char *filename[8];
int i;
for (i = 0; i < 8; i++) {
char buff[64];
sprintf(buff, "%dfile%d.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxasdasdasdadxxxxxxxxxxxxxxxxxxx", i, i);
buff[9 + 2 * i] = 0;
filename[i] = strdup(buff);
}
int modes[8] = {SPIFFS_RDONLY, SPIFFS_RDWR, SPIFFS_RDWR|SPIFFS_TRUNC, SPIFFS_RDWR|SPIFFS_CREAT, SPIFFS_RDWR|SPIFFS_CREAT|SPIFFS_TRUNC,
SPIFFS_WRONLY|SPIFFS_CREAT|SPIFFS_TRUNC, SPIFFS_RDWR|SPIFFS_CREAT|SPIFFS_TRUNC|SPIFFS_DIRECT, SPIFFS_WRONLY};
char buff[2048];
for (i = 0; i < sizeof(buff); i++) {
buff[i] = i * 19;
}
while ((c = fgetc(f)) >= 0) {
int add;
char rbuff[2048];
if (c <= ' ') {
continue;
}
int arg = fgetc(f);
if (arg < 0) {
break;
}
int fdn = (arg >> 6) & 3;
switch(c) {
case 'O':
if (fd[fdn] >= 0) {
SPIFFS_close(FS, fd[fdn]);
}
fd[fdn] = SPIFFS_open(FS, filename[(arg>>3) & 7], modes[arg & 7], 0);
printf("Open returned %d\n", fd[fdn]);
break;
case 'R':
if (fd[fdn] >= 0) {
SPIFFS_read(FS, fd[fdn], rbuff, (15 << (arg & 7)) + (arg & 127));
}
break;
case 'W':
if (fd[fdn] >= 0) {
int rc = SPIFFS_write(FS, fd[fdn], buff, (15 << (arg & 7)) + (arg & 127));
printf("Write returned %d\n", rc);
}
break;
case 'C':
if (fd[fdn] >= 0) {
SPIFFS_close(FS, fd[fdn]);
}
fd[fdn] = -1;
break;
case 'b':
add = fgetc(f);
for (i = 0; i < sizeof(buff); i++) {
buff[i] = add + i * arg;
}
break;
case 'f':
if (fd[fdn] >= 0) {
SPIFFS_fflush(FS, fd[fdn]);
}
break;
case 'd':
SPIFFS_remove(FS, filename[arg & 7]);
break;
case 'r':
SPIFFS_rename(FS, filename[arg & 7], filename[(arg >> 3) & 7]);
break;
}
}
for (i = 0; i < 4; i++) {
if (fd[i] >= 0) {
SPIFFS_close(FS, fd[i]);
}
}
return TEST_RES_OK;
} TEST_END
TEST(small_free_space) {
fs_reset_specific(0, 0, 400*1024, 4096, 2*4096, 256);
spiffs_file fd;
@@ -688,6 +793,85 @@ TEST(small_free_space) {
return TEST_RES_OK;
} TEST_END
TEST(lots_of_overwrite) {
fs_reset_specific(0, 0, 3000*1024, 4096, 2*4096, 256);
spiffs_file fd;
int res;
(FS)->fd_count = 4;
int i;
for (i = 0; i < 5; i++) {
char filename[64];
sprintf(filename, "%d-tstfile", i);
int tfd = SPIFFS_open(FS, filename, SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
TEST_CHECK(tfd > 0);
char tbuf[1024];
memset(tbuf, 'a', 700);
tbuf[700] = 0;
res = SPIFFS_write(FS, tfd, tbuf, strlen(tbuf));
TEST_CHECK(res == strlen(tbuf));
res = SPIFFS_fflush(FS, tfd);
TEST_CHECK(res >= SPIFFS_OK);
SPIFFS_close(FS, tfd);
}
const int runs = 100000;
int run = 0;
for (run = 0; run < runs; run++) {
u8_t buf[2000];
sprintf(buf, "%d", run);
int i;
for (i = 0; i < 100 + (run % 100); i++) {
strcat(buf, " azzaaax");
}
int tfd = SPIFFS_open(FS, "file.dat", SPIFFS_RDWR | SPIFFS_CREAT | SPIFFS_TRUNC, 0);
TEST_CHECK(tfd > 0);
res = SPIFFS_write(FS, tfd, buf, strlen(buf));
TEST_CHECK(res == strlen(buf));
res = SPIFFS_fflush(FS, tfd);
TEST_CHECK(res >= SPIFFS_OK);
SPIFFS_close(FS, tfd);
tfd = SPIFFS_open(FS, "file.dat", SPIFFS_RDONLY, 0);
TEST_CHECK(tfd > 0);
char rbuf[2000];
res = SPIFFS_read(FS, tfd, rbuf, sizeof(rbuf));
TEST_CHECK(res == strlen(buf));
SPIFFS_close(FS, tfd);
TEST_CHECK(memcmp(rbuf, buf, strlen(buf)) == 0);
char filename[64];
sprintf(filename, "%d-tstfile", run % 5);
tfd = SPIFFS_open(FS, filename, SPIFFS_RDONLY, 0);
TEST_CHECK(tfd > 0);
char tbuf[1024];
memset(tbuf, 'a', 700);
tbuf[700] = 0;
res = SPIFFS_read(FS, tfd, rbuf, sizeof(rbuf));
TEST_CHECK(res == strlen(tbuf));
SPIFFS_close(FS, tfd);
TEST_CHECK(memcmp(rbuf, tbuf, strlen(tbuf)) == 0);
}
return TEST_RES_OK;
} TEST_END
#if 0
TEST(spiffs_hidden_file_90) {
@@ -764,6 +948,8 @@ SUITE_TESTS(bug_tests)
ADD_TEST(spiffs_dup_file_74)
ADD_TEST(temporal_fd_cache)
ADD_TEST(small_free_space)
ADD_TEST(lots_of_overwrite)
ADD_TEST_NON_DEFAULT(afl_test)
#if 0
ADD_TEST(spiffs_hidden_file_90)
#endif
+22 -11
View File
@@ -27,6 +27,8 @@
#define AREA(x) _area[(x) - addr_offset]
#define ERREXIT(i) abort()
static u32_t _area_sz;
static unsigned char *_area = NULL;
static u32_t addr_offset = 0;
@@ -152,12 +154,12 @@ static s32_t _read(spiffs *fs, u32_t addr, u32_t size, u8_t *dst) {
}
}
if (addr < __fs.cfg.phys_addr) {
printf("FATAL read addr too low %08x < %08x\n", addr, SPIFFS_PHYS_ADDR);
exit(0);
printf("FATAL read addr too low %08x < %08x\n", addr, __fs.cfg.phys_addr);
ERREXIT(0);
}
if (addr + size > __fs.cfg.phys_addr + __fs.cfg.phys_size) {
printf("FATAL read addr too high %08x + %08x > %08x\n", addr, size, SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE);
exit(0);
printf("FATAL read addr too high %08x + %08x > %08x\n", addr, size, __fs.cfg.phys_addr + __fs.cfg.phys_size);
ERREXIT(0);
}
memcpy(dst, &AREA(addr), size);
return 0;
@@ -178,12 +180,12 @@ static s32_t _write(spiffs *fs, u32_t addr, u32_t size, u8_t *src) {
}
if (addr < __fs.cfg.phys_addr) {
printf("FATAL write addr too low %08x < %08x\n", addr, SPIFFS_PHYS_ADDR);
exit(0);
printf("FATAL write addr too low %08x < %08x\n", addr, __fs.cfg.phys_addr);
ERREXIT(0);
}
if (addr + size > __fs.cfg.phys_addr + __fs.cfg.phys_size) {
printf("FATAL write addr too high %08x + %08x > %08x\n", addr, size, SPIFFS_PHYS_ADDR + SPIFFS_FLASH_SIZE);
exit(0);
printf("FATAL write addr too high %08x + %08x > %08x\n", addr, size, __fs.cfg.phys_addr + __fs.cfg.phys_size);
ERREXIT(0);
}
for (i = 0; i < size; i++) {
@@ -192,6 +194,7 @@ static s32_t _write(spiffs *fs, u32_t addr, u32_t size, u8_t *src) {
printf("trying to write %02x to %02x at addr %08x\n", src[i], AREA(addr + i), addr+i);
spiffs_page_ix pix = (addr + i) / LOG_PAGE;
dump_page(&__fs, pix);
ERREXIT(0);
return -1;
}
}
@@ -202,10 +205,12 @@ static s32_t _write(spiffs *fs, u32_t addr, u32_t size, u8_t *src) {
static s32_t _erase(spiffs *fs, u32_t addr, u32_t size) {
if (addr & (__fs.cfg.phys_erase_block-1)) {
printf("trying to erase at addr %08x, out of boundary\n", addr);
ERREXIT(0);
return -1;
}
if (size & (__fs.cfg.phys_erase_block-1)) {
printf("trying to erase at with size %08x, out of boundary\n", size);
ERREXIT(0);
return -1;
}
_erases[(addr-__fs.cfg.phys_addr)/__fs.cfg.phys_erase_block]++;
@@ -343,6 +348,7 @@ void dump_flash_access_stats() {
static u32_t old_perc = 999;
static int check_cb_count;
static void spiffs_check_cb_f(spiffs *fs, spiffs_check_type type, spiffs_check_report report,
u32_t arg1, u32_t arg2) {
/* if (report == SPIFFS_CHECK_PROGRESS && old_perc != arg1) {
@@ -359,6 +365,7 @@ static void spiffs_check_cb_f(spiffs *fs, spiffs_check_type type, spiffs_check_r
printf("%i%%\n", arg1 * 100 / 256);
}*/
if (report != SPIFFS_CHECK_PROGRESS) {
check_cb_count++;
if (report != SPIFFS_CHECK_ERROR) fs_check_fixes++;
printf(" check: ");
switch (type) {
@@ -397,7 +404,7 @@ void fs_set_addr_offset(u32_t offset) {
void test_lock(spiffs *fs) {
if (_fs_locks != 0) {
printf("FATAL: reentrant locks. Abort.\n");
exit(-1);
ERREXIT(-1);
}
_fs_locks++;
}
@@ -405,7 +412,7 @@ void test_lock(spiffs *fs) {
void test_unlock(spiffs *fs) {
if (_fs_locks != 1) {
printf("FATAL: unlocking unlocked. Abort.\n");
exit(-1);
ERREXIT(-1);
}
_fs_locks--;
}
@@ -880,15 +887,19 @@ void _teardown() {
#endif
dump_erase_counts(FS);
printf(" fs consistency check output begin\n");
check_cb_count = 0;
SPIFFS_check(FS);
printf(" fs consistency check output end\n");
if (check_cb_count) {
//abort();
}
}
clear_test_path();
fs_free();
printf(" locks : %i\n", _fs_locks);
if (_fs_locks != 0) {
printf("FATAL: lock asymmetry. Abort.\n");
exit(-1);
ERREXIT(-1);
}
}
+3 -3
View File
@@ -57,7 +57,7 @@ static char check_spec(char *name) {
static char check_incl_filter(char *name) {
if (strlen(test_main.incl_filter)== 0) return 1;
return strstr(name, test_main.incl_filter) == 0 ? 0 : 1;
return strstr(name, test_main.incl_filter) == 0 ? 0 : 2;
}
static char check_excl_filter(char *name) {
@@ -65,10 +65,10 @@ static char check_excl_filter(char *name) {
return strstr(name, test_main.excl_filter) == 0 ? 1 : 0;
}
void _add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t)) {
void _add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t), int non_default) {
if (f == 0) return;
if (!check_spec(name)) return;
if (!check_incl_filter(name)) return;
if (check_incl_filter(name) <= non_default) return;
if (!check_excl_filter(name)) return;
DBGT("adding test %s\n", name);
test *t = malloc(sizeof(test));
+5 -2
View File
@@ -130,7 +130,10 @@ typedef struct test_res_s {
}
#define ADD_TEST(tf) \
_add_test(__test_##tf, str(tf), setup, teardown);
_add_test(__test_##tf, str(tf), setup, teardown, 0);
#define ADD_TEST_NON_DEFAULT(tf) \
_add_test(__test_##tf, str(tf), setup, teardown, 1);
#define ADD_SUITE(sui) \
extern void _add_suite_tests_##sui(void); \
@@ -150,6 +153,6 @@ void test_init(void (*on_stop)(test *t));
// returns 0 if all tests ok, -1 if any test failed, -2 on badness
int run_tests(int argc, char **args);
void _add_suite(const char *suite_name);
void _add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t));
void _add_test(test_f f, char *name, void (*setup)(test *t), void (*teardown)(test *t), int non_default);
#endif /* TESTRUNNER_H_ */