github.com/trustbloc/kms-go@v1.1.2/kms/localkms/localkms_writer_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package localkms 8 9 import ( 10 "encoding/base64" 11 "errors" 12 "fmt" 13 "testing" 14 15 "github.com/google/tink/go/subtle/random" 16 "github.com/stretchr/testify/require" 17 18 kmsapi "github.com/trustbloc/kms-go/spi/kms" 19 20 "github.com/trustbloc/kms-go/kms" 21 ) 22 23 func TestLocalKMSWriter(t *testing.T) { 24 t.Run("success case - create a valid storeWriter and store 20 non empty random keys", func(t *testing.T) { 25 keys := map[string][]byte{} 26 mockStore := &inMemoryKMSStore{keys: keys} 27 28 for i := 0; i < 256; i++ { 29 l := newWriter(mockStore) 30 require.NotEmpty(t, l) 31 someKey := random.GetRandomBytes(uint32(32)) 32 n, err := l.Write(someKey) 33 require.NoError(t, err) 34 require.Equal(t, len(someKey), n) 35 require.Equal(t, maxKeyIDLen, len(l.KeysetID), "for key creation iteration %d", i) 36 key, ok := keys[l.KeysetID] 37 require.True(t, ok) 38 require.Equal(t, key, someKey) 39 } 40 41 require.Equal(t, 256, len(keys)) 42 }) 43 44 t.Run("error case - create a storeWriter using a bad storeWriter to storage", func(t *testing.T) { 45 putErr := fmt.Errorf("failed to put data") 46 errGet := kms.ErrKeyNotFound 47 mockStore := &mockStore{ 48 errPut: putErr, 49 errGet: errGet, 50 } 51 52 l := newWriter(mockStore) 53 require.NotEmpty(t, l) 54 someKey := []byte("someKeyData") 55 n, err := l.Write(someKey) 56 require.EqualError(t, err, putErr.Error()) 57 require.Equal(t, 0, n) 58 }) 59 60 t.Run("error case - create a storeWriter using a bad storeReader from storage", func(t *testing.T) { 61 errGet := errors.New("failed to get data") 62 mockStore := &mockStore{ 63 errGet: errGet, 64 } 65 66 l := newWriter(mockStore) 67 require.NotEmpty(t, l) 68 someKey := []byte("someKeyData") 69 n, err := l.Write(someKey) 70 require.EqualError(t, err, errGet.Error()) 71 require.Equal(t, 0, n) 72 }) 73 74 t.Run("error case - create a storeWriter with a keysetID using a bad storeReader from storage", 75 func(t *testing.T) { 76 errGet := errors.New("failed to get data") 77 mockStore := &mockStore{ 78 errGet: errGet, 79 } 80 81 l := newWriter(mockStore, kmsapi.WithKeyID(base64.RawURLEncoding.EncodeToString(random.GetRandomBytes( 82 uint32(base64.RawURLEncoding.DecodedLen(maxKeyIDLen)))))) 83 84 require.NotEmpty(t, l) 85 someKey := []byte("someKeyData") 86 n, err := l.Write(someKey) 87 require.EqualError(t, err, "got error while verifying requested ID: "+errGet.Error()) 88 require.Equal(t, 0, n) 89 }) 90 91 t.Run("error case - import duplicate keysetID", func(t *testing.T) { 92 mockStore := newInMemoryKMSStore() 93 94 l := newWriter(mockStore) 95 require.NotEmpty(t, l) 96 someKey := random.GetRandomBytes(uint32(32)) 97 n, err := l.Write(someKey) 98 require.NoError(t, err) 99 require.Equal(t, len(someKey), n) 100 require.Equal(t, maxKeyIDLen, len(l.KeysetID)) 101 key, ok := mockStore.keys[l.KeysetID] 102 require.True(t, ok) 103 require.Equal(t, key, someKey) 104 105 require.Equal(t, 1, len(mockStore.keys)) 106 107 // create s second writer with keysetID created above 108 l2 := newWriter(mockStore, kmsapi.WithKeyID(l.KeysetID)) 109 110 _, err = l2.Write(someKey) 111 require.EqualError(t, err, fmt.Sprintf("requested ID '%s' already exists, cannot write keyset", 112 l.KeysetID)) 113 }) 114 }