/
node_test.go
326 lines (274 loc) · 8.29 KB
/
node_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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package node
import (
cr "crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"encoding/hex"
"fmt"
xr "github.com/jddixon/rnglib_go"
xc "github.com/jddixon/xlCrypto_go"
xi "github.com/jddixon/xlNodeID_go"
xt "github.com/jddixon/xlTransport_go"
xu "github.com/jddixon/xlUtil_go"
. "gopkg.in/check.v1"
"runtime"
"strings"
//"time"
)
const (
VERBOSITY = 1
)
var (
MY_MAX_PROC = 2 // should be OK for test, a 2-core machine
)
func makeNodeID(rng *xr.PRNG) (*xi.NodeID, error) {
var buffer []byte
// quasi-random choice, whether to use an SHA1 or SHA3 nodeID
if rng.NextBoolean() {
buffer = make([]byte, xu.SHA1_BIN_LEN)
} else {
buffer = make([]byte, xu.SHA3_BIN_LEN)
}
rng.NextBytes(buffer)
return xi.NewNodeID(buffer)
}
func (s *XLSuite) doKeyTests(c *C, node *Node, rng *xr.PRNG) {
// COMMS KEY
commsPubKey := node.GetCommsPublicKey()
c.Assert(commsPubKey, Not(IsNil)) // NOT
privCommsKey := node.GetCommsPrivateKey()
c.Assert(privCommsKey.Validate(), IsNil)
expLen := (*privCommsKey.D).BitLen()
if VERBOSITY > 1 {
fmt.Printf("bit length of comms private key exponent is %d\n", expLen)
}
// 2037 seen at least once
c.Assert(true, Equals, (2036 <= expLen) && (expLen <= 2048))
c.Assert(privCommsKey.PublicKey, Equals, *commsPubKey) // XXX FAILS
// SIG KEY
sigPubKey := node.GetSigPublicKey()
c.Assert(sigPubKey, Not(IsNil)) // NOT
privSigKey := node.GetSigPrivateKey()
c.Assert(privSigKey.Validate(), IsNil)
expLen = (*privSigKey.D).BitLen()
if VERBOSITY > 1 {
fmt.Printf("bit length of sig private key exponent is %d\n", expLen)
}
// lowest value seen as of 2013-07-16 was 2039
c.Assert(true, Equals, (2036 <= expLen) && (expLen <= 2048))
c.Assert(privSigKey.PublicKey, Equals, *sigPubKey) // FOO
// sign /////////////////////////////////////////////////////////
msgLen := 128
msg := make([]byte, msgLen)
rng.NextBytes(msg)
d := sha1.New()
d.Write(msg)
hash := d.Sum(nil)
sig, err := rsa.SignPKCS1v15(rand.Reader, node.skPriv, cr.SHA1, hash)
c.Assert(err, IsNil)
signer := node.getSigner()
signer.Update(msg)
sig2, err := signer.Sign() // XXX change interface to allow arg
lenSig := len(sig)
lenSig2 := len(sig2)
c.Assert(lenSig, Equals, lenSig2)
for i := 0; i < lenSig; i++ {
c.Assert(sig[i], Equals, sig2[i])
}
// verify ///////////////////////////////////////////////////////
err = rsa.VerifyPKCS1v15(sigPubKey, cr.SHA1, hash, sig)
c.Assert(err, IsNil)
// 2013-06-15, SigVerify now returns error, so nil means OK
c.Assert(nil, Equals, xc.SigVerify(sigPubKey, msg, sig))
s.nilArgCheck(c)
}
// XXX TODO: move these tests into crypto/sig_test.go
// func nilArgCheck(t *testing.T) {
func (s *XLSuite) nilArgCheck(c *C) {
// the next statement should always return an error
err := xc.SigVerify(nil, nil, nil)
c.Assert(nil, Not(Equals), err)
}
// END OF TODO
func (s *XLSuite) TestRuntime(c *C) {
if VERBOSITY > 0 {
fmt.Println("TEST_RUN_TIME")
}
if VERBOSITY > 1 {
MY_MAX_PROC = runtime.NumCPU()
was := runtime.GOMAXPROCS(MY_MAX_PROC)
fmt.Printf("GOMAXPROCS was %d, has been reset to %d\n",
was, MY_MAX_PROC)
fmt.Printf("Number of CPUs: %d\n", runtime.NumCPU())
}
}
func (s *XLSuite) TestNewConstructor(c *C) {
if VERBOSITY > 0 {
fmt.Println("TEST_NEW_CONSTRUCTOR")
}
// if constructor assigns a nil NodeID, we should get an
// IllegalArgument panic
// XXX STUB
// if assigned a nil key, the New constructor should panic
// with an IllegalArgument string
// XXX STUB
}
func (s *XLSuite) shouldCreateTcpEndPoint(c *C, addr string) *xt.TcpEndPoint {
ep, err := xt.NewTcpEndPoint(addr)
c.Assert(err, Equals, nil)
c.Assert(ep, Not(Equals), nil)
return ep
}
func (s *XLSuite) TestAutoCreateOverlays(c *C) {
if VERBOSITY > 0 {
fmt.Println("TEST_AUTO_CREATE_OVERLAYS")
}
MY_MAX_PROC = runtime.NumCPU()
was := runtime.GOMAXPROCS(MY_MAX_PROC)
if VERBOSITY > 1 {
fmt.Printf("GOMAXPROCS was %d, has been reset to %d\n",
was, MY_MAX_PROC)
}
rng := xr.MakeSimpleRNG()
name := rng.NextFileName(4)
id, err := makeNodeID(rng)
c.Assert(err, Equals, nil)
c.Assert(id, Not(IsNil))
ep0 := s.shouldCreateTcpEndPoint(c, "127.0.0.0:0")
ep1 := s.shouldCreateTcpEndPoint(c, "127.0.0.0:0")
ep2 := s.shouldCreateTcpEndPoint(c, "127.0.0.0:0")
e := []xt.EndPointI{ep0, ep1, ep2}
lfs := "tmp/" + hex.EncodeToString(id.Value())
n, err := New(name, id, lfs, nil, nil, nil, e, nil)
c.Assert(err, Equals, nil)
c.Assert(n, Not(Equals), nil)
defer n.CloseAcc()
c.Assert(n.SizeEndPoints(), Equals, len(e))
c.Assert(n.SizeOverlays(), Equals, 1)
// expect to find an acceptor for each endpoint
// XXX STUB XXX
// CloseAcc must close all three acceptors
// XXX STUB XXX
}
// Return an initialized and tested host, with a NodeID, ckPriv,
// and skPriv. OpenAcc() is not called and so any acceptors are not open.
func (s *XLSuite) makeHost(c *C, rng *xr.PRNG) *Node {
// XXX names may not be unique
name := rng.NextFileName(6)
for {
first := string(name[0])
if !strings.Contains(first, "0123456789") &&
!strings.Contains(name, "-") {
break
}
name = rng.NextFileName(6)
}
id, err := makeNodeID(rng)
c.Assert(err, Equals, nil)
c.Assert(id, Not(IsNil))
lfs := "tmp/" + hex.EncodeToString(id.Value())
n, err := NewNew(name, id, lfs)
c.Assert(err, IsNil)
c.Assert(n, Not(IsNil))
c.Assert(name, Equals, n.GetName())
actualID := n.GetNodeID()
c.Assert(true, Equals, id.Equal(actualID))
s.doKeyTests(c, n, rng)
c.Assert(0, Equals, (*n).SizePeers())
c.Assert(0, Equals, (*n).SizeOverlays())
c.Assert(0, Equals, n.SizeConnections())
c.Assert(lfs, Equals, n.GetLFS())
return n
}
// Create a Peer from information in the Node passed. Endpoints
// (and so Overlays) must have already been added to the Node.
func (s *XLSuite) peerFromHost(c *C, n *Node) (peer *Peer) {
var err error
k := len(n.endPoints)
ctors := make([]xt.ConnectorI, k)
for i := 0; i < k; i++ {
ctors[i], err = xt.NewTcpConnector(n.GetEndPoint(i))
c.Assert(err, Equals, nil)
}
peer = &Peer{connectors: ctors, BaseNode: n.BaseNode}
//peer.commsPubKey = n.GetCommsPublicKey()
//peer.sigPubKey = n.GetSigPublicKey()
return peer
}
// Creates a local (127.0.0.1) endPoint and adds it to the node.
func (s *XLSuite) makeAnEndPoint(c *C, node *Node) {
addr := fmt.Sprintf("127.0.0.1:0")
ep, err := xt.NewTcpEndPoint(addr)
c.Assert(err, IsNil)
c.Assert(ep, Not(IsNil))
ndx, err := node.AddEndPoint(ep)
c.Assert(err, IsNil)
c.Assert(ndx, Equals, 0) // it's the only one
}
func (s *XLSuite) TestNodeSerialization(c *C) {
if VERBOSITY > 0 {
fmt.Println("TEST_NODE_SERIALIZATION")
}
if VERBOSITY > 1 {
was := runtime.GOMAXPROCS(MY_MAX_PROC)
fmt.Printf("GOMAXPROCS was %d, has been reset to %d\n",
was, MY_MAX_PROC)
}
rng := xr.MakeSimpleRNG()
node := s.makeHost(c, rng)
s.makeAnEndPoint(c, node)
// SEGUE I: TEST Get/setLFS()
currentLFS := node.GetLFS()
// This does not follow our standard practice of using the nodeID
// as the name of the local file system directory.
lfs := "tmp/" + rng.NextFileName(4)
err := node.setLFS(lfs)
c.Assert(err, IsNil)
c.Assert(node.GetLFS(), Equals, lfs)
// restore old LFS
err = node.setLFS(currentLFS)
c.Assert(err, IsNil)
c.Assert(node.GetLFS(), Equals, currentLFS)
err = node.OpenAcc()
c.Assert(err, IsNil)
defer node.CloseAcc() // any error ignored
const K = 3
peers := make([]*Peer, K)
for i := 0; i < K; i++ {
host := s.makeHost(c, rng)
s.makeAnEndPoint(c, host)
peers[i] = s.peerFromHost(c, host)
ndx, err := node.AddPeer(peers[i])
c.Assert(err, IsNil)
c.Assert(ndx, Equals, i)
}
// we now have a node with K peers
serialized := node.String()
// SEGUE II: verify that FindPeer works
for i := 0; i < K; i++ {
id := peers[i].GetNodeID().Value()
p, err := node.FindPeer(id)
c.Assert(err, IsNil)
c.Assert(p, Not(IsNil))
c.Assert(p.Equal(peers[i]), Equals, true)
}
// closes all acceptors
err = node.CloseAcc()
c.Assert(err, IsNil)
c.Assert(node.running, Equals, false)
// XXX parse succeeds if we sleep 100ms, fails if we sleep 10ms
// time.Sleep(70 * time.Millisecond)
// DEBUG
fmt.Printf("SERIALIZED:\n%s\n", serialized)
// END
backAgain, rest, err := Parse(serialized)
c.Assert(err, IsNil)
c.Assert(len(rest), Equals, 0)
c.Assert(backAgain.running, Equals, false)
err = backAgain.OpenAcc()
c.Assert(err, IsNil)
defer backAgain.CloseAcc()
reserialized := backAgain.String()
c.Assert(reserialized, Equals, serialized)
}