|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "io/ioutil" |
| 9 | + "net" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "reflect" |
| 14 | + "regexp" |
| 15 | + "strings" |
| 16 | + "time" |
| 17 | + |
| 18 | + "github.com/ethereum/hive/hivesim" |
| 19 | +) |
| 20 | + |
| 21 | +var ( |
| 22 | + clientEnv = hivesim.Params{ |
| 23 | + "HIVE_NODETYPE": "full", |
| 24 | + "HIVE_NETWORK_ID": "1", |
| 25 | + "HIVE_CHAIN_ID": "1", |
| 26 | + "HIVE_FORK_HOMESTEAD": "0", |
| 27 | + //"HIVE_FORK_DAO_BLOCK": 2000, |
| 28 | + "HIVE_FORK_TANGERINE": "0", |
| 29 | + "HIVE_FORK_SPURIOUS": "0", |
| 30 | + "HIVE_FORK_BYZANTIUM": "0", |
| 31 | + "HIVE_FORK_CONSTANTINOPLE": "0", |
| 32 | + "HIVE_FORK_PETERSBURG": "0", |
| 33 | + "HIVE_FORK_ISTANBUL": "0", |
| 34 | + "HIVE_FORK_BERLIN": "0", |
| 35 | + "HIVE_FORK_LONDON": "0", |
| 36 | + } |
| 37 | + files = map[string]string{ |
| 38 | + "genesis.json": "./tests/genesis.json", |
| 39 | + "chain.rlp": "./tests/chain.rlp", |
| 40 | + } |
| 41 | +) |
| 42 | + |
| 43 | +type test struct { |
| 44 | + Name string |
| 45 | + Data []byte |
| 46 | +} |
| 47 | + |
| 48 | +func main() { |
| 49 | + suite := hivesim.Suite{ |
| 50 | + Name: "rpc-compat", |
| 51 | + Description: ` |
| 52 | +The RPC-compatibility test suite runs a set of RPC related tests against a |
| 53 | +running node. It tests client implementations of the JSON-RPC API for |
| 54 | +conformance with the execution API specification.`[1:], |
| 55 | + } |
| 56 | + suite.Add(&hivesim.ClientTestSpec{ |
| 57 | + Role: "eth1", |
| 58 | + Name: "client launch", |
| 59 | + Description: `This test launches the client and collects its logs.`, |
| 60 | + Parameters: clientEnv, |
| 61 | + Files: files, |
| 62 | + Run: func(t *hivesim.T, c *hivesim.Client) { |
| 63 | + runAllTests(t, c, c.Type) |
| 64 | + }, |
| 65 | + AlwaysRun: true, |
| 66 | + }) |
| 67 | + sim := hivesim.New() |
| 68 | + hivesim.MustRunSuite(sim, suite) |
| 69 | +} |
| 70 | + |
| 71 | +func runAllTests(t *hivesim.T, c *hivesim.Client, clientName string) { |
| 72 | + _, testPattern := t.Sim.TestPattern() |
| 73 | + re := regexp.MustCompile(testPattern) |
| 74 | + |
| 75 | + tests := loadTests(t, "tests", re) |
| 76 | + for _, test := range tests { |
| 77 | + t.Run(hivesim.TestSpec{ |
| 78 | + Name: fmt.Sprintf("%s (%s)", test.Name, clientName), |
| 79 | + Run: func(t *hivesim.T) { |
| 80 | + if err := runTest(t, c, test.Data); err != nil { |
| 81 | + t.Fatal(err) |
| 82 | + } |
| 83 | + }, |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func runTest(t *hivesim.T, c *hivesim.Client, data []byte) error { |
| 89 | + var ( |
| 90 | + client = &http.Client{ |
| 91 | + Timeout: 5 * time.Second, |
| 92 | + Transport: &loggingRoundTrip{ |
| 93 | + t: t, |
| 94 | + inner: http.DefaultTransport, |
| 95 | + }, |
| 96 | + } |
| 97 | + url = fmt.Sprintf("http://%s", net.JoinHostPort(c.IP.String(), "8545")) |
| 98 | + err error |
| 99 | + resp []byte |
| 100 | + ) |
| 101 | + |
| 102 | + for _, line := range strings.Split(string(data), "\n") { |
| 103 | + line = strings.TrimSpace(line) |
| 104 | + switch { |
| 105 | + case len(line) == 0 || strings.HasPrefix(line, "//"): |
| 106 | + // Skip comments, blank lines. |
| 107 | + continue |
| 108 | + case strings.HasPrefix(line, ">> "): |
| 109 | + // Send request. |
| 110 | + resp, err = postHttp(client, url, []byte(line[3:])) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + case strings.HasPrefix(line, "<< "): |
| 115 | + // Read response. Unmarshal to interface{} to verify deep equality. Marshal |
| 116 | + // again to remove padding differences and to print each field in the same |
| 117 | + // order. This makes it easy to spot any discrepancies. |
| 118 | + if resp == nil { |
| 119 | + return fmt.Errorf("invalid test, response before request") |
| 120 | + } |
| 121 | + var want interface{} |
| 122 | + if err := json.Unmarshal([]byte(strings.TrimSpace(line)[3:]), &want); err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + var got interface{} |
| 126 | + if err := json.Unmarshal(resp, &got); err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + if !reflect.DeepEqual(want, got) { |
| 130 | + want, _ := json.Marshal(want) |
| 131 | + got, _ := json.Marshal(got) |
| 132 | + return fmt.Errorf("invalid response\nwant: %s\ngot: %s", want, got) |
| 133 | + } |
| 134 | + resp = nil |
| 135 | + default: |
| 136 | + t.Fatalf("invalid line in test script: %s", line) |
| 137 | + } |
| 138 | + } |
| 139 | + if resp != nil { |
| 140 | + t.Fatalf("unhandled response in test case") |
| 141 | + } |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +// sendHttp sends an HTTP POST with the provided json data and reads the |
| 146 | +// response into a byte slice and returns it. |
| 147 | +func postHttp(c *http.Client, url string, d []byte) ([]byte, error) { |
| 148 | + data := bytes.NewBuffer(d) |
| 149 | + req, err := http.NewRequest("POST", url, data) |
| 150 | + if err != nil { |
| 151 | + return nil, fmt.Errorf("error building request: %v", err) |
| 152 | + } |
| 153 | + req.Header.Set("Content-Type", "application/json") |
| 154 | + resp, err := c.Do(req) |
| 155 | + if err != nil { |
| 156 | + return nil, fmt.Errorf("write error: %v", err) |
| 157 | + } |
| 158 | + return io.ReadAll(resp.Body) |
| 159 | +} |
| 160 | + |
| 161 | +// loggingRoundTrip writes requests and responses to the test log. |
| 162 | +type loggingRoundTrip struct { |
| 163 | + t *hivesim.T |
| 164 | + inner http.RoundTripper |
| 165 | +} |
| 166 | + |
| 167 | +func (rt *loggingRoundTrip) RoundTrip(req *http.Request) (*http.Response, error) { |
| 168 | + // Read and log the request body. |
| 169 | + reqBytes, err := ioutil.ReadAll(req.Body) |
| 170 | + req.Body.Close() |
| 171 | + if err != nil { |
| 172 | + return nil, err |
| 173 | + } |
| 174 | + rt.t.Logf(">> %s", bytes.TrimSpace(reqBytes)) |
| 175 | + reqCopy := *req |
| 176 | + reqCopy.Body = ioutil.NopCloser(bytes.NewReader(reqBytes)) |
| 177 | + |
| 178 | + // Do the round trip. |
| 179 | + resp, err := rt.inner.RoundTrip(&reqCopy) |
| 180 | + if err != nil { |
| 181 | + return nil, err |
| 182 | + } |
| 183 | + defer resp.Body.Close() |
| 184 | + |
| 185 | + // Read and log the response bytes. |
| 186 | + respBytes, err := ioutil.ReadAll(resp.Body) |
| 187 | + if err != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + respCopy := *resp |
| 191 | + respCopy.Body = ioutil.NopCloser(bytes.NewReader(respBytes)) |
| 192 | + rt.t.Logf("<< %s", bytes.TrimSpace(respBytes)) |
| 193 | + return &respCopy, nil |
| 194 | +} |
| 195 | + |
| 196 | +// loadTests walks the given directory looking for *.io files to load. |
| 197 | +func loadTests(t *hivesim.T, root string, re *regexp.Regexp) []test { |
| 198 | + tests := make([]test, 0) |
| 199 | + filepath.Walk(root, func(path string, info os.FileInfo, err error) error { |
| 200 | + if err != nil { |
| 201 | + t.Logf("unable to walk path: %s", err) |
| 202 | + return err |
| 203 | + } |
| 204 | + if info.IsDir() { |
| 205 | + return nil |
| 206 | + } |
| 207 | + if fname := info.Name(); !strings.HasSuffix(fname, ".io") { |
| 208 | + return nil |
| 209 | + } |
| 210 | + pathname := strings.TrimSuffix(strings.TrimPrefix(path, root), ".io") |
| 211 | + if !re.MatchString(pathname) { |
| 212 | + fmt.Println("skip", pathname) |
| 213 | + return nil // skip |
| 214 | + } |
| 215 | + data, err := ioutil.ReadFile(path) |
| 216 | + if err != nil { |
| 217 | + return err |
| 218 | + } |
| 219 | + tests = append(tests, test{strings.TrimLeft(pathname, "/"), data}) |
| 220 | + return nil |
| 221 | + }) |
| 222 | + return tests |
| 223 | +} |
0 commit comments