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