summaryrefslogtreecommitdiff
path: root/tools/testcase1.py
blob: 63dddc775842c17d3bcb76dce48cf889620329a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
import urllib2
import urllib
import json
import base64
import sys
import struct
import hashlib
import itertools
from certtools import *

baseurl = "https://127.0.0.1:8080/"
certfiles = ["testcerts/cert1.txt", "testcerts/cert2.txt",
             "testcerts/cert3.txt", "testcerts/cert4.txt",
             "testcerts/cert5.txt"]

cc1 = get_certs_from_file(certfiles[0])
cc2 = get_certs_from_file(certfiles[1])
cc3 = get_certs_from_file(certfiles[2])
cc4 = get_certs_from_file(certfiles[3])
cc5 = get_certs_from_file(certfiles[4])

failures = 0

def assert_equal(actual, expected, name):
    global failures
    if actual != expected:
        print "ERROR:", name, "expected", expected, "got", actual
        failures += 1
    else:
        print name, "was correct"

def print_and_check_tree_size(expected):
    global failures
    sth = get_sth(baseurl)
    try:
        check_sth_signature(baseurl, sth)
    except AssertionError, e:
        print "ERROR:", e
        failures += 1
    except ecdsa.keys.BadSignatureError, e:
        print "ERROR: bad STH signature"
        failures += 1
    tree_size = sth["tree_size"]
    if tree_size == expected:
        print "tree size", tree_size
    else:
        print "ERROR: tree size", tree_size, "expected", expected
        failures += 1

def do_add_chain(chain):
    global failures
    try:
        result = add_chain(baseurl, {"chain":map(base64.b64encode, chain)})
    except ValueError, e:
        print "ERROR:", e
        failures += 1
    try:
        check_sct_signature(baseurl, chain[0], result)
    except AssertionError, e:
        print "ERROR:", e
        failures += 1
    except ecdsa.keys.BadSignatureError, e:
        print "ERROR: bad SCT signature"
        failures += 1
    print "signature check succeeded"
    return result

def get_and_validate_proof(timestamp, cert, leaf_index, nentries):
    merkle_tree_leaf = pack_mtl(timestamp, cert)
    leaf_hash = get_leaf_hash(merkle_tree_leaf)
    sth = get_sth(baseurl)
    proof = get_proof_by_hash(baseurl, leaf_hash, sth["tree_size"])
    assert_equal(proof["leaf_index"], leaf_index, "leaf_index")
    assert_equal(len(proof["audit_path"]), nentries, "audit_path length")

print_and_check_tree_size(0)

result1 = do_add_chain(cc1)

print_and_check_tree_size(1)

result2 = do_add_chain(cc1)

assert_equal(result2["timestamp"], result1["timestamp"], "timestamp")

print_and_check_tree_size(1)

# TODO: add invalid cert and check that it generates an error
#       and that treesize still is 1

get_and_validate_proof(result1["timestamp"], cc1[0], 0, 0)

result3 = do_add_chain(cc2)

print_and_check_tree_size(2)

get_and_validate_proof(result1["timestamp"], cc1[0], 0, 1)
get_and_validate_proof(result3["timestamp"], cc2[0], 1, 1)

result4 = do_add_chain(cc3)

print_and_check_tree_size(3)

get_and_validate_proof(result1["timestamp"], cc1[0], 0, 2)
get_and_validate_proof(result3["timestamp"], cc2[0], 1, 2)
get_and_validate_proof(result4["timestamp"], cc3[0], 2, 1)

result5 = do_add_chain(cc4)

print_and_check_tree_size(4)

get_and_validate_proof(result1["timestamp"], cc1[0], 0, 2)
get_and_validate_proof(result3["timestamp"], cc2[0], 1, 2)
get_and_validate_proof(result4["timestamp"], cc3[0], 2, 2)
get_and_validate_proof(result5["timestamp"], cc4[0], 3, 2)

result6 = do_add_chain(cc5)

print_and_check_tree_size(5)

get_and_validate_proof(result1["timestamp"], cc1[0], 0, 3)
get_and_validate_proof(result3["timestamp"], cc2[0], 1, 3)
get_and_validate_proof(result4["timestamp"], cc3[0], 2, 3)
get_and_validate_proof(result5["timestamp"], cc4[0], 3, 3)
get_and_validate_proof(result6["timestamp"], cc5[0], 4, 1)

print "-------"
if failures:
    print failures, "failed tests" if failures != 1 else "failed test"
    sys.exit(1)
else:
    print "all tests succeeded"