-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqlite_test.go
116 lines (103 loc) · 2.42 KB
/
sqlite_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
package main
import (
"encoding/json"
"fmt"
"testing"
"github.com/niclabs/dtc/v3/config"
)
const TestFirstTokenLabel = "TCBHSM"
const TestOtherTokenLabel = "LABEL2"
func initDB() (Storage, error) {
db, err := GetDatabase("/tmp/dtctest.sqlite")
if err != nil {
return nil, fmt.Errorf("couldn't get database: %v", err)
}
if err := db.Init([]*config.SlotsConfig{
{TestFirstTokenLabel, "1234"},
{TestOtherTokenLabel, "1234"},
}); err != nil {
return nil, fmt.Errorf("couldn't init storage in database: %v", err)
}
return db, nil
}
func TestDB_InitStorage(t *testing.T) {
_, err := initDB()
if err != nil {
t.Errorf("init_storage: %v", err)
}
}
func TestDB_GetMaxHandle(t *testing.T) {
db, err := initDB()
if err != nil {
t.Errorf("init_storage: %v", err)
}
maxHandle, err := db.GetMaxHandle()
if err != nil {
t.Errorf("couldn't get max handle: %v", err)
}
if maxHandle != 0 {
t.Errorf("max handle is not zero on an empty database (it is %d)", maxHandle)
}
}
func TestDB_GetToken(t *testing.T) {
db, err := initDB()
if err != nil {
t.Errorf("init_storage: %v", err)
}
token, err := db.GetToken(TestFirstTokenLabel)
if err != nil {
t.Errorf("get_token: %v", err)
return
}
if token.Label != TestFirstTokenLabel {
t.Errorf("wrong token retrieved, its label should have been %s, but it is %s", TestFirstTokenLabel, token.Label)
}
}
func TestDB_SaveToken(t *testing.T) {
db, err := initDB()
if err != nil {
t.Errorf("init_storage: %v", err)
}
newToken, err := NewToken(TestOtherTokenLabel, "1234", "1234")
if err != nil {
t.Errorf("new_token: %v", err)
}
if err != nil {
t.Errorf("max_handle: %v", err)
}
co := &CryptoObject{
Attributes: make(Attributes),
}
newToken.AddObject(co)
co.Attributes.SetIfUndefined(&Attribute{
Type: uint32(0),
Value: []byte("hello_world"),
})
err = db.SaveToken(newToken)
if err != nil {
t.Errorf("save_token: %v", err)
return
}
gotToken, err := db.GetToken(TestOtherTokenLabel)
if err != nil {
t.Errorf("get_token: %v", err)
return
}
if !newToken.Equals(gotToken) {
newTokenJson, _ := json.Marshal(newToken)
gotTokenJson, _ := json.Marshal(gotToken)
t.Errorf(`wrong token retrieved
expected: %s
result: %s`, newTokenJson, gotTokenJson)
}
}
func TestDB_CloseStorage(t *testing.T) {
db, err := initDB()
if err != nil {
t.Errorf("init_storage: %v", err)
}
err = db.Close()
if err != nil {
t.Errorf("close_storage: %v", err)
}
}