-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathblacknote_test.go
97 lines (90 loc) · 2.44 KB
/
blacknote_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
package main
import (
"os"
"regexp"
"testing"
)
func init() {
}
func TestValidPath(t *testing.T) {
gtests := []string{"/s/8b7b1ab80027dedd3a5203e4c3be6775",
"blacknote/s/8b7b1ab80027dedd3a5203e4c3be6775",
"blacknote/a/b/c/d/s/8b7b1ab80027dedd3a5203e4c3be6775",
}
btests := []string{"/s/8b7b1ab8002",
"/s/8b7b1ab80027dedd3a5203e4c3be6775a",
"/s/8b7b1ab80027dedd3a5203e4c3be6775/",
"/s/8b7b1ab80027dedd3a5203e4c3be6775/a",
"blacknote/s/8b7b1ab80027dedd!a5203e4c3be6775",
}
for _, gt := range gtests {
if !regexp.MustCompile("^.*s/[A-Fa-f0-9]{32}$").MatchString(gt) {
t.Error("test failed for good path check of ", gt)
}
}
for _, bt := range btests {
if regexp.MustCompile("^.*s/[A-Fa-f0-9]{32}$").MatchString(bt) {
t.Error("test failed for bad path check of ", bt)
}
}
}
func TestValidBase64(t *testing.T) {
var pairs = []string{
// RFC 4648 examples
"",
"Zg==",
"Zm8=",
"Zm9v",
"Zm9vYg==",
"Zm9vYmE=",
"Zm9vYmFy",
}
for _, tp := range pairs {
if !regexp.MustCompile("^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2}==|[A-Za-z0-9-_]{3}=)?$").MatchString(tp) {
t.Error("valid test for base64 failed: ", tp)
}
}
}
func TestGenUID(t *testing.T) {
tuid := genUID()
if !regexp.MustCompile("^[A-Fa-f0-9]{32}$").MatchString(tuid) {
t.Error("expected", "only hex characters of length 32", "got", tuid)
}
}
func TestDB(t *testing.T) {
err := initDB("./test.db")
if err != nil {
t.Error("database could not be initialized:", err)
}
err = insertDB("8b7b1ab80027dedd3a5203e4c3be6775a", "Zm9vYmE=")
if err != nil {
t.Error("database could not be inserted into:", err)
}
tv, err := getPasteDB("8b7b1ab80027dedd3a5203e4c3be6775a")
if err != nil {
t.Error("database info could not be retrieved:", err)
}
if tv != "Zm9vYmE=" {
t.Error("database retrieved incorrect information:", err)
}
err = delPasteDB("8b7b1ab80027dedd3a5203e4c3be6775a")
if err != nil {
t.Error("database info could not be deleted:", err)
}
err = os.Remove("./test.db")
if err != nil {
t.Error("database could not be removed:", err)
}
err = insertDB("8b7b1ab80027dedd3a5203e4c3be6775a", "Zm9vYmE=")
if err == nil {
t.Error("database could not be inserted into:", err)
}
tv, err = getPasteDB("8b7b1ab80027dedd3a5203e4c3be6775a")
if err == nil && tv != "" {
t.Error("database info could not be retrieved:", err)
}
err = delPasteDB("8b7b1ab80027dedd3a5203e4c3be6775a")
if err == nil {
t.Error("database info could not be deleted:", err)
}
}