-
Notifications
You must be signed in to change notification settings - Fork 3
/
simplemaria_test.go
429 lines (383 loc) · 11.2 KB
/
simplemaria_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package simplemaria
import (
"crypto/sha256"
"io"
"testing"
"github.com/xyproto/cookie/v2"
"github.com/xyproto/pinterface"
"golang.org/x/crypto/bcrypt"
)
const (
listname = "testlist"
setname = "testset"
hashmapname = "testhashmap"
keyvaluename = "testkeyvalue"
testdata1 = "abc123"
testdata2 = "def456"
testdata3 = "ghi789"
)
func TestLocalConnection(t *testing.T) {
Verbose = true
//err := TestConnection() // locally
err := TestConnectionHost("travis:@127.0.0.1/") // for travis-ci
//err := TestConnectionHost("go:go@/main") // laptop
if err != nil {
t.Errorf(err.Error())
}
}
func TestList(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost("travis:@127.0.0.1/") // for travis-ci
//host := NewHost("go:go@/main") // laptop
defer host.Close()
list, err := NewList(host, listname)
if err != nil {
t.Error(err)
}
list.Clear()
if err := list.Add(testdata1); err != nil {
t.Errorf("Error, could not add item to list! %s", err.Error())
}
items, err := list.GetAll()
if err != nil {
t.Errorf("Error when retrieving list! %s", err.Error())
}
if len(items) != 1 {
t.Errorf("Error, wrong list length! %v", len(items))
}
if (len(items) > 0) && (items[0] != testdata1) {
t.Errorf("Error, wrong list contents! %v", items)
}
if err := list.Add(testdata2); err != nil {
t.Errorf("Error, could not add item to list! %s", err.Error())
}
if err := list.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to list! %s", err.Error())
}
items, err = list.GetAll()
if err != nil {
t.Errorf("Error when retrieving list! %s", err.Error())
}
if len(items) != 3 {
t.Errorf("Error, wrong list length! %v", len(items))
}
item, err := list.GetLast()
if err != nil {
t.Errorf("Error, could not get last item from list! %s", err.Error())
}
if item != testdata3 {
t.Errorf("Error, expected %s, got %s with GetLast()!", testdata3, item)
}
items, err = list.GetLastN(2)
if err != nil {
t.Errorf("Error, could not get last N items from list! %s", err.Error())
}
if len(items) != 2 {
t.Errorf("Error, wrong list length! %v", len(items))
}
if items[0] != testdata2 {
t.Errorf("Error, expected %s, got %s with GetLast()!", testdata2, items[0])
}
err = list.Remove()
if err != nil {
t.Errorf("Error, could not remove list! %s", err.Error())
}
// Check that list qualifies for the IList interface
var _ pinterface.IList = list
}
func TestSet(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost("travis:@127.0.0.1/") // for travis-ci
//host := NewHost("go:go@/main") // laptop
defer host.Close()
set, err := NewSet(host, setname)
if err != nil {
t.Error(err)
}
set.Clear()
if err := set.Add(testdata1); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
items, err := set.GetAll()
if err != nil {
t.Errorf("Error when retrieving set! %s", err.Error())
}
if len(items) != 1 {
t.Errorf("Error, wrong set length! %v", len(items))
}
if (len(items) > 0) && (items[0] != testdata1) {
t.Errorf("Error, wrong set contents! %v", items)
}
if err := set.Add(testdata2); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
// Add an element twice. This is a set, so the element should only appear once.
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
items, err = set.GetAll()
if err != nil {
t.Errorf("Error when retrieving set! %s", err.Error())
}
if len(items) != 3 {
t.Errorf("Error, wrong set length! %v\n%v\n", len(items), items)
}
err = set.Remove()
if err != nil {
t.Errorf("Error, could not remove set! %s", err.Error())
}
// Check that set qualifies for the ISet interface
var _ pinterface.ISet = set
}
func TestRawSet(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost("travis:@127.0.0.1/") // for travis-ci
//host := NewHost("go:go@/main") // laptop
host.SetRawUTF8(true)
defer host.Close()
set, err := NewSet(host, setname)
if err != nil {
t.Error(err)
}
set.Clear()
if err := set.Add(testdata1); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
items, err := set.GetAll()
if err != nil {
t.Errorf("Error when retrieving set! %s", err.Error())
}
if len(items) != 1 {
t.Errorf("Error, wrong set length! %v", len(items))
}
if (len(items) > 0) && (items[0] != testdata1) {
t.Errorf("Error, wrong set contents! %v", items)
}
if err := set.Add(testdata2); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
// Add an element twice. This is a set, so the element should only appear once.
if err := set.Add(testdata3); err != nil {
t.Errorf("Error, could not add item to set! %s", err.Error())
}
items, err = set.GetAll()
if err != nil {
t.Errorf("Error when retrieving set! %s", err.Error())
}
if len(items) != 3 {
t.Errorf("Error, wrong set length! %v\n%v\n", len(items), items)
}
err = set.Remove()
if err != nil {
t.Errorf("Error, could not remove set! %s", err.Error())
}
// Check that set qualifies for the ISet interface
var _ pinterface.ISet = set
}
func TestHashMap(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost("travis:@127.0.0.1/") // for travis-ci
//host := NewHost("go:go@/main") // laptop
defer host.Close()
hashmap, err := NewHashMap(host, hashmapname)
if err != nil {
t.Error(err)
}
hashmap.Clear()
username := "bob"
key := "password"
value := "hunter1"
// Get key that doesn't exist yet
_, err = hashmap.Get("ownerblabla", "keyblabla")
if err == nil {
t.Errorf("Key found, when it should be missing! %s", err.Error())
}
if err := hashmap.Set(username, key, value); err != nil {
t.Errorf("Error, could not set value in hashmap! %s", err.Error())
}
// Once more, with the same data
if err := hashmap.Set(username, key, value); err != nil {
t.Errorf("Error, could not set value in hashmap! %s", err.Error())
}
items, err := hashmap.GetAll()
if err != nil {
t.Errorf("Error when retrieving elements! %s", err.Error())
}
if len(items) != 1 {
t.Errorf("Error, wrong element length! %v", len(items))
}
if (len(items) > 0) && (items[0] != username) {
t.Errorf("Error, wrong elementid! %v", items)
}
item, err := hashmap.Get(username, key)
if err != nil {
t.Errorf("Error, could not fetch value from hashmap! %s", err.Error())
}
if item != value {
t.Errorf("Error, expected %s, got %s!", value, item)
}
keys, err := hashmap.Keys(username)
if err != nil {
t.Error(err)
}
// only "password"
if len(keys) != 1 {
t.Errorf("Error, wrong keys: %v\n", keys)
}
if keys[0] != "password" {
t.Errorf("Error, wrong keys: %v\n", keys)
}
err = hashmap.Remove()
if err != nil {
t.Errorf("Error, could not remove hashmap! %s", err.Error())
}
// Check that hashmap qualifies for the IHashMap interface
var _ pinterface.IHashMap = hashmap
}
func TestKeyValue(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost("travis:@127.0.0.1/") // for travis-ci
//host := NewHost("go:go@/main") // laptop
defer host.Close()
keyvalue, err := NewKeyValue(host, keyvaluename)
if err != nil {
t.Error(err)
}
keyvalue.Clear()
key := "password"
value := "hunter1"
if err := keyvalue.Set(key, value); err != nil {
t.Errorf("Error, could not set value in keyvalue! %s", err.Error())
}
// Twice
if err := keyvalue.Set(key, value); err != nil {
t.Errorf("Error, could not set value in keyvalue! %s", err.Error())
}
item, err := keyvalue.Get(key)
if err != nil {
t.Errorf("Error, could not fetch value from keyvalue! %s", err.Error())
}
if item != value {
t.Errorf("Error, expected %s, got %s!", value, item)
}
err = keyvalue.Remove()
if err != nil {
t.Errorf("Error, could not remove keyvalue! %s", err.Error())
}
// Check that keyvalue qualifies for the IKeyValue interface
var _ pinterface.IKeyValue = keyvalue
}
func TestHashStorage(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost("travis:@127.0.0.1/") // for travis-ci
//host := NewHost("go:go@/main") // laptop
defer host.Close()
hashmap, err := NewHashMap(host, hashmapname)
if err != nil {
t.Error(err)
}
hashmap.Clear()
username := "bob"
key := "password"
password := "hunter1"
// bcrypt test
passwordHash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
value := string(passwordHash)
if err := hashmap.Set(username, key, value); err != nil {
t.Errorf("Error, could not set value in hashmap! %s", err.Error())
}
item, err := hashmap.Get(username, key)
if err != nil {
t.Errorf("Unable to retrieve from hashmap! %s\n", err.Error())
}
if item != value {
t.Errorf("Error, got a different value back (bcrypt)! %s != %s\n", value, item)
}
// sha256 test
hasher := sha256.New()
io.WriteString(hasher, password+cookie.RandomCookieFriendlyString(30)+username)
passwordHash = hasher.Sum(nil)
value = string(passwordHash)
if err := hashmap.Set(username, key, value); err != nil {
t.Errorf("Error, could not set value in hashmap! %s", err.Error())
}
item, err = hashmap.Get(username, key)
if err != nil {
t.Errorf("Unable to retrieve from hashmap! %s\n", err.Error())
}
if item != value {
t.Errorf("Error, got a different value back (sha256)! %s != %s\n", value, item)
}
err = hashmap.Remove()
if err != nil {
t.Errorf("Error, could not remove hashmap! %s", err.Error())
}
}
func TestTwoFields(t *testing.T) {
test, test23, ok := twoFields("test1@test2@test3", "@")
if ok && ((test != "test1") || (test23 != "test2@test3")) {
t.Error("Error in twoFields functions")
}
}
func TestInc(t *testing.T) {
Verbose = true
const (
kvname = "kv_234_test_test_test"
testkey = "key_234_test_test_test"
testvalue0 = "9"
testvalue1 = "10"
testvalue2 = "1"
)
host := NewHost("travis:@127.0.0.1/") // for travis-ci
defer host.Close()
kv, err := NewKeyValue(host, kvname)
if err != nil {
t.Error(err)
}
if err := kv.Set(testkey, testvalue0); err != nil {
t.Errorf("Error, could not set key and value! %s", err.Error())
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue0 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue0)
}
incval, err := kv.Inc(testkey)
if err != nil {
t.Errorf("Error, could not INCR key! %s", err.Error())
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue1 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue1)
} else if incval != testvalue1 {
t.Errorf("Error, wrong inc value! %s != %s", incval, testvalue1)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
// Creates "0" and increases the value with 1
kv.Inc(testkey)
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue2 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue2)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
}