25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

296 lines
10 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. "strconv"
  24. "strings"
  25. "syscall"
  26. "time"
  27. )
  28. // TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner.
  29. var (
  30. useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
  31. useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
  32. buildDir = flag.String("build-dir", "build", "The build directory to run the tests from.")
  33. jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
  34. mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
  35. 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.")
  36. )
  37. type test []string
  38. var tests = []test{
  39. {"crypto/base64/base64_test"},
  40. {"crypto/bio/bio_test"},
  41. {"crypto/bn/bn_test"},
  42. {"crypto/bytestring/bytestring_test"},
  43. {"crypto/cipher/aead_test", "aes-128-gcm", "crypto/cipher/test/aes_128_gcm_tests.txt"},
  44. {"crypto/cipher/aead_test", "aes-128-key-wrap", "crypto/cipher/test/aes_128_key_wrap_tests.txt"},
  45. {"crypto/cipher/aead_test", "aes-256-gcm", "crypto/cipher/test/aes_256_gcm_tests.txt"},
  46. {"crypto/cipher/aead_test", "aes-256-key-wrap", "crypto/cipher/test/aes_256_key_wrap_tests.txt"},
  47. {"crypto/cipher/aead_test", "chacha20-poly1305", "crypto/cipher/test/chacha20_poly1305_tests.txt"},
  48. {"crypto/cipher/aead_test", "rc4-md5-tls", "crypto/cipher/test/rc4_md5_tls_tests.txt"},
  49. {"crypto/cipher/aead_test", "rc4-sha1-tls", "crypto/cipher/test/rc4_sha1_tls_tests.txt"},
  50. {"crypto/cipher/aead_test", "aes-128-cbc-sha1-tls", "crypto/cipher/test/aes_128_cbc_sha1_tls_tests.txt"},
  51. {"crypto/cipher/aead_test", "aes-128-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/aes_128_cbc_sha1_tls_implicit_iv_tests.txt"},
  52. {"crypto/cipher/aead_test", "aes-128-cbc-sha256-tls", "crypto/cipher/test/aes_128_cbc_sha256_tls_tests.txt"},
  53. {"crypto/cipher/aead_test", "aes-256-cbc-sha1-tls", "crypto/cipher/test/aes_256_cbc_sha1_tls_tests.txt"},
  54. {"crypto/cipher/aead_test", "aes-256-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/aes_256_cbc_sha1_tls_implicit_iv_tests.txt"},
  55. {"crypto/cipher/aead_test", "aes-256-cbc-sha256-tls", "crypto/cipher/test/aes_256_cbc_sha256_tls_tests.txt"},
  56. {"crypto/cipher/aead_test", "aes-256-cbc-sha384-tls", "crypto/cipher/test/aes_256_cbc_sha384_tls_tests.txt"},
  57. {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-tls", "crypto/cipher/test/des_ede3_cbc_sha1_tls_tests.txt"},
  58. {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-tls-implicit-iv", "crypto/cipher/test/des_ede3_cbc_sha1_tls_implicit_iv_tests.txt"},
  59. {"crypto/cipher/aead_test", "rc4-md5-ssl3", "crypto/cipher/test/rc4_md5_ssl3_tests.txt"},
  60. {"crypto/cipher/aead_test", "rc4-sha1-ssl3", "crypto/cipher/test/rc4_sha1_ssl3_tests.txt"},
  61. {"crypto/cipher/aead_test", "aes-128-cbc-sha1-ssl3", "crypto/cipher/test/aes_128_cbc_sha1_ssl3_tests.txt"},
  62. {"crypto/cipher/aead_test", "aes-256-cbc-sha1-ssl3", "crypto/cipher/test/aes_256_cbc_sha1_ssl3_tests.txt"},
  63. {"crypto/cipher/aead_test", "des-ede3-cbc-sha1-ssl3", "crypto/cipher/test/des_ede3_cbc_sha1_ssl3_tests.txt"},
  64. {"crypto/cipher/aead_test", "aes-128-ctr-hmac-sha256", "crypto/cipher/test/aes_128_ctr_hmac_sha256.txt"},
  65. {"crypto/cipher/aead_test", "aes-256-ctr-hmac-sha256", "crypto/cipher/test/aes_256_ctr_hmac_sha256.txt"},
  66. {"crypto/cipher/cipher_test", "crypto/cipher/test/cipher_test.txt"},
  67. {"crypto/cmac/cmac_test"},
  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_extra_test"},
  77. {"crypto/evp/evp_test", "crypto/evp/evp_tests.txt"},
  78. {"crypto/evp/evp_test", "crypto/hmac/hmac_tests.txt"},
  79. {"crypto/evp/pbkdf_test"},
  80. {"crypto/hkdf/hkdf_test"},
  81. {"crypto/hmac/hmac_test", "crypto/hmac/hmac_tests.txt"},
  82. {"crypto/lhash/lhash_test"},
  83. {"crypto/modes/gcm_test"},
  84. {"crypto/pkcs8/pkcs12_test"},
  85. {"crypto/refcount_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. // testOutput is a representation of Chromium's JSON test result format. See
  95. // https://www.chromium.org/developers/the-json-test-results-format
  96. type testOutput struct {
  97. Version int `json:"version"`
  98. Interrupted bool `json:"interrupted"`
  99. PathDelimiter string `json:"path_delimiter"`
  100. SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
  101. NumFailuresByType map[string]int `json:"num_failures_by_type"`
  102. Tests map[string]testResult `json:"tests"`
  103. }
  104. type testResult struct {
  105. Actual string `json:"actual"`
  106. Expected string `json:"expected"`
  107. IsUnexpected bool `json:"is_unexpected"`
  108. }
  109. func 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. func (t *testOutput) addResult(name, result string) {
  119. if _, found := t.Tests[name]; found {
  120. panic(name)
  121. }
  122. t.Tests[name] = testResult{
  123. Actual: result,
  124. Expected: "PASS",
  125. IsUnexpected: result != "PASS",
  126. }
  127. t.NumFailuresByType[result]++
  128. }
  129. func (t *testOutput) writeTo(name string) error {
  130. file, err := os.Create(name)
  131. if err != nil {
  132. return err
  133. }
  134. defer file.Close()
  135. out, err := json.MarshalIndent(t, "", " ")
  136. if err != nil {
  137. return err
  138. }
  139. _, err = file.Write(out)
  140. return err
  141. }
  142. func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
  143. valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full"}
  144. if dbAttach {
  145. valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
  146. }
  147. valgrindArgs = append(valgrindArgs, path)
  148. valgrindArgs = append(valgrindArgs, args...)
  149. return exec.Command("valgrind", valgrindArgs...)
  150. }
  151. func gdbOf(path string, args ...string) *exec.Cmd {
  152. xtermArgs := []string{"-e", "gdb", "--args"}
  153. xtermArgs = append(xtermArgs, path)
  154. xtermArgs = append(xtermArgs, args...)
  155. return exec.Command("xterm", xtermArgs...)
  156. }
  157. type moreMallocsError struct{}
  158. func (moreMallocsError) Error() string {
  159. return "child process did not exhaust all allocation calls"
  160. }
  161. var errMoreMallocs = moreMallocsError{}
  162. func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) {
  163. prog := path.Join(*buildDir, test[0])
  164. args := test[1:]
  165. var cmd *exec.Cmd
  166. if *useValgrind {
  167. cmd = valgrindOf(false, prog, args...)
  168. } else if *useGDB {
  169. cmd = gdbOf(prog, args...)
  170. } else {
  171. cmd = exec.Command(prog, args...)
  172. }
  173. var stdoutBuf bytes.Buffer
  174. var stderrBuf bytes.Buffer
  175. cmd.Stdout = &stdoutBuf
  176. cmd.Stderr = &stderrBuf
  177. if mallocNumToFail >= 0 {
  178. cmd.Env = os.Environ()
  179. cmd.Env = append(cmd.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
  180. if *mallocTestDebug {
  181. cmd.Env = append(cmd.Env, "MALLOC_ABORT_ON_FAIL=1")
  182. }
  183. cmd.Env = append(cmd.Env, "_MALLOC_CHECK=1")
  184. }
  185. if err := cmd.Start(); err != nil {
  186. return false, err
  187. }
  188. if err := cmd.Wait(); err != nil {
  189. if exitError, ok := err.(*exec.ExitError); ok {
  190. if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
  191. return false, errMoreMallocs
  192. }
  193. }
  194. fmt.Print(string(stderrBuf.Bytes()))
  195. return false, err
  196. }
  197. fmt.Print(string(stderrBuf.Bytes()))
  198. // Account for Windows line-endings.
  199. stdout := bytes.Replace(stdoutBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1)
  200. if bytes.HasSuffix(stdout, []byte("PASS\n")) &&
  201. (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') {
  202. return true, nil
  203. }
  204. return false, nil
  205. }
  206. func runTest(test test) (bool, error) {
  207. if *mallocTest < 0 {
  208. return runTestOnce(test, -1)
  209. }
  210. for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
  211. if passed, err := runTestOnce(test, mallocNumToFail); err != errMoreMallocs {
  212. if err != nil {
  213. err = fmt.Errorf("at malloc %d: %s", mallocNumToFail, err)
  214. }
  215. return passed, err
  216. }
  217. }
  218. }
  219. // shortTestName returns the short name of a test. Except for evp_test, it
  220. // assumes that any argument which ends in .txt is a path to a data file and not
  221. // relevant to the test's uniqueness.
  222. func shortTestName(test test) string {
  223. var args []string
  224. for _, arg := range test {
  225. if test[0] == "crypto/evp/evp_test" || !strings.HasSuffix(arg, ".txt") {
  226. args = append(args, arg)
  227. }
  228. }
  229. return strings.Join(args, " ")
  230. }
  231. func main() {
  232. flag.Parse()
  233. testOutput := newTestOutput()
  234. var failed []test
  235. for _, test := range tests {
  236. fmt.Printf("%s\n", strings.Join([]string(test), " "))
  237. name := shortTestName(test)
  238. passed, err := runTest(test)
  239. if err != nil {
  240. fmt.Printf("%s failed to complete: %s\n", test[0], err)
  241. failed = append(failed, test)
  242. testOutput.addResult(name, "CRASHED")
  243. } else if !passed {
  244. fmt.Printf("%s failed to print PASS on the last line.\n", test[0])
  245. failed = append(failed, test)
  246. testOutput.addResult(name, "FAIL")
  247. } else {
  248. testOutput.addResult(name, "PASS")
  249. }
  250. }
  251. if *jsonOutput != "" {
  252. if err := testOutput.writeTo(*jsonOutput); err != nil {
  253. fmt.Fprintf(os.Stderr, "Error: %s\n", err)
  254. }
  255. }
  256. if len(failed) > 0 {
  257. fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(tests))
  258. for _, test := range failed {
  259. fmt.Printf("\t%s\n", strings.Join([]string(test), " "))
  260. }
  261. os.Exit(1)
  262. }
  263. fmt.Printf("\nAll tests passed!\n")
  264. }