-
Notifications
You must be signed in to change notification settings - Fork 16
/
file_hash_test.go
97 lines (87 loc) · 2.31 KB
/
file_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
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 checksum_test
import (
"fmt"
"github.com/codingsince1985/checksum"
"github.com/mitchellh/go-homedir"
"io/ioutil"
"os"
"path"
"testing"
)
func prepareFile() (string, error) {
file, err := ioutil.TempFile("", "prefix")
if err != nil {
return "", err
}
if err := ioutil.WriteFile(file.Name(), []byte("some data"), 0600); err != nil {
return "", err
}
return file.Name(), nil
}
func testFile(t *testing.T, checksumFunc func(string) error) {
file, err := prepareFile()
if err != nil {
t.Logf("could not create test file: %s", err)
t.FailNow()
}
defer func() {
err := os.Remove(file)
if err != nil {
t.Logf("could not remove test file: %s", err)
}
}()
if err := checksumFunc(file); err != nil {
t.Error(err)
}
}
func TestBlake2s256(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.Blake2s256(filename); err != nil || result != "54fc4fe89148c8f82479348f56168f71c4165eedda67961daec1d46015db3884" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestSHA1sumFile(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.SHA1sum(filename); err != nil || result != "baf34551fecb48acc3da868eb85e1b6dac9de356" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestSHA256sumFile(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.SHA256sum(filename); err != nil || result != "1307990e6ba5ca145eb35e99182a9bec46531bc54ddf656a602c780fa0240dee" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestMd5sumFile(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.MD5sum(filename); err != nil || result != "1e50210a0202497fb79bc38b6ade6c34" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestCrc32File(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.CRC32(filename); err != nil || result != "d9c2e91e" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestMd5sumDir(t *testing.T) {
homeDirectory, err := homedir.Dir()
if err != nil {
t.Logf("could not get home directory: %s", err)
t.FailNow()
}
file := path.Join(homeDirectory, "Downloads")
if md5sum, err := checksum.MD5sum(file); err != nil || md5sum != "" {
t.Error("Md5sum(dir) failed", md5sum, err)
}
}