github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/signer/core/validation_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:42</date>
    10  //</624450110620700672>
    11  
    12  
    13  package core
    14  
    15  import (
    16  	"fmt"
    17  	"math/big"
    18  	"testing"
    19  
    20  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/common/hexutil"
    22  )
    23  
    24  func hexAddr(a string) common.Address { return common.BytesToAddress(common.FromHex(a)) }
    25  func mixAddr(a string) (*common.MixedcaseAddress, error) {
    26  	return common.NewMixedcaseAddressFromString(a)
    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) *SendTxArgs {
    37  	to, _ := mixAddr(t.to)
    38  	from, _ := mixAddr(t.from)
    39  	n := toHexUint(t.n)
    40  	gas := toHexUint(t.g)
    41  	gasPrice := toHexBig(t.gp)
    42  	value := toHexBig(t.value)
    43  	var (
    44  		data, input *hexutil.Bytes
    45  	)
    46  	if t.d != "" {
    47  		a := hexutil.Bytes(common.FromHex(t.d))
    48  		data = &a
    49  	}
    50  	if t.i != "" {
    51  		a := hexutil.Bytes(common.FromHex(t.i))
    52  		input = &a
    53  
    54  	}
    55  	return &SendTxArgs{
    56  		From:     *from,
    57  		To:       to,
    58  		Value:    value,
    59  		Nonce:    n,
    60  		GasPrice: gasPrice,
    61  		Gas:      gas,
    62  		Data:     data,
    63  		Input:    input,
    64  	}
    65  }
    66  
    67  type txtestcase struct {
    68  	from, to, n, g, gp, value, d, i string
    69  	expectErr                       bool
    70  	numMessages                     int
    71  }
    72  
    73  func TestValidator(t *testing.T) {
    74  	var (
    75  //使用空数据库,对ABI特定的东西还有其他测试
    76  		db, _ = NewEmptyAbiDB()
    77  		v     = NewValidator(db)
    78  	)
    79  	testcases := []txtestcase{
    80  //校验和无效
    81  		{from: "000000000000000000000000000000000000dead", to: "000000000000000000000000000000000000dead",
    82  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
    83  //有效的0x0000000000000000000000000000000标题
    84  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    85  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0},
    86  //输入和数据冲突
    87  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    88  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", i: "0x02", expectErr: true},
    89  //无法分析数据
    90  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    91  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x0102", numMessages: 1},
    92  //无法分析(输入时)数据
    93  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    94  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", i: "0x0102", numMessages: 1},
    95  //发送到0
    96  		{from: "000000000000000000000000000000000000dead", to: "0x0000000000000000000000000000000000000000",
    97  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
    98  //创建空合同(无值)
    99  		{from: "000000000000000000000000000000000000dead", to: "",
   100  			n: "0x01", g: "0x20", gp: "0x40", value: "0x00", numMessages: 1},
   101  //创建空合同(带值)
   102  		{from: "000000000000000000000000000000000000dead", to: "",
   103  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", expectErr: true},
   104  //用于创建的小负载
   105  		{from: "000000000000000000000000000000000000dead", to: "",
   106  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", numMessages: 1},
   107  	}
   108  	for i, test := range testcases {
   109  		msgs, err := v.ValidateTransaction(dummyTxArgs(test), nil)
   110  		if err == nil && test.expectErr {
   111  			t.Errorf("Test %d, expected error", i)
   112  			for _, msg := range msgs.Messages {
   113  				fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
   114  			}
   115  		}
   116  		if err != nil && !test.expectErr {
   117  			t.Errorf("Test %d, unexpected error: %v", i, err)
   118  		}
   119  		if err == nil {
   120  			got := len(msgs.Messages)
   121  			if got != test.numMessages {
   122  				for _, msg := range msgs.Messages {
   123  					fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
   124  				}
   125  				t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got)
   126  			} else {
   127  //调试打印输出,稍后删除
   128  				for _, msg := range msgs.Messages {
   129  					fmt.Printf("* [%d] %s: %s\n", i, msg.Typ, msg.Message)
   130  				}
   131  				fmt.Println()
   132  			}
   133  		}
   134  	}
   135  }
   136  
   137  func TestPasswordValidation(t *testing.T) {
   138  	testcases := []struct {
   139  		pw         string
   140  		shouldFail bool
   141  	}{
   142  		{"test", true},
   143  		{"testtest\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98", true},
   144  		{"placeOfInterest⌘", true},
   145  		{"password\nwith\nlinebreak", true},
   146  		{"password\twith\vtabs", true},
   147  //OK密码
   148  		{"password WhichIsOk", false},
   149  		{"passwordOk!@#$%^&*()", false},
   150  		{"12301203123012301230123012", false},
   151  	}
   152  	for _, test := range testcases {
   153  		err := ValidatePasswordFormat(test.pw)
   154  		if err == nil && test.shouldFail {
   155  			t.Errorf("password '%v' should fail validation", test.pw)
   156  		} else if err != nil && !test.shouldFail {
   157  
   158  			t.Errorf("password '%v' shound not fail validation, but did: %v", test.pw, err)
   159  		}
   160  	}
   161  }
   162