-
Notifications
You must be signed in to change notification settings - Fork 1
/
proj2_test.go
187 lines (164 loc) · 4.17 KB
/
proj2_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
package proj2
// You MUST NOT change what you import. If you add ANY additional
// imports it will break the autograder, and we will be Very Upset.
import (
"encoding/hex"
_ "encoding/hex"
_ "encoding/json"
_ "errors"
"github.com/cs161-staff/userlib"
_ "github.com/google/uuid"
"reflect"
_ "strconv"
_ "strings"
"testing"
)
func TestInit(t *testing.T) {
t.Log("Initialization test")
// You may want to turn it off someday
userlib.SetDebugStatus(true)
// someUsefulThings() // Don't call someUsefulThings() in the autograder in case a student removes it
userlib.SetDebugStatus(false)
u, err := InitUser("alice", "fubar")
if err != nil {
// t.Error says the test fails
t.Error("Failed to initialize user", err)
return
}
// t.Log() only produces output if you run with "go test -v"
t.Log("Got user", u)
// If you want to comment the line above,
// write _ = u here to make the compiler happy
// You probably want many more tests here.
}
func TestStorage(t *testing.T) {
// And some more tests, because
u, err := GetUser("alice", "fubar")
if err != nil {
t.Error("Failed to reload user", err)
return
}
t.Log("Loaded user", u)
v := []byte("This is a test")
t.Log("starting to store file")
u.StoreFile("file1", v)
t.Log("file store complete")
t.Log("starting to loaf file")
v2, err2 := u.LoadFile("file1")
t.Log("file load complete")
if err2 != nil {
t.Error("Failed to upload and download", err2)
return
}
if !reflect.DeepEqual(v, v2) {
t.Error("Downloaded file is not the same", v, v2)
return
}
}
func TestShare(t *testing.T) {
u, err := GetUser("alice", "fubar")
if err != nil {
t.Error("Failed to reload user", err)
return
}
u2, err2 := InitUser("bob", "foobar")
if err2 != nil {
t.Error("Failed to initialize bob", err2)
return
}
var v, v2 []byte
var magic_string string
v, err = u.LoadFile("file1")
if err != nil {
t.Error("Failed to download the file from alice", err)
return
}
magic_string, err = u.ShareFile("file1", "bob")
if err != nil {
t.Error("Failed to share the a file", err)
return
}
err = u2.ReceiveFile("file2", "alice", magic_string)
if err != nil {
t.Error("Failed to receive the share message", err)
return
}
v2, err = u2.LoadFile("file2")
if err != nil {
t.Error("Failed to download the file after sharing", err)
return
}
if !reflect.DeepEqual(v, v2) {
t.Error("Shared file is not the same", v, v2)
return
}
}
func TestUser_AppendFile(t *testing.T) {
u, err := GetUser("alice", "fubar")
v,_ := u.LoadFile("file1")
if err != nil {
t.Error("Failed to download the file from alice", err)
return
}
err = u.AppendFile("file1", []byte("helloworld"))
if err != nil {
t.Error("Failed to append file", err)
return
}
v2,_ := u.LoadFile("file1")
if err != nil {
t.Error("Failed to download the file from alice", err)
return
}
t.Log("initial file is: " + hex.EncodeToString(v))
t.Log("appended file is: " + hex.EncodeToString([]byte("helloworld")))
t.Log("final file is: " + hex.EncodeToString(v2))
if !reflect.DeepEqual(append(v, []byte("helloworld")...), v2) {
t.Error("Appending wrong", v, v2)
return
}
}
func TestUser_RevokeFile(t *testing.T) {
u, err := GetUser("alice", "fubar")
if err != nil {
t.Error("Failed to reload user", err)
return
}
var v, v2, v3 []byte
v, err = u.LoadFile("file1")
if err != nil {
t.Error("Failed to download the file from alice", err)
return
}
u2, err := GetUser("bob", "foobar")
if err != nil {
t.Error("Failed to reload user", err)
return
}
v2, err = u2.LoadFile("file2")
if err != nil {
t.Error("Failed to download the file from bob", err)
return
}
if !reflect.DeepEqual(v, v2) {
t.Error("Shared file is initially not the same", v, v2)
return
}
err = u.RevokeFile("file1", "bob")
if err != nil {
t.Error("Failed to revoke file", err)
return
}
v3, err = u2.LoadFile("file2")
t.Log("alice's file is: " + hex.EncodeToString(v))
t.Log("bob's file is: " + hex.EncodeToString(v2))
t.Log("bob's file after revocation: " + hex.EncodeToString(v3))
if err == nil {
t.Error("Can still download the file from bob", err)
return
}
if reflect.DeepEqual(v, v3) {
t.Error("Shared file is still the same", v, v3)
return
}
}