github.com/jiajun1992/watercarver@v0.0.0-20191031150618-dfc2b17c0c4a/go-ethereum/tests/init_test.go (about)

     1  // Copyright 2017 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
    18  
    19  import (
    20  	"encoding/json"
    21  	"flag"
    22  	"fmt"
    23  	"io"
    24  	"io/ioutil"
    25  	"os"
    26  	"path/filepath"
    27  	"reflect"
    28  	"regexp"
    29  	"runtime"
    30  	"sort"
    31  	"strings"
    32  	"testing"
    33  
    34  	"github.com/ethereum/go-ethereum/params"
    35  )
    36  
    37  // Command line flags to configure the interpreters.
    38  var (
    39  	testEVM   = flag.String("vm.evm", "", "EVM configuration")
    40  	testEWASM = flag.String("vm.ewasm", "", "EWASM configuration")
    41  )
    42  
    43  func TestMain(m *testing.M) {
    44  	flag.Parse()
    45  	os.Exit(m.Run())
    46  }
    47  
    48  var (
    49  	baseDir            = filepath.Join(".", "testdata")
    50  	blockTestDir       = filepath.Join(baseDir, "BlockchainTests")
    51  	stateTestDir       = filepath.Join(baseDir, "GeneralStateTests")
    52  	transactionTestDir = filepath.Join(baseDir, "TransactionTests")
    53  	vmTestDir          = filepath.Join(baseDir, "VMTests")
    54  	rlpTestDir         = filepath.Join(baseDir, "RLPTests")
    55  	difficultyTestDir  = filepath.Join(baseDir, "BasicTests")
    56  )
    57  
    58  func readJSON(reader io.Reader, value interface{}) error {
    59  	data, err := ioutil.ReadAll(reader)
    60  	if err != nil {
    61  		return fmt.Errorf("error reading JSON file: %v", err)
    62  	}
    63  	if err = json.Unmarshal(data, &value); err != nil {
    64  		if syntaxerr, ok := err.(*json.SyntaxError); ok {
    65  			line := findLine(data, syntaxerr.Offset)
    66  			return fmt.Errorf("JSON syntax error at line %v: %v", line, err)
    67  		}
    68  		return err
    69  	}
    70  	return nil
    71  }
    72  
    73  func readJSONFile(fn string, value interface{}) error {
    74  	file, err := os.Open(fn)
    75  	if err != nil {
    76  		return err
    77  	}
    78  	defer file.Close()
    79  
    80  	err = readJSON(file, value)
    81  	if err != nil {
    82  		return fmt.Errorf("%s in file %s", err.Error(), fn)
    83  	}
    84  	return nil
    85  }
    86  
    87  // findLine returns the line number for the given offset into data.
    88  func findLine(data []byte, offset int64) (line int) {
    89  	line = 1
    90  	for i, r := range string(data) {
    91  		if int64(i) >= offset {
    92  			return
    93  		}
    94  		if r == '\n' {
    95  			line++
    96  		}
    97  	}
    98  	return
    99  }
   100  
   101  // testMatcher controls skipping and chain config assignment to tests.
   102  type testMatcher struct {
   103  	configpat    []testConfig
   104  	failpat      []testFailure
   105  	skiploadpat  []*regexp.Regexp
   106  	slowpat      []*regexp.Regexp
   107  	whitelistpat *regexp.Regexp
   108  }
   109  
   110  type testConfig struct {
   111  	p      *regexp.Regexp
   112  	config params.ChainConfig
   113  }
   114  
   115  type testFailure struct {
   116  	p      *regexp.Regexp
   117  	reason string
   118  }
   119  
   120  // skipShortMode skips tests matching when the -short flag is used.
   121  func (tm *testMatcher) slow(pattern string) {
   122  	tm.slowpat = append(tm.slowpat, regexp.MustCompile(pattern))
   123  }
   124  
   125  // skipLoad skips JSON loading of tests matching the pattern.
   126  func (tm *testMatcher) skipLoad(pattern string) {
   127  	tm.skiploadpat = append(tm.skiploadpat, regexp.MustCompile(pattern))
   128  }
   129  
   130  // fails adds an expected failure for tests matching the pattern.
   131  func (tm *testMatcher) fails(pattern string, reason string) {
   132  	if reason == "" {
   133  		panic("empty fail reason")
   134  	}
   135  	tm.failpat = append(tm.failpat, testFailure{regexp.MustCompile(pattern), reason})
   136  }
   137  
   138  func (tm *testMatcher) whitelist(pattern string) {
   139  	tm.whitelistpat = regexp.MustCompile(pattern)
   140  }
   141  
   142  // config defines chain config for tests matching the pattern.
   143  func (tm *testMatcher) config(pattern string, cfg params.ChainConfig) {
   144  	tm.configpat = append(tm.configpat, testConfig{regexp.MustCompile(pattern), cfg})
   145  }
   146  
   147  // findSkip matches name against test skip patterns.
   148  func (tm *testMatcher) findSkip(name string) (reason string, skipload bool) {
   149  	isWin32 := runtime.GOARCH == "386" && runtime.GOOS == "windows"
   150  	for _, re := range tm.slowpat {
   151  		if re.MatchString(name) {
   152  			if testing.Short() {
   153  				return "skipped in -short mode", false
   154  			}
   155  			if isWin32 {
   156  				return "skipped on 32bit windows", false
   157  			}
   158  		}
   159  	}
   160  	for _, re := range tm.skiploadpat {
   161  		if re.MatchString(name) {
   162  			return "skipped by skipLoad", true
   163  		}
   164  	}
   165  	return "", false
   166  }
   167  
   168  // findConfig returns the chain config matching defined patterns.
   169  func (tm *testMatcher) findConfig(name string) *params.ChainConfig {
   170  	// TODO(fjl): name can be derived from testing.T when min Go version is 1.8
   171  	for _, m := range tm.configpat {
   172  		if m.p.MatchString(name) {
   173  			return &m.config
   174  		}
   175  	}
   176  	return new(params.ChainConfig)
   177  }
   178  
   179  // checkFailure checks whether a failure is expected.
   180  func (tm *testMatcher) checkFailure(t *testing.T, name string, err error) error {
   181  	// TODO(fjl): name can be derived from t when min Go version is 1.8
   182  	failReason := ""
   183  	for _, m := range tm.failpat {
   184  		if m.p.MatchString(name) {
   185  			failReason = m.reason
   186  			break
   187  		}
   188  	}
   189  	if failReason != "" {
   190  		t.Logf("expected failure: %s", failReason)
   191  		if err != nil {
   192  			t.Logf("error: %v", err)
   193  			return nil
   194  		}
   195  		return fmt.Errorf("test succeeded unexpectedly")
   196  	}
   197  	return err
   198  }
   199  
   200  // walk invokes its runTest argument for all subtests in the given directory.
   201  //
   202  // runTest should be a function of type func(t *testing.T, name string, x <TestType>),
   203  // where TestType is the type of the test contained in test files.
   204  func (tm *testMatcher) walk(t *testing.T, dir string, runTest interface{}) {
   205  	// Walk the directory.
   206  	dirinfo, err := os.Stat(dir)
   207  	if os.IsNotExist(err) || !dirinfo.IsDir() {
   208  		fmt.Fprintf(os.Stderr, "can't find test files in %s, did you clone the tests submodule?\n", dir)
   209  		t.Skip("missing test files")
   210  	}
   211  	err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
   212  		name := filepath.ToSlash(strings.TrimPrefix(path, dir+string(filepath.Separator)))
   213  		if info.IsDir() {
   214  			if _, skipload := tm.findSkip(name + "/"); skipload {
   215  				return filepath.SkipDir
   216  			}
   217  			return nil
   218  		}
   219  		if filepath.Ext(path) == ".json" {
   220  			t.Run(name, func(t *testing.T) { tm.runTestFile(t, path, name, runTest) })
   221  		}
   222  		return nil
   223  	})
   224  	if err != nil {
   225  		t.Fatal(err)
   226  	}
   227  }
   228  
   229  func (tm *testMatcher) runTestFile(t *testing.T, path, name string, runTest interface{}) {
   230  	if r, _ := tm.findSkip(name); r != "" {
   231  		t.Skip(r)
   232  	}
   233  	if tm.whitelistpat != nil {
   234  		if !tm.whitelistpat.MatchString(name) {
   235  			t.Skip("Skipped by whitelist")
   236  		}
   237  	}
   238  	t.Parallel()
   239  
   240  	// Load the file as map[string]<testType>.
   241  	m := makeMapFromTestFunc(runTest)
   242  	if err := readJSONFile(path, m.Addr().Interface()); err != nil {
   243  		t.Fatal(err)
   244  	}
   245  
   246  	// Run all tests from the map. Don't wrap in a subtest if there is only one test in the file.
   247  	keys := sortedMapKeys(m)
   248  	if len(keys) == 1 {
   249  		runTestFunc(runTest, t, name, m, keys[0])
   250  	} else {
   251  		for _, key := range keys {
   252  			name := name + "/" + key
   253  			t.Run(key, func(t *testing.T) {
   254  				if r, _ := tm.findSkip(name); r != "" {
   255  					t.Skip(r)
   256  				}
   257  				runTestFunc(runTest, t, name, m, key)
   258  			})
   259  		}
   260  	}
   261  }
   262  
   263  func makeMapFromTestFunc(f interface{}) reflect.Value {
   264  	stringT := reflect.TypeOf("")
   265  	testingT := reflect.TypeOf((*testing.T)(nil))
   266  	ftyp := reflect.TypeOf(f)
   267  	if ftyp.Kind() != reflect.Func || ftyp.NumIn() != 3 || ftyp.NumOut() != 0 || ftyp.In(0) != testingT || ftyp.In(1) != stringT {
   268  		panic(fmt.Sprintf("bad test function type: want func(*testing.T, string, <TestType>), have %s", ftyp))
   269  	}
   270  	testType := ftyp.In(2)
   271  	mp := reflect.New(reflect.MapOf(stringT, testType))
   272  	return mp.Elem()
   273  }
   274  
   275  func sortedMapKeys(m reflect.Value) []string {
   276  	keys := make([]string, m.Len())
   277  	for i, k := range m.MapKeys() {
   278  		keys[i] = k.String()
   279  	}
   280  	sort.Strings(keys)
   281  	return keys
   282  }
   283  
   284  func runTestFunc(runTest interface{}, t *testing.T, name string, m reflect.Value, key string) {
   285  	reflect.ValueOf(runTest).Call([]reflect.Value{
   286  		reflect.ValueOf(t),
   287  		reflect.ValueOf(name),
   288  		m.MapIndex(reflect.ValueOf(key)),
   289  	})
   290  }