36dfaaeb7adaebb0ed03e438b2a652b126282488
[ccan] / ccan / crypto / shachain / test / run.c
1 #include <ccan/crypto/shachain/shachain.h>
2 /* Include the C files directly. */
3 #include <ccan/crypto/shachain/shachain.c>
4 #include <ccan/tap/tap.h>
5
6 #define NUM_TESTS 50
7
8 int main(void)
9 {
10         struct sha256 seed;
11         struct shachain chain;
12         struct sha256 expect[NUM_TESTS];
13         uint64_t i, j;
14
15         /* This is how many tests you plan to run */
16         plan_tests(NUM_TESTS * 3 + NUM_TESTS * (NUM_TESTS + 1) - 1);
17
18         memset(&seed, 0, sizeof(seed));
19         /* Generate a whole heap. */
20         for (i = 0xFFFFFFFFFFFFFFFFULL;
21              i > 0xFFFFFFFFFFFFFFFFULL - NUM_TESTS;
22              i--) {
23                 int expidx = 0xFFFFFFFFFFFFFFFFULL - i;
24                 shachain_from_seed(&seed, i, &expect[expidx]);
25                 if (i != 0xFFFFFFFFFFFFFFFFULL)
26                         ok1(memcmp(&expect[expidx], &expect[expidx-1],
27                                    sizeof(expect[expidx])));
28         }
29
30         shachain_init(&chain);
31
32         for (i = 0xFFFFFFFFFFFFFFFFULL;
33              i > 0xFFFFFFFFFFFFFFFFULL - NUM_TESTS;
34              i--) {
35                 struct sha256 hash;
36                 int expidx = 0xFFFFFFFFFFFFFFFFULL - i;
37                 ok1(shachain_add_hash(&chain, i, &expect[expidx]));
38                 for (j = i; j != 0; j++) {
39                         ok1(shachain_get_hash(&chain, j, &hash));
40                         expidx = 0xFFFFFFFFFFFFFFFFULL - j;
41                         ok1(memcmp(&hash, &expect[expidx], sizeof(hash)) == 0);
42                 }
43                 ok1(!shachain_get_hash(&chain, i-1, &hash));
44         }
45
46         return exit_status();
47 }