-
Notifications
You must be signed in to change notification settings - Fork 29
/
simulation_test.go
165 lines (147 loc) · 4.03 KB
/
simulation_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
package onet
import (
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v3/suites"
"go.dedis.ch/onet/v3/log"
"golang.org/x/xerrors"
)
var pairingSuite = suites.MustFind("bn256.adapter")
func registerService() {
RegisterNewServiceWithSuite("simulationTestService", tSuite, func(c *Context) (Service, error) {
return nil, nil
})
RegisterNewServiceWithSuite("simulationTestService2", pairingSuite, func(c *Context) (Service, error) {
return nil, nil
})
}
func unregisterService() {
UnregisterService("simulationTestService")
UnregisterService("simulationTestService2")
}
func TestSimulationBF(t *testing.T) {
sc, _, err := createBFTree(7, 2, false, []string{"test1", "test2"})
if err != nil {
t.Fatal(err)
}
addresses := []string{
"test1:2000",
"test2:2000",
"test1:2002",
"test2:2002",
"test1:2004",
"test2:2004",
"test1:2006",
}
for i, a := range sc.Roster.List {
if !strings.Contains(string(a.Address), addresses[i]) {
t.Fatal("Address", string(a.Address), "should be", addresses[i])
}
}
if !sc.Tree.IsBinary(sc.Tree.Root) {
t.Fatal("Created tree is not binary")
}
sc, _, err = createBFTree(13, 3, false, []string{"test1", "test2"})
if err != nil {
t.Fatal(err)
}
if len(sc.Tree.Root.Children) != 3 {
t.Fatal("Branching-factor 3 tree has not 3 children")
}
if !sc.Tree.IsNary(sc.Tree.Root, 3) {
t.Fatal("Created tree is not binary")
}
}
func TestSimulationBigTree(t *testing.T) {
if testing.Short() {
t.Skip()
}
for i := uint(4); i < 8; i++ {
_, _, err := createBFTree(1<<i-1, 2, false, []string{"test1", "test2"})
require.Nil(t, err)
}
}
func TestSimulationLoadSave(t *testing.T) {
registerService()
defer unregisterService()
sc, _, err := createBFTree(7, 2, false, []string{"127.0.0.1", "127.0.0.2"})
if err != nil {
t.Fatal(err)
}
dir, err := ioutil.TempDir("", "example")
log.ErrFatal(err)
defer os.RemoveAll(dir)
sc.Save(dir)
sc2, err := LoadSimulationConfig("Ed25519", dir, sc.Roster.List[0].Address.NetworkAddress())
if err != nil {
t.Fatal(err)
}
if !sc2[0].Tree.ID.Equal(sc.Tree.ID) {
t.Fatal("Tree-id is not correct")
}
for key, privKeys := range sc.PrivateKeys {
require.Equal(t, privKeys.Private.String(), sc2[0].PrivateKeys[key].Private.String())
require.Equal(t, 2, len(sc2[0].PrivateKeys[key].Services))
require.Equal(t, privKeys.Services[0].String(), sc2[0].PrivateKeys[key].Services[0].String())
require.Equal(t, privKeys.Services[1].String(), sc2[0].PrivateKeys[key].Services[1].String())
}
closeAll(sc2)
}
func TestSimulationMultipleInstances(t *testing.T) {
sc, _, err := createBFTree(7, 2, false, []string{"127.0.0.1", "127.0.0.2"})
if err != nil {
t.Fatal(err)
}
dir, err := ioutil.TempDir("", "example")
log.ErrFatal(err)
defer os.RemoveAll(dir)
sc.Save(dir)
sc2, err := LoadSimulationConfig("Ed25519", dir, sc.Roster.List[0].Address.Host())
if err != nil {
t.Fatal(err)
}
defer closeAll(sc2)
if len(sc2) != 4 {
t.Fatal("We should have 4 local1-hosts but have", len(sc2))
}
if sc2[0].Server.ServerIdentity.ID.Equal(sc2[1].Server.ServerIdentity.ID) {
t.Fatal("Hosts are not copies")
}
}
func closeAll(scs []*SimulationConfig) {
for _, s := range scs {
if err := s.Server.Close(); err != nil {
log.Error("Error closing host", s.Server.ServerIdentity, err)
}
for s.Server.Router.Listening() {
log.Lvl2("Sleeping while waiting for router to be closed")
time.Sleep(20 * time.Millisecond)
}
}
}
func createBFTree(hosts, bf int, tls bool, addresses []string) (*SimulationConfig, *SimulationBFTree, error) {
sc := &SimulationConfig{}
sb := &SimulationBFTree{
Hosts: hosts,
BF: bf,
Suite: "Ed25519",
TLS: tls,
}
sb.CreateRoster(sc, addresses, 2000)
if len(sc.Roster.List) != hosts {
return nil, nil, xerrors.New("Didn't get correct number of entities")
}
err := sb.CreateTree(sc)
if err != nil {
return nil, nil, err
}
if !sc.Tree.IsNary(sc.Tree.Root, bf) {
return nil, nil, xerrors.New("Tree isn't " + strconv.Itoa(bf) + "-ary")
}
return sc, sb, nil
}