validation_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. package core
  17. import (
  18. "fmt"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/common/hexutil"
  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. return &SendTxArgs{
  55. From: *from,
  56. To: to,
  57. Value: value,
  58. Nonce: n,
  59. GasPrice: gasPrice,
  60. Gas: gas,
  61. Data: data,
  62. Input: input,
  63. }
  64. }
  65. type txtestcase struct {
  66. from, to, n, g, gp, value, d, i string
  67. expectErr bool
  68. numMessages int
  69. }
  70. func TestValidator(t *testing.T) {
  71. var (
  72. // use empty db, there are other tests for the abi-specific stuff
  73. db, _ = NewEmptyAbiDB()
  74. v = NewValidator(db)
  75. )
  76. testcases := []txtestcase{
  77. // Invalid to checksum
  78. {from: "000000000000000000000000000000000000dead", to: "000000000000000000000000000000000000dead",
  79. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
  80. // valid 0x000000000000000000000000000000000000dEaD
  81. {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
  82. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0},
  83. // conflicting input and data
  84. {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
  85. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", i: "0x02", expectErr: true},
  86. // Data can't be parsed
  87. {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
  88. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x0102", numMessages: 1},
  89. // Data (on Input) can't be parsed
  90. {from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
  91. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", i: "0x0102", numMessages: 1},
  92. // Send to 0
  93. {from: "000000000000000000000000000000000000dead", to: "0x0000000000000000000000000000000000000000",
  94. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
  95. // Create empty contract (no value)
  96. {from: "000000000000000000000000000000000000dead", to: "",
  97. n: "0x01", g: "0x20", gp: "0x40", value: "0x00", numMessages: 1},
  98. // Create empty contract (with value)
  99. {from: "000000000000000000000000000000000000dead", to: "",
  100. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", expectErr: true},
  101. // Small payload for create
  102. {from: "000000000000000000000000000000000000dead", to: "",
  103. n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", numMessages: 1},
  104. }
  105. for i, test := range testcases {
  106. msgs, err := v.ValidateTransaction(dummyTxArgs(test), nil)
  107. if err == nil && test.expectErr {
  108. t.Errorf("Test %d, expected error", i)
  109. for _, msg := range msgs.Messages {
  110. fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
  111. }
  112. }
  113. if err != nil && !test.expectErr {
  114. t.Errorf("Test %d, unexpected error: %v", i, err)
  115. }
  116. if err == nil {
  117. got := len(msgs.Messages)
  118. if got != test.numMessages {
  119. for _, msg := range msgs.Messages {
  120. fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
  121. }
  122. t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got)
  123. } else {
  124. //Debug printout, remove later
  125. for _, msg := range msgs.Messages {
  126. fmt.Printf("* [%d] %s: %s\n", i, msg.Typ, msg.Message)
  127. }
  128. fmt.Println()
  129. }
  130. }
  131. }
  132. }