-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey_test.go
75 lines (65 loc) · 1.92 KB
/
key_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
package main
import (
"fmt"
"testing"
)
func Test_KeyFromPath(t *testing.T) {
k, err := keyFromPath("sha1/ae/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/data")
if err != nil {
t.Error("bad key")
}
if k.String() != "sha1:ae28605f0ffc34fe5314342f78efaa13ee45f699" {
t.Error("didn't get the right key")
}
k, err = keyFromPath("/tmp/cask0/sha1/ae/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/data")
if err != nil {
t.Error("error on full key")
}
if k != nil && k.String() != "sha1:ae28605f0ffc34fe5314342f78efaa13ee45f699" {
t.Error("failed on full path")
}
}
func Test_KeyFromPathExceptions(t *testing.T) {
// too few parts
_, err := keyFromPath("sha1/ae/28/60/5f/0f/fc/34/fe/53/")
if err == nil {
t.Error("not enough parts for a valid key")
}
// too many parts
_, err = keyFromPath("sha1/ae/28/60/5f/0f/fc/34/fe/53/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/data")
if err == nil {
t.Error("too many parts for a valid key")
}
// right number of parts, but not enough chars
_, err = keyFromPath("sha1/a/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/data")
if err == nil {
t.Error("must be 40 chars in hash")
}
}
func Test_AsPath(t *testing.T) {
p := "ae/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99"
k, _ := keyFromPath("sha1/" + p + "/data")
if p != k.AsPath() {
t.Error("path came back changed")
}
}
func Test_Valid(t *testing.T) {
p := "ae/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99"
k, _ := keyFromPath("sha1/" + p + "/data")
if !k.Valid() {
fmt.Println(k.Algorithm, k.String())
t.Error("should be valid")
}
}
func Test_keyFromString(t *testing.T) {
// invalid algorithm
_, err := keyFromString("foo:not valid")
if err == nil {
t.Error("'foo' is not a valid algorithm")
}
// valid algorithm, invalid hash length
_, err = keyFromString("sha1:not enough chars")
if err == nil {
t.Error("sha1 hash must be 40 chars")
}
}