forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation_test.go
137 lines (125 loc) · 3.04 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
package onet
import (
"errors"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
"time"
"github.com/dedis/onet/log"
)
func TestSimulationBF(t *testing.T) {
sc, _, err := createBFTree(7, 2, []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, []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, []string{"test1", "test2"})
if err != nil {
t.Fatal(err)
}
}
}
func TestSimulationLoadSave(t *testing.T) {
sc, _, err := createBFTree(7, 2, []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")
}
closeAll(sc2)
}
func TestSimulationMultipleInstances(t *testing.T) {
sc, _, err := createBFTree(7, 2, []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)
}
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, addresses []string) (*SimulationConfig, *SimulationBFTree, error) {
sc := &SimulationConfig{}
sb := &SimulationBFTree{
Hosts: hosts,
BF: bf,
Suite: "Ed25519",
}
sb.CreateRoster(sc, addresses, 2000)
if len(sc.Roster.List) != hosts {
return nil, nil, errors.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, errors.New("Tree isn't " + strconv.Itoa(bf) + "-ary")
}
return sc, sb, nil
}