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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
|
// Copyright 2020 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/dbus/tpm_manager/tpm_manager_client.h"
#include <string>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "chromeos/dbus/tpm_manager/tpm_manager.pb.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/tpm_manager/dbus-constants.h"
using ::testing::_;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SaveArg;
namespace chromeos {
namespace {
// Runs |callback| with |response|. Needed due to ResponseCallback expecting a
// bare pointer rather than an std::unique_ptr.
void RunResponseCallback(dbus::ObjectProxy::ResponseCallback callback,
std::unique_ptr<dbus::Response> response) {
std::move(callback).Run(response.get());
}
// The observer class used for testing to watch the invocation of the signal
// callbacks.
class TestObserver : public TpmManagerClient::Observer {
public:
// TpmManagerClient::Observer.
void OnOwnershipTaken() override { ++signal_count_; }
int signal_count() const { return signal_count_; }
private:
int signal_count_ = 0;
};
} // namespace
class TpmManagerClientTest : public testing::Test {
public:
TpmManagerClientTest() = default;
~TpmManagerClientTest() override = default;
void SetUp() override {
dbus::Bus::Options options;
options.bus_type = dbus::Bus::SYSTEM;
bus_ = new dbus::MockBus(options);
dbus::ObjectPath tpm_manager_object_path =
dbus::ObjectPath(::tpm_manager::kTpmManagerServicePath);
proxy_ = new dbus::MockObjectProxy(bus_.get(),
::tpm_manager::kTpmManagerServiceName,
tpm_manager_object_path);
// Makes sure `GetObjectProxy()` is caled with the correct service name and
// path.
EXPECT_CALL(*bus_.get(),
GetObjectProxy(::tpm_manager::kTpmManagerServiceName,
tpm_manager_object_path))
.WillRepeatedly(Return(proxy_.get()));
EXPECT_CALL(*proxy_.get(), DoCallMethod(_, _, _))
.WillRepeatedly(Invoke(this, &TpmManagerClientTest::OnCallMethod));
EXPECT_CALL(*proxy_,
DoConnectToSignal(::tpm_manager::kTpmManagerInterface,
::tpm_manager::kOwnershipTakenSignal, _, _))
.WillOnce(SaveArg<2>(&ownership_taken_signal_callback_));
TpmManagerClient::Initialize(bus_.get());
// Execute callbacks posted by `client_->Init()`.
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(ownership_taken_signal_callback_.is_null());
client_ = TpmManagerClient::Get();
}
void TearDown() override { TpmManagerClient::Shutdown(); }
protected:
void EmitOwnershipTakenSignal() {
::tpm_manager::OwnershipTakenSignal ownership_taken_signal;
dbus::Signal signal(::tpm_manager::kTpmManagerInterface,
::tpm_manager::kOwnershipTakenSignal);
dbus::MessageWriter(&signal).AppendProtoAsArrayOfBytes(
ownership_taken_signal);
// Emit signal.
ASSERT_FALSE(ownership_taken_signal_callback_.is_null());
ownership_taken_signal_callback_.Run(&signal);
}
base::test::SingleThreadTaskEnvironment task_environment_;
// Mock bus and proxy for simulating calls.
scoped_refptr<dbus::MockBus> bus_;
scoped_refptr<dbus::MockObjectProxy> proxy_;
// Convenience pointer to the global instance.
raw_ptr<TpmManagerClient, DanglingUntriaged> client_;
// The expected replies to the respective D-Bus calls.
::tpm_manager::GetTpmNonsensitiveStatusReply expected_status_reply_;
::tpm_manager::GetVersionInfoReply expected_version_info_reply_;
::tpm_manager::GetSupportedFeaturesReply expected_supported_features_reply_;
::tpm_manager::GetDictionaryAttackInfoReply expected_get_da_info_reply_;
::tpm_manager::TakeOwnershipReply expected_take_ownership_reply_;
::tpm_manager::ClearStoredOwnerPasswordReply expected_clear_password_reply_;
::tpm_manager::ClearTpmReply expected_clear_tpm_reply_;
// When it is set `true`, the parsing failure is expected to be translated by
// proxy to status `STATUS_DBUS_ERROR`.
bool shall_message_parsing_fail_ = false;
private:
// Handles calls to |proxy_|'s `CallMethod()`.
void OnCallMethod(dbus::MethodCall* method_call,
int timeout_ms,
dbus::ObjectProxy::ResponseCallback* callback) {
std::unique_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
dbus::MessageWriter writer(response.get());
if (shall_message_parsing_fail_) {
// Append anything but a valid string that can be deserialized to any
// protobuf messages that tpm manager could possibly sends. A numerical
// type is chosen instead of a string in case the string can actually be
// parsed into any ptotobuf message unexpectedly.
writer.AppendUint32(0);
} else if (method_call->GetMember() ==
::tpm_manager::kGetTpmNonsensitiveStatus) {
writer.AppendProtoAsArrayOfBytes(expected_status_reply_);
} else if (method_call->GetMember() == ::tpm_manager::kGetVersionInfo) {
writer.AppendProtoAsArrayOfBytes(expected_version_info_reply_);
} else if (method_call->GetMember() ==
::tpm_manager::kGetSupportedFeatures) {
writer.AppendProtoAsArrayOfBytes(expected_supported_features_reply_);
} else if (method_call->GetMember() ==
::tpm_manager::kGetDictionaryAttackInfo) {
writer.AppendProtoAsArrayOfBytes(expected_get_da_info_reply_);
} else if (method_call->GetMember() == ::tpm_manager::kTakeOwnership) {
writer.AppendProtoAsArrayOfBytes(expected_take_ownership_reply_);
} else if (method_call->GetMember() ==
::tpm_manager::kClearStoredOwnerPassword) {
writer.AppendProtoAsArrayOfBytes(expected_clear_password_reply_);
} else if (method_call->GetMember() == ::tpm_manager::kClearTpm) {
writer.AppendProtoAsArrayOfBytes(expected_clear_tpm_reply_);
} else {
ASSERT_FALSE(true) << "Unrecognized member: " << method_call->GetMember();
}
task_environment_.GetMainThreadTaskRunner()->PostTask(
FROM_HERE, base::BindOnce(RunResponseCallback, std::move(*callback),
std::move(response)));
}
dbus::ObjectProxy::SignalCallback ownership_taken_signal_callback_;
};
TEST_F(TpmManagerClientTest, GetTpmNonsensitiveStatus) {
expected_status_reply_.set_status(::tpm_manager::STATUS_SUCCESS);
expected_status_reply_.set_is_owned(true);
expected_status_reply_.set_is_enabled(true);
::tpm_manager::GetTpmNonsensitiveStatusReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetTpmNonsensitiveStatusReply* result_reply,
const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetTpmNonsensitiveStatus(
::tpm_manager::GetTpmNonsensitiveStatusRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_status_reply_.status(), result_reply.status());
EXPECT_EQ(expected_status_reply_.is_owned(), result_reply.is_owned());
EXPECT_EQ(expected_status_reply_.is_enabled(), result_reply.is_enabled());
}
TEST_F(TpmManagerClientTest, GetTpmNonsensitiveStatusDBusFailure) {
shall_message_parsing_fail_ = true;
::tpm_manager::GetTpmNonsensitiveStatusReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetTpmNonsensitiveStatusReply* result_reply,
const ::tpm_manager::GetTpmNonsensitiveStatusReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetTpmNonsensitiveStatus(
::tpm_manager::GetTpmNonsensitiveStatusRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(::tpm_manager::STATUS_DBUS_ERROR, result_reply.status());
}
TEST_F(TpmManagerClientTest, GetVersionInfo) {
expected_version_info_reply_.set_status(::tpm_manager::STATUS_SUCCESS);
expected_version_info_reply_.set_tpm_model(123);
expected_version_info_reply_.set_family(456);
::tpm_manager::GetVersionInfoReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetVersionInfoReply* result_reply,
const ::tpm_manager::GetVersionInfoReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetVersionInfo(::tpm_manager::GetVersionInfoRequest(),
std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_version_info_reply_.status(), result_reply.status());
EXPECT_EQ(expected_version_info_reply_.family(), result_reply.family());
EXPECT_EQ(expected_version_info_reply_.tpm_model(), result_reply.tpm_model());
}
TEST_F(TpmManagerClientTest, GetVersionInfoDBusFailure) {
shall_message_parsing_fail_ = true;
::tpm_manager::GetVersionInfoReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetVersionInfoReply* result_reply,
const ::tpm_manager::GetVersionInfoReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetVersionInfo(::tpm_manager::GetVersionInfoRequest(),
std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(::tpm_manager::STATUS_DBUS_ERROR, result_reply.status());
}
TEST_F(TpmManagerClientTest, GetSupportedFeatures) {
expected_supported_features_reply_.set_status(::tpm_manager::STATUS_SUCCESS);
expected_supported_features_reply_.set_support_u2f(true);
::tpm_manager::GetSupportedFeaturesReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetSupportedFeaturesReply* result_reply,
const ::tpm_manager::GetSupportedFeaturesReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetSupportedFeatures(::tpm_manager::GetSupportedFeaturesRequest(),
std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_supported_features_reply_.status(), result_reply.status());
EXPECT_EQ(expected_supported_features_reply_.support_u2f(),
result_reply.support_u2f());
}
TEST_F(TpmManagerClientTest, GetSupportedFeaturesDBusFailure) {
shall_message_parsing_fail_ = true;
::tpm_manager::GetSupportedFeaturesReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetSupportedFeaturesReply* result_reply,
const ::tpm_manager::GetSupportedFeaturesReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetSupportedFeatures(::tpm_manager::GetSupportedFeaturesRequest(),
std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(::tpm_manager::STATUS_DBUS_ERROR, result_reply.status());
}
TEST_F(TpmManagerClientTest, GetDictionaryAttackInfo) {
expected_get_da_info_reply_.set_status(::tpm_manager::STATUS_SUCCESS);
expected_get_da_info_reply_.set_dictionary_attack_counter(123);
expected_get_da_info_reply_.set_dictionary_attack_lockout_in_effect(true);
::tpm_manager::GetDictionaryAttackInfoReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetDictionaryAttackInfoReply* result_reply,
const ::tpm_manager::GetDictionaryAttackInfoReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetDictionaryAttackInfo(
::tpm_manager::GetDictionaryAttackInfoRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_get_da_info_reply_.status(), result_reply.status());
EXPECT_EQ(expected_get_da_info_reply_.dictionary_attack_counter(),
result_reply.dictionary_attack_counter());
EXPECT_EQ(expected_get_da_info_reply_.dictionary_attack_lockout_in_effect(),
result_reply.dictionary_attack_lockout_in_effect());
}
TEST_F(TpmManagerClientTest, GetDictionaryAttackInfoDBusFailure) {
shall_message_parsing_fail_ = true;
::tpm_manager::GetDictionaryAttackInfoReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::GetDictionaryAttackInfoReply* result_reply,
const ::tpm_manager::GetDictionaryAttackInfoReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->GetDictionaryAttackInfo(
::tpm_manager::GetDictionaryAttackInfoRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(::tpm_manager::STATUS_DBUS_ERROR, result_reply.status());
}
TEST_F(TpmManagerClientTest, TakeOwnership) {
// Use a non-zero status value to make sure the value is correctly set.
expected_take_ownership_reply_.set_status(::tpm_manager::STATUS_DEVICE_ERROR);
::tpm_manager::TakeOwnershipReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::TakeOwnershipReply* result_reply,
const ::tpm_manager::TakeOwnershipReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->TakeOwnership(::tpm_manager::TakeOwnershipRequest(),
std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_take_ownership_reply_.status(), result_reply.status());
}
TEST_F(TpmManagerClientTest, TakeOwnershipDBusFailure) {
shall_message_parsing_fail_ = true;
::tpm_manager::TakeOwnershipReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::TakeOwnershipReply* result_reply,
const ::tpm_manager::TakeOwnershipReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->TakeOwnership(::tpm_manager::TakeOwnershipRequest(),
std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(::tpm_manager::STATUS_DBUS_ERROR, result_reply.status());
}
TEST_F(TpmManagerClientTest, ClearStoredOwnerPassword) {
// Use a non-zero status value to make sure the value is correctly set.
expected_clear_password_reply_.set_status(::tpm_manager::STATUS_DEVICE_ERROR);
::tpm_manager::ClearStoredOwnerPasswordReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::ClearStoredOwnerPasswordReply* result_reply,
const ::tpm_manager::ClearStoredOwnerPasswordReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->ClearStoredOwnerPassword(
::tpm_manager::ClearStoredOwnerPasswordRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_clear_password_reply_.status(), result_reply.status());
}
TEST_F(TpmManagerClientTest, ClearTpm) {
// Use a non-zero status value to make sure the value is correctly set.
expected_clear_password_reply_.set_status(::tpm_manager::STATUS_DEVICE_ERROR);
::tpm_manager::ClearTpmReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::ClearTpmReply* result_reply,
const ::tpm_manager::ClearTpmReply& reply) { *result_reply = reply; },
&result_reply);
client_->ClearTpm(::tpm_manager::ClearTpmRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(expected_clear_tpm_reply_.status(), result_reply.status());
}
TEST_F(TpmManagerClientTest, OnwershipTakenSignal) {
TestObserver observer;
ASSERT_EQ(observer.signal_count(), 0);
client_->AddObserver(&observer);
EmitOwnershipTakenSignal();
EXPECT_EQ(observer.signal_count(), 1);
client_->RemoveObserver(&observer);
EmitOwnershipTakenSignal();
EXPECT_EQ(observer.signal_count(), 1);
}
TEST_F(TpmManagerClientTest, ClearStoredOwnerPasswordDBusFailure) {
shall_message_parsing_fail_ = true;
::tpm_manager::ClearStoredOwnerPasswordReply result_reply;
auto callback = base::BindOnce(
[](::tpm_manager::ClearStoredOwnerPasswordReply* result_reply,
const ::tpm_manager::ClearStoredOwnerPasswordReply& reply) {
*result_reply = reply;
},
&result_reply);
client_->ClearStoredOwnerPassword(
::tpm_manager::ClearStoredOwnerPasswordRequest(), std::move(callback));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(::tpm_manager::STATUS_DBUS_ERROR, result_reply.status());
}
} // namespace chromeos
|