Skip to content
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<Version>3.9.50</Version>
</PackageReference>
<PackageReference Include="SauceControl.InheritDoc" Version="2.0.2" PrivateAssets="all"/>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.203">
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.300">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions PG.Benchmarks/PG.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.8" />
<PackageReference Include="System.Buffers" Version="4.6.1" />
<PackageReference Include="Testably.Abstractions.Testing" Version="6.2.0" />
<PackageReference Include="Testably.Abstractions.Testing" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions PG.Commons/PG.Commons.Test/PG.Commons.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@
<PackageReference Include="GitHubActionsTestLogger" Version="3.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
<PackageReference Include="xunit.v3.mtp-v2" Version="3.2.2" />
<PackageReference Include="Microsoft.Testing.Platform" Version="2.2.2" />
<PackageReference Include="Microsoft.Testing.Platform" Version="2.2.3" />
<PackageReference Include="IsExternalInit" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="10.0.0">
<PackageReference Include="coverlet.msbuild" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="10.0.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
12 changes: 6 additions & 6 deletions PG.Commons/PG.Commons/PG.Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AnakinRaW.CommonUtilities" Version="13.0.18" />
<PackageReference Include="AnakinRaW.CommonUtilities.FileSystem" Version="13.0.18" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.7" />
<PackageReference Include="System.IO.Hashing" Version="10.0.7" />
<PackageReference Include="AnakinRaW.CommonUtilities" Version="13.0.23" />
<PackageReference Include="AnakinRaW.CommonUtilities.FileSystem" Version="13.0.23" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.8" />
<PackageReference Include="System.IO.Hashing" Version="10.0.8" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="6.0.0" Condition="$(TargetFramework) == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Bcl.Memory" Version="10.0.7" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Bcl.Memory" Version="10.0.8" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Nullable" Version="1.3.1" Condition="$(TargetFramework) == 'netstandard2.0'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
<PackageReference Include="GitHubActionsTestLogger" Version="3.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
<PackageReference Include="xunit.v3.mtp-v2" Version="3.2.2" />
<PackageReference Include="Microsoft.Testing.Platform" Version="2.2.2" />
<PackageReference Include="coverlet.msbuild" Version="10.0.0">
<PackageReference Include="Microsoft.Testing.Platform" Version="2.2.3" />
<PackageReference Include="coverlet.msbuild" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="10.0.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Bcl.HashCode" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PG.Commons\PG.Commons\PG.Commons.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void Ctor_InvalidArgs_Throws()
{
Assert.Throws<ArgumentNullException>(() => new MegDataEntryOriginInfo((IFileInfo)null!));
Assert.Throws<ArgumentNullException>(() => new MegDataEntryOriginInfo((MegDataEntryLocationReference)null!));
Assert.Throws<ArgumentNullException>(() => new MegDataEntryOriginInfo((byte[])null!));
}

[Fact]
Expand All @@ -26,9 +27,11 @@ public void Ctor_FileInfo()

Assert.Same(fi, originInfo.FileInfo);
Assert.Null(originInfo.MegFileLocation);
Assert.Null(originInfo.Bytes);

Assert.True(originInfo.IsLocalFile);
Assert.False(originInfo.IsEntryReference);
Assert.False(originInfo.IsBytes);
}

[Fact]
Expand All @@ -44,11 +47,93 @@ public void Ctor_ReferenceLocation()

Assert.Equal(location, originInfo.MegFileLocation);
Assert.Null(originInfo.FileInfo);
Assert.Null(originInfo.Bytes);

Assert.True(originInfo.IsEntryReference);
Assert.False(originInfo.IsLocalFile);
Assert.False(originInfo.IsBytes);
}

#region CTOR_Byte[]

[Fact]
public void Ctor_Bytes()
{
var bytes = new byte[] { 1, 2, 3, 4 };
var originInfo = new MegDataEntryOriginInfo(bytes);

Assert.NotSame(bytes, originInfo.Bytes);
Assert.Equal(bytes, originInfo.Bytes);
Assert.Null(originInfo.FileInfo);
Assert.Null(originInfo.MegFileLocation);

Assert.True(originInfo.IsBytes);
Assert.False(originInfo.IsLocalFile);
Assert.False(originInfo.IsEntryReference);
}

[Fact]
public void Ctor_Bytes_Empty_OK()
{
var bytes = Array.Empty<byte>();
var originInfo = new MegDataEntryOriginInfo(bytes);

Assert.True(originInfo.IsBytes);
Assert.Empty(originInfo.Bytes!);
}

[Fact]
public void Ctor_Bytes_DefensiveCopy()
{
var bytes = new byte[] { 1, 2, 3 };
var originInfo = new MegDataEntryOriginInfo(bytes);

bytes[0] = 99;

Assert.Equal([1, 2, 3], originInfo.Bytes);
}

#endregion

#region CTOR_ReadOnlySpan<byte>

[Fact]
public void Ctor_Bytes_Span()
{
Span<byte> bytes = [1, 2, 3, 4];
var originInfo = new MegDataEntryOriginInfo(bytes);

Assert.Equal(bytes.ToArray(), originInfo.Bytes);
Assert.Null(originInfo.FileInfo);
Assert.Null(originInfo.MegFileLocation);

Assert.True(originInfo.IsBytes);
Assert.False(originInfo.IsLocalFile);
Assert.False(originInfo.IsEntryReference);
}

[Fact]
public void Ctor_Bytes_Span_Empty_OK()
{
var originInfo = new MegDataEntryOriginInfo(ReadOnlySpan<byte>.Empty);

Assert.True(originInfo.IsBytes);
Assert.Empty(originInfo.Bytes!);
}

[Fact]
public void Ctor_Bytes_Span_DefensiveCopy()
{
Span<byte> bytes = [1, 2, 3];
var originInfo = new MegDataEntryOriginInfo(bytes);

bytes[0] = 99;

Assert.Equal([1, 2, 3], originInfo.Bytes);
}

#endregion

[Fact]
public void EqualsHashCode()
{
Expand Down Expand Up @@ -92,4 +177,29 @@ public void EqualsHashCode()
Assert.Equal(originLoc.GetHashCode(), otherOriginLoc.GetHashCode());
Assert.Equal(originPath.GetHashCode(), otherOriginPath.GetHashCode());
}
}

[Fact]
public void EqualsHashCode_Bytes()
{
var bytes = new byte[] { 1, 2, 3 };
var origin = new MegDataEntryOriginInfo(bytes);

// Self-equality.
Assert.Equal(origin, origin);
Assert.Equal(origin, (object)origin);
Assert.False(origin.Equals(null));

// Each ctor call clones the buffer, so two origins from the same input are NOT equal
// (they own distinct internal buffers).
var otherOrigin = new MegDataEntryOriginInfo(bytes);
Assert.NotEqual(origin, otherOrigin);

Assert.NotEqual(origin, new MegDataEntryOriginInfo(FileSystem.FileInfo.New("test.xml")));

var spanOrigin = new MegDataEntryOriginInfo((ReadOnlySpan<byte>)bytes);
Assert.Equal(spanOrigin, spanOrigin);
Assert.Equal(spanOrigin, (object)spanOrigin);

Assert.NotEqual(origin, otherOrigin);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,115 @@ public void FromEntryReference_OriginIsEntryReference_EntryPathEmpty_ThrowsArgum

#endregion

#region Factory FromBytes

[Theory]
[InlineData("entry.bin", true)]
[InlineData("entry.bin", false)]
public void FromBytes(string entryPath, bool encrypted)
{
var bytes = new byte[] { 1, 2, 3, 4, 5 };

var info = MegDataEntryBuilderInfo.FromBytes(bytes, entryPath, encrypted);

Assert.True(info.OriginInfo.IsBytes);
Assert.NotSame(bytes, info.OriginInfo.Bytes);
Assert.Equal(bytes, info.OriginInfo.Bytes);
Assert.Equal(entryPath, info.EntryPath);
Assert.Equal(5u, info.Size);
Assert.Equal(encrypted, info.Encrypted);
}

[Fact]
public void FromBytes_NullBytes_Throws()
{
Assert.Throws<ArgumentNullException>(() => MegDataEntryBuilderInfo.FromBytes(null!, "entry"));
}

[Fact]
public void FromBytes_NullOrEmptyEntryPath_Throws()
{
var bytes = new byte[] { 1, 2, 3 };
Assert.Throws<ArgumentNullException>(() => MegDataEntryBuilderInfo.FromBytes(bytes, null!));
Assert.Throws<ArgumentException>(() => MegDataEntryBuilderInfo.FromBytes(bytes, ""));
}

[Fact]
public void FromBytes_Empty_OK()
{
var info = MegDataEntryBuilderInfo.FromBytes((byte[])[], "entry.bin");

Assert.True(info.OriginInfo.IsBytes);
Assert.Equal(0u, info.Size);
}

#endregion

#region Factory FromReadOnlySpan<byte>

[Theory]
[InlineData("entry.bin", true)]
[InlineData("entry.bin", false)]
public void FromBytes_Span(string entryPath, bool encrypted)
{
Span<byte> bytes = [1, 2, 3, 4, 5];

var info = MegDataEntryBuilderInfo.FromBytes(bytes, entryPath, encrypted);

Assert.True(info.OriginInfo.IsBytes);
Assert.Equal(bytes.ToArray(), info.OriginInfo.Bytes);
Assert.Equal(entryPath, info.EntryPath);
Assert.Equal(5u, info.Size);
Assert.Equal(encrypted, info.Encrypted);
}

[Fact]
public void FromBytes_Soan_NullOrEmptyEntryPath_Throws()
{
byte[] bytes = [1, 2, 3];
Assert.Throws<ArgumentNullException>(() => MegDataEntryBuilderInfo.FromBytes(bytes.AsSpan(), null!));
Assert.Throws<ArgumentException>(() => MegDataEntryBuilderInfo.FromBytes(bytes.AsSpan(), ""));
}

[Fact]
public void FromBytes_Span_Empty_OK()
{
var info = MegDataEntryBuilderInfo.FromBytes([], "entry.bin");

Assert.True(info.OriginInfo.IsBytes);
Assert.Equal(0u, info.Size);
}

#endregion

#region RefreshSize

[Fact]
public void RefreshSize_FromBytes_Unchanged()
{
var bytes = new byte[] { 1, 2, 3, 4 };
var info = MegDataEntryBuilderInfo.FromBytes(bytes, "entry.bin");

Assert.Equal(4u, info.Size);

info.RefreshSize();

Assert.Equal(4u, info.Size);
}

[Fact]
public void RefreshSize_FromBytes_Span_Unchanged()
{
Span<byte> bytes = [1, 2, 3, 4];
var info = MegDataEntryBuilderInfo.FromBytes(bytes, "entry.bin");

Assert.Equal(4u, info.Size);

info.RefreshSize();

Assert.Equal(4u, info.Size);
}

[Fact]
public void RefreshSize_FromMegEntry()
{
Expand Down Expand Up @@ -324,7 +431,7 @@ public void RefreshSize_FromFile_FileDeleted_ThrowsFileNotFoundException()

FileSystem.File.Delete("test.xml");

Assert.Throws<FileNotFoundException>(() => info.RefreshSize());
Assert.Throws<FileNotFoundException>(info.RefreshSize);
}

[Fact]
Expand All @@ -339,7 +446,7 @@ public void RefreshSize_FileTooLarge_ThrowsMegEntrySizeException()

mockFileInfo.Length = (long)uint.MaxValue + 1;

Assert.Throws<MegEntrySizeException>(() => info.RefreshSize());
Assert.Throws<MegEntrySizeException>(info.RefreshSize);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
<ItemGroup>
<PackageReference Include="GitHubActionsTestLogger" Version="3.0.4" />
<PackageReference Include="xunit.v3.mtp-v2" Version="3.2.2" />
<PackageReference Include="Microsoft.Testing.Platform" Version="2.2.2" />
<PackageReference Include="Microsoft.Testing.Platform" Version="2.2.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.5.1" />
<PackageReference Include="coverlet.msbuild" Version="10.0.0">
<PackageReference Include="coverlet.msbuild" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="10.0.0">
<PackageReference Include="coverlet.collector" Version="10.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Loading
Loading