fourbyte_test.go 2.7 KB

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