Skip to content

Commit 674d359

Browse files
authored
Improve exception messages in IPAddress class for (ReadOnlyIPAddress, AddressFamily IPv4/IPv6) (#118917)
1 parent bd94b14 commit 674d359

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/libraries/System.Net.Primitives/src/Resources/Strings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,10 @@
117117
<data name="PlatformNotSupported_NetPrimitives" xml:space="preserve">
118118
<value>System.Net.Primitives is not supported on this platform.</value>
119119
</data>
120+
<data name="net_SocketException_OperationNotSupported" xml:space="preserve">
121+
<value>The requested property is not supported for the '{0}' AddressFamily.</value>
122+
</data>
123+
<data name="net_SocketException_OperationNotSupported_ReadOnlyIPAddress" xml:space="preserve">
124+
<value>This operation is not supported on a read-only IPAddress instance.</value>
125+
</data>
120126
</root>

src/libraries/System.Net.Primitives/src/System/Net/IPAddress.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ public long ScopeId
438438

439439
if (this is ReadOnlyIPAddress)
440440
{
441-
ThrowSocketOperationNotSupported();
441+
ThrowSocketOperationNotSupportedReadOnly();
442442
}
443443

444444
// Consider: Since scope is only valid for link-local and site-local
@@ -668,7 +668,7 @@ public long Address
668668
{
669669
if (this is ReadOnlyIPAddress)
670670
{
671-
ThrowSocketOperationNotSupported();
671+
ThrowSocketOperationNotSupportedReadOnly();
672672
}
673673

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

771771
[DoesNotReturn]
772-
private static void ThrowSocketOperationNotSupported() => throw new SocketException(SocketError.OperationNotSupported);
772+
private void ThrowSocketOperationNotSupported() => throw new SocketException(SocketError.OperationNotSupported, SR.Format(SR.net_SocketException_OperationNotSupported, AddressFamily));
773+
774+
[DoesNotReturn]
775+
private static void ThrowSocketOperationNotSupportedReadOnly() => throw new SocketException(SocketError.OperationNotSupported, SR.net_SocketException_OperationNotSupported_ReadOnlyIPAddress);
773776

774777
private sealed class ReadOnlyIPAddress : IPAddress
775778
{

src/libraries/System.Net.Primitives/tests/FunctionalTests/IPAddressTest.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ public static IEnumerable<object[]> GetValidIPAddresses()
306306
{
307307
return IPAddressParsingFormatting.ValidIpv4Addresses
308308
.Concat(IPAddressParsingFormatting.ValidIpv6Addresses)
309-
.Select(array => new object[] {IPAddress.Parse((string)array[0])});
309+
.Select(array => new object[] { IPAddress.Parse((string)array[0]) });
310310
}
311311

312312
public static readonly object[][] GeneratedIPAddresses =
@@ -351,6 +351,34 @@ public static void Address_ReadOnlyStatics_Set_Failure()
351351
Assert.Throws<SocketException>(() => IPAddress.IPv6Loopback.ScopeId = 1);
352352
Assert.Throws<SocketException>(() => IPAddress.IPv6None.ScopeId = 1);
353353
}
354+
355+
[Fact]
356+
public static void ScopeId_Throws_SocketException_IPv4Message()
357+
{
358+
IPAddress ip = IPV4Address1();
359+
SocketException ex = Assert.Throws<SocketException>(() => _ = ip.ScopeId);
360+
Assert.Equal(SocketError.OperationNotSupported, ex.SocketErrorCode);
361+
Assert.Contains($"'{ip.AddressFamily}' {nameof(AddressFamily)}.", ex.Message, StringComparison.OrdinalIgnoreCase);
362+
}
363+
364+
[Fact]
365+
public static void Address_Throws_SocketException_IPv6Message()
366+
{
367+
IPAddress ip = IPV6Address1();
368+
SocketException ex = Assert.Throws<SocketException>(() => _ = ip.Address);
369+
Assert.Equal(SocketError.OperationNotSupported, ex.SocketErrorCode);
370+
Assert.Contains($"'{ip.AddressFamily}' {nameof(AddressFamily)}.", ex.Message, StringComparison.OrdinalIgnoreCase);
371+
}
372+
373+
[Fact]
374+
public static void Address_Throws_SocketException_ReadOnlyMessage()
375+
{
376+
IPAddress ip1 = IPAddress.Loopback; // This is readonly
377+
IPAddress ip2 = IPV4Address1();
378+
SocketException ex = Assert.Throws<SocketException>(() => ip1.Address = ip2.Address);
379+
Assert.Equal(SocketError.OperationNotSupported, ex.SocketErrorCode);
380+
Assert.Contains("read-only", ex.Message, StringComparison.OrdinalIgnoreCase);
381+
}
354382
#pragma warning restore 618
355383
}
356384
}

0 commit comments

Comments
 (0)