Debian Codegraph @

sumdb/tlog/tlog_test.go

raw
1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package tlog
6
7 import (
8 "bytes"
9 "crypto/sha256"
10 "fmt"
11 "testing"
12 )
13
14 type testHashStorage []Hash
15
16 func (t testHashStorage) ReadHash(level int, n int64) (Hash, error) {
17 return t[StoredHashIndex(level, n)], nil
18 }
19
20 func (t testHashStorage) ReadHashes(index []int64) ([]Hash, error) {
21 // It's not required by HashReader that indexes be in increasing order,
22 // but check that the functions we are testing only ever ask for
23 // indexes in increasing order.
24 for i := 1; i < len(index); i++ {
25 if index[i-1] >= index[i] {
26 panic("indexes out of order")
27 }
28 }
29
30 out := make([]Hash, len(index))
31 for i, x := range index {
32 out[i] = t[x]
33 }
34 return out, nil
35 }
36
37 type testTilesStorage struct {
38 unsaved int
39 m map[Tile][]byte
40 }
41
42 func (t testTilesStorage) Height() int {
43 return 2
44 }
45
46 func (t *testTilesStorage) SaveTiles(tiles []Tile, data [][]byte) {
47 t.unsaved -= len(tiles)
48 }
49
50 func (t *testTilesStorage) ReadTiles(tiles []Tile) ([][]byte, error) {
51 out := make([][]byte, len(tiles))
52 for i, tile := range tiles {
53 out[i] = t.m[tile]
54 }
55 t.unsaved += len(tiles)
56 return out, nil
57 }
58
59 func TestTree(t *testing.T) {
60 var trees []Hash
61 var leafhashes []Hash
62 var storage testHashStorage
63 tiles := make(map[Tile][]byte)
64 const testH = 2
65 for i := range int64(100) {
66 data := fmt.Appendf(nil, "leaf %d", i)
67 hashes, err := StoredHashes(i, data, storage)
68 if err != nil {
69 t.Fatal(err)
70 }
71 leafhashes = append(leafhashes, RecordHash(data))
72 oldStorage := len(storage)
73 storage = append(storage, hashes...)
74 if count := StoredHashCount(i + 1); count != int64(len(storage)) {
75 t.Errorf("StoredHashCount(%d) = %d, have %d StoredHashes", i+1, count, len(storage))
76 }
77 th, err := TreeHash(i+1, storage)
78 if err != nil {
79 t.Fatal(err)
80 }
81
82 for _, tile := range NewTiles(testH, i, i+1) {
83 data, err := ReadTileData(tile, storage)
84 if err != nil {
85 t.Fatal(err)
86 }
87 old := Tile{H: tile.H, L: tile.L, N: tile.N, W: tile.W - 1}
88 oldData := tiles[old]
89 if len(oldData) != len(data)-HashSize || !bytes.Equal(oldData, data[:len(oldData)]) {
90 t.Fatalf("tile %v not extending earlier tile %v", tile.Path(), old.Path())
91 }
92 tiles[tile] = data
93 }
94 for _, tile := range NewTiles(testH, 0, i+1) {
95 data, err := ReadTileData(tile, storage)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if !bytes.Equal(tiles[tile], data) {
100 t.Fatalf("mismatch at %+v", tile)
101 }
102 }
103 for _, tile := range NewTiles(testH, i/2, i+1) {
104 data, err := ReadTileData(tile, storage)
105 if err != nil {
106 t.Fatal(err)
107 }
108 if !bytes.Equal(tiles[tile], data) {
109 t.Fatalf("mismatch at %+v", tile)
110 }
111 }
112
113 // Check that all the new hashes are readable from their tiles.
114 for j := oldStorage; j < len(storage); j++ {
115 tile := TileForIndex(testH, int64(j))
116 data, ok := tiles[tile]
117 if !ok {
118 t.Log(NewTiles(testH, 0, i+1))
119 t.Fatalf("TileForIndex(%d, %d) = %v, not yet stored (i=%d, stored %d)", testH, j, tile.Path(), i, len(storage))
120 continue
121 }
122 h, err := HashFromTile(tile, data, int64(j))
123 if err != nil {
124 t.Fatal(err)
125 }
126 if h != storage[j] {
127 t.Errorf("HashFromTile(%v, %d) = %v, want %v", tile.Path(), int64(j), h, storage[j])
128 }
129 }
130
131 trees = append(trees, th)
132
133 // Check that leaf proofs work, for all trees and leaves so far.
134 for j := int64(0); j <= i; j++ {
135 p, err := ProveRecord(i+1, j, storage)
136 if err != nil {
137 t.Fatalf("ProveRecord(%d, %d): %v", i+1, j, err)
138 }
139 if err := CheckRecord(p, i+1, th, j, leafhashes[j]); err != nil {
140 t.Fatalf("CheckRecord(%d, %d): %v", i+1, j, err)
141 }
142 for k := range p {
143 p[k][0] ^= 1
144 if err := CheckRecord(p, i+1, th, j, leafhashes[j]); err == nil {
145 t.Fatalf("CheckRecord(%d, %d) succeeded with corrupt proof hash #%d!", i+1, j, k)
146 }
147 p[k][0] ^= 1
148 }
149 }
150
151 // Check that leaf proofs work using TileReader.
152 // To prove a leaf that way, all you have to do is read and verify its hash.
153 storage := &testTilesStorage{m: tiles}
154 thr := TileHashReader(Tree{i + 1, th}, storage)
155 for j := int64(0); j <= i; j++ {
156 h, err := thr.ReadHashes([]int64{StoredHashIndex(0, j)})
157 if err != nil {
158 t.Fatalf("TileHashReader(%d).ReadHashes(%d): %v", i+1, j, err)
159 }
160 if h[0] != leafhashes[j] {
161 t.Fatalf("TileHashReader(%d).ReadHashes(%d) returned wrong hash", i+1, j)
162 }
163
164 // Even though reading the hash suffices,
165 // check we can generate the proof too.
166 p, err := ProveRecord(i+1, j, thr)
167 if err != nil {
168 t.Fatalf("ProveRecord(%d, %d, TileHashReader(%d)): %v", i+1, j, i+1, err)
169 }
170 if err := CheckRecord(p, i+1, th, j, leafhashes[j]); err != nil {
171 t.Fatalf("CheckRecord(%d, %d, TileHashReader(%d)): %v", i+1, j, i+1, err)
172 }
173 }
174 if storage.unsaved != 0 {
175 t.Fatalf("TileHashReader(%d) did not save %d tiles", i+1, storage.unsaved)
176 }
177
178 // Check that ReadHashes will give an error if the index is not in the tree.
179 if _, err := thr.ReadHashes([]int64{(i + 1) * 2}); err == nil {
180 t.Fatalf("TileHashReader(%d).ReadHashes(%d) for index not in tree <nil>, want err", i, i+1)
181 }
182 if storage.unsaved != 0 {
183 t.Fatalf("TileHashReader(%d) did not save %d tiles", i+1, storage.unsaved)
184 }
185
186 // Check that tree proofs work, for all trees so far, using TileReader.
187 // To prove a tree that way, all you have to do is compute and verify its hash.
188 for j := int64(0); j <= i; j++ {
189 h, err := TreeHash(j+1, thr)
190 if err != nil {
191 t.Fatalf("TreeHash(%d, TileHashReader(%d)): %v", j, i+1, err)
192 }
193 if h != trees[j] {
194 t.Fatalf("TreeHash(%d, TileHashReader(%d)) = %x, want %x (%v)", j, i+1, h[:], trees[j][:], trees[j])
195 }
196
197 // Even though computing the subtree hash suffices,
198 // check that we can generate the proof too.
199 p, err := ProveTree(i+1, j+1, thr)
200 if err != nil {
201 t.Fatalf("ProveTree(%d, %d): %v", i+1, j+1, err)
202 }
203 if err := CheckTree(p, i+1, th, j+1, trees[j]); err != nil {
204 t.Fatalf("CheckTree(%d, %d): %v [%v]", i+1, j+1, err, p)
205 }
206 for k := range p {
207 p[k][0] ^= 1
208 if err := CheckTree(p, i+1, th, j+1, trees[j]); err == nil {
209 t.Fatalf("CheckTree(%d, %d) succeeded with corrupt proof hash #%d!", i+1, j+1, k)
210 }
211 p[k][0] ^= 1
212 }
213 }
214 if storage.unsaved != 0 {
215 t.Fatalf("TileHashReader(%d) did not save %d tiles", i+1, storage.unsaved)
216 }
217 }
218 }
219
220 func TestSplitStoredHashIndex(t *testing.T) {
221 for l := range 10 {
222 for n := range int64(100) {
223 x := StoredHashIndex(l, n)
224 l1, n1 := SplitStoredHashIndex(x)
225 if l1 != l || n1 != n {
226 t.Fatalf("StoredHashIndex(%d, %d) = %d, but SplitStoredHashIndex(%d) = %d, %d", l, n, x, x, l1, n1)
227 }
228 }
229 }
230 }
231
232 // TODO(rsc): Test invalid paths too, like "tile/3/5/123/456/078".
233 var tilePaths = []struct {
234 path string
235 tile Tile
236 }{
237 {"tile/4/0/001", Tile{4, 0, 1, 16}},
238 {"tile/4/0/001.p/5", Tile{4, 0, 1, 5}},
239 {"tile/3/5/x123/x456/078", Tile{3, 5, 123456078, 8}},
240 {"tile/3/5/x123/x456/078.p/2", Tile{3, 5, 123456078, 2}},
241 {"tile/1/0/x003/x057/500", Tile{1, 0, 3057500, 2}},
242 {"tile/3/5/123/456/078", Tile{}},
243 {"tile/3/-1/123/456/078", Tile{}},
244 {"tile/1/data/x003/x057/500", Tile{1, -1, 3057500, 2}},
245 }
246
247 func TestTilePath(t *testing.T) {
248 for _, tt := range tilePaths {
249 if tt.tile.H > 0 {
250 p := tt.tile.Path()
251 if p != tt.path {
252 t.Errorf("%+v.Path() = %q, want %q", tt.tile, p, tt.path)
253 }
254 }
255 tile, err := ParseTilePath(tt.path)
256 if err != nil {
257 if tt.tile.H == 0 {
258 // Expected error.
259 continue
260 }
261 t.Errorf("ParseTilePath(%q): %v", tt.path, err)
262 } else if tile != tt.tile {
263 if tt.tile.H == 0 {
264 t.Errorf("ParseTilePath(%q): expected error, got %+v", tt.path, tt.tile)
265 continue
266 }
267 t.Errorf("ParseTilePath(%q) = %+v, want %+v", tt.path, tile, tt.tile)
268 }
269 }
270 }
271
272 func TestEmptyTree(t *testing.T) {
273 h, err := TreeHash(0, nil)
274 if err != nil {
275 t.Fatal(err)
276 }
277 if h != sha256.Sum256(nil) {
278 t.Fatalf("TreeHash(0) = %x, want SHA-256('')", h)
279 }
280 }