|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "reflect" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/docker/docker/pkg/reexec" |
| 12 | + "github.com/ethereum/go-ethereum/internal/cmdtest" |
| 13 | +) |
| 14 | + |
| 15 | +func TestMain(m *testing.M) { |
| 16 | + // Run the app if we've been exec'd as "ethkey-test" in runEthkey. |
| 17 | + reexec.Register("evm-test", func() { |
| 18 | + if err := app.Run(os.Args); err != nil { |
| 19 | + fmt.Fprintln(os.Stderr, err) |
| 20 | + os.Exit(1) |
| 21 | + } |
| 22 | + os.Exit(0) |
| 23 | + }) |
| 24 | + // check if we have been reexec'd |
| 25 | + if reexec.Init() { |
| 26 | + return |
| 27 | + } |
| 28 | + os.Exit(m.Run()) |
| 29 | +} |
| 30 | + |
| 31 | +type testT8n struct { |
| 32 | + *cmdtest.TestCmd |
| 33 | +} |
| 34 | + |
| 35 | +type t8nInput struct { |
| 36 | + inAlloc string |
| 37 | + inTxs string |
| 38 | + inEnv string |
| 39 | + stFork string |
| 40 | + stReward string |
| 41 | +} |
| 42 | + |
| 43 | +func (args *t8nInput) get(base string) []string { |
| 44 | + var out []string |
| 45 | + if opt := args.inAlloc; opt != "" { |
| 46 | + out = append(out, "--input.alloc") |
| 47 | + out = append(out, fmt.Sprintf("%v/%v", base, opt)) |
| 48 | + } |
| 49 | + if opt := args.inTxs; opt != "" { |
| 50 | + out = append(out, "--input.txs") |
| 51 | + out = append(out, fmt.Sprintf("%v/%v", base, opt)) |
| 52 | + } |
| 53 | + if opt := args.inEnv; opt != "" { |
| 54 | + out = append(out, "--input.env") |
| 55 | + out = append(out, fmt.Sprintf("%v/%v", base, opt)) |
| 56 | + } |
| 57 | + if opt := args.stFork; opt != "" { |
| 58 | + out = append(out, "--state.fork", opt) |
| 59 | + } |
| 60 | + if opt := args.stReward; opt != "" { |
| 61 | + out = append(out, "--state.reward", opt) |
| 62 | + } |
| 63 | + return out |
| 64 | +} |
| 65 | + |
| 66 | +type t8nOutput struct { |
| 67 | + alloc bool |
| 68 | + result bool |
| 69 | + body bool |
| 70 | +} |
| 71 | + |
| 72 | +func (args *t8nOutput) get() (out []string) { |
| 73 | + out = append(out, "t8n") |
| 74 | + if args.body { |
| 75 | + out = append(out, "--output.body", "stdout") |
| 76 | + } else { |
| 77 | + out = append(out, "--output.body", "") // empty means ignore |
| 78 | + } |
| 79 | + if args.result { |
| 80 | + out = append(out, "--output.result", "stdout") |
| 81 | + } else { |
| 82 | + out = append(out, "--output.result", "") |
| 83 | + } |
| 84 | + if args.alloc { |
| 85 | + out = append(out, "--output.alloc", "stdout") |
| 86 | + } else { |
| 87 | + out = append(out, "--output.alloc", "") |
| 88 | + } |
| 89 | + return out |
| 90 | +} |
| 91 | + |
| 92 | +func TestT8n(t *testing.T) { |
| 93 | + tt := new(testT8n) |
| 94 | + tt.TestCmd = cmdtest.NewTestCmd(t, tt) |
| 95 | + for i, tc := range []struct { |
| 96 | + base string |
| 97 | + input t8nInput |
| 98 | + output t8nOutput |
| 99 | + expExitCode int |
| 100 | + expOut string |
| 101 | + }{ |
| 102 | + { // Test exit (3) on bad config |
| 103 | + base: "./testdata/1", |
| 104 | + input: t8nInput{ |
| 105 | + "alloc.json", "txs.json", "env.json", "Frontier+1346", "", |
| 106 | + }, |
| 107 | + output: t8nOutput{alloc: true, result: true}, |
| 108 | + expExitCode: 3, |
| 109 | + }, |
| 110 | + { |
| 111 | + base: "./testdata/1", |
| 112 | + input: t8nInput{ |
| 113 | + "alloc.json", "txs.json", "env.json", "Byzantium", "", |
| 114 | + }, |
| 115 | + output: t8nOutput{alloc: true, result: true}, |
| 116 | + expOut: "exp.json", |
| 117 | + }, |
| 118 | + { // blockhash test |
| 119 | + base: "./testdata/3", |
| 120 | + input: t8nInput{ |
| 121 | + "alloc.json", "txs.json", "env.json", "Berlin", "", |
| 122 | + }, |
| 123 | + output: t8nOutput{alloc: true, result: true}, |
| 124 | + expOut: "exp.json", |
| 125 | + }, |
| 126 | + { // missing blockhash test |
| 127 | + base: "./testdata/4", |
| 128 | + input: t8nInput{ |
| 129 | + "alloc.json", "txs.json", "env.json", "Berlin", "", |
| 130 | + }, |
| 131 | + output: t8nOutput{alloc: true, result: true}, |
| 132 | + expExitCode: 4, |
| 133 | + }, |
| 134 | + { // Ommer test |
| 135 | + base: "./testdata/5", |
| 136 | + input: t8nInput{ |
| 137 | + "alloc.json", "txs.json", "env.json", "Byzantium", "0x80", |
| 138 | + }, |
| 139 | + output: t8nOutput{alloc: true, result: true}, |
| 140 | + expOut: "exp.json", |
| 141 | + }, |
| 142 | + { // Sign json transactions |
| 143 | + base: "./testdata/13", |
| 144 | + input: t8nInput{ |
| 145 | + "alloc.json", "txs.json", "env.json", "London", "", |
| 146 | + }, |
| 147 | + output: t8nOutput{body: true}, |
| 148 | + expOut: "exp.json", |
| 149 | + }, |
| 150 | + { // Already signed transactions |
| 151 | + base: "./testdata/13", |
| 152 | + input: t8nInput{ |
| 153 | + "alloc.json", "signed_txs.rlp", "env.json", "London", "", |
| 154 | + }, |
| 155 | + output: t8nOutput{result: true}, |
| 156 | + expOut: "exp2.json", |
| 157 | + }, |
| 158 | + { // Difficulty calculation - no uncles |
| 159 | + base: "./testdata/14", |
| 160 | + input: t8nInput{ |
| 161 | + "alloc.json", "txs.json", "env.json", "London", "", |
| 162 | + }, |
| 163 | + output: t8nOutput{result: true}, |
| 164 | + expOut: "exp.json", |
| 165 | + }, |
| 166 | + { // Difficulty calculation - with uncles |
| 167 | + base: "./testdata/14", |
| 168 | + input: t8nInput{ |
| 169 | + "alloc.json", "txs.json", "env.uncles.json", "London", "", |
| 170 | + }, |
| 171 | + output: t8nOutput{result: true}, |
| 172 | + expOut: "exp2.json", |
| 173 | + }, |
| 174 | + } { |
| 175 | + |
| 176 | + args := append(tc.output.get(), tc.input.get(tc.base)...) |
| 177 | + tt.Run("evm-test", args...) |
| 178 | + tt.Logf("args: %v\n", strings.Join(args, " ")) |
| 179 | + // Compare the expected output, if provided |
| 180 | + if tc.expOut != "" { |
| 181 | + want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut)) |
| 182 | + if err != nil { |
| 183 | + t.Fatalf("test %d: could not read expected output: %v", i, err) |
| 184 | + } |
| 185 | + have := tt.Output() |
| 186 | + ok, err := cmpJson(have, want) |
| 187 | + switch { |
| 188 | + case err != nil: |
| 189 | + t.Fatalf("test %d, json parsing failed: %v", i, err) |
| 190 | + case !ok: |
| 191 | + t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want)) |
| 192 | + } |
| 193 | + } |
| 194 | + tt.WaitExit() |
| 195 | + if have, want := tt.ExitStatus(), tc.expExitCode; have != want { |
| 196 | + t.Fatalf("test %d: wrong exit code, have %d, want %d", i, have, want) |
| 197 | + } |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// cmpJson compares the JSON in two byte slices. |
| 202 | +func cmpJson(a, b []byte) (bool, error) { |
| 203 | + var j, j2 interface{} |
| 204 | + if err := json.Unmarshal(a, &j); err != nil { |
| 205 | + return false, err |
| 206 | + } |
| 207 | + if err := json.Unmarshal(b, &j2); err != nil { |
| 208 | + return false, err |
| 209 | + } |
| 210 | + return reflect.DeepEqual(j2, j), nil |
| 211 | +} |
0 commit comments