aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2018-04-30 22:55:05 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2018-05-01 15:55:49 +0000
commit6a8cff57309ba3168844640b2da2ea3103d594fd (patch)
treebcac7dca3c1dac904c1c9a4e49892d85dd7c6c74 /src
parentd46980995be7a88713b27f829fb9f2cd9b307fa7 (diff)
downloadgo-6a8cff57309ba3168844640b2da2ea3103d594fd.tar.xz
os: fix missing break bug in earlier CL 110295's use of Uname
The Uname name was never being used because it always generated a too-long string. The new test looking for zero bytes wouldn't have caught it (I thought it would've), but is still nice to have. Updates #24701 Change-Id: I2648074452609e4ad1b9736973e1b3a95eac658d Reviewed-on: https://go-review.googlesource.com/110436 Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src')
-rw-r--r--src/os/os_test.go35
-rw-r--r--src/os/sys_linux.go1
2 files changed, 16 insertions, 20 deletions
diff --git a/src/os/os_test.go b/src/os/os_test.go
index af3a2fbee6..9d13fe05ac 100644
--- a/src/os/os_test.go
+++ b/src/os/os_test.go
@@ -1523,11 +1523,7 @@ func runBinHostname(t *testing.T) string {
return output
}
-func testWindowsHostname(t *testing.T) {
- hostname, err := Hostname()
- if err != nil {
- t.Fatal(err)
- }
+func testWindowsHostname(t *testing.T, hostname string) {
cmd := osexec.Command("hostname")
out, err := cmd.CombinedOutput()
if err != nil {
@@ -1535,27 +1531,30 @@ func testWindowsHostname(t *testing.T) {
}
want := strings.Trim(string(out), "\r\n")
if hostname != want {
- t.Fatalf("Hostname() = %q, want %q", hostname, want)
+ t.Fatalf("Hostname() = %q != system hostname of %q", hostname, want)
}
}
func TestHostname(t *testing.T) {
+ hostname, err := Hostname()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if hostname == "" {
+ t.Fatal("Hostname returned empty string and no error")
+ }
+ if strings.Contains(hostname, "\x00") {
+ t.Fatalf("unexpected zero byte in hostname: %q", hostname)
+ }
+
// There is no other way to fetch hostname on windows, but via winapi.
// On Plan 9 it can be taken from #c/sysname as Hostname() does.
switch runtime.GOOS {
case "android", "plan9":
- // No /bin/hostname to verify against, but at least
- // verify we get something back from Hostname.
- hostname, err := Hostname()
- if err != nil {
- t.Fatal(err)
- }
- if hostname == "" {
- t.Fatal("Hostname returned empty string and no error")
- }
+ // No /bin/hostname to verify against.
return
case "windows":
- testWindowsHostname(t)
+ testWindowsHostname(t, hostname)
return
}
@@ -1564,10 +1563,6 @@ func TestHostname(t *testing.T) {
// Check internal Hostname() against the output of /bin/hostname.
// Allow that the internal Hostname returns a Fully Qualified Domain Name
// and the /bin/hostname only returns the first component
- hostname, err := Hostname()
- if err != nil {
- t.Fatalf("%v", err)
- }
want := runBinHostname(t)
if hostname != want {
i := strings.Index(hostname, ".")
diff --git a/src/os/sys_linux.go b/src/os/sys_linux.go
index 3fbe5e9f82..467127653a 100644
--- a/src/os/sys_linux.go
+++ b/src/os/sys_linux.go
@@ -22,6 +22,7 @@ func hostname() (name string, err error) {
buf[i] = uint8(b)
if b == 0 {
name = string(buf[:i])
+ break
}
}
// If we got a name and it's not potentially truncated