-
Notifications
You must be signed in to change notification settings - Fork 3
/
config_test.go
482 lines (416 loc) · 13.5 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package config
import (
"context"
"math/rand"
"path"
"testing"
"time"
"github.com/TheCacophonyProject/go-config/configtest"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/wawandco/fako"
)
func TestDefaults(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
location := DefaultWindowLocation()
assert.NoError(t, conf.Unmarshal(LocationKey, &location))
assert.Equal(t, DefaultWindowLocation(), location)
}
func TestLoadingPublicKeys(t *testing.T) {
defer newFs(t, "./test-files/test.toml")()
c, err := New(DefaultConfigDir)
require.NoError(t, err)
_, err = c.GetAllValues()
require.NoError(t, err)
}
func TestReadingConfigInDir(t *testing.T) {
defer newFs(t, "./test-files/test.toml")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
var device Device
deviceChanges := Device{}
deviceChanges.ID = 789
assert.NoError(t, conf.Unmarshal(DeviceKey, &device))
assert.Equal(t, deviceChanges, device)
windows := DefaultWindows()
windowChanges := windows
windowChanges.PowerOff = "+1s"
assert.NoError(t, conf.Unmarshal(WindowsKey, &windows))
assert.Equal(t, windowChanges, windows)
var location Location
locationChanges := Location{}
locationChanges.Accuracy = 543
assert.NoError(t, conf.Unmarshal(LocationKey, &location))
assert.Equal(t, locationChanges, location)
windowLocation := DefaultWindowLocation()
windowLocationChanges := DefaultWindowLocation()
windowLocationChanges.Accuracy = 543
assert.NoError(t, conf.Unmarshal(LocationKey, &windowLocation))
assert.Equal(t, windowLocationChanges, windowLocation)
testHosts := DefaultTestHosts()
testHostsChanges := DefaultTestHosts()
testHostsChanges.PingRetries = 333
assert.NoError(t, conf.Unmarshal(TestHostsKey, &testHosts))
assert.Equal(t, testHostsChanges, testHosts)
var battery Battery
batteryChanges := Battery{}
assert.NoError(t, conf.Unmarshal(BatteryKey, &battery))
assert.Equal(t, batteryChanges, battery)
modemd := DefaultModemd()
modemdChanges := DefaultModemd()
modemdChanges.ConnectionTimeout = time.Second * 21
assert.NoError(t, conf.Unmarshal(ModemdKey, &modemd))
assert.Equal(t, modemdChanges, modemd)
lepton := DefaultLepton()
leptonChanges := DefaultLepton()
leptonChanges.SPISpeed = 2
assert.NoError(t, conf.Unmarshal(LeptonKey, &lepton))
assert.Equal(t, leptonChanges, lepton)
thermalRecorder := DefaultThermalRecorder()
thermalRecorderChanges := thermalRecorder
thermalRecorderChanges.MaxSecs = 321
assert.NoError(t, conf.Unmarshal(ThermalRecorderKey, &thermalRecorder))
assert.Equal(t, thermalRecorderChanges, thermalRecorder)
thermalThrottler := DefaultThermalThrottler()
thermalThrottlerChanges := DefaultThermalThrottler()
thermalThrottlerChanges.BucketSize = time.Second * 16
assert.NoError(t, conf.Unmarshal(ThermalThrottlerKey, &thermalThrottler))
assert.Equal(t, thermalThrottlerChanges, thermalThrottler)
ports := DefaultPorts()
portsChanges := DefaultPorts()
portsChanges.Managementd = 3
assert.NoError(t, conf.Unmarshal(PortsKey, &ports))
assert.Equal(t, portsChanges, ports)
var secrets Secrets
secretsChanges := Secrets{}
secretsChanges.DevicePassword = "pass"
assert.NoError(t, conf.Unmarshal(SecretsKey, &secrets))
assert.Equal(t, secretsChanges, secrets)
thermalMotion := DefaultThermalMotion("")
thermalMotionChanges := DefaultThermalMotion("")
thermalMotionChanges.TempThresh = 398
assert.NoError(t, conf.Unmarshal(ThermalMotionKey, &thermalMotion))
assert.Equal(t, thermalMotionChanges, thermalMotion)
audio := DefaultAudioBait()
audioChanges := DefaultAudioBait()
audioChanges.Card = 1
assert.NoError(t, conf.Unmarshal(AudioBaitKey, &audio))
assert.Equal(t, audioChanges, audio)
audioRec := DefaultAudioRecording()
audioRecChanges := DefaultAudioRecording()
audioRecChanges.AudioMode = "AudioOnly"
assert.NoError(t, conf.Unmarshal(AudioRecordingKey, &audioRec))
assert.Equal(t, audioRecChanges, audioRec)
setup := DefaultDeviceSetup()
setupChanges := DefaultDeviceSetup()
assert.NoError(t, conf.Unmarshal(DeviceSetupKey, &setup))
assert.Equal(t, setup, setupChanges)
salt := DefaultSalt()
saltChanges := DefaultSalt()
assert.NoError(t, conf.Unmarshal(SaltKey, &salt))
assert.Equal(t, salt, saltChanges)
}
func TestSettingInvalidKeys(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
w := randomWindows()
require.NoError(t, conf.Set(WindowsKey, w))
m := map[string]interface{}{
"invalid-key": "a value",
}
require.Error(t, conf.SetFromMap(WindowsKey, m, false))
require.NoError(t, conf.SetFromMap(WindowsKey, m, true))
require.Equal(t, "a value", conf.Get("windows.invalid-key"))
}
func TestWriting(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
conf2, err := New(DefaultConfigDir)
require.NoError(t, err)
d := randomDevice()
w := randomWindows()
l := randomLocation()
h := randomTestHosts()
require.NoError(t, conf.Set(DeviceKey, d))
require.NoError(t, conf2.Set(WindowsKey, w))
require.NoError(t, conf.Set(LocationKey, &l))
require.NoError(t, conf2.Set(TestHostsKey, &h))
conf, err = New(DefaultConfigDir)
require.NoError(t, err)
d2 := Device{}
require.NoError(t, conf.Unmarshal(DeviceKey, &d2))
w2 := Windows{}
require.NoError(t, conf.Unmarshal(WindowsKey, &w2))
l2 := Location{}
require.NoError(t, conf.Unmarshal(LocationKey, &l2))
h2 := TestHosts{}
require.NoError(t, conf.Unmarshal(TestHostsKey, &h2))
require.Equal(t, d, d2)
equalWindows(t, w, w2)
equalLocation(t, l, l2)
require.Equal(t, h, h2)
}
func TestClearSection(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
l := randomLocation()
w := randomWindows()
require.NoError(t, conf.Set(LocationKey, &l))
require.NoError(t, conf.Set(WindowsKey, &w))
require.NoError(t, conf.Unset(LocationKey+".latitude"))
require.Error(t, conf.Unset(LocationKey+".latitude.foo"))
require.NoError(t, conf.Unset(LocationKey+".bar"))
conf, err = New(DefaultConfigDir)
require.NoError(t, err)
l2 := Location{}
require.NoError(t, conf.Unmarshal(LocationKey, &l2))
w2 := Windows{}
require.NoError(t, conf.Unmarshal(WindowsKey, &w2))
l.Latitude = 0
equalLocation(t, l, l2)
equalWindows(t, w, w2)
}
func TestClear(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
l := randomLocation()
w := randomWindows()
require.NoError(t, conf.Set(LocationKey, &l))
require.NoError(t, conf.Set(WindowsKey, &w))
require.NoError(t, conf.Unset(LocationKey))
conf, err = New(DefaultConfigDir)
require.NoError(t, err)
l2 := Location{}
require.NoError(t, conf.Unmarshal(LocationKey, &l2))
w2 := Windows{}
require.NoError(t, conf.Unmarshal(WindowsKey, &w2))
equalLocation(t, Location{}, l2)
equalWindows(t, w, w2)
}
func TestFileLock(t *testing.T) {
defer newFs(t, "")()
lockTimeout = time.Millisecond * 100
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
conf2, err := New(DefaultConfigDir)
require.NoError(t, err)
require.NoError(t, conf.getFileLock())
require.Equal(t, context.DeadlineExceeded, conf2.getFileLock())
conf.fileLock.Unlock()
require.NoError(t, conf2.getFileLock())
}
func TestSettingUpdated(t *testing.T) {
defer newFs(t, "")()
newNow()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
require.NoError(t, conf.Set(DeviceKey, randomDevice()))
require.Equal(t, conf.Get(DeviceKey+".updated"), now())
}
func TestMapToLocation(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
newNow()
locationMap := map[string]interface{}{
"latitude": "123.321",
"timestamp": now().Format(TimeFormat),
}
locationExpected := Location{
Latitude: 123.321,
Timestamp: now(),
}
var location Location
require.NoError(t, conf.SetFromMap(LocationKey, locationMap, false))
require.NoError(t, conf.Unmarshal(LocationKey, &location))
equalLocation(t, locationExpected, location)
}
func TestNotWritingZeroValues(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
newNow()
locationMap := map[string]interface{}{
"lAtitUde": "123.321",
}
locationMapExpected := map[string]interface{}{
"latitude": float32(123.321),
"updated": now(),
}
require.NoError(t, conf.SetFromMap(LocationKey, locationMap, false))
require.Equal(t, locationMapExpected, (conf.v.AllSettings()[LocationKey]))
}
func TestMapToAudio(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
audioMap := map[string]interface{}{
"directory": "/audio/directory",
"card": "4",
"volume-control": "audio volume control",
}
audioExpected := AudioBait{
Dir: "/audio/directory",
Card: 4,
VolumeControl: "audio volume control",
}
checkWritingMap(t, AudioBaitKey, &AudioBait{}, &audioExpected, audioMap, conf)
}
func TestMapToBattery(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
batteryMap := map[string]interface{}{"enable-voltage-readings": "true"}
batteryExpected := Battery{EnableVoltageReadings: true}
checkWritingMap(t, BatteryKey, &Battery{}, &batteryExpected, batteryMap, conf)
}
func TestMapToDevice(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
deviceMap := map[string]interface{}{"Group": "a-group"}
deviceExpected := Device{Group: "a-group"}
checkWritingMap(t, DeviceKey, &Device{}, &deviceExpected, deviceMap, conf)
}
func TestMapToModemd(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
modemdMap := map[string]interface{}{
"test-interval": "10m4s",
"modems": []map[string]interface{}{{"name": "modem name"}},
}
modemdExpected := Modemd{
TestInterval: 10*time.Minute + 4*time.Second,
Modems: []Modem{{Name: "modem name"}},
}
checkWritingMap(t, ModemdKey, &Modemd{}, &modemdExpected, modemdMap, conf)
}
func TestSetField(t *testing.T) {
defer newFs(t, "")()
conf, err := New(DefaultConfigDir)
require.NoError(t, err)
audio := AudioBait{
Dir: "/audio/directory",
Card: 4,
VolumeControl: "audio volume control",
}
require.NoError(t, conf.Set(AudioBaitKey, audio))
require.NoError(t, conf.SetField(AudioBaitKey, "card", "5", false))
require.Error(t, conf.SetField(AudioBaitKey, "not-a-key", "5", false))
var audio2 AudioBait
require.NoError(t, conf.Unmarshal(AudioBaitKey, &audio2))
audioExpected := AudioBait{
Dir: "/audio/directory",
Card: 5,
VolumeControl: "audio volume control",
}
require.Equal(t, audioExpected, audio2)
}
func checkWritingMap(
t *testing.T,
key string,
s, expected interface{},
m map[string]interface{},
conf *Config,
) {
require.NoError(t, conf.SetFromMap(key, m, false))
require.NoError(t, conf.Unmarshal(key, s))
require.Equal(t, expected, s)
}
func newFs(t *testing.T, configFile string) func() {
writeEvents = false
fs := afero.NewMemMapFs()
SetFs(fs)
fsConfigFile := path.Join(DefaultConfigDir, ConfigFileName)
lockFileFunc, cleanupFunc := configtest.WriteConfigFromFile(t, configFile, fsConfigFile, fs)
SetLockFilePath(lockFileFunc)
return cleanupFunc
}
func newNow() {
n := time.Now()
now = func() time.Time {
return n
}
}
func randomDevice() (d Device) {
fako.Fuzz(&d)
return
}
func randomWindows() Windows {
// Repliates how the time is formatted in the config file
updated, err := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
if err != nil {
panic(err)
}
return Windows{
StartRecording: randomDuration(),
StopRecording: randomDuration(),
PowerOn: randomDuration(),
PowerOff: randomDuration(),
Updated: updated,
}
}
func randomDuration() string {
durations := []string{"-30m", "+30m", "12:00", "13:00"}
return durations[rand.Intn(len(durations))]
}
func randomLocation() Location {
return Location{
Accuracy: float32(randSrc.Int63()),
Longitude: float32(randSrc.Int63()),
Timestamp: now(),
}
}
func equalLocation(t *testing.T, l1, l2 Location) {
require.Equal(t, l1.Accuracy, l2.Accuracy)
require.Equal(t, l1.Altitude, l2.Altitude)
require.Equal(t, l1.Latitude, l2.Latitude)
require.Equal(t, l1.Longitude, l2.Longitude)
require.Equal(t, l1.Timestamp.Unix(), l2.Timestamp.Unix())
}
func equalWindows(t *testing.T, w1, w2 Windows) {
require.Equal(t, w1.StartRecording, w2.StartRecording)
require.Equal(t, w1.StopRecording, w2.StopRecording)
require.Equal(t, w1.PowerOn, w2.PowerOn)
require.Equal(t, w1.PowerOff, w2.PowerOff)
require.Equal(t, w1.Updated.Unix(), w2.Updated.Unix())
}
func randomTestHosts() TestHosts {
return TestHosts{
URLs: []string{randString(10), randString(20), randString(15)},
PingRetries: int(randSrc.Int63()),
PingWaitTime: time.Duration(randSrc.Int63()) * time.Second,
}
}
// Random string
const (
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
charIdxBits = 6 // 6 bits to represent a char index
charIdxMax = 63 / charIdxBits // # of char indices fitting in 63 bits
charIdxMask = 1<<charIdxBits - 1 // All 1-bits, as many as charIdxBits
)
var randSrc = rand.NewSource(time.Now().UnixNano())
func randString(n int) string {
b := make([]byte, n)
// A randSrc.Int63() generates 63 random bits, enough for charIdxMax characters!
for i, cache, remain := n-1, randSrc.Int63(), charIdxMax; i >= 0; {
if remain == 0 {
cache, remain = randSrc.Int63(), charIdxMax
}
if idx := int(cache & charIdxMask); idx < len(chars) {
b[i] = chars[idx]
i--
}
cache >>= charIdxBits
remain--
}
return string(b)
}