forked from internetarchive/gowarc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigest_test.go
More file actions
148 lines (115 loc) · 4.01 KB
/
digest_test.go
File metadata and controls
148 lines (115 loc) · 4.01 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package warc
import (
"bytes"
"strings"
"testing"
)
// Tests for the GetSHA1 function
func TestGetSHA1(t *testing.T) {
helloWorldSHA1 := "sha1:FKXGYNOJJ7H3IFO35FPUBC445EPOQRXN"
hash, err := GetDigest(bytes.NewReader([]byte("hello world")), SHA1)
if err != nil {
t.Errorf("Failed to generate SHA1: %v", err)
}
if hash != helloWorldSHA1 {
t.Errorf("Failed to generate SHA1, expected %s, got %s", helloWorldSHA1, hash)
}
}
func TestSHA1EmptyBytes(t *testing.T) {
// Create an empty byte slice reader
emptyBytes := make([]byte, 0)
emptyReader := strings.NewReader(string(emptyBytes))
// Generate SHA1 hash
hash, err := GetDigest(emptyReader, SHA1)
if err != nil {
t.Fatalf("failed to get SHA1 digest of empty bytes: %v", err)
}
expectedHash := "sha1:3I42H3S6NNFQ2MSVX7XZKYAYSCX5QBYJ"
if hash != expectedHash {
t.Fatalf("expected SHA1 hash %s, got %s", expectedHash, hash)
}
t.Logf("SHA1 hash of empty bytes: %s", hash)
}
// Tests for the GetSHA256Base32 function
func TestGetSHA256Base32(t *testing.T) {
helloWorldSHA256Base32 := "sha256:XFGSPOMTJU7ARJJOKLL5U7NL7LCIJ37DPJJYB3UQRD32ZYXPZXUQ===="
hash, err := GetDigest(bytes.NewReader([]byte("hello world")), SHA256Base32)
if err != nil {
t.Errorf("Failed to generate SHA256Base32: %v", err)
return
}
if hash != helloWorldSHA256Base32 {
t.Errorf("Failed to generate SHA256Base32, expected %s, got %s", helloWorldSHA256Base32, hash)
return
}
}
func TestSHA256Base32EmptyBytes(t *testing.T) {
// Create an empty byte slice reader
emptyBytes := make([]byte, 0)
emptyReader := strings.NewReader(string(emptyBytes))
// Generate SHA256Base32 hash
hash, err := GetDigest(emptyReader, SHA256Base32)
if err != nil {
t.Fatalf("failed to get SHA256Base32 digest of empty bytes: %v", err)
}
expectedHash := "sha256:4OYMIQUY7QOBJGX36TEJS35ZEQT24QPEMSNZGTFESWMRW6CSXBKQ===="
if hash != expectedHash {
t.Fatalf("expected SHA256Base32 hash %s, got %s", expectedHash, hash)
}
t.Logf("SHA256Base32 hash of empty bytes: %s", hash)
}
// Tests for the GetSHA256Base16 function
func TestGetSHA256Base16(t *testing.T) {
helloWorldSHA256Base16 := "sha256:b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
hash, err := GetDigest(bytes.NewReader([]byte("hello world")), SHA256Base16)
if err != nil {
t.Errorf("Failed to generate SHA256Base16: %v", err)
return
}
if hash != helloWorldSHA256Base16 {
t.Errorf("Failed to generate SHA256Base16, expected %s, got %s", helloWorldSHA256Base16, hash)
return
}
}
func TestSHA256Base16EmptyBytes(t *testing.T) {
// Create an empty byte slice reader
emptyBytes := make([]byte, 0)
emptyReader := strings.NewReader(string(emptyBytes))
// Generate SHA256Base16 hash
hash, err := GetDigest(emptyReader, SHA256Base16)
if err != nil {
t.Fatalf("failed to get SHA256Base16 digest of empty bytes: %v", err)
}
expectedHash := "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
if hash != expectedHash {
t.Fatalf("expected SHA256Base16 hash %s, got %s", expectedHash, hash)
}
t.Logf("SHA256Base16 hash of empty bytes: %s", hash)
}
func TestGetBLAKE3(t *testing.T) {
helloWorldBLAKE3 := "blake3:d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24"
hash, err := GetDigest(bytes.NewReader([]byte("hello world")), BLAKE3)
if err != nil {
t.Errorf("Failed to generate BLAKE3: %v", err)
return
}
if hash != helloWorldBLAKE3 {
t.Errorf("Failed to generate BLAKE3, expected %s, got %s", helloWorldBLAKE3, hash)
return
}
}
func TestBlake3EmptyBytes(t *testing.T) {
// Create an empty byte slice reader
emptyBytes := make([]byte, 0)
emptyReader := strings.NewReader(string(emptyBytes))
// Generate BLAKE3 hash
hash, err := GetDigest(emptyReader, BLAKE3)
if err != nil {
t.Fatalf("failed to get BLAKE3 digest of empty bytes: %v", err)
}
expectedHash := "blake3:af1349b9f5f9a1a6a0404dea36dcc9499bcb25c9adc112b7cc9a93cae41f3262"
if hash != expectedHash {
t.Fatalf("expected BLAKE3 hash %s, got %s", expectedHash, hash)
}
t.Logf("BLAKE3 hash of empty bytes: %s", hash)
}