signed_data_internal_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 core
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common/hexutil"
  22. )
  23. func TestBytesPadding(t *testing.T) {
  24. tests := []struct {
  25. Type string
  26. Input []byte
  27. Output []byte // nil => error
  28. }{
  29. {
  30. // Fail on wrong length
  31. Type: "bytes20",
  32. Input: []byte{},
  33. Output: nil,
  34. },
  35. {
  36. Type: "bytes1",
  37. Input: []byte{1},
  38. Output: []byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  39. },
  40. {
  41. Type: "bytes1",
  42. Input: []byte{1, 2},
  43. Output: nil,
  44. },
  45. {
  46. Type: "bytes7",
  47. Input: []byte{1, 2, 3, 4, 5, 6, 7},
  48. Output: []byte{1, 2, 3, 4, 5, 6, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  49. },
  50. {
  51. Type: "bytes32",
  52. Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
  53. Output: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
  54. },
  55. {
  56. Type: "bytes32",
  57. Input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33},
  58. Output: nil,
  59. },
  60. }
  61. d := TypedData{}
  62. for i, test := range tests {
  63. val, err := d.EncodePrimitiveValue(test.Type, test.Input, 1)
  64. if test.Output == nil {
  65. if err == nil {
  66. t.Errorf("test %d: expected error, got no error (result %x)", i, val)
  67. }
  68. } else {
  69. if err != nil {
  70. t.Errorf("test %d: expected no error, got %v", i, err)
  71. }
  72. if len(val) != 32 {
  73. t.Errorf("test %d: expected len 32, got %d", i, len(val))
  74. }
  75. if !bytes.Equal(val, test.Output) {
  76. t.Errorf("test %d: expected %x, got %x", i, test.Output, val)
  77. }
  78. }
  79. }
  80. }
  81. func TestParseBytes(t *testing.T) {
  82. for i, tt := range []struct {
  83. v interface{}
  84. exp []byte
  85. }{
  86. {"0x", []byte{}},
  87. {"0x1234", []byte{0x12, 0x34}},
  88. {[]byte{12, 34}, []byte{12, 34}},
  89. {hexutil.Bytes([]byte{12, 34}), []byte{12, 34}},
  90. {"1234", nil}, // not a proper hex-string
  91. {"0x01233", nil}, // nibbles should be rejected
  92. {"not a hex string", nil},
  93. {15, nil},
  94. {nil, nil},
  95. } {
  96. out, ok := parseBytes(tt.v)
  97. if tt.exp == nil {
  98. if ok || out != nil {
  99. t.Errorf("test %d: expected !ok, got ok = %v with out = %x", i, ok, out)
  100. }
  101. continue
  102. }
  103. if !ok {
  104. t.Errorf("test %d: expected ok got !ok", i)
  105. }
  106. if !bytes.Equal(out, tt.exp) {
  107. t.Errorf("test %d: expected %x got %x", i, tt.exp, out)
  108. }
  109. }
  110. }
  111. func TestParseInteger(t *testing.T) {
  112. for i, tt := range []struct {
  113. t string
  114. v interface{}
  115. exp *big.Int
  116. }{
  117. {"uint32", "-123", nil},
  118. {"int32", "-123", big.NewInt(-123)},
  119. {"uint32", "0xff", big.NewInt(0xff)},
  120. {"int8", "0xffff", nil},
  121. } {
  122. res, err := parseInteger(tt.t, tt.v)
  123. if tt.exp == nil && res == nil {
  124. continue
  125. }
  126. if tt.exp == nil && res != nil {
  127. t.Errorf("test %d, got %v, expected nil", i, res)
  128. continue
  129. }
  130. if tt.exp != nil && res == nil {
  131. t.Errorf("test %d, got '%v', expected %v", i, err, tt.exp)
  132. continue
  133. }
  134. if tt.exp.Cmp(res) != 0 {
  135. t.Errorf("test %d, got %v expected %v", i, res, tt.exp)
  136. }
  137. }
  138. }