forked from fido-alliance/iot-fdo-conformance-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seeding.setup.go
103 lines (79 loc) · 2.62 KB
/
seeding.setup.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
package main
import (
"fmt"
"log"
"sync"
fdoshared "github.com/fido-alliance/iot-fdo-conformance-tools/core/shared"
"github.com/fido-alliance/iot-fdo-conformance-tools/dbs"
)
const SeedingSize = 10000
const ThreadsPerAlg = 10
type SeedRunResult struct {
DeviceSgType fdoshared.DeviceSgType
Guids []fdoshared.FdoGuid
CredBases []fdoshared.WawDeviceCredential
Error error
}
func SeedRunInst(threadID int, seedSize int, sgType fdoshared.DeviceSgType, wg *sync.WaitGroup, resultChannel chan SeedRunResult) {
var result = SeedRunResult{
DeviceSgType: sgType,
Guids: []fdoshared.FdoGuid{},
CredBases: []fdoshared.WawDeviceCredential{},
}
defer wg.Done()
log.Printf("----- [%d] Starting SgType %d. -----\n", threadID, sgType)
for i := 0; i < seedSize; i++ {
if i != 0 && i%(seedSize/10) == 0 {
log.Printf("[%d] %d. %d%% completed\n", threadID, sgType, int(float64(i/(seedSize/10)))*10)
}
newDeviceBase, err := fdoshared.NewWawDeviceCredential(sgType)
if err != nil {
result.Error = fmt.Errorf("[%d] Error generating device base for sgType %d. %s ", threadID, sgType, err.Error())
log.Println(result.Error.Error())
resultChannel <- result
return
}
result.CredBases = append(result.CredBases, *newDeviceBase)
result.Guids = append(result.Guids, newDeviceBase.DCGuid)
}
resultChannel <- result
}
func PreSeed(configdb *dbs.ConfigDB, devbasedb *dbs.DeviceBaseDB) error {
var wg sync.WaitGroup
totalChannels := len(fdoshared.DeviceSgTypeList) * ThreadsPerAlg
chn := make(chan SeedRunResult, totalChannels)
var batchSize int = SeedingSize / ThreadsPerAlg
for _, sgType := range fdoshared.DeviceSgTypeList {
if sgType == fdoshared.StEPID10 || sgType == fdoshared.StEPID11 {
log.Println("EPID is not currently supported!")
continue
}
for i := 0; i < ThreadsPerAlg; i++ {
wg.Add(1)
go SeedRunInst(i, batchSize, sgType, &wg, chn)
}
}
wg.Wait()
var newConfig dbs.MainConfig = dbs.MainConfig{
SeededGuids: fdoshared.FdoSeedIDs{},
}
for i := 0; i < totalChannels; i++ {
result := <-chn
if result.Error != nil {
return fmt.Errorf("failed to pre-generate creds for %d. %s ", result.DeviceSgType, result.Error)
}
for _, newDeviceBase := range result.CredBases {
err := devbasedb.Save(newDeviceBase)
if err != nil {
return fmt.Errorf("Error saving device base. " + err.Error())
}
}
newConfig.SeededGuids[result.DeviceSgType] = append(newConfig.SeededGuids[result.DeviceSgType], result.Guids...)
}
err := configdb.Save(newConfig)
if err != nil {
return fmt.Errorf("error saving config. " + err.Error())
}
log.Println("Done!")
return nil
}