forked from dedis/onet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
159 lines (136 loc) · 3.51 KB
/
context_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
package onet
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"sync"
"testing"
bolt "github.com/coreos/bbolt"
"github.com/dedis/kyber/util/key"
"github.com/dedis/onet/log"
"github.com/dedis/onet/network"
"github.com/stretchr/testify/require"
)
type ContextData struct {
I int
S string
}
func TestContextSaveLoad(t *testing.T) {
tmp, err := ioutil.TempDir("", "conode")
defer os.RemoveAll(tmp)
os.Setenv("CONODE_SERVICE_PATH", tmp)
p := dbPathFromEnv()
require.Equal(t, p, tmp)
nbr := 10
c := make([]*Context, nbr)
for i := range c {
c[i] = createContext(t, p)
}
testSaveFailure(t, c[0])
var wg sync.WaitGroup
wg.Add(nbr)
for i := range c {
go func(i int) {
testLoadSave(t, c[i])
wg.Done()
}(i)
}
wg.Wait()
files, err := ioutil.ReadDir(tmp)
log.ErrFatal(err)
require.False(t, files[0].IsDir())
require.True(t, files[0].Mode().IsRegular())
require.True(t, strings.HasSuffix(files[0].Name(), ".db"))
}
func testLoadSave(t *testing.T, c *Context) {
key := []byte("test")
cd := &ContextData{42, "meaning of life"}
network.RegisterMessage(ContextData{})
require.Nil(t, c.Save(key, cd))
msg, err := c.Load(append(key, byte('_')))
if err != nil || msg != nil {
log.Fatal("this should not exist")
}
cdInt, err := c.Load(key)
require.Nil(t, err)
cd2, ok := cdInt.(*ContextData)
if !ok {
log.Fatal("contextData should exist")
}
if cd.I != cd2.I || cd.S != cd2.S {
log.Fatal("stored and loaded data should be equal", cd, cd2)
}
}
func testSaveFailure(t *testing.T, c *Context) {
key := []byte("test")
cd := &ContextData{42, "meaning of life"}
// should fail because ContextData is not registered
if c.Save(key, cd) == nil {
log.Fatal("Save should fail")
}
}
func TestContext_GetAdditionalBucket(t *testing.T) {
tmp, err := ioutil.TempDir("", "conode")
log.ErrFatal(err)
defer os.RemoveAll(tmp)
c := createContext(t, tmp)
db, name := c.GetAdditionalBucket([]byte("new"))
require.NotNil(t, db)
require.Equal(t, []byte("testService_new"), name)
// Need to accept a second run with an existing bucket
db, name = c.GetAdditionalBucket([]byte("new"))
require.NotNil(t, db)
require.Equal(t, []byte("testService_new"), name)
}
func TestContext_Path(t *testing.T) {
tmp, err := ioutil.TempDir("", "conode")
log.ErrFatal(err)
defer os.RemoveAll(tmp)
c := createContext(t, tmp)
pub, _ := c.ServerIdentity().Public.MarshalBinary()
dbPath := path.Join(tmp, fmt.Sprintf("%x.db", pub))
_, err = os.Stat(dbPath)
if err != nil {
t.Error(err)
}
os.Remove(dbPath)
tmp, err = ioutil.TempDir("", "conode")
log.ErrFatal(err)
defer os.RemoveAll(tmp)
c = createContext(t, tmp)
_, err = os.Stat(tmp)
log.ErrFatal(err)
pub, _ = c.ServerIdentity().Public.MarshalBinary()
_, err = os.Stat(path.Join(tmp, fmt.Sprintf("%x.db", pub)))
log.ErrFatal(err)
}
// createContext creates the minimum number of things required for the test
func createContext(t *testing.T, dbPath string) *Context {
kp := key.NewKeyPair(tSuite)
si := network.NewServerIdentity(kp.Public,
network.NewAddress(network.Local, "localhost:0"))
cn := &Server{
Router: &network.Router{
ServerIdentity: si,
},
}
name := "testService"
RegisterNewService(name, func(c *Context) (Service, error) {
return nil, nil
})
sm := &serviceManager{
server: cn,
dbPath: dbPath,
}
db, err := openDb(sm.dbFileName())
require.Nil(t, err)
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucket([]byte(name))
return err
})
require.Nil(t, err)
sm.db = db
return newContext(cn, nil, ServiceFactory.ServiceID(name), sm)
}