fourbyte_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "fmt"
  19. "io/ioutil"
  20. "strings"
  21. "testing"
  22. "github.com/ethereum/go-ethereum/accounts/abi"
  23. "github.com/ethereum/go-ethereum/common"
  24. )
  25. // Tests that all the selectors contained in the 4byte database are valid.
  26. func TestEmbeddedDatabase(t *testing.T) {
  27. db, err := New()
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. for id, selector := range db.embedded {
  32. abistring, err := parseSelector(selector)
  33. if err != nil {
  34. t.Errorf("Failed to convert selector to ABI: %v", err)
  35. continue
  36. }
  37. abistruct, err := abi.JSON(strings.NewReader(string(abistring)))
  38. if err != nil {
  39. t.Errorf("Failed to parse ABI: %v", err)
  40. continue
  41. }
  42. m, err := abistruct.MethodById(common.Hex2Bytes(id))
  43. if err != nil {
  44. t.Errorf("Failed to get method by id (%s): %v", id, err)
  45. continue
  46. }
  47. if m.Sig != selector {
  48. t.Errorf("Selector mismatch: have %v, want %v", m.Sig, selector)
  49. }
  50. }
  51. }
  52. // Tests that custom 4byte datasets can be handled too.
  53. func TestCustomDatabase(t *testing.T) {
  54. // Create a new custom 4byte database with no embedded component
  55. tmpdir, err := ioutil.TempDir("", "signer-4byte-test")
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. filename := fmt.Sprintf("%s/4byte_custom.json", tmpdir)
  60. db, err := NewWithFile(filename)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. db.embedded = make(map[string]string)
  65. // Ensure the database is empty, insert and verify
  66. calldata := common.Hex2Bytes("a52c101edeadbeef")
  67. if _, err = db.Selector(calldata); err == nil {
  68. t.Fatalf("Should not find a match on empty database")
  69. }
  70. if err = db.AddSelector("send(uint256)", calldata); err != nil {
  71. t.Fatalf("Failed to save file: %v", err)
  72. }
  73. if _, err = db.Selector(calldata); err != nil {
  74. t.Fatalf("Failed to find a match for abi signature: %v", err)
  75. }
  76. // Check that the file as persisted to disk by creating a new instance
  77. db2, err := NewFromFile(filename)
  78. if err != nil {
  79. t.Fatalf("Failed to create new abidb: %v", err)
  80. }
  81. if _, err = db2.Selector(calldata); err != nil {
  82. t.Fatalf("Failed to find a match for persisted abi signature: %v", err)
  83. }
  84. }