github.com/ethereum/go-ethereum@v1.16.1/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  	"encoding/json"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/accounts/abi"
    25  	"github.com/ethereum/go-ethereum/common"
    26  )
    27  
    28  // Tests that all the selectors contained in the 4byte database are valid.
    29  func TestEmbeddedDatabase(t *testing.T) {
    30  	t.Parallel()
    31  	db, err := New()
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	var abistruct abi.ABI
    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  		if err := json.Unmarshal(abistring, &abistruct); 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  	t.Parallel()
    60  	// Create a new custom 4byte database with no embedded component
    61  	tmpdir := t.TempDir()
    62  	filename := fmt.Sprintf("%s/4byte_custom.json", tmpdir)
    63  
    64  	db, err := NewWithFile(filename)
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  	db.embedded = make(map[string]string)
    69  
    70  	// Ensure the database is empty, insert and verify
    71  	calldata := common.Hex2Bytes("a52c101edeadbeef")
    72  	if _, err = db.Selector(calldata); err == nil {
    73  		t.Fatalf("Should not find a match on empty database")
    74  	}
    75  	if err = db.AddSelector("send(uint256)", calldata); err != nil {
    76  		t.Fatalf("Failed to save file: %v", err)
    77  	}
    78  	if _, err = db.Selector(calldata); err != nil {
    79  		t.Fatalf("Failed to find a match for abi signature: %v", err)
    80  	}
    81  	// Check that the file as persisted to disk by creating a new instance
    82  	db2, err := NewFromFile(filename)
    83  	if err != nil {
    84  		t.Fatalf("Failed to create new abidb: %v", err)
    85  	}
    86  	if _, err = db2.Selector(calldata); err != nil {
    87  		t.Fatalf("Failed to find a match for persisted abi signature: %v", err)
    88  	}
    89  }