diff options
Diffstat (limited to 'src/systemcmds/tests/test_file2.c')
-rw-r--r-- | src/systemcmds/tests/test_file2.c | 74 |
1 files changed, 48 insertions, 26 deletions
diff --git a/src/systemcmds/tests/test_file2.c b/src/systemcmds/tests/test_file2.c index 8db3ea5ae..2e7b5c184 100644 --- a/src/systemcmds/tests/test_file2.c +++ b/src/systemcmds/tests/test_file2.c @@ -69,42 +69,51 @@ static uint8_t get_value(uint32_t ofs) static void test_corruption(const char *filename, uint32_t write_chunk, uint32_t write_size, uint16_t flags) { - printf("Testing on %s with write_chunk=%u write_size=%u\n", + printf("Testing on %s with write_chunk=%u write_size=%u\n", filename, (unsigned)write_chunk, (unsigned)write_size); - + uint32_t ofs = 0; int fd = open(filename, O_CREAT | O_RDWR | O_TRUNC); + if (fd == -1) { perror(filename); exit(1); } - + // create a file of size write_size, in write_chunk blocks uint8_t counter = 0; + while (ofs < write_size) { - uint8_t buffer[write_chunk]; - for (uint16_t j=0; j<write_chunk; j++) { - buffer[j] = get_value(ofs); - ofs++; - } - if (write(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) { - printf("write failed at offset %u\n", ofs); - exit(1); - } - if (flags & FLAG_FSYNC) { - fsync(fd); - } - if (counter % 100 == 0) { - printf("write ofs=%u\r", ofs); - } - counter++; + uint8_t buffer[write_chunk]; + + for (uint16_t j = 0; j < write_chunk; j++) { + buffer[j] = get_value(ofs); + ofs++; + } + + if (write(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) { + printf("write failed at offset %u\n", ofs); + exit(1); + } + + if (flags & FLAG_FSYNC) { + fsync(fd); + } + + if (counter % 100 == 0) { + printf("write ofs=%u\r", ofs); + } + + counter++; } + close(fd); - + printf("write ofs=%u\n", ofs); - + // read and check fd = open(filename, O_RDONLY); + if (fd == -1) { perror(filename); exit(1); @@ -112,31 +121,39 @@ static void test_corruption(const char *filename, uint32_t write_chunk, uint32_t counter = 0; ofs = 0; + while (ofs < write_size) { uint8_t buffer[write_chunk]; + if (counter % 100 == 0) { printf("read ofs=%u\r", ofs); } + counter++; + if (read(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) { printf("read failed at offset %u\n", ofs); exit(1); } - for (uint16_t j=0; j<write_chunk; j++) { + + for (uint16_t j = 0; j < write_chunk; j++) { if (buffer[j] != get_value(ofs)) { printf("corruption at ofs=%u got %u\n", ofs, buffer[j]); exit(1); } + ofs++; } + if (flags & FLAG_LSEEK) { lseek(fd, 0, SEEK_CUR); } } + printf("read ofs=%u\n", ofs); close(fd); unlink(filename); - printf("All OK\n"); + printf("All OK\n"); } static void usage(void) @@ -155,22 +172,26 @@ int test_file2(int argc, char *argv[]) uint16_t flags = 0; const char *filename = "/fs/microsd/testfile2.dat"; uint32_t write_chunk = 64; - uint32_t write_size = 5*1024; + uint32_t write_size = 5 * 1024; while ((opt = getopt(argc, argv, "c:s:FLh")) != EOF) { switch (opt) { case 'F': flags |= FLAG_FSYNC; break; + case 'L': flags |= FLAG_LSEEK; break; + case 's': write_size = strtoul(optarg, NULL, 0); break; + case 'c': write_chunk = strtoul(optarg, NULL, 0); break; + case 'h': default: usage(); @@ -187,12 +208,13 @@ int test_file2(int argc, char *argv[]) /* check if microSD card is mounted */ struct stat buffer; + if (stat("/fs/microsd/", &buffer)) { warnx("no microSD card mounted, aborting file test"); return 1; } - test_corruption(filename, write_chunk, write_size, flags); - return 0; + test_corruption(filename, write_chunk, write_size, flags); + return 0; } |