| | @@ -0,0 +1,464 @@ |
| 1 | +추가: using System.Formats.Tar; |
| 2 | +추가: using System.Security.Cryptography; |
| 3 | +추가: using System.Text; |
| 4 | +추가: using Zipelo.Core; |
| 5 | +추가: using Zipelo.Worker.Tar; |
| 6 | +추가: |
| 7 | +추가: var tests = new List<(string Name, Func<Task> Run)>(); |
| 8 | +추가: var normal = Fixtures.Archive(new Fixture("folder/", [], '5'), new Fixture("folder/a.txt", "alpha"u8.ToArray()), |
| 9 | +추가: new Fixture("empty", []), new Fixture("b.bin", Enumerable.Range(0, 140000).Select(i => (byte)i).ToArray())); |
| 10 | +추가: var defaults = new UstarLimits(new JobLimits(1024, 128L << 20, 16L << 20, TimeSpan.FromSeconds(15), 1), 16L << 20, 1L << 20); |
| 11 | +추가: |
| 12 | +추가: tests.Add(("list", async () => |
| 13 | +추가: { |
| 14 | +추가: using var source = new MemoryStream(normal, false); |
| 15 | +추가: using var sink = new HashSink(); |
| 16 | +추가: var result = await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => [], sink.Write, default); |
| 17 | +추가: Check(result.Entries.Count == 4 && result.Entries[0].Name == "folder" && result.Usage.Entries == 4); |
| 18 | +추가: Check(result.Usage.ProcessedBytes == normal.Length && result.Usage.OutputBytes == 0 && sink.Calls == 0); |
| 19 | +추가: Check(result.ContentVerification == VerificationState.Unavailable); |
| 20 | +추가: Check(result.InputSha256 == Convert.ToHexString(SHA256.HashData(normal)) && source.CanRead); |
| 21 | +추가: })); |
| 22 | +추가: tests.Add(("selected.hash.chunks", async () => |
| 23 | +추가: { |
| 24 | +추가: using var source = new FragmentedStream(normal); |
| 25 | +추가: using var sink = new HashSink(); |
| 26 | +추가: var result = await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => ["2", "3"], sink.Write, default); |
| 27 | +추가: Check(sink.Counts["2"] == 0 && sink.Counts["3"] == 140000 && !sink.Counts.ContainsKey("1")); |
| 28 | +추가: Check(sink.Digest("3") == Convert.ToHexString(SHA256.HashData(Enumerable.Range(0, 140000).Select(i => (byte)i).ToArray()))); |
| 29 | +추가: Check(sink.MaxChunk == UstarReader.ChunkSize && result.Usage.ProcessedBytes == normal.Length * 2L); |
| 30 | +추가: Check(result.Usage.OutputBytes == 140000 && result.Usage.Entries == 4); |
| 31 | +추가: })); |
| 32 | +추가: tests.Add(("bcl.ustar.prefix", async () => |
| 33 | +추가: { |
| 34 | +추가: using var archive = new MemoryStream(); |
| 35 | +추가: var prefix = new string('p', 80) + "/" + new string('q', 60); |
| 36 | +추가: using (var writer = new TarWriter(archive, TarEntryFormat.Ustar, leaveOpen: true)) |
| 37 | +추가: { |
| 38 | +추가: var entry = new UstarTarEntry(TarEntryType.RegularFile, prefix + "/a") { DataStream = new MemoryStream("ok"u8.ToArray()) }; |
| 39 | +추가: writer.WriteEntry(entry); |
| 40 | +추가: } |
| 41 | +추가: using var sink = new HashSink(); |
| 42 | +추가: var result = await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(archive, _ => ["0"], sink.Write, default); |
| 43 | +추가: Check(result.Entries[0].Name == prefix + "/a" && sink.Counts["0"] == 2); |
| 44 | +추가: })); |
| 45 | +추가: tests.Add(("empty.archive", async () => { await Run(new byte[1024]); })); |
| 46 | +추가: tests.Add(("zero.trailing.blocks", async () => { await Run([.. normal, .. new byte[1024]]); })); |
| 47 | +추가: tests.Add(("directory.after.child", async () => { await Run(Fixtures.Archive(new Fixture("a/b", []), new Fixture("a/", [], '5'))); })); |
| 48 | +추가: tests.Add(("linux.case.distinct", async () => { await Run(Fixtures.Archive(new Fixture("A", []), new Fixture("a", []))); })); |
| 49 | +추가: tests.Add(("header.checksum.not.content", async () => |
| 50 | +추가: { |
| 51 | +추가: var bytes = Fixtures.Archive(new Fixture("a", "one"u8.ToArray())); |
| 52 | +추가: bytes[512] ^= 1; |
| 53 | +추가: var result = await Run(bytes); |
| 54 | +추가: Check(result.ContentVerification == VerificationState.Unavailable); |
| 55 | +추가: })); |
| 56 | +추가: |
| 57 | +추가: Reject("checksum", Mutate(normal, 0, 1), ArchiveError.IntegrityFailure, checksum: false); |
| 58 | +추가: Reject("truncated.header", normal[..511], ArchiveError.IntegrityFailure, checksum: false); |
| 59 | +추가: Reject("truncated.body", Fixtures.Archive(new Fixture("a", new byte[1024]))[..1024], ArchiveError.IntegrityFailure, checksum: false); |
| 60 | +추가: Reject("end.missing", Fixtures.Archive(new Fixture("a", []))[..512], ArchiveError.IntegrityFailure, checksum: false); |
| 61 | +추가: Reject("end.single", Fixtures.Archive(new Fixture("a", []))[..1024], ArchiveError.IntegrityFailure, checksum: false); |
| 62 | +추가: Reject("end.second.nonzero", Mutate(new byte[1024], 512, 1), ArchiveError.IntegrityFailure, checksum: false); |
| 63 | +추가: Reject("trailing.nonzero", [.. normal, .. Enumerable.Repeat((byte)1, 512)], ArchiveError.IntegrityFailure, checksum: false); |
| 64 | +추가: Reject("trailing.partial", [.. normal, (byte)0], ArchiveError.IntegrityFailure, checksum: false); |
| 65 | +추가: Reject("padding.nonzero", Mutate(Fixtures.Archive(new Fixture("a", [1])), 513, 1), ArchiveError.IntegrityFailure, checksum: false); |
| 66 | +추가: Reject("size.huge.octal", Field(Fixtures.Archive(new Fixture("a", [])), 124, "77777777777\0"), ArchiveError.IntegrityFailure); |
| 67 | +추가: RejectSizeEncoding("size.base256", [0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], []); |
| 68 | +추가: RejectSizeEncoding("size.negative", Encoding.ASCII.GetBytes("-0000000000\0"), []); |
| 69 | +추가: RejectSizeEncoding("size.invalid.digit", Encoding.ASCII.GetBytes("00000000008\0"), new byte[8]); |
| 70 | +추가: RejectSizeEncoding("size.internal.terminator", Encoding.ASCII.GetBytes("000000000\0" + "1\0"), new byte[1]); |
| 71 | +추가: Reject("directory.nonzero", Field(normal, 124, "00000000001\0"), ArchiveError.IntegrityFailure); |
| 72 | +추가: Reject("version", Mutate(normal, 264, (byte)'1'), ArchiveError.UnsupportedEntry); |
| 73 | +추가: Reject("gnu.magic", Mutate(normal, 262, (byte)' '), ArchiveError.UnsupportedEntry); |
| 74 | +추가: Reject("linkname.on.file", Mutate(Fixtures.Archive(new Fixture("a", [])), 157, (byte)'x'), ArchiveError.IntegrityFailure); |
| 75 | +추가: Reject("reserved.bytes", Mutate(normal, 500, 1), ArchiveError.IntegrityFailure); |
| 76 | +추가: Reject("name.hidden.after.nul", Mutate(Fixtures.Archive(new Fixture("a", [])), 2, (byte)'b'), ArchiveError.IntegrityFailure); |
| 77 | +추가: Reject("invalid.utf8", Mutate(Fixtures.Archive(new Fixture("a", [])), 0, 255), ArchiveError.IntegrityFailure); |
| 78 | +추가: foreach (var type in new[] { 'x', 'g', 'L', 'K', 'S', '1', '2', '3', '4', '6', '7', 'V', '?' }) |
| 79 | +추가: { |
| 80 | +추가: Reject("type." + type, Fixtures.Archive(new Fixture("a", [], type)), ArchiveError.UnsupportedEntry); |
| 81 | +추가: } |
| 82 | +추가: foreach (var name in new[] { "../a", "/root", "a//b", "a/./b", "a/../b", "a\\b", "" }) |
| 83 | +추가: { |
| 84 | +추가: Reject("path." + name, Fixtures.Archive(new Fixture(name, [])), name.Length == 0 ? ArchiveError.IntegrityFailure : ArchiveError.UnsafePath); |
| 85 | +추가: } |
| 86 | +추가: foreach (var entries in new[] |
| 87 | +추가: { |
| 88 | +추가: new[] { new Fixture("a", []), new Fixture("a", []) }, |
| 89 | +추가: new[] { new Fixture("a/", [], '5'), new Fixture("a", []) }, |
| 90 | +추가: new[] { new Fixture("a", []), new Fixture("a/b", []) }, |
| 91 | +추가: new[] { new Fixture("a/b", []), new Fixture("a", []) } |
| 92 | +추가: }) |
| 93 | +추가: { |
| 94 | +추가: Reject("collision." + tests.Count, Fixtures.Archive(entries), ArchiveError.OutputConflict); |
| 95 | +추가: } |
| 96 | +추가: Reject("windows.case.collision", Fixtures.Archive(new Fixture("A", []), new Fixture("a", [])), ArchiveError.OutputConflict, policy: FileNamePolicy.Windows); |
| 97 | +추가: Reject("windows.device", Fixtures.Archive(new Fixture("CON.txt", [])), ArchiveError.UnsafePath, policy: FileNamePolicy.Windows); |
| 98 | +추가: Reject("windows.drive", Fixtures.Archive(new Fixture("C:/a", [])), ArchiveError.UnsafePath, policy: FileNamePolicy.Windows); |
| 99 | +추가: Reject("unselected.unsafe", Fixtures.Archive(new Fixture("ok", [1]), new Fixture("../bad", [])), ArchiveError.UnsafePath, selection: _ => ["0"]); |
| 100 | +추가: Reject("input.limit", normal, ArchiveError.BudgetExceeded, limits: defaults with { MaxInputBytes = normal.Length - 1 }); |
| 101 | +추가: Reject("entries.limit", normal, ArchiveError.BudgetExceeded, limits: defaults with { Job = defaults.Job with { MaxEntries = 3 } }); |
| 102 | +추가: Reject("metadata.limit", normal, ArchiveError.BudgetExceeded, limits: defaults with { MaxMetadataBytes = 2047 }); |
| 103 | +추가: Reject("processed.first.pass", normal, ArchiveError.BudgetExceeded, limits: defaults with { Job = defaults.Job with { MaxProcessedBytes = normal.Length - 1 } }); |
| 104 | +추가: Reject("processed.second.pass.cumulative", normal, ArchiveError.BudgetExceeded, |
| 105 | +추가: limits: defaults with { Job = defaults.Job with { MaxProcessedBytes = normal.Length * 2L - 1 } }, expectNoOutput: false); |
| 106 | +추가: Reject("output.limit", normal, ArchiveError.BudgetExceeded, limits: defaults with { Job = defaults.Job with { MaxOutputBytes = 4 } }); |
| 107 | +추가: Reject("entries.hard.cap", Fixtures.Archive(Enumerable.Range(0, 1025).Select(i => new Fixture("f" + i, [])).ToArray()), |
| 108 | +추가: ArchiveError.BudgetExceeded, limits: defaults with { Job = defaults.Job with { MaxEntries = long.MaxValue } }); |
| 109 | +추가: Reject("input.hard.cap", new byte[(16 << 20) + 512], ArchiveError.BudgetExceeded, |
| 110 | +추가: limits: defaults with { MaxInputBytes = long.MaxValue }); |
| 111 | +추가: Reject("selection.duplicate", normal, ArchiveError.UnsupportedEntry, selection: _ => ["1", "1"]); |
| 112 | +추가: Reject("selection.unknown", normal, ArchiveError.UnsupportedEntry, selection: _ => ["99"]); |
| 113 | +추가: |
| 114 | +추가: tests.Add(("cancel.before.read", async () => |
| 115 | +추가: { |
| 116 | +추가: using var cts = new CancellationTokenSource(); |
| 117 | +추가: cts.Cancel(); |
| 118 | +추가: await Cancelled(() => Run(normal, token: cts.Token)); |
| 119 | +추가: })); |
| 120 | +추가: tests.Add(("cancel.during.read", async () => |
| 121 | +추가: { |
| 122 | +추가: using var cts = new CancellationTokenSource(); |
| 123 | +추가: using var source = new FragmentedStream(normal, () => cts.Cancel()); |
| 124 | +추가: using var sink = new HashSink(); |
| 125 | +추가: await Cancelled(() => new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => [], sink.Write, cts.Token)); |
| 126 | +추가: Check(sink.Calls == 0); |
| 127 | +추가: })); |
| 128 | +추가: tests.Add(("cancel.sink", async () => |
| 129 | +추가: { |
| 130 | +추가: using var cts = new CancellationTokenSource(); |
| 131 | +추가: using var source = new MemoryStream(normal, false); |
| 132 | +추가: await Cancelled(() => new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => ["3"], (_, _, _) => |
| 133 | +추가: { |
| 134 | +추가: cts.Cancel(); |
| 135 | +추가: return ValueTask.CompletedTask; |
| 136 | +추가: }, cts.Token)); |
| 137 | +추가: })); |
| 138 | +추가: tests.Add(("duration.cumulative", async () => |
| 139 | +추가: { |
| 140 | +추가: var time = new TestTime(); |
| 141 | +추가: using var source = new MemoryStream(normal, false); |
| 142 | +추가: using var sink = new HashSink(); |
| 143 | +추가: await Error(ArchiveError.BudgetExceeded, () => new UstarReader(defaults, FileNamePolicy.Linux, time).ReadAsync(source, _ => |
| 144 | +추가: { |
| 145 | +추가: time.Ticks = TimeSpan.FromSeconds(15).Ticks; |
| 146 | +추가: return ["1"]; |
| 147 | +추가: }, sink.Write, default)); |
| 148 | +추가: Check(sink.Calls == 0); |
| 149 | +추가: })); |
| 150 | +추가: tests.Add(("input.changed.between.passes", async () => |
| 151 | +추가: { |
| 152 | +추가: var bytes = Fixtures.Archive(new Fixture("a", "old"u8.ToArray())); |
| 153 | +추가: using var source = new MemoryStream(bytes, false); |
| 154 | +추가: using var sink = new HashSink(); |
| 155 | +추가: await Error(ArchiveError.IntegrityFailure, () => new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => |
| 156 | +추가: { |
| 157 | +추가: bytes[512] ^= 1; |
| 158 | +추가: return ["0"]; |
| 159 | +추가: }, sink.Write, default)); |
| 160 | +추가: Check(sink.Calls > 0); |
| 161 | +추가: })); |
| 162 | +추가: tests.Add(("input.unselected.changed", async () => |
| 163 | +추가: { |
| 164 | +추가: var bytes = Fixtures.Archive(new Fixture("a", [1]), new Fixture("b", [2])); |
| 165 | +추가: using var source = new MemoryStream(bytes, false); |
| 166 | +추가: using var sink = new HashSink(); |
| 167 | +추가: await Error(ArchiveError.IntegrityFailure, () => new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => |
| 168 | +추가: { |
| 169 | +추가: bytes[1536] ^= 1; |
| 170 | +추가: return ["0"]; |
| 171 | +추가: }, sink.Write, default)); |
| 172 | +추가: })); |
| 173 | +추가: tests.Add(("single.use", async () => |
| 174 | +추가: { |
| 175 | +추가: var reader = new UstarReader(defaults, FileNamePolicy.Linux); |
| 176 | +추가: using var source = new MemoryStream(normal, false); |
| 177 | +추가: using var sink = new HashSink(); |
| 178 | +추가: await reader.ReadAsync(source, _ => [], sink.Write, default); |
| 179 | +추가: try { await reader.ReadAsync(source, _ => [], sink.Write, default); } |
| 180 | +추가: catch (InvalidOperationException) { return; } |
| 181 | +추가: throw new Exception("Reuse accepted."); |
| 182 | +추가: })); |
| 183 | +추가: |
| 184 | +추가: tests.Add(("metadata.exact.boundary", async () => |
| 185 | +추가: { |
| 186 | +추가: using var source = new MemoryStream(normal, false); |
| 187 | +추가: using var sink = new HashSink(); |
| 188 | +추가: var result = await new UstarReader(defaults with { MaxMetadataBytes = 2048 }, FileNamePolicy.Linux) |
| 189 | +추가: .ReadAsync(source, _ => ["0"], sink.Write, default); |
| 190 | +추가: Check(result.Entries.Count == 4 && sink.Calls == 0); |
| 191 | +추가: })); |
| 192 | +추가: tests.Add(("input.limit.exact", async () => |
| 193 | +추가: { |
| 194 | +추가: using var source = new MemoryStream(normal, false); |
| 195 | +추가: using var sink = new HashSink(); |
| 196 | +추가: await new UstarReader(defaults with { MaxInputBytes = normal.Length }, FileNamePolicy.Linux) |
| 197 | +추가: .ReadAsync(source, _ => [], sink.Write, default); |
| 198 | +추가: })); |
| 199 | +추가: tests.Add(("sink.failure.propagates", async () => |
| 200 | +추가: { |
| 201 | +추가: using var source = new MemoryStream(normal, false); |
| 202 | +추가: var expected = new IOException("Test sink failure."); |
| 203 | +추가: try |
| 204 | +추가: { |
| 205 | +추가: await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => ["1"], |
| 206 | +추가: (_, _, _) => throw expected, default); |
| 207 | +추가: } |
| 208 | +추가: catch (IOException error) when (ReferenceEquals(error, expected)) { Check(source.CanRead); return; } |
| 209 | +추가: throw new Exception("Sink failure suppressed."); |
| 210 | +추가: })); |
| 211 | +추가: tests.Add(("nonseekable.rejected", async () => |
| 212 | +추가: { |
| 213 | +추가: using var source = new NonSeekableStream(normal); |
| 214 | +추가: using var sink = new HashSink(); |
| 215 | +추가: try { await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => [], sink.Write, default); } |
| 216 | +추가: catch (ArgumentException) { Check(sink.Calls == 0); return; } |
| 217 | +추가: throw new Exception("Nonseekable input accepted."); |
| 218 | +추가: })); |
| 219 | +추가: tests.Add(("invalid.limits", () => |
| 220 | +추가: { |
| 221 | +추가: foreach (var limits in new[] |
| 222 | +추가: { |
| 223 | +추가: defaults with { MaxMetadataBytes = 0 }, defaults with { MaxInputBytes = 0 }, |
| 224 | +추가: defaults with { Job = defaults.Job with { MaxEntries = 0 } }, |
| 225 | +추가: defaults with { Job = defaults.Job with { MaxDuration = TimeSpan.Zero } } |
| 226 | +추가: }) |
| 227 | +추가: { |
| 228 | +추가: try { _ = new UstarReader(limits, FileNamePolicy.Linux); } |
| 229 | +추가: catch (ArgumentOutOfRangeException) { continue; } |
| 230 | +추가: throw new Exception("Invalid limits accepted."); |
| 231 | +추가: } |
| 232 | +추가: return Task.CompletedTask; |
| 233 | +추가: })); |
| 234 | +추가: |
| 235 | +추가: var longPrefix = new string('p', 70) + "/" + new string('q', 70); |
| 236 | +추가: var longLeaf = new string('a', 80); |
| 237 | +추가: foreach (var policy in new[] { FileNamePolicy.Linux, FileNamePolicy.Windows }) |
| 238 | +추가: { |
| 239 | +추가: Reject("long.exact." + policy, Fixtures.Archive( |
| 240 | +추가: new Fixture(longLeaf, [], Prefix: longPrefix), new Fixture(longLeaf, [], Prefix: longPrefix)), |
| 241 | +추가: ArchiveError.OutputConflict, policy: policy); |
| 242 | +추가: Reject("long.parent.first." + policy, Fixtures.Archive( |
| 243 | +추가: new Fixture(longLeaf, [], Prefix: longPrefix), new Fixture(longLeaf + "/child", [], Prefix: longPrefix)), |
| 244 | +추가: ArchiveError.OutputConflict, policy: policy); |
| 245 | +추가: Reject("long.child.first." + policy, Fixtures.Archive( |
| 246 | +추가: new Fixture(longLeaf + "/child", [], Prefix: longPrefix), new Fixture(longLeaf, [], Prefix: longPrefix)), |
| 247 | +추가: ArchiveError.OutputConflict, policy: policy); |
| 248 | +추가: Reject("long.directory.file.same." + policy, Fixtures.Archive( |
| 249 | +추가: new Fixture(longLeaf + "/", [], '5', longPrefix), new Fixture(longLeaf, [], Prefix: longPrefix)), |
| 250 | +추가: ArchiveError.OutputConflict, policy: policy); |
| 251 | +추가: foreach (var reverse in new[] { false, true }) |
| 252 | +추가: { |
| 253 | +추가: tests.Add(($"long.siblings.directories.{policy}.{reverse}", async () => |
| 254 | +추가: { |
| 255 | +추가: var entries = new[] |
| 256 | +추가: { |
| 257 | +추가: new Fixture(longLeaf, [1], Prefix: longPrefix), |
| 258 | +추가: new Fixture(longLeaf + "x", [2], Prefix: longPrefix), |
| 259 | +추가: new Fixture(longLeaf + "-dir/", [], '5', longPrefix), |
| 260 | +추가: new Fixture(longLeaf + "-dir/child", [3], Prefix: longPrefix) |
| 261 | +추가: }; |
| 262 | +추가: if (reverse) { Array.Reverse(entries); } |
| 263 | +추가: using var source = new MemoryStream(Fixtures.Archive(entries), false); |
| 264 | +추가: using var sink = new HashSink(); |
| 265 | +추가: var result = await new UstarReader(defaults, policy).ReadAsync(source, |
| 266 | +추가: listed => listed.Select(e => e.Id).ToArray(), sink.Write, default); |
| 267 | +추가: Check(result.Entries.Count == 4 && result.Usage.OutputBytes == 3 && sink.Counts.Count == 3); |
| 268 | +추가: })); |
| 269 | +추가: } |
| 270 | +추가: tests.Add(("long.maximum.siblings." + policy, async () => |
| 271 | +추가: { |
| 272 | +추가: var entries = Enumerable.Range(0, 1024).Select(i => |
| 273 | +추가: new Fixture(longLeaf + i.ToString("D4", System.Globalization.CultureInfo.InvariantCulture), [(byte)i], Prefix: longPrefix)).ToArray(); |
| 274 | +추가: using var source = new MemoryStream(Fixtures.Archive(entries), false); |
| 275 | +추가: using var sink = new HashSink(); |
| 276 | +추가: var result = await new UstarReader(defaults, policy).ReadAsync(source, _ => ["1023"], sink.Write, default); |
| 277 | +추가: Check(result.Entries.Count == 1024 && result.Usage.Entries == 1024 && result.Usage.OutputBytes == 1); |
| 278 | +추가: Check(result.Usage.ProcessedBytes == source.Length * 2 && sink.Counts.Count == 1 && sink.Counts["1023"] == 1); |
| 279 | +추가: Check(sink.Digest("1023") == Convert.ToHexString(SHA256.HashData(new byte[] { 255 }))); |
| 280 | +추가: })); |
| 281 | +추가: } |
| 282 | +추가: foreach (var childFirst in new[] { false, true }) |
| 283 | +추가: { |
| 284 | +추가: var entries = new[] |
| 285 | +추가: { |
| 286 | +추가: new Fixture(longLeaf.ToUpperInvariant(), [], Prefix: longPrefix.ToUpperInvariant()), |
| 287 | +추가: new Fixture(longLeaf + "/child", [], Prefix: longPrefix) |
| 288 | +추가: }; |
| 289 | +추가: if (childFirst) { Array.Reverse(entries); } |
| 290 | +추가: var bytes = Fixtures.Archive(entries); |
| 291 | +추가: Reject("long.case.parent.windows." + childFirst, bytes, ArchiveError.OutputConflict, policy: FileNamePolicy.Windows); |
| 292 | +추가: tests.Add(("long.case.parent.linux." + childFirst, async () => { await Run(bytes); })); |
| 293 | +추가: } |
| 294 | +추가: var caseNames = Fixtures.Archive( |
| 295 | +추가: new Fixture(longLeaf, [], Prefix: longPrefix), |
| 296 | +추가: new Fixture(longLeaf.ToUpperInvariant(), [], Prefix: longPrefix.ToUpperInvariant())); |
| 297 | +추가: Reject("long.case.exact.windows", caseNames, ArchiveError.OutputConflict, policy: FileNamePolicy.Windows); |
| 298 | +추가: tests.Add(("long.case.exact.linux", async () => { await Run(caseNames); })); |
| 299 | +추가: |
| 300 | +추가: var failures = 0; |
| 301 | +추가: foreach (var test in tests) |
| 302 | +추가: { |
| 303 | +추가: try { await test.Run(); Console.WriteLine("PASS " + test.Name); } |
| 304 | +추가: catch (Exception error) { failures++; Console.WriteLine("FAIL " + test.Name + " " + error); } |
| 305 | +추가: } |
| 306 | +추가: Console.WriteLine($"total={tests.Count} failed={failures}"); |
| 307 | +추가: return failures == 0 ? 0 : 1; |
| 308 | +추가: |
| 309 | +추가: void Reject(string name, byte[] bytes, ArchiveError code, bool checksum = true, UstarLimits? limits = null, |
| 310 | +추가: FileNamePolicy policy = FileNamePolicy.Linux, Func<IReadOnlyList<ArchiveEntry>, IReadOnlyCollection<string>>? selection = null, |
| 311 | +추가: bool expectNoOutput = true) |
| 312 | +추가: { |
| 313 | +추가: if (checksum && bytes.Length >= 512) { Fixtures.Checksum(bytes.AsSpan(0, 512)); } |
| 314 | +추가: tests.Add((name, async () => |
| 315 | +추가: { |
| 316 | +추가: using var source = new MemoryStream(bytes, false); |
| 317 | +추가: using var sink = new HashSink(); |
| 318 | +추가: await Error(code, () => new UstarReader(limits ?? defaults, policy).ReadAsync(source, |
| 319 | +추가: selection ?? (entries => entries.Where(e => e.Kind == EntryKind.File).Select(e => e.Id).ToArray()), sink.Write, default)); |
| 320 | +추가: if (expectNoOutput) { Check(sink.Calls == 0); } |
| 321 | +추가: })); |
| 322 | +추가: } |
| 323 | +추가: |
| 324 | +추가: void RejectSizeEncoding(string name, byte[] size, byte[] data) |
| 325 | +추가: { |
| 326 | +추가: Check(size.Length == 12); |
| 327 | +추가: var valid = Fixtures.Archive(new Fixture("number.bin", data)); |
| 328 | +추가: tests.Add((name + ".valid", async () => |
| 329 | +추가: { |
| 330 | +추가: using var source = new MemoryStream(valid, false); |
| 331 | +추가: using var sink = new HashSink(); |
| 332 | +추가: var result = await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => ["0"], sink.Write, default); |
| 333 | +추가: Check(result.Entries.Count == 1 && result.Entries[0].Kind == EntryKind.File); |
| 334 | +추가: Check(result.Entries[0].DeclaredSize == data.LongLength && sink.Counts["0"] == data.LongLength); |
| 335 | +추가: Check(sink.Digest("0") == Convert.ToHexString(SHA256.HashData(data))); |
| 336 | +추가: })); |
| 337 | +추가: // size 이외의 구조는 정상 대조군과 같으며 헤더 체크섬만 다시 계산함. |
| 338 | +추가: var invalid = (byte[])valid.Clone(); |
| 339 | +추가: size.CopyTo(invalid, 124); |
| 340 | +추가: Reject(name, invalid, ArchiveError.IntegrityFailure); |
| 341 | +추가: } |
| 342 | +추가: |
| 343 | +추가: async Task<UstarResult> Run(byte[] bytes, CancellationToken token = default) |
| 344 | +추가: { |
| 345 | +추가: using var source = new MemoryStream(bytes, false); |
| 346 | +추가: using var sink = new HashSink(); |
| 347 | +추가: return await new UstarReader(defaults, FileNamePolicy.Linux).ReadAsync(source, _ => [], sink.Write, token); |
| 348 | +추가: } |
| 349 | +추가: |
| 350 | +추가: static async Task Error(ArchiveError expected, Func<Task> action) |
| 351 | +추가: { |
| 352 | +추가: try { await action(); } |
| 353 | +추가: catch (ArchiveException error) when (error.Code == expected) { return; } |
| 354 | +추가: throw new Exception("Expected " + expected); |
| 355 | +추가: } |
| 356 | +추가: |
| 357 | +추가: static async Task Cancelled(Func<Task> action) |
| 358 | +추가: { |
| 359 | +추가: try { await action(); } |
| 360 | +추가: catch (OperationCanceledException) { return; } |
| 361 | +추가: throw new Exception("Expected cancellation."); |
| 362 | +추가: } |
| 363 | +추가: |
| 364 | +추가: static void Check(bool condition) { if (!condition) { throw new Exception("Assertion failed."); } } |
| 365 | +추가: static byte[] Mutate(byte[] bytes, int index, byte value) { var copy = (byte[])bytes.Clone(); copy[index] = value; return copy; } |
| 366 | +추가: static byte[] Field(byte[] bytes, int offset, string value) |
| 367 | +추가: { |
| 368 | +추가: var copy = (byte[])bytes.Clone(); |
| 369 | +추가: Encoding.ASCII.GetBytes(value).CopyTo(copy, offset); |
| 370 | +추가: return copy; |
| 371 | +추가: } |
| 372 | +추가: |
| 373 | +추가: internal sealed record Fixture(string Name, byte[] Data, char Type = '0', string Prefix = ""); |
| 374 | +추가: |
| 375 | +추가: internal static class Fixtures |
| 376 | +추가: { |
| 377 | +추가: internal static byte[] Archive(params Fixture[] entries) |
| 378 | +추가: { |
| 379 | +추가: using var stream = new MemoryStream(); |
| 380 | +추가: foreach (var entry in entries) |
| 381 | +추가: { |
| 382 | +추가: var header = new byte[512]; |
| 383 | +추가: var name = Encoding.UTF8.GetBytes(entry.Name); |
| 384 | +추가: var prefix = Encoding.UTF8.GetBytes(entry.Prefix); |
| 385 | +추가: if (name.Length > 100 || prefix.Length > 155) { throw new ArgumentException("Fixture name too long."); } |
| 386 | +추가: name.CopyTo(header, 0); |
| 387 | +추가: prefix.CopyTo(header, 345); |
| 388 | +추가: Number(header, 100, 8, 420); |
| 389 | +추가: Number(header, 108, 8, 0); |
| 390 | +추가: Number(header, 116, 8, 0); |
| 391 | +추가: Number(header, 124, 12, entry.Data.LongLength); |
| 392 | +추가: Number(header, 136, 12, 0); |
| 393 | +추가: header[156] = (byte)entry.Type; |
| 394 | +추가: "ustar\000"u8.CopyTo(header.AsSpan(257)); |
| 395 | +추가: Checksum(header); |
| 396 | +추가: stream.Write(header); |
| 397 | +추가: stream.Write(entry.Data); |
| 398 | +추가: stream.Write(new byte[(512 - entry.Data.Length % 512) % 512]); |
| 399 | +추가: } |
| 400 | +추가: stream.Write(new byte[1024]); |
| 401 | +추가: return stream.ToArray(); |
| 402 | +추가: } |
| 403 | +추가: |
| 404 | +추가: internal static void Checksum(Span<byte> header) |
| 405 | +추가: { |
| 406 | +추가: header.Slice(148, 8).Fill(32); |
| 407 | +추가: var sum = 0; |
| 408 | +추가: foreach (var b in header) { sum += b; } |
| 409 | +추가: Encoding.ASCII.GetBytes(Convert.ToString(sum, 8).PadLeft(6, '0')).CopyTo(header.Slice(148, 6)); |
| 410 | +추가: header[154] = 0; |
| 411 | +추가: header[155] = 32; |
| 412 | +추가: } |
| 413 | +추가: |
| 414 | +추가: private static void Number(byte[] bytes, int offset, int width, long value) => |
| 415 | +추가: Encoding.ASCII.GetBytes(Convert.ToString(value, 8).PadLeft(width - 1, '0') + "\0").CopyTo(bytes, offset); |
| 416 | +추가: } |
| 417 | +추가: |
| 418 | +추가: internal sealed class HashSink : IDisposable |
| 419 | +추가: { |
| 420 | +추가: private readonly Dictionary<string, IncrementalHash> hashes = new(); |
| 421 | +추가: internal Dictionary<string, long> Counts { get; } = new(); |
| 422 | +추가: internal int Calls { get; private set; } |
| 423 | +추가: internal int MaxChunk { get; private set; } |
| 424 | +추가: |
| 425 | +추가: internal ValueTask Write(ArchiveEntry entry, ReadOnlyMemory<byte> bytes, CancellationToken token) |
| 426 | +추가: { |
| 427 | +추가: token.ThrowIfCancellationRequested(); |
| 428 | +추가: if (!hashes.TryGetValue(entry.Id, out var hash)) |
| 429 | +추가: { |
| 430 | +추가: hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); |
| 431 | +추가: hashes.Add(entry.Id, hash); |
| 432 | +추가: Counts.Add(entry.Id, 0); |
| 433 | +추가: } |
| 434 | +추가: hash.AppendData(bytes.Span); |
| 435 | +추가: Counts[entry.Id] += bytes.Length; |
| 436 | +추가: Calls++; |
| 437 | +추가: MaxChunk = Math.Max(MaxChunk, bytes.Length); |
| 438 | +추가: return ValueTask.CompletedTask; |
| 439 | +추가: } |
| 440 | +추가: |
| 441 | +추가: internal string Digest(string id) => Convert.ToHexString(hashes[id].GetHashAndReset()); |
| 442 | +추가: public void Dispose() { foreach (var hash in hashes.Values) { hash.Dispose(); } } |
| 443 | +추가: } |
| 444 | +추가: |
| 445 | +추가: internal sealed class FragmentedStream(byte[] bytes, Action? onRead = null) : MemoryStream(bytes, false) |
| 446 | +추가: { |
| 447 | +추가: public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) |
| 448 | +추가: { |
| 449 | +추가: onRead?.Invoke(); |
| 450 | +추가: return base.ReadAsync(buffer[..Math.Min(buffer.Length, 97)], cancellationToken); |
| 451 | +추가: } |
| 452 | +추가: } |
| 453 | +추가: |
| 454 | +추가: internal sealed class TestTime : TimeProvider |
| 455 | +추가: { |
| 456 | +추가: internal long Ticks { get; set; } |
| 457 | +추가: public override long TimestampFrequency => TimeSpan.TicksPerSecond; |
| 458 | +추가: public override long GetTimestamp() => Ticks; |
| 459 | +추가: } |
| 460 | +추가: |
| 461 | +추가: internal sealed class NonSeekableStream(byte[] bytes) : MemoryStream(bytes, false) |
| 462 | +추가: { |
| 463 | +추가: public override bool CanSeek => false; |
| 464 | +추가: } |