diff mbox series

[ovs-dev] tests: Add overflow test for the sha1 library.

Message ID 20201116190822.1199992-1-i.maximets@ovn.org
State New
Headers show
Series [ovs-dev] tests: Add overflow test for the sha1 library. | expand

Commit Message

Ilya Maximets Nov. 16, 2020, 7:08 p.m. UTC
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>
---
 tests/library.at  |  3 ++-
 tests/test-sha1.c | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

Comments

Paolo Valerio Nov. 23, 2020, 4:07 p.m. UTC | #1
Ilya Maximets <i.maximets@ovn.org> writes:

> 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>
diff mbox series

Patch

diff --git a/tests/library.at b/tests/library.at
index ac4ea4abf..1702b7556 100644
--- a/tests/library.at
+++ b/tests/library.at
@@ -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
 
diff --git a/tests/test-sha1.c b/tests/test-sha1.c
index b7279db6a..cc80888a7 100644
--- a/tests/test-sha1.c
+++ b/tests/test-sha1.c
@@ -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');
 }