| 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 |
"fmt" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
type testTree struct { |
| 14 |
t *testing.T |
| 15 |
h int |
| 16 |
n int64 |
| 17 |
hashes testHashStorage |
| 18 |
} |
| 19 |
|
| 20 |
func newTestTree(t *testing.T, height int) *testTree { |
| 21 |
return &testTree{t: t, h: height} |
| 22 |
} |
| 23 |
|
| 24 |
// Add appends a record with the given data to the tree. |
| 25 |
func (tt *testTree) Add(data []byte) { |
| 26 |
tt.t.Helper() |
| 27 |
hashes, err := StoredHashes(tt.n, data, tt.hashes) |
| 28 |
if err != nil { |
| 29 |
tt.t.Fatal(err) |
| 30 |
} |
| 31 |
tt.hashes = append(tt.hashes, hashes...) |
| 32 |
tt.n++ |
| 33 |
} |
| 34 |
|
| 35 |
func (tt *testTree) Tree() Tree { |
| 36 |
tt.t.Helper() |
| 37 |
th, err := TreeHash(tt.n, tt.hashes) |
| 38 |
if err != nil { |
| 39 |
tt.t.Fatal(err) |
| 40 |
} |
| 41 |
return Tree{N: tt.n, Hash: th} |
| 42 |
} |
| 43 |
|
| 44 |
func (tt *testTree) Height() int { return tt.h } |
| 45 |
|
| 46 |
func (tt *testTree) ReadTiles(tiles []Tile) ([][]byte, error) { |
| 47 |
out := make([][]byte, len(tiles)) |
| 48 |
for i, tile := range tiles { |
| 49 |
data, err := ReadTileData(tile, tt.hashes) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
out[i] = data |
| 54 |
} |
| 55 |
return out, nil |
| 56 |
} |
| 57 |
|
| 58 |
func (tt *testTree) SaveTiles(tiles []Tile, data [][]byte) { |
| 59 |
tt.t.Helper() |
| 60 |
if len(data) != len(tiles) { |
| 61 |
tt.t.Errorf("SaveTiles: got %d data for %d tiles", len(data), len(tiles)) |
| 62 |
return |
| 63 |
} |
| 64 |
for i, tile := range tiles { |
| 65 |
want, err := ReadTileData(tile, tt.hashes) |
| 66 |
if err != nil { |
| 67 |
tt.t.Errorf("SaveTiles(%v): %v", tile.Path(), err) |
| 68 |
continue |
| 69 |
} |
| 70 |
if !bytes.Equal(data[i], want) { |
| 71 |
tt.t.Errorf("SaveTiles(%v): saved data does not match tree", tile.Path()) |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// zeroIndex returns a TileReader that serves the same tiles as tr, |
| 77 |
// except that the hash at the given stored index is replaced with an |
| 78 |
// all-zeroes hash. If the index is not in the bottom row of its tile, |
| 79 |
// the bottom-row hashes it is computed from are zeroed instead. |
| 80 |
func zeroIndex(tr TileReader, index int64) TileReader { |
| 81 |
return &zeroIndexReader{tr, index} |
| 82 |
} |
| 83 |
|
| 84 |
type zeroIndexReader struct { |
| 85 |
TileReader |
| 86 |
index int64 |
| 87 |
} |
| 88 |
|
| 89 |
func (r *zeroIndexReader) ReadTiles(tiles []Tile) ([][]byte, error) { |
| 90 |
data, err := r.TileReader.ReadTiles(tiles) |
| 91 |
if err != nil { |
| 92 |
return nil, err |
| 93 |
} |
| 94 |
t, start, end := tileForIndex(r.Height(), r.index) |
| 95 |
for i, tile := range tiles { |
| 96 |
if tile.H == t.H && tile.L == t.L && tile.N == t.N && end <= len(data[i]) { |
| 97 |
data[i] = bytes.Clone(data[i]) |
| 98 |
clear(data[i][start:end]) |
| 99 |
} |
| 100 |
} |
| 101 |
return data, nil |
| 102 |
} |
| 103 |
|
| 104 |
func TestTileHashReader(t *testing.T) { |
| 105 |
tt := newTestTree(t, 2) |
| 106 |
for range int64(100) { |
| 107 |
tt.Add(fmt.Appendf(nil, "leaf %d", tt.n)) |
| 108 |
t.Run(fmt.Sprintf("N=%d", tt.n), func(t *testing.T) { |
| 109 |
th := TileHashReader(tt.Tree(), tt) |
| 110 |
for i := range StoredHashIndex(0, tt.n) { |
| 111 |
hashes, err := th.ReadHashes([]int64{i}) |
| 112 |
if err != nil { |
| 113 |
t.Fatal(err) |
| 114 |
} |
| 115 |
if len(hashes) != 1 { |
| 116 |
t.Fatalf("ReadHashes(%d) = %d hashes, want 1", i, len(hashes)) |
| 117 |
} |
| 118 |
if hashes[0] != tt.hashes[i] { |
| 119 |
t.Errorf("ReadHashes(%d) = %x, want %x", i, hashes[0], tt.hashes[i]) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
var indexes []int64 |
| 124 |
for j := range StoredHashIndex(0, tt.n) { |
| 125 |
indexes = append(indexes, j) |
| 126 |
} |
| 127 |
all, err := th.ReadHashes(indexes) |
| 128 |
if err != nil { |
| 129 |
t.Fatal(err) |
| 130 |
} |
| 131 |
if len(all) != len(tt.hashes) { |
| 132 |
t.Fatalf("ReadHashes(%d) = %d hashes, want %d", tt.n, len(all), len(tt.hashes)) |
| 133 |
} |
| 134 |
for j, h := range all { |
| 135 |
if h != tt.hashes[j] { |
| 136 |
t.Errorf("ReadHashes(%d)[%d] = %v, want %v", tt.n, j, h, tt.hashes[j]) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
for i := range StoredHashIndex(0, tt.n) { |
| 141 |
t.Run(fmt.Sprintf("tampered=%d", i), func(t *testing.T) { |
| 142 |
thz := TileHashReader(tt.Tree(), zeroIndex(tt, i)) |
| 143 |
|
| 144 |
hashes, err := thz.ReadHashes([]int64{i}) |
| 145 |
if err == nil { |
| 146 |
t.Errorf("ReadHashes(%d) = %v, want error", i, hashes[0]) |
| 147 |
} |
| 148 |
|
| 149 |
all, err := thz.ReadHashes(indexes) |
| 150 |
if err == nil { |
| 151 |
t.Errorf("ReadHashes(%d) = %d hashes, want error", tt.n, len(all)) |
| 152 |
} |
| 153 |
}) |
| 154 |
} |
| 155 |
}) |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
// FuzzParseTilePath tests that ParseTilePath never crashes |
| 160 |
func FuzzParseTilePath(f *testing.F) { |
| 161 |
f.Add("tile/4/0/001") |
| 162 |
f.Add("tile/4/0/001.p/5") |
| 163 |
f.Add("tile/3/5/x123/x456/078") |
| 164 |
f.Add("tile/3/5/x123/x456/078.p/2") |
| 165 |
f.Add("tile/1/0/x003/x057/500") |
| 166 |
f.Add("tile/3/5/123/456/078") |
| 167 |
f.Add("tile/3/-1/123/456/078") |
| 168 |
f.Add("tile/1/data/x003/x057/500") |
| 169 |
f.Fuzz(func(t *testing.T, path string) { |
| 170 |
ParseTilePath(path) |
| 171 |
}) |
| 172 |
} |
| 173 |
|
| 174 |
func TestNewTilesForSize(t *testing.T) { |
| 175 |
for _, tt := range []struct { |
| 176 |
old, new int64 |
| 177 |
want int |
| 178 |
}{ |
| 179 |
{1, 1, 0}, |
| 180 |
{100, 101, 1}, |
| 181 |
{1023, 1025, 3}, |
| 182 |
{1024, 1030, 1}, |
| 183 |
{1030, 2000, 1}, |
| 184 |
{1030, 10000, 10}, |
| 185 |
{49516517, 49516586, 3}, |
| 186 |
} { |
| 187 |
t.Run(fmt.Sprintf("%d-%d", tt.old, tt.new), func(t *testing.T) { |
| 188 |
tiles := NewTiles(10, tt.old, tt.new) |
| 189 |
if got := len(tiles); got != tt.want { |
| 190 |
t.Errorf("got %d, want %d", got, tt.want) |
| 191 |
for _, tile := range tiles { |
| 192 |
t.Logf("%+v", tile) |
| 193 |
} |
| 194 |
} |
| 195 |
}) |
| 196 |
} |
| 197 |
} |