1// run
2
3//go:build !nacl && !js && !wasip1 && !android && gc
4
5// Copyright 2016 The Go Authors. All rights reserved.
6// Use of this source code is governed by a BSD-style
7// license that can be found in the LICENSE file.
8
9package main
10
11import (
12	"bytes"
13	"log"
14	"os/exec"
15	"strings"
16)
17
18func main() {
19	checkLinkOutput("", "-B argument must start with 0x")
20	checkLinkOutput("0", "-B argument must start with 0x")
21	checkLinkOutput("0x", "usage")
22	checkLinkOutput("0x0", "-B argument must have even number of digits")
23	checkLinkOutput("0x00", "usage")
24	checkLinkOutput("0xYZ", "-B argument contains invalid hex digit")
25	checkLinkOutput("0x"+strings.Repeat("00", 32), "usage")
26	checkLinkOutput("0x"+strings.Repeat("00", 33), "-B option too long (max 32 digits)")
27}
28
29func checkLinkOutput(buildid string, message string) {
30	cmd := exec.Command("go", "tool", "link", "-B", buildid)
31	out, err := cmd.CombinedOutput()
32	if err == nil {
33		log.Fatalf("expected cmd/link to fail")
34	}
35
36	firstLine := string(bytes.SplitN(out, []byte("\n"), 2)[0])
37	if strings.HasPrefix(firstLine, "panic") {
38		log.Fatalf("cmd/link panicked:\n%s", out)
39	}
40
41	if !strings.Contains(firstLine, message) {
42		log.Fatalf("cmd/link output did not include expected message %q: %s", message, firstLine)
43	}
44}
45