Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,10 @@
<data name="PlatformNotSupported_NetPrimitives" xml:space="preserve">
<value>System.Net.Primitives is not supported on this platform.</value>
</data>
<data name="net_SocketException_OperationNotSupported" xml:space="preserve">
<value>The requested property is not supported for the '{0}' AddressFamily.</value>
</data>
<data name="net_SocketException_OperationNotSupported_ReadOnlyIPAddress" xml:space="preserve">
<value>This operation is not supported on a read-only IPAddress instance.</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ public long ScopeId

if (this is ReadOnlyIPAddress)
{
ThrowSocketOperationNotSupported();
ThrowSocketOperationNotSupportedReadOnly();
}

// Consider: Since scope is only valid for link-local and site-local
Expand Down Expand Up @@ -668,7 +668,7 @@ public long Address
{
if (this is ReadOnlyIPAddress)
{
ThrowSocketOperationNotSupported();
ThrowSocketOperationNotSupportedReadOnly();
}

PrivateAddress = unchecked((uint)value);
Expand Down Expand Up @@ -769,7 +769,10 @@ public IPAddress MapToIPv4()
private static byte[] ThrowAddressNullException() => throw new ArgumentNullException("address");

[DoesNotReturn]
private static void ThrowSocketOperationNotSupported() => throw new SocketException(SocketError.OperationNotSupported);
private void ThrowSocketOperationNotSupported() => throw new SocketException(SocketError.OperationNotSupported, SR.Format(SR.net_SocketException_OperationNotSupported, AddressFamily));

[DoesNotReturn]
private static void ThrowSocketOperationNotSupportedReadOnly() => throw new SocketException(SocketError.OperationNotSupported, SR.net_SocketException_OperationNotSupported_ReadOnlyIPAddress);

private sealed class ReadOnlyIPAddress : IPAddress
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static IEnumerable<object[]> GetValidIPAddresses()
{
return IPAddressParsingFormatting.ValidIpv4Addresses
.Concat(IPAddressParsingFormatting.ValidIpv6Addresses)
.Select(array => new object[] {IPAddress.Parse((string)array[0])});
.Select(array => new object[] { IPAddress.Parse((string)array[0]) });
}

public static readonly object[][] GeneratedIPAddresses =
Expand Down Expand Up @@ -351,6 +351,34 @@ public static void Address_ReadOnlyStatics_Set_Failure()
Assert.Throws<SocketException>(() => IPAddress.IPv6Loopback.ScopeId = 1);
Assert.Throws<SocketException>(() => IPAddress.IPv6None.ScopeId = 1);
}

[Fact]
public static void ScopeId_Throws_SocketException_IPv4Message()
{
IPAddress ip = IPV4Address1();
SocketException ex = Assert.Throws<SocketException>(() => _ = ip.ScopeId);
Assert.Equal(SocketError.OperationNotSupported, ex.SocketErrorCode);
Assert.Contains($"'{ip.AddressFamily}' {nameof(AddressFamily)}.", ex.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public static void Address_Throws_SocketException_IPv6Message()
{
IPAddress ip = IPV6Address1();
SocketException ex = Assert.Throws<SocketException>(() => _ = ip.Address);
Assert.Equal(SocketError.OperationNotSupported, ex.SocketErrorCode);
Assert.Contains($"'{ip.AddressFamily}' {nameof(AddressFamily)}.", ex.Message, StringComparison.OrdinalIgnoreCase);
}

[Fact]
public static void Address_Throws_SocketException_ReadOnlyMessage()
{
IPAddress ip1 = IPAddress.Loopback; // This is readonly
IPAddress ip2 = IPV4Address1();
SocketException ex = Assert.Throws<SocketException>(() => ip1.Address = ip2.Address);
Assert.Equal(SocketError.OperationNotSupported, ex.SocketErrorCode);
Assert.Contains("read-only", ex.Message, StringComparison.OrdinalIgnoreCase);
}
#pragma warning restore 618
}
}
Loading