3ed52de5ac630c2e94b46ba41c7d95a5cf2a5e7a
[openssl.git] / test / mdc2_internal_test.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Internal tests for the mdc2 module */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 #include <openssl/mdc2.h>
16 #include "testutil.h"
17 #include "e_os.h"
18
19 typedef struct {
20     const char *input;
21     const unsigned char expected[MDC2_DIGEST_LENGTH];
22 } TESTDATA;
23
24 typedef struct {
25     const char *case_name;
26     int num;
27     const TESTDATA *data;
28 } SIMPLE_FIXTURE;
29
30 /**********************************************************************
31  *
32  * Test of mdc2 internal functions
33  *
34  ***/
35
36 static SIMPLE_FIXTURE setup_mdc2(const char *const test_case_name)
37 {
38     SIMPLE_FIXTURE fixture;
39     fixture.case_name = test_case_name;
40     return fixture;
41 }
42
43 static int execute_mdc2(SIMPLE_FIXTURE fixture)
44 {
45     unsigned char md[MDC2_DIGEST_LENGTH];
46     MDC2_CTX c;
47
48     MDC2_Init(&c);
49     MDC2_Update(&c, (const unsigned char *)fixture.data->input,
50                 strlen(fixture.data->input));
51     MDC2_Final(&(md[0]), &c);
52
53     if (memcmp(fixture.data->expected, md, MDC2_DIGEST_LENGTH)) {
54         fprintf(stderr, "mdc2 test %d: unexpected output\n", fixture.num);
55         return 0;
56     }
57
58     return 1;
59 }
60
61 static void teardown_mdc2(SIMPLE_FIXTURE fixture)
62 {
63 }
64
65 /**********************************************************************
66  *
67  * Test driver
68  *
69  ***/
70
71 static TESTDATA tests[] = {
72     {
73         "Now is the time for all ",
74         {
75             0x42, 0xE5, 0x0C, 0xD2, 0x24, 0xBA, 0xCE, 0xBA,
76             0x76, 0x0B, 0xDD, 0x2B, 0xD4, 0x09, 0x28, 0x1A
77         }
78     }
79 };
80
81 static int drive_tests(int idx)
82 {
83     SETUP_TEST_FIXTURE(SIMPLE_FIXTURE, setup_mdc2);
84     fixture.num = idx;
85     fixture.data = &tests[idx];
86     EXECUTE_TEST(execute_mdc2, teardown_mdc2);
87 }
88
89 int main(int argc, char **argv)
90 {
91     ADD_ALL_TESTS(drive_tests, OSSL_NELEM(tests));
92
93     return run_tests(argv[0]);
94 }