|
| 1 | +using System.Security.Cryptography; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using MS2Lib; |
| 4 | +using MiscUtils; |
| 5 | + |
| 6 | +namespace MS2Tools.IntegrationTests; |
| 7 | + |
| 8 | +[TestClass] |
| 9 | +public class ServerArchiveTests |
| 10 | +{ |
| 11 | + private const string ServerSourcePath = @"D:\Projetos\GitHub\MapleStory2-XML\server"; |
| 12 | + private const string TestOutputDir = "ServerArchiveTestOutput"; |
| 13 | + private const string ArchiveName = "server"; |
| 14 | + private const string HeaderFileExtension = ".m2h"; |
| 15 | + private const string DataFileExtension = ".m2d"; |
| 16 | + |
| 17 | + [ClassInitialize] |
| 18 | + public static void ClassInit(TestContext context) |
| 19 | + { |
| 20 | + if (!Directory.Exists(ServerSourcePath)) |
| 21 | + { |
| 22 | + Assert.Inconclusive($"Server source folder not found: {ServerSourcePath}"); |
| 23 | + } |
| 24 | + |
| 25 | + if (Directory.Exists(TestOutputDir)) |
| 26 | + { |
| 27 | + Directory.Delete(TestOutputDir, true); |
| 28 | + } |
| 29 | + |
| 30 | + Directory.CreateDirectory(TestOutputDir); |
| 31 | + } |
| 32 | + |
| 33 | + [ClassCleanup] |
| 34 | + public static void ClassCleanup() |
| 35 | + { |
| 36 | + if (Directory.Exists(TestOutputDir)) |
| 37 | + { |
| 38 | + Directory.Delete(TestOutputDir, true); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static (string FullPath, string RelativePath)[] GetFilesRelative(string path) |
| 43 | + { |
| 44 | + if (!path.EndsWith(Path.DirectorySeparatorChar)) |
| 45 | + { |
| 46 | + path += Path.DirectorySeparatorChar; |
| 47 | + } |
| 48 | + |
| 49 | + string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories); |
| 50 | + Array.Sort(files, StringComparer.Ordinal); |
| 51 | + |
| 52 | + var result = new (string FullPath, string RelativePath)[files.Length]; |
| 53 | + for (int i = 0; i < files.Length; i++) |
| 54 | + { |
| 55 | + result[i] = (files[i], files[i].Substring(path.Length)); |
| 56 | + } |
| 57 | + |
| 58 | + return result; |
| 59 | + } |
| 60 | + |
| 61 | + private static CompressionType GetCompressionTypeFromFileExtension(string filePath) => |
| 62 | + Path.GetExtension(filePath) switch |
| 63 | + { |
| 64 | + ".png" => CompressionType.Png, |
| 65 | + ".usm" => CompressionType.Usm, |
| 66 | + ".zlib" => CompressionType.Zlib, |
| 67 | + _ => CompressionType.Zlib, |
| 68 | + }; |
| 69 | + |
| 70 | + private static async Task<(string headerPath, string dataPath)> PackageServerFolder(string outputSubDir) |
| 71 | + { |
| 72 | + string outputPath = Path.Combine(TestOutputDir, outputSubDir); |
| 73 | + Directory.CreateDirectory(outputPath); |
| 74 | + |
| 75 | + string headerPath = Path.Combine(outputPath, ArchiveName + HeaderFileExtension); |
| 76 | + string dataPath = Path.Combine(outputPath, ArchiveName + DataFileExtension); |
| 77 | + |
| 78 | + var archive = new MS2Archive(Repositories.Repos[MS2CryptoMode.MS2F]); |
| 79 | + var filePaths = GetFilesRelative(ServerSourcePath); |
| 80 | + |
| 81 | + for (uint i = 0; i < filePaths.Length; i++) |
| 82 | + { |
| 83 | + var (fullPath, relativePath) = filePaths[i]; |
| 84 | + uint id = i + 1; |
| 85 | + FileStream fs = File.OpenRead(fullPath); |
| 86 | + IMS2FileInfo info = new MS2FileInfo(id.ToString(), relativePath); |
| 87 | + IMS2FileHeader header = new MS2FileHeader(fs.Length, id, 0, GetCompressionTypeFromFileExtension(fullPath)); |
| 88 | + IMS2File file = new MS2File(archive, fs, info, header, false); |
| 89 | + archive.Add(file); |
| 90 | + } |
| 91 | + |
| 92 | + await archive.SaveConcurrentlyAsync(headerPath, dataPath); |
| 93 | + |
| 94 | + return (headerPath, dataPath); |
| 95 | + } |
| 96 | + |
| 97 | + private static string ComputeFileHash(string filePath) |
| 98 | + { |
| 99 | + using var stream = File.OpenRead(filePath); |
| 100 | + byte[] hash = SHA256.HashData(stream); |
| 101 | + return Convert.ToHexString(hash); |
| 102 | + } |
| 103 | + |
| 104 | + [TestMethod] |
| 105 | + [Timeout(300000)] // 5 min timeout for large archive |
| 106 | + public async Task Package_ServerFolder_ProducesDeterministicOutput() |
| 107 | + { |
| 108 | + // First run |
| 109 | + var (headerPath1, dataPath1) = await PackageServerFolder("run1"); |
| 110 | + string headerHash1 = ComputeFileHash(headerPath1); |
| 111 | + string dataHash1 = ComputeFileHash(dataPath1); |
| 112 | + |
| 113 | + Assert.IsTrue(File.Exists(headerPath1), "Header file should exist after first run"); |
| 114 | + Assert.IsTrue(File.Exists(dataPath1), "Data file should exist after first run"); |
| 115 | + Assert.IsTrue(new FileInfo(headerPath1).Length > 0, "Header file should not be empty"); |
| 116 | + Assert.IsTrue(new FileInfo(dataPath1).Length > 0, "Data file should not be empty"); |
| 117 | + |
| 118 | + // Second run |
| 119 | + var (headerPath2, dataPath2) = await PackageServerFolder("run2"); |
| 120 | + string headerHash2 = ComputeFileHash(headerPath2); |
| 121 | + string dataHash2 = ComputeFileHash(dataPath2); |
| 122 | + |
| 123 | + Assert.AreEqual(headerHash1, headerHash2, |
| 124 | + $"Header hash mismatch between runs.\nRun1: {headerHash1}\nRun2: {headerHash2}"); |
| 125 | + Assert.AreEqual(dataHash1, dataHash2, |
| 126 | + $"Data hash mismatch between runs.\nRun1: {dataHash1}\nRun2: {dataHash2}"); |
| 127 | + |
| 128 | + // Third run for extra confidence |
| 129 | + var (headerPath3, dataPath3) = await PackageServerFolder("run3"); |
| 130 | + string headerHash3 = ComputeFileHash(headerPath3); |
| 131 | + string dataHash3 = ComputeFileHash(dataPath3); |
| 132 | + |
| 133 | + Assert.AreEqual(headerHash1, headerHash3, |
| 134 | + $"Header hash mismatch on third run.\nRun1: {headerHash1}\nRun3: {headerHash3}"); |
| 135 | + Assert.AreEqual(dataHash1, dataHash3, |
| 136 | + $"Data hash mismatch on third run.\nRun1: {dataHash1}\nRun3: {dataHash3}"); |
| 137 | + } |
| 138 | + |
| 139 | + [TestMethod] |
| 140 | + [Timeout(300000)] |
| 141 | + public async Task Package_ThenExtract_ServerFolder_FilesMatchOriginals() |
| 142 | + { |
| 143 | + var (headerPath, dataPath) = await PackageServerFolder("extract_test"); |
| 144 | + |
| 145 | + string extractPath = Path.Combine(TestOutputDir, "extracted"); |
| 146 | + Directory.CreateDirectory(extractPath); |
| 147 | + |
| 148 | + // Extract |
| 149 | + using (IMS2Archive archive = await MS2Archive.GetAndLoadArchiveAsync(headerPath, dataPath)) |
| 150 | + { |
| 151 | + Assert.IsTrue(archive.Count > 0, "Archive should contain files"); |
| 152 | + |
| 153 | + foreach (var file in archive) |
| 154 | + { |
| 155 | + string destPath = Path.Combine(extractPath, file.Name); |
| 156 | + |
| 157 | + using Stream stream = await file.GetStreamAsync(); |
| 158 | + await stream.CopyToAsync(destPath); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Verify extracted files match originals |
| 163 | + var originalFiles = GetFilesRelative(ServerSourcePath); |
| 164 | + int verifiedCount = 0; |
| 165 | + |
| 166 | + foreach (var (fullPath, relativePath) in originalFiles) |
| 167 | + { |
| 168 | + string extractedPath = Path.Combine(extractPath, relativePath); |
| 169 | + Assert.IsTrue(File.Exists(extractedPath), |
| 170 | + $"Extracted file missing: {relativePath}"); |
| 171 | + |
| 172 | + byte[] originalBytes = await File.ReadAllBytesAsync(fullPath); |
| 173 | + byte[] extractedBytes = await File.ReadAllBytesAsync(extractedPath); |
| 174 | + |
| 175 | + CollectionAssert.AreEqual(originalBytes, extractedBytes, |
| 176 | + $"Content mismatch for: {relativePath}"); |
| 177 | + verifiedCount++; |
| 178 | + } |
| 179 | + |
| 180 | + Assert.AreEqual(originalFiles.Length, verifiedCount, |
| 181 | + "Number of verified files should match number of original files"); |
| 182 | + } |
| 183 | + |
| 184 | + [TestMethod] |
| 185 | + [Timeout(300000)] |
| 186 | + public async Task Package_ServerFolder_ArchiveFileCountMatchesSourceFileCount() |
| 187 | + { |
| 188 | + var (headerPath, dataPath) = await PackageServerFolder("count_test"); |
| 189 | + |
| 190 | + using IMS2Archive archive = await MS2Archive.GetAndLoadArchiveAsync(headerPath, dataPath); |
| 191 | + |
| 192 | + var sourceFiles = GetFilesRelative(ServerSourcePath); |
| 193 | + Assert.AreEqual(sourceFiles.Length, (int)archive.Count, |
| 194 | + $"Archive should contain {sourceFiles.Length} files, but has {archive.Count}"); |
| 195 | + } |
| 196 | +} |
0 commit comments