-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Equals/GetHashCode for UnixDomainSocketEndPoint #69722
Changes from 4 commits
afc0431
b308d73
90ddb6f
1d1b9c2
0fde07a
7e3d6ee
843405e
6b25d4f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Text; | ||
using System.IO; | ||
|
||
|
@@ -10,13 +11,38 @@ namespace System.Net.Sockets | |
/// <summary>Represents a Unix Domain Socket endpoint as a path.</summary> | ||
public sealed partial class UnixDomainSocketEndPoint : EndPoint | ||
{ | ||
// taken from System.IO.PathInternal | ||
private static readonly StringComparison s_filePathComparison = OperatingSystem.IsWindows() || | ||
OperatingSystem.IsMacOS() || | ||
OperatingSystem.IsIOS() || | ||
OperatingSystem.IsTvOS() || | ||
OperatingSystem.IsWatchOS() | ||
? StringComparison.OrdinalIgnoreCase | ||
: StringComparison.Ordinal; | ||
|
||
private const AddressFamily EndPointAddressFamily = AddressFamily.Unix; | ||
|
||
private readonly string _path; | ||
private readonly byte[] _encodedPath; | ||
|
||
// The field is needed to distinguish the situation when | ||
// _fullPath is not null but the object doesn't bound | ||
// to the full path. _fullPath can be initialized by Equals/GetHashCode | ||
// but it doesn't mean that the EndPoint is bound to the file system object. | ||
private readonly bool _isBound; | ||
|
||
// The field can be initialized lazily in the following circumstances: | ||
// 1. Inside of Equals method | ||
// 2. Inside of GetHashCode method | ||
// 3. Inside of CreateBoundEndPoint method | ||
// In case of non-abstract path, we need to have full path to the file system object. | ||
// Otherwise, two endpoints may not be equal even if they pointing to the same file. | ||
// Lazily initialized field then can be reused by these methods to avoid further | ||
// allocations. | ||
private string? _fullPath; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is somewhat surprising that GetHash or equality would need new state. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not actually new, the value of this field is used for two purposes. One of them is Equals/GetHashCode, yes. Otherwise, two paths pointing to the same location in fs will give different hash codes for EndPoint. However, we can assume that relative paths pointing to the same location give us different EndPoints. If so, I can change the behavior. |
||
|
||
// Tracks the file Socket should delete on Dispose. | ||
internal string? BoundFileName { get; } | ||
internal string? BoundFileName => _isBound ? _fullPath : null; | ||
|
||
public UnixDomainSocketEndPoint(string path) | ||
: this(path, null) | ||
|
@@ -26,7 +52,8 @@ private UnixDomainSocketEndPoint(string path, string? boundFileName) | |
{ | ||
ArgumentNullException.ThrowIfNull(path); | ||
|
||
BoundFileName = boundFileName; | ||
_isBound = boundFileName is not null; | ||
_fullPath = boundFileName; | ||
|
||
// Pathname socket addresses should be null-terminated. | ||
// Linux abstract socket addresses start with a zero byte, they must not be null-terminated. | ||
|
@@ -124,21 +151,61 @@ public override string ToString() | |
} | ||
} | ||
|
||
[MemberNotNull(nameof(_fullPath))] | ||
private void EnsurePathNormalized() | ||
{ | ||
Debug.Assert(!IsAbstract(_path)); | ||
|
||
_fullPath ??= Path.GetFullPath(_path); | ||
} | ||
|
||
public override bool Equals([NotNullWhen(true)] object? obj) | ||
{ | ||
if (obj is not UnixDomainSocketEndPoint uds) | ||
return false; | ||
|
||
switch ((IsAbstract(_path), IsAbstract(uds._path))) | ||
{ | ||
case (true, true): | ||
// abstract paths are case-sensitive on Windows and Unix-based systems | ||
return MemoryExtensions.Equals(_path.AsSpan(1), uds._path.AsSpan(1), StringComparison.Ordinal); | ||
rzikm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case (false, false): | ||
EnsurePathNormalized(); | ||
uds.EnsurePathNormalized(); | ||
return MemoryExtensions.Equals(_fullPath, uds._fullPath, s_filePathComparison); | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
// abstract paths are case-sensitive on Windows and Unix-based systems | ||
if (IsAbstract(_path)) | ||
return string.GetHashCode(_path.AsSpan(1), StringComparison.Ordinal); | ||
|
||
EnsurePathNormalized(); | ||
return string.GetHashCode(_fullPath, s_filePathComparison); | ||
} | ||
|
||
internal UnixDomainSocketEndPoint CreateBoundEndPoint() | ||
{ | ||
if (IsAbstract(_path)) | ||
{ | ||
return this; | ||
} | ||
return new UnixDomainSocketEndPoint(_path, Path.GetFullPath(_path)); | ||
|
||
EnsurePathNormalized(); | ||
return new UnixDomainSocketEndPoint(_path, _fullPath); | ||
} | ||
|
||
internal UnixDomainSocketEndPoint CreateUnboundEndPoint() | ||
{ | ||
if (IsAbstract(_path) || BoundFileName is null) | ||
if (IsAbstract(_path) || !_isBound) | ||
{ | ||
return this; | ||
} | ||
|
||
return new UnixDomainSocketEndPoint(_path, null); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -518,6 +518,43 @@ public void UnixDomainSocketEndPoint_RelativePathDeletesFile() | |
}).Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void AbstractPathEquality() | ||
{ | ||
string abstractPath = '\0' + Guid.NewGuid().ToString(); | ||
UnixDomainSocketEndPoint endPoint1 = new(abstractPath); | ||
UnixDomainSocketEndPoint endPoint2 = new(abstractPath); | ||
UnixDomainSocketEndPoint endPoint3 = new('\0' + Guid.NewGuid().ToString()); | ||
|
||
Assert.Equal(endPoint1, endPoint2); | ||
Assert.Equal(endPoint1.GetHashCode(), endPoint2.GetHashCode()); | ||
|
||
Assert.NotEqual(endPoint1, endPoint3); | ||
Assert.NotEqual(endPoint2, endPoint3); | ||
Assert.NotEqual(endPoint1.GetHashCode(), endPoint3.GetHashCode()); | ||
Assert.NotEqual(endPoint2.GetHashCode(), endPoint3.GetHashCode()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These hashcode NotEqual asserts could sporadically fail. It should be exceedingly rare, but better to remove them. We could instead assert that the endpoint hashcode equals the path hashcode, and leave the correctness of the path hashcode up to string's implementation. Same for the test below. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of hash code collision.. hmm, makes sense. I would prefer just to remove |
||
} | ||
|
||
[Fact] | ||
public void FilePathEquality() | ||
{ | ||
string path1 = "filename"; | ||
string path2 = "." + Path.DirectorySeparatorChar + "filename"; | ||
string path3 = GetRandomNonExistingFilePath(); | ||
|
||
UnixDomainSocketEndPoint endPoint1 = new(path1); | ||
UnixDomainSocketEndPoint endPoint2 = new(path2); | ||
UnixDomainSocketEndPoint endPoint3 = new(path3); | ||
|
||
Assert.Equal(endPoint1, endPoint2); | ||
Assert.Equal(endPoint1.GetHashCode(), endPoint2.GetHashCode()); | ||
|
||
Assert.NotEqual(endPoint1, endPoint3); | ||
Assert.NotEqual(endPoint2, endPoint3); | ||
Assert.NotEqual(endPoint1.GetHashCode(), endPoint3.GetHashCode()); | ||
Assert.NotEqual(endPoint2.GetHashCode(), endPoint3.GetHashCode()); | ||
} | ||
|
||
private static string GetRandomNonExistingFilePath() | ||
{ | ||
string result; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this may be copy this is only best guess. Aside from Windows, the case (in)sensitivity is property of the file system e.g. while apfs is case insensitive by default, you can have case sensitive version as well. Similarly on Linux, ext2fs is case sensitive but you can have different filesystems (like vfat) that are not.