-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.go
145 lines (139 loc) · 4.05 KB
/
model_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
package vsafe_test
import (
"github.com/keep94/toolbox/kdf"
"github.com/keep94/vsafe"
"testing"
)
func TestKeyEqual(t *testing.T) {
first := &vsafe.Key{Id: 1}
second := &vsafe.Key{Id: 1}
third := &vsafe.Key{Id: 2}
fourth := &vsafe.Key{Id: 1, Value: []byte{0x3}}
var fifth *vsafe.Key
var sixth *vsafe.Key
if !first.Equal(first) {
t.Error("Expected first and first to be equal.")
}
if !first.Equal(second) {
t.Error("Expected first and second to be equal.")
}
if first.Equal(third) {
t.Error("Expected first and third not to be equal.")
}
if first.Equal(fourth) {
t.Error("Expected first and fouth not to be equal.")
}
if first.Equal(fifth) {
t.Error("Expected first and fifth not to be equal.")
}
if fifth.Equal(first) {
t.Error("Expected first and fifth not to be equal.")
}
if !fifth.Equal(sixth) {
t.Error("Expected first and sixt to be equal.")
}
}
func TestUser(t *testing.T) {
user := vsafe.User{Id: 1}
var err error
var key *vsafe.Key
var key2 *vsafe.Key
if err = user.Init("keep94", "somepassword"); err != nil {
t.Fatalf("Error creating user: %v", err)
}
if key, err = user.VerifyPassword("somepassword"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
if _, err = user.VerifyPassword("aardvark"); err != vsafe.ErrWrongPassword {
t.Errorf("Expected wrong password, got %v", err)
}
user2 := vsafe.User{Id: 2}
if err = user2.InitWithKey("keep94+admin", "foo", key); err != nil {
t.Fatalf("Error creating user: %v", err)
}
if key2, err = user2.VerifyPassword("foo"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
if !key2.Equal(key) {
t.Error("key and key2 should be equal.")
}
if key.Id != 1 {
t.Error("Expected key Id to be 1")
}
}
func TestChangePassword(t *testing.T) {
user := vsafe.User{Id: 1}
var err error
if err = user.Init("keep94", "somepassword"); err != nil {
t.Fatalf("Error creating user: %v", err)
}
if err = user.ChangePassword("somepassword", "another"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
if _, err = user.VerifyPassword("another"); err != nil {
t.Error("Expected password to change")
}
if err = user.ChangePassword("somepassword", "be"); err != vsafe.ErrWrongPassword {
t.Errorf("Expected ErrWrongPassword, got %v", err)
}
}
func TestKey(t *testing.T) {
user := vsafe.User{Id: 1}
var err error
if err = user.Init("keep94", "somepassword"); err != nil {
t.Fatalf("Error creating user: %v", err)
}
user2 := vsafe.User{Id: 2}
if err = user2.Init("keep94", "somepassword"); err != nil {
t.Fatalf("Error creating user: %v", err)
}
var key *vsafe.Key
if key, err = user.VerifyPassword("somepassword"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
var keyAgain *vsafe.Key
if keyAgain, err = user.VerifyPassword("somepassword"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
var key2 *vsafe.Key
if key2, err = user2.VerifyPassword("somepassword"); err != nil {
t.Fatalf("Verify password failed: %v", err)
}
if !key.Equal(keyAgain) {
t.Errorf("Expected equal keys")
}
if key.Equal(key2) {
t.Errorf("Expected unequal keys")
}
}
func TestEncryptDecrypt(t *testing.T) {
e := vsafe.Entry{
Title: "title",
Desc: "desc",
UName: "foo",
Password: "bar",
Special: "baz"}
key := &vsafe.Key{Id: 1, Value: kdf.Random(32)}
encrypted := e
if err := encrypted.Encrypt(key); err != nil {
t.Fatalf("Got error encrypting: %v", err)
}
if encrypted.UName == e.UName || encrypted.Password == e.Password || encrypted.Special == e.Special {
t.Error("Encrypted is the same as plain text")
}
if encrypted.Title != e.Title || encrypted.Desc != e.Desc {
t.Error("Encrypted fields should be the same as plain text")
}
decrypted := encrypted
if err := decrypted.Decrypt(key); err != nil {
t.Fatalf("Got error encrypting: %v", err)
}
// Owner of entity changes to key Id
e.Owner = key.Id
if decrypted != e {
t.Errorf("Expected %v, got %v", e, decrypted)
}
if err := decrypted.Decrypt(&vsafe.Key{Id: 2, Value: kdf.Random(32)}); err != vsafe.ErrKeyMismatch {
t.Errorf("Expected ErrKeyMismatch, got %v", err)
}
}