github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/tests/init.go (about)

     1  // Copyright 2015 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 tests implements execution of Ethereum JSON tests.
    18  package tests
    19  
    20  import (
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  
    28  	"github.com/ethereumproject/go-ethereum/core"
    29  )
    30  
    31  var (
    32  	baseDir            = filepath.Join(".", "files")
    33  	blockTestDir       = filepath.Join(baseDir, "BlockchainTests")
    34  	stateTestDir       = filepath.Join(baseDir, "StateTests")
    35  	transactionTestDir = filepath.Join(baseDir, "TransactionTests")
    36  	vmTestDir          = filepath.Join(baseDir, "VMTests")
    37  	rlpTestDir         = filepath.Join(baseDir, "RLPTests")
    38  
    39  	BlockSkipTests = initBlockSkipTests()
    40  
    41  	/* Go client does not support transaction (account) nonces above 2^64. This
    42  	technically breaks consensus but is regarded as "reasonable
    43  	engineering constraint" as accounts cannot easily reach such high
    44  	nonce values in practice
    45  	*/
    46  	TransSkipTests = []string{"TransactionWithHihghNonce256"}
    47  	StateSkipTests = []string{}
    48  	VmSkipTests    = []string{}
    49  )
    50  
    51  func initBlockSkipTests() []string {
    52  	if core.UseSputnikVM == "true" {
    53  		return []string{
    54  			// These tests are not valid, as they are out of scope for RLP and
    55  			// the consensus protocol.
    56  			"BLOCK__RandomByteAtTheEnd",
    57  			"TRANSCT__RandomByteAtTheEnd",
    58  			"BLOCK__ZeroByteAtTheEnd",
    59  			"TRANSCT__ZeroByteAtTheEnd",
    60  
    61  			"ChainAtoChainB_blockorder2",
    62  			"ChainAtoChainB_blockorder1",
    63  			"ChainAtoChainB_BlockHash",
    64  			"CallingCanonicalContractFromFork_CALLCODE",
    65  		}
    66  	} else {
    67  		return []string{
    68  			// These tests are not valid, as they are out of scope for RLP and
    69  			// the consensus protocol.
    70  			"BLOCK__RandomByteAtTheEnd",
    71  			"TRANSCT__RandomByteAtTheEnd",
    72  			"BLOCK__ZeroByteAtTheEnd",
    73  			"TRANSCT__ZeroByteAtTheEnd",
    74  
    75  			"ChainAtoChainB_blockorder2",
    76  			"ChainAtoChainB_blockorder1",
    77  		}
    78  	}
    79  }
    80  
    81  func readJson(reader io.Reader, value interface{}) error {
    82  	data, err := ioutil.ReadAll(reader)
    83  	if err != nil {
    84  		return fmt.Errorf("error reading JSON file: %v", err)
    85  	}
    86  	if err = json.Unmarshal(data, &value); err != nil {
    87  		if syntaxerr, ok := err.(*json.SyntaxError); ok {
    88  			line := findLine(data, syntaxerr.Offset)
    89  			return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
    90  		}
    91  		return fmt.Errorf("JSON unmarshal error: %v", err)
    92  	}
    93  	return nil
    94  }
    95  
    96  func readJsonFile(fn string, value interface{}) error {
    97  	file, err := os.Open(fn)
    98  	if err != nil {
    99  		return err
   100  	}
   101  	defer file.Close()
   102  
   103  	err = readJson(file, value)
   104  	if err != nil {
   105  		return fmt.Errorf("%s in file %s", err.Error(), fn)
   106  	}
   107  	return nil
   108  }
   109  
   110  // findLine returns the line number for the given offset into data.
   111  func findLine(data []byte, offset int64) (line int) {
   112  	line = 1
   113  	for i, r := range string(data) {
   114  		if int64(i) >= offset {
   115  			return
   116  		}
   117  		if r == '\n' {
   118  			line++
   119  		}
   120  	}
   121  	return
   122  }