-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto_test.go
129 lines (99 loc) · 3.92 KB
/
crypto_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
package main
import (
"reflect"
"strings"
"testing"
"github.com/gobwas/glob"
)
func TestHashDataBlake3(t *testing.T) {
data := "abcd1234"
expected := "e8b21af482045332254fc63468995bac6a013a19c080a542c31289312d382b87"
bytes := HashData(strings.NewReader(data), BLAKE3)
actual := EncodeHash(bytes)
if actual != expected {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
}
func TestHashDataSha3(t *testing.T) {
data := "abcd1234"
expected := "6366c340328616f0393c1647ffd72b7252ce8ff0090240c095253f255c8edeb1"
bytes := HashData(strings.NewReader(data), SHA3)
actual := EncodeHash(bytes)
if actual != expected {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
}
func TestHashDataSha256(t *testing.T) {
data := "abcd1234"
expected := "e9cee71ab932fde863338d08be4de9dfe39ea049bdafb342ce659ec5450b69ae"
bytes := HashData(strings.NewReader(data), SHA256)
actual := EncodeHash(bytes)
if actual != expected {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
}
func TestHashFile(t *testing.T) {
file := "test-data/test1.bin"
expected_hash := "400a322239c83fbed043a8c9d898f2dd3d3634eeec0fab6fb0f577c1a56ada3a"
expected_size := int64(10240)
bytes, actual_size := HashFile(file, BLAKE3)
actual_hash := EncodeHash(bytes)
if actual_hash != expected_hash {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected_hash, actual_hash)
}
if actual_size != expected_size {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected_size, actual_size)
}
}
func TestHashZipContents(t *testing.T) {
file := "test-data/test.zip"
expected := map[string]string{
"test1.bin": "400a322239c83fbed043a8c9d898f2dd3d3634eeec0fab6fb0f577c1a56ada3a",
"test2.bin": "41d066840196797b48b2b30ac31cd7a2d916ea690f6a364bd94fdd1823418f3e",
"testdir/test3.bin": "a12959c6398697a95c097eebef1656ef686c92b1db60d537a942b6ce735f0285",
}
actual := *HashContents(file, glob.MustCompile("*"), BLAKE3)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
actual_file_pattern := *HashContents(file, glob.MustCompile("*.bin"), BLAKE3)
if !reflect.DeepEqual(actual_file_pattern, expected) {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual_file_pattern)
}
}
func TestHashZipContentsPatternDir(t *testing.T) {
file := "test-data/test.zip"
expected := map[string]string{
"testdir/test3.bin": "a12959c6398697a95c097eebef1656ef686c92b1db60d537a942b6ce735f0285",
}
actual := *HashContents(file, glob.MustCompile("testdir/*.bin"), BLAKE3)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
}
func TestHashTarContents(t *testing.T) {
file := "test-data/test.tar"
expected := map[string]string{
"test1.bin": "400a322239c83fbed043a8c9d898f2dd3d3634eeec0fab6fb0f577c1a56ada3a",
"test2.bin": "41d066840196797b48b2b30ac31cd7a2d916ea690f6a364bd94fdd1823418f3e",
"testdir/test3.bin": "a12959c6398697a95c097eebef1656ef686c92b1db60d537a942b6ce735f0285",
}
actual := *HashContents(file, glob.MustCompile("*"), BLAKE3)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
actual_file_pattern := *HashContents(file, glob.MustCompile("*.bin"), BLAKE3)
if !reflect.DeepEqual(actual_file_pattern, expected) {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual_file_pattern)
}
}
func TestHashTarContentsPatternDir(t *testing.T) {
file := "test-data/test.tar"
expected := map[string]string{
"testdir/test3.bin": "a12959c6398697a95c097eebef1656ef686c92b1db60d537a942b6ce735f0285",
}
actual := *HashContents(file, glob.MustCompile("testdir/*.bin"), BLAKE3)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("Results don't match. Expected '%v', Actual %v", expected, actual)
}
}