Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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