shachain: clarify design in terms of binary tree, reverse indexes.
[ccan] / ccan / crypto / shachain / test / run-badhash.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 1000
7
8 int main(void)
9 {
10         struct sha256 seed;
11         struct shachain chain;
12         uint64_t i;
13
14         plan_tests(NUM_TESTS);
15
16         memset(&seed, 0xFF, sizeof(seed));
17         shachain_init(&chain);
18
19         for (i = 0xFFFFFFFFFFFFFFFFULL;
20              i > 0xFFFFFFFFFFFFFFFFULL - NUM_TESTS;
21              i--) {
22                 struct sha256 expect;
23
24                 shachain_from_seed(&seed, i, &expect);
25                 /* Screw it up. */
26                 expect.u.u8[0]++;
27
28                 /* Either it should fail, or it couldn't derive any others (ie. pos 0). */
29                 if (shachain_add_hash(&chain, i, &expect)) {
30                         ok1(chain.known[0].index == i);
31                         /* Fix it up in-place */
32                         chain.known[0].hash.u.u8[0]--;
33                 } else {
34                         expect.u.u8[0]--;
35                         ok1(shachain_add_hash(&chain, i, &expect));
36                 }
37         }
38         return exit_status();
39 }