github.com/core-coin/go-core/v2@v2.1.9/signer/fourbyte/validation_test.go (about) 1 // Copyright 2019 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The go-core library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package fourbyte 18 19 import ( 20 "math/big" 21 "testing" 22 23 "github.com/core-coin/go-core/v2/common" 24 "github.com/core-coin/go-core/v2/common/hexutil" 25 "github.com/core-coin/go-core/v2/signer/core" 26 ) 27 28 func toHexBig(h string) hexutil.Big { 29 b := big.NewInt(0).SetBytes(common.FromHex(h)) 30 return hexutil.Big(*b) 31 } 32 func toHexUint(h string) hexutil.Uint64 { 33 b := big.NewInt(0).SetBytes(common.FromHex(h)) 34 return hexutil.Uint64(b.Uint64()) 35 } 36 func dummyTxArgs(t txtestcase) *core.SendTxArgs { 37 to, _ := common.HexToAddress(t.to) 38 39 from, _ := common.HexToAddress(t.from) 40 n := toHexUint(t.n) 41 energy := toHexUint(t.g) 42 energyPrice := toHexBig(t.gp) 43 value := toHexBig(t.value) 44 var ( 45 data, input *hexutil.Bytes 46 ) 47 if t.d != "" { 48 a := hexutil.Bytes(common.FromHex(t.d)) 49 data = &a 50 } 51 if t.i != "" { 52 a := hexutil.Bytes(common.FromHex(t.i)) 53 input = &a 54 55 } 56 args := &core.SendTxArgs{ 57 From: from, 58 To: &to, 59 Value: value, 60 Nonce: n, 61 EnergyPrice: energyPrice, 62 Energy: energy, 63 Data: data, 64 Input: input, 65 } 66 if t.to == "" { 67 args.To = nil 68 } 69 return args 70 } 71 72 type txtestcase struct { 73 from, to, n, g, gp, value, d, i string 74 expectErr bool 75 numMessages int 76 } 77 78 func TestTransactionValidation(t *testing.T) { 79 var ( 80 // use empty db, there are other tests for the abi-specific stuff 81 db = newEmpty() 82 ) 83 testcases := []txtestcase{ 84 // Invalid to checksum 85 // valid 0x000000000000000000000000000000000000dEaD 86 {from: "cb3300000000000000000000000000000000deadbeef", to: "cb3300000000000000000000000000000000deadbeef", 87 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0}, 88 // conflicting input and data 89 {from: "cb3300000000000000000000000000000000deadbeef", to: "cb3300000000000000000000000000000000deadbeef", 90 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", i: "0x02", expectErr: true}, 91 // Data can't be parsed 92 {from: "cb3300000000000000000000000000000000deadbeef", to: "cb3300000000000000000000000000000000deadbeef", 93 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x0102", numMessages: 1}, 94 // Data (on Input) can't be parsed 95 {from: "cb3300000000000000000000000000000000deadbeef", to: "cb3300000000000000000000000000000000deadbeef", 96 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", i: "0x0102", numMessages: 1}, 97 // Send to 0 98 {from: "cb3300000000000000000000000000000000deadbeef", to: "cb270000000000000000000000000000000000000001", 99 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0}, 100 // Create empty contract (no value) 101 {from: "cb3300000000000000000000000000000000deadbeef", to: "", 102 n: "0x01", g: "0x20", gp: "0x40", value: "0x00", numMessages: 1}, 103 // Create empty contract (with value) 104 {from: "cb3300000000000000000000000000000000deadbeef", to: "", 105 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", expectErr: true}, 106 // Small payload for create 107 {from: "cb3300000000000000000000000000000000deadbeef", to: "", 108 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", numMessages: 1}, 109 } 110 for i, test := range testcases { 111 msgs, err := db.ValidateTransaction(nil, dummyTxArgs(test)) 112 if err == nil && test.expectErr { 113 t.Errorf("Test %d, expected error", i) 114 for _, msg := range msgs.Messages { 115 t.Logf("* %s: %s", msg.Typ, msg.Message) 116 } 117 } 118 if err != nil && !test.expectErr { 119 t.Errorf("Test %d, unexpected error: %v", i, err) 120 } 121 if err == nil { 122 got := len(msgs.Messages) 123 if got != test.numMessages { 124 for _, msg := range msgs.Messages { 125 t.Logf("* %s: %s", msg.Typ, msg.Message) 126 } 127 t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got) 128 } else { 129 //Debug printout, remove later 130 for _, msg := range msgs.Messages { 131 t.Logf("* [%d] %s: %s", i, msg.Typ, msg.Message) 132 } 133 t.Log() 134 } 135 } 136 } 137 }