-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash_test.go
More file actions
149 lines (137 loc) · 2.84 KB
/
Copy pathhash_test.go
File metadata and controls
149 lines (137 loc) · 2.84 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
149
package kx_test
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"testing"
"time"
"github.com/qor5/kx"
"github.com/qor5/kx/xhmac"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type Birthday struct {
time.Time
}
func (b *Birthday) String() string {
if b == nil {
return ""
}
return b.Format(time.RFC3339)
}
func TestHashVal(t *testing.T) {
key := make([]byte, sha256.BlockSize)
n, err := rand.Read(key)
require.NoError(t, err)
require.Equal(t, sha256.BlockSize, n)
hashFactory, err := xhmac.NewHashFactory(key)
require.NoError(t, err)
hashData := func(data []byte) string {
h := hashFactory.NewHash()
h.Write(data)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
tests := []struct {
name string
val any
want string
wantErr string
}{
{
name: "string value",
val: "Hello World",
// nop hash factory uses sha256
want: hashData([]byte("hello world")),
},
{
name: "pointer to string",
val: stringPtr("Hello World"),
want: hashData([]byte("hello world")),
},
{
name: "bytes",
val: []byte("Hello World"),
want: hashData([]byte("Hello World")),
},
{
name: "pointer to bytes",
val: bytesPtr([]byte("Hello World")),
want: hashData([]byte("Hello World")),
},
{
name: "struct",
val: struct {
Name string
Age int
}{
Name: "John",
Age: 30,
},
want: hashData(mustMarshalJSON(struct {
Name string
Age int
}{
Name: "John",
Age: 30,
})),
},
{
name: "slice of non-bytes",
val: []string{"hello", "world"},
want: hashData(mustMarshalJSON([]string{"hello", "world"})),
},
{
name: "unicode string",
val: "Café",
// é is preserved in lowercase
want: hashData([]byte("café")),
},
{
name: "full width string",
val: "Hello",
want: hashData([]byte("hello")),
},
{
name: "nil pointer with fmt.Stringer implementation",
val: (*Birthday)(nil),
want: hashData([]byte("")), // String() returns empty string for nil
},
{
name: "valid pointer with fmt.Stringer implementation",
val: func() *Birthday {
t := time.Date(2024, 1, 15, 10, 30, 0, 0, time.UTC)
b := Birthday{
Time: t,
}
return &b
}(),
want: hashData([]byte("2024-01-15T10:30:00Z")), // String() returns RFC3339 format
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := kx.HashVal(hashFactory, tt.val)
if tt.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
}
func stringPtr(s string) *string {
return &s
}
func bytesPtr(b []byte) *[]byte {
return &b
}
func mustMarshalJSON(v any) []byte {
data, err := json.Marshal(v)
if err != nil {
panic(err)
}
return data
}