]> git.ozlabs.org Git - ccan/blob - ccan/crypto/sha256/test/run-test-vectors.c
sha256: Use hex for test results
[ccan] / ccan / crypto / sha256 / test / run-test-vectors.c
1 #include <ccan/crypto/sha256/sha256.h>
2 #include <ccan/str/hex/hex.h>
3 /* Include the C files directly. */
4 #include <ccan/crypto/sha256/sha256.c>
5 #include <ccan/tap/tap.h>
6
7 /* Test vectors. */
8 struct test {
9         const char *test;
10         size_t repetitions;
11         const char *result;
12 };
13
14 static struct test tests[] = {
15         { "", 1,
16           "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" },
17         { "abc", 1,
18           "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
19         { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
20           "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
21         { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
22           "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" },
23         { "a", 1000000,
24           "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" }
25 #if 0 /* Good test, but takes ages! */
26         , { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno",
27             16777216,
28             "50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e" },
29 #endif
30 };
31
32 static bool do_test(const struct test *t)
33 {
34         struct sha256 h, expected;
35
36         if (t->repetitions == 1)
37                 sha256(&h, t->test, strlen(t->test));
38         else {
39                 struct sha256_ctx ctx = SHA256_INIT;
40                 size_t i;
41
42                 for (i = 0; i < t->repetitions; i++)
43                         sha256_update(&ctx, t->test, strlen(t->test));
44                 sha256_done(&ctx, &h);
45         }
46
47         hex_decode(t->result, strlen(t->result), &expected, sizeof(expected));
48         return memcmp(&h, &expected, sizeof(h)) == 0;
49 }
50
51 int main(void)
52 {
53         const size_t num_tests = sizeof(tests) / sizeof(tests[0]);
54         size_t i;
55
56         /* This is how many tests you plan to run */
57         plan_tests(num_tests);
58
59         for (i = 0; i < num_tests; i++)
60                 ok1(do_test(&tests[i]));
61
62         /* This exits depending on whether all tests passed */
63         return exit_status();
64 }