forked from anacrolix/dht
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
112 lines (87 loc) · 3.06 KB
/
server_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
package dht
import (
"context"
"crypto/ed25519"
"testing"
"github.com/anacrolix/dht/v2/bep44"
"github.com/anacrolix/log"
"github.com/anacrolix/torrent/bencode"
"github.com/stretchr/testify/require"
)
func TestPutGet(t *testing.T) {
require := require.New(t)
l := log.Default.WithNames(t.Name())
s1 := newServer(t, l.WithNames("s1"))
s2 := newServer(t, l.WithNames("s2"))
s2Addr := NewAddr(s2.Addr())
immuItem, err := bep44.NewItem("Hello World! immu", nil, 1, 1, nil)
require.NoError(err)
// send get request to s2, we need a write token to put data
qr := s1.Get(context.TODO(), s2Addr, immuItem.Target(), nil, QueryRateLimiting{})
require.NoError(qr.ToError())
require.NotNil(qr.Reply.R)
require.NotNil(qr.Reply.R.Token)
// send put request to s2
qr = s1.Put(context.TODO(), s2Addr, immuItem.ToPut(), *qr.Reply.R.Token, QueryRateLimiting{})
require.NoError(qr.ToError())
qr = s1.Get(context.TODO(), s2Addr, immuItem.Target(), nil, QueryRateLimiting{})
require.NoError(qr.ToError())
var vStr string // heueahea
require.NoError(bencode.Unmarshal(qr.Reply.R.V, &vStr))
require.Equal("Hello World! immu", vStr)
_, priv, err := ed25519.GenerateKey(nil)
require.NoError(err)
mutItem, err := bep44.NewItem("Hello World!", []byte("s1"), 1, 1, priv)
require.NoError(err)
// send get request to s2, we need a write token to put data
qr = s1.Get(context.TODO(), s2Addr, mutItem.Target(), nil, QueryRateLimiting{})
require.NoError(qr.ToError())
require.NotNil(qr.Reply.R)
mutToken := qr.Reply.R.Token
require.NotNil(mutToken)
// send put request to s2
qr = s1.Put(context.TODO(), s2Addr, mutItem.ToPut(), *mutToken, QueryRateLimiting{})
require.NoError(qr.ToError())
qr = s1.Get(context.TODO(), s2Addr, mutItem.Target(), nil, QueryRateLimiting{})
require.NoError(qr.ToError())
require.NoError(bencode.Unmarshal(qr.Reply.R.V, &vStr))
require.Equal("Hello World!", vStr)
ii, err := s2.store.Get(immuItem.Target())
require.NoError(err)
require.Equal("Hello World! immu", ii.V)
mi, err := s2.store.Get(mutItem.Target())
require.NoError(err)
require.Equal("Hello World!", mi.V)
// change mutable item
ok := mutItem.Modify("Bye World!", priv)
require.True(ok)
qr = s1.Put(context.TODO(), s2Addr, mutItem.ToPut(), *mutToken, QueryRateLimiting{})
require.NoError(qr.ToError())
mi, err = s2.store.Get(mutItem.Target())
require.NoError(err)
require.Equal("Bye World!", mi.V)
qr = s1.Get(context.TODO(), s2Addr, mutItem.Target(), nil, QueryRateLimiting{})
require.NoError(qr.ToError())
require.NoError(bencode.Unmarshal(qr.Reply.R.V, &vStr))
require.Equal("Bye World!", vStr)
seqPtr := new(int64)
*seqPtr = 3
qr = s1.Get(context.TODO(), s2Addr, mutItem.Target(), seqPtr, QueryRateLimiting{})
require.NoError(qr.ToError())
require.Nil(qr.Reply.R.V)
require.Equal(int64(2), *qr.Reply.R.Seq)
}
func newServer(t *testing.T, l log.Logger) *Server {
cfg := NewDefaultServerConfig()
cfg.WaitToReply = true
cfg.Conn = mustListen("localhost:0")
cfg.Logger = l
s, err := NewServer(cfg)
if err != nil {
panic(err)
}
t.Cleanup(func() {
s.Close()
})
return s
}