Skip to content

Commit e74b224

Browse files
committed
crypto/tls: streamline BoGo testing w/ -bogo-local-dir
If -bogo-local-dir is provided but doesn't exist, populate it with a git checkout of the BoringSSL repo at the correct SHA. Without any -bogo-local-dir argument the BoGo TLS handshake test will fetch the BoringSSL source at a specific SHA as a Go module in a r/o module directory. When debugging, or extending BoGo coverage, it's preferable to have a mutable local copy of BoGo that the test will use. The pre-existing -bogo-local-dir flag offered a way to use a checkout of BoGo but it relied on the user fetching the correct repo & revision manually ahead of time. This commit extends the test to automatically invoke `git` to clone the repo into the provided local dir at the correct SHA based on the boringsslModVer const if the local dir doesn't exist. This leaves the user ready to make changes in local BoGo dir to aid debugging, or to upstream as CRs to BoringSSL, and prevents using an incorrect SHA by mistake. Updates golang#72006 Change-Id: I0451a3d35203878cdf02a7587e138c3cd60d15a9 Reviewed-on: https://go-review.googlesource.com/c/go/+/687475 Reviewed-by: Roland Shoemaker <[email protected]> Reviewed-by: Carlos Amedee <[email protected]> TryBot-Bypass: Daniel McCarney <[email protected]>
1 parent 3a05e7b commit e74b224

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/crypto/tls/bogo_shim_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"encoding/base64"
1212
"encoding/json"
1313
"encoding/pem"
14+
"errors"
1415
"flag"
1516
"fmt"
1617
"html/template"
@@ -541,6 +542,7 @@ func orderlyShutdown(tlsConn *Conn) {
541542
}
542543

543544
func TestBogoSuite(t *testing.T) {
545+
testenv.MustHaveGoBuild(t)
544546
if testing.Short() {
545547
t.Skip("skipping in short mode")
546548
}
@@ -559,6 +561,7 @@ func TestBogoSuite(t *testing.T) {
559561

560562
var bogoDir string
561563
if *bogoLocalDir != "" {
564+
ensureLocalBogo(t, *bogoLocalDir)
562565
bogoDir = *bogoLocalDir
563566
} else {
564567
bogoDir = cryptotest.FetchModule(t, "boringssl.googlesource.com/boringssl.git", boringsslModVer)
@@ -664,6 +667,49 @@ func TestBogoSuite(t *testing.T) {
664667
}
665668
}
666669

670+
// ensureLocalBogo fetches BoringSSL to localBogoDir at the correct revision
671+
// (from boringsslModVer) if localBogoDir doesn't already exist.
672+
//
673+
// If localBogoDir does exist, ensureLocalBogo fails the test if it isn't
674+
// a directory.
675+
func ensureLocalBogo(t *testing.T, localBogoDir string) {
676+
t.Helper()
677+
678+
if stat, err := os.Stat(localBogoDir); err == nil {
679+
if !stat.IsDir() {
680+
t.Fatalf("local bogo dir (%q) exists but is not a directory", localBogoDir)
681+
}
682+
683+
t.Logf("using local bogo checkout from %q", localBogoDir)
684+
return
685+
} else if !errors.Is(err, os.ErrNotExist) {
686+
t.Fatalf("failed to stat local bogo dir (%q): %v", localBogoDir, err)
687+
}
688+
689+
testenv.MustHaveExecPath(t, "git")
690+
691+
idx := strings.LastIndex(boringsslModVer, "-")
692+
if idx == -1 || idx == len(boringsslModVer)-1 {
693+
t.Fatalf("invalid boringsslModVer format: %q", boringsslModVer)
694+
}
695+
commitSHA := boringsslModVer[idx+1:]
696+
697+
t.Logf("cloning boringssl@%s to %q", commitSHA, localBogoDir)
698+
cloneCmd := testenv.Command(t, "git", "clone", "--no-checkout", "https://boringssl.googlesource.com/boringssl", localBogoDir)
699+
if err := cloneCmd.Run(); err != nil {
700+
t.Fatalf("git clone failed: %v", err)
701+
}
702+
703+
checkoutCmd := testenv.Command(t, "git", "checkout", commitSHA)
704+
checkoutCmd.Dir = localBogoDir
705+
if err := checkoutCmd.Run(); err != nil {
706+
t.Fatalf("git checkout failed: %v", err)
707+
}
708+
709+
t.Logf("using fresh local bogo checkout from %q", localBogoDir)
710+
return
711+
}
712+
667713
func generateReport(results bogoResults, outPath string) error {
668714
data := reportData{
669715
Results: results,

src/crypto/tls/handshake_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ var (
4646
keyFile = flag.String("keylog", "", "destination file for KeyLogWriter")
4747
bogoMode = flag.Bool("bogo-mode", false, "Enabled bogo shim mode, ignore everything else")
4848
bogoFilter = flag.String("bogo-filter", "", "BoGo test filter")
49-
bogoLocalDir = flag.String("bogo-local-dir", "", "Local BoGo to use, instead of fetching from source")
50-
bogoReport = flag.String("bogo-html-report", "", "File path to render an HTML report with BoGo results")
49+
bogoLocalDir = flag.String("bogo-local-dir", "",
50+
"If not-present, checkout BoGo into this dir, or otherwise use it as a pre-existing checkout")
51+
bogoReport = flag.String("bogo-html-report", "", "File path to render an HTML report with BoGo results")
5152
)
5253

5354
func runTestAndUpdateIfNeeded(t *testing.T, name string, run func(t *testing.T, update bool), wait bool) {

0 commit comments

Comments
 (0)