go.dedis.ch/onet/v3@v3.2.11-0.20210930124529-e36530bca7ef/server_test.go (about) 1 package onet 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/google/uuid" 8 "github.com/stretchr/testify/require" 9 "go.dedis.ch/onet/v3/log" 10 "go.dedis.ch/onet/v3/network" 11 bbolt "go.etcd.io/bbolt" 12 ) 13 14 func TestServer_ProtocolRegisterName(t *testing.T) { 15 c := NewLocalServer(tSuite, 0) 16 defer c.Close() 17 plen := len(c.protocols.instantiators) 18 require.True(t, plen > 0) 19 id, err := c.ProtocolRegister("ServerProtocol", NewServerProtocol) 20 log.ErrFatal(err) 21 require.NotNil(t, id) 22 require.True(t, plen < len(c.protocols.instantiators)) 23 _, err = c.protocolInstantiate(ProtocolID(uuid.Nil), nil) 24 require.NotNil(t, err) 25 // Test for not overwriting 26 _, err = c.ProtocolRegister("ServerProtocol", NewServerProtocol2) 27 require.NotNil(t, err) 28 } 29 30 func TestServer_GetService(t *testing.T) { 31 c := NewLocalServer(tSuite, 0) 32 defer c.Close() 33 s := c.Service("nil") 34 require.Nil(t, s) 35 } 36 37 func TestServer_Database(t *testing.T) { 38 c := NewLocalServer(tSuite, 0) 39 require.NotNil(t, c.serviceManager.db) 40 41 for _, s := range c.serviceManager.availableServices() { 42 c.serviceManager.db.Update(func(tx *bbolt.Tx) error { 43 b := tx.Bucket([]byte(s)) 44 require.NotNil(t, b) 45 return nil 46 }) 47 } 48 c.Close() 49 } 50 51 func TestServer_FilterConnectionsIncomingInvalid(t *testing.T) { 52 local := NewTCPTest(tSuite) 53 defer local.CloseAll() 54 55 srv := local.GenServers(3) 56 msg := &SimpleMessage{42} 57 58 testPeersID := network.NewPeerSetID([]byte{}) 59 60 // Set the valid peers of Srv0 to Srv1 61 validPeers0 := []*network.ServerIdentity{srv[1].ServerIdentity} 62 srv[0].SetValidPeers(testPeersID, validPeers0) 63 // Set the valid peers of Srv1 to Srv2 64 validPeers1 := []*network.ServerIdentity{srv[2].ServerIdentity} 65 srv[1].SetValidPeers(testPeersID, validPeers1) 66 67 // Srv0 can send to Srv1, but Srv1 cannot receive from Srv0 68 log.OutputToBuf() 69 defer log.OutputToOs() 70 71 srv[0].Send(srv[1].ServerIdentity, msg) 72 time.Sleep(500 * time.Millisecond) 73 74 // An error was logged 75 require.Regexp(t, "rejecting incoming connection.*invalid peer", log.GetStdErr()) 76 } 77 78 func TestServer_FilterConnectionsIncomingValid(t *testing.T) { 79 local := NewTCPTest(tSuite) 80 defer local.CloseAll() 81 82 srv := local.GenServers(3) 83 msg := &SimpleMessage{42} 84 85 testPeersID := network.NewPeerSetID([]byte{}) 86 87 // Set the valid peers of Srv0 to Srv1 88 validPeers0 := []*network.ServerIdentity{srv[1].ServerIdentity} 89 srv[0].SetValidPeers(testPeersID, validPeers0) 90 // Set the valid peers of Srv1 to Srv0 91 validPeers1 := []*network.ServerIdentity{srv[0].ServerIdentity} 92 srv[1].SetValidPeers(testPeersID, validPeers1) 93 94 // Srv1 can receive from Srv0 95 log.OutputToBuf() 96 defer log.OutputToOs() 97 98 srv[0].Send(srv[1].ServerIdentity, msg) 99 time.Sleep(500 * time.Millisecond) 100 101 // No error was logged 102 require.Empty(t, log.GetStdErr()) 103 } 104 105 type ServerProtocol struct { 106 *TreeNodeInstance 107 } 108 109 // NewExampleHandlers initialises the structure for use in one round 110 func NewServerProtocol(n *TreeNodeInstance) (ProtocolInstance, error) { 111 return &ServerProtocol{n}, nil 112 } 113 114 // NewExampleHandlers initialises the structure for use in one round 115 func NewServerProtocol2(n *TreeNodeInstance) (ProtocolInstance, error) { 116 return &ServerProtocol{n}, nil 117 } 118 119 func (cp *ServerProtocol) Start() error { 120 return nil 121 }