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