code.vegaprotocol.io/vega@v0.79.0/wallet/service/store/v1/store_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package v1_test 17 18 import ( 19 "testing" 20 "time" 21 22 vgencoding "code.vegaprotocol.io/vega/libs/encoding" 23 vgrand "code.vegaprotocol.io/vega/libs/rand" 24 vgtest "code.vegaprotocol.io/vega/libs/test" 25 "code.vegaprotocol.io/vega/paths" 26 "code.vegaprotocol.io/vega/wallet/service" 27 storeV1 "code.vegaprotocol.io/vega/wallet/service/store/v1" 28 v1 "code.vegaprotocol.io/vega/wallet/service/v1" 29 30 "github.com/stretchr/testify/assert" 31 "github.com/stretchr/testify/require" 32 "go.uber.org/zap" 33 ) 34 35 func TestFileStoreV1(t *testing.T) { 36 t.Run("New store succeeds", testNewStoreSucceeds) 37 t.Run("Saving already existing RSA keys succeeds", testFileStoreV1SaveAlreadyExistingRSAKeysSucceeds) 38 t.Run("Saving RSA keys succeeds", testFileStoreV1SaveRSAKeysSucceeds) 39 t.Run("Verifying non-existing RSA keys fails", testFileStoreV1VerifyingNonExistingRSAKeysFails) 40 t.Run("Verifying existing RSA keys succeeds", testFileStoreV1VerifyingExistingRSAKeysSucceeds) 41 t.Run("Getting non-existing RSA keys fails", testFileStoreV1GetNonExistingRSAKeysFails) 42 t.Run("Getting existing RSA keys succeeds", testFileStoreV1GetExistingRSAKeysSucceeds) 43 t.Run("Getting config while not being initialised succeeds", testFileStoreV1GetConfigWhileNotInitialisedSucceeds) 44 t.Run("Saving config succeeds", testFileStoreV1SavingConfigSucceeds) 45 t.Run("Verifying config succeeds", testFileStoreV1VerifyingConfigSucceeds) 46 } 47 48 func testNewStoreSucceeds(t *testing.T) { 49 vegaHome := newVegaHome(t) 50 51 s, err := storeV1.InitialiseStore(vegaHome) 52 53 require.NoError(t, err) 54 assert.NotNil(t, s) 55 vgtest.AssertDirAccess(t, rsaKeysHome(t, vegaHome)) 56 } 57 58 func testFileStoreV1SaveAlreadyExistingRSAKeysSucceeds(t *testing.T) { 59 vegaHome := newVegaHome(t) 60 61 // given 62 s := initialiseFromPath(t, vegaHome) 63 keys := &v1.RSAKeys{ 64 Pub: []byte("my public key"), 65 Priv: []byte("my private key"), 66 } 67 68 // when 69 err := s.SaveRSAKeys(keys) 70 71 // then 72 require.NoError(t, err) 73 74 // when 75 err = s.SaveRSAKeys(keys) 76 77 // then 78 require.NoError(t, err) 79 } 80 81 func testFileStoreV1SaveRSAKeysSucceeds(t *testing.T) { 82 vegaHome := newVegaHome(t) 83 84 // given 85 s := initialiseFromPath(t, vegaHome) 86 keys := &v1.RSAKeys{ 87 Pub: []byte("my public key"), 88 Priv: []byte("my private key"), 89 } 90 91 // when 92 err := s.SaveRSAKeys(keys) 93 94 // then 95 require.NoError(t, err) 96 vgtest.AssertFileAccess(t, publicRSAKeyFilePath(t, vegaHome)) 97 vgtest.AssertFileAccess(t, privateRSAKeyFilePath(t, vegaHome)) 98 99 // when 100 returnedKeys, err := s.GetRsaKeys() 101 102 // then 103 require.NoError(t, err) 104 assert.Equal(t, keys, returnedKeys) 105 } 106 107 func testFileStoreV1VerifyingNonExistingRSAKeysFails(t *testing.T) { 108 vegaHome := newVegaHome(t) 109 110 // given 111 s := initialiseFromPath(t, vegaHome) 112 113 // when 114 exists, err := s.RSAKeysExists() 115 116 // then 117 assert.NoError(t, err) 118 assert.False(t, exists) 119 } 120 121 func testFileStoreV1VerifyingExistingRSAKeysSucceeds(t *testing.T) { 122 vegaHome := newVegaHome(t) 123 124 // given 125 s := initialiseFromPath(t, vegaHome) 126 keys := &v1.RSAKeys{ 127 Pub: []byte("my public key"), 128 Priv: []byte("my private key"), 129 } 130 131 // when 132 err := s.SaveRSAKeys(keys) 133 134 // then 135 require.NoError(t, err) 136 vgtest.AssertFileAccess(t, publicRSAKeyFilePath(t, vegaHome)) 137 vgtest.AssertFileAccess(t, privateRSAKeyFilePath(t, vegaHome)) 138 139 // when 140 exists, err := s.RSAKeysExists() 141 142 // then 143 require.NoError(t, err) 144 assert.True(t, exists) 145 } 146 147 func testFileStoreV1GetNonExistingRSAKeysFails(t *testing.T) { 148 vegaHome := newVegaHome(t) 149 150 // given 151 s := initialiseFromPath(t, vegaHome) 152 153 // when 154 keys, err := s.GetRsaKeys() 155 156 // then 157 assert.Error(t, err) 158 assert.Nil(t, keys) 159 } 160 161 func testFileStoreV1GetExistingRSAKeysSucceeds(t *testing.T) { 162 vegaHome := newVegaHome(t) 163 164 // given 165 s := initialiseFromPath(t, vegaHome) 166 keys := &v1.RSAKeys{ 167 Pub: []byte("my public key"), 168 Priv: []byte("my private key"), 169 } 170 171 // when 172 err := s.SaveRSAKeys(keys) 173 174 // then 175 require.NoError(t, err) 176 vgtest.AssertFileAccess(t, publicRSAKeyFilePath(t, vegaHome)) 177 vgtest.AssertFileAccess(t, privateRSAKeyFilePath(t, vegaHome)) 178 179 // when 180 returnedKeys, err := s.GetRsaKeys() 181 182 // then 183 require.NoError(t, err) 184 assert.Equal(t, keys, returnedKeys) 185 } 186 187 func testFileStoreV1GetConfigWhileNotInitialisedSucceeds(t *testing.T) { 188 vegaHome := newVegaHome(t) 189 190 // given 191 s := initialiseFromPath(t, vegaHome) 192 193 // when 194 cfg, err := s.GetConfig() 195 196 // then 197 require.NoError(t, err) 198 assert.Equal(t, service.DefaultConfig(), cfg) 199 } 200 201 func testFileStoreV1SavingConfigSucceeds(t *testing.T) { 202 vegaHome := newVegaHome(t) 203 204 // given 205 s := initialiseFromPath(t, vegaHome) 206 originalCfg := &service.Config{ 207 LogLevel: vgencoding.LogLevel{ 208 Level: zap.DebugLevel, 209 }, 210 Server: service.ServerConfig{ 211 Port: 123456789, 212 Host: vgrand.RandomStr(5), 213 }, 214 APIV1: service.APIV1Config{ 215 MaximumTokenDuration: vgencoding.Duration{ 216 Duration: 234 * time.Second, 217 }, 218 }, 219 } 220 221 // when 222 err := s.SaveConfig(originalCfg) 223 224 // then 225 require.NoError(t, err) 226 227 // when 228 cfg, err := s.GetConfig() 229 230 // then 231 require.NoError(t, err) 232 assert.Equal(t, originalCfg, cfg) 233 } 234 235 func testFileStoreV1VerifyingConfigSucceeds(t *testing.T) { 236 vegaHome := newVegaHome(t) 237 238 // given 239 s := initialiseFromPath(t, vegaHome) 240 originalCfg := &service.Config{ 241 LogLevel: vgencoding.LogLevel{ 242 Level: zap.DebugLevel, 243 }, 244 Server: service.ServerConfig{ 245 Port: 123456789, 246 Host: vgrand.RandomStr(5), 247 }, 248 APIV1: service.APIV1Config{ 249 MaximumTokenDuration: vgencoding.Duration{ 250 Duration: 234 * time.Second, 251 }, 252 }, 253 } 254 255 // when 256 err := s.SaveConfig(originalCfg) 257 258 // then 259 require.NoError(t, err) 260 261 // when 262 cfg, err := s.GetConfig() 263 264 // then 265 require.NoError(t, err) 266 assert.Equal(t, originalCfg, cfg) 267 } 268 269 func initialiseFromPath(t *testing.T, vegaHome *paths.CustomPaths) *storeV1.Store { 270 t.Helper() 271 s, err := storeV1.InitialiseStore(vegaHome) 272 if err != nil { 273 t.Fatalf("couldn't initialise store: %v", err) 274 } 275 276 return s 277 } 278 279 func newVegaHome(t *testing.T) *paths.CustomPaths { 280 t.Helper() 281 return &paths.CustomPaths{CustomHome: t.TempDir()} 282 } 283 284 func rsaKeysHome(t *testing.T, vegaHome *paths.CustomPaths) string { 285 t.Helper() 286 return vegaHome.DataPathFor(paths.WalletServiceRSAKeysDataHome) 287 } 288 289 func publicRSAKeyFilePath(t *testing.T, vegaHome *paths.CustomPaths) string { 290 t.Helper() 291 return vegaHome.DataPathFor(paths.WalletServicePublicRSAKeyDataFile) 292 } 293 294 func privateRSAKeyFilePath(t *testing.T, vegaHome *paths.CustomPaths) string { 295 t.Helper() 296 return vegaHome.DataPathFor(paths.WalletServicePrivateRSAKeyDataFile) 297 }