Commit 6a8cff57 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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/110436Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent d4698099
...@@ -1523,11 +1523,7 @@ func runBinHostname(t *testing.T) string { ...@@ -1523,11 +1523,7 @@ func runBinHostname(t *testing.T) string {
return output return output
} }
func testWindowsHostname(t *testing.T) { func testWindowsHostname(t *testing.T, hostname string) {
hostname, err := Hostname()
if err != nil {
t.Fatal(err)
}
cmd := osexec.Command("hostname") cmd := osexec.Command("hostname")
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
...@@ -1535,27 +1531,30 @@ func testWindowsHostname(t *testing.T) { ...@@ -1535,27 +1531,30 @@ func testWindowsHostname(t *testing.T) {
} }
want := strings.Trim(string(out), "\r\n") want := strings.Trim(string(out), "\r\n")
if hostname != want { 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) { 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. // 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. // On Plan 9 it can be taken from #c/sysname as Hostname() does.
switch runtime.GOOS { switch runtime.GOOS {
case "android", "plan9": case "android", "plan9":
// No /bin/hostname to verify against, but at least // No /bin/hostname to verify against.
// 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")
}
return return
case "windows": case "windows":
testWindowsHostname(t) testWindowsHostname(t, hostname)
return return
} }
...@@ -1564,10 +1563,6 @@ func TestHostname(t *testing.T) { ...@@ -1564,10 +1563,6 @@ func TestHostname(t *testing.T) {
// Check internal Hostname() against the output of /bin/hostname. // Check internal Hostname() against the output of /bin/hostname.
// Allow that the internal Hostname returns a Fully Qualified Domain Name // Allow that the internal Hostname returns a Fully Qualified Domain Name
// and the /bin/hostname only returns the first component // and the /bin/hostname only returns the first component
hostname, err := Hostname()
if err != nil {
t.Fatalf("%v", err)
}
want := runBinHostname(t) want := runBinHostname(t)
if hostname != want { if hostname != want {
i := strings.Index(hostname, ".") i := strings.Index(hostname, ".")
......
...@@ -22,6 +22,7 @@ func hostname() (name string, err error) { ...@@ -22,6 +22,7 @@ func hostname() (name string, err error) {
buf[i] = uint8(b) buf[i] = uint8(b)
if b == 0 { if b == 0 {
name = string(buf[:i]) name = string(buf[:i])
break
} }
} }
// If we got a name and it's not potentially truncated // If we got a name and it's not potentially truncated
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment