From a2302934de69c82d1d81453bdbccf28e23031234 Mon Sep 17 00:00:00 2001 From: Tomas Weinfurt Date: Thu, 24 Nov 2022 12:21:24 -0800 Subject: [PATCH] Implement Equals & GetHashCode for LingerOption - fix Socket outerloop tests (#78747) --- .../src/System/Net/Sockets/LingerOption.cs | 7 +++++++ .../src/System/Net/Sockets/Socket.Unix.cs | 14 +++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/LingerOption.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/LingerOption.cs index 17cb04c6db63a..3839aa10c166a 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/LingerOption.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/LingerOption.cs @@ -41,5 +41,12 @@ public int LingerTime _lingerTime = value; } } + + public override bool Equals(object? comparand) + { + return comparand is LingerOption option && option.Enabled == _enabled && option.LingerTime == _lingerTime; + } + + public override int GetHashCode() => HashCode.Combine(_enabled, _lingerTime); } } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Unix.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Unix.cs index 8dba083e1c0ac..70b89071349be 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Unix.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Unix.cs @@ -283,9 +283,17 @@ internal Socket CopyStateFromSource(Socket source) // Try to detect if a property gets added that we're not copying correctly. foreach (PropertyInfo pi in typeof(Socket).GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { - object? origValue = pi.GetValue(source); - object? cloneValue = pi.GetValue(this); - Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue}"); + try + { + object? origValue = pi.GetValue(source); + object? cloneValue = pi.GetValue(this); + + Debug.Assert(Equals(origValue, cloneValue), $"{pi.Name}. Expected: {origValue}, Actual: {cloneValue}"); + } + catch (TargetInvocationException ex) when (ex.InnerException is SocketException se && se.SocketErrorCode == SocketError.OperationNotSupported) + { + // macOS fails to retrieve DontFragment and MulticastLoopback at the moment + } } #endif return this;