Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

242 rader
8.5 KiB

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