AtlatestRepositorysigil-cloudflare
1import http from 'node:http';
2import assert from 'node:assert/strict';
3import { createHash, createHmac } from 'node:crypto';
4import { spawn } from 'node:child_process';
5
6const hash = data => createHash('sha256').update(data).digest('hex');
7const hmac = (key, data) => createHmac('sha256', key).update(data).digest();
8const objects = new Map();
9let requests = 0;
10let failure;
11const server = http.createServer(async (req, res) => {
12 try {
13 const chunks = [];
14 for await (const chunk of req) chunks.push(chunk);
15 const body = Buffer.concat(chunks);
16 requests++;
17 const auth = req.headers.authorization;
18 const match = /^AWS4-HMAC-SHA256 Credential=TESTACCESS\/(\d{8})\/auto\/s3\/aws4_request, SignedHeaders=([^,]+), Signature=([a-f0-9]{64})$/.exec(auth);
19 assert.ok(match, 'well-formed R2 authorization');
20 assert.equal(req.rawHeaders.filter(x => x.toLowerCase() === 'host').length, 1);
21 assert.equal(req.headers['x-amz-content-sha256'], hash(body));
22 const names = match[2].split(';');
23 const headers = names.map(name => `${name}:${req.headers[name]}\n`).join('');
24 const canonical = `${req.method}\n${req.url}\n\n${headers}\n${match[2]}\n${hash(body)}`;
25 const scope = `${match[1]}/auto/s3/aws4_request`;
26 const toSign = `AWS4-HMAC-SHA256\n${req.headers['x-amz-date']}\n${scope}\n${hash(canonical)}`;
27 const key = hmac(hmac(hmac(hmac('AWS4test-secret', match[1]), 'auto'), 's3'), 'aws4_request');
28 assert.equal(hmac(key, toSign).toString('hex'), match[3], 'independently verified signature');
29 if (req.url === '/test-assets/redirect') {
30 res.writeHead(307, { location: `http://127.0.0.1:${server.address().port}/must-not-follow` });
31 return res.end();
32 }
33 if (req.url === '/test-assets/denied') { res.writeHead(403); return res.end('AccessDenied'); }
34 if (req.url === '/test-assets/truncated') {
35 res.writeHead(200, { 'content-length': '10' }); return res.end('short');
36 }
37 assert.equal(req.url, '/test-assets/logos/%C3%A9%20%2B%25%E9%9B%AA.png');
38 if (req.method === 'PUT') {
39 assert.equal(req.headers['content-length'], '65536');
40 assert.equal(req.headers['content-type'], 'image/png');
41 assert.equal(req.headers['cache-control'], 'public, max-age=3600');
42 assert.deepEqual(body, Buffer.from(Array.from({ length: 65536 }, (_, i) => i % 256)));
43 objects.set(req.url, body);
44 res.writeHead(200, { etag: '"test-etag"', 'content-length': '0' }); return res.end();
45 }
46 if (req.method === 'DELETE') { objects.delete(req.url); res.writeHead(204); return res.end(); }
47 if (!objects.has(req.url)) { res.writeHead(404); return res.end('NoSuchKey'); }
48 res.writeHead(200, { 'content-length': String(objects.get(req.url).length), 'content-type': 'image/png' });
49 res.end(req.method === 'HEAD' ? undefined : objects.get(req.url));
50 } catch (error) {
51 failure ??= error;
52 res.writeHead(500); res.end('Fake service assertion failed');
53 }
54});
55await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
56try {
57 const child = spawn('sigil', ['test', '--sgl', '--strict', ...process.argv.slice(2)], {
58 stdio: 'inherit', env: { ...process.env, R2_TEST_PORT: String(server.address().port) },
59 });
60 // Allow the released toolchain to build the native test harness from cache misses.
61 const timeout = setTimeout(() => child.kill('SIGTERM'), 420_000);
62 const code = await new Promise((resolve, reject) => { child.once('error', reject); child.once('exit', resolve); });
63 clearTimeout(timeout);
64 if (failure) throw failure;
65 assert.equal(code, 0, 'Sigil tests passed');
66 assert.equal(requests, 8, 'no redirects followed and no automatic retries');
67 console.log('R2 fake-service checks passed: byte-exact upload/download, signatures, metadata, deletion and failures.');
68} finally { server.closeAllConnections(); server.close(); }