From f8b2c0bbff5c83b164974e8c7c7c46c5702e9eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E8=85=BE=E7=8C=AB=E8=B7=83?= <1043137532@qq.com> Date: Tue, 5 May 2026 13:12:41 +0800 Subject: [PATCH] test: OpenZip --- Tests/MeloongCore.Tests.csproj | 23 +++++++ Tests/TestFiles/FileUtils/Corrupted.zip | Bin 0 -> 767 bytes Tests/TestFiles/FileUtils/GB Encoding.zip | Bin 0 -> 1041 bytes Tests/TestFiles/FileUtils/Not zip.zip | 2 + Tests/TestFiles/FileUtils/UTF8 Encoding.zip | Bin 0 -> 1221 bytes Tests/TestUtils.cs | 11 ++++ Tests/Utils/FileUtilsTest.cs | 69 ++++++++------------ 7 files changed, 62 insertions(+), 43 deletions(-) create mode 100644 Tests/TestFiles/FileUtils/Corrupted.zip create mode 100644 Tests/TestFiles/FileUtils/GB Encoding.zip create mode 100644 Tests/TestFiles/FileUtils/Not zip.zip create mode 100644 Tests/TestFiles/FileUtils/UTF8 Encoding.zip create mode 100644 Tests/TestUtils.cs diff --git a/Tests/MeloongCore.Tests.csproj b/Tests/MeloongCore.Tests.csproj index da9e95f..526a754 100644 --- a/Tests/MeloongCore.Tests.csproj +++ b/Tests/MeloongCore.Tests.csproj @@ -11,6 +11,13 @@ enable + + + + + + + @@ -25,6 +32,21 @@ + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + @@ -35,6 +57,7 @@ + diff --git a/Tests/TestFiles/FileUtils/Corrupted.zip b/Tests/TestFiles/FileUtils/Corrupted.zip new file mode 100644 index 0000000000000000000000000000000000000000..f68e5f379bc5d00ac2427fa6c23944abe64ef6b1 GIT binary patch literal 767 zcmWIWW@Zs#U}E545MnqUwSIFO&wU0420caw1_24WG)YZO95<~6jjttF$nR5va3HA^0tgGd2Z4ge}1ac44v1uwe$C`%b#_7 zciPT734iPUf0}b#d28UV#6lvgdKww;4j}5$A6d`Ur{ai=I*% zs<&>{COz#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SM92!RBEq2D!V0AP<)RyL5yEI@b_=*wF` HqZt?gWK`df literal 0 HcmV?d00001 diff --git a/Tests/TestUtils.cs b/Tests/TestUtils.cs new file mode 100644 index 0000000..9582ad6 --- /dev/null +++ b/Tests/TestUtils.cs @@ -0,0 +1,11 @@ +namespace MeloongCore.Tests; +public static class TestUtils { + + /// + /// 获取测试用文件的路径。 + /// + public static string GetTestFile(string directoryName, string fileName) { + return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestFiles", directoryName, fileName); + } + +} diff --git a/Tests/Utils/FileUtilsTest.cs b/Tests/Utils/FileUtilsTest.cs index 8a5a7bd..5121b97 100644 --- a/Tests/Utils/FileUtilsTest.cs +++ b/Tests/Utils/FileUtilsTest.cs @@ -3,6 +3,32 @@ using System.IO.Compression; namespace MeloongCore.Tests; public class FileUtilsTest : IDisposable { + #region 压缩包 + + [Fact(DisplayName = nameof(OpenZip))] + public void OpenZip() { + // GB 编码 + using var gbArchive = FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "GB Encoding.zip")); + Assert.Contains(gbArchive.Entries, e => e.Name == "中文文件.txt"); + Assert.Contains(gbArchive.Entries, e => e.Name == "fabricloader.log"); + Assert.DoesNotContain(gbArchive.Entries, e => e.Name == "fabricloader.log2"); + // UTF8 编码 + using var utfArchive = FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "UTF8 Encoding.zip")); + Assert.Contains(utfArchive.Entries, e => e.Name == "中文文件.txt"); + Assert.Contains(utfArchive.Entries, e => e.Name == "fabricloader.log"); + Assert.DoesNotContain(utfArchive.Entries, e => e.Name == "fabricloader.log2"); + // 损坏的文件 + Assert.Throws(() => FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "Corrupted.zip"))); + // 非压缩文件 + Assert.Throws(() => FileUtils.OpenZip(TestUtils.GetTestFile(nameof(FileUtils), "Not zip.zip"))); + } + + #endregion + + + + + #region 辅助方法 private readonly string _tempDir; @@ -66,49 +92,6 @@ public class FileUtilsTest : IDisposable { #endregion - #region OpenZip - - public void OpenZip_ValidUtf8Zip_OpensSuccessfully() { - string zipPath = CreateZipWithEncoding("utf8.zip", new UTF8Encoding(false, true), - ("hello.txt", "world")); - using var archive = FileUtils.OpenZip(zipPath); - Assert.Single(archive.Entries); - Assert.Equal("hello.txt", archive.Entries[0].FullName); - } - - public void OpenZip_ValidUtf8Zip_EntryContentIsCorrect() { - const string expectedContent = "测试内容"; - string zipPath = CreateZipWithEncoding("utf8content.zip", new UTF8Encoding(false, true), - ("data.txt", expectedContent)); - using var archive = FileUtils.OpenZip(zipPath); - using var entryStream = archive.Entries[0].Open(); - using var reader = new StreamReader(entryStream, Encoding.UTF8); - Assert.Equal(expectedContent, reader.ReadToEnd()); - } - - public void OpenZip_MultipleEntries_AllEntriesAccessible() { - string zipPath = CreateZipWithEncoding("multi.zip", new UTF8Encoding(false, true), - ("a.txt", "aaa"), - ("b.txt", "bbb"), - ("c.txt", "ccc")); - using var archive = FileUtils.OpenZip(zipPath); - Assert.Equal(3, archive.Entries.Count); - } - - public void OpenZip_InvalidFile_ThrowsInvalidDataException() { - string invalidPath = Path.Combine(_tempDir, "invalid.zip"); - File.WriteAllBytes(invalidPath, [0x00, 0x01, 0x02, 0x03]); // 非 zip 数据 - Assert.Throws(() => FileUtils.OpenZip(invalidPath)); - } - - public void OpenZip_EmptyZip_ReturnsEmptyEntries() { - string zipPath = CreateZipWithEncoding("empty.zip", new UTF8Encoding(false, true)); - using var archive = FileUtils.OpenZip(zipPath); - Assert.Empty(archive.Entries); - } - - #endregion - #region ExtractToDirectory 测试