Add test recipes for internal SM2 tests
[openssl.git] / test / poly1305_internal_test.c
index 05ef878c535c1e2cbe2ea31368592a96591e4100..7f7a9e39c5bf4a755e575ff1a04633bf50a073aa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the OpenSSL license (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -15,7 +15,7 @@
 #include "testutil.h"
 #include "internal/poly1305.h"
 #include "../crypto/poly1305/poly1305_local.h"
-#include "e_os.h"
+#include "internal/nelem.h"
 
 typedef struct {
     size_t size;
@@ -28,170 +28,12 @@ typedef struct {
     SIZED_DATA expected;
 } TESTDATA;
 
-typedef struct {
-    const char *test_case_name;
-    int test_num;
-    const TESTDATA *test_data;
-} SIMPLE_FIXTURE;
-
 /**********************************************************************
  *
  * Test of poly1305 internal functions
  *
  ***/
 
-static SIMPLE_FIXTURE setup_poly1305(const char *const test_case_name)
-{
-    SIMPLE_FIXTURE fixture;
-    fixture.test_case_name = test_case_name;
-    return fixture;
-}
-
-/* TODO : hex decoder / encoder should be implemented in testutil.c */
-static void hexdump(const unsigned char *a, size_t len)
-{
-    size_t i;
-
-    for (i = 0; i < len; i++)
-        fprintf(stderr, "%02x", a[i]);
-}
-
-static int execute_poly1305(SIMPLE_FIXTURE fixture)
-{
-    POLY1305 poly1305;
-    unsigned int i = fixture.test_num;
-    const TESTDATA *test = fixture.test_data;
-    const unsigned char *in = test->input.data;
-    size_t inlen = test->input.size;
-    const unsigned char *key = test->key.data;
-    const unsigned char *expected = test->expected.data;
-    size_t expectedlen = test->expected.size;
-    unsigned char out[16];
-
-    if (expectedlen != sizeof(out))
-        return 0;
-
-    Poly1305_Init(&poly1305, key);
-    Poly1305_Update(&poly1305, in, inlen);
-    Poly1305_Final(&poly1305, out);
-
-    if (memcmp(out, expected, expectedlen) != 0) {
-        fprintf(stderr, "Poly1305 test #%d failed.\n", i);
-        fprintf(stderr, "got:      ");
-        hexdump(out, sizeof(out));
-        fprintf(stderr, "\nexpected: ");
-        hexdump(expected, expectedlen);
-        fprintf(stderr, "\n");
-        return 0;
-    }
-
-    if (inlen > 16) {
-        Poly1305_Init(&poly1305, key);
-        Poly1305_Update(&poly1305, in, 1);
-        Poly1305_Update(&poly1305, in+1, inlen-1);
-        Poly1305_Final(&poly1305, out);
-
-        if (memcmp(out, expected, expectedlen) != 0) {
-            fprintf(stderr, "Poly1305 test #%d/1+(N-1) failed.\n", i);
-            fprintf(stderr, "got:      ");
-            hexdump(out, sizeof(out));
-            fprintf(stderr, "\nexpected: ");
-            hexdump(expected, expectedlen);
-            fprintf(stderr, "\n");
-            return 0;
-        }
-    }
-
-    if (inlen > 32) {
-        size_t half = inlen / 2;
-
-        Poly1305_Init(&poly1305, key);
-        Poly1305_Update(&poly1305, in, half);
-        Poly1305_Update(&poly1305, in+half, inlen-half);
-        Poly1305_Final(&poly1305, out);
-
-        if (memcmp(out, expected, expectedlen) != 0) {
-            fprintf(stderr, "Poly1305 test #%d/2 failed.\n", i);
-            fprintf(stderr, "got:      ");
-            hexdump(out, sizeof(out));
-            fprintf(stderr, "\nexpected: ");
-            hexdump(expected, expectedlen);
-            fprintf(stderr, "\n");
-            return 0;
-        }
-
-        for (half = 16; half < inlen; half += 16) {
-            Poly1305_Init(&poly1305, key);
-            Poly1305_Update(&poly1305, in, half);
-            Poly1305_Update(&poly1305, in+half, inlen-half);
-            Poly1305_Final(&poly1305, out);
-
-            if (memcmp(out, expected, expectedlen) != 0) {
-                fprintf(stderr, "Poly1305 test #%d/%" OSSLzu "+%" OSSLzu " failed.\n",
-                       i, half, inlen-half);
-                fprintf(stderr, "got:      ");
-                hexdump(out, sizeof(out));
-                fprintf(stderr, "\nexpected: ");
-                hexdump(expected, expectedlen);
-                fprintf(stderr, "\n");
-                return 0;
-            }
-        }
-    }
-
-    return 1;
-}
-
-static void teardown_poly1305(SIMPLE_FIXTURE fixture)
-{
-}
-
-static void benchmark_poly1305()
-{
-# ifdef OPENSSL_CPUID_OBJ
-    POLY1305 poly1305;
-    unsigned char key[32];
-    unsigned char buf[8192];
-    unsigned long long stopwatch;
-    unsigned long long OPENSSL_rdtsc();
-    unsigned int i;
-
-    memset (buf,0x55,sizeof(buf));
-    memset (key,0xAA,sizeof(key));
-
-    Poly1305_Init(&poly1305, key);
-
-    for (i=0;i<100000;i++)
-        Poly1305_Update(&poly1305,buf,sizeof(buf));
-
-    stopwatch = OPENSSL_rdtsc();
-    for (i=0;i<10000;i++)
-        Poly1305_Update(&poly1305,buf,sizeof(buf));
-    stopwatch = OPENSSL_rdtsc() - stopwatch;
-
-    printf("%g\n",stopwatch/(double)(i*sizeof(buf)));
-
-    stopwatch = OPENSSL_rdtsc();
-    for (i=0;i<10000;i++) {
-        Poly1305_Init(&poly1305, key);
-        Poly1305_Update(&poly1305,buf,16);
-        Poly1305_Final(&poly1305,buf);
-    }
-    stopwatch = OPENSSL_rdtsc() - stopwatch;
-
-    printf("%g\n",stopwatch/(double)(i));
-# else
-    fprintf(stderr,
-            "Benchmarking of poly1305 isn't available on this platform\n");
-# endif
-}
-
-/**********************************************************************
- *
- * Test driver
- *
- ***/
-
 static TESTDATA tests[] = {
     /*
      * RFC7539
@@ -1662,39 +1504,73 @@ static TESTDATA tests[] = {
     }
 };
 
-static int drive_tests(int idx)
+static int test_poly1305(int idx)
 {
-    SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, setup_poly1305);
-    fixture.test_num = idx;
-    fixture.test_data = &tests[idx];
-    EXECUTE_TEST(execute_poly1305, teardown_poly1305);
-}
+    POLY1305 poly1305;
+    const TESTDATA test = tests[idx];
+    const unsigned char *in = test.input.data;
+    size_t inlen = test.input.size;
+    const unsigned char *key = test.key.data;
+    const unsigned char *expected = test.expected.data;
+    size_t expectedlen = test.expected.size;
+    unsigned char out[16];
 
-int main(int argc, char **argv)
-{
-    int result = 0;
-    int iter_argv;
-    int benchmark = 0;
-
-    for (iter_argv = 1; iter_argv < argc; iter_argv++) {
-        if (strcmp(argv[iter_argv], "-b") == 0)
-            benchmark = 1;
-        else if (strcmp(argv[iter_argv], "-h") == 0)
-            goto help;
+    if (!TEST_size_t_eq(expectedlen, sizeof(out)))
+        return 0;
+
+    Poly1305_Init(&poly1305, key);
+    Poly1305_Update(&poly1305, in, inlen);
+    Poly1305_Final(&poly1305, out);
+
+    if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) {
+        TEST_info("Poly1305 test #%d failed.", idx);
+        return 0;
     }
 
-    ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
+    if (inlen > 16) {
+        Poly1305_Init(&poly1305, key);
+        Poly1305_Update(&poly1305, in, 1);
+        Poly1305_Update(&poly1305, in+1, inlen-1);
+        Poly1305_Final(&poly1305, out);
+
+        if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) {
+            TEST_info("Poly1305 test #%d/1+(N-1) failed.", idx);
+            return 0;
+        }
+    }
+
+    if (inlen > 32) {
+        size_t half = inlen / 2;
+
+        Poly1305_Init(&poly1305, key);
+        Poly1305_Update(&poly1305, in, half);
+        Poly1305_Update(&poly1305, in+half, inlen-half);
+        Poly1305_Final(&poly1305, out);
 
-    result = run_tests(argv[0]);
+        if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) {
+            TEST_info("Poly1305 test #%d/2 failed.", idx);
+            return 0;
+        }
 
-    if (benchmark)
-        benchmark_poly1305();
+        for (half = 16; half < inlen; half += 16) {
+            Poly1305_Init(&poly1305, key);
+            Poly1305_Update(&poly1305, in, half);
+            Poly1305_Update(&poly1305, in+half, inlen-half);
+            Poly1305_Final(&poly1305, out);
 
-    return result;
+            if (!TEST_mem_eq(out, expectedlen, expected, expectedlen)) {
+                TEST_info("Poly1305 test #%d/%zu+%zu failed.",
+                          idx, half, inlen-half);
+                return 0;
+            }
+        }
+    }
 
- help:
-    printf("-h\tThis help\n");
-    printf("-b\tBenchmark in addition to the tests\n");
+    return 1;
+}
 
-    return 0;
+int setup_tests(void)
+{
+    ADD_ALL_TESTS(test_poly1305, OSSL_NELEM(tests));
+    return 1;
 }