github.com/daeglee/go-ethereum@v0.0.0-20190504220456-cad3e8d18e9b/signer/core/validation_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU 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 // go-ethereum 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 General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package core 18 19 import ( 20 "fmt" 21 "math/big" 22 "testing" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/common/hexutil" 26 ) 27 28 func mixAddr(a string) (*common.MixedcaseAddress, error) { 29 return common.NewMixedcaseAddressFromString(a) 30 } 31 func toHexBig(h string) hexutil.Big { 32 b := big.NewInt(0).SetBytes(common.FromHex(h)) 33 return hexutil.Big(*b) 34 } 35 func toHexUint(h string) hexutil.Uint64 { 36 b := big.NewInt(0).SetBytes(common.FromHex(h)) 37 return hexutil.Uint64(b.Uint64()) 38 } 39 func dummyTxArgs(t txtestcase) *SendTxArgs { 40 to, _ := mixAddr(t.to) 41 from, _ := mixAddr(t.from) 42 n := toHexUint(t.n) 43 gas := toHexUint(t.g) 44 gasPrice := toHexBig(t.gp) 45 value := toHexBig(t.value) 46 var ( 47 data, input *hexutil.Bytes 48 ) 49 if t.d != "" { 50 a := hexutil.Bytes(common.FromHex(t.d)) 51 data = &a 52 } 53 if t.i != "" { 54 a := hexutil.Bytes(common.FromHex(t.i)) 55 input = &a 56 57 } 58 return &SendTxArgs{ 59 From: *from, 60 To: to, 61 Value: value, 62 Nonce: n, 63 GasPrice: gasPrice, 64 Gas: gas, 65 Data: data, 66 Input: input, 67 } 68 } 69 70 type txtestcase struct { 71 from, to, n, g, gp, value, d, i string 72 expectErr bool 73 numMessages int 74 } 75 76 func TestValidator(t *testing.T) { 77 var ( 78 // use empty db, there are other tests for the abi-specific stuff 79 db, _ = NewEmptyAbiDB() 80 v = NewValidator(db) 81 ) 82 testcases := []txtestcase{ 83 // Invalid to checksum 84 {from: "000000000000000000000000000000000000dead", to: "000000000000000000000000000000000000dead", 85 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1}, 86 // valid 0x000000000000000000000000000000000000dEaD 87 {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD", 88 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0}, 89 // conflicting input and data 90 {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD", 91 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", i: "0x02", expectErr: true}, 92 // Data can't be parsed 93 {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD", 94 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x0102", numMessages: 1}, 95 // Data (on Input) can't be parsed 96 {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD", 97 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", i: "0x0102", numMessages: 1}, 98 // Send to 0 99 {from: "000000000000000000000000000000000000dead", to: "0x0000000000000000000000000000000000000000", 100 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1}, 101 // Create empty contract (no value) 102 {from: "000000000000000000000000000000000000dead", to: "", 103 n: "0x01", g: "0x20", gp: "0x40", value: "0x00", numMessages: 1}, 104 // Create empty contract (with value) 105 {from: "000000000000000000000000000000000000dead", to: "", 106 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", expectErr: true}, 107 // Small payload for create 108 {from: "000000000000000000000000000000000000dead", to: "", 109 n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", numMessages: 1}, 110 } 111 for i, test := range testcases { 112 msgs, err := v.ValidateTransaction(dummyTxArgs(test), nil) 113 if err == nil && test.expectErr { 114 t.Errorf("Test %d, expected error", i) 115 for _, msg := range msgs.Messages { 116 fmt.Printf("* %s: %s\n", msg.Typ, msg.Message) 117 } 118 } 119 if err != nil && !test.expectErr { 120 t.Errorf("Test %d, unexpected error: %v", i, err) 121 } 122 if err == nil { 123 got := len(msgs.Messages) 124 if got != test.numMessages { 125 for _, msg := range msgs.Messages { 126 fmt.Printf("* %s: %s\n", msg.Typ, msg.Message) 127 } 128 t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got) 129 } else { 130 //Debug printout, remove later 131 for _, msg := range msgs.Messages { 132 fmt.Printf("* [%d] %s: %s\n", i, msg.Typ, msg.Message) 133 } 134 fmt.Println() 135 } 136 } 137 } 138 } 139 140 func TestPasswordValidation(t *testing.T) { 141 testcases := []struct { 142 pw string 143 shouldFail bool 144 }{ 145 {"test", true}, 146 {"testtest\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98", true}, 147 {"placeOfInterest⌘", true}, 148 {"password\nwith\nlinebreak", true}, 149 {"password\twith\vtabs", true}, 150 // Ok passwords 151 {"password WhichIsOk", false}, 152 {"passwordOk!@#$%^&*()", false}, 153 {"12301203123012301230123012", false}, 154 } 155 for _, test := range testcases { 156 err := ValidatePasswordFormat(test.pw) 157 if err == nil && test.shouldFail { 158 t.Errorf("password '%v' should fail validation", test.pw) 159 } else if err != nil && !test.shouldFail { 160 161 t.Errorf("password '%v' shound not fail validation, but did: %v", test.pw, err) 162 } 163 } 164 }