| 1 | using System.IO.Compression; |
| 2 | using Zipelo.Core; |
| 3 | using Zipelo.Runtime; |
| 4 | using Zipelo.SecurityTests; |
| 5 | |
| 6 | namespace Zipelo.RuntimeTests; |
| 7 | |
| 8 | internal sealed partial class RuntimeTests |
| 9 | { |
| 10 | private string PublicationDirectory() |
| 11 | { |
| 12 | var directory = Path.Combine(sandbox, Guid.NewGuid().ToString("N")); |
| 13 | Directory.CreateDirectory(directory); |
| 14 | return directory; |
| 15 | } |
| 16 | |
| 17 | private static async Task<ZipWorkerResult> Artifact() |
| 18 | { |
| 19 | var result = await Create(Worker, "input.txt", new byte[] { 2, 4, 6 }); |
| 20 | Check.Equal(JobState.Completed, result.Job.State); |
| 21 | Check.That(result.CreatedZip is { Length: > 0 }, "실제 생성물이 없음"); |
| 22 | return result; |
| 23 | } |
| 24 | |
| 25 | private static void PublicationFailed(ZipPublicationResult result, ArchiveError error) |
| 26 | { |
| 27 | Check.That(!result.Published, "실패한 게시를 성공으로 표시함"); |
| 28 | Check.Equal(error, result.Error); |
| 29 | Check.That(!result.CleanupFailed && result.TemporaryFileName is null, "작업 임시 파일 정리에 실패함"); |
| 30 | } |
| 31 | |
| 32 | private async Task PublishNewFile() |
| 33 | { |
| 34 | var directory = PublicationDirectory(); |
| 35 | var destination = Path.Combine(directory, "new.zip"); |
| 36 | var artifact = await Artifact(); |
| 37 | var result = await NewZipPublisher.PublishAsync(artifact, destination, default); |
| 38 | Check.That(result.Published && !result.CleanupFailed, "새 파일 게시 또는 정리에 실패함"); |
| 39 | Check.Equal(ArchiveError.None, result.Error); |
| 40 | Check.That(result.TemporaryFileName is null, "성공한 게시에 임시 파일이 보고됨"); |
| 41 | var actual = await File.ReadAllBytesAsync(destination); |
| 42 | Check.That(actual.SequenceEqual(artifact.CreatedZip!), "게시 바이트가 생성물과 다름"); |
| 43 | Check.That(Directory.GetFileSystemEntries(directory).SequenceEqual(new[] { destination }), "임시 파일이 남음"); |
| 44 | using var input = File.OpenRead(destination); |
| 45 | using var zip = new ZipArchive(input, ZipArchiveMode.Read); |
| 46 | Check.Equal("input.txt", zip.Entries.Single().FullName); |
| 47 | } |
| 48 | |
| 49 | private async Task PublishConflictPreservesSource(bool useSourcePath) |
| 50 | { |
| 51 | var directory = PublicationDirectory(); |
| 52 | var source = Path.Combine(directory, "input.txt"); |
| 53 | var occupied = Path.Combine(directory, "existing.zip"); |
| 54 | byte[] original = [3, 5, 7]; |
| 55 | byte[] existing = [11, 13]; |
| 56 | await File.WriteAllBytesAsync(source, original); |
| 57 | await File.WriteAllBytesAsync(occupied, existing); |
| 58 | ZipWorkerResult artifact; |
| 59 | await using (var input = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read)) |
| 60 | { |
| 61 | artifact = await Worker.CreateAsync(Guid.NewGuid(), [new("input.txt", input)], Limits, null, default); |
| 62 | } |
| 63 | Check.Equal(JobState.Completed, artifact.Job.State); |
| 64 | var destination = useSourcePath ? source : occupied; |
| 65 | var result = await NewZipPublisher.PublishAsync(artifact, destination, default); |
| 66 | // 오류 분류가 틀려도 파일 보존과 임시 파일 잔존 여부를 먼저 확인함. |
| 67 | var sourceAfter = await File.ReadAllBytesAsync(source); |
| 68 | var occupiedAfter = await File.ReadAllBytesAsync(occupied); |
| 69 | Check.That(original.SequenceEqual(sourceAfter), "원본 파일이 변경됨"); |
| 70 | Check.That(existing.SequenceEqual(occupiedAfter), "기존 출력 파일이 변경됨"); |
| 71 | Check.Equal(2, Directory.GetFileSystemEntries(directory).Length); |
| 72 | PublicationFailed(result, ArchiveError.OutputConflict); |
| 73 | } |
| 74 | |
| 75 | private async Task PublishPreCancelled() |
| 76 | { |
| 77 | var directory = PublicationDirectory(); |
| 78 | var sentinel = Path.Combine(directory, "existing.txt"); |
| 79 | await File.WriteAllTextAsync(sentinel, "보존"); |
| 80 | var artifact = await Artifact(); |
| 81 | using var cancellation = new CancellationTokenSource(); |
| 82 | cancellation.Cancel(); |
| 83 | foreach (var destination in new[] { sentinel, Path.Combine(directory, "new.zip") }) |
| 84 | { |
| 85 | PublicationFailed(await NewZipPublisher.PublishAsync(artifact, destination, cancellation.Token), |
| 86 | ArchiveError.Cancelled); |
| 87 | } |
| 88 | Check.Equal("보존", await File.ReadAllTextAsync(sentinel)); |
| 89 | Check.Equal(1, Directory.GetFileSystemEntries(directory).Length); |
| 90 | } |
| 91 | |
| 92 | private async Task PublishFailurePreservesFiles() |
| 93 | { |
| 94 | var directory = PublicationDirectory(); |
| 95 | var sentinel = Path.Combine(directory, "sentinel.txt"); |
| 96 | await File.WriteAllTextAsync(sentinel, "보존"); |
| 97 | var artifact = await Artifact(); |
| 98 | var failed = artifact with { Job = artifact.Job with { State = JobState.Failed, Error = ArchiveError.IntegrityFailure } }; |
| 99 | PublicationFailed(await NewZipPublisher.PublishAsync(failed, sentinel, default), ArchiveError.UnsupportedOperation); |
| 100 | PublicationFailed(await NewZipPublisher.PublishAsync(artifact, "relative.zip", default), ArchiveError.UnsafePath); |
| 101 | PublicationFailed(await NewZipPublisher.PublishAsync(artifact, Path.Combine(directory, "absent", "new.zip"), default), |
| 102 | ArchiveError.OutputUnavailable); |
| 103 | Check.Equal("보존", await File.ReadAllTextAsync(sentinel)); |
| 104 | Check.Equal(1, Directory.GetFileSystemEntries(directory).Length); |
| 105 | } |
| 106 | |
| 107 | private async Task PublishRejectsLinks(bool parentLink) |
| 108 | { |
| 109 | var directory = PublicationDirectory(); |
| 110 | var target = Path.Combine(directory, "target"); |
| 111 | Directory.CreateDirectory(target); |
| 112 | var sentinel = Path.Combine(target, "sentinel.txt"); |
| 113 | await File.WriteAllTextAsync(sentinel, "보존"); |
| 114 | var link = Path.Combine(directory, parentLink ? "link" : "existing.zip"); |
| 115 | FileSystemInfo linkInfo; |
| 116 | try |
| 117 | { |
| 118 | linkInfo = parentLink |
| 119 | ? Directory.CreateSymbolicLink(link, target) |
| 120 | : File.CreateSymbolicLink(link, sentinel); |
| 121 | } |
| 122 | catch (Exception exception) when (OperatingSystem.IsWindows() && |
| 123 | (exception is UnauthorizedAccessException || exception is IOException && |
| 124 | (exception.HResult & 0xFFFF) == 1314)) |
| 125 | { |
| 126 | throw new CaseSkippedException("Windows 심볼릭 링크 생성 권한이 없어 해당 사례 미실행"); |
| 127 | } |
| 128 | try |
| 129 | { |
| 130 | var originalLinkTarget = linkInfo.LinkTarget; |
| 131 | Check.That(originalLinkTarget is not null, "검사용 링크가 생성되지 않음"); |
| 132 | var artifact = await Artifact(); |
| 133 | var destination = parentLink ? Path.Combine(link, "new.zip") : link; |
| 134 | var result = await NewZipPublisher.PublishAsync(artifact, destination, default); |
| 135 | Check.Equal("보존", await File.ReadAllTextAsync(sentinel)); |
| 136 | Check.Equal(1, Directory.GetFileSystemEntries(target).Length); |
| 137 | Check.Equal(2, Directory.GetFileSystemEntries(directory).Length); |
| 138 | linkInfo.Refresh(); |
| 139 | Check.That((linkInfo.Attributes & FileAttributes.ReparsePoint) != 0, "기존 링크가 교체됨"); |
| 140 | Check.Equal(originalLinkTarget, linkInfo.LinkTarget); |
| 141 | PublicationFailed(result, parentLink ? ArchiveError.UnsafePath : ArchiveError.OutputConflict); |
| 142 | } |
| 143 | finally { linkInfo.Delete(); } |
| 144 | } |
| 145 | } |