<!DOCTYPE html><!-- webkit-test-runner [ WebAuthenticationModernEnabled=true ] -->
<title>Web Authentication API: PublicKeyCredential's [[get]] failure cases.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/util.js"></script>
<script>
    promise_test(t => {
        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ silentFailure: true, local: { userVerification: "no", acceptAttestation: false } });

        const options = {
            publicKey: {
                challenge: asciiToUint8Array("123456"),
                allowCredentials: [
                    { type: "public-key", id: Base64URL.parse(testCredentialIdBase64), transports: ["usb"] },
                    { type: "public-key", id: Base64URL.parse(testCredentialIdBase64), transports: ["nfc"] },
                    { type: "public-key", id: Base64URL.parse(testCredentialIdBase64), transports: ["ble"] },
                    { type: "public-key", id: Base64URL.parse(testCredentialIdBase64), transports: ["internal"] }
                ],
                timeout: 10
            }
        };

        return promiseRejects(t, "NotAllowedError", navigator.credentials.get(options), "Operation timed out.");
    }, "PublicKeyCredential's [[get]] with no matched credentials in a mock local authenticator.");

    promise_test(async t => {
        const privateKeyBase64 = await generatePrivateKeyBase64();
        const credentialID = await calculateCredentialID(privateKeyBase64);
        const credentialIDBase64 = btoa(String.fromCharCode.apply(0, credentialID));
        // Default mock configuration. Tests need to override if they need different configuration.
        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ silentFailure: true, local: { userVerification: "no", acceptAttestation: false, preferredCredentialIdBase64: credentialIDBase64 } });

        const options = {
            publicKey: {
                challenge: asciiToUint8Array("123456"),
                allowCredentials: [
                    { type: "public-key", id: Base64URL.parse(testUserhandleBase64) }
                ],
                timeout: 10
            }
        };

        if (window.testRunner)
            testRunner.addTestKeyToKeychain(privateKeyBase64, testRpId, testUserEntityBundleBase64);
        return promiseRejects(t, "NotAllowedError", navigator.credentials.get(options), "Operation timed out.").then(() => {
                if (window.testRunner)
                    testRunner.cleanUpKeychain(testRpId, credentialIDBase64);
            });
    }, "PublicKeyCredential's [[get]] with no matched credentials in a mock local authenticator. 2nd");

    promise_test(async t => {
        const privateKeyBase64 = await generatePrivateKeyBase64();
        const credentialID = await calculateCredentialID(privateKeyBase64);
        const credentialIDBase64 = btoa(String.fromCharCode.apply(0, credentialID));
        // Default mock configuration. Tests need to override if they need different configuration.
        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ silentFailure: true, local: { userVerification: "no", acceptAttestation: false, preferredCredentialIdBase64: credentialIDBase64 } });

        const options = {
            publicKey: {
                challenge: asciiToUint8Array("123456"),
                timeout: 10
            }
        };

        if (window.testRunner)
            testRunner.addTestKeyToKeychain(privateKeyBase64, testRpId, testUserEntityBundleBase64);
        return promiseRejects(t, "NotAllowedError", navigator.credentials.get(options), "Operation timed out.").then(() => {
            if (window.testRunner)
                testRunner.cleanUpKeychain(testRpId, credentialIDBase64);
        });
    }, "PublicKeyCredential's [[get]] without user consent in a mock local authenticator.");
</script>
