Skip to content

Commit 01ec4db

Browse files
committed
Add stdin option
1 parent a86452d commit 01ec4db

6 files changed

Lines changed: 386 additions & 201 deletions

File tree

‎cmd/ethtest/main.go‎

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ package main
2424

2525
import (
2626
"fmt"
27+
"io"
2728
"io/ioutil"
2829
"os"
2930
"path/filepath"
@@ -55,28 +56,37 @@ var (
5556
Name: "continue",
5657
Usage: "Continue running tests on error (true) or [default] exit immediately (false)",
5758
}
59+
ReadStdInFlag = cli.BoolFlag{
60+
Name: "stdin",
61+
Usage: "Accept input from stdin instead of reading from file",
62+
}
5863
)
5964

60-
func runTest(test, file string) error {
61-
// glog.Infoln("runTest", test, file)
65+
func runTestWithReader(test string, r io.Reader) error {
66+
glog.Infoln("runTest", test)
6267
var err error
6368
switch test {
64-
case "bc", "BlockTest", "BlockTests", "BlockChainTest":
65-
err = tests.RunBlockTest(file)
69+
case "bt", "BlockTest", "BlockTests", "BlockChainTest":
70+
err = tests.RunBlockTestWithReader(r)
6671
case "st", "state", "StateTest", "StateTests":
67-
err = tests.RunStateTest(file)
72+
err = tests.RunStateTestWithReader(r)
6873
case "tx", "TransactionTest", "TransactionTests":
69-
err = tests.RunTransactionTests(file)
74+
err = tests.RunTransactionTestsWithReader(r)
7075
case "vm", "VMTest", "VMTests":
71-
err = tests.RunVmTest(file)
76+
err = tests.RunVmTestWithReader(r)
7277
default:
73-
err = fmt.Errorf("Invalid test type specified:", test)
78+
err = fmt.Errorf("Invalid test type specified: %v", test)
79+
}
80+
81+
if err != nil {
82+
return err
7483
}
75-
return err
84+
85+
return nil
7686
}
7787

7888
func getFiles(path string) ([]string, error) {
79-
// glog.Infoln("getFiles", path)
89+
glog.Infoln("getFiles", path)
8090
var files []string
8191
f, err := os.Open(path)
8292
if err != nil {
@@ -97,7 +107,7 @@ func getFiles(path string) ([]string, error) {
97107
// only go 1 depth and leave directory entires blank
98108
if !v.IsDir() && v.Name()[len(v.Name())-len(testExtension):len(v.Name())] == testExtension {
99109
files[i] = filepath.Join(path, v.Name())
100-
// glog.Infoln("Found file", files[i])
110+
glog.Infoln("Found file", files[i])
101111
}
102112
}
103113
case mode.IsRegular():
@@ -118,7 +128,7 @@ func runSuite(test, file string) {
118128
}
119129

120130
for _, curTest := range tests {
121-
// glog.Infoln("runSuite", curTest, file)
131+
glog.Infoln("runSuite", curTest, file)
122132
var err error
123133
var files []string
124134
if test == defaultTest {
@@ -134,16 +144,19 @@ func runSuite(test, file string) {
134144
if len(files) == 0 {
135145
glog.Warningln("No files matched path")
136146
}
137-
for _, testfile := range files {
147+
for _, curFile := range files {
138148
// Skip blank entries
139-
if len(testfile) == 0 {
149+
if len(curFile) == 0 {
140150
continue
141151
}
142152

143-
// TODO allow io.Reader to be passed so Stdin can be piped
144-
// RunVmTest(strings.NewReader(os.Args[2]))
145-
// RunVmTest(os.Stdin)
146-
err := runTest(curTest, testfile)
153+
r, err := os.Open(curFile)
154+
if err != nil {
155+
glog.Fatalln(err)
156+
}
157+
defer r.Close()
158+
159+
err = runTestWithReader(curTest, r)
147160
if err != nil {
148161
if continueOnError {
149162
glog.Errorln(err)
@@ -160,8 +173,16 @@ func setupApp(c *cli.Context) {
160173
flagTest := c.GlobalString(TestFlag.Name)
161174
flagFile := c.GlobalString(FileFlag.Name)
162175
continueOnError = c.GlobalBool(ContinueOnErrorFlag.Name)
176+
useStdIn := c.GlobalBool(ReadStdInFlag.Name)
177+
178+
if !useStdIn {
179+
runSuite(flagTest, flagFile)
180+
} else {
181+
if err := runTestWithReader(flagTest, os.Stdin); err != nil {
182+
glog.Fatalln(err)
183+
}
163184

164-
runSuite(flagTest, flagFile)
185+
}
165186
}
166187

167188
func main() {
@@ -178,6 +199,7 @@ func main() {
178199
TestFlag,
179200
FileFlag,
180201
ContinueOnErrorFlag,
202+
ReadStdInFlag,
181203
}
182204

183205
if err := app.Run(os.Args); err != nil {

‎tests/block_test_util.go‎

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/hex"
66
"fmt"
7+
"io"
78
"math/big"
89
"path/filepath"
910
"runtime"
@@ -85,15 +86,42 @@ type btTransaction struct {
8586
Value string
8687
}
8788

88-
func RunBlockTest(filepath string) error {
89-
bt, err := LoadBlockTests(filepath)
89+
func RunBlockTestWithReader(r io.Reader) error {
90+
btjs := make(map[string]*btJSON)
91+
if err := readJson(r, &btjs); err != nil {
92+
return err
93+
}
94+
95+
bt, err := convertBlockTests(btjs)
9096
if err != nil {
9197
return err
9298
}
9399

94-
// map skipped tests to boolean set
95-
skipTest := make(map[string]bool, len(blockSkipTests))
96-
for _, name := range blockSkipTests {
100+
if err := runBlockTests(bt); err != nil {
101+
return err
102+
}
103+
return nil
104+
}
105+
106+
func RunBlockTest(file string) error {
107+
btjs := make(map[string]*btJSON)
108+
if err := readJsonFile(file, &btjs); err != nil {
109+
return err
110+
}
111+
112+
bt, err := convertBlockTests(btjs)
113+
if err != nil {
114+
return err
115+
}
116+
if err := runBlockTests(bt); err != nil {
117+
return err
118+
}
119+
return nil
120+
}
121+
122+
func runBlockTests(bt map[string]*BlockTest) error {
123+
skipTest := make(map[string]bool, len(BlockSkipTests))
124+
for _, name := range BlockSkipTests {
97125
skipTest[name] = true
98126
}
99127

@@ -103,17 +131,19 @@ func RunBlockTest(filepath string) error {
103131
glog.Infoln("Skipping block test", name)
104132
return nil
105133
}
134+
106135
// test the block
107-
if err := testBlock(test); err != nil {
136+
if err := runBlockTest(test); err != nil {
108137
return err
109138
}
110139
glog.Infoln("Block test passed: ", name)
140+
111141
}
112142
return nil
113-
}
114143

115-
func testBlock(test *BlockTest) error {
116-
cfg := testEthConfig()
144+
}
145+
func runBlockTest(test *BlockTest) error {
146+
cfg := test.makeEthConfig()
117147
ethereum, err := eth.New(cfg)
118148
if err != nil {
119149
return err
@@ -144,7 +174,7 @@ func testBlock(test *BlockTest) error {
144174
return nil
145175
}
146176

147-
func testEthConfig() *eth.Config {
177+
func (test *BlockTest) makeEthConfig() *eth.Config {
148178
ks := crypto.NewKeyStorePassphrase(filepath.Join(common.DefaultDataDir(), "keystore"))
149179

150180
return &eth.Config{
@@ -230,15 +260,15 @@ func (t *BlockTest) TryBlocksInsert(chainManager *core.ChainManager) error {
230260
if b.BlockHeader == nil {
231261
return fmt.Errorf("Block insertion should have failed")
232262
}
233-
err = validateBlockHeader(b.BlockHeader, cb.Header())
263+
err = t.validateBlockHeader(b.BlockHeader, cb.Header())
234264
if err != nil {
235265
return fmt.Errorf("Block header validation failed: ", err)
236266
}
237267
}
238268
return nil
239269
}
240270

241-
func validateBlockHeader(h *btHeader, h2 *types.Header) error {
271+
func (s *BlockTest) validateBlockHeader(h *btHeader, h2 *types.Header) error {
242272
expectedBloom := mustConvertBytes(h.Bloom)
243273
if !bytes.Equal(expectedBloom, h2.Bloom.Bytes()) {
244274
return fmt.Errorf("Bloom: expected: %v, decoded: %v", expectedBloom, h2.Bloom.Bytes())
@@ -341,7 +371,18 @@ func (t *BlockTest) ValidatePostState(statedb *state.StateDB) error {
341371
return nil
342372
}
343373

344-
func convertTest(in *btJSON) (out *BlockTest, err error) {
374+
func convertBlockTests(in map[string]*btJSON) (map[string]*BlockTest, error) {
375+
out := make(map[string]*BlockTest)
376+
for name, test := range in {
377+
var err error
378+
if out[name], err = convertBlockTest(test); err != nil {
379+
return out, fmt.Errorf("bad test %q: %v", name, err)
380+
}
381+
}
382+
return out, nil
383+
}
384+
385+
func convertBlockTest(in *btJSON) (out *BlockTest, err error) {
345386
// the conversion handles errors by catching panics.
346387
// you might consider this ugly, but the alternative (passing errors)
347388
// would be much harder to read.
@@ -450,19 +491,12 @@ func mustConvertUint(in string, base int) uint64 {
450491
}
451492

452493
func LoadBlockTests(file string) (map[string]*BlockTest, error) {
453-
bt := make(map[string]*btJSON)
454-
if err := readTestFile(file, &bt); err != nil {
494+
btjs := make(map[string]*btJSON)
495+
if err := readJsonFile(file, &btjs); err != nil {
455496
return nil, err
456497
}
457498

458-
out := make(map[string]*BlockTest)
459-
for name, in := range bt {
460-
var err error
461-
if out[name], err = convertTest(in); err != nil {
462-
return out, fmt.Errorf("bad test %q: %v", name, err)
463-
}
464-
}
465-
return out, nil
499+
return convertBlockTests(btjs)
466500
}
467501

468502
// Nothing to see here, please move along...

‎tests/init.go‎

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"net/http"
99
"os"
1010
"path/filepath"
11+
12+
// "github.com/ethereum/go-ethereum/logger/glog"
1113
)
1214

1315
var (
@@ -17,13 +19,40 @@ var (
1719
transactionTestDir = filepath.Join(baseDir, "TransactionTests")
1820
vmTestDir = filepath.Join(baseDir, "VMTests")
1921

20-
blockSkipTests = []string{"SimpleTx3"}
21-
transSkipTests = []string{"TransactionWithHihghNonce256"}
22-
stateSkipTests = []string{"mload32bitBound_return", "mload32bitBound_return2"}
23-
vmSkipTests = []string{}
22+
BlockSkipTests = []string{"SimpleTx3"}
23+
TransSkipTests = []string{"TransactionWithHihghNonce256"}
24+
StateSkipTests = []string{"mload32bitBound_return", "mload32bitBound_return2"}
25+
VmSkipTests = []string{}
2426
)
2527

26-
func readJSON(reader io.Reader, value interface{}) error {
28+
// type TestRunner interface {
29+
// // LoadTest()
30+
// RunTest() error
31+
// }
32+
33+
// func RunTests(bt map[string]TestRunner, skipTests []string) error {
34+
// // map skipped tests to boolean set
35+
// skipTest := make(map[string]bool, len(skipTests))
36+
// for _, name := range skipTests {
37+
// skipTest[name] = true
38+
// }
39+
40+
// for name, test := range bt {
41+
// // if the test should be skipped, return
42+
// if skipTest[name] {
43+
// glog.Infoln("Skipping block test", name)
44+
// return nil
45+
// }
46+
// // test the block
47+
// if err := test.RunTest(); err != nil {
48+
// return err
49+
// }
50+
// glog.Infoln("Block test passed: ", name)
51+
// }
52+
// return nil
53+
// }
54+
55+
func readJson(reader io.Reader, value interface{}) error {
2756
data, err := ioutil.ReadAll(reader)
2857
if err != nil {
2958
return fmt.Errorf("Error reading JSON file", err.Error())
@@ -39,44 +68,44 @@ func readJSON(reader io.Reader, value interface{}) error {
3968
return nil
4069
}
4170

42-
// findLine returns the line number for the given offset into data.
43-
func findLine(data []byte, offset int64) (line int) {
44-
line = 1
45-
for i, r := range string(data) {
46-
if int64(i) >= offset {
47-
return
48-
}
49-
if r == '\n' {
50-
line++
51-
}
52-
}
53-
return
54-
}
55-
56-
func readHttpFile(uri string, value interface{}) error {
71+
func readJsonHttp(uri string, value interface{}) error {
5772
resp, err := http.Get(uri)
5873
if err != nil {
5974
return err
6075
}
6176
defer resp.Body.Close()
6277

63-
err = readJSON(resp.Body, value)
78+
err = readJson(resp.Body, value)
6479
if err != nil {
6580
return err
6681
}
6782
return nil
6883
}
6984

70-
func readTestFile(fn string, value interface{}) error {
85+
func readJsonFile(fn string, value interface{}) error {
7186
file, err := os.Open(fn)
7287
if err != nil {
7388
return err
7489
}
7590
defer file.Close()
7691

77-
err = readJSON(file, value)
92+
err = readJson(file, value)
7893
if err != nil {
7994
return fmt.Errorf("%s in file %s", err.Error(), fn)
8095
}
8196
return nil
8297
}
98+
99+
// findLine returns the line number for the given offset into data.
100+
func findLine(data []byte, offset int64) (line int) {
101+
line = 1
102+
for i, r := range string(data) {
103+
if int64(i) >= offset {
104+
return
105+
}
106+
if r == '\n' {
107+
line++
108+
}
109+
}
110+
return
111+
}

0 commit comments

Comments
 (0)