-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconfig_test.go
323 lines (277 loc) · 10.7 KB
/
config_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package app
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.dedis.ch/kyber/v3/pairing"
"go.dedis.ch/kyber/v3/suites"
"go.dedis.ch/onet/v3"
"go.dedis.ch/onet/v3/log"
"go.dedis.ch/onet/v3/network"
)
var o bytes.Buffer
const testServiceName = "OnetConfigTestService"
func registerService() {
onet.RegisterNewServiceWithSuite(testServiceName, pairing.NewSuiteBn256(), func(c *onet.Context) (onet.Service, error) {
return nil, nil
})
}
func unregisterService() {
onet.UnregisterService(testServiceName)
}
func TestMain(m *testing.M) {
out = &o
log.MainTest(m)
}
var serverGroup = `Description = "Default Dedis Cothority"
[[servers]]
Address = "tcp://5.135.161.91:2000"
Public = "94b8255379e11df5167b8a7ae3b85f7e7eb5f13894abee85bd31b3270f1e4c65"
Description = "Nikkolasg's server: spreading the love of singing"
[servers.Services]
[servers.Services.OnetConfigTestService]
Suite = "bn256.adapter"
Public = "593c700babf825b6056a2339ce437f73f717226a77d618a5e8f0251c00273b38557c3cda8dbde5431d062804275f8757a2c942d888ac09f2df34f806e35e660a3c6f13dc64a7cf112865807450ccbd9f75bb3aadb98599f7034cf377a9b976045df374f840e9ee617631257fc9611def6c7c2e5cf23f5ab36cf72f68f14b6686"
[servers.Services.abc]
Suite = "Ed25519"
Public = "94b8255379e11df5167b8a7ae3b85f7e7eb5f13894abee85bd31b3270f1e4c65"
[[servers]]
Address = "tcp://185.26.156.40:61117"
Suite = "Ed25519"
Public = "6a921638a4ade8970ebcd9e371570f08d71a24987f90f12391b9f6c525be5be4"
Description = "Ismail's server"
URL = "https://ismail.example.com/conode"
`
func TestReadGroupDescToml(t *testing.T) {
registerService()
defer unregisterService()
group, err := ReadGroupDescToml(strings.NewReader(serverGroup))
if err != nil {
t.Fatal(err)
}
if len(group.Roster.List) != 2 {
t.Fatal("Should have 2 ServerIdentities")
}
nikkoAddr := group.Roster.List[0].Address
if !nikkoAddr.Valid() || nikkoAddr != network.NewTCPAddress("5.135.161.91:2000") {
t.Fatal("Address not valid " + group.Roster.List[0].Address.String())
}
if len(group.Description) != 2 {
t.Fatal("Should have 2 descriptions")
}
if group.Description[group.Roster.List[1]] != "Ismail's server" {
t.Fatal("This should be Ismail's server")
}
if group.Roster.List[1].URL != "https://ismail.example.com/conode" {
t.Fatal("Did not find expected URL.")
}
require.Equal(t, 1, len(group.Roster.List[0].ServiceIdentities))
require.Equal(t, "bn256.adapter", group.Roster.List[0].ServiceIdentities[0].Suite)
}
// TestReadGroupWithWrongSuite checks if an error is returned when the wrong suite
// is used in the service configuration
func TestReadGroupWithWrongSuite(t *testing.T) {
registerService()
defer unregisterService()
const group = `
[[servers]]
Address = "tcp://5.135.161.91:2000"
Public = "94b8255379e11df5167b8a7ae3b85f7e7eb5f13894abee85bd31b3270f1e4c65"
Description = "Nikkolasg's server: spreading the love of singing"
[servers.Services]
[servers.Services.OnetConfigTestService]
Suite = "fake_name"
Public = ""
`
require.Panics(t, func() { ReadGroupDescToml(strings.NewReader(group)) })
}
// TestSaveGroup checks that the group is correctly written into the file
func TestSaveGroup(t *testing.T) {
registerService()
defer unregisterService()
group, err := ReadGroupDescToml(strings.NewReader(serverGroup))
require.NoError(t, err)
tmp, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(tmp)
filename := path.Join(tmp, "public.toml")
suite := suites.MustFind("ed25519")
err = group.Save(suite, filename)
require.NoError(t, err)
data, err := ioutil.ReadFile(filename)
require.NoError(t, err)
fmt.Print(string(data))
require.Contains(t, string(data), serverGroup[strings.LastIndex(serverGroup, "[[servers]]"):])
}
func TestParseCothority(t *testing.T) {
registerService()
defer unregisterService()
suite := "Ed25519"
public := "6a921638a4ade8970ebcd9e371570f08d71a24987f90f12391b9f6c525be5be4"
private := "6a921638a4ade8970ebcd9e371570f08d71a24987f90f12391b9f6c525be5be4"
address := "tcp://1.2.3.4:1234"
listenAddr := "127.0.0.1:0"
description := "This is a description."
scPublic := "593c700babf825b6056a2339ce437f73f717226a77d618a5e8f0251c00273b38557c3cda8dbde5431d062804275f8757a2c942d888ac09f2df34f806e35e660a3c6f13dc64a7cf112865807450ccbd9f75bb3aadb98599f7034cf377a9b976045df374f840e9ee617631257fc9611def6c7c2e5cf23f5ab36cf72f68f14b6686"
scPrivate := "622f20fbc7995dd48bab00b0f3d7d13220a9d71716c6be7a45b4b284836041a8"
privateInfo := fmt.Sprintf(`Suite = "%s"
Public = "%s"
Private = "%s"
Address = "%s"
ListenAddress = "%s"
Description = "%s"
[services]
[services.%s]
suite = "bn256.adapter"
public = "%s"
private = "%s"
[services.abc]
suite = "Ed25519"
public = "6a921638a4ade8970ebcd9e371570f08d71a24987f90f12391b9f6c525be5be4"`,
suite, public, private, address, listenAddr,
description, testServiceName, scPublic, scPrivate)
privateToml, err := ioutil.TempFile("", "temp_private.toml")
require.Nil(t, err)
privateToml.WriteString(privateInfo)
privateToml.Close()
cothConfig, srv, err := ParseCothority(privateToml.Name())
require.Nil(t, err)
// Check basic information
require.Equal(t, suite, cothConfig.Suite)
require.Equal(t, public, cothConfig.Public)
require.Equal(t, private, cothConfig.Private)
require.Equal(t, address, cothConfig.Address.String())
require.Equal(t, listenAddr, cothConfig.ListenAddress)
require.Equal(t, description, cothConfig.Description)
require.Equal(t, 1, len(srv.ServerIdentity.ServiceIdentities))
require.Equal(t, "bn256.adapter", cothConfig.Services[testServiceName].Suite)
require.Equal(t, scPublic, cothConfig.Services[testServiceName].Public)
require.Equal(t, scPrivate, cothConfig.Services[testServiceName].Private)
srv.Close()
}
func TestParseCothorityWithTLSWebSocket(t *testing.T) {
suite := "Ed25519"
public := "6a921638a4ade8970ebcd9e371570f08d71a24987f90f12391b9f6c525be5be4"
private := "6a921638a4ade8970ebcd9e371570f08d71a24987f90f12391b9f6c525be5be4"
address := "tcp://1.2.3.4:1234"
listenAddr := "127.0.0.1:0"
description := "This is a description."
// Certificate and key examples taken from
// 'https://gist.github.com/blinksmith/579b2650a09f128a03ca'
wsTLSCert := `-----BEGIN CERTIFICATE-----
MIICEzCCAXygAwIBAgIQMIMChMLGrR+QvmQvpwAU6zANBgkqhkiG9w0BAQsFADAS
MRAwDgYDVQQKEwdBY21lIENvMCAXDTcwMDEwMTAwMDAwMFoYDzIwODQwMTI5MTYw
MDAwWjASMRAwDgYDVQQKEwdBY21lIENvMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB
iQKBgQDuLnQAI3mDgey3VBzWnB2L39JUU4txjeVE6myuDqkM/uGlfjb9SjY1bIw4
iA5sBBZzHi3z0h1YV8QPuxEbi4nW91IJm2gsvvZhIrCHS3l6afab4pZ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cPg/Fvyt0Xlp/DoCzjA0CQQDU
y2ptGsuSmgUtWj3NM9xuwYPm+Z/F84K6+ARYiZ6PYj013sovGKUFfYAqVXVlxtIX
qyUBnu3X9ps8ZfjLZO7BAkEAlT4R5Yl6cGhaJQYZHOde3JEMhNRcVFMO8dJDaFeo
f9Oeos0UUothgiDktdQHxdNEwLjQf7lJJBzV+5OtwswCWA==
-----END RSA PRIVATE KEY-----`
// Write files containing cert and key (+ be sure to delete them at the end)
certFile, err := ioutil.TempFile("", "temp_cert.pem")
defer func() {
err := os.Remove(certFile.Name())
require.Nil(t, err)
}()
require.Nil(t, err)
certFile.WriteString(wsTLSCert)
certFile.Close()
keyFile, err := ioutil.TempFile("", "temp_key.pem")
defer func() {
err := os.Remove(keyFile.Name())
require.Nil(t, err)
}()
require.Nil(t, err)
keyFile.WriteString(wsTLSCertKey)
keyFile.Close()
// Testing different ways of putting TLS info.
privateInfos := []string{
fmt.Sprintf(`Suite = "%s"
Public = "%s"
Private = "%s"
Address = "%s"
ListenAddress = "%s"
Description = "%s"
WebSocketTLSCertificate = """string://%s"""
WebSocketTLSCertificateKey = """string://%s"""`,
suite, public, private, address, listenAddr,
description, wsTLSCert, wsTLSCertKey),
fmt.Sprintf(`Suite = "%s"
Public = "%s"
Private = "%s"
Address = "%s"
ListenAddress = "%s"
Description = "%s"
WebSocketTLSCertificate = "file://%s"
WebSocketTLSCertificateKey = "file://%s"`,
suite, public, private, address, listenAddr,
description, certFile.Name(), keyFile.Name()),
fmt.Sprintf(`Suite = "%s"
Public = "%s"
Private = "%s"
Address = "%s"
ListenAddress = "%s"
Description = "%s"
WebSocketTLSCertificate = "%s"
WebSocketTLSCertificateKey = "%s"`,
suite, public, private, address, listenAddr,
description, certFile.Name(), keyFile.Name()),
}
for i, privateInfo := range privateInfos {
privateToml, err := ioutil.TempFile("", "temp_private.toml")
require.Nil(t, err)
privateToml.WriteString(privateInfo)
privateToml.Close()
cothConfig, srv, err := ParseCothority(privateToml.Name())
require.Nil(t, err)
// Check basic information
require.Equal(t, suite, cothConfig.Suite)
require.Equal(t, public, cothConfig.Public)
require.Equal(t, private, cothConfig.Private)
require.Equal(t, address, cothConfig.Address.String())
require.Equal(t, listenAddr, cothConfig.ListenAddress)
require.Equal(t, description, cothConfig.Description)
// Check content of certificate and key
certContent, err := cothConfig.WebSocketTLSCertificate.Content()
require.Nil(t, err)
require.Equal(t, wsTLSCert, string(certContent))
keyContent, err := cothConfig.WebSocketTLSCertificateKey.Content()
require.Nil(t, err)
require.Equal(t, wsTLSCertKey, string(keyContent))
if i != 0 {
// Check when the certificate is a file.
require.NotNil(t, srv.WebSocket.TLSConfig.GetCertificate)
cert, err := srv.WebSocket.TLSConfig.GetCertificate(nil)
require.NoError(t, err)
require.NotNil(t, cert)
}
srv.Close()
err = os.Remove(privateToml.Name())
require.Nil(t, err)
}
}