fast_pair_data_encryptor_impl_unittest.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor_impl.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <array>
  8. #include "ash/quick_pair/common/protocol.h"
  9. #include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"
  10. #include "ash/quick_pair/repository/fake_fast_pair_repository.h"
  11. #include "ash/services/quick_pair/fast_pair_data_parser.h"
  12. #include "ash/services/quick_pair/mock_quick_pair_process_manager.h"
  13. #include "ash/services/quick_pair/quick_pair_process.h"
  14. #include "ash/services/quick_pair/quick_pair_process_manager.h"
  15. #include "ash/services/quick_pair/quick_pair_process_manager_impl.h"
  16. #include "base/base64.h"
  17. #include "base/callback_helpers.h"
  18. #include "base/memory/weak_ptr.h"
  19. #include "base/run_loop.h"
  20. #include "base/test/bind.h"
  21. #include "base/test/gmock_callback_support.h"
  22. #include "base/test/task_environment.h"
  23. #include "testing/gmock/include/gmock/gmock.h"
  24. #include "testing/gtest/include/gtest/gtest.h"
  25. namespace {
  26. const std::array<uint8_t, kBlockSizeBytes> kResponseBytes = {
  27. 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D,
  28. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6};
  29. const std::array<uint8_t, kBlockSizeBytes> kPasskeyBytes = {
  30. 0x02, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D,
  31. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6};
  32. const std::vector<uint8_t> kAccountKey = {0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F,
  33. 0xF7, 0xB6, 0xCF, 0x5E, 0x3F, 0x45,
  34. 0x61, 0xC3, 0x32, 0x1D};
  35. const char kPublicAntiSpoof[] =
  36. "Wuyr48lD3txnUhGiMF1IfzlTwRxxe+wMB1HLzP+"
  37. "0wVcljfT3XPoiy1fntlneziyLD5knDVAJSE+RM/zlPRP/Jg==";
  38. const char kInvalidPublicAntiSpoof[] = "";
  39. constexpr char kValidModelId[] = "718c17";
  40. constexpr char kTestAddress[] = "test_address";
  41. } // namespace
  42. namespace ash {
  43. namespace quick_pair {
  44. // This controls the protocol we use to test.
  45. using TestParam = bool;
  46. class FastPairDataEncryptorImplTest : public testing::TestWithParam<TestParam> {
  47. public:
  48. void SetUp() override {
  49. data_parser_ = std::make_unique<ash::quick_pair::FastPairDataParser>(
  50. fast_pair_data_parser_.InitWithNewPipeAndPassReceiver());
  51. data_parser_remote_.Bind(std::move(fast_pair_data_parser_),
  52. task_environment_.GetMainThreadTaskRunner());
  53. process_manager_ = std::make_unique<MockQuickPairProcessManager>();
  54. quick_pair_process::SetProcessManager(process_manager_.get());
  55. }
  56. void TearDown() override { data_encryptor_.reset(); }
  57. void FailedSetUpNoMetadata() {
  58. repository_ = std::make_unique<FakeFastPairRepository>();
  59. // Not using the param here to control the type of device because only
  60. // the kFastPairInitial protocol can fail.
  61. device_ = base::MakeRefCounted<Device>(kValidModelId, kTestAddress,
  62. Protocol::kFastPairInitial);
  63. FastPairDataEncryptorImpl::Factory::CreateAsync(
  64. device_, base::BindOnce(
  65. &FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
  66. weak_ptr_factory_.GetWeakPtr()));
  67. }
  68. void SuccessfulSetUp() {
  69. repository_ = std::make_unique<FakeFastPairRepository>();
  70. nearby::fastpair::Device metadata;
  71. std::string decoded_key;
  72. base::Base64Decode(kPublicAntiSpoof, &decoded_key);
  73. metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
  74. repository_->SetFakeMetadata(kValidModelId, metadata);
  75. // The param controls which protocol we use.
  76. if (GetParam()) {
  77. device_ = base::MakeRefCounted<Device>(kValidModelId, kTestAddress,
  78. Protocol::kFastPairInitial);
  79. } else {
  80. device_ = base::MakeRefCounted<Device>(kValidModelId, kTestAddress,
  81. Protocol::kFastPairSubsequent);
  82. device_->SetAdditionalData(Device::AdditionalDataType::kAccountKey,
  83. kAccountKey);
  84. }
  85. FastPairDataEncryptorImpl::Factory::CreateAsync(
  86. device_, base::BindOnce(
  87. &FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
  88. weak_ptr_factory_.GetWeakPtr()));
  89. }
  90. void SuccessfulSetUpToTestPublicKey() {
  91. repository_ = std::make_unique<FakeFastPairRepository>();
  92. nearby::fastpair::Device metadata;
  93. std::string decoded_key;
  94. base::Base64Decode(kPublicAntiSpoof, &decoded_key);
  95. metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
  96. repository_->SetFakeMetadata(kValidModelId, metadata);
  97. // Not using the param here to control the type of device because the
  98. // public key expectations differ for protocols
  99. device_ = base::MakeRefCounted<Device>(kValidModelId, kTestAddress,
  100. Protocol::kFastPairInitial);
  101. FastPairDataEncryptorImpl::Factory::CreateAsync(
  102. device_, base::BindOnce(
  103. &FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
  104. weak_ptr_factory_.GetWeakPtr()));
  105. }
  106. void FailedSetUpNoKeyPair() {
  107. repository_ = std::make_unique<FakeFastPairRepository>();
  108. nearby::fastpair::Device metadata;
  109. std::string decoded_key;
  110. base::Base64Decode(kInvalidPublicAntiSpoof, &decoded_key);
  111. metadata.mutable_anti_spoofing_key_pair()->set_public_key(decoded_key);
  112. repository_->SetFakeMetadata(kValidModelId, metadata);
  113. // Not using the param here to control the type of device because only
  114. // the kFastPairInitial protocol can fail..
  115. device_ = base::MakeRefCounted<Device>(kValidModelId, kTestAddress,
  116. Protocol::kFastPairInitial);
  117. FastPairDataEncryptorImpl::Factory::CreateAsync(
  118. device_, base::BindOnce(
  119. &FastPairDataEncryptorImplTest::OnDataEncryptorCreateAsync,
  120. weak_ptr_factory_.GetWeakPtr()));
  121. }
  122. void OnDataEncryptorCreateAsync(
  123. std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor) {
  124. data_encryptor_ = std::move(fast_pair_data_encryptor);
  125. }
  126. const std::array<uint8_t, kBlockSizeBytes> EncryptBytes() {
  127. return data_encryptor_->EncryptBytes(kResponseBytes);
  128. }
  129. void ParseDecryptedResponse() {
  130. const std::array<uint8_t, kBlockSizeBytes> bytes =
  131. data_encryptor_->EncryptBytes(kResponseBytes);
  132. data_encryptor_->ParseDecryptedResponse(
  133. std::vector<uint8_t>(bytes.begin(), bytes.end()),
  134. base::BindOnce(
  135. &FastPairDataEncryptorImplTest::ParseDecryptedResponseCallback,
  136. weak_ptr_factory_.GetWeakPtr()));
  137. }
  138. void ParseDecryptedResponseInvalidBytes() {
  139. const std::array<uint8_t, kBlockSizeBytes> bytes =
  140. data_encryptor_->EncryptBytes(kResponseBytes);
  141. data_encryptor_->ParseDecryptedResponse(
  142. std::vector<uint8_t>(bytes.begin() + 3, bytes.end()),
  143. base::BindOnce(
  144. &FastPairDataEncryptorImplTest::ParseDecryptedResponseCallback,
  145. weak_ptr_factory_.GetWeakPtr()));
  146. }
  147. void ParseDecryptedResponseCallback(
  148. const absl::optional<DecryptedResponse>& response) {
  149. response_ = response;
  150. }
  151. void ParseDecryptedPasskey() {
  152. const std::array<uint8_t, kBlockSizeBytes> bytes =
  153. data_encryptor_->EncryptBytes(kPasskeyBytes);
  154. data_encryptor_->ParseDecryptedPasskey(
  155. std::vector<uint8_t>(bytes.begin(), bytes.end()),
  156. base::BindOnce(
  157. &FastPairDataEncryptorImplTest::ParseDecryptedPasskeyCallback,
  158. weak_ptr_factory_.GetWeakPtr()));
  159. }
  160. void ParseDecryptedPasskeyInvalidBytes() {
  161. const std::array<uint8_t, kBlockSizeBytes> bytes =
  162. data_encryptor_->EncryptBytes(kPasskeyBytes);
  163. data_encryptor_->ParseDecryptedPasskey(
  164. std::vector<uint8_t>(bytes.begin() + 3, bytes.end()),
  165. base::BindOnce(
  166. &FastPairDataEncryptorImplTest::ParseDecryptedPasskeyCallback,
  167. weak_ptr_factory_.GetWeakPtr()));
  168. }
  169. void ParseDecryptedPasskeyCallback(
  170. const absl::optional<DecryptedPasskey>& passkey) {
  171. passkey_ = passkey;
  172. }
  173. protected:
  174. std::unique_ptr<FastPairDataEncryptor> data_encryptor_;
  175. absl::optional<DecryptedResponse> response_ = absl::nullopt;
  176. absl::optional<DecryptedPasskey> passkey_ = absl::nullopt;
  177. std::unique_ptr<MockQuickPairProcessManager> process_manager_;
  178. mojo::SharedRemote<ash::quick_pair::mojom::FastPairDataParser>
  179. data_parser_remote_;
  180. mojo::PendingRemote<ash::quick_pair::mojom::FastPairDataParser>
  181. fast_pair_data_parser_;
  182. std::unique_ptr<ash::quick_pair::FastPairDataParser> data_parser_;
  183. private:
  184. scoped_refptr<Device> device_;
  185. std::unique_ptr<FakeFastPairRepository> repository_;
  186. base::test::TaskEnvironment task_environment_;
  187. base::WeakPtrFactory<FastPairDataEncryptorImplTest> weak_ptr_factory_{this};
  188. };
  189. TEST_P(FastPairDataEncryptorImplTest, FailedSetUpNoMetadata) {
  190. EXPECT_FALSE(data_encryptor_);
  191. FailedSetUpNoMetadata();
  192. base::RunLoop().RunUntilIdle();
  193. EXPECT_FALSE(data_encryptor_);
  194. }
  195. TEST_P(FastPairDataEncryptorImplTest, SuccessfulSetUp) {
  196. EXPECT_FALSE(data_encryptor_);
  197. SuccessfulSetUp();
  198. base::RunLoop().RunUntilIdle();
  199. EXPECT_TRUE(data_encryptor_);
  200. }
  201. TEST_P(FastPairDataEncryptorImplTest, EncryptBytes) {
  202. SuccessfulSetUp();
  203. base::RunLoop().RunUntilIdle();
  204. EXPECT_TRUE(data_encryptor_);
  205. EXPECT_FALSE(EncryptBytes().empty());
  206. }
  207. TEST_P(FastPairDataEncryptorImplTest, ParseDecryptedResponse) {
  208. SuccessfulSetUp();
  209. base::RunLoop().RunUntilIdle();
  210. EXPECT_TRUE(data_encryptor_);
  211. EXPECT_CALL(*process_manager_, GetProcessReference);
  212. ParseDecryptedResponse();
  213. base::RunLoop().RunUntilIdle();
  214. }
  215. TEST_P(FastPairDataEncryptorImplTest, ParseDecryptedPasskey) {
  216. SuccessfulSetUp();
  217. base::RunLoop().RunUntilIdle();
  218. EXPECT_TRUE(data_encryptor_);
  219. EXPECT_CALL(*process_manager_, GetProcessReference);
  220. ParseDecryptedPasskey();
  221. base::RunLoop().RunUntilIdle();
  222. }
  223. TEST_P(FastPairDataEncryptorImplTest, ParseDecryptedPasskey_InvalidInputSize) {
  224. SuccessfulSetUp();
  225. base::RunLoop().RunUntilIdle();
  226. EXPECT_TRUE(data_encryptor_);
  227. EXPECT_CALL(*process_manager_, GetProcessReference).Times(0);
  228. ParseDecryptedPasskeyInvalidBytes();
  229. base::RunLoop().RunUntilIdle();
  230. }
  231. TEST_P(FastPairDataEncryptorImplTest, ParseDecryptedResponse_InvalidInputSize) {
  232. SuccessfulSetUp();
  233. base::RunLoop().RunUntilIdle();
  234. EXPECT_TRUE(data_encryptor_);
  235. EXPECT_CALL(*process_manager_, GetProcessReference).Times(0);
  236. ParseDecryptedResponseInvalidBytes();
  237. base::RunLoop().RunUntilIdle();
  238. }
  239. TEST_P(FastPairDataEncryptorImplTest, NoKeyPair) {
  240. FailedSetUpNoKeyPair();
  241. base::RunLoop().RunUntilIdle();
  242. EXPECT_FALSE(data_encryptor_);
  243. }
  244. // TODO(crbug.com/1298377) flaky on ASan + LSan bots
  245. #if defined(ADDRESS_SANITIZER) && defined(LEAK_SANITIZER)
  246. #define MAYBE_ParseDecryptedPasskey_ProcessStopped \
  247. DISABLED_ParseDecryptedPasskey_ProcessStopped
  248. #else
  249. #define MAYBE_ParseDecryptedPasskey_ProcessStopped \
  250. ParseDecryptedPasskey_ProcessStopped
  251. #endif
  252. TEST_P(FastPairDataEncryptorImplTest,
  253. MAYBE_ParseDecryptedPasskey_ProcessStopped) {
  254. SuccessfulSetUp();
  255. base::RunLoop().RunUntilIdle();
  256. EXPECT_TRUE(data_encryptor_);
  257. EXPECT_CALL(*process_manager_, GetProcessReference)
  258. .WillRepeatedly(
  259. [&](QuickPairProcessManager::ProcessStoppedCallback callback) {
  260. std::move(callback).Run(
  261. QuickPairProcessManager::ShutdownReason::kCrash);
  262. return std::make_unique<
  263. QuickPairProcessManagerImpl::ProcessReferenceImpl>(
  264. data_parser_remote_, base::DoNothing());
  265. });
  266. ParseDecryptedPasskey();
  267. base::RunLoop().RunUntilIdle();
  268. }
  269. // TODO(crbug.com/1298377) flaky on ASan + LSan bots
  270. #if defined(ADDRESS_SANITIZER) && defined(LEAK_SANITIZER)
  271. #define MAYBE_ParseDecryptedResponse_ProcessStopped \
  272. DISABLED_ParseDecryptedResponse_ProcessStopped
  273. #else
  274. #define MAYBE_ParseDecryptedResponse_ProcessStopped \
  275. ParseDecryptedResponse_ProcessStopped
  276. #endif
  277. TEST_P(FastPairDataEncryptorImplTest,
  278. MAYBE_ParseDecryptedResponse_ProcessStopped) {
  279. SuccessfulSetUp();
  280. base::RunLoop().RunUntilIdle();
  281. EXPECT_TRUE(data_encryptor_);
  282. EXPECT_CALL(*process_manager_, GetProcessReference)
  283. .WillRepeatedly(
  284. [&](QuickPairProcessManager::ProcessStoppedCallback callback) {
  285. std::move(callback).Run(
  286. QuickPairProcessManager::ShutdownReason::kCrash);
  287. return std::make_unique<
  288. QuickPairProcessManagerImpl::ProcessReferenceImpl>(
  289. data_parser_remote_, base::DoNothing());
  290. });
  291. ParseDecryptedResponse();
  292. base::RunLoop().RunUntilIdle();
  293. }
  294. TEST_P(FastPairDataEncryptorImplTest, GetPublicKey) {
  295. SuccessfulSetUpToTestPublicKey();
  296. base::RunLoop().RunUntilIdle();
  297. EXPECT_TRUE(data_encryptor_);
  298. EXPECT_CALL(*process_manager_, GetProcessReference);
  299. ParseDecryptedPasskey();
  300. base::RunLoop().RunUntilIdle();
  301. EXPECT_NE(data_encryptor_->GetPublicKey(), absl::nullopt);
  302. }
  303. INSTANTIATE_TEST_SUITE_P(FastPairDataEncryptorImplTest,
  304. FastPairDataEncryptorImplTest,
  305. testing::Bool());
  306. } // namespace quick_pair
  307. } // namespace ash