]> git.proxmox.com Git - mirror_ovs.git/commitdiff
tests: Add overflow test for the sha1 library.
authorIlya Maximets <i.maximets@ovn.org>
Mon, 16 Nov 2020 19:08:22 +0000 (20:08 +0100)
committerIlya Maximets <i.maximets@ovn.org>
Fri, 27 Nov 2020 13:31:27 +0000 (14:31 +0100)
This is a unit test for the overflow detection issue fixed by commit
a1d2c5f5d9ed ("sha1: Fix algorithm for data bigger than 512 megabytes.")

Signed-off-by: Ilya Maximets <i.maximets@ovn.org>
Acked-by: Paolo Valerio <pvalerio@redhat.com>
Tested-by: Paolo Valerio <pvalerio@redhat.com>
tests/library.at
tests/test-sha1.c

index ac4ea4abf28151afb5b4218650d64a82597f3a29..1702b7556bcfafd4818dc4cf3097dd45633fc440 100644 (file)
@@ -53,7 +53,8 @@ AT_CHECK([ovstest test-packets])
 AT_CLEANUP
 
 AT_SETUP([SHA-1])
-AT_CHECK([ovstest test-sha1], [0], [.........
+AT_KEYWORDS([sha1])
+AT_CHECK([ovstest test-sha1], [0], [..........
 ])
 AT_CLEANUP
 
index b7279db6aaa4594b0a4a3c4be46dd68440426958..cc80888a7dd5358ca2a0ffaa19fd82f704e83f75 100644 (file)
@@ -137,6 +137,42 @@ test_big_vector(void)
     free(vec.data);
 }
 
+static void
+test_huge_vector(void)
+{
+    enum { SIZE = 1000000000 };
+    struct test_vector vec = {
+        NULL, SIZE,
+        /* Computed by the sha1sum utility for a file with 10^9 symbols 'a'. */
+        { 0xD0, 0xF3, 0xE4, 0xF2, 0xF3, 0x1C, 0x66, 0x5A, 0xBB, 0xD8,
+          0xF5, 0x18, 0xE8, 0x48, 0xD5, 0xCB, 0x80, 0xCA, 0x78, 0xF7 }
+    };
+    int chunk = random_range(SIZE / 10000);
+    uint8_t md[SHA1_DIGEST_SIZE];
+    struct sha1_ctx sha1;
+    size_t i, sz;
+
+    /* It's not user-friendly to allocate 1GB of memory for a unit test,
+     * so we're allocating only a small chunk and re-using it. */
+    vec.data = xmalloc(chunk);
+    for (i = 0; i < chunk; i++) {
+        vec.data[i] = 'a';
+    }
+
+    sha1_init(&sha1);
+    for (sz = 0; sz < SIZE; sz += chunk) {
+        int n = sz + chunk < SIZE ? chunk : SIZE - sz;
+
+        sha1_update(&sha1, vec.data, n);
+    }
+    sha1_final(&sha1, md);
+    ovs_assert(!memcmp(md, vec.output, SHA1_DIGEST_SIZE));
+
+    free(vec.data);
+    putchar('.');
+    fflush(stdout);
+}
+
 static void
 test_shar1_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
@@ -147,6 +183,7 @@ test_shar1_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
     }
 
     test_big_vector();
+    test_huge_vector();
 
     putchar('\n');
 }