team-ljyzipelo

공개
141줄5.9 KB
1using Zipelo.GzipBoundaryProbe;
2
3internal static class BudgetRegression
4{
5 public static void Register(Action<string, Action> run)
6 {
7 var malformed = Fixture.Wrap([0x00, 0x01, 0x00, 0xfe, 0xff, 0x41, 0x07], [0x41], 0);
8 var valid = Fixture.Wrap([0x01, 0x01, 0x00, 0xfe, 0xff, 0x41], [0x41], 0);
9 foreach (var selected in new[] { false, true })
10 {
11 run($"accounting.decoder-exception.{selected}", () =>
12 {
13 var budget = new ProbeBudget(new(Processed: malformed.Bytes.Length + 1));
14 var observer = new ObservedInflater();
15 var pending = new PendingValidation();
16 ExpectFailure(Failure.Deflate, () => pending.Execute(malformed.Bytes, budget, selected, inflater: observer));
17 Check(observer.TotalOut == 1 && budget.Processed == malformed.Bytes.Length + 1);
18 Check(budget.Output == 0);
19 Uncommitted(pending);
20 RejectNext(budget, selected);
21 });
22 run($"accounting.exception-over-limit.{selected}", () =>
23 {
24 var budget = new ProbeBudget(new(Processed: malformed.Bytes.Length));
25 var pending = new PendingValidation();
26 ExpectFailure(Failure.Budget, () => pending.Execute(malformed.Bytes, budget, selected));
27 Check(budget.Processed == budget.ProcessedLimit + 1);
28 Uncommitted(pending);
29 RejectNext(budget, selected);
30 });
31 run($"accounting.return-cancel.{selected}", () =>
32 {
33 using var cts = new CancellationTokenSource();
34 var budget = new ProbeBudget(new(Processed: valid.Bytes.Length + 1));
35 var observer = new ObservedInflater(cts.Cancel);
36 var pending = new PendingValidation();
37 Expect<OperationCanceledException>(() => pending.Execute(valid.Bytes, budget, selected,
38 cts.Token, inflater: observer));
39 Check(observer.TotalOut == 1 && budget.Processed == valid.Bytes.Length + 1);
40 Check(budget.Output == 0);
41 Uncommitted(pending);
42 RejectNext(budget, selected);
43 });
44 run($"accounting.return-deadline.{selected}", () =>
45 {
46 var clock = new ManualClock();
47 var budget = new ProbeBudget(new(Duration: TimeSpan.FromSeconds(1)), clock);
48 var observer = new ObservedInflater(() => clock.Advance(TimeSpan.FromSeconds(1)));
49 var pending = new PendingValidation();
50 ExpectFailure(Failure.Budget, () => pending.Execute(valid.Bytes, budget, selected, inflater: observer));
51 Check(observer.TotalOut == 1 && budget.Processed == valid.Bytes.Length + 1);
52 Check(budget.Output == 0);
53 Uncommitted(pending);
54 RejectNext(budget, selected);
55 });
56 run($"accounting.other-exception.{selected}", () =>
57 {
58 var budget = new ProbeBudget(new(Processed: valid.Bytes.Length + 1));
59 var observer = new ObservedInflater(() => throw new IOException("시험 어댑터 실패"));
60 var pending = new PendingValidation();
61 Expect<IOException>(() => pending.Execute(valid.Bytes, budget, selected, inflater: observer));
62 Check(observer.TotalOut == 1 && budget.Processed == valid.Bytes.Length + 1);
63 Check(budget.Output == 0);
64 Uncommitted(pending);
65 RejectNext(budget, selected);
66 });
67 }
68 }
69
70 private static void RejectNext(ProbeBudget budget, bool selected)
71 {
72 var processed = budget.Processed;
73 var metadata = budget.Metadata;
74 var next = new PendingValidation();
75 var observer = new ObservedInflater();
76 ExpectFailure(Failure.Budget, () => next.Execute(Fixture.Wrap([3, 0], [], 0).Bytes,
77 budget, selected, inflater: observer));
78 Check(budget.Processed == processed && budget.Output == 0);
79 Check(budget.Metadata >= metadata && observer.Calls == 0);
80 Uncommitted(next);
81 }
82
83 private static void Uncommitted(PendingValidation pending) =>
84 Check(pending.CommitCount == 0 && pending.CommittedHash == null && pending.CommittedBoundary == null);
85
86 private static void ExpectFailure(Failure failure, Action action)
87 {
88 try
89 {
90 action();
91 }
92 catch (ProbeException error) when (error.Failure == failure)
93 {
94 return;
95 }
96 throw new Exception($"예상 실패 없음: {failure}");
97 }
98
99 private static void Expect<T>(Action action) where T : Exception
100 {
101 try
102 {
103 action();
104 }
105 catch (T)
106 {
107 return;
108 }
109 throw new Exception($"예상 예외 없음: {typeof(T).Name}");
110 }
111
112 private static void Check(bool condition)
113 {
114 if (!condition)
115 {
116 throw new Exception("실패 경로의 누적 예산 검사 실패");
117 }
118 }
119}
120
121// 실제 inflater 반환 직후의 취소와 기한 경계를 시험함.
122internal sealed class ObservedInflater(Action? afterReturn = null) : IRawInflater
123{
124 private readonly SharpInflater inner = new();
125 public int Calls { get; private set; }
126 public bool IsFinished => inner.IsFinished;
127 public bool IsNeedingInput => inner.IsNeedingInput;
128 public bool IsNeedingDictionary => inner.IsNeedingDictionary;
129 public long TotalIn => inner.TotalIn;
130 public long TotalOut => inner.TotalOut;
131 public int RemainingInput => inner.RemainingInput;
132 public void SetInput(byte[] bytes, int count) => inner.SetInput(bytes, count);
133
134 public int Inflate(byte[] output)
135 {
136 Calls++;
137 var written = inner.Inflate(output);
138 afterReturn?.Invoke();
139 return written;
140 }
141}