github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/tests/init.go (about)

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