validation_test.go 4.7 KB

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