github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/identity/selector/handler_test.go (about) 1 /* 2 * Copyright (C) 2017 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package selector 19 20 import ( 21 "testing" 22 23 "github.com/mysteriumnetwork/node/identity" 24 "github.com/stretchr/testify/assert" 25 ) 26 27 var fakeSignerFactory = func(id identity.Identity) identity.Signer { 28 return &fakeSigner{} 29 } 30 var existingIdentity = identity.Identity{Address: "existing"} 31 var newIdentity = identity.Identity{Address: "new"} 32 var chainID int64 = 1 33 34 func TestUseOrCreateSucceeds(t *testing.T) { 35 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 36 registry := &mockRegistry{} 37 cache := identity.NewIdentityCacheFake() 38 39 handler := NewHandler(identityManager, cache, fakeSignerFactory) 40 41 id, err := handler.UseOrCreate(existingIdentity.Address, "pass", chainID) 42 assert.Equal(t, existingIdentity, id) 43 assert.Nil(t, err) 44 assert.Equal(t, "", registry.registeredIdentity.Address) 45 46 assert.Equal(t, existingIdentity.Address, identityManager.LastUnlockAddress) 47 assert.Equal(t, "pass", identityManager.LastUnlockPassphrase) 48 assert.Equal(t, chainID, identityManager.LastUnlockChainID) 49 } 50 51 func TestUseOrCreateFailsWhenUnlockFails(t *testing.T) { 52 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 53 identityManager.MarkUnlockToFail() 54 cache := identity.NewIdentityCacheFake() 55 56 handler := NewHandler(identityManager, cache, fakeSignerFactory) 57 58 _, err := handler.UseOrCreate(existingIdentity.Address, "pass", chainID) 59 assert.Error(t, err) 60 61 assert.Equal(t, existingIdentity.Address, identityManager.LastUnlockAddress) 62 assert.Equal(t, "pass", identityManager.LastUnlockPassphrase) 63 assert.Equal(t, chainID, identityManager.LastUnlockChainID) 64 } 65 66 func TestUseOrCreateReturnsFirstIdentity(t *testing.T) { 67 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity, newIdentity}, newIdentity) 68 cache := identity.NewIdentityCacheFake() 69 _ = cache.StoreIdentity(existingIdentity) 70 71 handler := NewHandler(identityManager, cache, fakeSignerFactory) 72 73 id, err := handler.UseOrCreate("", "pass", chainID) 74 assert.Equal(t, existingIdentity, id) 75 assert.Nil(t, err) 76 } 77 78 func TestUseOrCreateFailsWhenIdentityNotFound(t *testing.T) { 79 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 80 cache := identity.NewIdentityCacheFake() 81 82 handler := NewHandler(identityManager, cache, fakeSignerFactory) 83 _, err := handler.UseOrCreate("does-not-exist", "pass", chainID) 84 assert.NotNil(t, err) 85 } 86 87 func TestUseFailsWhenIdentityNotFound(t *testing.T) { 88 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 89 cache := identity.NewIdentityCacheFake() 90 91 handler := NewHandler(identityManager, cache, fakeSignerFactory) 92 93 _, err := handler.UseOrCreate("does-not-exist", "pass", chainID) 94 assert.NotNil(t, err) 95 } 96 97 func TestUseLastSucceeds(t *testing.T) { 98 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 99 registry := &mockRegistry{} 100 cache := identity.NewIdentityCacheFake() 101 102 fakeIdentity := identity.FromAddress("abc") 103 _ = cache.StoreIdentity(fakeIdentity) 104 105 handler := NewHandler(identityManager, cache, fakeSignerFactory) 106 107 id, err := handler.useLast("pass", chainID) 108 assert.Equal(t, fakeIdentity, id) 109 assert.Nil(t, err) 110 111 assert.Equal(t, "", registry.registeredIdentity.Address) 112 113 assert.Equal(t, "abc", identityManager.LastUnlockAddress) 114 assert.Equal(t, "pass", identityManager.LastUnlockPassphrase) 115 assert.Equal(t, chainID, identityManager.LastUnlockChainID) 116 } 117 118 func TestUseLastFailsWhenUnlockFails(t *testing.T) { 119 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 120 identityManager.MarkUnlockToFail() 121 registry := &mockRegistry{} 122 cache := identity.NewIdentityCacheFake() 123 124 fakeIdentity := identity.FromAddress("abc") 125 _ = cache.StoreIdentity(fakeIdentity) 126 127 handler := NewHandler(identityManager, cache, fakeSignerFactory) 128 129 _, err := handler.useLast("pass", chainID) 130 assert.Error(t, err) 131 132 assert.Equal(t, "", registry.registeredIdentity.Address) 133 134 assert.Equal(t, "abc", identityManager.LastUnlockAddress) 135 assert.Equal(t, "pass", identityManager.LastUnlockPassphrase) 136 assert.Equal(t, chainID, identityManager.LastUnlockChainID) 137 } 138 139 func TestUseNewSucceeds(t *testing.T) { 140 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 141 cache := identity.NewIdentityCacheFake() 142 143 handler := NewHandler(identityManager, cache, fakeSignerFactory) 144 145 id, err := handler.useNew("pass", chainID) 146 assert.Equal(t, newIdentity, id) 147 assert.Nil(t, err) 148 149 assert.Equal(t, newIdentity.Address, identityManager.LastUnlockAddress) 150 assert.Equal(t, "pass", identityManager.LastUnlockPassphrase) 151 assert.Equal(t, chainID, identityManager.LastUnlockChainID) 152 } 153 154 func TestUseNewFailsWhenUnlockFails(t *testing.T) { 155 identityManager := identity.NewIdentityManagerFake([]identity.Identity{existingIdentity}, newIdentity) 156 identityManager.MarkUnlockToFail() 157 cache := identity.NewIdentityCacheFake() 158 159 handler := NewHandler(identityManager, cache, fakeSignerFactory) 160 161 _, err := handler.useNew("pass", chainID) 162 assert.Error(t, err) 163 164 assert.Equal(t, newIdentity.Address, identityManager.LastUnlockAddress) 165 assert.Equal(t, "pass", identityManager.LastUnlockPassphrase) 166 assert.Equal(t, chainID, identityManager.LastUnlockChainID) 167 } 168 169 type fakeSigner struct { 170 } 171 172 func (fs *fakeSigner) Sign(message []byte) (identity.Signature, error) { 173 return identity.SignatureBase64("deadbeef"), nil 174 } 175 176 type mockRegistry struct { 177 registeredIdentity identity.Identity 178 } 179 180 func (mr *mockRegistry) IdentityExists(identity.Identity, identity.Signer) (bool, error) { 181 return true, nil 182 } 183 184 func (mr *mockRegistry) RegisterIdentity(ID identity.Identity, signer identity.Signer) error { 185 mr.registeredIdentity = ID 186 return nil 187 }