code.vegaprotocol.io/vega@v0.79.0/wallet/api/admin_verify_message_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 api_test 17 18 import ( 19 "context" 20 "testing" 21 22 "code.vegaprotocol.io/vega/libs/jsonrpc" 23 vgrand "code.vegaprotocol.io/vega/libs/rand" 24 "code.vegaprotocol.io/vega/wallet/api" 25 26 "github.com/golang/mock/gomock" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestAdminVerifyMessage(t *testing.T) { 31 t.Run("Documentation matches the code", testAdminVerifyMessageSchemaCorrect) 32 t.Run("verify message with invalid params fails", testVerifyMessageWithInvalidParamsFails) 33 } 34 35 func testAdminVerifyMessageSchemaCorrect(t *testing.T) { 36 assertEqualSchema(t, "admin.verify_message", api.AdminVerifyMessageParams{}, api.AdminVerifyMessageResult{}) 37 } 38 39 func testVerifyMessageWithInvalidParamsFails(t *testing.T) { 40 tcs := []struct { 41 name string 42 params interface{} 43 expectedError error 44 }{ 45 { 46 name: "with nil params", 47 params: nil, 48 expectedError: api.ErrParamsRequired, 49 }, 50 { 51 name: "with wrong type of params", 52 params: "test", 53 expectedError: api.ErrParamsDoNotMatch, 54 }, 55 { 56 name: "with empty publickey", 57 params: api.AdminVerifyMessageParams{ 58 PublicKey: "", 59 }, 60 expectedError: api.ErrPublicKeyIsRequired, 61 }, 62 { 63 name: "with empty message", 64 params: api.AdminVerifyMessageParams{ 65 PublicKey: vgrand.RandomStr(5), 66 EncodedMessage: "", 67 }, 68 expectedError: api.ErrMessageIsRequired, 69 }, 70 { 71 name: "with non-base64 message", 72 params: api.AdminVerifyMessageParams{ 73 PublicKey: vgrand.RandomStr(5), 74 EncodedMessage: "blahh", 75 EncodedSignature: "sigsig", 76 }, 77 expectedError: api.ErrEncodedMessageIsNotValidBase64String, 78 }, 79 { 80 name: "with empty signature", 81 params: api.AdminVerifyMessageParams{ 82 PublicKey: vgrand.RandomStr(5), 83 EncodedMessage: "blah", 84 EncodedSignature: "", 85 }, 86 expectedError: api.ErrSignatureIsRequired, 87 }, 88 { 89 name: "with non-base64 signature", 90 params: api.AdminVerifyMessageParams{ 91 PublicKey: vgrand.RandomStr(5), 92 EncodedMessage: "blah", 93 EncodedSignature: "blahh", 94 }, 95 expectedError: api.ErrEncodedSignatureIsNotValidBase64String, 96 }, 97 } 98 99 for _, tc := range tcs { 100 t.Run(tc.name, func(tt *testing.T) { 101 // given 102 ctx := context.Background() 103 104 // setup 105 handler := newVerifyMessageHandler(tt) 106 107 // when 108 result, errorDetails := handler.handle(t, ctx, tc.params) 109 110 // then 111 assertInvalidParams(tt, errorDetails, tc.expectedError) 112 assert.Empty(tt, result) 113 }) 114 } 115 } 116 117 type verifyMessageHandler struct { 118 *api.AdminVerifyMessage 119 ctrl *gomock.Controller 120 } 121 122 func (h *verifyMessageHandler) handle(t *testing.T, ctx context.Context, params jsonrpc.Params) (api.AdminSignMessageResult, *jsonrpc.ErrorDetails) { 123 t.Helper() 124 125 rawResult, err := h.Handle(ctx, params) 126 if rawResult != nil { 127 result, ok := rawResult.(api.AdminSignMessageResult) 128 if !ok { 129 t.Fatal("AdminUpdatePermissions handler result is not a AdminSignTransactionResult") 130 } 131 return result, err 132 } 133 return api.AdminSignMessageResult{}, err 134 } 135 136 func newVerifyMessageHandler(t *testing.T) *verifyMessageHandler { 137 t.Helper() 138 139 ctrl := gomock.NewController(t) 140 141 return &verifyMessageHandler{ 142 AdminVerifyMessage: api.NewAdminVerifyMessage(), 143 ctrl: ctrl, 144 } 145 }