aboutsummaryrefslogtreecommitdiff
path: root/src/debug
diff options
context:
space:
mode:
authorMichael Pratt <mpratt@google.com>2024-07-26 12:05:40 -0400
committerGopher Robot <gobot@golang.org>2024-07-29 15:32:56 +0000
commit28aed4015ee7ba85b29007d685de9150e01a9acd (patch)
tree7673a215333b7ef9d3f58734402cc2edae254d40 /src/debug
parentd531e344aea38f165a9d13d1e4173816fec35050 (diff)
downloadgo-28aed4015ee7ba85b29007d685de9150e01a9acd.tar.xz
debug/buildinfo: add old-Go and not-Go tests
There is currently no coverage for the pre-1.18 buildinfo format, or for parsing non-Go binaries. Add basic tests for each of these. Updates #68592. Change-Id: Iec14d29ffc1392e46f592c0c7bebf2eb75f7d0d3 Reviewed-on: https://go-review.googlesource.com/c/go/+/601457 Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Pratt <mpratt@google.com>
Diffstat (limited to 'src/debug')
-rw-r--r--src/debug/buildinfo/buildinfo_test.go56
-rwxr-xr-xsrc/debug/buildinfo/testdata/go117bin0 -> 1166278 bytes
-rwxr-xr-xsrc/debug/buildinfo/testdata/notgobin0 -> 15832 bytes
3 files changed, 56 insertions, 0 deletions
diff --git a/src/debug/buildinfo/buildinfo_test.go b/src/debug/buildinfo/buildinfo_test.go
index ea7228c300..9f9756a22c 100644
--- a/src/debug/buildinfo/buildinfo_test.go
+++ b/src/debug/buildinfo/buildinfo_test.go
@@ -248,6 +248,62 @@ func TestReadFile(t *testing.T) {
}
}
+// Test117 verifies that parsing of the old, pre-1.18 format works.
+func Test117(t *testing.T) {
+ // go117 was generated for linux-amd64 with:
+ //
+ // main.go:
+ //
+ // package main
+ // func main() {}
+ //
+ // GOTOOLCHAIN=go1.17 go mod init example.com/go117
+ // GOTOOLCHAIN=go1.17 go build
+ //
+ // TODO(prattmic): Ideally this would be built on the fly to better
+ // cover all executable formats, but then we need a network connection
+ // to download an old Go toolchain.
+ info, err := buildinfo.ReadFile("testdata/go117")
+ if err != nil {
+ t.Fatalf("ReadFile got err %v, want nil", err)
+ }
+
+ if info.GoVersion != "go1.17" {
+ t.Errorf("GoVersion got %s want go1.17", info.GoVersion)
+ }
+ if info.Path != "example.com/go117" {
+ t.Errorf("Path got %s want example.com/go117", info.Path)
+ }
+ if info.Main.Path != "example.com/go117" {
+ t.Errorf("Main.Path got %s want example.com/go117", info.Main.Path)
+ }
+}
+
+// TestNotGo verifies that parsing of a non-Go binary returns the proper error.
+func TestNotGo(t *testing.T) {
+ // notgo was generated for linux-amd64 with:
+ //
+ // main.c:
+ //
+ // int main(void) { return 0; }
+ //
+ // cc -o notgo main.c
+ //
+ // TODO(prattmic): Ideally this would be built on the fly to better
+ // cover all executable formats, but then we need to encode the
+ // intricacies of calling each platform's C compiler.
+ _, err := buildinfo.ReadFile("testdata/notgo")
+ if err == nil {
+ t.Fatalf("ReadFile got nil err, want non-nil")
+ }
+
+ // The precise error text here isn't critical, but we want something
+ // like errNotGoExe rather than e.g., a file read error.
+ if !strings.Contains(err.Error(), "not a Go executable") {
+ t.Errorf("ReadFile got err %v want not a Go executable", err)
+ }
+}
+
// FuzzIssue57002 is a regression test for golang.org/issue/57002.
//
// The cause of issue 57002 is when pointerSize is not being checked,
diff --git a/src/debug/buildinfo/testdata/go117 b/src/debug/buildinfo/testdata/go117
new file mode 100755
index 0000000000..d7acbeef28
--- /dev/null
+++ b/src/debug/buildinfo/testdata/go117
Binary files differ
diff --git a/src/debug/buildinfo/testdata/notgo b/src/debug/buildinfo/testdata/notgo
new file mode 100755
index 0000000000..bc19ec2d3e
--- /dev/null
+++ b/src/debug/buildinfo/testdata/notgo
Binary files differ