<!DOCTYPE html><!-- webkit-test-runner [ WebAuthenticationModernEnabled=true ] -->
<title>Web Authentication API: PublicKeyCredential's [[create]] failure cases with a mock nfc authenticator.</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./resources/util.js"></script>
<script>
    // Default mock configuration. Tests need to override it if they need different configuration.
    if (window.internals)
        internals.setMockWebAuthenticationConfiguration({ nfc: { error: "no-tags" } });

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10,
                authenticatorSelection: { authenticatorAttachment: "platform" }
            }
        };

        return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "This request has been cancelled by the user.");
    }, "PublicKeyCredential's [[create]] with timeout in a mock nfc authenticator.");

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10
            }
        };

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

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10
            }
        };

        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ nfc: { error: "wrong-tag-type" } });
        return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Operation timed out.");
    }, "PublicKeyCredential's [[create]] with wrong tag type in a mock nfc authenticator.");

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10
            }
        };

        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ nfc: { error: "no-connections" } });
        return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Operation timed out.");
    }, "PublicKeyCredential's [[create]] with no connections in a mock nfc authenticator.");

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10
            }
        };

        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ nfc: { error: "malicious-payload", payloadBase64: [ ] } });
        return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Operation timed out.");
    }, "PublicKeyCredential's [[create]] with null version in a mock nfc authenticator.");

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10
            }
        };

        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ nfc: { error: "malicious-payload", payloadBase64:[testDummyMessagePayloadBase64] } });
        return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Operation timed out.");
    }, "PublicKeyCredential's [[create]] with wrong version in a mock nfc authenticator.");

    promise_test(function(t) {
        const options = {
            publicKey: {
                rp: {
                    name: "example.com"
                },
                user: {
                    name: "John Appleseed",
                    id: asciiToUint8Array("123456"),
                    displayName: "John",
                },
                challenge: asciiToUint8Array("123456"),
                pubKeyCredParams: [{ type: "public-key", alg: -7 }],
                timeout: 10
            }
        };

        if (window.internals)
            internals.setMockWebAuthenticationConfiguration({ nfc: { error: "malicious-payload", payloadBase64:[testDummyMessagePayloadBase64, testDummyMessagePayloadBase64] } });
        return promiseRejects(t, "NotAllowedError", navigator.credentials.create(options), "Operation timed out.");
    }, "PublicKeyCredential's [[create]] with wrong version in a mock nfc authenticator.2");

</script>
