-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_test.go
More file actions
120 lines (98 loc) · 3.22 KB
/
Copy pathbatch_test.go
File metadata and controls
120 lines (98 loc) · 3.22 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
package kx_test
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/qor5/kx"
"github.com/qor5/kx/api/mock"
"github.com/qor5/kx/xhmac"
)
type batchUser struct {
Name string
HashedName string
}
func newBatchManager(t *testing.T) *kx.Manager {
t.Helper()
registry, err := kx.NewRegistry()
require.NoError(t, err)
registry.MustRegisterStruct(batchUser{}, kx.WithRegularField("Name", true))
hashFactory, err := xhmac.NewHashFactory(getHashKey(t))
require.NoError(t, err)
m, err := kx.NewManager(mock.NewCipherFactory(), hashFactory, registry)
require.NoError(t, err)
return m
}
func mustEncryptBatchUser(t *testing.T, m *kx.Manager, name string) kx.DecryptItem[*batchUser] {
t.Helper()
encCtx := map[string]string{"id": name}
encrypted, ciphertext, err := kx.EncryptStruct(ctx, m, &batchUser{Name: name}, encCtx)
require.NoError(t, err)
return kx.DecryptItem[*batchUser]{
EncryptedObj: encrypted,
Ciphertext: ciphertext,
EncryptionContext: encCtx,
}
}
func TestDecryptStructs(t *testing.T) {
// Run with -race to catch any accidental sharing between decrypt goroutines.
t.Run("round_trip_preserves_order", func(t *testing.T) {
m := newBatchManager(t)
const n = 50
items := make([]kx.DecryptItem[*batchUser], n)
for i := range items {
items[i] = mustEncryptBatchUser(t, m, fmt.Sprintf("user-%d", i))
}
got, err := kx.DecryptStructs(ctx, m, items)
require.NoError(t, err)
require.Len(t, got, n)
for i := range got {
assert.Equal(t, fmt.Sprintf("user-%d", i), got[i].Name)
}
})
t.Run("empty_returns_nil", func(t *testing.T) {
m := newBatchManager(t)
got, err := kx.DecryptStructs[*batchUser](ctx, m, nil)
require.NoError(t, err)
assert.Nil(t, got)
})
t.Run("concurrency_one_preserves_order", func(t *testing.T) {
m := newBatchManager(t)
items := make([]kx.DecryptItem[*batchUser], 10)
for i := range items {
items[i] = mustEncryptBatchUser(t, m, fmt.Sprintf("u%d", i))
}
got, err := kx.DecryptStructs(ctx, m, items, kx.WithConcurrency(1))
require.NoError(t, err)
require.Len(t, got, len(items))
for i := range got {
assert.Equal(t, fmt.Sprintf("u%d", i), got[i].Name)
}
})
t.Run("concurrency_larger_than_items", func(t *testing.T) {
m := newBatchManager(t)
items := []kx.DecryptItem[*batchUser]{
mustEncryptBatchUser(t, m, "a"),
mustEncryptBatchUser(t, m, "b"),
}
// WithConcurrency(0) is ignored (default used); a huge value is capped
// to len(items) internally — both must still succeed and preserve order.
got, err := kx.DecryptStructs(ctx, m, items, kx.WithConcurrency(0), kx.WithConcurrency(1000))
require.NoError(t, err)
require.Len(t, got, 2)
assert.Equal(t, "a", got[0].Name)
assert.Equal(t, "b", got[1].Name)
})
t.Run("fail_fast_on_error", func(t *testing.T) {
m := newBatchManager(t)
items := []kx.DecryptItem[*batchUser]{
mustEncryptBatchUser(t, m, "ok"),
// Invalid ciphertext: fails inside DecryptStruct; the whole batch
// must fail and return a nil slice.
{EncryptedObj: &batchUser{}, Ciphertext: "not-a-valid-ciphertext", EncryptionContext: nil},
}
got, err := kx.DecryptStructs(ctx, m, items)
require.Error(t, err)
assert.Nil(t, got)
})
}