github.com/hyperledger/aries-framework-go@v0.3.2/pkg/wallet/profile_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  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  
    16  	mockkms "github.com/hyperledger/aries-framework-go/pkg/mock/kms"
    17  	"github.com/hyperledger/aries-framework-go/pkg/mock/secretlock"
    18  	mockstorage "github.com/hyperledger/aries-framework-go/pkg/mock/storage"
    19  )
    20  
    21  const (
    22  	sampleProfileUser      = "sampleProfileUser#01"
    23  	sampleKeyServerURL     = "sample/keyserver/test"
    24  	sampleMasterCipherText = "sample-master-cipher"
    25  	sampleCustomProfileErr = "sample profile custom error"
    26  )
    27  
    28  func TestCreateNewProfile(t *testing.T) {
    29  	t.Run("test create new profile with key server URL", func(t *testing.T) {
    30  		profile, err := createProfile(sampleProfileUser,
    31  			&profileOpts{passphrase: "", secretLockSvc: nil, keyServerURL: sampleKeyServerURL})
    32  
    33  		require.NoError(t, err)
    34  		require.NotEmpty(t, profile)
    35  		require.NotEmpty(t, profile.ID)
    36  		require.Equal(t, profile.KeyServerURL, sampleKeyServerURL)
    37  		require.Empty(t, profile.MasterLockCipher)
    38  	})
    39  
    40  	t.Run("test create new profile with passphrase", func(t *testing.T) {
    41  		profile, err := createProfile(sampleProfileUser,
    42  			&profileOpts{passphrase: samplePassPhrase, secretLockSvc: nil, keyServerURL: sampleKeyServerURL})
    43  
    44  		require.NoError(t, err)
    45  		require.NotEmpty(t, profile)
    46  		require.NotEmpty(t, profile.ID)
    47  		require.Empty(t, profile.KeyServerURL, "")
    48  		require.NotEmpty(t, profile.MasterLockCipher)
    49  	})
    50  
    51  	t.Run("test create new profile with secret lock service", func(t *testing.T) {
    52  		profile, err := createProfile(sampleProfileUser,
    53  			&profileOpts{passphrase: "", secretLockSvc: &secretlock.MockSecretLock{
    54  				ValEncrypt: sampleMasterCipherText,
    55  			}, keyServerURL: sampleKeyServerURL})
    56  
    57  		require.NoError(t, err)
    58  		require.NotEmpty(t, profile)
    59  		require.NotEmpty(t, profile.ID)
    60  		require.Empty(t, profile.KeyServerURL, "")
    61  		require.Equal(t, profile.MasterLockCipher, sampleMasterCipherText)
    62  	})
    63  
    64  	t.Run("test create new profile with EDV conf", func(t *testing.T) {
    65  		profile, err := createProfile(sampleProfileUser,
    66  			&profileOpts{
    67  				passphrase: samplePassPhrase, secretLockSvc: nil, keyServerURL: sampleKeyServerURL,
    68  				edvConf: &edvConf{
    69  					ServerURL:       "sample-server-url",
    70  					VaultID:         "sample-vault-ID",
    71  					EncryptionKeyID: "sample-enc-kid",
    72  					MACKeyID:        "sample-mac-kid",
    73  				},
    74  			})
    75  
    76  		require.NoError(t, err)
    77  		require.NotEmpty(t, profile)
    78  		require.NotEmpty(t, profile.ID)
    79  		require.Empty(t, profile.KeyServerURL, "")
    80  		require.NotEmpty(t, profile.MasterLockCipher)
    81  		require.NotEmpty(t, profile.EDVConf)
    82  		require.NotEmpty(t, profile.EDVConf.ServerURL)
    83  		require.NotEmpty(t, profile.EDVConf.VaultID)
    84  		require.NotEmpty(t, profile.EDVConf.EncryptionKeyID)
    85  		require.NotEmpty(t, profile.EDVConf.MACKeyID)
    86  	})
    87  
    88  	t.Run("test create new profile failure", func(t *testing.T) {
    89  		// invalid profile option
    90  		profile, err := createProfile(sampleProfileUser,
    91  			&profileOpts{passphrase: "", secretLockSvc: nil, keyServerURL: ""})
    92  
    93  		require.Empty(t, profile)
    94  		require.Error(t, err)
    95  		require.EqualError(t, err, "invalid create profile options")
    96  
    97  		// secret lock service error
    98  		profile, err = createProfile(sampleProfileUser,
    99  			&profileOpts{passphrase: "", secretLockSvc: &secretlock.MockSecretLock{
   100  				ErrEncrypt: fmt.Errorf(sampleCustomProfileErr),
   101  			}, keyServerURL: ""})
   102  
   103  		require.Empty(t, profile)
   104  		require.Error(t, err)
   105  		require.Contains(t, err.Error(), "failed to create master lock from secret lock service provided")
   106  		require.Contains(t, err.Error(), sampleCustomProfileErr)
   107  
   108  		// invalid EDV settings
   109  		profile, err = createProfile(sampleProfileUser,
   110  			&profileOpts{
   111  				passphrase: samplePassPhrase, secretLockSvc: nil, keyServerURL: sampleKeyServerURL,
   112  				edvConf: &edvConf{
   113  					ServerURL: "sample-server-url",
   114  				},
   115  			})
   116  		require.Empty(t, profile)
   117  		require.Error(t, err)
   118  		require.Contains(t, err.Error(), "invalid EDV settings in profile")
   119  	})
   120  
   121  	t.Run("test setup edv keys", func(t *testing.T) {
   122  		sampleProfile := &profile{
   123  			EDVConf: &edvConf{
   124  				ServerURL: "sample-server-url",
   125  				VaultID:   "sample-vault-id",
   126  			},
   127  		}
   128  
   129  		kid := "sample-kid"
   130  
   131  		// setup edv keys
   132  		err := sampleProfile.setupEDVEncryptionKey(&mockkms.KeyManager{CreateKeyID: kid})
   133  		require.NoError(t, err)
   134  		require.Equal(t, sampleProfile.EDVConf.EncryptionKeyID, kid)
   135  
   136  		err = sampleProfile.setupEDVMacKey(&mockkms.KeyManager{CreateKeyID: kid})
   137  		require.NoError(t, err)
   138  		require.Equal(t, sampleProfile.EDVConf.MACKeyID, kid)
   139  
   140  		// test create key error
   141  		err = sampleProfile.setupEDVEncryptionKey(&mockkms.KeyManager{CreateKeyErr: errors.New(sampleKeyMgrErr)})
   142  		require.Error(t, err)
   143  		require.Contains(t, err.Error(), sampleKeyMgrErr)
   144  
   145  		err = sampleProfile.setupEDVMacKey(&mockkms.KeyManager{CreateKeyErr: errors.New(sampleKeyMgrErr)})
   146  		require.Error(t, err)
   147  		require.Contains(t, err.Error(), sampleKeyMgrErr)
   148  	})
   149  }
   150  
   151  func TestProfileStore(t *testing.T) {
   152  	t.Run("test create new profile store instance", func(t *testing.T) {
   153  		// success
   154  		profileStore, err := newProfileStore(mockstorage.NewMockStoreProvider())
   155  		require.NoError(t, err)
   156  		require.NotEmpty(t, profileStore)
   157  
   158  		// error
   159  		profileStore, err = newProfileStore(&mockstorage.MockStoreProvider{
   160  			ErrOpenStoreHandle: fmt.Errorf(sampleCustomProfileErr),
   161  		})
   162  		require.Error(t, err)
   163  		require.EqualError(t, err, sampleCustomProfileErr)
   164  		require.Empty(t, profileStore)
   165  	})
   166  
   167  	t.Run("test save profiles in store", func(t *testing.T) {
   168  		profileStore, err := newProfileStore(mockstorage.NewMockStoreProvider())
   169  		require.NoError(t, err)
   170  		require.NotEmpty(t, profileStore)
   171  
   172  		// success save
   173  		err = profileStore.save(&profile{User: sampleProfileUser}, false)
   174  		require.NoError(t, err)
   175  		result, err := profileStore.get(sampleProfileUser)
   176  		require.NoError(t, err)
   177  		require.Equal(t, result.User, sampleProfileUser)
   178  
   179  		// save existing profile
   180  		err = profileStore.save(&profile{User: sampleProfileUser}, false)
   181  		require.Error(t, err)
   182  		require.EqualError(t, err, "profile already exists for given user")
   183  
   184  		// save override existing profile
   185  		err = profileStore.save(&profile{User: sampleProfileUser}, true)
   186  		require.NoError(t, err)
   187  		result, err = profileStore.get(sampleProfileUser)
   188  		require.NoError(t, err)
   189  		require.Equal(t, result.User, sampleProfileUser)
   190  	})
   191  
   192  	t.Run("test get profiles from store", func(t *testing.T) {
   193  		profileStore, err := newProfileStore(mockstorage.NewMockStoreProvider())
   194  		require.NoError(t, err)
   195  		require.NotEmpty(t, profileStore)
   196  
   197  		// setup data
   198  		err = profileStore.save(&profile{User: sampleProfileUser}, false)
   199  		require.NoError(t, err)
   200  
   201  		// get profile from store
   202  		result, err := profileStore.get(sampleProfileUser)
   203  		require.NoError(t, err)
   204  		require.Equal(t, result.User, sampleProfileUser)
   205  
   206  		// get non-existing profile from store
   207  		result, err = profileStore.get("non-existing-user")
   208  		require.Empty(t, result)
   209  		require.Error(t, err)
   210  		require.Equal(t, err, ErrProfileNotFound)
   211  	})
   212  
   213  	t.Run("test errors while getting profiles from store", func(t *testing.T) {
   214  		const sampleProfileUser2 = "sampleProfileUser#02"
   215  		profileStore, err := newProfileStore(&mockstorage.MockStoreProvider{
   216  			Store: &mockstorage.MockStore{
   217  				ErrGet: fmt.Errorf(sampleCustomProfileErr),
   218  			},
   219  		})
   220  		require.NoError(t, err)
   221  		require.NotEmpty(t, profileStore)
   222  
   223  		// get profile from store
   224  		result, err := profileStore.get(sampleProfileUser)
   225  		require.Empty(t, result)
   226  		require.Error(t, err)
   227  		require.EqualError(t, err, sampleCustomProfileErr)
   228  
   229  		// unmarshal error test
   230  		profileStore, err = newProfileStore(&mockstorage.MockStoreProvider{
   231  			Store: &mockstorage.MockStore{
   232  				Store: map[string]mockstorage.DBEntry{
   233  					getUserKeyPrefix(sampleProfileUser2): {Value: []byte("----")},
   234  				},
   235  			},
   236  		})
   237  		require.NoError(t, err)
   238  
   239  		// put invalid data in store to get unmarshal error
   240  		result, err = profileStore.get(sampleProfileUser2)
   241  		require.Empty(t, result)
   242  		require.Error(t, err)
   243  		require.Contains(t, err.Error(), "invalid character")
   244  	})
   245  }