summaryrefslogtreecommitdiffstats
path: root/test/poly1305_internal_test.c
diff options
context:
space:
mode:
authorEmilia Kasper <emilia@openssl.org>2016-11-04 16:06:12 +0100
committerEmilia Kasper <emilia@openssl.org>2016-11-07 16:55:16 +0100
commitd836d71b2da026b4ed9a2233657b2289ab8e4be0 (patch)
treec4c9b9e2112eaea750871d66914868ea8585a6da /test/poly1305_internal_test.c
parent8e47ee18c8f7e59575effdd8dfcfbfff1a365ede (diff)
Simplify tests part 2
1) Remove some unnecessary fixtures 2) Add EXECUTE_TEST_NO_TEARDOWN shorthand when a fixture exists but has no teardown. 3) Fix return values in ct_test.c (introduced by an earlier refactoring, oops) Note that for parameterized tests, the index (test vector) usually holds all the customization, and there should be no need for a separate test fixture. The CTS test is an exception: it demonstrates how to combine customization with parameterization. Reviewed-by: Richard Levitte <levitte@openssl.org>
Diffstat (limited to 'test/poly1305_internal_test.c')
-rw-r--r--test/poly1305_internal_test.c198
1 files changed, 83 insertions, 115 deletions
diff --git a/test/poly1305_internal_test.c b/test/poly1305_internal_test.c
index 05ef878c53..17746baed4 100644
--- a/test/poly1305_internal_test.c
+++ b/test/poly1305_internal_test.c
@@ -28,25 +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)
{
@@ -56,96 +43,6 @@ static void hexdump(const unsigned char *a, size_t len)
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
@@ -186,12 +83,6 @@ static void benchmark_poly1305()
# endif
}
-/**********************************************************************
- *
- * Test driver
- *
- ***/
-
static TESTDATA tests[] = {
/*
* RFC7539
@@ -1662,12 +1553,89 @@ 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];
+
+ 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", idx);
+ 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", idx);
+ 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", idx);
+ 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",
+ idx, half, inlen-half);
+ fprintf(stderr, "got: ");
+ hexdump(out, sizeof(out));
+ fprintf(stderr, "\nexpected: ");
+ hexdump(expected, expectedlen);
+ fprintf(stderr, "\n");
+ return 0;
+ }
+ }
+ }
+
+ return 1;
}
int main(int argc, char **argv)
@@ -1683,7 +1651,7 @@ int main(int argc, char **argv)
goto help;
}
- ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
+ ADD_ALL_TESTS(test_poly1305, OSSL_NELEM(tests));
result = run_tests(argv[0]);