Commit f2d9411d authored by Maksim Ivanov's avatar Maksim Ivanov Committed by Commit Bot

Add tests for security_token_pin error generator

This CL adds unit tests for the C++ code that generates
human-readable error messages for the smart card PIN errors.

Bug: 1001288
Change-Id: I9ed50272b78237f237e8a6c7834986f7698def81
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2095014Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: default avatarAlexander Hendrich <hendrich@chromium.org>
Commit-Queue: Maksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#749260}
parent 14e7f0ab
...@@ -23,6 +23,7 @@ test("chromeos_components_unittests") { ...@@ -23,6 +23,7 @@ test("chromeos_components_unittests") {
"//chromeos/components/power:unit_tests", "//chromeos/components/power:unit_tests",
"//chromeos/components/proximity_auth:unit_tests", "//chromeos/components/proximity_auth:unit_tests",
"//chromeos/components/quick_answers:unit_tests", "//chromeos/components/quick_answers:unit_tests",
"//chromeos/components/security_token_pin:unit_tests",
"//chromeos/components/smbfs:unit_tests", "//chromeos/components/smbfs:unit_tests",
"//chromeos/components/sync_wifi:unit_tests", "//chromeos/components/sync_wifi:unit_tests",
"//chromeos/components/tether:unit_tests", "//chromeos/components/tether:unit_tests",
......
...@@ -20,3 +20,20 @@ component("security_token_pin") { ...@@ -20,3 +20,20 @@ component("security_token_pin") {
"//ui/base", "//ui/base",
] ]
} }
source_set("unit_tests") {
testonly = true
sources = [ "error_generator_unittest.cc" ]
deps = [
":security_token_pin",
"//base",
"//base:i18n",
"//testing/gtest",
"//ui/base",
]
data_deps = [
"//chromeos/strings:chromeos_test_strings",
"//ui/resources:ui_test_pak_data",
]
}
...@@ -2,3 +2,9 @@ include_rules = [ ...@@ -2,3 +2,9 @@ include_rules = [
"+chromeos/strings/grit/chromeos_strings.h", "+chromeos/strings/grit/chromeos_strings.h",
"+ui/base/l10n", "+ui/base/l10n",
] ]
specific_include_rules = {
".*test\.cc": [
"+ui/base",
],
}
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/security_token_pin/error_generator.h"
#include "base/base_paths.h"
#include "base/files/file_path.h"
#include "base/i18n/rtl.h"
#include "base/path_service.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "chromeos/components/security_token_pin/constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/resource/scale_factor.h"
#include "ui/base/ui_base_paths.h"
namespace chromeos {
namespace security_token_pin {
class SecurityTokenPinErrorGeneratorTest : public testing::Test {
protected:
SecurityTokenPinErrorGeneratorTest() { InitI18n(); }
~SecurityTokenPinErrorGeneratorTest() override = default;
private:
// Initializes the i18n stack and loads the necessary strings. Uses a specific
// locale, so that the tests can compare against golden strings without
// depending on the environment.
void InitI18n() {
base::i18n::SetICUDefaultLocale("en_US");
ui::RegisterPathProvider();
base::FilePath ui_test_pak_path;
ASSERT_TRUE(base::PathService::Get(ui::UI_TEST_PAK, &ui_test_pak_path));
ui::ResourceBundle::InitSharedInstanceWithPakPath(ui_test_pak_path);
base::FilePath dir_module_path;
ASSERT_TRUE(base::PathService::Get(base::DIR_MODULE, &dir_module_path));
base::FilePath chromeos_test_strings_path =
dir_module_path.Append(FILE_PATH_LITERAL("chromeos_test_strings.pak"));
ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
chromeos_test_strings_path, ui::SCALE_FACTOR_NONE);
}
};
// Tests that an empty message is returned when there's neither an error nor the
// number of attempts left.
TEST_F(SecurityTokenPinErrorGeneratorTest, NoError) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kNone, /*attempts_left=*/-1,
/*accept_input=*/true),
base::string16());
}
// Tests the message for the kInvalidPin error.
TEST_F(SecurityTokenPinErrorGeneratorTest, InvalidPin) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kInvalidPin, /*attempts_left=*/-1,
/*accept_input=*/true),
base::ASCIIToUTF16("Invalid PIN."));
}
// Tests the message for the kInvalidPuk error.
TEST_F(SecurityTokenPinErrorGeneratorTest, InvalidPuk) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kInvalidPuk, /*attempts_left=*/-1,
/*accept_input=*/true),
base::ASCIIToUTF16("Invalid PUK."));
}
// Tests the message for the kMaxAttemptsExceeded error.
TEST_F(SecurityTokenPinErrorGeneratorTest, MaxAttemptsExceeded) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kMaxAttemptsExceeded,
/*attempts_left=*/-1,
/*accept_input=*/false),
base::ASCIIToUTF16("Maximum allowed attempts exceeded."));
}
// Tests the message for the kMaxAttemptsExceeded error with the zero number of
// attempts left.
TEST_F(SecurityTokenPinErrorGeneratorTest, MaxAttemptsExceededZeroAttempts) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kMaxAttemptsExceeded,
/*attempts_left=*/0,
/*accept_input=*/false),
base::ASCIIToUTF16("Maximum allowed attempts exceeded."));
}
// Tests the message for the kUnknown error.
TEST_F(SecurityTokenPinErrorGeneratorTest, UnknownError) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kUnknown, /*attempts_left=*/-1,
/*accept_input=*/true),
base::ASCIIToUTF16("Unknown error."));
}
// Tests the message when the number of attempts left is given.
TEST_F(SecurityTokenPinErrorGeneratorTest, Attempts) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kNone, /*attempts_left=*/3,
/*accept_input=*/true),
base::ASCIIToUTF16("3 attempts left"));
}
// Tests that an empty message is returned when the number of attempts is given
// such that, heuristically, it's too big to be displayed for the user.
TEST_F(SecurityTokenPinErrorGeneratorTest, HiddenAttempts) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kNone, /*attempts_left=*/4,
/*accept_input=*/true),
base::string16());
}
// Tests the message for the kInvalidPin error with the number of attempts left.
TEST_F(SecurityTokenPinErrorGeneratorTest, InvalidPinWithAttempts) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kInvalidPin, /*attempts_left=*/3,
/*accept_input=*/true),
base::ASCIIToUTF16("Invalid PIN. 3 attempts left"));
}
// Tests the message for the kInvalidPin error with such a number of attempts
// left that, heuristically, shouldn't be displayed to the user.
TEST_F(SecurityTokenPinErrorGeneratorTest, InvalidPinWithHiddenAttempts) {
EXPECT_EQ(GenerateErrorMessage(ErrorLabel::kInvalidPin, /*attempts_left=*/4,
/*accept_input=*/true),
base::ASCIIToUTF16("Invalid PIN."));
}
} // namespace security_token_pin
} // namespace chromeos
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
# found in the LICENSE file. # found in the LICENSE file.
import("//tools/grit/grit_rule.gni") import("//tools/grit/grit_rule.gni")
import("//tools/grit/repack.gni")
grit("strings") { grit("strings") {
source = "../chromeos_strings.grd" source = "../chromeos_strings.grd"
...@@ -64,3 +65,9 @@ grit("strings") { ...@@ -64,3 +65,9 @@ grit("strings") {
"chromeos_strings_zh-TW.pak", "chromeos_strings_zh-TW.pak",
] ]
} }
repack("chromeos_test_strings") {
sources = [ "$root_gen_dir/chromeos/strings/chromeos_strings_en-US.pak" ]
output = "$root_out_dir/chromeos_test_strings.pak"
deps = [ ":strings" ]
}
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