Chromium Code Reviews| Index: chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api_unittest.cc |
| diff --git a/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api_unittest.cc b/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api_unittest.cc |
| index b97b51a4116eaa15a80684c327af073db9df4918..6d7bf95f25a2fe9430a82c34b44e1f69cbc3edf8 100644 |
| --- a/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api_unittest.cc |
| +++ b/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api_unittest.cc |
| @@ -6,6 +6,8 @@ |
| #include "chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.h" |
| +#include <algorithm> |
| + |
| #include "base/bind.h" |
| #include "base/memory/ptr_util.h" |
| #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| @@ -14,12 +16,16 @@ |
| #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
| #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
| #include "chrome/browser/extensions/extension_api_unittest.h" |
| +#include "chrome/common/pref_names.h" |
| #include "chromeos/login/auth/fake_extended_authenticator.h" |
| #include "extensions/browser/api_test_utils.h" |
| #include "extensions/browser/extension_function_dispatcher.h" |
| using namespace extensions; |
| namespace quick_unlock_private = extensions::api::quick_unlock_private; |
| +using CredentialRequirement = quick_unlock_private::CredentialRequirement; |
| +using CredentialRequirementFailure = |
| + quick_unlock_private::CredentialRequirementFailure; |
| using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; |
| using QuickUnlockModeList = std::vector<QuickUnlockMode>; |
| using CredentialList = std::vector<std::string>; |
| @@ -50,6 +56,14 @@ void FailIfCalled(const QuickUnlockModeList& modes) { |
| FAIL(); |
| } |
| +enum TestPinState { |
| + PIN_GOOD = 0x01, |
|
jdufault
2016/10/04 22:57:28
What about using 1 << 0, 1 << 1, 1 << 2, 1 << 3 in
sammiequon
2016/10/05 19:48:11
Done.
|
| + PIN_SHORT = 0x02, |
| + PIN_LONG = 0x04, |
| + PIN_WEAK = 0x08, |
| + PIN_WEAK_BUT_OK = 0x10 |
| +}; |
| + |
| } // namespace |
| class QuickUnlockPrivateUnitTest : public ExtensionApiUnittest { |
| @@ -129,6 +143,59 @@ class QuickUnlockPrivateUnitTest : public ExtensionApiUnittest { |
| return modes; |
| } |
| + bool CredentialFailuresContains( |
| + CredentialRequirementFailure failure_to_check, |
| + const std::vector<CredentialRequirementFailure> list_of_failures) { |
| + return std::find(list_of_failures.begin(), list_of_failures.end(), |
| + failure_to_check) != list_of_failures.end(); |
| + } |
| + |
| + // Helper function for checking whether |IsCredentialUsableUsingPin| will |
| + // return the right message given a pin. |
| + void CheckPin(int expected_outcome, const std::string& pin) { |
| + auto result = IsCredentialUsableUsingPin(pin); |
| + auto failures = result.failures; |
| + bool easy_pins_allowed = result.allow_easy_pins && *result.allow_easy_pins; |
| + |
| + EXPECT_EQ(expected_outcome & PIN_GOOD ? true : false, failures.empty()); |
| + EXPECT_EQ( |
| + expected_outcome & PIN_SHORT ? true : false, |
| + CredentialFailuresContains(CredentialRequirementFailure:: |
| + CREDENTIAL_REQUIREMENT_FAILURE_TOO_SHORT, |
| + failures)); |
| + EXPECT_EQ( |
| + expected_outcome & PIN_LONG ? true : false, |
| + CredentialFailuresContains(CredentialRequirementFailure:: |
| + CREDENTIAL_REQUIREMENT_FAILURE_TOO_LONG, |
| + failures)); |
| + if (easy_pins_allowed) { |
| + EXPECT_EQ(expected_outcome & PIN_WEAK_BUT_OK ? true : false, |
| + CredentialFailuresContains( |
| + CredentialRequirementFailure:: |
| + CREDENTIAL_REQUIREMENT_FAILURE_TOO_WEAK, |
| + failures)); |
| + } else { |
| + EXPECT_EQ(expected_outcome & PIN_WEAK ? true : false, |
| + CredentialFailuresContains( |
| + CredentialRequirementFailure:: |
| + CREDENTIAL_REQUIREMENT_FAILURE_TOO_WEAK, |
| + failures)); |
| + } |
| + } |
| + |
| + CredentialRequirement IsCredentialUsableUsingPin(const std::string& pin) { |
| + auto params = base::MakeUnique<base::ListValue>(); |
| + params->AppendString(ToString(QuickUnlockMode::QUICK_UNLOCK_MODE_PIN)); |
| + params->AppendString(pin); |
| + |
| + std::unique_ptr<base::Value> result = RunFunction( |
| + new QuickUnlockPrivateIsCredentialUsableFunction(), std::move(params)); |
| + |
| + CredentialRequirement function_result; |
| + EXPECT_TRUE(CredentialRequirement::Populate(*result, &function_result)); |
| + return function_result; |
| + } |
| + |
| // Wrapper for chrome.quickUnlockPrivate.setModes that automatically uses a |
| // valid password. |
| bool SetModes(const QuickUnlockModeList& modes, |
| @@ -245,7 +312,7 @@ TEST_F(QuickUnlockPrivateUnitTest, SetModesFailsWithInvalidPassword) { |
| FailIfModesChanged(); |
| EXPECT_FALSE(SetModesUsingPassword( |
| kInvalidPassword, |
| - QuickUnlockModeList{QuickUnlockMode::QUICK_UNLOCK_MODE_PIN}, {"11"})); |
| + QuickUnlockModeList{QuickUnlockMode::QUICK_UNLOCK_MODE_PIN}, {"1111"})); |
| EXPECT_EQ(GetActiveModes(), QuickUnlockModeList{}); |
| } |
| @@ -316,4 +383,56 @@ TEST_F(QuickUnlockPrivateUnitTest, ThrowErrorOnMismatchedParameterCount) { |
| EXPECT_FALSE(SetModesWithError("[\"valid\", [], [\"11\"]]").empty()); |
| } |
| +// Verifies that unsubmitted pins give the right message to users. |
| +TEST_F(QuickUnlockPrivateUnitTest, VerifyPinsAgainstPreferences) { |
| + PrefService* pref_service = profile()->GetPrefs(); |
| + |
| + // Verify the pin checks work with the default preferences which are minimum |
| + // length of 4, maximum length of 0 (no maximum) and no easy to guess check. |
| + CheckPin(PIN_GOOD, "1112"); |
|
jdufault
2016/10/04 22:57:28
These are much easier to read, thanks.
sammiequon
2016/10/05 19:48:11
:)
|
| + CheckPin(PIN_GOOD, "22223"); |
| + CheckPin(PIN_GOOD, "3333333333333334"); |
| + CheckPin(PIN_WEAK_BUT_OK, "3333"); |
| + CheckPin(PIN_SHORT, "4"); |
| + CheckPin(PIN_SHORT, "55"); |
| + CheckPin(PIN_SHORT | PIN_WEAK_BUT_OK, "777"); |
| + |
| + // Verify that now if the minimum is set to 3, pins of length 3 are accepted. |
| + pref_service->SetInteger(prefs::kPinUnlockMinimumLength, 3); |
| + CheckPin(PIN_WEAK_BUT_OK, "777"); |
| + |
| + // Verify that now if the maximum is set to 5, pins longer than 5 are |
| + // considered as long pins. |
| + pref_service->SetInteger(prefs::kPinUnlockMaximumLength, 5); |
| + CheckPin(PIN_LONG | PIN_WEAK_BUT_OK, "888888"); |
| + CheckPin(PIN_LONG | PIN_WEAK_BUT_OK, "9999999"); |
| + |
| + // Set the pins minimum/maximum lengths back to their defaults. |
| + pref_service->SetInteger(prefs::kPinUnlockMinimumLength, 4); |
| + pref_service->SetInteger(prefs::kPinUnlockMaximumLength, 0); |
| + // Verify that pins that consists of the same character, or pins that are |
| + // increasing are considered same digit and increasing pins respectively. A 9 |
| + // followed by a 0 is not considered increasing. |
| + pref_service->SetBoolean(prefs::kPinUnlockAllowEasyPins, false); |
| + CheckPin(PIN_GOOD, "7890"); |
| + CheckPin(PIN_WEAK, "1111"); |
| + CheckPin(PIN_WEAK, "2222222"); |
| + CheckPin(PIN_WEAK, "0123"); |
| + CheckPin(PIN_WEAK, "3456789"); |
| + CheckPin(PIN_SHORT | PIN_WEAK, "111"); |
| + |
| + // Verify that trying out pins under the minimum/maximum lengths will send the |
| + // minimum/maximum lengths as additional information for display purposes. |
| + pref_service->SetInteger(prefs::kPinUnlockMinimumLength, 6); |
| + pref_service->SetInteger(prefs::kPinUnlockMaximumLength, 8); |
| + EXPECT_EQ(6, *(IsCredentialUsableUsingPin("1456").min_length)); |
| + EXPECT_EQ(8, *(IsCredentialUsableUsingPin("141329556").max_length)); |
| + |
| + // Verify that changing the allow easy pins preference affects the output. |
| + pref_service->SetBoolean(prefs::kPinUnlockAllowEasyPins, true); |
| + pref_service->SetInteger(prefs::kPinUnlockMinimumLength, 4); |
| + EXPECT_EQ(true, *(IsCredentialUsableUsingPin("1234").allow_easy_pins)); |
| + pref_service->SetBoolean(prefs::kPinUnlockAllowEasyPins, false); |
| + EXPECT_EQ(false, *(IsCredentialUsableUsingPin("1234").allow_easy_pins)); |
| +} |
| } // namespace chromeos |