From 4a970683d17db29579c4454c96139a401b094355 Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 1 Feb 2023 20:28:32 +0800 Subject: [PATCH] throw error if account already exist when doing local pairing on receiver side (#3091) --- server/pairing/payload_manager.go | 17 ++++++++++++++--- server/pairing/payload_manager_test.go | 7 +++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/server/pairing/payload_manager.go b/server/pairing/payload_manager.go index e6eb8ac42..2c005955d 100644 --- a/server/pairing/payload_manager.go +++ b/server/pairing/payload_manager.go @@ -3,6 +3,7 @@ package pairing import ( "crypto/rand" "encoding/json" + "errors" "fmt" "io/ioutil" "os" @@ -20,6 +21,10 @@ import ( "github.com/status-im/status-go/protocol/protobuf" ) +var ( + ErrKeyFileAlreadyExists = errors.New("key file already exists") +) + // PayloadManager is the interface for PayloadManagers and wraps the basic functions for fulfilling payload management type PayloadManager interface { // Mount Loads the payload into the PayloadManager's state @@ -475,10 +480,16 @@ func (apr *AccountPayloadRepository) storeKeys(keyStorePath string) error { return fmt.Errorf("no known Key UID") } keyStorePath = filepath.Join(keyStorePath, apr.multiaccount.KeyUID) - - err := os.MkdirAll(keyStorePath, 0777) - if err != nil { + _, err := os.Stat(keyStorePath) + if os.IsNotExist(err) { + err := os.MkdirAll(keyStorePath, 0777) + if err != nil { + return err + } + } else if err != nil { return err + } else { + return ErrKeyFileAlreadyExists } } diff --git a/server/pairing/payload_manager_test.go b/server/pairing/payload_manager_test.go index 2b014321f..776315cb7 100644 --- a/server/pairing/payload_manager_test.go +++ b/server/pairing/payload_manager_test.go @@ -70,7 +70,7 @@ func makeKeystores(t *testing.T) (string, string, func()) { keyStoreDir = filepath.Join(keyStoreDir, "keystore", keyUID) // TODO test case where the keystore dir does not yet exist because the device is new - emptyKeyStoreDir = filepath.Join(emptyKeyStoreDir, "keystore", keyUID) + emptyKeyStoreDir = filepath.Join(emptyKeyStoreDir, "keystore") err = os.MkdirAll(keyStoreDir, 0777) require.NoError(t, err) @@ -301,7 +301,7 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_StorePayloads() { pms.Require().NoError(err) // TEST PairingPayloadRepository 2 StoreToSource() - keys := getFiles(pms.T(), pms.config2.KeystorePath) + keys := getFiles(pms.T(), filepath.Join(pms.config2.KeystorePath, pms.config2.KeyUID)) pms.Require().Len(keys, 2) pms.Require().Len(keys[utils.GetAccount1PKFile()], 489) @@ -326,6 +326,9 @@ func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_StorePayloads() { pms.Require().Exactly(expected.Name, acc.Name) pms.Require().Exactly(expected.Timestamp, acc.Timestamp) pms.Require().Len(acc.Images, 2) + + err = ppr2.StoreToSource() + pms.Require().ErrorIs(err, ErrKeyFileAlreadyExists) } func (pms *PayloadMarshallerSuite) TestPayloadMarshaller_LockPayload() {