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