forked from libp2p/go-libp2p-routing-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimited_test.go
79 lines (73 loc) · 1.53 KB
/
limited_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
package routinghelpers
import (
"context"
"testing"
"github.com/libp2p/go-libp2p/core/routing"
)
func TestLimitedValueStore(t *testing.T) {
d := LimitedValueStore{
ValueStore: new(dummyValueStore),
Namespaces: []string{"allow"},
}
ctx := context.Background()
for i, k := range []string{
"/allow/hello",
"/allow/foo",
"/allow/foo/bar",
} {
if err := d.PutValue(ctx, k, []byte{byte(i)}); err != nil {
t.Fatal(err)
}
v, err := d.GetValue(ctx, k)
if err != nil {
t.Fatal(err)
}
if len(v) != 1 || v[0] != byte(i) {
t.Fatalf("expected value [%d], got %v", i, v)
}
}
for i, k := range []string{
"/deny/hello",
"/allow",
"allow",
"deny",
"",
"/",
"//",
"///",
"//allow",
} {
if err := d.PutValue(ctx, k, []byte{byte(i)}); err != routing.ErrNotSupported {
t.Fatalf("expected put with key %s to fail", k)
}
_, err := d.GetValue(ctx, k)
if err != routing.ErrNotFound {
t.Fatalf("expected get with key %s to fail", k)
}
_, err = d.ValueStore.GetValue(ctx, k)
if err != routing.ErrNotFound {
t.Fatalf("expected get with key %s to fail", k)
}
err = d.ValueStore.PutValue(ctx, k, []byte{byte(i)})
if err != nil {
t.Fatal(err)
}
_, err = d.GetValue(ctx, k)
if err == nil {
t.Fatalf("expected get with key %s to fail", k)
}
}
}
func TestLimitedClose(t *testing.T) {
closer := new(testCloser)
d := LimitedValueStore{
ValueStore: struct {
*testCloser
routing.Routing
}{closer, Null{}},
}
d.Close()
if closer.closed != 1 {
t.Fatal("expected one close")
}
}