blob: 566c3f70eb0a60753e803b0f0128d08c0565c276 [file] [log] [blame]
David Benjamin491b9212015-02-11 14:18:45 -05001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15package main
16
17import (
18 "bytes"
David Benjamin5f237bc2015-02-11 17:14:15 -050019 "encoding/json"
David Benjamin491b9212015-02-11 14:18:45 -050020 "flag"
21 "fmt"
22 "os"
23 "os/exec"
24 "path"
David Benjamin0b635c52015-05-15 19:08:49 -040025 "strconv"
David Benjamin491b9212015-02-11 14:18:45 -050026 "strings"
David Benjamin0b635c52015-05-15 19:08:49 -040027 "syscall"
David Benjamin5f237bc2015-02-11 17:14:15 -050028 "time"
David Benjamin491b9212015-02-11 14:18:45 -050029)
30
31// TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner.
32
33var (
David Benjamin0b635c52015-05-15 19:08:49 -040034 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
35 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
36 buildDir = flag.String("build-dir", "build", "The build directory to run the tests from.")
37 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
38 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
39 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask each test to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
David Benjamin491b9212015-02-11 14:18:45 -050040)
41
42type test []string
43
David Benjamin5f237bc2015-02-11 17:14:15 -050044// testOutput is a representation of Chromium's JSON test result format. See
45// https://www.chromium.org/developers/the-json-test-results-format
46type testOutput struct {
47 Version int `json:"version"`
48 Interrupted bool `json:"interrupted"`
49 PathDelimiter string `json:"path_delimiter"`
50 SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
51 NumFailuresByType map[string]int `json:"num_failures_by_type"`
52 Tests map[string]testResult `json:"tests"`
53}
54
55type testResult struct {
David Benjamin7ead6052015-04-04 03:57:26 -040056 Actual string `json:"actual"`
57 Expected string `json:"expected"`
58 IsUnexpected bool `json:"is_unexpected"`
David Benjamin5f237bc2015-02-11 17:14:15 -050059}
60
61func newTestOutput() *testOutput {
62 return &testOutput{
63 Version: 3,
64 PathDelimiter: ".",
65 SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
66 NumFailuresByType: make(map[string]int),
67 Tests: make(map[string]testResult),
68 }
69}
70
71func (t *testOutput) addResult(name, result string) {
72 if _, found := t.Tests[name]; found {
73 panic(name)
74 }
David Benjamin7ead6052015-04-04 03:57:26 -040075 t.Tests[name] = testResult{
76 Actual: result,
77 Expected: "PASS",
78 IsUnexpected: result != "PASS",
79 }
David Benjamin5f237bc2015-02-11 17:14:15 -050080 t.NumFailuresByType[result]++
81}
82
83func (t *testOutput) writeTo(name string) error {
84 file, err := os.Create(name)
85 if err != nil {
86 return err
87 }
88 defer file.Close()
89 out, err := json.MarshalIndent(t, "", " ")
90 if err != nil {
91 return err
92 }
93 _, err = file.Write(out)
94 return err
95}
96
David Benjamin491b9212015-02-11 14:18:45 -050097func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
98 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
99 if dbAttach {
100 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
101 }
102 valgrindArgs = append(valgrindArgs, path)
103 valgrindArgs = append(valgrindArgs, args...)
104
105 return exec.Command("valgrind", valgrindArgs...)
106}
107
David Benjamin0b635c52015-05-15 19:08:49 -0400108func gdbOf(path string, args ...string) *exec.Cmd {
109 xtermArgs := []string{"-e", "gdb", "--args"}
110 xtermArgs = append(xtermArgs, path)
111 xtermArgs = append(xtermArgs, args...)
112
113 return exec.Command("xterm", xtermArgs...)
114}
115
116type moreMallocsError struct{}
117
118func (moreMallocsError) Error() string {
119 return "child process did not exhaust all allocation calls"
120}
121
122var errMoreMallocs = moreMallocsError{}
123
124func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) {
David Benjamin491b9212015-02-11 14:18:45 -0500125 prog := path.Join(*buildDir, test[0])
126 args := test[1:]
127 var cmd *exec.Cmd
128 if *useValgrind {
129 cmd = valgrindOf(false, prog, args...)
David Benjamin0b635c52015-05-15 19:08:49 -0400130 } else if *useGDB {
131 cmd = gdbOf(prog, args...)
David Benjamin491b9212015-02-11 14:18:45 -0500132 } else {
133 cmd = exec.Command(prog, args...)
134 }
135 var stdoutBuf bytes.Buffer
David Benjamin0b635c52015-05-15 19:08:49 -0400136 var stderrBuf bytes.Buffer
David Benjamin491b9212015-02-11 14:18:45 -0500137 cmd.Stdout = &stdoutBuf
David Benjamin0b635c52015-05-15 19:08:49 -0400138 cmd.Stderr = &stderrBuf
139 if mallocNumToFail >= 0 {
140 cmd.Env = os.Environ()
141 cmd.Env = append(cmd.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
142 if *mallocTestDebug {
143 cmd.Env = append(cmd.Env, "MALLOC_ABORT_ON_FAIL=1")
144 }
145 cmd.Env = append(cmd.Env, "_MALLOC_CHECK=1")
146 }
David Benjamin491b9212015-02-11 14:18:45 -0500147
148 if err := cmd.Start(); err != nil {
149 return false, err
150 }
151 if err := cmd.Wait(); err != nil {
David Benjamin0b635c52015-05-15 19:08:49 -0400152 if exitError, ok := err.(*exec.ExitError); ok {
153 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
154 return false, errMoreMallocs
155 }
156 }
157 fmt.Print(string(stderrBuf.Bytes()))
David Benjamin491b9212015-02-11 14:18:45 -0500158 return false, err
159 }
David Benjamin0b635c52015-05-15 19:08:49 -0400160 fmt.Print(string(stderrBuf.Bytes()))
David Benjamin491b9212015-02-11 14:18:45 -0500161
162 // Account for Windows line-endings.
163 stdout := bytes.Replace(stdoutBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1)
164
165 if bytes.HasSuffix(stdout, []byte("PASS\n")) &&
166 (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') {
167 return true, nil
168 }
169 return false, nil
170}
171
David Benjamin0b635c52015-05-15 19:08:49 -0400172func runTest(test test) (bool, error) {
173 if *mallocTest < 0 {
174 return runTestOnce(test, -1)
175 }
176
177 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
178 if passed, err := runTestOnce(test, mallocNumToFail); err != errMoreMallocs {
179 if err != nil {
180 err = fmt.Errorf("at malloc %d: %s", mallocNumToFail, err)
181 }
182 return passed, err
183 }
184 }
185}
186
David Benjamin5c694e32015-05-11 15:58:08 -0400187// shortTestName returns the short name of a test. Except for evp_test, it
188// assumes that any argument which ends in .txt is a path to a data file and not
189// relevant to the test's uniqueness.
David Benjamin5f237bc2015-02-11 17:14:15 -0500190func shortTestName(test test) string {
191 var args []string
192 for _, arg := range test {
David Benjamin5c694e32015-05-11 15:58:08 -0400193 if test[0] == "crypto/evp/evp_test" || !strings.HasSuffix(arg, ".txt") {
David Benjamin5f237bc2015-02-11 17:14:15 -0500194 args = append(args, arg)
195 }
196 }
197 return strings.Join(args, " ")
198}
199
Adam Langley117da412015-06-10 17:32:25 -0700200// setWorkingDirectory walks up directories as needed until the current working
201// directory is the top of a BoringSSL checkout.
202func setWorkingDirectory() {
203 for i := 0; i < 64; i++ {
David Benjamin95aaf4a2015-09-03 12:09:36 -0400204 if _, err := os.Stat("BUILDING.md"); err == nil {
Adam Langley117da412015-06-10 17:32:25 -0700205 return
206 }
207 os.Chdir("..")
208 }
209
David Benjamin95aaf4a2015-09-03 12:09:36 -0400210 panic("Couldn't find BUILDING.md in a parent directory!")
Adam Langley117da412015-06-10 17:32:25 -0700211}
212
213func parseTestConfig(filename string) ([]test, error) {
214 in, err := os.Open(filename)
215 if err != nil {
216 return nil, err
217 }
218 defer in.Close()
219
220 decoder := json.NewDecoder(in)
221 var result []test
222 if err := decoder.Decode(&result); err != nil {
223 return nil, err
224 }
225 return result, nil
226}
227
David Benjamin491b9212015-02-11 14:18:45 -0500228func main() {
229 flag.Parse()
Adam Langley117da412015-06-10 17:32:25 -0700230 setWorkingDirectory()
231
232 tests, err := parseTestConfig("util/all_tests.json")
233 if err != nil {
234 fmt.Printf("Failed to parse input: %s\n", err)
235 os.Exit(1)
236 }
David Benjamin491b9212015-02-11 14:18:45 -0500237
David Benjamin5f237bc2015-02-11 17:14:15 -0500238 testOutput := newTestOutput()
David Benjamin491b9212015-02-11 14:18:45 -0500239 var failed []test
240 for _, test := range tests {
241 fmt.Printf("%s\n", strings.Join([]string(test), " "))
David Benjamin5f237bc2015-02-11 17:14:15 -0500242
243 name := shortTestName(test)
David Benjamin491b9212015-02-11 14:18:45 -0500244 passed, err := runTest(test)
245 if err != nil {
David Benjamin5f237bc2015-02-11 17:14:15 -0500246 fmt.Printf("%s failed to complete: %s\n", test[0], err)
David Benjamin491b9212015-02-11 14:18:45 -0500247 failed = append(failed, test)
David Benjamin5f237bc2015-02-11 17:14:15 -0500248 testOutput.addResult(name, "CRASHED")
249 } else if !passed {
David Benjamin491b9212015-02-11 14:18:45 -0500250 fmt.Printf("%s failed to print PASS on the last line.\n", test[0])
251 failed = append(failed, test)
David Benjamin5f237bc2015-02-11 17:14:15 -0500252 testOutput.addResult(name, "FAIL")
253 } else {
254 testOutput.addResult(name, "PASS")
David Benjamin491b9212015-02-11 14:18:45 -0500255 }
256 }
257
David Benjamin5f237bc2015-02-11 17:14:15 -0500258 if *jsonOutput != "" {
259 if err := testOutput.writeTo(*jsonOutput); err != nil {
260 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
261 }
262 }
David Benjamin2ab7a862015-04-04 17:02:18 -0400263
264 if len(failed) > 0 {
265 fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(tests))
266 for _, test := range failed {
267 fmt.Printf("\t%s\n", strings.Join([]string(test), " "))
268 }
269 os.Exit(1)
270 }
271
272 fmt.Printf("\nAll tests passed!\n")
David Benjamin491b9212015-02-11 14:18:45 -0500273}