]> granicus.if.org Git - strace/commitdiff
tests: add checks for dumpstr output into read-write test
authorEugene Syromyatnikov <evgsyr@gmail.com>
Wed, 20 Feb 2019 13:52:59 +0000 (14:52 +0100)
committerEugene Syromyatnikov <evgsyr@gmail.com>
Thu, 21 Feb 2019 00:18:38 +0000 (01:18 +0100)
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
tests/read-write.c
tests/tests.h

index 6b15bbed7cacfd6b6ffa04ed378200a8ae422220..80a4247ddc07237e99618fa430c6615a8cba269a 100644 (file)
@@ -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;
index 66d0242a69d43431b8758fc3c7e5a6e454b911cb..650573b5e2e058169664a983ea0a44fbf235efec 100644 (file)
@@ -17,7 +17,7 @@
 #include <asm/unistd.h>
 
 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(" | <Cannot fetch 1 byte from pid %d @%p>\n",
+                               getpid(), big_buf + buf_size);
+
+               if (sizes[i] == buf_size + 2)
+                       tprintf(" | <Cannot fetch 2 bytes from pid %d @%p>\n",
+                               getpid(), big_buf + buf_size);
+       }
+
+       close(1);
+
        if (open("/dev/zero", O_RDONLY))
                perror_msg_and_fail("open");
 
index 69ec810accb4f1bb478b5082f3a76d0dcc76ffda..76fca294f0a1f3f356d06ca2e0f1787f10825f7b 100644 (file)
@@ -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);