forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve-fetch_test.go
214 lines (176 loc) · 5.05 KB
/
serve-fetch_test.go
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
213
214
package segmented_test
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/usnistgov/ndn-dpdk/core/testenv"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/ndntestenv"
"github.com/usnistgov/ndn-dpdk/ndn/segmented"
"go4.org/must"
)
var (
makeAR = testenv.MakeAR
randBytes = testenv.RandBytes
)
const fetchTimeout = 10 * time.Second
type ServeFetchFixture struct {
t testing.TB
Bridge *ndntestenv.Bridge
Payload []byte
SOpt segmented.ServeOptions
FOpt segmented.FetchOptions
}
func (f *ServeFetchFixture) EnableBridge() {
relay := ndntestenv.BridgeRelayConfig{
Loss: 0.05,
MinDelay: 40 * time.Millisecond,
MaxDelay: 80 * time.Millisecond,
}
f.Bridge = ndntestenv.NewBridge(ndntestenv.BridgeConfig{
RelayAB: relay,
RelayBA: relay,
})
f.SOpt.Fw = f.Bridge.FwA
f.FOpt.Fw = f.Bridge.FwB
if f.FOpt.RetxLimit == 0 {
f.FOpt.RetxLimit = 3
}
}
func (f *ServeFetchFixture) Prepare(payloadLen, chunkSize int) {
f.Payload = make([]byte, payloadLen)
randBytes(f.Payload)
f.SOpt.ChunkSize = chunkSize
}
func (f *ServeFetchFixture) Serve() (close func()) {
_, require := makeAR(f.t)
s, e := segmented.Serve(context.Background(), bytes.NewReader(f.Payload), f.SOpt)
require.NoError(e)
return func() { must.Close(s) }
}
func (f *ServeFetchFixture) Fetch() segmented.FetchResult {
return segmented.Fetch(f.SOpt.Prefix, f.FOpt)
}
func NewServeFetchFixture(t testing.TB) (f *ServeFetchFixture) {
f = &ServeFetchFixture{t: t}
f.SOpt.Prefix = ndn.ParseName("/D")
return f
}
func TestInexact(t *testing.T) {
assert, require := makeAR(t)
fixture := NewServeFetchFixture(t)
fixture.EnableBridge()
fixture.Prepare(10000, 3333)
fixture.SOpt.DataSigner = ndn.DigestSigning
fixture.FOpt.Verifier = ndn.DigestSigning
defer fixture.Serve()()
f := fixture.Fetch()
ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout)
defer cancel()
pkts, e := f.Packets(ctx)
require.NoError(e)
require.Len(pkts, 4)
assert.Equal(len(pkts), f.Count())
assert.Equal(fixture.Payload[0:3333], pkts[0].Content)
assert.Equal(fixture.Payload[3333:6666], pkts[1].Content)
assert.Equal(fixture.Payload[6666:9999], pkts[2].Content)
assert.Equal(fixture.Payload[9999:10000], pkts[3].Content)
assert.False(pkts[0].IsFinalBlock())
assert.False(pkts[1].IsFinalBlock())
assert.False(pkts[2].IsFinalBlock())
assert.True(pkts[3].IsFinalBlock())
}
func TestExact(t *testing.T) {
assert, require := makeAR(t)
fixture := NewServeFetchFixture(t)
fixture.EnableBridge()
fixture.Prepare(4000, 2000)
defer fixture.Serve()()
f := fixture.Fetch()
ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout)
defer cancel()
chunks := make(chan []byte, 64)
e := f.Chunks(ctx, chunks)
require.NoError(e)
require.Len(chunks, 2)
chunk0 := <-chunks
chunk1 := <-chunks
assert.Equal(fixture.Payload[0:2000], chunk0)
assert.Equal(fixture.Payload[2000:4000], chunk1)
}
func TestEmpty(t *testing.T) {
assert, require := makeAR(t)
fixture := NewServeFetchFixture(t)
fixture.EnableBridge()
fixture.Prepare(0, 1024)
defer fixture.Serve()()
f1 := fixture.Fetch()
f2 := fixture.Fetch()
ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout)
defer cancel()
pkts, e := f1.Packets(ctx)
require.NoError(e)
require.Len(pkts, 1)
assert.Len(pkts[0].Content, 0)
assert.True(pkts[0].IsFinalBlock())
payload, e := f2.Payload(ctx)
require.NoError(e)
require.Len(payload, 0)
}
type verifyErrorVerifier struct{}
func (verifyErrorVerifier) Verify(packet ndn.Verifiable) error {
return packet.VerifyWith(func(name ndn.Name, si ndn.SigInfo) (ndn.LLVerify, error) {
seg := name[len(name)-1].Value[0]
if seg == 1 {
return nil, errors.New("mock-verify-error")
}
return func(input, sig []byte) error { return nil }, nil
})
}
func TestVerifyError(t *testing.T) {
assert, _ := makeAR(t)
fixture := NewServeFetchFixture(t)
fixture.EnableBridge()
fixture.Prepare(5000, 3000)
fixture.FOpt.Verifier = verifyErrorVerifier{}
defer fixture.Serve()()
f := fixture.Fetch()
ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout)
defer cancel()
pkts, e := f.Packets(ctx)
assert.Error(e)
assert.Nil(pkts)
}
type verifySlowVerifier struct{}
func (verifySlowVerifier) Verify(packet ndn.Verifiable) error {
return packet.VerifyWith(func(name ndn.Name, si ndn.SigInfo) (ndn.LLVerify, error) {
seg := name[len(name)-1].Value[0]
switch seg % 4 {
case 0:
case 1:
time.Sleep(30 * time.Millisecond)
case 2:
time.Sleep(10 * time.Millisecond)
case 3:
time.Sleep(20 * time.Millisecond)
}
return func(input, sig []byte) error { return nil }, nil
})
}
func TestVerifySlow(t *testing.T) {
assert, require := makeAR(t)
fixture := NewServeFetchFixture(t)
fixture.EnableBridge()
fixture.Prepare(50000, 600)
fixture.FOpt.Verifier = verifySlowVerifier{}
defer fixture.Serve()()
f := fixture.Fetch()
ctx, cancel := context.WithTimeout(context.Background(), fetchTimeout)
defer cancel()
payload, e := f.Payload(ctx)
require.NoError(e)
assert.Equal(fixture.Payload, payload)
assert.Equal(84, f.Count())
}