|
7 | 7 | package main
|
8 | 8 |
|
9 | 9 | import (
|
| 10 | + "bytes" |
| 11 | + "cmd/internal/buildid" |
| 12 | + "cmd/internal/notsha256" |
| 13 | + "cmd/link/internal/ld" |
10 | 14 | "debug/elf"
|
11 | 15 | "fmt"
|
12 | 16 | "internal/platform"
|
@@ -199,6 +203,50 @@ func TestMinusRSymsWithSameName(t *testing.T) {
|
199 | 203 | }
|
200 | 204 | }
|
201 | 205 |
|
| 206 | +func TestGNUBuildIDDerivedFromGoBuildID(t *testing.T) { |
| 207 | + testenv.MustHaveGoBuild(t) |
| 208 | + |
| 209 | + switch runtime.GOOS { |
| 210 | + case "linux", "dragonfly", "openbsd", "netbsd", "freebsd": |
| 211 | + default: |
| 212 | + t.Skip("We should only test on elf output.") |
| 213 | + } |
| 214 | + t.Parallel() |
| 215 | + |
| 216 | + goFile := filepath.Join(t.TempDir(), "notes.go") |
| 217 | + if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil { |
| 218 | + t.Fatal(err) |
| 219 | + } |
| 220 | + outFile := filepath.Join(t.TempDir(), "notes.exe") |
| 221 | + goTool := testenv.GoToolPath(t) |
| 222 | + |
| 223 | + cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags", |
| 224 | + "-B actionid", goFile) |
| 225 | + cmd.Dir = t.TempDir() |
| 226 | + |
| 227 | + out, err := cmd.CombinedOutput() |
| 228 | + if err != nil { |
| 229 | + t.Fatal(err) |
| 230 | + } |
| 231 | + t.Logf("%s", out) |
| 232 | + |
| 233 | + goBuildID, err := buildid.ReadFile(outFile) |
| 234 | + if err != nil { |
| 235 | + t.Fatalf("can't read Go build ID") |
| 236 | + } |
| 237 | + actionID := goBuildID[:strings.Index(goBuildID, "/")] |
| 238 | + hashedActionID := notsha256.Sum256([]byte(actionID)) |
| 239 | + |
| 240 | + gnuBuildID, err := buildid.ReadELFNote(outFile, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG) |
| 241 | + if err != nil || gnuBuildID == nil { |
| 242 | + t.Fatalf("can't read GNU build ID") |
| 243 | + } |
| 244 | + |
| 245 | + if !bytes.Equal(gnuBuildID, hashedActionID[:20]) { |
| 246 | + t.Fatalf("build id not matching") |
| 247 | + } |
| 248 | +} |
| 249 | + |
202 | 250 | func TestMergeNoteSections(t *testing.T) {
|
203 | 251 | testenv.MustHaveGoBuild(t)
|
204 | 252 | expected := 1
|
|
0 commit comments