Newer
Older
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
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 (
"testing"
"reflect"
"github.com/nweaver/cs161-p2/userlib"
"encoding/json"
"encoding/hex"
"github.com/google/uuid"
"strings"
"errors"
)
func TestInit(t *testing.T) {
t.Log("Initialization test")
// You may want to turn it off someday
userlib.SetDebugStatus(true)
someUsefulThings()
userlib.SetDebugStatus(false)
u, err := InitUser("alice", "fubar")
if err != nil {
// t.Error says the test fails
t.Error("Failed to initialize user", err)
}
// 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")
u.StoreFile("file1", v)
v2, err2 := u.LoadFile("file1")
if err2 != nil {
t.Error("Failed to upload and download", err2)
}
if !reflect.DeepEqual(v, v2) {
t.Error("Downloaded file is not the same", v, v2)
}
}
func TestShare(t *testing.T) {
u, err := GetUser("alice", "fubar")
if err != nil {
t.Error("Failed to reload user", err)
}
u2, err2 := InitUser("bob", "foobar")
if err2 != nil {
t.Error("Failed to initialize bob", err2)
}
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)
}
magic_string, err = u.ShareFile("file1", "bob")
if err != nil {
t.Error("Failed to share the a file", err)
}
err = u2.ReceiveFile("file2", "alice", magic_string)
if err != nil {
t.Error("Failed to receive the share message", err)
}
v2, err = u2.LoadFile("file2")
if err != nil {
t.Error("Failed to download the file after sharing", err)
}
if !reflect.DeepEqual(v, v2) {
t.Error("Shared file is not the same", v, v2)
}
}