-
Notifications
You must be signed in to change notification settings - Fork 1
/
hash_test.go
55 lines (48 loc) · 1.35 KB
/
hash_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
package main
import (
"fmt"
"testing"
)
func Test_hashFromString(t *testing.T) {
h, err := hashFromString("ae28605f0ffc34fe5314342f78efaa13ee45f699", "")
if err != nil {
t.Error("bad hash")
}
if h.String() != "ae28605f0ffc34fe5314342f78efaa13ee45f699" {
t.Error("doesn't match")
}
if h.Algorithm != "sha1" {
t.Error("wrong algorithm")
}
if h.AsPath() != "ae/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99" {
t.Error(fmt.Sprintf("wrong path: %s", h.AsPath()))
}
h, err = hashFromString("ae28605f0ffc34e5314342f78efaa13ee45f699", "")
if err == nil {
t.Error("non 40 char hash should've been an error")
}
}
func Test_Valid(t *testing.T) {
h, _ := hashFromString("ae28605f0ffc34fe5314342f78efaa13ee45f699", "")
if !h.Valid() {
t.Error("hash should be valid")
}
h.Algorithm = "foo"
if h.Valid() {
t.Error("hash should not be valid (only sha1 supported for now)")
}
}
func Test_hashFromPath(t *testing.T) {
_, err := hashFromPath("ae/28/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/foo.jpg")
if err != nil {
t.Error("bad hash")
}
_, err = hashFromPath("ae/28/60/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/foo.jpg")
if err == nil {
t.Error("shouldn't be enough pieces")
}
_, err = hashFromPath("ae/288/60/5f/0f/fc/34/fe/53/14/34/2f/78/ef/aa/13/ee/45/f6/99/foo.jpg")
if err == nil {
t.Error("not 40 chars")
}
}