-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathserver_test.go
121 lines (97 loc) · 3.14 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
113
114
115
116
117
118
119
120
121
package onet
import (
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
bbolt "go.etcd.io/bbolt"
)
func TestServer_ProtocolRegisterName(t *testing.T) {
c := NewLocalServer(tSuite, 0)
defer c.Close()
plen := len(c.protocols.instantiators)
require.True(t, plen > 0)
id, err := c.ProtocolRegister("ServerProtocol", NewServerProtocol)
log.ErrFatal(err)
require.NotNil(t, id)
require.True(t, plen < len(c.protocols.instantiators))
_, err = c.protocolInstantiate(ProtocolID(uuid.Nil), nil)
require.NotNil(t, err)
// Test for not overwriting
_, err = c.ProtocolRegister("ServerProtocol", NewServerProtocol2)
require.NotNil(t, err)
}
func TestServer_GetService(t *testing.T) {
c := NewLocalServer(tSuite, 0)
defer c.Close()
s := c.Service("nil")
require.Nil(t, s)
}
func TestServer_Database(t *testing.T) {
c := NewLocalServer(tSuite, 0)
require.NotNil(t, c.serviceManager.db)
for _, s := range c.serviceManager.availableServices() {
c.serviceManager.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket([]byte(s))
require.NotNil(t, b)
return nil
})
}
c.Close()
}
func TestServer_FilterConnectionsIncomingInvalid(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
srv := local.GenServers(3)
msg := &SimpleMessage{42}
testPeersID := network.NewPeerSetID([]byte{})
// Set the valid peers of Srv0 to Srv1
validPeers0 := []*network.ServerIdentity{srv[1].ServerIdentity}
srv[0].SetValidPeers(testPeersID, validPeers0)
// Set the valid peers of Srv1 to Srv2
validPeers1 := []*network.ServerIdentity{srv[2].ServerIdentity}
srv[1].SetValidPeers(testPeersID, validPeers1)
// Srv0 can send to Srv1, but Srv1 cannot receive from Srv0
log.OutputToBuf()
defer log.OutputToOs()
srv[0].Send(srv[1].ServerIdentity, msg)
time.Sleep(500 * time.Millisecond)
// An error was logged
require.Regexp(t, "rejecting incoming connection.*invalid peer", log.GetStdErr())
}
func TestServer_FilterConnectionsIncomingValid(t *testing.T) {
local := NewTCPTest(tSuite)
defer local.CloseAll()
srv := local.GenServers(3)
msg := &SimpleMessage{42}
testPeersID := network.NewPeerSetID([]byte{})
// Set the valid peers of Srv0 to Srv1
validPeers0 := []*network.ServerIdentity{srv[1].ServerIdentity}
srv[0].SetValidPeers(testPeersID, validPeers0)
// Set the valid peers of Srv1 to Srv0
validPeers1 := []*network.ServerIdentity{srv[0].ServerIdentity}
srv[1].SetValidPeers(testPeersID, validPeers1)
// Srv1 can receive from Srv0
log.OutputToBuf()
defer log.OutputToOs()
srv[0].Send(srv[1].ServerIdentity, msg)
time.Sleep(500 * time.Millisecond)
// No error was logged
require.Empty(t, log.GetStdErr())
}
type ServerProtocol struct {
*TreeNodeInstance
}
// NewExampleHandlers initialises the structure for use in one round
func NewServerProtocol(n *TreeNodeInstance) (ProtocolInstance, error) {
return &ServerProtocol{n}, nil
}
// NewExampleHandlers initialises the structure for use in one round
func NewServerProtocol2(n *TreeNodeInstance) (ProtocolInstance, error) {
return &ServerProtocol{n}, nil
}
func (cp *ServerProtocol) Start() error {
return nil
}