-
Notifications
You must be signed in to change notification settings - Fork 14
/
hoard_test.go
47 lines (37 loc) · 1.31 KB
/
hoard_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
package hoard
import (
"testing"
"github.com/go-kit/kit/log"
"github.com/monax/hoard/v8/config"
"github.com/monax/hoard/v8/reference"
"github.com/monax/hoard/v8/stores"
"github.com/stretchr/testify/assert"
)
func TestDeterministicEncryptedStore(t *testing.T) {
hrd := NewHoard(stores.NewMemoryStore(), config.NoopSecretManager, log.NewNopLogger())
bunsIn := []byte("hot buns")
ref, err := hrd.Put(bunsIn, make([]byte, 32))
assert.NoError(t, err)
bunsOut, err := hrd.Get(ref)
assert.Equal(t, bunsIn, bunsOut)
_, err = hrd.Get(reference.New(ref.Address, pad("wrong secret", 32), nil, 1024))
assert.Error(t, err)
statInfo, err := hrd.Store().Stat(ref.Address)
assert.NoError(t, err)
assert.True(t, statInfo.Exists)
// Our GCM cipher should be running an overhead of 16 bytes
// (no IV, but 16-byte authentication tag)
assert.Equal(t, uint64(len(bunsIn))+16+32, statInfo.Size_)
loc := hrd.Store().Location(ref.Address)
assert.Equal(t, "memfs://75b382c29b0d8382a09b856f7a0f00300548c9f369574f68cfc9c62fcab2d1dc", loc)
// flip LSB of first byte of address to get an non-existent address
ref.Address[0] = ref.Address[0] ^ 1
statInfo, err = hrd.Store().Stat(ref.Address)
assert.NoError(t, err)
assert.False(t, statInfo.Exists)
}
func pad(s string, n int) []byte {
b := make([]byte, n)
copy(b, []byte(s))
return b
}