1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/child_accounts/parent_access_code/authenticator.h"
#include <map>
#include <optional>
#include <string>
#include "base/numerics/safe_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/ash/child_accounts/parent_access_code/parent_access_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace parent_access {
AccessCodeConfig GetZeroClockDriftConfig() {
return AccessCodeConfig(kTestSharedSecret, kDefaultCodeValidity,
base::Minutes(0));
}
class ParentAccessCodeAuthenticatorTest : public testing::Test {
public:
ParentAccessCodeAuthenticatorTest(const ParentAccessCodeAuthenticatorTest&) =
delete;
ParentAccessCodeAuthenticatorTest& operator=(
const ParentAccessCodeAuthenticatorTest&) = delete;
protected:
ParentAccessCodeAuthenticatorTest() = default;
~ParentAccessCodeAuthenticatorTest() override = default;
// Verifies that |code| is valid for the given |timestamp|.
void Verify(std::optional<AccessCode> code, base::Time timestamp) {
ASSERT_TRUE(code.has_value());
EXPECT_GE(timestamp, code->valid_from());
EXPECT_LE(timestamp, code->valid_to());
}
};
TEST_F(ParentAccessCodeAuthenticatorTest, GenerateHardcodedCodeValues) {
// Test generation against Parent Access Code values generated in Family
// Link Android app.
std::map<base::Time, std::string> test_values;
ASSERT_NO_FATAL_FAILURE(GetTestAccessCodeValues(&test_values));
Authenticator gen(GetDefaultTestConfig());
for (const auto& it : test_values) {
std::optional<AccessCode> code = gen.Generate(it.first);
ASSERT_NO_FATAL_FAILURE(Verify(code, it.first));
EXPECT_EQ(it.second, code->code());
}
}
TEST_F(ParentAccessCodeAuthenticatorTest, GenerateInTheSameTimeBucket) {
// Test that the same code is generated for whole time bucket defined by code
// validity period.
base::Time timestamp;
ASSERT_TRUE(base::Time::FromString("14 Jan 2019 15:00:00 PST", ×tamp));
const AccessCodeConfig config = GetDefaultTestConfig();
Authenticator gen(GetDefaultTestConfig());
std::optional<AccessCode> first_code = gen.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(first_code, timestamp));
int range = base::ClampFloor(config.code_validity() /
Authenticator::kAccessCodeGranularity) -
1;
for (int i = 0; i < range; ++i) {
timestamp += Authenticator::kAccessCodeGranularity;
std::optional<AccessCode> code = gen.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code, timestamp));
EXPECT_EQ(*first_code, *code);
}
}
TEST_F(ParentAccessCodeAuthenticatorTest, GenerateInDifferentTimeBuckets) {
// Test that the different codes are generated for different time buckets
// defined by code validity period.
base::Time initial_timestamp;
ASSERT_TRUE(
base::Time::FromString("14 Jan 2019 15:00:00 PST", &initial_timestamp));
Authenticator gen(GetDefaultTestConfig());
std::optional<AccessCode> first_code = gen.Generate(initial_timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(first_code, initial_timestamp));
for (int i = 1; i < 10; ++i) {
// "Earlier" time bucket.
{
const base::Time timestamp = initial_timestamp - i * kDefaultCodeValidity;
std::optional<AccessCode> code = gen.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code, timestamp));
EXPECT_NE(*first_code, *code);
}
// "Later" time bucket.
{
const base::Time timestamp = initial_timestamp + i * kDefaultCodeValidity;
std::optional<AccessCode> code = gen.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code, timestamp));
EXPECT_NE(*first_code, *code);
}
}
}
TEST_F(ParentAccessCodeAuthenticatorTest, GenerateWithSameTimestamp) {
// Test that codes generated with the same timestamp and config are the same.
const AccessCodeConfig config = GetDefaultTestConfig();
const base::Time timestamp = base::Time::Now();
Authenticator gen1(GetDefaultTestConfig());
std::optional<AccessCode> code1 = gen1.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code1, timestamp));
Authenticator gen2(GetDefaultTestConfig());
std::optional<AccessCode> code2 = gen2.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code2, timestamp));
EXPECT_EQ(*code1, *code2);
}
TEST_F(ParentAccessCodeAuthenticatorTest, GenerateWithDifferentSharedSecret) {
// Test that codes generated with the different secrets are not the same.
const base::Time timestamp = base::Time::Now();
Authenticator gen1(AccessCodeConfig(
"AAAAAAAAAAAAAAAAAAA", kDefaultCodeValidity, kDefaultClockDrift));
std::optional<AccessCode> code1 = gen1.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code1, timestamp));
Authenticator gen2(AccessCodeConfig(
"AAAAAAAAAAAAAAAAAAB", kDefaultCodeValidity, kDefaultClockDrift));
std::optional<AccessCode> code2 = gen2.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code2, timestamp));
EXPECT_NE(*code1, *code2);
}
TEST_F(ParentAccessCodeAuthenticatorTest, GenerateWithDifferentCodeValidity) {
// Test that codes generated with the different validity are not the same.
const base::Time timestamp = base::Time::Now();
Authenticator gen1(AccessCodeConfig(kTestSharedSecret, base::Minutes(1),
kDefaultClockDrift));
std::optional<AccessCode> code1 = gen1.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code1, timestamp));
Authenticator gen2(AccessCodeConfig(kTestSharedSecret, base::Minutes(3),
kDefaultClockDrift));
std::optional<AccessCode> code2 = gen2.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code2, timestamp));
EXPECT_NE(*code1, *code2);
}
TEST_F(ParentAccessCodeAuthenticatorTest,
GenerateWihtDifferentClockDriftTolerance) {
// Test that clock drift tolerance does not affect code generation.
const base::Time timestamp = base::Time::Now();
Authenticator gen1(AccessCodeConfig(kTestSharedSecret, kDefaultCodeValidity,
base::Minutes(1)));
std::optional<AccessCode> code1 = gen1.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code1, timestamp));
Authenticator gen2(AccessCodeConfig(kTestSharedSecret, kDefaultCodeValidity,
base::Minutes(10)));
std::optional<AccessCode> code2 = gen2.Generate(timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(code2, timestamp));
EXPECT_EQ(*code1, *code2);
}
TEST_F(ParentAccessCodeAuthenticatorTest, ValidateHardcodedCodeValues) {
// Test validation against Parent Access Code values generated in Family Link
// Android app.
std::map<base::Time, std::string> test_values;
ASSERT_NO_FATAL_FAILURE(GetTestAccessCodeValues(&test_values));
Authenticator gen(AccessCodeConfig(kTestSharedSecret, kDefaultCodeValidity,
base::Minutes(0)));
for (const auto& it : test_values) {
std::optional<AccessCode> code = gen.Validate(it.second, it.first);
ASSERT_NO_FATAL_FAILURE(Verify(code, it.first));
EXPECT_EQ(it.second, code->code());
}
}
TEST_F(ParentAccessCodeAuthenticatorTest,
ValidationAndGenerationOnDifferentAuthenticators) {
// Test validation against codes generated by separate
// Authenticator object in and outside of the valid time
// bucket.
const AccessCodeConfig config(GetZeroClockDriftConfig());
Authenticator generator(GetZeroClockDriftConfig());
Authenticator validator(GetZeroClockDriftConfig());
base::Time generation_timestamp;
ASSERT_TRUE(base::Time::FromString("15 Jan 2019 00:00:00 PST",
&generation_timestamp));
std::optional<AccessCode> generated_code =
generator.Generate(generation_timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(generated_code, generation_timestamp));
// Before valid period.
std::optional<AccessCode> validated_code = validator.Validate(
generated_code->code(), generation_timestamp - base::Seconds(1));
EXPECT_FALSE(validated_code);
// In valid period.
int range = base::ClampFloor(config.code_validity() /
Authenticator::kAccessCodeGranularity);
for (int i = 0; i < range; ++i) {
validated_code = validator.Validate(
generated_code->code(),
generation_timestamp + i * Authenticator::kAccessCodeGranularity);
ASSERT_TRUE(validated_code);
EXPECT_EQ(*generated_code, *validated_code);
}
// After valid period.
validated_code = validator.Validate(
generated_code->code(), generation_timestamp + config.code_validity());
EXPECT_FALSE(validated_code);
}
TEST_F(ParentAccessCodeAuthenticatorTest,
ValidationAndGenerationOnSameAuthenticator) {
// Test generation and validation on the same Authenticator
// object in and outside of the valid time bucket.
const AccessCodeConfig config(GetZeroClockDriftConfig());
Authenticator authenticator(GetZeroClockDriftConfig());
base::Time generation_timestamp;
ASSERT_TRUE(base::Time::FromString("15 Jan 2019 00:00:00 PST",
&generation_timestamp));
std::optional<AccessCode> generated_code =
authenticator.Generate(generation_timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(generated_code, generation_timestamp));
// Before valid period.
std::optional<AccessCode> validated_code = authenticator.Validate(
generated_code->code(), generation_timestamp - base::Seconds(1));
EXPECT_FALSE(validated_code);
// In valid period.
int range = base::ClampFloor(config.code_validity() /
Authenticator::kAccessCodeGranularity);
for (int i = 0; i < range; ++i) {
validated_code = authenticator.Validate(
generated_code->code(),
generation_timestamp + i * Authenticator::kAccessCodeGranularity);
ASSERT_TRUE(validated_code);
EXPECT_EQ(*generated_code, *validated_code);
}
// After valid period.
validated_code = authenticator.Validate(
generated_code->code(), generation_timestamp + config.code_validity());
EXPECT_FALSE(validated_code);
}
TEST_F(ParentAccessCodeAuthenticatorTest, ValidationWithClockDriftTolerance) {
// Test validation with clock drift tolerance.
Authenticator generator(GetDefaultTestConfig());
Authenticator validator_with_tolerance(GetDefaultTestConfig());
Authenticator validator_no_tolerance(AccessCodeConfig(
kTestSharedSecret, kDefaultCodeValidity, base::Minutes(0)));
// By default code will be valid [15:30:00-15:40:00).
// With clock drift tolerance code will be valid [15:25:00-15:45:00).
base::Time generation_timestamp;
ASSERT_TRUE(base::Time::FromString("15 Jan 2019 15:30:00 PST",
&generation_timestamp));
std::optional<AccessCode> generated_code =
generator.Generate(generation_timestamp);
ASSERT_NO_FATAL_FAILURE(Verify(generated_code, generation_timestamp));
// Both validators accept the code in valid period.
int range = base::ClampFloor(kDefaultCodeValidity /
Authenticator::kAccessCodeGranularity);
base::Time timestamp;
std::optional<AccessCode> validated_code_no_tolerance;
std::optional<AccessCode> validated_code_with_tolerance;
for (int i = 0; i < range; ++i) {
timestamp =
generation_timestamp + i * Authenticator::kAccessCodeGranularity;
validated_code_no_tolerance =
validator_no_tolerance.Validate(generated_code->code(), timestamp);
ASSERT_TRUE(validated_code_no_tolerance);
validated_code_with_tolerance =
validator_with_tolerance.Validate(generated_code->code(), timestamp);
ASSERT_TRUE(validated_code_with_tolerance);
EXPECT_EQ(*validated_code_no_tolerance, *validated_code_with_tolerance);
}
// Validator's device clock late by tolerated drift.
timestamp = generation_timestamp - kDefaultClockDrift / 2;
validated_code_no_tolerance =
validator_no_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_FALSE(validated_code_no_tolerance);
validated_code_with_tolerance =
validator_with_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_TRUE(validated_code_with_tolerance);
// Validator's device clock late outside of tolerated drift.
timestamp = generation_timestamp - kDefaultClockDrift - base::Seconds(1);
validated_code_no_tolerance =
validator_no_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_FALSE(validated_code_no_tolerance);
validated_code_with_tolerance =
validator_with_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_FALSE(validated_code_with_tolerance);
// Validator's device clock ahead by tolerated drift.
timestamp =
generation_timestamp + kDefaultCodeValidity + kDefaultClockDrift / 2;
validated_code_no_tolerance =
validator_no_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_FALSE(validated_code_no_tolerance);
validated_code_with_tolerance =
validator_with_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_TRUE(validated_code_with_tolerance);
// Validator's device clock ahead outside of tolerated drift.
timestamp = generation_timestamp + kDefaultCodeValidity + kDefaultClockDrift;
validated_code_no_tolerance =
validator_no_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_FALSE(validated_code_no_tolerance);
validated_code_with_tolerance =
validator_with_tolerance.Validate(generated_code->code(), timestamp);
EXPECT_FALSE(validated_code_with_tolerance);
}
TEST_F(ParentAccessCodeAuthenticatorTest, UnixEpoch) {
// Test authenticator with Unix Epoch timestamp.
const base::Time unix_epoch = base::Time::UnixEpoch();
Authenticator authenticator(GetDefaultTestConfig());
std::optional<AccessCode> generated = authenticator.Generate(unix_epoch);
ASSERT_NO_FATAL_FAILURE(Verify(generated, unix_epoch));
std::optional<AccessCode> validated =
authenticator.Validate(generated->code(), unix_epoch);
ASSERT_NO_FATAL_FAILURE(Verify(validated, unix_epoch));
EXPECT_EQ(generated, validated);
}
} // namespace parent_access
} // namespace ash
|