github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/signer/fourbyte/fourbyte_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package fourbyte 19 20 import ( 21 "fmt" 22 "io/ioutil" 23 "strings" 24 "testing" 25 26 "github.com/AigarNetwork/aigar/accounts/abi" 27 "github.com/AigarNetwork/aigar/common" 28 ) 29 30 // Tests that all the selectors contained in the 4byte database are valid. 31 func TestEmbeddedDatabase(t *testing.T) { 32 db, err := New() 33 if err != nil { 34 t.Fatal(err) 35 } 36 for id, selector := range db.embedded { 37 abistring, err := parseSelector(selector) 38 if err != nil { 39 t.Errorf("Failed to convert selector to ABI: %v", err) 40 continue 41 } 42 abistruct, err := abi.JSON(strings.NewReader(string(abistring))) 43 if err != nil { 44 t.Errorf("Failed to parse ABI: %v", err) 45 continue 46 } 47 m, err := abistruct.MethodById(common.Hex2Bytes(id)) 48 if err != nil { 49 t.Errorf("Failed to get method by id (%s): %v", id, err) 50 continue 51 } 52 if m.Sig() != selector { 53 t.Errorf("Selector mismatch: have %v, want %v", m.Sig(), selector) 54 } 55 } 56 } 57 58 // Tests that custom 4byte datasets can be handled too. 59 func TestCustomDatabase(t *testing.T) { 60 // Create a new custom 4byte database with no embedded component 61 tmpdir, err := ioutil.TempDir("", "signer-4byte-test") 62 if err != nil { 63 t.Fatal(err) 64 } 65 filename := fmt.Sprintf("%s/4byte_custom.json", tmpdir) 66 67 db, err := NewWithFile(filename) 68 if err != nil { 69 t.Fatal(err) 70 } 71 db.embedded = make(map[string]string) 72 73 // Ensure the database is empty, insert and verify 74 calldata := common.Hex2Bytes("a52c101edeadbeef") 75 if _, err = db.Selector(calldata); err == nil { 76 t.Fatalf("Should not find a match on empty database") 77 } 78 if err = db.AddSelector("send(uint256)", calldata); err != nil { 79 t.Fatalf("Failed to save file: %v", err) 80 } 81 if _, err = db.Selector(calldata); err != nil { 82 t.Fatalf("Failed to find a match for abi signature: %v", err) 83 } 84 // Check that the file as persisted to disk by creating a new instance 85 db2, err := NewFromFile(filename) 86 if err != nil { 87 t.Fatalf("Failed to create new abidb: %v", err) 88 } 89 if _, err = db2.Selector(calldata); err != nil { 90 t.Fatalf("Failed to find a match for persisted abi signature: %v", err) 91 } 92 }