Skip to content

Commit c43ca05

Browse files
Jake ChampionJakeChampion
authored andcommitted
chore: add our own tests for crypto.subtle.digest
1 parent dad74dc commit c43ca05

File tree

3 files changed

+349
-0
lines changed

3 files changed

+349
-0
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/// <reference path="../../../../../types/index.d.ts" />
2+
3+
import { env } from 'fastly:env';
4+
import { pass, fail, assert, assertThrows, assertRejects } from "../../../assertions.js";
5+
6+
addEventListener("fetch", event => {
7+
event.respondWith(app(event));
8+
});
9+
/**
10+
* @param {FetchEvent} event
11+
* @returns {Response}
12+
*/
13+
async function app(event) {
14+
try {
15+
const path = (new URL(event.request.url)).pathname;
16+
console.log(`path: ${path}`)
17+
console.log(`FASTLY_SERVICE_VERSION: ${env('FASTLY_SERVICE_VERSION')}`)
18+
if (routes.has(path)) {
19+
const routeHandler = routes.get(path);
20+
return await routeHandler();
21+
}
22+
return fail(`${path} endpoint does not exist`)
23+
} catch (error) {
24+
return fail(`The routeHandler threw an error: ${error.message}` + '\n' + error.stack)
25+
}
26+
}
27+
28+
const routes = new Map();
29+
routes.set('/', () => {
30+
routes.delete('/');
31+
let test_routes = Array.from(routes.keys())
32+
return new Response(JSON.stringify(test_routes), { 'headers': { 'content-type': 'application/json' } });
33+
});
34+
35+
let error;
36+
routes.set("/crypto", async () => {
37+
error = assert(typeof crypto, 'object', `typeof crypto`)
38+
if (error) { return error; }
39+
error = assert(crypto instanceof Crypto, true, `crypto instanceof Crypto`)
40+
if (error) { return error; }
41+
return pass();
42+
});
43+
44+
routes.set("/crypto.subtle", async () => {
45+
error = assert(typeof crypto.subtle, 'object', `typeof crypto.subtle`)
46+
if (error) { return error; }
47+
error = assert(crypto.subtle instanceof SubtleCrypto, true, `crypto.subtle instanceof SubtleCrypto`)
48+
if (error) { return error; }
49+
return pass();
50+
});
51+
52+
// digest
53+
{
54+
const enc = new TextEncoder();
55+
const data = enc.encode('hello world');
56+
routes.set("/crypto.subtle.digest", async () => {
57+
error = assert(typeof crypto.subtle.digest, 'function', `typeof crypto.subtle.digest`)
58+
if (error) { return error; }
59+
error = assert(crypto.subtle.digest, SubtleCrypto.prototype.digest, `crypto.subtle.digest === SubtleCrypto.prototype.digest`)
60+
if (error) { return error; }
61+
return pass()
62+
});
63+
routes.set("/crypto.subtle.digest/length", async () => {
64+
error = assert(crypto.subtle.digest.length, 2, `crypto.subtle.digest.length === 2`)
65+
if (error) { return error; }
66+
return pass()
67+
});
68+
routes.set("/crypto.subtle.digest/called-as-constructor", async () => {
69+
error = assertThrows(() => {
70+
new crypto.subtle.digest
71+
}, TypeError, "crypto.subtle.digest is not a constructor")
72+
if (error) { return error; }
73+
return pass()
74+
});
75+
routes.set("/crypto.subtle.digest/called-with-wrong-this", async () => {
76+
error = await assertRejects(async () => {
77+
await crypto.subtle.digest.call(undefined)
78+
}, TypeError, "Method SubtleCrypto.digest called on receiver that's not an instance of SubtleCrypto")
79+
if (error) { return error; }
80+
return pass()
81+
});
82+
routes.set("/crypto.subtle.digest/called-with-no-arguments", async () => {
83+
error = await assertRejects(async () => {
84+
await crypto.subtle.digest()
85+
}, TypeError, "SubtleCrypto.digest: At least 2 arguments required, but only 0 passed")
86+
if (error) { return error; }
87+
return pass()
88+
});
89+
90+
// first-parameter
91+
{
92+
routes.set("/crypto.subtle.digest/first-parameter-calls-7.1.17-ToString", async () => {
93+
const sentinel = Symbol("sentinel");
94+
const test = async () => {
95+
await crypto.subtle.digest({
96+
name: {
97+
toString() {
98+
throw sentinel;
99+
}
100+
}
101+
}, data);
102+
}
103+
let error = await assertRejects(test);
104+
if (error) { return error; }
105+
try {
106+
await test();
107+
} catch (thrownError) {
108+
let error = assert(thrownError, sentinel, 'thrownError === sentinel');
109+
if (error) { return error; }
110+
}
111+
return pass();
112+
});
113+
routes.set("/crypto.subtle.digest/first-parameter-non-existant-format", async () => {
114+
let error = await assertRejects(async () => {
115+
await crypto.subtle.digest('jake', data);
116+
}, Error, "Algorithm: Unrecognized name");
117+
if (error) { return error; }
118+
return pass();
119+
});
120+
}
121+
// second-parameter
122+
{
123+
routes.set("/crypto.subtle.digest/second-parameter-undefined", async () => {
124+
let error = await assertRejects(async () => {
125+
await crypto.subtle.digest("sha-1", undefined);
126+
}, Error, "SubtleCrypto.digest: data must be of type ArrayBuffer or ArrayBufferView but got \"\"");
127+
if (error) { return error; }
128+
return pass();
129+
});
130+
}
131+
// happy paths
132+
{
133+
// "SHA-1"
134+
routes.set("/crypto.subtle.digest/sha-1", async () => {
135+
const result = new Uint8Array(await crypto.subtle.digest("sha-1", new Uint8Array));
136+
const expected = new Uint8Array([218, 57, 163, 238, 94, 107, 75, 13, 50, 85, 191, 239, 149, 96, 24, 144, 175, 216, 7, 9]);
137+
error = assert(result, expected, "result deep equals expected");
138+
if (error) { return error; }
139+
return pass();
140+
});
141+
// "SHA-256"
142+
routes.set("/crypto.subtle.digest/sha-256", async () => {
143+
const result = new Uint8Array(await crypto.subtle.digest("sha-256", new Uint8Array));
144+
const expected = new Uint8Array([227, 176, 196, 66, 152, 252, 28, 20, 154, 251, 244, 200, 153, 111, 185, 36, 39, 174, 65, 228, 100, 155, 147, 76, 164, 149, 153, 27, 120, 82, 184, 85]);
145+
error = assert(result, expected, "result deep equals expected");
146+
if (error) { return error; }
147+
return pass();
148+
});
149+
// "SHA-384"
150+
routes.set("/crypto.subtle.digest/sha-384", async () => {
151+
const result = new Uint8Array(await crypto.subtle.digest("sha-384", new Uint8Array));
152+
const expected = new Uint8Array([56, 176, 96, 167, 81, 172, 150, 56, 76, 217, 50, 126, 177, 177, 227, 106, 33, 253, 183, 17, 20, 190, 7, 67, 76, 12, 199, 191, 99, 246, 225, 218, 39, 78, 222, 191, 231, 111, 101, 251, 213, 26, 210, 241, 72, 152, 185, 91]);
153+
error = assert(result, expected, "result deep equals expected");
154+
if (error) { return error; }
155+
return pass();
156+
});
157+
// "SHA-512"
158+
routes.set("/crypto.subtle.digest/sha-512", async () => {
159+
const result = new Uint8Array(await crypto.subtle.digest("sha-512", new Uint8Array));
160+
const expected = new Uint8Array([207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62]);
161+
error = assert(result, expected, "result deep equals expected");
162+
if (error) { return error; }
163+
return pass();
164+
});
165+
}
166+
167+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file describes a Fastly Compute@Edge package. To learn more visit:
2+
# https://developer.fastly.com/reference/fastly-toml/
3+
4+
authors = ["[email protected]"]
5+
description = ""
6+
language = "other"
7+
manifest_version = 2
8+
name = "crypto"
9+
service_id = ""
10+
11+
[scripts]
12+
build = "node ../../../../js-compute-runtime-cli.js"
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"GET /crypto": {
3+
"environments": [
4+
"viceroy", "c@e"
5+
],
6+
"downstream_request": {
7+
"method": "GET",
8+
"pathname": "/crypto"
9+
},
10+
"downstream_response": {
11+
"status": 200
12+
}
13+
},
14+
"GET /crypto.subtle": {
15+
"environments": [
16+
"viceroy", "c@e"
17+
],
18+
"downstream_request": {
19+
"method": "GET",
20+
"pathname": "/crypto.subtle"
21+
},
22+
"downstream_response": {
23+
"status": 200
24+
}
25+
},
26+
"GET /crypto.subtle.digest": {
27+
"environments": [
28+
"viceroy", "c@e"
29+
],
30+
"downstream_request": {
31+
"method": "GET",
32+
"pathname": "/crypto.subtle.digest"
33+
},
34+
"downstream_response": {
35+
"status": 200
36+
}
37+
},
38+
"GET /crypto.subtle.digest/length": {
39+
"environments": [
40+
"viceroy", "c@e"
41+
],
42+
"downstream_request": {
43+
"method": "GET",
44+
"pathname": "/crypto.subtle.digest/length"
45+
},
46+
"downstream_response": {
47+
"status": 200
48+
}
49+
},
50+
"GET /crypto.subtle.digest/called-as-constructor": {
51+
"environments": [
52+
"viceroy", "c@e"
53+
],
54+
"downstream_request": {
55+
"method": "GET",
56+
"pathname": "/crypto.subtle.digest/called-as-constructor"
57+
},
58+
"downstream_response": {
59+
"status": 200
60+
}
61+
},
62+
"GET /crypto.subtle.digest/called-with-wrong-this": {
63+
"environments": [
64+
"viceroy", "c@e"
65+
],
66+
"downstream_request": {
67+
"method": "GET",
68+
"pathname": "/crypto.subtle.digest/called-with-wrong-this"
69+
},
70+
"downstream_response": {
71+
"status": 200
72+
}
73+
},
74+
"GET /crypto.subtle.digest/called-with-no-arguments": {
75+
"environments": [
76+
"viceroy", "c@e"
77+
],
78+
"downstream_request": {
79+
"method": "GET",
80+
"pathname": "/crypto.subtle.digest/called-with-no-arguments"
81+
},
82+
"downstream_response": {
83+
"status": 200
84+
}
85+
},
86+
"GET /crypto.subtle.digest/first-parameter-calls-7.1.17-ToString": {
87+
"environments": [
88+
"viceroy", "c@e"
89+
],
90+
"downstream_request": {
91+
"method": "GET",
92+
"pathname": "/crypto.subtle.digest/first-parameter-calls-7.1.17-ToString"
93+
},
94+
"downstream_response": {
95+
"status": 200
96+
}
97+
},
98+
"GET /crypto.subtle.digest/first-parameter-non-existant-format": {
99+
"environments": [
100+
"viceroy", "c@e"
101+
],
102+
"downstream_request": {
103+
"method": "GET",
104+
"pathname": "/crypto.subtle.digest/first-parameter-non-existant-format"
105+
},
106+
"downstream_response": {
107+
"status": 200
108+
}
109+
},
110+
"GET /crypto.subtle.digest/second-parameter-undefined": {
111+
"environments": [
112+
"viceroy", "c@e"
113+
],
114+
"downstream_request": {
115+
"method": "GET",
116+
"pathname": "/crypto.subtle.digest/second-parameter-undefined"
117+
},
118+
"downstream_response": {
119+
"status": 200
120+
}
121+
},
122+
"GET /crypto.subtle.digest/sha-1": {
123+
"environments": [
124+
"viceroy", "c@e"
125+
],
126+
"downstream_request": {
127+
"method": "GET",
128+
"pathname": "/crypto.subtle.digest/sha-1"
129+
},
130+
"downstream_response": {
131+
"status": 200
132+
}
133+
},
134+
"GET /crypto.subtle.digest/sha-256": {
135+
"environments": [
136+
"viceroy", "c@e"
137+
],
138+
"downstream_request": {
139+
"method": "GET",
140+
"pathname": "/crypto.subtle.digest/sha-256"
141+
},
142+
"downstream_response": {
143+
"status": 200
144+
}
145+
},
146+
"GET /crypto.subtle.digest/sha-384": {
147+
"environments": [
148+
"viceroy", "c@e"
149+
],
150+
"downstream_request": {
151+
"method": "GET",
152+
"pathname": "/crypto.subtle.digest/sha-384"
153+
},
154+
"downstream_response": {
155+
"status": 200
156+
}
157+
},
158+
"GET /crypto.subtle.digest/sha-512": {
159+
"environments": [
160+
"viceroy", "c@e"
161+
],
162+
"downstream_request": {
163+
"method": "GET",
164+
"pathname": "/crypto.subtle.digest/sha-512"
165+
},
166+
"downstream_response": {
167+
"status": 200
168+
}
169+
}
170+
}

0 commit comments

Comments
 (0)