code.vegaprotocol.io/vega@v0.79.0/commands/node_signature_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 commands_test 17 18 import ( 19 "errors" 20 "testing" 21 22 "code.vegaprotocol.io/vega/commands" 23 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 24 25 "github.com/stretchr/testify/assert" 26 ) 27 28 func TestCheckNodeSignature(t *testing.T) { 29 t.Run("Submitting a nil command fails", testNilNodeSignatureFails) 30 t.Run("Submitting a node signature without id fails", testNodeSignatureWithoutIDFails) 31 t.Run("Submitting a node signature with id succeeds", testNodeSignatureWithIDSucceeds) 32 t.Run("Submitting a node signature without sig fails", testNodeSignatureWithoutSigFails) 33 t.Run("Submitting a node signature with sig succeeds", testNodeSignatureWithSigSucceeds) 34 t.Run("Submitting a node signature without kind fails", testNodeSignatureWithoutKindFails) 35 t.Run("Submitting a node signature with invalid kind fails", testNodeSignatureWithInvalidKindFails) 36 t.Run("Submitting a node signature with kind succeeds", testNodeSignatureWithKindSucceeds) 37 } 38 39 func testNilNodeSignatureFails(t *testing.T) { 40 err := checkNodeSignature(nil) 41 42 assert.Error(t, err) 43 } 44 45 func testNodeSignatureWithoutIDFails(t *testing.T) { 46 err := checkNodeSignature(&commandspb.NodeSignature{}) 47 assert.Contains(t, err.Get("node_signature.id"), commands.ErrIsRequired) 48 } 49 50 func testNodeSignatureWithIDSucceeds(t *testing.T) { 51 err := checkNodeSignature(&commandspb.NodeSignature{ 52 Id: "My ID", 53 }) 54 assert.NotContains(t, err.Get("node_signature.id"), commands.ErrIsRequired) 55 } 56 57 func testNodeSignatureWithoutSigFails(t *testing.T) { 58 err := checkNodeSignature(&commandspb.NodeSignature{}) 59 assert.Contains(t, err.Get("node_signature.sig"), commands.ErrIsRequired) 60 } 61 62 func testNodeSignatureWithSigSucceeds(t *testing.T) { 63 err := checkNodeSignature(&commandspb.NodeSignature{ 64 Sig: []byte("0xDEADBEEF"), 65 }) 66 assert.NotContains(t, err.Get("node_signature.sig"), commands.ErrIsRequired) 67 } 68 69 func testNodeSignatureWithoutKindFails(t *testing.T) { 70 err := checkNodeSignature(&commandspb.NodeSignature{}) 71 assert.Contains(t, err.Get("node_signature.kind"), commands.ErrIsRequired) 72 } 73 74 func testNodeSignatureWithInvalidKindFails(t *testing.T) { 75 err := checkNodeSignature(&commandspb.NodeSignature{ 76 Kind: commandspb.NodeSignatureKind(-42), 77 }) 78 assert.Contains(t, err.Get("node_signature.kind"), commands.ErrIsNotValid) 79 } 80 81 func testNodeSignatureWithKindSucceeds(t *testing.T) { 82 testCases := []struct { 83 msg string 84 value commandspb.NodeSignatureKind 85 }{ 86 { 87 msg: "with new kind", 88 value: commandspb.NodeSignatureKind_NODE_SIGNATURE_KIND_ASSET_NEW, 89 }, { 90 msg: "with withdrawal kind", 91 value: commandspb.NodeSignatureKind_NODE_SIGNATURE_KIND_ASSET_WITHDRAWAL, 92 }, 93 } 94 for _, tc := range testCases { 95 t.Run(tc.msg, func(t *testing.T) { 96 err := checkNodeSignature(&commandspb.NodeSignature{ 97 Kind: tc.value, 98 }) 99 assert.NotContains(t, err.Get("node_signature.kind"), commands.ErrIsRequired) 100 assert.NotContains(t, err.Get("node_signature.kind"), commands.ErrIsNotValid) 101 }) 102 } 103 } 104 105 func checkNodeSignature(cmd *commandspb.NodeSignature) commands.Errors { 106 err := commands.CheckNodeSignature(cmd) 107 108 var e commands.Errors 109 if ok := errors.As(err, &e); !ok { 110 return commands.NewErrors() 111 } 112 113 return e 114 }