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 all 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 Down Expand Up @@ -124,12 +125,18 @@ public override string ToString()
}
}

public override bool Equals([NotNullWhen(true)] object? obj)
=> obj is UnixDomainSocketEndPoint ep && _path == ep._path;

public override int GetHashCode() => _path.GetHashCode();

internal UnixDomainSocketEndPoint CreateBoundEndPoint()
{
if (IsAbstract(_path))
{
return this;
}

return new UnixDomainSocketEndPoint(_path, Path.GetFullPath(_path));
}

Expand All @@ -139,6 +146,7 @@ internal UnixDomainSocketEndPoint CreateUnboundEndPoint()
{
return this;
}

return new UnixDomainSocketEndPoint(_path, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,39 @@ public void UnixDomainSocketEndPoint_RelativePathDeletesFile()
}).Dispose();
}

[ConditionalFact(nameof(PlatformSupportsUnixDomainSockets))]
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);
}

[ConditionalFact(nameof(PlatformSupportsUnixDomainSockets))]
public void FilePathEquality()
{
string path1 = "relative" + Path.DirectorySeparatorChar + "path";
string path2 = new(path1); // make a copy to avoid reference equality
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);
}

private static string GetRandomNonExistingFilePath()
{
string result;
Expand Down