From 66149607ca0beb39d86cbd686144f5674be1a849 Mon Sep 17 00:00:00 2001 From: Eugene Syromyatnikov Date: Wed, 20 Feb 2019 14:52:59 +0100 Subject: [PATCH] tests: add checks for dumpstr output into read-write test util.c:dumpstr() now aligns offsets in accordance with the total length of the dump and also may perform only partial dump, so let's add checks for these cases. * tests/fill_memory.c (fill_memory_ex): Change the type of the period argument from unsigned char to unsigned int. * tests/tests.h (fill_memory_ex): Likewise. * tests/read-write.c (dump_str_ex): Rename from dump_str, add idx_w argument, support len greater than 240 bytes. (dump_str): New function, wrapper for dump_str_ex with the default index width. (print_hex): Add checks for dumpstr output, close fd 1 later. --- tests/fill_memory.c | 2 +- tests/read-write.c | 54 +++++++++++++++++++++++++++++++++++++++++---- tests/tests.h | 2 +- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/tests/fill_memory.c b/tests/fill_memory.c index 6b15bbed..80a4247d 100644 --- a/tests/fill_memory.c +++ b/tests/fill_memory.c @@ -9,7 +9,7 @@ void fill_memory_ex(void *ptr, size_t size, unsigned char start, - unsigned char period) + unsigned int period) { unsigned char *p = ptr; size_t i; diff --git a/tests/read-write.c b/tests/read-write.c index 66d0242a..650573b5 100644 --- a/tests/read-write.c +++ b/tests/read-write.c @@ -17,7 +17,7 @@ #include static void -dump_str(const char *str, const unsigned int len) +dump_str_ex(const char *str, const unsigned int len, const int idx_w) { static const char chars[256] = "................................" @@ -34,13 +34,19 @@ dump_str(const char *str, const unsigned int len) unsigned int n = len - i > 16 ? 16 : len - i; const char *dump = hexdump_memdup(str + i, n); - tprintf(" | %05x %-49s %-16.*s |\n", - i, dump, n, chars + i); + tprintf(" | %0*x %-49s %-16.*s |\n", + idx_w, i, dump, n, chars + i % 0x100); free((void *) dump); } } +static inline void +dump_str(const char *str, const unsigned int len) +{ + dump_str_ex(str, len, 5); +} + static void print_hex(const char *str, const unsigned int len) { @@ -189,7 +195,6 @@ main(void) tprintf("write(1, \"%s\", %u) = %ld\n" " | 00000 %-49s %-16s |\n", w_c, w_len, rc, w_d, w_c); - close(1); rc = k_read(0, r0, 0); if (rc) @@ -218,6 +223,47 @@ main(void) r1_c, w_len, rc, r1_d, r1_c); close(0); + /* + * Check partial dump; relies on dumpstr() implementation details + * (maximum size of chunk to be copied at once). + */ + static const size_t six_wide_size = 1 << 20; + static const size_t fetch_size = 1 << 16; + static const char big_buf_str[] = + "\\0\\1\\2\\3\\4\\5\\6\\7" + "\\10\\t\\n\\v\\f\\r\\16\\17" + "\\20\\21\\22\\23\\24\\25\\26\\27" + "\\30\\31\\32\\33\\34\\35\\36\\37"; + const size_t buf_size = six_wide_size + fetch_size; + const size_t sizes[] = { + six_wide_size, + six_wide_size + 1, + buf_size, + buf_size + 1, + buf_size + 2, + }; + char *big_buf = tail_alloc(buf_size); + + fill_memory_ex(big_buf, buf_size, 0, 0x100); + + for (size_t i = 0; i < ARRAY_SIZE(sizes); i++) { + rc = k_write(1, big_buf, sizes[i]); + tprintf("write(1, \"%s\"..., %zu) = %s\n", + big_buf_str, sizes[i], sprintrc(rc)); + dump_str_ex(big_buf, MIN(sizes[i], buf_size), + sizes[i] > six_wide_size ? 6 : 5); + + if (sizes[i] == buf_size + 1) + tprintf(" | \n", + getpid(), big_buf + buf_size); + + if (sizes[i] == buf_size + 2) + tprintf(" | \n", + getpid(), big_buf + buf_size); + } + + close(1); + if (open("/dev/zero", O_RDONLY)) perror_msg_and_fail("open"); diff --git a/tests/tests.h b/tests/tests.h index 69ec810a..76fca294 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -122,7 +122,7 @@ void *tail_memdup(const void *, const size_t) * sign, byte order and/or alignment errors. */ void fill_memory_ex(void *ptr, size_t size, unsigned char start, - unsigned char period); + unsigned int period); /* Shortcut for fill_memory_ex(ptr, size, 0x80, 0x80) */ void fill_memory(void *ptr, size_t size); -- 2.40.0