You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright (c) 2016, 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. "encoding/json"
  17. "flag"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "os/exec"
  23. "path/filepath"
  24. "strings"
  25. )
  26. var (
  27. buildDir = flag.String("build-dir", "build", "Specifies the build directory to push.")
  28. device = flag.String("device", "", "Specifies the device or emulator. See adb's -s argument.")
  29. aarch64 = flag.Bool("aarch64", false, "Build the test runners for aarch64 instead of arm.")
  30. arm = flag.Int("arm", 7, "Which arm revision to build for.")
  31. allTestsArgs = flag.String("all-tests-args", "", "Specifies space-separated arguments to pass to all_tests.go")
  32. runnerArgs = flag.String("runner-args", "", "Specifies space-separated arguments to pass to ssl/test/runner")
  33. )
  34. func adb(args ...string) error {
  35. if len(*device) > 0 {
  36. args = append([]string{"-s", *device}, args...)
  37. }
  38. cmd := exec.Command("adb", args...)
  39. cmd.Stdout = os.Stdout
  40. cmd.Stderr = os.Stderr
  41. return cmd.Run()
  42. }
  43. func goTool(args ...string) error {
  44. cmd := exec.Command("go", args...)
  45. cmd.Stdout = os.Stdout
  46. cmd.Stderr = os.Stderr
  47. if *aarch64 {
  48. cmd.Env = append(cmd.Env, "GOARCH=arm64")
  49. } else {
  50. cmd.Env = append(cmd.Env, "GOARCH=arm")
  51. cmd.Env = append(cmd.Env, fmt.Sprintf("GOARM=%d", *arm))
  52. }
  53. return cmd.Run()
  54. }
  55. // setWorkingDirectory walks up directories as needed until the current working
  56. // directory is the top of a BoringSSL checkout.
  57. func setWorkingDirectory() {
  58. for i := 0; i < 64; i++ {
  59. if _, err := os.Stat("BUILDING.md"); err == nil {
  60. return
  61. }
  62. os.Chdir("..")
  63. }
  64. panic("Couldn't find BUILDING.md in a parent directory!")
  65. }
  66. type test []string
  67. func parseTestConfig(filename string) ([]test, error) {
  68. in, err := os.Open(filename)
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer in.Close()
  73. decoder := json.NewDecoder(in)
  74. var result []test
  75. if err := decoder.Decode(&result); err != nil {
  76. return nil, err
  77. }
  78. return result, nil
  79. }
  80. func copyFile(dst, src string) error {
  81. srcFile, err := os.Open(src)
  82. if err != nil {
  83. return err
  84. }
  85. defer srcFile.Close()
  86. srcInfo, err := srcFile.Stat()
  87. if err != nil {
  88. return err
  89. }
  90. dir := filepath.Dir(dst)
  91. if err := os.MkdirAll(dir, 0777); err != nil {
  92. return err
  93. }
  94. dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, srcInfo.Mode())
  95. if err != nil {
  96. return err
  97. }
  98. defer dstFile.Close()
  99. _, err = io.Copy(dstFile, srcFile)
  100. return err
  101. }
  102. func main() {
  103. flag.Parse()
  104. setWorkingDirectory()
  105. tests, err := parseTestConfig("util/all_tests.json")
  106. if err != nil {
  107. fmt.Printf("Failed to parse input: %s\n", err)
  108. os.Exit(1)
  109. }
  110. // Clear the target directory.
  111. if err := adb("shell", "rm -Rf /data/local/tmp/boringssl-tmp"); err != nil {
  112. fmt.Printf("Failed to clear target directory: %s\n", err)
  113. os.Exit(1)
  114. }
  115. // Stage everything in a temporary directory.
  116. tmpDir, err := ioutil.TempDir("", "boringssl-android")
  117. if err != nil {
  118. fmt.Printf("Error making temporary directory: %s\n", err)
  119. os.Exit(1)
  120. }
  121. defer os.RemoveAll(tmpDir)
  122. seenBinary := make(map[string]struct{})
  123. binaries := []string{"ssl/test/bssl_shim"}
  124. files := []string{
  125. "BUILDING.md",
  126. "util/all_tests.json",
  127. "ssl/test/runner/cert.pem",
  128. "ssl/test/runner/channel_id_key.pem",
  129. "ssl/test/runner/ecdsa_cert.pem",
  130. "ssl/test/runner/ecdsa_key.pem",
  131. "ssl/test/runner/key.pem",
  132. }
  133. for _, test := range tests {
  134. if _, ok := seenBinary[test[0]]; !ok {
  135. binaries = append(binaries, test[0])
  136. seenBinary[test[0]] = struct{}{}
  137. }
  138. for _, arg := range test[1:] {
  139. if strings.Contains(arg, "/") {
  140. files = append(files, arg)
  141. }
  142. }
  143. }
  144. fmt.Printf("Copying test binaries...\n")
  145. for _, binary := range binaries {
  146. if err := copyFile(filepath.Join(tmpDir, "build", binary), filepath.Join(*buildDir, binary)); err != nil {
  147. fmt.Printf("Failed to copy %s: %s\n", binary, err)
  148. os.Exit(1)
  149. }
  150. }
  151. fmt.Printf("Copying data files...\n")
  152. for _, file := range files {
  153. if err := copyFile(filepath.Join(tmpDir, file), file); err != nil {
  154. fmt.Printf("Failed to copy %s: %s\n", file, err)
  155. os.Exit(1)
  156. }
  157. }
  158. fmt.Printf("Building all_tests...\n")
  159. if err := goTool("build", "-o", filepath.Join(tmpDir, "util/all_tests"), "util/all_tests.go"); err != nil {
  160. fmt.Printf("Error building all_tests.go: %s\n", err)
  161. os.Exit(1)
  162. }
  163. fmt.Printf("Building runner...\n")
  164. if err := goTool("test", "-c", "-o", filepath.Join(tmpDir, "ssl/test/runner/runner"), "./ssl/test/runner/"); err != nil {
  165. fmt.Printf("Error building runner: %s\n", err)
  166. os.Exit(1)
  167. }
  168. fmt.Printf("Uploading files...\n")
  169. if err := adb("push", "-p", tmpDir, "/data/local/tmp/boringssl-tmp"); err != nil {
  170. fmt.Printf("Failed to push runner: %s\n", err)
  171. os.Exit(1)
  172. }
  173. fmt.Printf("Running unit tests...\n")
  174. if err := adb("shell", fmt.Sprintf("cd /data/local/tmp/boringssl-tmp && ./util/all_tests %s", *allTestsArgs)); err != nil {
  175. fmt.Printf("Failed to run unit tests: %s\n", err)
  176. os.Exit(1)
  177. }
  178. fmt.Printf("Running SSL tests...\n")
  179. if err := adb("shell", fmt.Sprintf("cd /data/local/tmp/boringssl-tmp/ssl/test/runner && ./runner %s", *runnerArgs)); err != nil {
  180. fmt.Printf("Failed to run SSL tests: %s\n", err)
  181. os.Exit(1)
  182. }
  183. }