-
Notifications
You must be signed in to change notification settings - Fork 2
/
util_test.go
78 lines (64 loc) · 1.51 KB
/
util_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
package bunnystorage_test
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"log"
"testing"
"git.sr.ht/~jamesponddotco/bunnystorage-go"
)
type ErrorReader struct{}
func (*ErrorReader) Read(_ []byte) (n int, err error) {
return 0, fmt.Errorf("forced reader error")
}
func TestComputeSHA256(t *testing.T) {
t.Parallel()
fileContent := []byte("Test data for hashing.")
var (
expectedHash = sha256.Sum256(fileContent)
expectedHashHex = hex.EncodeToString(expectedHash[:])
)
emptyHash := sha256.New()
emptyHashHex := hex.EncodeToString(emptyHash.Sum(nil))
testCases := []struct {
name string
reader io.Reader
expectedHash string
expectError bool
}{
{
name: "ValidFileContent",
reader: bytes.NewReader(fileContent),
expectedHash: expectedHashHex,
expectError: false,
},
{
name: "EmptyFileContent",
reader: bytes.NewReader([]byte{}),
expectedHash: emptyHashHex,
expectError: false,
},
{
name: "ReaderError",
reader: &ErrorReader{},
expectedHash: "",
expectError: true,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
hash, err := bunnystorage.ComputeSHA256(tc.reader)
if (err != nil) != tc.expectError {
t.Fatalf("Expected error: %v, got: %v", tc.expectError, err)
}
if err == nil && hash != tc.expectedHash {
t.Errorf("Expected hash: %s, got: %s", tc.expectedHash, hash)
}
log.Printf("Hash: %s", hash)
})
}
}