github.com/hyperledger/aries-framework-go@v0.3.2/pkg/wallet/storage_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package wallet 8 9 import ( 10 "errors" 11 "testing" 12 13 "github.com/google/uuid" 14 "github.com/stretchr/testify/require" 15 16 "github.com/hyperledger/aries-framework-go/component/storage/edv" 17 kmsapi "github.com/hyperledger/aries-framework-go/pkg/kms" 18 mockcrypto "github.com/hyperledger/aries-framework-go/pkg/mock/crypto" 19 mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms" 20 mockstorage "github.com/hyperledger/aries-framework-go/pkg/mock/storage" 21 "github.com/hyperledger/aries-framework-go/spi/storage" 22 ) 23 24 func TestStorageProvider_OpenStore(t *testing.T) { 25 sampleUser := uuid.New().String() 26 masterLock, err := getDefaultSecretLock(samplePassPhrase) 27 require.NoError(t, err) 28 29 masterLockCipherText, err := createMasterLock(masterLock) 30 require.NoError(t, err) 31 require.NotEmpty(t, masterLockCipherText) 32 33 profileInfo := &profile{ 34 User: sampleUser, 35 MasterLockCipher: masterLockCipherText, 36 } 37 38 kmsStore, err := kmsapi.NewAriesProviderWrapper(mockstorage.NewMockStoreProvider()) 39 require.NoError(t, err) 40 41 kmgr, err := keyManager().createKeyManager(profileInfo, kmsStore, 42 &unlockOpts{passphrase: samplePassPhrase}) 43 require.NoError(t, err) 44 require.NotEmpty(t, kmgr) 45 46 token, err := sessionManager().createSession(uuid.New().String(), kmgr, 0) 47 48 require.NoError(t, err) 49 require.NotEmpty(t, token) 50 51 t.Run("successfully open store", func(t *testing.T) { 52 sp := getMockStorageProvider() 53 sampleProfile := &profile{ID: uuid.New().String(), User: uuid.New().String()} 54 wsp := newWalletStorageProvider(sampleProfile, sp) 55 56 store, e := wsp.OpenStore(kmgr, &unlockOpts{}, 57 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 58 59 require.NoError(t, e) 60 require.NotEmpty(t, store) 61 require.Len(t, sp.config.TagNames, 1) 62 }) 63 64 t.Run("successfully open EDV store", func(t *testing.T) { 65 sampleProfile := &profile{ 66 ID: uuid.New().String(), User: uuid.New().String(), 67 EDVConf: &edvConf{ 68 ServerURL: "sample-server", 69 VaultID: "sample-vault-ID", 70 }, 71 KeyServerURL: sampleKeyServerURL, 72 } 73 74 err = sampleProfile.setupEDVEncryptionKey(kmgr) 75 require.NoError(t, err) 76 77 err = sampleProfile.setupEDVMacKey(kmgr) 78 require.NoError(t, err) 79 80 wsp := newWalletStorageProvider(sampleProfile, nil) 81 82 store, err := wsp.OpenStore(kmgr, &unlockOpts{}, 83 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 84 require.NoError(t, err) 85 require.NotEmpty(t, store) 86 87 // with edv opts 88 store, err = wsp.OpenStore(kmgr, &unlockOpts{ 89 edvOpts: []edv.RESTProviderOption{ 90 edv.WithFullDocumentsReturnedFromQueries(), 91 edv.WithBatchEndpointExtension(), 92 }, 93 }, storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 94 require.NoError(t, err) 95 require.NotEmpty(t, store) 96 97 // no edv opts 98 store, err = wsp.OpenStore(kmgr, &unlockOpts{}, 99 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 100 require.NoError(t, err) 101 require.NotEmpty(t, store) 102 }) 103 104 t.Run("failed to open EDV store", func(t *testing.T) { 105 sampleProfile := &profile{ 106 ID: uuid.New().String(), User: uuid.New().String(), 107 EDVConf: &edvConf{ 108 ServerURL: "sample-server", 109 VaultID: "sample-vault-ID", 110 }, 111 } 112 113 // invalid settings 114 wsp := newWalletStorageProvider(sampleProfile, nil) 115 store, err := wsp.OpenStore(kmgr, &unlockOpts{}, 116 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 117 require.Error(t, err) 118 require.Contains(t, err.Error(), "invalid EDV configuration found in wallet profile") 119 require.Empty(t, store) 120 121 err = sampleProfile.setupEDVEncryptionKey(kmgr) 122 require.NoError(t, err) 123 124 err = sampleProfile.setupEDVMacKey(kmgr) 125 require.NoError(t, err) 126 127 wsp = newWalletStorageProvider(sampleProfile, nil) 128 129 // incorrect mac key ID 130 wsp.profile.EDVConf.MACKeyID += "x" 131 store, err = wsp.OpenStore(kmgr, &unlockOpts{}, 132 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 133 require.Error(t, err) 134 require.Contains(t, err.Error(), "failed to create mac crypto") 135 require.Empty(t, store) 136 137 // incorrect encryption key ID 138 wsp.profile.EDVConf.EncryptionKeyID += "x" 139 store, err = wsp.OpenStore(kmgr, &unlockOpts{}, 140 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 141 require.Error(t, err) 142 require.Contains(t, err.Error(), "failed to create JWE encrypter") 143 require.Empty(t, store) 144 145 val, err := getJWSEncrypter("sample-kid", &mockkms.KeyManager{ 146 ExportPubKeyBytesValue: []byte("invalid"), 147 }, &mockcrypto.Crypto{}) 148 require.Error(t, err) 149 require.Contains(t, err.Error(), "failed to unmarshal JWE public key bytes to an EC public key") 150 require.Empty(t, val) 151 }) 152 153 t.Run("failed to set store config", func(t *testing.T) { 154 sp := getMockStorageProvider() 155 sampleProfile := &profile{ID: uuid.New().String(), User: uuid.New().String()} 156 wsp := newWalletStorageProvider(sampleProfile, sp) 157 158 sp.failure = errors.New(sampleWalletErr) 159 sp.Store.ErrClose = errors.New(sampleWalletErr) 160 161 store, err := wsp.OpenStore(kmgr, &unlockOpts{}, 162 storage.StoreConfiguration{TagNames: []string{Credential.Name()}}) 163 require.Error(t, err) 164 require.Empty(t, store) 165 }) 166 }