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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/network/managed_cellular_pref_handler.h"
#include <string>
#include "ash/constants/ash_pref_names.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/components/network/policy_util.h"
#include "components/onc/onc_constants.h"
#include "components/prefs/testing_pref_service.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace {
constexpr char kName0[] = "cellular0";
constexpr char kName1[] = "cellular1";
constexpr char kIccid0[] = "0000000000000000000";
constexpr char kIccid1[] = "1111111111111111111";
constexpr char kActivationCode0[] = "LPA:1$ActivationCode0$MatchingId";
class FakeObserver : public ManagedCellularPrefHandler::Observer {
public:
FakeObserver() = default;
~FakeObserver() override = default;
int change_count() const { return change_count_; }
// ManagedCellularPref::Observer:
void OnManagedCellularPrefChanged() override { ++change_count_; }
private:
int change_count_ = 0u;
};
} // namespace
class ManagedCellularPrefHandlerTest : public testing::Test {
protected:
ManagedCellularPrefHandlerTest() = default;
~ManagedCellularPrefHandlerTest() override = default;
// testing::Test:
void SetUp() override {
ManagedCellularPrefHandler::RegisterLocalStatePrefs(
device_prefs_.registry());
}
void TearDown() override {
managed_cellular_pref_handler_->RemoveObserver(&observer_);
managed_cellular_pref_handler_.reset();
}
void Init() {
if (managed_cellular_pref_handler_ &&
managed_cellular_pref_handler_->HasObserver(&observer_)) {
managed_cellular_pref_handler_->RemoveObserver(&observer_);
}
managed_cellular_pref_handler_ =
std::make_unique<ManagedCellularPrefHandler>();
managed_cellular_pref_handler_->AddObserver(&observer_);
managed_cellular_pref_handler_->Init(helper_.network_state_handler());
}
void SetDevicePrefs(bool set_to_null = false) {
managed_cellular_pref_handler_->SetDevicePrefs(
set_to_null ? nullptr : &device_prefs_);
}
void AddApnMigratedIccid(const std::string& iccid) {
managed_cellular_pref_handler_->AddApnMigratedIccid(iccid);
}
bool ContainsApnMigratedIccid(const std::string& iccid) {
return managed_cellular_pref_handler_->ContainsApnMigratedIccid(iccid);
}
void ExpectESimMetadata(
const char* expected_iccid,
const char* expected_name,
const policy_util::SmdxActivationCode& expected_activation_code) {
const base::Value::Dict* esim_metadata =
managed_cellular_pref_handler_->GetESimMetadata(expected_iccid);
ASSERT_TRUE(esim_metadata);
const std::string* name =
esim_metadata->FindString(::onc::network_config::kName);
ASSERT_TRUE(name);
EXPECT_EQ(expected_name, *name);
const std::string* activation_code = esim_metadata->FindString(
expected_activation_code.type() ==
policy_util::SmdxActivationCode::Type::SMDP
? ::onc::cellular::kSMDPAddress
: ::onc::cellular::kSMDSAddress);
ASSERT_TRUE(activation_code);
EXPECT_EQ(expected_activation_code.value(), *activation_code);
}
int NumObserverEvents() { return observer_.change_count(); }
ManagedCellularPrefHandler* managed_cellular_pref_handler() {
return managed_cellular_pref_handler_.get();
}
TestingPrefServiceSimple* device_prefs() { return &device_prefs_; }
private:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
NetworkStateTestHelper helper_{/*use_default_devices_and_services=*/false};
TestingPrefServiceSimple device_prefs_;
FakeObserver observer_;
std::unique_ptr<ManagedCellularPrefHandler> managed_cellular_pref_handler_;
};
TEST_F(ManagedCellularPrefHandlerTest, AddAndRemoveESimMetadata) {
Init();
SetDevicePrefs();
const policy_util::SmdxActivationCode smdp_activation_code(
policy_util::SmdxActivationCode::Type::SMDP,
HermesEuiccClient::Get()
->GetTestInterface()
->GenerateFakeActivationCode());
const policy_util::SmdxActivationCode smds_activation_code(
policy_util::SmdxActivationCode::Type::SMDS,
HermesEuiccClient::Get()
->GetTestInterface()
->GenerateFakeActivationCode());
EXPECT_EQ(0, NumObserverEvents());
EXPECT_FALSE(managed_cellular_pref_handler()->GetESimMetadata(kIccid0));
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName0,
smdp_activation_code);
EXPECT_EQ(1, NumObserverEvents());
ExpectESimMetadata(kIccid0, kName0, smdp_activation_code);
// When there are not any differences between the existing metadata and the
// metadata we are trying to add we don't notify observers.
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName0,
smdp_activation_code);
EXPECT_EQ(1, NumObserverEvents());
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName1,
smdp_activation_code);
EXPECT_EQ(2, NumObserverEvents());
ExpectESimMetadata(kIccid0, kName1, smdp_activation_code);
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName1,
smds_activation_code);
EXPECT_EQ(3, NumObserverEvents());
ExpectESimMetadata(kIccid0, kName1, smds_activation_code);
managed_cellular_pref_handler()->RemoveESimMetadata(kIccid0);
EXPECT_EQ(4, NumObserverEvents());
// When the metadata does not exist we should not notify observers.
managed_cellular_pref_handler()->RemoveESimMetadata(kIccid0);
EXPECT_EQ(4, NumObserverEvents());
}
TEST_F(ManagedCellularPrefHandlerTest, AddAndRemovePolicyESimMetadata) {
Init();
SetDevicePrefs();
const policy_util::SmdxActivationCode activation_code(
policy_util::SmdxActivationCode::Type::SMDP,
HermesEuiccClient::Get()
->GetTestInterface()
->GenerateFakeActivationCode());
EXPECT_EQ(0, NumObserverEvents());
EXPECT_FALSE(managed_cellular_pref_handler()->GetESimMetadata(kIccid0));
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName0,
activation_code);
EXPECT_EQ(1, NumObserverEvents());
ExpectESimMetadata(kIccid0, kName0, activation_code);
EXPECT_TRUE(managed_cellular_pref_handler()->IsESimManaged(kIccid0));
// Reach into the prefs and manually erase the field used to communicate that
// an eSIM was installed by policy but is no longer managed.
base::Value::Dict prefs =
device_prefs()->GetDict(prefs::kManagedCellularESimMetadata).Clone();
ASSERT_TRUE(prefs.contains(kIccid0));
base::Value::Dict* esim_metadata = prefs.FindDict(kIccid0);
esim_metadata->Remove("PolicyMissing");
device_prefs()->SetDict(prefs::kManagedCellularESimMetadata,
std::move(prefs));
// The eSIM metadata should still be considered managed even if the "policy
// missing" key is not found.
EXPECT_TRUE(managed_cellular_pref_handler()->IsESimManaged(kIccid0));
managed_cellular_pref_handler()->SetPolicyMissing(kIccid0);
EXPECT_EQ(2, NumObserverEvents());
ExpectESimMetadata(kIccid0, kName0, activation_code);
EXPECT_FALSE(managed_cellular_pref_handler()->IsESimManaged(kIccid0));
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName0,
activation_code);
EXPECT_EQ(3, NumObserverEvents());
// Whenever metadata is added it should always flag the eSIM as being managed.
ExpectESimMetadata(kIccid0, kName0, activation_code);
EXPECT_TRUE(managed_cellular_pref_handler()->IsESimManaged(kIccid0));
}
TEST_F(ManagedCellularPrefHandlerTest, AddApnMigratedIccid) {
Init();
SetDevicePrefs();
EXPECT_FALSE(ContainsApnMigratedIccid(kIccid0));
// Add APN migrated ICCIDs to pref and verify that the prefs store these
// values.
AddApnMigratedIccid(kIccid0);
EXPECT_EQ(0, NumObserverEvents());
EXPECT_TRUE(ContainsApnMigratedIccid(kIccid0));
EXPECT_FALSE(ContainsApnMigratedIccid(kIccid1));
AddApnMigratedIccid(kIccid1);
EXPECT_EQ(0, NumObserverEvents());
EXPECT_TRUE(ContainsApnMigratedIccid(kIccid0));
EXPECT_TRUE(ContainsApnMigratedIccid(kIccid1));
}
TEST_F(ManagedCellularPrefHandlerTest, NoDevicePrefSet) {
Init();
SetDevicePrefs(/*set_to_null=*/true);
// Verify that metadata cannot be added, removed, or accessed when there are
// no device prefs.
const policy_util::SmdxActivationCode activation_code(
policy_util::SmdxActivationCode::Type::SMDP,
HermesEuiccClient::Get()
->GetTestInterface()
->GenerateFakeActivationCode());
EXPECT_EQ(0, NumObserverEvents());
managed_cellular_pref_handler()->AddESimMetadata(kIccid0, kName0,
activation_code);
managed_cellular_pref_handler()->RemoveESimMetadata(kIccid0);
EXPECT_EQ(0, NumObserverEvents());
EXPECT_FALSE(managed_cellular_pref_handler()->GetESimMetadata(kIccid0));
// Verify that APN migration information can be added or accessed when there
// are no device prefs.
EXPECT_FALSE(ContainsApnMigratedIccid(kIccid0));
AddApnMigratedIccid(kIccid0);
EXPECT_EQ(0, NumObserverEvents());
EXPECT_FALSE(ContainsApnMigratedIccid(kIccid0));
}
TEST_F(ManagedCellularPrefHandlerTest,
IccidSmdpPairMigration_MigrationHappensOnce) {
Init();
// The value that we will set the existing/pre-migration prefs to.
auto existing_prefs = base::Value::Dict().Set(kIccid0, kActivationCode0);
device_prefs()->Set(prefs::kManagedCellularIccidSmdpPair,
base::Value(existing_prefs.Clone()));
// Set the pref to some arbitrary value since we just want to confirm that if
// the pref has a value we will assume that we have already performed the
// migration and will not attempt another migration.
base::Value::Dict new_prefs;
device_prefs()->Set(prefs::kManagedCellularESimMetadata,
base::Value(new_prefs.Clone()));
EXPECT_TRUE(device_prefs()->HasPrefPath(prefs::kManagedCellularESimMetadata));
SetDevicePrefs();
const base::Value::Dict& migrated_prefs =
device_prefs()->GetDict(prefs::kManagedCellularESimMetadata);
EXPECT_EQ(new_prefs, migrated_prefs);
}
TEST_F(ManagedCellularPrefHandlerTest, IccidSmdpPairMigration_Migration) {
Init();
auto generate_esim_metadata = [](const std::string& smdp_activation_code) {
return base::Value::Dict().Set(::onc::cellular::kSMDPAddress,
smdp_activation_code);
};
// The value that we will set the existing/pre-migration prefs to.
base::Value::Dict existing_prefs;
// The value that we expect the new/post-migration prefs to be.
base::Value::Dict new_prefs;
// Nothing missing
existing_prefs.Set(kIccid0, kActivationCode0);
base::Value::Dict esim_metadata0 = generate_esim_metadata(kActivationCode0);
new_prefs.Set(kIccid0, esim_metadata0.Clone());
// Activation code empty
existing_prefs.Set(kIccid1, "");
device_prefs()->Set(prefs::kManagedCellularIccidSmdpPair,
base::Value(existing_prefs.Clone()));
SetDevicePrefs();
// The existing prefs should not have changed.
EXPECT_EQ(device_prefs()->GetDict(prefs::kManagedCellularIccidSmdpPair),
existing_prefs);
const base::Value::Dict& migrated_prefs =
device_prefs()->GetDict(prefs::kManagedCellularESimMetadata);
const base::Value::Dict* actual_pref = migrated_prefs.FindDict(kIccid0);
ASSERT_TRUE(actual_pref);
EXPECT_EQ(esim_metadata0, *actual_pref);
EXPECT_FALSE(migrated_prefs.FindDict(kIccid1));
}
} // namespace ash
|