aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ethtest/main.go
blob: a107c701fbfcedf298877a86acfe95738b749962 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2014 The go-ethereum Authors
// This file is part of go-ethereum.
//
// go-ethereum is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// go-ethereum is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.

// ethtest executes Ethereum JSON tests.
package main

import (
    "fmt"
    "io"
    "io/ioutil"
    "os"
    "path/filepath"
    "strings"

    "github.com/ethereum/go-ethereum/log"
    "github.com/ethereum/go-ethereum/params"
    "github.com/ethereum/go-ethereum/tests"
    "gopkg.in/urfave/cli.v1"
)

var (
    continueOnError = false
    testExtension   = ".json"
    defaultTest     = "all"
    defaultDir      = "."
    allTests        = []string{"BlockTests", "StateTests", "TransactionTests", "VMTests", "RLPTests"}
    testDirMapping  = map[string]string{"BlockTests": "BlockchainTests"}
    skipTests       = []string{}

    TestFlag = cli.StringFlag{
        Name:  "test",
        Usage: "Test type (string): VMTests, TransactionTests, StateTests, BlockTests",
        Value: defaultTest,
    }
    FileFlag = cli.StringFlag{
        Name:   "file",
        Usage:  "Test file or directory. Directories are searched for .json files 1 level deep",
        Value:  defaultDir,
        EnvVar: "ETHEREUM_TEST_PATH",
    }
    ContinueOnErrorFlag = cli.BoolFlag{
        Name:  "continue",
        Usage: "Continue running tests on error (true) or [default] exit immediately (false)",
    }
    ReadStdInFlag = cli.BoolFlag{
        Name:  "stdin",
        Usage: "Accept input from stdin instead of reading from file",
    }
    SkipTestsFlag = cli.StringFlag{
        Name:  "skip",
        Usage: "Tests names to skip",
    }
    TraceFlag = cli.BoolFlag{
        Name:  "trace",
        Usage: "Enable VM tracing",
    }
)

func runTestWithReader(test string, r io.Reader) error {
    log.Info(fmt.Sprint("runTest", test))
    var err error
    switch strings.ToLower(test) {
    case "bk", "block", "blocktest", "blockchaintest", "blocktests", "blockchaintests":
        err = tests.RunBlockTestWithReader(params.MainNetHomesteadBlock, params.MainNetDAOForkBlock, params.MainNetHomesteadGasRepriceBlock, r, skipTests)
    case "st", "state", "statetest", "statetests":
        rs := &params.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock, DAOForkBlock: params.MainNetDAOForkBlock, DAOForkSupport: true, EIP150Block: params.MainNetHomesteadGasRepriceBlock}
        err = tests.RunStateTestWithReader(rs, r, skipTests)
    case "tx", "transactiontest", "transactiontests":
        rs := &params.ChainConfig{HomesteadBlock: params.MainNetHomesteadBlock, DAOForkBlock: params.MainNetDAOForkBlock, DAOForkSupport: true, EIP150Block: params.MainNetHomesteadGasRepriceBlock}
        err = tests.RunTransactionTestsWithReader(rs, r, skipTests)
    case "vm", "vmtest", "vmtests":
        err = tests.RunVmTestWithReader(r, skipTests)
    case "rlp", "rlptest", "rlptests":
        err = tests.RunRLPTestWithReader(r, skipTests)
    default:
        err = fmt.Errorf("Invalid test type specified: %v", test)
    }
    return err
}

func getFiles(path string) ([]string, error) {
    log.Info(fmt.Sprint("getFiles", path))
    var files []string
    f, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer f.Close()

    fi, err := f.Stat()
    if err != nil {
        return nil, err
    }

    switch mode := fi.Mode(); {
    case mode.IsDir():
        fi, _ := ioutil.ReadDir(path)
        files = make([]string, len(fi))
        for i, v := range fi {
            // only go 1 depth and leave directory entires blank
            if !v.IsDir() && v.Name()[len(v.Name())-len(testExtension):len(v.Name())] == testExtension {
                files[i] = filepath.Join(path, v.Name())
                log.Info(fmt.Sprint("Found file", files[i]))
            }
        }
    case mode.IsRegular():
        files = make([]string, 1)
        files[0] = path
    }

    return files, nil
}

func runSuite(test, file string) {
    var tests []string

    if test == defaultTest {
        tests = allTests
    } else {
        tests = []string{test}
    }

    for _, curTest := range tests {
        log.Info(fmt.Sprint("runSuite", curTest, file))
        var err error
        var files []string
        if test == defaultTest {
            // check if we have an explicit directory mapping for the test
            if _, ok := testDirMapping[curTest]; ok {
                files, err = getFiles(filepath.Join(file, testDirMapping[curTest]))
            } else {
                // otherwise assume test name
                files, err = getFiles(filepath.Join(file, curTest))
            }
        } else {
            files, err = getFiles(file)
        }
        if err != nil {
            log.Crit(fmt.Sprint(err))
        }

        if len(files) == 0 {
            log.Warn("No files matched path")
        }
        for _, curFile := range files {
            // Skip blank entries
            if len(curFile) == 0 {
                continue
            }

            r, err := os.Open(curFile)
            if err != nil {
                log.Crit(fmt.Sprint(err))
            }
            defer r.Close()

            err = runTestWithReader(curTest, r)
            if err != nil {
                if continueOnError {
                    log.Error(fmt.Sprint(err))
                } else {
                    log.Crit(fmt.Sprint(err))
                }
            }
        }
    }
}

func setupApp(c *cli.Context) error {
    flagTest := c.GlobalString(TestFlag.Name)
    flagFile := c.GlobalString(FileFlag.Name)
    continueOnError = c.GlobalBool(ContinueOnErrorFlag.Name)
    useStdIn := c.GlobalBool(ReadStdInFlag.Name)
    skipTests = strings.Split(c.GlobalString(SkipTestsFlag.Name), " ")

    if !useStdIn {
        runSuite(flagTest, flagFile)
    } else {
        if err := runTestWithReader(flagTest, os.Stdin); err != nil {
            log.Crit(fmt.Sprint(err))
        }
    }
    return nil
}

func main() {
    log.Root().SetHandler(log.StreamHandler(os.Stderr, log.TerminalFormat()))

    app := cli.NewApp()
    app.Name = "ethtest"
    app.Usage = "go-ethereum test interface"
    app.Action = setupApp
    app.Version = "0.2.0"
    app.Author = "go-ethereum team"

    app.Flags = []cli.Flag{
        TestFlag,
        FileFlag,
        ContinueOnErrorFlag,
        ReadStdInFlag,
        SkipTestsFlag,
        TraceFlag,
    }

    if err := app.Run(os.Args); err != nil {
        log.Crit(fmt.Sprint(err))
    }

}