code.vegaprotocol.io/vega@v0.79.0/cmd/vegawallet/commands/key_rotate_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 cmd_test 17 18 import ( 19 "testing" 20 21 cmd "code.vegaprotocol.io/vega/cmd/vegawallet/commands" 22 "code.vegaprotocol.io/vega/cmd/vegawallet/commands/flags" 23 vgrand "code.vegaprotocol.io/vega/libs/rand" 24 "code.vegaprotocol.io/vega/wallet/api" 25 26 "github.com/stretchr/testify/assert" 27 "github.com/stretchr/testify/require" 28 ) 29 30 func TestRotateKeyFlags(t *testing.T) { 31 t.Run("Valid flags succeeds", testRotateKeyFlagsValidFlagsSucceeds) 32 t.Run("Missing wallet fails", testRotateKeyFlagsMissingWalletFails) 33 t.Run("Missing chain ID fails", testRotateKeyFlagsMissingChainIDFails) 34 t.Run("Missing new public key fails", testRotateKeyFlagsMissingNewPublicKeyFails) 35 t.Run("Missing current public key fails", testRotateKeyFlagsMissingCurrentPublicKeyFails) 36 t.Run("Missing tx height fails", testRotateKeyFlagsMissingTxBlockHeightFails) 37 t.Run("Missing target height fails", testRotateKeyFlagsMissingTargetBlockHeightFails) 38 t.Run("Validate fails when target height is less then tx height", testRotateKeyFlagsTargetFailsWhenBlockHeightIsLessThanTXHeight) 39 } 40 41 func testRotateKeyFlagsValidFlagsSucceeds(t *testing.T) { 42 testDir := t.TempDir() 43 44 // given 45 expectedPassphrase, passphraseFilePath := NewPassphraseFile(t, testDir) 46 walletName := vgrand.RandomStr(10) 47 currentPubKey := vgrand.RandomStr(20) 48 pubKey := vgrand.RandomStr(20) 49 submissionBlockHeight := uint64(20) 50 enactmentBlockHeight := uint64(25) 51 chainID := vgrand.RandomStr(5) 52 53 f := &cmd.RotateKeyFlags{ 54 Wallet: walletName, 55 PassphraseFile: passphraseFilePath, 56 FromPublicKey: currentPubKey, 57 ChainID: chainID, 58 ToPublicKey: pubKey, 59 SubmissionBlockHeight: submissionBlockHeight, 60 EnactmentBlockHeight: enactmentBlockHeight, 61 } 62 63 expectedReq := api.AdminRotateKeyParams{ 64 Wallet: walletName, 65 FromPublicKey: currentPubKey, 66 ChainID: chainID, 67 ToPublicKey: pubKey, 68 SubmissionBlockHeight: submissionBlockHeight, 69 EnactmentBlockHeight: enactmentBlockHeight, 70 } 71 72 // when 73 req, passphrase, err := f.Validate() 74 75 // then 76 require.NoError(t, err) 77 assert.Equal(t, expectedReq, req) 78 assert.Equal(t, expectedPassphrase, passphrase) 79 } 80 81 func testRotateKeyFlagsMissingWalletFails(t *testing.T) { 82 testDir := t.TempDir() 83 84 // given 85 f := newRotateKeyFlags(t, testDir) 86 f.Wallet = "" 87 88 // when 89 req, _, err := f.Validate() 90 91 // then 92 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("wallet")) 93 assert.Empty(t, req) 94 } 95 96 func testRotateKeyFlagsMissingChainIDFails(t *testing.T) { 97 testDir := t.TempDir() 98 99 // given 100 f := newRotateKeyFlags(t, testDir) 101 f.ChainID = "" 102 103 // when 104 req, _, err := f.Validate() 105 106 // then 107 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("chain-id")) 108 assert.Empty(t, req) 109 } 110 111 func testRotateKeyFlagsMissingTxBlockHeightFails(t *testing.T) { 112 testDir := t.TempDir() 113 114 // given 115 f := newRotateKeyFlags(t, testDir) 116 f.SubmissionBlockHeight = 0 117 118 // when 119 req, _, err := f.Validate() 120 121 // then 122 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("tx-height")) 123 assert.Empty(t, req) 124 } 125 126 func testRotateKeyFlagsMissingTargetBlockHeightFails(t *testing.T) { 127 testDir := t.TempDir() 128 129 // given 130 f := newRotateKeyFlags(t, testDir) 131 f.EnactmentBlockHeight = 0 132 133 // when 134 req, _, err := f.Validate() 135 136 // then 137 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("target-height")) 138 assert.Empty(t, req) 139 } 140 141 func testRotateKeyFlagsMissingNewPublicKeyFails(t *testing.T) { 142 testDir := t.TempDir() 143 144 // given 145 f := newRotateKeyFlags(t, testDir) 146 f.ToPublicKey = "" 147 148 // when 149 req, _, err := f.Validate() 150 151 // then 152 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("new-pubkey")) 153 assert.Empty(t, req) 154 } 155 156 func testRotateKeyFlagsMissingCurrentPublicKeyFails(t *testing.T) { 157 testDir := t.TempDir() 158 159 // given 160 f := newRotateKeyFlags(t, testDir) 161 f.FromPublicKey = "" 162 163 // when 164 req, _, err := f.Validate() 165 166 // then 167 assert.ErrorIs(t, err, flags.MustBeSpecifiedError("current-pubkey")) 168 assert.Empty(t, req) 169 } 170 171 func testRotateKeyFlagsTargetFailsWhenBlockHeightIsLessThanTXHeight(t *testing.T) { 172 testDir := t.TempDir() 173 174 // given 175 f := newRotateKeyFlags(t, testDir) 176 f.SubmissionBlockHeight = 25 177 f.EnactmentBlockHeight = 20 178 179 // when 180 req, _, err := f.Validate() 181 182 // then 183 assert.EqualError(t, err, "--target-height flag must be greater than --tx-height") 184 assert.Empty(t, req) 185 } 186 187 func newRotateKeyFlags(t *testing.T, testDir string) *cmd.RotateKeyFlags { 188 t.Helper() 189 190 _, passphraseFilePath := NewPassphraseFile(t, testDir) 191 walletName := vgrand.RandomStr(10) 192 pubKey := vgrand.RandomStr(20) 193 currentPubKey := vgrand.RandomStr(20) 194 chainID := vgrand.RandomStr(5) 195 txBlockHeight := uint64(20) 196 targetBlockHeight := uint64(25) 197 198 return &cmd.RotateKeyFlags{ 199 Wallet: walletName, 200 ToPublicKey: pubKey, 201 FromPublicKey: currentPubKey, 202 PassphraseFile: passphraseFilePath, 203 SubmissionBlockHeight: txBlockHeight, 204 ChainID: chainID, 205 EnactmentBlockHeight: targetBlockHeight, 206 } 207 }