blob: 785e42304af78dce1a0957a773dd547f9bf5a275 [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"
25 "strings"
David Benjamin5f237bc2015-02-11 17:14:15 -050026 "time"
David Benjamin491b9212015-02-11 14:18:45 -050027)
28
29// TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner.
30
31var (
32 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
33 buildDir = flag.String("build-dir", "build", "The build directory to run the tests from.")
David Benjamin5f237bc2015-02-11 17:14:15 -050034 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
David Benjamin491b9212015-02-11 14:18:45 -050035)
36
37type test []string
38
39var tests = []test{
40 {"crypto/base64/base64_test"},
41 {"crypto/bio/bio_test"},
42 {"crypto/bn/bn_test"},
43 {"crypto/bytestring/bytestring_test"},
44 {"crypto/cipher/aead_test", "aes-128-gcm", "crypto/cipher/test/aes_128_gcm_tests.txt"},
45 {"crypto/cipher/aead_test", "aes-128-key-wrap", "crypto/cipher/test/aes_128_key_wrap_tests.txt"},
46 {"crypto/cipher/aead_test", "aes-256-gcm", "crypto/cipher/test/aes_256_gcm_tests.txt"},
47 {"crypto/cipher/aead_test", "aes-256-key-wrap", "crypto/cipher/test/aes_256_key_wrap_tests.txt"},
48 {"crypto/cipher/aead_test", "chacha20-poly1305", "crypto/cipher/test/chacha20_poly1305_tests.txt"},
49 {"crypto/cipher/aead_test", "rc4-md5-tls", "crypto/cipher/test/rc4_md5_tls_tests.txt"},
50 {"crypto/cipher/aead_test", "rc4-sha1-tls", "crypto/cipher/test/rc4_sha1_tls_tests.txt"},
51 {"crypto/cipher/aead_test", "aes-128-cbc-sha1-tls", "crypto/cipher/test/aes_128_cbc_sha1_tls_tests.txt"},
52 {"crypto/cipher/aead_test", "aes-128-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/aes_128_cbc_sha1_tls_implicit_iv_tests.txt"},
53 {"crypto/cipher/aead_test", "aes-128-cbc-sha256-tls", "crypto/cipher/test/aes_128_cbc_sha256_tls_tests.txt"},
54 {"crypto/cipher/aead_test", "aes-256-cbc-sha1-tls", "crypto/cipher/test/aes_256_cbc_sha1_tls_tests.txt"},
55 {"crypto/cipher/aead_test", "aes-256-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/aes_256_cbc_sha1_tls_implicit_iv_tests.txt"},
56 {"crypto/cipher/aead_test", "aes-256-cbc-sha256-tls", "crypto/cipher/test/aes_256_cbc_sha256_tls_tests.txt"},
57 {"crypto/cipher/aead_test", "aes-256-cbc-sha384-tls", "crypto/cipher/test/aes_256_cbc_sha384_tls_tests.txt"},
58 {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-tls", "crypto/cipher/test/des_ede3_cbc_sha1_tls_tests.txt"},
59 {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/des_ede3_cbc_sha1_tls_implicit_iv_tests.txt"},
60 {"crypto/cipher/aead_test", "rc4-md5-ssl3", "crypto/cipher/test/rc4_md5_ssl3_tests.txt"},
61 {"crypto/cipher/aead_test", "rc4-sha1-ssl3", "crypto/cipher/test/rc4_sha1_ssl3_tests.txt"},
62 {"crypto/cipher/aead_test", "aes-128-cbc-sha1-ssl3", "crypto/cipher/test/aes_128_cbc_sha1_ssl3_tests.txt"},
63 {"crypto/cipher/aead_test", "aes-256-cbc-sha1-ssl3", "crypto/cipher/test/aes_256_cbc_sha1_ssl3_tests.txt"},
64 {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-ssl3", "crypto/cipher/test/des_ede3_cbc_sha1_ssl3_tests.txt"},
Adam Langley0e782a92015-03-13 12:11:00 -070065 {"crypto/cipher/aead_test", "aes-128-ctr-hmac-sha256", "crypto/cipher/test/aes_128_ctr_hmac_sha256.txt"},
66 {"crypto/cipher/aead_test", "aes-256-ctr-hmac-sha256", "crypto/cipher/test/aes_256_ctr_hmac_sha256.txt"},
David Benjamin491b9212015-02-11 14:18:45 -050067 {"crypto/cipher/cipher_test", "crypto/cipher/test/cipher_test.txt"},
68 {"crypto/constant_time_test"},
69 {"crypto/dh/dh_test"},
70 {"crypto/digest/digest_test"},
71 {"crypto/dsa/dsa_test"},
72 {"crypto/ec/ec_test"},
73 {"crypto/ec/example_mul"},
74 {"crypto/ecdsa/ecdsa_test"},
75 {"crypto/err/err_test"},
76 {"crypto/evp/evp_test"},
77 {"crypto/evp/pbkdf_test"},
78 {"crypto/hkdf/hkdf_test"},
79 {"crypto/hmac/hmac_test"},
80 {"crypto/lhash/lhash_test"},
81 {"crypto/modes/gcm_test"},
82 {"crypto/pkcs8/pkcs12_test"},
83 {"crypto/rsa/rsa_test"},
Adam Langleyd7c5dfb2015-03-16 12:48:56 -070084 {"crypto/thread_test"},
David Benjamin491b9212015-02-11 14:18:45 -050085 {"crypto/x509/pkcs7_test"},
86 {"crypto/x509v3/tab_test"},
87 {"crypto/x509v3/v3name_test"},
88 {"ssl/pqueue/pqueue_test"},
89 {"ssl/ssl_test"},
90}
91
David Benjamin5f237bc2015-02-11 17:14:15 -050092// testOutput is a representation of Chromium's JSON test result format. See
93// https://www.chromium.org/developers/the-json-test-results-format
94type testOutput struct {
95 Version int `json:"version"`
96 Interrupted bool `json:"interrupted"`
97 PathDelimiter string `json:"path_delimiter"`
98 SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
99 NumFailuresByType map[string]int `json:"num_failures_by_type"`
100 Tests map[string]testResult `json:"tests"`
101}
102
103type testResult struct {
David Benjamin7ead6052015-04-04 03:57:26 -0400104 Actual string `json:"actual"`
105 Expected string `json:"expected"`
106 IsUnexpected bool `json:"is_unexpected"`
David Benjamin5f237bc2015-02-11 17:14:15 -0500107}
108
109func newTestOutput() *testOutput {
110 return &testOutput{
111 Version: 3,
112 PathDelimiter: ".",
113 SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
114 NumFailuresByType: make(map[string]int),
115 Tests: make(map[string]testResult),
116 }
117}
118
119func (t *testOutput) addResult(name, result string) {
120 if _, found := t.Tests[name]; found {
121 panic(name)
122 }
David Benjamin7ead6052015-04-04 03:57:26 -0400123 t.Tests[name] = testResult{
124 Actual: result,
125 Expected: "PASS",
126 IsUnexpected: result != "PASS",
127 }
David Benjamin5f237bc2015-02-11 17:14:15 -0500128 t.NumFailuresByType[result]++
129}
130
131func (t *testOutput) writeTo(name string) error {
132 file, err := os.Create(name)
133 if err != nil {
134 return err
135 }
136 defer file.Close()
137 out, err := json.MarshalIndent(t, "", " ")
138 if err != nil {
139 return err
140 }
141 _, err = file.Write(out)
142 return err
143}
144
David Benjamin491b9212015-02-11 14:18:45 -0500145func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
146 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
147 if dbAttach {
148 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
149 }
150 valgrindArgs = append(valgrindArgs, path)
151 valgrindArgs = append(valgrindArgs, args...)
152
153 return exec.Command("valgrind", valgrindArgs...)
154}
155
156func runTest(test test) (passed bool, err error) {
157 prog := path.Join(*buildDir, test[0])
158 args := test[1:]
159 var cmd *exec.Cmd
160 if *useValgrind {
161 cmd = valgrindOf(false, prog, args...)
162 } else {
163 cmd = exec.Command(prog, args...)
164 }
165 var stdoutBuf bytes.Buffer
166 cmd.Stdout = &stdoutBuf
167 cmd.Stderr = os.Stderr
168
169 if err := cmd.Start(); err != nil {
170 return false, err
171 }
172 if err := cmd.Wait(); err != nil {
173 return false, err
174 }
175
176 // Account for Windows line-endings.
177 stdout := bytes.Replace(stdoutBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1)
178
179 if bytes.HasSuffix(stdout, []byte("PASS\n")) &&
180 (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') {
181 return true, nil
182 }
183 return false, nil
184}
185
David Benjamin5f237bc2015-02-11 17:14:15 -0500186// shortTestName returns the short name of a test. It assumes that any argument
187// which ends in .txt is a path to a data file and not relevant to the test's
188// uniqueness.
189func shortTestName(test test) string {
190 var args []string
191 for _, arg := range test {
192 if !strings.HasSuffix(arg, ".txt") {
193 args = append(args, arg)
194 }
195 }
196 return strings.Join(args, " ")
197}
198
David Benjamin491b9212015-02-11 14:18:45 -0500199func main() {
200 flag.Parse()
201
David Benjamin5f237bc2015-02-11 17:14:15 -0500202 testOutput := newTestOutput()
David Benjamin491b9212015-02-11 14:18:45 -0500203 var failed []test
204 for _, test := range tests {
205 fmt.Printf("%s\n", strings.Join([]string(test), " "))
David Benjamin5f237bc2015-02-11 17:14:15 -0500206
207 name := shortTestName(test)
David Benjamin491b9212015-02-11 14:18:45 -0500208 passed, err := runTest(test)
209 if err != nil {
David Benjamin5f237bc2015-02-11 17:14:15 -0500210 fmt.Printf("%s failed to complete: %s\n", test[0], err)
David Benjamin491b9212015-02-11 14:18:45 -0500211 failed = append(failed, test)
David Benjamin5f237bc2015-02-11 17:14:15 -0500212 testOutput.addResult(name, "CRASHED")
213 } else if !passed {
David Benjamin491b9212015-02-11 14:18:45 -0500214 fmt.Printf("%s failed to print PASS on the last line.\n", test[0])
215 failed = append(failed, test)
David Benjamin5f237bc2015-02-11 17:14:15 -0500216 testOutput.addResult(name, "FAIL")
217 } else {
218 testOutput.addResult(name, "PASS")
David Benjamin491b9212015-02-11 14:18:45 -0500219 }
220 }
221
David Benjamin5f237bc2015-02-11 17:14:15 -0500222 if *jsonOutput != "" {
223 if err := testOutput.writeTo(*jsonOutput); err != nil {
224 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
225 }
226 }
David Benjamin2ab7a862015-04-04 17:02:18 -0400227
228 if len(failed) > 0 {
229 fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(tests))
230 for _, test := range failed {
231 fmt.Printf("\t%s\n", strings.Join([]string(test), " "))
232 }
233 os.Exit(1)
234 }
235
236 fmt.Printf("\nAll tests passed!\n")
David Benjamin491b9212015-02-11 14:18:45 -0500237}