Commit cd63e9e0 authored by Kim Paulhamus's avatar Kim Paulhamus Committed by Commit Bot

[WebAuthn] Refactor layout tests to reduce code.

Instead of custom building each set of options per test,
deep copy the options and remove select properties. Both reduces
code and makes it more clear what is being tested.

Also adds a helper to set up a successful response from the
mock authenticator.

Bug: 664630
Change-Id: If5986e21b56e1093835866283f0e69134f265390
Reviewed-on: https://chromium-review.googlesource.com/957831Reviewed-by: default avatarBalazs Engedy <engedy@chromium.org>
Commit-Queue: Balazs Engedy <engedy@chromium.org>
Cr-Commit-Position: refs/heads/master@{#542753}
parent aa7d638b
...@@ -115,16 +115,10 @@ promise_test(_ => { ...@@ -115,16 +115,10 @@ promise_test(_ => {
promise_test(_ => { promise_test(_ => {
mockAuthenticator.setRawId(RAW_ID); mockAuthenticator.setRawId(RAW_ID);
mockAuthenticator.setId(ID); mockAuthenticator.setDefaultsForSuccessfulGetAssertion();
mockAuthenticator.setClientDataJson(CLIENT_DATA_JSON); return navigator.credentials.get({publicKey : GET_CREDENTIAL_OPTIONS}).then(r => {
mockAuthenticator.setAuthenticatorData(AUTHENTICATOR_DATA); assertValidGetCredentialResponse(r);
mockAuthenticator.setSignature(SIGNATURE); });
mockAuthenticator.setAuthenticatorStatus(
webauth.mojom.AuthenticatorStatus.SUCCESS);
return navigator.credentials.get({publicKey : GET_CREDENTIAL_OPTIONS}).then(r => {
assertValidGetCredentialResponse(r);
});
}, "Verify that mockAuthenticator returns the values we give it."); }, "Verify that mockAuthenticator returns the values we give it.");
promise_test(t => { promise_test(t => {
...@@ -156,54 +150,28 @@ promise_test(t => { ...@@ -156,54 +150,28 @@ promise_test(t => {
}, "Verify that not supported error returned by mock is properly handled."); }, "Verify that not supported error returned by mock is properly handled.");
promise_test(function(t) { promise_test(function(t) {
var customGetCredentialOptions = { var customGetCredentialOptions = deepCopy(MAKE_CREDENTIAL_OPTIONS);
// No challenge. delete customGetCredentialOptions.challenge;
rpId: "google.com", return promise_rejects(t, new TypeError(),
allowCredentials: [ACCEPTABLE_CREDENTIAL], navigator.credentials.get({publicKey: customGetCredentialOptions}));
userVerification: "preferred",
};
return promise_rejects(t, new TypeError(),
navigator.credentials.get({publicKey: customGetCredentialOptions}));
}, "navigator.credentials.get() with missing challenge"); }, "navigator.credentials.get() with missing challenge");
promise_test(_ => { promise_test(_ => {
mockAuthenticator.reset(); mockAuthenticator.reset();
mockAuthenticator.setRawId(RAW_ID); mockAuthenticator.setDefaultsForSuccessfulGetAssertion();
mockAuthenticator.setId(ID); var customGetCredentialOptions = deepCopy(MAKE_CREDENTIAL_OPTIONS);
mockAuthenticator.setClientDataJson(CLIENT_DATA_JSON); delete customGetCredentialOptions.rpId;
mockAuthenticator.setAuthenticatorData(AUTHENTICATOR_DATA); return navigator.credentials.get({publicKey: customGetCredentialOptions}).then(r => {
mockAuthenticator.setSignature(SIGNATURE);
mockAuthenticator.setAuthenticatorStatus(
webauth.mojom.AuthenticatorStatus.SUCCESS);
var customPublicKey = {
challenge: CHALLENGE,
allowCredentials: [ACCEPTABLE_CREDENTIAL],
userVerification: "preferred",
};
return navigator.credentials.get({publicKey: customPublicKey}).then(r => {
assertValidGetCredentialResponse(r); assertValidGetCredentialResponse(r);
}); });
}, "navigator.credentials.get() with missing rpId"); }, "navigator.credentials.get() with missing rpId");
promise_test(_ => { promise_test(_ => {
mockAuthenticator.reset(); mockAuthenticator.reset();
mockAuthenticator.setRawId(RAW_ID); mockAuthenticator.setDefaultsForSuccessfulGetAssertion();
mockAuthenticator.setId(ID); var customGetCredentialOptions = deepCopy(MAKE_CREDENTIAL_OPTIONS);
mockAuthenticator.setClientDataJson(CLIENT_DATA_JSON); delete customGetCredentialOptions.userVerification;
mockAuthenticator.setAuthenticatorData(AUTHENTICATOR_DATA); return navigator.credentials.get({publicKey: customGetCredentialOptions}).then(r => {
mockAuthenticator.setSignature(SIGNATURE);
mockAuthenticator.setAuthenticatorStatus(
webauth.mojom.AuthenticatorStatus.SUCCESS);
var customPublicKey = {
challenge: CHALLENGE,
allowCredentials: [ACCEPTABLE_CREDENTIAL]
};
return navigator.credentials.get({publicKey: customPublicKey}).then(r => {
assertValidGetCredentialResponse(r); assertValidGetCredentialResponse(r);
}); });
}, "navigator.credentials.get() with missing user verification requirement"); }, "navigator.credentials.get() with missing user verification requirement");
......
...@@ -138,6 +138,17 @@ class MockAuthenticator { ...@@ -138,6 +138,17 @@ class MockAuthenticator {
webauth.mojom.AuthenticatorStatus.SUCCESS); webauth.mojom.AuthenticatorStatus.SUCCESS);
} }
// Sets everything needed for a GetAssertion success response.
setDefaultsForSuccessfulGetAssertion() {
mockAuthenticator.setRawId(RAW_ID);
mockAuthenticator.setId(ID);
mockAuthenticator.setClientDataJson(CLIENT_DATA_JSON);
mockAuthenticator.setAuthenticatorData(AUTHENTICATOR_DATA);
mockAuthenticator.setSignature(SIGNATURE);
mockAuthenticator.setAuthenticatorStatus(
webauth.mojom.AuthenticatorStatus.SUCCESS);
}
setAuthenticatorStatus(status) { setAuthenticatorStatus(status) {
this.status_ = status; this.status_ = status;
} }
...@@ -267,6 +278,16 @@ function EncloseInScriptTag(code) { ...@@ -267,6 +278,16 @@ function EncloseInScriptTag(code) {
return "<script>" + code + "</scr" + "ipt>"; return "<script>" + code + "</scr" + "ipt>";
} }
function deepCopy(value) {
if ([Number, String, Uint8Array].includes(value.constructor))
return value;
let copy = (value.constructor == Array) ? [] : {};
for (let key of Object.keys(value))
copy[key] = deepCopy(value[key]);
return copy;
}
// Verifies if |r| is the valid response to credentials.create(publicKey). // Verifies if |r| is the valid response to credentials.create(publicKey).
function assertValidMakeCredentialResponse(r) { function assertValidMakeCredentialResponse(r) {
assert_equals(r.id, ID, 'id'); assert_equals(r.id, ID, 'id');
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment