Skip to content

Commit

Permalink
Fix IPGlobalProperties.DomainName to return string.Empty when domain …
Browse files Browse the repository at this point in the history
…name is not set (#107241)

* Fix IPGlobalProperties.DomainName to return string.Empty when domain name is not set

Fixes #104882

Fix `IPGlobalProperties.DomainName` to return `string.Empty` instead of `"(none)"` on Linux.

* **HostInformationPal.Unix.cs**
  - Check if the domain name is `"(none)"` in `GetDomainName()`.
  - Return `string.Empty` if the domain name is `"(none)"`.

* **Interop.GetDomainName.cs**
  - Handle the case where the domain name is not set in `GetDomainName()`.
  - Return `string.Empty` if the domain name is `"(none)"`.

* **IPGlobalPropertiesTest.cs**
  - Add a test to verify that `IPGlobalProperties.DomainName` returns `string.Empty` when the domain name is not set.

* Update Interop.GetDomainName.cs

* Update HostInformationPal.Unix.cs

* Update IPGlobalPropertiesTest.cs

* Update HostInformationPal.Unix.cs

---------

Co-authored-by: wfurt <[email protected]>
  • Loading branch information
hootanht and wfurt authored Oct 22, 2024
1 parent 2e9e431 commit e27438a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ internal static unsafe string GetDomainName()
throw new InvalidOperationException($"{nameof(GetDomainName)}: {err}");
}

return Marshal.PtrToStringUTF8((IntPtr)name)!;
string domainName = Marshal.PtrToStringUTF8((IntPtr)name)!;
if (domainName == "(none)")
{
return string.Empty;
}

return domainName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,12 @@ public async Task GetUnicastAddresses_NotEmpty()
Assert.NotEmpty(await props.GetUnicastAddressesAsync());
Assert.NotEmpty(await Task.Factory.FromAsync(props.BeginGetUnicastAddresses, props.EndGetUnicastAddresses, null));
}

[Fact]
public void IPGlobalProperties_DomainName_ReturnsEmptyStringWhenNotSet()
{
IPGlobalProperties gp = IPGlobalProperties.GetIPGlobalProperties();
Assert.Equal(string.Empty, gp.DomainName);
}
}
}

0 comments on commit e27438a

Please sign in to comment.