-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathshadow-execute.test.ts
More file actions
191 lines (170 loc) · 6.99 KB
/
Copy pathshadow-execute.test.ts
File metadata and controls
191 lines (170 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { describe, it, expect, beforeEach } from 'vitest';
import {
createShadowExecutor,
type ProtocolPointerFetchFn,
type ResolverHttpPointerFetchFn,
} from '../src/lib/shadow-execute.js';
import { resetShadowSinkForTests, getMismatches } from '../src/lib/shadow-mismatch-sink.js';
const URL_A = 'https://issuer.example.com/r/abc123';
const DIGEST_A = 'a'.repeat(64);
const DIGEST_B = 'b'.repeat(64);
function mockProtocolFetch(impl: ProtocolPointerFetchFn): ProtocolPointerFetchFn {
return impl;
}
function mockResolverHttpFetch(impl: ResolverHttpPointerFetchFn): ResolverHttpPointerFetchFn {
return impl;
}
describe('createShadowExecutor', () => {
beforeEach(() => {
resetShadowSinkForTests({ PEAC_INTERNAL_SHADOW_BUFFER_SIZE: '64' });
});
it('returns aligned verdict and records nothing when both implementations succeed identically', async () => {
const executor = createShadowExecutor({
protocolFetch: mockProtocolFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'application/jose',
})),
resolverHttpFetch: mockResolverHttpFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'application/jose',
})),
});
const outcome = await executor(URL_A, DIGEST_A);
expect(outcome.verdict.classMatches).toBe(true);
expect(outcome.verdict.digestMatches).toBe(true);
expect(outcome.verdict.successShapeMatches).toBe(true);
expect(outcome.verdict.contentTypeWarningClassMatches).toBe(true);
expect(outcome.verdict.mismatchClasses).toEqual([]);
expect(outcome.recordedEntry).toBeUndefined();
expect(getMismatches()).toEqual([]);
});
it('records a mismatch when protocol succeeds and resolver-http fails', async () => {
const executor = createShadowExecutor({
protocolFetch: mockProtocolFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'application/jose',
})),
resolverHttpFetch: mockResolverHttpFetch(async () => ({
ok: false,
code: 'fetch_timeout',
})),
});
const outcome = await executor(URL_A, DIGEST_A);
expect(outcome.verdict.mismatchClasses).toContain('parity_class_mismatch');
expect(outcome.recordedEntry).toBeDefined();
expect(outcome.recordedEntry?.legacySummary.ok).toBe(true);
expect(outcome.recordedEntry?.shadowSummary.ok).toBe(false);
expect(outcome.recordedEntry?.shadowSummary.code).toBe('fetch_timeout');
expect(outcome.recordedEntry?.requestHash).toMatch(/^[0-9a-f]{64}$/);
expect(getMismatches().length).toBe(1);
});
it('records a digest-only mismatch when both succeed but digests differ', async () => {
const executor = createShadowExecutor({
protocolFetch: mockProtocolFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'application/jose',
})),
resolverHttpFetch: mockResolverHttpFetch(async () => ({
ok: true,
actualDigest: DIGEST_B,
contentType: 'application/jose',
})),
});
const outcome = await executor(URL_A, DIGEST_A);
expect(outcome.verdict.classMatches).toBe(true);
expect(outcome.verdict.digestMatches).toBe(false);
expect(outcome.verdict.mismatchClasses).toContain('parity_digest_mismatch');
expect(outcome.recordedEntry).toBeDefined();
});
it('records a content-type warning class mismatch on success', async () => {
const executor = createShadowExecutor({
protocolFetch: mockProtocolFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'text/html',
contentTypeWarning: 'Unexpected Content-Type: text/html; expected ...',
})),
resolverHttpFetch: mockResolverHttpFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'text/html',
})),
});
const outcome = await executor(URL_A, DIGEST_A);
expect(outcome.verdict.contentTypeWarningClassMatches).toBe(false);
expect(outcome.verdict.mismatchClasses).toContain('parity_content_type_warning_mismatch');
});
it('records a success-shape mismatch when content-type presence differs', async () => {
const executor = createShadowExecutor({
protocolFetch: mockProtocolFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
contentType: 'application/jose',
})),
resolverHttpFetch: mockResolverHttpFetch(async () => ({
ok: true,
actualDigest: DIGEST_A,
})),
});
const outcome = await executor(URL_A, DIGEST_A);
expect(outcome.verdict.successShapeMatches).toBe(false);
expect(outcome.verdict.mismatchClasses).toContain('parity_success_shape_mismatch');
});
it('runs both implementations in parallel (Promise.all)', async () => {
const order: string[] = [];
const executor = createShadowExecutor({
protocolFetch: mockProtocolFetch(async () => {
order.push('protocol-start');
await new Promise((r) => setTimeout(r, 10));
order.push('protocol-end');
return { ok: true, actualDigest: DIGEST_A };
}),
resolverHttpFetch: mockResolverHttpFetch(async () => {
order.push('shadow-start');
await new Promise((r) => setTimeout(r, 10));
order.push('shadow-end');
return { ok: true, actualDigest: DIGEST_A };
}),
});
await executor(URL_A, DIGEST_A);
expect(order.indexOf('protocol-start')).toBeLessThan(order.indexOf('protocol-end'));
expect(order.indexOf('shadow-start')).toBeLessThan(order.indexOf('shadow-end'));
// Both starts happen before either end (parallel).
const earlierEnd = Math.min(order.indexOf('protocol-end'), order.indexOf('shadow-end'));
expect(order.indexOf('protocol-start')).toBeLessThan(earlierEnd);
expect(order.indexOf('shadow-start')).toBeLessThan(earlierEnd);
});
it('records the same requestHash across two invocations of the same input', async () => {
const protocolFetch: ProtocolPointerFetchFn = async () => ({
ok: true,
actualDigest: DIGEST_A,
});
const resolverHttpFetch: ResolverHttpPointerFetchFn = async () => ({
ok: false,
code: 'fetch_timeout',
});
const executor = createShadowExecutor({ protocolFetch, resolverHttpFetch });
const a = await executor(URL_A, DIGEST_A);
const b = await executor(URL_A, DIGEST_A);
expect(a.recordedEntry?.requestHash).toBe(b.recordedEntry?.requestHash);
});
it('produces different requestHash for different (url, expectedDigest) pairs', async () => {
const protocolFetch: ProtocolPointerFetchFn = async () => ({
ok: false,
reason: 'pointer_fetch_failed',
message: 'x',
});
const resolverHttpFetch: ResolverHttpPointerFetchFn = async () => ({
ok: true,
actualDigest: DIGEST_A,
});
const executor = createShadowExecutor({ protocolFetch, resolverHttpFetch });
const a = await executor(URL_A, DIGEST_A);
const b = await executor(URL_A, DIGEST_B);
expect(a.recordedEntry?.requestHash).not.toBe(b.recordedEntry?.requestHash);
});
});