|
7 | 7 | package main
|
8 | 8 |
|
9 | 9 | import (
|
10 |
| - "bytes" |
11 | 10 | "cmd/internal/buildid"
|
12 | 11 | "cmd/internal/hash"
|
13 | 12 | "cmd/link/internal/ld"
|
@@ -203,36 +202,51 @@ func TestMinusRSymsWithSameName(t *testing.T) {
|
203 | 202 | }
|
204 | 203 | }
|
205 | 204 |
|
206 |
| -func TestGNUBuildIDDerivedFromGoBuildID(t *testing.T) { |
| 205 | +func TestGNUBuildID(t *testing.T) { |
207 | 206 | testenv.MustHaveGoBuild(t)
|
208 | 207 |
|
209 | 208 | t.Parallel()
|
210 | 209 |
|
211 |
| - goFile := filepath.Join(t.TempDir(), "notes.go") |
| 210 | + tmpdir := t.TempDir() |
| 211 | + goFile := filepath.Join(tmpdir, "notes.go") |
212 | 212 | if err := os.WriteFile(goFile, []byte(goSource), 0444); err != nil {
|
213 | 213 | t.Fatal(err)
|
214 | 214 | }
|
215 |
| - outFile := filepath.Join(t.TempDir(), "notes.exe") |
216 |
| - goTool := testenv.GoToolPath(t) |
217 | 215 |
|
218 |
| - cmd := testenv.Command(t, goTool, "build", "-o", outFile, "-ldflags", "-buildid 0x1234 -B gobuildid", goFile) |
219 |
| - cmd.Dir = t.TempDir() |
| 216 | + // Use a specific Go buildid for testing. |
| 217 | + const gobuildid = "testbuildid" |
| 218 | + h := hash.Sum32([]byte(gobuildid)) |
| 219 | + gobuildidHash := string(h[:20]) |
220 | 220 |
|
221 |
| - out, err := cmd.CombinedOutput() |
222 |
| - if err != nil { |
223 |
| - t.Logf("%s", out) |
224 |
| - t.Fatal(err) |
| 221 | + tests := []struct{ name, ldflags, expect string }{ |
| 222 | + {"default", "", gobuildidHash}, |
| 223 | + {"gobuildid", "-B=gobuildid", gobuildidHash}, |
| 224 | + {"specific", "-B=0x0123456789abcdef", "\x01\x23\x45\x67\x89\xab\xcd\xef"}, |
| 225 | + {"none", "-B=none", ""}, |
225 | 226 | }
|
226 |
| - |
227 |
| - expectedGoBuildID := hash.Sum32([]byte("0x1234")) |
228 |
| - |
229 |
| - gnuBuildID, err := buildid.ReadELFNote(outFile, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG) |
230 |
| - if err != nil || gnuBuildID == nil { |
231 |
| - t.Fatalf("can't read GNU build ID") |
| 227 | + if testenv.HasCGO() { |
| 228 | + for _, test := range tests { |
| 229 | + t1 := test |
| 230 | + t1.name += "_external" |
| 231 | + t1.ldflags += " -linkmode=external" |
| 232 | + tests = append(tests, t1) |
| 233 | + } |
232 | 234 | }
|
233 |
| - |
234 |
| - if !bytes.Equal(gnuBuildID, expectedGoBuildID[:20]) { |
235 |
| - t.Fatalf("build id not matching") |
| 235 | + for _, test := range tests { |
| 236 | + t.Run(test.name, func(t *testing.T) { |
| 237 | + exe := filepath.Join(tmpdir, test.name) |
| 238 | + cmd := testenv.Command(t, testenv.GoToolPath(t), "build", "-ldflags=-buildid="+gobuildid+" "+test.ldflags, "-o", exe, goFile) |
| 239 | + if out, err := cmd.CombinedOutput(); err != nil { |
| 240 | + t.Fatalf("%v: %v:\n%s", cmd.Args, err, out) |
| 241 | + } |
| 242 | + gnuBuildID, err := buildid.ReadELFNote(exe, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG) |
| 243 | + if err != nil { |
| 244 | + t.Fatalf("can't read GNU build ID") |
| 245 | + } |
| 246 | + if string(gnuBuildID) != test.expect { |
| 247 | + t.Errorf("build id mismatch: got %x, want %x", gnuBuildID, test.expect) |
| 248 | + } |
| 249 | + }) |
236 | 250 | }
|
237 | 251 | }
|
238 | 252 |
|
|
0 commit comments