Debian Codegraph @

storage/posix/antispam/badger_test.go

raw
1 // Copyright 2025 The Tessera authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package badger
16
17 import (
18 "crypto/sha256"
19 "testing"
20 "time"
21
22 "github.com/transparency-dev/tessera"
23 "github.com/transparency-dev/tessera/api"
24 "github.com/transparency-dev/tessera/testonly"
25 "k8s.io/klog/v2"
26 )
27
28 type testLookup struct {
29 entryHash []byte
30 wantNotFound bool
31 }
32
33 func TestAntispamStorage(t *testing.T) {
34
35 for _, test := range []struct {
36 name string
37 opts AntispamOpts
38 logEntries [][]byte
39 lookupEntries []testLookup
40 }{
41 {
42 name: "roundtrip",
43 logEntries: [][]byte{
44 []byte("one"),
45 []byte("two"),
46 []byte("three"),
47 },
48 lookupEntries: []testLookup{
49 {
50 entryHash: testIDHash([]byte("one")),
51 }, {
52 entryHash: testIDHash([]byte("two")),
53 }, {
54 entryHash: testIDHash([]byte("three")),
55 }, {
56 entryHash: testIDHash([]byte("nowhere to be found")),
57 wantNotFound: true,
58 },
59 },
60 },
61 } {
62 t.Run(test.name, func(t *testing.T) {
63 as, err := NewAntispam(t.Context(), t.TempDir(), test.opts)
64 if err != nil {
65 t.Fatalf("NewAntispam: %v", err)
66 }
67
68 fl, shutdown := testonly.NewTestLog(t, tessera.NewAppendOptions().WithCheckpointInterval(time.Second))
69 defer func() {
70 if err := shutdown(t.Context()); err != nil {
71 t.Logf("shutdown: %v", err)
72 }
73 }()
74
75 f := as.Follower(testBundleHasher)
76
77 go f.Follow(t.Context(), fl.LogReader)
78
79 entryIndex := make(map[string]uint64)
80 a := tessera.NewPublicationAwaiter(t.Context(), fl.LogReader.ReadCheckpoint, 100*time.Millisecond)
81 for i, e := range test.logEntries {
82 entry := tessera.NewEntry(e)
83 f := fl.Appender.Add(t.Context(), entry)
84 idx, _, err := a.Await(t.Context(), f)
85 if err != nil {
86 t.Fatalf("Await(%d): %v", i, err)
87 }
88 klog.Infof("%d == %x", i, entry.Identity())
89 entryIndex[string(testIDHash(e))] = idx.Index
90 }
91
92 for {
93 time.Sleep(time.Second)
94 pos, err := f.EntriesProcessed(t.Context())
95 if err != nil {
96 t.Logf("EntriesProcessed: %v", err)
97 continue
98 }
99 sz, err := fl.LogReader.IntegratedSize(t.Context())
100 if err != nil {
101 t.Logf("IntegratedSize: %v", err)
102 continue
103 }
104 klog.Infof("Wait for follower (%d) to catch up with tree (%d)", pos, sz)
105 if pos >= sz {
106 break
107 }
108 }
109
110 for _, e := range test.lookupEntries {
111 gotIndex, err := as.index(t.Context(), e.entryHash)
112 if err != nil {
113 t.Errorf("error looking up hash %x: %v", e.entryHash, err)
114 }
115 wantIndex := entryIndex[string(e.entryHash)]
116 if gotIndex == nil {
117 if !e.wantNotFound {
118 t.Errorf("no index for hash %x, but expected index %d", e.entryHash, wantIndex)
119 }
120 continue
121 }
122 if *gotIndex != wantIndex {
123 t.Errorf("got index %d, want %d from looking up hash %x", gotIndex, wantIndex, e.entryHash)
124 }
125 }
126 })
127 }
128 }
129
130 func TestAntispamPushbackRecovers(t *testing.T) {
131 for _, test := range []struct {
132 name string
133 opts AntispamOpts
134 logEntries [][]byte
135 }{
136 {
137 name: "pushback",
138 opts: AntispamOpts{
139 PushbackThreshold: 1,
140 },
141 logEntries: [][]byte{
142 []byte("one"),
143 []byte("two"),
144 []byte("three"),
145 },
146 },
147 } {
148 t.Run(test.name, func(t *testing.T) {
149 as, err := NewAntispam(t.Context(), t.TempDir(), test.opts)
150 if err != nil {
151 t.Fatalf("NewAntispam: %v", err)
152 }
153
154 fl, shutdown := testonly.NewTestLog(t, tessera.NewAppendOptions().WithCheckpointInterval(time.Second))
155 defer func() {
156 if err := shutdown(t.Context()); err != nil {
157 t.Logf("shutdown: %v", err)
158 }
159 }()
160
161 f := as.Follower(testBundleHasher)
162
163 entryIndex := make(map[string]uint64)
164 a := tessera.NewPublicationAwaiter(t.Context(), fl.LogReader.ReadCheckpoint, 100*time.Millisecond)
165 for i, e := range test.logEntries {
166 entry := tessera.NewEntry(e)
167 f := fl.Appender.Add(t.Context(), entry)
168 idx, _, err := a.Await(t.Context(), f)
169 if err != nil {
170 t.Fatalf("Await(%d): %v", i, err)
171 }
172 klog.Infof("%d == %x", i, entry.Identity())
173 entryIndex[string(testIDHash(e))] = idx.Index
174 }
175
176 // Wait for entries te be integrated before we start the follower, so we know we'll hit the pushback condition
177 go f.Follow(t.Context(), fl.LogReader)
178
179 for {
180 time.Sleep(time.Second)
181 pos, err := f.EntriesProcessed(t.Context())
182 if err != nil {
183 t.Logf("EntriesProcessed: %v", err)
184 continue
185 }
186 sz, err := fl.LogReader.IntegratedSize(t.Context())
187 if err != nil {
188 t.Logf("IntegratedSize: %v", err)
189 continue
190 }
191 klog.Infof("Wait for follower (%d) to catch up with tree (%d)", pos, sz)
192 if pos >= sz {
193 break
194 }
195 }
196
197 // Ensure that the follower gets itself _out_ of pushback mode once it's caught up.
198 // We'll give the follower some time to do its thing and notice.
199 // It runs onces a second, so this should be plenty of time.
200 for i := range 5 {
201 time.Sleep(time.Second)
202 if !as.pushBack.Load() {
203 t.Logf("Antispam caught up and out of pushback in %ds", i)
204 return
205 }
206 }
207 t.Fatalf("pushBack remains true after 5 seconds despite being caught up!")
208 })
209 }
210 }
211
212 func testIDHash(d []byte) []byte {
213 r := sha256.Sum256(d)
214 return r[:]
215 }
216
217 func testBundleHasher(b []byte) ([][]byte, error) {
218 bun := &api.EntryBundle{}
219 err := bun.UnmarshalText(b)
220 if err != nil {
221 return nil, err
222 }
223 r := make([][]byte, len(bun.Entries))
224 for i, e := range bun.Entries {
225 r[i] = testIDHash(e)
226 }
227 return r, err
228 }