forked from vainaviv/project2-starter-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proj2_test.go
executable file
·128 lines (110 loc) · 2.7 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
package proj2
// You MUST NOT change these default imports. ANY additional imports it will
// break the autograder and everyone will be sad.
import (
_ "encoding/hex"
_ "encoding/json"
_ "errors"
"reflect"
_ "strconv"
_ "strings"
"testing"
"github.com/cs161-staff/userlib"
"github.com/google/uuid"
_ "github.com/google/uuid"
)
func clear() {
// Wipes the storage so one test does not affect another
userlib.DatastoreClear()
userlib.KeystoreClear()
}
func TestInit(t *testing.T) {
clear()
t.Log("Initialization test")
// You can set this to false!
userlib.SetDebugStatus(true)
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) {
clear()
u, err := InitUser("alice", "fubar")
if err != nil {
t.Error("Failed to initialize user", err)
return
}
v := []byte("This is a test")
u.StoreFile("file1", v)
v2, err2 := u.LoadFile("file1")
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 TestInvalidFile(t *testing.T) {
clear()
u, err := InitUser("alice", "fubar")
if err != nil {
t.Error("Failed to initialize user", err)
return
}
_, err2 := u.LoadFile("this file does not exist")
if err2 == nil {
t.Error("Downloaded a ninexistent file", err2)
return
}
}
func TestShare(t *testing.T) {
clear()
u, err := InitUser("alice", "fubar")
if err != nil {
t.Error("Failed to initialize user", err)
return
}
u2, err2 := InitUser("bob", "foobar")
if err2 != nil {
t.Error("Failed to initialize bob", err2)
return
}
v := []byte("This is a test")
u.StoreFile("file1", v)
var v2 []byte
var accessToken uuid.UUID
v, err = u.LoadFile("file1")
if err != nil {
t.Error("Failed to download the file from alice", err)
return
}
accessToken, err = u.ShareFile("file1", "bob")
if err != nil {
t.Error("Failed to share the a file", err)
return
}
err = u2.ReceiveFile("file2", "alice", accessToken)
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
}
}