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