blob: 9d8f2daff822e916193c605a39d36fa54e7912b5 [file] [log] [blame]
David Benjaminaf18cdd2016-04-23 01:40:03 -04001// 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
David Benjaminf9459522016-03-07 15:30:26 -050015package main
16
17import (
18 "encoding/json"
19 "flag"
20 "fmt"
21 "io"
22 "io/ioutil"
23 "os"
24 "os/exec"
25 "path/filepath"
26 "strings"
27)
28
29var (
30 buildDir = flag.String("build-dir", "build", "Specifies the build directory to push.")
David Benjamin00b10692016-05-19 00:13:22 -040031 adbPath = flag.String("adb", "adb", "Specifies the adb binary to use. Defaults to looking in PATH.")
David Benjaminf9459522016-03-07 15:30:26 -050032 device = flag.String("device", "", "Specifies the device or emulator. See adb's -s argument.")
33 aarch64 = flag.Bool("aarch64", false, "Build the test runners for aarch64 instead of arm.")
34 arm = flag.Int("arm", 7, "Which arm revision to build for.")
David Benjamin8de8b3d2016-05-12 23:07:47 -040035 suite = flag.String("suite", "all", "Specifies the test suites to run (all, unit, or ssl).")
David Benjaminf9459522016-03-07 15:30:26 -050036 allTestsArgs = flag.String("all-tests-args", "", "Specifies space-separated arguments to pass to all_tests.go")
37 runnerArgs = flag.String("runner-args", "", "Specifies space-separated arguments to pass to ssl/test/runner")
David Benjamin8de8b3d2016-05-12 23:07:47 -040038 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
David Benjaminf9459522016-03-07 15:30:26 -050039)
40
David Benjamin8de8b3d2016-05-12 23:07:47 -040041func enableUnitTests() bool {
42 return *suite == "all" || *suite == "unit"
43}
44
45func enableSSLTests() bool {
46 return *suite == "all" || *suite == "ssl"
47}
48
David Benjaminf9459522016-03-07 15:30:26 -050049func adb(args ...string) error {
50 if len(*device) > 0 {
51 args = append([]string{"-s", *device}, args...)
52 }
David Benjamin00b10692016-05-19 00:13:22 -040053 cmd := exec.Command(*adbPath, args...)
David Benjaminf9459522016-03-07 15:30:26 -050054 cmd.Stdout = os.Stdout
55 cmd.Stderr = os.Stderr
56 return cmd.Run()
57}
58
59func goTool(args ...string) error {
60 cmd := exec.Command("go", args...)
61 cmd.Stdout = os.Stdout
62 cmd.Stderr = os.Stderr
63
64 if *aarch64 {
65 cmd.Env = append(cmd.Env, "GOARCH=arm64")
66 } else {
67 cmd.Env = append(cmd.Env, "GOARCH=arm")
68 cmd.Env = append(cmd.Env, fmt.Sprintf("GOARM=%d", *arm))
69 }
70 return cmd.Run()
71}
72
73// setWorkingDirectory walks up directories as needed until the current working
74// directory is the top of a BoringSSL checkout.
75func setWorkingDirectory() {
76 for i := 0; i < 64; i++ {
77 if _, err := os.Stat("BUILDING.md"); err == nil {
78 return
79 }
80 os.Chdir("..")
81 }
82
83 panic("Couldn't find BUILDING.md in a parent directory!")
84}
85
86type test []string
87
88func parseTestConfig(filename string) ([]test, error) {
89 in, err := os.Open(filename)
90 if err != nil {
91 return nil, err
92 }
93 defer in.Close()
94
95 decoder := json.NewDecoder(in)
96 var result []test
97 if err := decoder.Decode(&result); err != nil {
98 return nil, err
99 }
100 return result, nil
101}
102
103func copyFile(dst, src string) error {
104 srcFile, err := os.Open(src)
105 if err != nil {
106 return err
107 }
108 defer srcFile.Close()
109
110 srcInfo, err := srcFile.Stat()
111 if err != nil {
112 return err
113 }
114
115 dir := filepath.Dir(dst)
116 if err := os.MkdirAll(dir, 0777); err != nil {
117 return err
118 }
119
120 dstFile, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, srcInfo.Mode())
121 if err != nil {
122 return err
123 }
124 defer dstFile.Close()
125
126 _, err = io.Copy(dstFile, srcFile)
127 return err
128}
129
130func main() {
131 flag.Parse()
David Benjaminf9459522016-03-07 15:30:26 -0500132
David Benjamin8de8b3d2016-05-12 23:07:47 -0400133 if *suite == "all" && *jsonOutput != "" {
134 fmt.Printf("To use -json-output flag, select only one test suite with -suite.\n")
David Benjaminf9459522016-03-07 15:30:26 -0500135 os.Exit(1)
136 }
137
David Benjamin8de8b3d2016-05-12 23:07:47 -0400138 setWorkingDirectory()
139
David Benjaminf9459522016-03-07 15:30:26 -0500140 // Clear the target directory.
141 if err := adb("shell", "rm -Rf /data/local/tmp/boringssl-tmp"); err != nil {
142 fmt.Printf("Failed to clear target directory: %s\n", err)
143 os.Exit(1)
144 }
145
146 // Stage everything in a temporary directory.
147 tmpDir, err := ioutil.TempDir("", "boringssl-android")
148 if err != nil {
149 fmt.Printf("Error making temporary directory: %s\n", err)
150 os.Exit(1)
151 }
152 defer os.RemoveAll(tmpDir)
153
David Benjamin8de8b3d2016-05-12 23:07:47 -0400154 var binaries, files []string
155
156 if enableUnitTests() {
157 files = append(files,
158 "util/all_tests.json",
159 "BUILDING.md",
160 )
161
162 tests, err := parseTestConfig("util/all_tests.json")
163 if err != nil {
164 fmt.Printf("Failed to parse input: %s\n", err)
165 os.Exit(1)
David Benjaminf9459522016-03-07 15:30:26 -0500166 }
David Benjamin8de8b3d2016-05-12 23:07:47 -0400167
168 seenBinary := make(map[string]struct{})
169 for _, test := range tests {
170 if _, ok := seenBinary[test[0]]; !ok {
171 binaries = append(binaries, test[0])
172 seenBinary[test[0]] = struct{}{}
David Benjaminf9459522016-03-07 15:30:26 -0500173 }
David Benjamin8de8b3d2016-05-12 23:07:47 -0400174 for _, arg := range test[1:] {
175 if strings.Contains(arg, "/") {
176 files = append(files, arg)
177 }
178 }
179 }
180
181 fmt.Printf("Building all_tests...\n")
182 if err := goTool("build", "-o", filepath.Join(tmpDir, "util/all_tests"), "util/all_tests.go"); err != nil {
183 fmt.Printf("Error building all_tests.go: %s\n", err)
184 os.Exit(1)
185 }
186 }
187
188 if enableSSLTests() {
189 binaries = append(binaries, "ssl/test/bssl_shim")
190 files = append(files,
191 "BUILDING.md",
192 "util/all_tests.json",
193 "ssl/test/runner/cert.pem",
194 "ssl/test/runner/channel_id_key.pem",
195 "ssl/test/runner/ecdsa_cert.pem",
196 "ssl/test/runner/ecdsa_key.pem",
197 "ssl/test/runner/key.pem",
198 )
199
200 fmt.Printf("Building runner...\n")
201 if err := goTool("test", "-c", "-o", filepath.Join(tmpDir, "ssl/test/runner/runner"), "./ssl/test/runner/"); err != nil {
202 fmt.Printf("Error building runner: %s\n", err)
203 os.Exit(1)
David Benjaminf9459522016-03-07 15:30:26 -0500204 }
205 }
206
207 fmt.Printf("Copying test binaries...\n")
208 for _, binary := range binaries {
209 if err := copyFile(filepath.Join(tmpDir, "build", binary), filepath.Join(*buildDir, binary)); err != nil {
210 fmt.Printf("Failed to copy %s: %s\n", binary, err)
211 os.Exit(1)
212 }
213 }
214
215 fmt.Printf("Copying data files...\n")
216 for _, file := range files {
217 if err := copyFile(filepath.Join(tmpDir, file), file); err != nil {
218 fmt.Printf("Failed to copy %s: %s\n", file, err)
219 os.Exit(1)
220 }
221 }
222
David Benjaminf9459522016-03-07 15:30:26 -0500223 fmt.Printf("Uploading files...\n")
224 if err := adb("push", "-p", tmpDir, "/data/local/tmp/boringssl-tmp"); err != nil {
225 fmt.Printf("Failed to push runner: %s\n", err)
226 os.Exit(1)
227 }
228
David Benjamin8de8b3d2016-05-12 23:07:47 -0400229 if enableUnitTests() {
230 fmt.Printf("Running unit tests...\n")
231 if err := adb("shell", fmt.Sprintf("cd /data/local/tmp/boringssl-tmp && ./util/all_tests -json-output results.json %s", *allTestsArgs)); err != nil {
232 fmt.Printf("Failed to run unit tests: %s\n", err)
233 os.Exit(1)
234 }
David Benjaminf9459522016-03-07 15:30:26 -0500235 }
236
David Benjamin8de8b3d2016-05-12 23:07:47 -0400237 if enableSSLTests() {
238 fmt.Printf("Running SSL tests...\n")
239 if err := adb("shell", fmt.Sprintf("cd /data/local/tmp/boringssl-tmp/ssl/test/runner && ./runner -json-output ../../../results.json %s", *runnerArgs)); err != nil {
240 fmt.Printf("Failed to run SSL tests: %s\n", err)
241 os.Exit(1)
242 }
243 }
244
245 if *jsonOutput != "" {
246 if err := adb("pull", "-p", "/data/local/tmp/boringssl-tmp/results.json", *jsonOutput); err != nil {
247 fmt.Printf("Failed to extract results.json: %s\n", err)
248 os.Exit(1)
249 }
David Benjaminf9459522016-03-07 15:30:26 -0500250 }
251}