-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathreplaylog_test.go
More file actions
212 lines (172 loc) · 5.8 KB
/
Copy pathreplaylog_test.go
File metadata and controls
212 lines (172 loc) · 5.8 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package sphinx
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestMemoryReplayLogStorageAndRetrieval tests that the non-batch methods on
// MemoryReplayLog work as expected.
func TestMemoryReplayLogStorageAndRetrieval(t *testing.T) {
rl := NewMemoryReplayLog()
rl.Start()
defer rl.Stop()
var hashPrefix HashPrefix
hashPrefix[0] = 1
var cltv1 uint32 = 1
// Attempt to lookup unknown sphinx packet.
_, err := rl.Get(&hashPrefix)
if err == nil {
t.Fatalf("Expected ErrLogEntryNotFound")
}
if err != ErrLogEntryNotFound {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
// Log incoming sphinx packet.
err = rl.Put(&hashPrefix, cltv1)
if err != nil {
t.Fatalf("Put failed - received unexpected error upon Put: %v", err)
}
// Attempt to replay sphinx packet.
err = rl.Put(&hashPrefix, cltv1)
if err == nil {
t.Fatalf("Expected ErrReplayedPacket")
}
if err != ErrReplayedPacket {
t.Fatalf("Put failed - received unexpected error upon Put: %v", err)
}
// Lookup logged sphinx packet.
cltv, err := rl.Get(&hashPrefix)
if err != nil {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
if cltv != cltv1 {
t.Fatalf("Get returned wrong value: expected %v, got %v", cltv1, cltv)
}
// Delete sphinx packet from log.
err = rl.Delete(&hashPrefix)
if err != nil {
t.Fatalf("Delete failed - received unexpected error upon Delete: %v", err)
}
// Attempt to lookup deleted sphinx packet.
_, err = rl.Get(&hashPrefix)
if err == nil {
t.Fatalf("Expected ErrLogEntryNotFound")
}
if err != ErrLogEntryNotFound {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
// Reinsert incoming sphinx packet into the log.
var cltv2 uint32 = 2
err = rl.Put(&hashPrefix, cltv2)
if err != nil {
t.Fatalf("Put failed - received unexpected error upon Put: %v", err)
}
// Lookup logged sphinx packet.
cltv, err = rl.Get(&hashPrefix)
if err != nil {
t.Fatalf("Get failed - received unexpected error upon Get: %v", err)
}
if cltv != cltv2 {
t.Fatalf("Get returned wrong value: expected %v, got %v", cltv2, cltv)
}
}
// TestMemoryReplayLogPutBatch tests that the batch adding of packets to a log
// works as expected.
func TestMemoryReplayLogPutBatch(t *testing.T) {
rl := NewMemoryReplayLog()
rl.Start()
defer rl.Stop()
var hashPrefix1, hashPrefix2 HashPrefix
hashPrefix1[0] = 1
hashPrefix2[0] = 2
// Create a batch with a duplicated packet.
batch1 := NewBatch([]byte{1})
err := batch1.Put(1, &hashPrefix1, 1)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
err = batch1.Put(1, &hashPrefix1, 1)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
replays, err := rl.PutBatch(batch1)
if replays.Size() != 1 || !replays.Contains(1) {
t.Fatalf("Unexpected replay set after adding batch 1 to log: %v", err)
}
// Create a batch with one replayed packet and one valid one.
batch2 := NewBatch([]byte{2})
err = batch2.Put(1, &hashPrefix1, 1)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
err = batch2.Put(2, &hashPrefix2, 2)
if err != nil {
t.Fatalf("Unexpected error adding entry to batch: %v", err)
}
replays, err = rl.PutBatch(batch2)
if replays.Size() != 1 || !replays.Contains(1) {
t.Fatalf("Unexpected replay set after adding batch 2 to log: %v", err)
}
// Reprocess batch 2, which should be idempotent.
replays, err = rl.PutBatch(batch2)
if replays.Size() != 1 || !replays.Contains(1) {
t.Fatalf("Unexpected replay set after adding batch 2 to log: %v", err)
}
}
// TestNoOpReplayLog tests that NoOpReplayLog performs no replay protection,
// allowing all packets through without storing any state.
func TestNoOpReplayLog(t *testing.T) {
t.Parallel()
rl := NewNoOpReplayLog()
// Start and Stop should succeed without error.
require.NoError(t, rl.Start())
defer func() {
require.NoError(t, rl.Stop())
}()
var hashPrefix HashPrefix
hashPrefix[0] = 1
// Get should always return ErrLogEntryNotFound since nothing is stored.
_, err := rl.Get(&hashPrefix)
require.ErrorIs(t, err, ErrLogEntryNotFound)
// Put should always succeed.
require.NoError(t, rl.Put(&hashPrefix, 1))
// Put the same packet again - should still succeed (no replay
// detection).
require.NoError(t, rl.Put(&hashPrefix, 1))
// Get should still return ErrLogEntryNotFound (nothing is stored).
_, err = rl.Get(&hashPrefix)
require.ErrorIs(t, err, ErrLogEntryNotFound)
// Delete should succeed.
require.NoError(t, rl.Delete(&hashPrefix))
}
// TestNoOpReplayLogPutBatch tests that NoOpReplayLog's PutBatch marks batches
// as committed and never reports replays.
func TestNoOpReplayLogPutBatch(t *testing.T) {
t.Parallel()
rl := NewNoOpReplayLog()
var hashPrefix1, hashPrefix2 HashPrefix
hashPrefix1[0] = 1
hashPrefix2[0] = 2
// Create a batch with duplicate packets.
batch1 := NewBatch([]byte{1})
require.NoError(t, batch1.Put(1, &hashPrefix1, 1))
require.NoError(t, batch1.Put(2, &hashPrefix1, 1))
replays, err := rl.PutBatch(batch1)
require.NoError(t, err)
require.True(t, batch1.IsCommitted, "Batch should be marked as "+
"committed")
// NoOpReplayLog doesn't detect intra-batch replays (that's done by
// Batch itself), but it should return an empty set from its own
// detection.
require.NotNil(t, replays)
// Create another batch with the same hash prefix - should not detect
// replay since NoOpReplayLog doesn't store anything.
batch2 := NewBatch([]byte{2})
require.NoError(t, batch2.Put(1, &hashPrefix1, 1))
require.NoError(t, batch2.Put(2, &hashPrefix2, 2))
replays, err = rl.PutBatch(batch2)
require.NoError(t, err)
require.True(t, batch2.IsCommitted, "Batch should be marked as "+
"committed")
// Should report no replays since NoOpReplayLog doesn't track state.
require.Equal(t, 0, replays.Size(), "Expected empty replay set")
}