-
Notifications
You must be signed in to change notification settings - Fork 0
/
harpocrates_test.go
142 lines (88 loc) · 2.76 KB
/
harpocrates_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
package harpocrates
import (
"encoding/json"
"os"
"testing"
"github.com/khelechy/harpocrates/utils"
"github.com/stretchr/testify/assert"
)
func TestMount(t *testing.T){
// Override the file writing path
oldPath := "keys.json"
defer func() {
// Clean up: restore the original file writing path
_ = os.Rename("keys.json", oldPath)
}()
// Call the Mount function
Mount(5)
// Check if keys.json file is created
_, err := os.Stat("keys.json")
assert.NoError(t, err, "keys.json file should be created")
// Check if harpocrates_db folder is created
_, err = os.Stat("harpocrates_db")
assert.NoError(t, err, "harpocrates_db folder should be created")
// Read the content of the keys.json file
fileContent, err := os.ReadFile("keys.json")
assert.NoError(t, err, "Error reading keys.json file")
// Unmarshal the JSON content
var jsonData map[string]interface{}
err = json.Unmarshal(fileContent, &jsonData)
assert.NoError(t, err, "Error unmarshaling JSON")
// Check if the "keys" key exists in the JSON data
keys, ok := jsonData["keys"].([]interface{})
assert.True(t, ok, "keys should be a slice in the JSON data")
// Check if the number of keys is correct
assert.Equal(t, 5, len(keys), "Number of keys should be 5")
stripTestFiles()
}
func TestUnseal(t *testing.T) {
keys := prepareTestEnv()
Unseal([]string{keys[0].(string), keys[1].(string), keys[2].(string)})
want := "1"
result := utils.GetItem("tholos_seal_key")
assert.Equal(t, want, result)
stripTestFiles()
}
func TestSeal(t *testing.T) {
keys := prepareTestEnv()
Unseal([]string{keys[0].(string), keys[1].(string), keys[2].(string)})
Seal([]string{keys[0].(string), keys[1].(string), keys[2].(string)})
want := "0"
result := utils.GetItem("tholos_seal_key")
assert.Equal(t, want, result)
stripTestFiles()
}
func TestGet(t *testing.T){
keys := prepareTestEnv()
Unseal([]string{keys[0].(string), keys[1].(string), keys[2].(string)})
want := "kelechi"
utils.SetItem("name", want)
result := Get("name")
assert.Equal(t, want, result)
stripTestFiles()
}
func TestSet(t *testing.T){
keys := prepareTestEnv()
Unseal([]string{keys[0].(string), keys[1].(string), keys[2].(string)})
want := "kelechi"
Set("name", "kelechi")
result := utils.GetItem("name")
assert.Equal(t, want, result)
stripTestFiles()
}
func prepareTestEnv() []interface{}{
Mount(5)
_, _ = os.Stat("harpocrates_db")
// Read the content of the keys.json file
fileContent, _ := os.ReadFile("keys.json")
// Unmarshal the JSON content
var jsonData map[string]interface{}
_ = json.Unmarshal(fileContent, &jsonData)
// Check if the "keys" key exists in the JSON data
keys, _ := jsonData["keys"].([]interface{})
return keys
}
func stripTestFiles(){
os.RemoveAll("harpocrates_db")
os.Remove("keys.json")
}