System.Uri and IPv6 with embedded IPv4

  • Thread starter Thread starter William Randlett
  • Start date Start date
W

William Randlett

The System.Uri class is not behaving as I expect when an IPv4 address is
embedded within an IPv6 address. The following test works fine, using IPv6
only:
[Test]
public void Uri_ShouldCreateValidUriFromSimpleIPv6Address()
{
Uri testUri = new Uri("http://[::1]:9050");
Assert.AreEqual(
"http://[0000:0000:0000:0000:0000:0000:0000:0001]:9050/",
testUri.ToString());
}

However, when I embed an IPv4 address, the Uri class begins converting the
address in a way that I do not expect:
public void Uri_ShouldCreateValidUriFromIPv4AddressEmbeddedWithinIPv6Address()
[Test]
{
Uri testUri = new Uri("http://[::101.45.75.219]:9050");
Assert.AreEqual(
"http://[0000:0000:0000:0000:0000:0000:101.45.75.219]:9050/",
testUri.ToString());
}
TestCase 'Uri_ShouldCreateValidUriFromIPv4AddressEmbeddedWithinIPv6Address'
failed:
String lengths differ. Expected length=58, but was length=54.
Strings differ at index 38.
expected: <"...p://[0000:0000:0000:0000:0000:0000:101.45.75.219]:9050/">
but was: <"...p://[0000:0000:0000:0000:0000:0000:652D:4BDB]:9050/">
--------------------------------------------------^

Am I missing something here?
 
Back
Top