blob: 91822d157c09fc3af19195956321489d14cd896d [file] [log] [blame]
Adam Langleye9ada862015-05-11 17:20:37 -07001/* 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"
19 "encoding/json"
20 "flag"
21 "fmt"
22 "os"
23 "os/exec"
24 "path"
25 "strings"
26 "time"
27)
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.")
34 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
35)
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"},
65 {"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"},
67 {"crypto/cipher/cipher_test", "crypto/cipher/test/cipher_test.txt"},
68 {"crypto/cmac/cmac_test"},
69 {"crypto/constant_time_test"},
70 {"crypto/dh/dh_test"},
71 {"crypto/digest/digest_test"},
72 {"crypto/dsa/dsa_test"},
73 {"crypto/ec/ec_test"},
74 {"crypto/ec/example_mul"},
75 {"crypto/ecdsa/ecdsa_test"},
76 {"crypto/err/err_test"},
77 {"crypto/evp/evp_extra_test"},
78 {"crypto/evp/evp_test", "crypto/evp/evp_tests.txt"},
79 {"crypto/evp/evp_test", "crypto/hmac/hmac_tests.txt"},
80 {"crypto/evp/pbkdf_test"},
81 {"crypto/hkdf/hkdf_test"},
82 {"crypto/hmac/hmac_test", "crypto/hmac/hmac_tests.txt"},
83 {"crypto/lhash/lhash_test"},
84 {"crypto/modes/gcm_test"},
85 {"crypto/pkcs8/pkcs12_test"},
86 {"crypto/rsa/rsa_test"},
87 {"crypto/thread_test"},
88 {"crypto/x509/pkcs7_test"},
89 {"crypto/x509v3/tab_test"},
90 {"crypto/x509v3/v3name_test"},
91 {"ssl/pqueue/pqueue_test"},
92 {"ssl/ssl_test"},
93}
94
95// testOutput is a representation of Chromium's JSON test result format. See
96// https://www.chromium.org/developers/the-json-test-results-format
97type testOutput struct {
98 Version int `json:"version"`
99 Interrupted bool `json:"interrupted"`
100 PathDelimiter string `json:"path_delimiter"`
101 SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
102 NumFailuresByType map[string]int `json:"num_failures_by_type"`
103 Tests map[string]testResult `json:"tests"`
104}
105
106type testResult struct {
107 Actual string `json:"actual"`
108 Expected string `json:"expected"`
109 IsUnexpected bool `json:"is_unexpected"`
110}
111
112func newTestOutput() *testOutput {
113 return &testOutput{
114 Version: 3,
115 PathDelimiter: ".",
116 SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
117 NumFailuresByType: make(map[string]int),
118 Tests: make(map[string]testResult),
119 }
120}
121
122func (t *testOutput) addResult(name, result string) {
123 if _, found := t.Tests[name]; found {
124 panic(name)
125 }
126 t.Tests[name] = testResult{
127 Actual: result,
128 Expected: "PASS",
129 IsUnexpected: result != "PASS",
130 }
131 t.NumFailuresByType[result]++
132}
133
134func (t *testOutput) writeTo(name string) error {
135 file, err := os.Create(name)
136 if err != nil {
137 return err
138 }
139 defer file.Close()
140 out, err := json.MarshalIndent(t, "", " ")
141 if err != nil {
142 return err
143 }
144 _, err = file.Write(out)
145 return err
146}
147
148func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
149 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
150 if dbAttach {
151 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
152 }
153 valgrindArgs = append(valgrindArgs, path)
154 valgrindArgs = append(valgrindArgs, args...)
155
156 return exec.Command("valgrind", valgrindArgs...)
157}
158
159func runTest(test test) (passed bool, err error) {
160 prog := path.Join(*buildDir, test[0])
161 args := test[1:]
162 var cmd *exec.Cmd
163 if *useValgrind {
164 cmd = valgrindOf(false, prog, args...)
165 } else {
166 cmd = exec.Command(prog, args...)
167 }
168 var stdoutBuf bytes.Buffer
169 cmd.Stdout = &stdoutBuf
170 cmd.Stderr = os.Stderr
171
172 if err := cmd.Start(); err != nil {
173 return false, err
174 }
175 if err := cmd.Wait(); err != nil {
176 return false, err
177 }
178
179 // Account for Windows line-endings.
180 stdout := bytes.Replace(stdoutBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1)
181
182 if bytes.HasSuffix(stdout, []byte("PASS\n")) &&
183 (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') {
184 return true, nil
185 }
186 return false, nil
187}
188
189// shortTestName returns the short name of a test. Except for evp_test, it
190// assumes that any argument which ends in .txt is a path to a data file and not
191// relevant to the test's uniqueness.
192func shortTestName(test test) string {
193 var args []string
194 for _, arg := range test {
195 if test[0] == "crypto/evp/evp_test" || !strings.HasSuffix(arg, ".txt") {
196 args = append(args, arg)
197 }
198 }
199 return strings.Join(args, " ")
200}
201
202func main() {
203 flag.Parse()
204
205 testOutput := newTestOutput()
206 var failed []test
207 for _, test := range tests {
208 fmt.Printf("%s\n", strings.Join([]string(test), " "))
209
210 name := shortTestName(test)
211 passed, err := runTest(test)
212 if err != nil {
213 fmt.Printf("%s failed to complete: %s\n", test[0], err)
214 failed = append(failed, test)
215 testOutput.addResult(name, "CRASHED")
216 } else if !passed {
217 fmt.Printf("%s failed to print PASS on the last line.\n", test[0])
218 failed = append(failed, test)
219 testOutput.addResult(name, "FAIL")
220 } else {
221 testOutput.addResult(name, "PASS")
222 }
223 }
224
225 if *jsonOutput != "" {
226 if err := testOutput.writeTo(*jsonOutput); err != nil {
227 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
228 }
229 }
230
231 if len(failed) > 0 {
232 fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(tests))
233 for _, test := range failed {
234 fmt.Printf("\t%s\n", strings.Join([]string(test), " "))
235 }
236 os.Exit(1)
237 }
238
239 fmt.Printf("\nAll tests passed!\n")
240}