Skip to content
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

Merged
merged 8 commits into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Copy link
Member

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.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is somewhat surprising that GetHash or equality would need new state.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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.
Expand Down Expand Up @@ -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);
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 NotEqual assertions.

}

[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;
Expand Down