[WebAuthn] Move the mock testing entrance to Internals
[WebKit-https.git] / LayoutTests / http / wpt / webauthn / public-key-credential-create-failure-local.https.html
1 <!DOCTYPE html>
2 <title>Web Authentication API: PublicKeyCredential's [[create]] failure cases with a mock local authenticator.</title>
3 <script src="/resources/testharness.js"></script>
4 <script src="/resources/testharnessreport.js"></script>
5 <script src="./resources/util.js"></script>
6 <script>
7     (async function() {
8         const userhandleBase64 = generateUserhandleBase64();
9         const privateKeyBase64 = await generatePrivateKeyBase64();
10         const credentialID = await calculateCredentialID(privateKeyBase64);
11         // Default mock configuration. Tests need to override if they need different configuration.
12         if (window.internals)
13             internals.setMockWebAuthenticationConfiguration({ local: { acceptAuthentication: false, acceptAttestation: false } });
14
15         promise_test(t => {
16             const options = {
17                 publicKey: {
18                     rp: {
19                         name: "example.com"
20                     },
21                     user: {
22                         name: "John Appleseed",
23                         id: Base64URL.parse(testUserhandleBase64),
24                         displayName: "John",
25                     },
26                     challenge: asciiToUint8Array("123456"),
27                     pubKeyCredParams: [{ type: "public-key", alg: -35 }, { type: "public-key", alg: -257 }], // ES384, RS256
28                 }
29             };
30             return promiseRejects(t, "NotSupportedError", navigator.credentials.create(options), "The platform attached authenticator doesn't support any provided PublicKeyCredentialParameters.");
31         }, "PublicKeyCredential's [[create]] with unsupported public key credential parameters in a mock local authenticator.");
32
33         promise_test(t => {
34             const options = {
35                 publicKey: {
36                     rp: {
37                         name: "example.com"
38                     },
39                     user: {
40                         name: "John Appleseed",
41                         id: Base64URL.parse(userhandleBase64),
42                         displayName: "John",
43                     },
44                     challenge: asciiToUint8Array("123456"),
45                     pubKeyCredParams: [{ type: "public-key", alg: -7 }],
46                     excludeCredentials: [{ type: "public-key", id: credentialID }]
47                 }
48             };
49             if (window.testRunner)
50                 testRunner.addTestKeyToKeychain(privateKeyBase64, testRpId, userhandleBase64);
51             return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "At least one credential matches an entry of the excludeCredentials list in the platform attached authenticator.").then(() => {
52                 if (window.testRunner)
53                     testRunner.cleanUpKeychain(testRpId, userhandleBase64);
54             });
55         }, "PublicKeyCredential's [[create]] with matched exclude credentials in a mock local authenticator.");
56
57         promise_test(t => {
58             const options = {
59                 publicKey: {
60                     rp: {
61                         name: "example.com"
62                     },
63                     user: {
64                         name: "John Appleseed",
65                         id: Base64URL.parse(userhandleBase64),
66                         displayName: "John",
67                     },
68                     challenge: asciiToUint8Array("123456"),
69                     pubKeyCredParams: [{ type: "public-key", alg: -7 }],
70                     excludeCredentials: [
71                         { type: "public-key", id: credentialID, transports: ["usb"] },
72                         { type: "public-key", id: credentialID, transports: ["nfc"] },
73                         { type: "public-key", id: credentialID, transports: ["ble"] },
74                         { type: "public-key", id: credentialID, transports: ["internal"] }
75                     ]
76                 }
77             };
78             if (window.testRunner)
79                 testRunner.addTestKeyToKeychain(privateKeyBase64, testRpId, userhandleBase64);
80             return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "At least one credential matches an entry of the excludeCredentials list in the platform attached authenticator.").then(() => {
81                 if (window.testRunner)
82                     testRunner.cleanUpKeychain(testRpId, userhandleBase64);
83             });
84         }, "PublicKeyCredential's [[create]] with matched exclude credentials in a mock local authenticator. 2nd");
85
86         promise_test(t => {
87             const options = {
88                 publicKey: {
89                     rp: {
90                         name: "example.com"
91                     },
92                     user: {
93                         name: "John Appleseed",
94                         id: Base64URL.parse(testUserhandleBase64),
95                         displayName: "John",
96                     },
97                     challenge: asciiToUint8Array("123456"),
98                     pubKeyCredParams: [{ type: "public-key", alg: -7 }]
99                 }
100             };
101             return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Couldn't get user consent.");
102         }, "PublicKeyCredential's [[create]] without user consent in a mock local authenticator.");
103
104         promise_test(t => {
105             const options = {
106                 publicKey: {
107                     rp: {
108                         name: "example.com"
109                     },
110                     user: {
111                         name: "John Appleseed",
112                         id: Base64URL.parse(testUserhandleBase64),
113                         displayName: "John",
114                     },
115                     challenge: asciiToUint8Array("123456"),
116                     pubKeyCredParams: [{ type: "public-key", alg: -7 }]
117                 }
118             };
119             if (window.internals)
120                 internals.setMockWebAuthenticationConfiguration({ local: { acceptAuthentication: true, acceptAttestation: false } });
121             return promiseRejects(t, "UnknownError", navigator.credentials.create(options), "Unknown internal error.");
122         }, "PublicKeyCredential's [[create]] without attestation in a mock local authenticator.");
123
124         promise_test(t => {
125             const options = {
126                 publicKey: {
127                     rp: {
128                         name: "example.com"
129                     },
130                     user: {
131                         name: userhandleBase64,
132                         id: Base64URL.parse(userhandleBase64),
133                         displayName: "John",
134                     },
135                     challenge: asciiToUint8Array("123456"),
136                     pubKeyCredParams: [{ type: "public-key", alg: -7 }]
137                 }
138             };
139             if (window.internals) {
140                 internals.setMockWebAuthenticationConfiguration({ local: { acceptAuthentication: true, acceptAttestation: false } });
141                 testRunner.addTestKeyToKeychain(privateKeyBase64, testRpId, userhandleBase64);
142             }
143             return promiseRejects(t, "UnknownError", navigator.credentials.create(options), "Unknown internal error.").then(() => {
144                 if (window.testRunner)
145                     assert_false(testRunner.keyExistsInKeychain(testRpId, userhandleBase64));
146             });
147         }, "PublicKeyCredential's [[create]] deleting old credential in a mock local authenticator.");
148
149         promise_test(function(t) {
150             const options = {
151                 publicKey: {
152                     rp: {
153                         name: "example.com"
154                     },
155                     user: {
156                         name: "John Appleseed",
157                         id: asciiToUint8Array("123456"),
158                         displayName: "John",
159                     },
160                     challenge: asciiToUint8Array("123456"),
161                     pubKeyCredParams: [{ type: "public-key", alg: -7 }],
162                     timeout: 10,
163                     authenticatorSelection: { authenticatorAttachment: "cross-platform" }
164                 }
165             };
166
167             if (window.internals)
168                 internals.setMockWebAuthenticationConfiguration({ local: { acceptAuthentication: false, acceptAttestation: false } });
169             return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Operation timed out.");
170         }, "PublicKeyCredential's [[create]] with timeout in a mock local authenticator.");
171     })();
172 </script>