| 1 | using System.Reflection; |
| 2 | using System.Security.Cryptography; |
| 3 | using Zipelo.GzipBoundaryProbe; |
| 4 | |
| 5 | internal static class IntegrationProgram |
| 6 | { |
| 7 | private static int passed; |
| 8 | private static int failed; |
| 9 | |
| 10 | public static int Main(string[] args) |
| 11 | { |
| 12 | if (args.Length != 0) |
| 13 | { |
| 14 | return 2; |
| 15 | } |
| 16 | |
| 17 | Console.WriteLine("candidate.inherited-suite.begin"); |
| 18 | var inheritedExit = Program.Main([]); |
| 19 | Console.WriteLine($"candidate.inherited-suite.exit={inheritedExit}"); |
| 20 | Run("assembly.identity", () => |
| 21 | { |
| 22 | var field = typeof(SharpInflater).GetField("inflater", BindingFlags.Instance | BindingFlags.NonPublic); |
| 23 | Check(field?.FieldType == typeof(Inflater)); |
| 24 | Check(typeof(Inflater).Assembly.GetName().Name == "Zipelo.Experimental.SharpZipLibHistoryProbe"); |
| 25 | Check(typeof(SharpZipBaseException).Assembly == typeof(Inflater).Assembly); |
| 26 | Check(typeof(ICSharpCode.SharpZipLib.Checksum.Crc32).Assembly != typeof(Inflater).Assembly); |
| 27 | }); |
| 28 | |
| 29 | var zero = Fixture.Create(new byte[3]); |
| 30 | var invalidHistory = Fixture.Wrap([3, 2, 0], new byte[3], 0); |
| 31 | var text = "123456789"u8.ToArray(); |
| 32 | var basic = Fixture.Create(text); |
| 33 | foreach (var selected in new[] { false, true }) |
| 34 | { |
| 35 | foreach (var feed in new[] { 1, 2, 3, 7, 65536 }) |
| 36 | { |
| 37 | var label = $"{selected}.{feed}"; |
| 38 | Run($"valid.controls.{label}", () => |
| 39 | { |
| 40 | Verify(zero, new byte[3], selected, feed); |
| 41 | Verify(Fixture.Wrap([3, 0], [], 0), [], selected, feed); |
| 42 | Verify(Fixture.Wrap([1, 0, 0, 255, 255], [], 0), [], selected, feed); |
| 43 | }); |
| 44 | Run($"reject.history.{label}", () => Reject(invalidHistory.Bytes, Failure.Deflate, selected, feed)); |
| 45 | Run($"reject.crc.{label}", () => Reject(Mutate(basic.Bytes, basic.Bytes.Length - 8), Failure.Crc, selected, feed)); |
| 46 | Run($"reject.size.{label}", () => Reject(Mutate(basic.Bytes, basic.Bytes.Length - 4), Failure.Size, selected, feed)); |
| 47 | Run($"reject.header.{label}", () => Reject(Mutate(basic.Bytes, 0), Failure.Header, selected, feed)); |
| 48 | Run($"reject.trailing.{label}", () => Reject([.. basic.Bytes, 0], Failure.TrailingData, selected, feed)); |
| 49 | Run($"reject.member.{label}", () => Reject([.. basic.Bytes, .. zero.Bytes], Failure.TrailingData, selected, feed)); |
| 50 | Run($"reject.trailer-truncation.{label}", () => Reject(basic.Bytes[..^1], Failure.Truncated, selected, feed)); |
| 51 | } |
| 52 | |
| 53 | Run($"final.cancel.{selected}", () => |
| 54 | { |
| 55 | using var cts = new CancellationTokenSource(); |
| 56 | var pending = new PendingValidation(); |
| 57 | Expect<OperationCanceledException>(() => pending.Execute(basic.Bytes, new(new()), selected, |
| 58 | cts.Token, beforeCommit: cts.Cancel)); |
| 59 | Uncommitted(pending); |
| 60 | }); |
| 61 | Run($"final.deadline.{selected}", () => |
| 62 | { |
| 63 | var clock = new ManualClock(); |
| 64 | var pending = new PendingValidation(); |
| 65 | var budget = new ProbeBudget(new(Duration: TimeSpan.FromSeconds(1)), clock); |
| 66 | ExpectFailure(Failure.Budget, () => pending.Execute(basic.Bytes, budget, selected, |
| 67 | beforeCommit: () => clock.Advance(TimeSpan.FromSeconds(1)))); |
| 68 | Uncommitted(pending); |
| 69 | }); |
| 70 | Run($"cumulative.processed.{selected}", () => |
| 71 | { |
| 72 | var cost = (long)basic.Bytes.Length + text.Length; |
| 73 | var budget = new ProbeBudget(new(Processed: cost)); |
| 74 | var first = new PendingValidation(); |
| 75 | first.Execute(basic.Bytes, budget, selected); |
| 76 | Check(first.CommitCount == 1 && budget.Processed == cost); |
| 77 | var second = new PendingValidation(); |
| 78 | ExpectFailure(Failure.Budget, () => second.Execute(basic.Bytes, budget, selected)); |
| 79 | Uncommitted(second); |
| 80 | Check(budget.Processed == cost); |
| 81 | }); |
| 82 | Run($"cumulative.time.{selected}", () => |
| 83 | { |
| 84 | var clock = new ManualClock(); |
| 85 | var budget = new ProbeBudget(new(Duration: TimeSpan.FromSeconds(1)), clock); |
| 86 | new PendingValidation().Execute(basic.Bytes, budget, selected); |
| 87 | clock.Advance(TimeSpan.FromSeconds(1)); |
| 88 | var second = new PendingValidation(); |
| 89 | ExpectFailure(Failure.Budget, () => second.Execute(basic.Bytes, budget, selected)); |
| 90 | Uncommitted(second); |
| 91 | }); |
| 92 | Run($"failure.discard.{selected}", () => |
| 93 | { |
| 94 | var failedAttempt = Reject(invalidHistory.Bytes, Failure.Deflate, selected, 1); |
| 95 | Expect<InvalidOperationException>(() => failedAttempt.Execute(zero.Bytes, new(new()), selected)); |
| 96 | Uncommitted(failedAttempt); |
| 97 | Verify(zero, new byte[3], selected, 1); |
| 98 | }); |
| 99 | } |
| 100 | |
| 101 | foreach (byte flags in Enumerable.Range(0, 32).Select(value => (byte)value)) |
| 102 | { |
| 103 | Run($"unselected.flags.{flags}", () => Verify(Fixture.Create(text, flags), text, false, 1)); |
| 104 | } |
| 105 | Run("unselected.all-prefixes", () => |
| 106 | { |
| 107 | var optional = Fixture.Create(text, 31); |
| 108 | for (var length = 0; length < optional.Bytes.Length; length++) |
| 109 | { |
| 110 | var pending = new PendingValidation(); |
| 111 | try |
| 112 | { |
| 113 | pending.Execute(optional.Bytes.AsMemory(0, length), new(new()), false, feedSize: 1); |
| 114 | } |
| 115 | catch (ProbeException error) when (error.Failure is Failure.Header or Failure.Truncated |
| 116 | or Failure.Deflate or Failure.Crc or Failure.Size) |
| 117 | { |
| 118 | Uncommitted(pending); |
| 119 | continue; |
| 120 | } |
| 121 | throw new Exception("잘린 입력이 거부되지 않음"); |
| 122 | } |
| 123 | }); |
| 124 | Run("pending.output-failure", () => |
| 125 | { |
| 126 | var pending = new PendingValidation(); |
| 127 | var sentinel = new IOException("시험 수신기 실패"); |
| 128 | try |
| 129 | { |
| 130 | pending.Execute(basic.Bytes, new(new()), true, afterChunk: () => throw sentinel); |
| 131 | } |
| 132 | catch (IOException error) when (ReferenceEquals(error, sentinel)) |
| 133 | { |
| 134 | Uncommitted(pending); |
| 135 | return; |
| 136 | } |
| 137 | throw new Exception("수신기 실패가 전파되지 않음"); |
| 138 | }); |
| 139 | Run("pending.output-cancel", () => |
| 140 | { |
| 141 | using var cts = new CancellationTokenSource(); |
| 142 | var pending = new PendingValidation(); |
| 143 | Expect<OperationCanceledException>(() => pending.Execute(basic.Bytes, new(new()), true, |
| 144 | cts.Token, afterChunk: cts.Cancel)); |
| 145 | Uncommitted(pending); |
| 146 | }); |
| 147 | |
| 148 | BudgetRegression.Register(Run); |
| 149 | FinalizationRegression.Register(Run); |
| 150 | Console.WriteLine($"candidate.integration passed={passed} failed={failed}"); |
| 151 | return inheritedExit == 0 && failed == 0 ? 0 : 1; |
| 152 | } |
| 153 | |
| 154 | private static void Verify(Fixture fixture, byte[] plain, bool selected, int feed) |
| 155 | { |
| 156 | var originalHash = SHA256.HashData(fixture.Bytes); |
| 157 | var budget = new ProbeBudget(new(Output: selected ? plain.Length : 0)); |
| 158 | var pending = new PendingValidation(); |
| 159 | pending.Execute(fixture.Bytes, budget, selected, feedSize: feed); |
| 160 | var result = pending.CommittedBoundary ?? throw new Exception("확정 경계 없음"); |
| 161 | Check(pending.CommitCount == 1); |
| 162 | Check(pending.CommittedHash == (selected ? Convert.ToHexString(SHA256.HashData(plain)) : null)); |
| 163 | Check(result.HeaderBytes == fixture.HeaderLength && result.DeflateBytes == fixture.RawLength); |
| 164 | Check(result.MemberBytes == fixture.Bytes.Length && result.InflatedBytes == plain.Length); |
| 165 | Check(result.InflaterTotalOut == plain.Length); |
| 166 | Check(result.OfferedBytes - result.ReadAheadBytes == result.DeflateBytes); |
| 167 | Check(budget.Processed == (long)fixture.Bytes.Length + plain.Length); |
| 168 | Check(budget.Output == (selected ? plain.Length : 0) && budget.Metadata == fixture.HeaderLength); |
| 169 | Check(originalHash.AsSpan().SequenceEqual(SHA256.HashData(fixture.Bytes))); |
| 170 | } |
| 171 | |
| 172 | private static PendingValidation Reject(byte[] input, Failure expected, bool selected, int feed) |
| 173 | { |
| 174 | var originalHash = SHA256.HashData(input); |
| 175 | var pending = new PendingValidation(); |
| 176 | ExpectFailure(expected, () => pending.Execute(input, new(new()), selected, feedSize: feed)); |
| 177 | Uncommitted(pending); |
| 178 | Check(originalHash.AsSpan().SequenceEqual(SHA256.HashData(input))); |
| 179 | return pending; |
| 180 | } |
| 181 | |
| 182 | private static byte[] Mutate(byte[] input, int index) |
| 183 | { |
| 184 | var copy = (byte[])input.Clone(); |
| 185 | copy[index] ^= 1; |
| 186 | return copy; |
| 187 | } |
| 188 | |
| 189 | private static void Uncommitted(PendingValidation pending) => |
| 190 | Check(pending.CommitCount == 0 && pending.CommittedHash == null && pending.CommittedBoundary == null); |
| 191 | |
| 192 | private static void ExpectFailure(Failure expected, Action action) |
| 193 | { |
| 194 | try |
| 195 | { |
| 196 | action(); |
| 197 | } |
| 198 | catch (ProbeException error) when (error.Failure == expected) |
| 199 | { |
| 200 | return; |
| 201 | } |
| 202 | throw new Exception($"예상 거부 없음: {expected}"); |
| 203 | } |
| 204 | |
| 205 | private static void Expect<T>(Action action) where T : Exception |
| 206 | { |
| 207 | try |
| 208 | { |
| 209 | action(); |
| 210 | } |
| 211 | catch (T) |
| 212 | { |
| 213 | return; |
| 214 | } |
| 215 | throw new Exception($"예상 예외 없음: {typeof(T).Name}"); |
| 216 | } |
| 217 | |
| 218 | private static void Check(bool condition) |
| 219 | { |
| 220 | if (!condition) |
| 221 | { |
| 222 | throw new Exception("검사 실패"); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | private static void Run(string name, Action action) |
| 227 | { |
| 228 | try |
| 229 | { |
| 230 | action(); |
| 231 | passed++; |
| 232 | Console.WriteLine($"PASS integration.{name}"); |
| 233 | } |
| 234 | catch (Exception error) |
| 235 | { |
| 236 | failed++; |
| 237 | Console.WriteLine($"FAIL integration.{name}: {error.GetType().Name}: {error.Message}"); |
| 238 | } |
| 239 | } |
| 240 | } |