quick_pair_process_unittest.cc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/services/quick_pair/quick_pair_process.h"
  5. #include <cstdint>
  6. #include <vector>
  7. #include "ash/quick_pair/fast_pair_handshake/fast_pair_encryption.h"
  8. #include "ash/services/quick_pair/fast_pair_data_parser.h"
  9. #include "ash/services/quick_pair/public/cpp/decrypted_passkey.h"
  10. #include "ash/services/quick_pair/public/cpp/decrypted_response.h"
  11. #include "ash/services/quick_pair/public/cpp/not_discoverable_advertisement.h"
  12. #include "ash/services/quick_pair/quick_pair_process_manager.h"
  13. #include "ash/services/quick_pair/quick_pair_process_manager_impl.h"
  14. #include "base/callback_helpers.h"
  15. #include "base/run_loop.h"
  16. #include "base/test/bind.h"
  17. #include "base/test/task_environment.h"
  18. #include "mojo/public/cpp/bindings/shared_remote.h"
  19. #include "testing/gtest/include/gtest/gtest.h"
  20. namespace {
  21. const std::array<uint8_t, 16> kResponseBytes = {
  22. 0x01, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D,
  23. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6};
  24. const std::array<uint8_t, 16> kAesKeyBytes = {
  25. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6,
  26. 0xCF, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D};
  27. const std::array<uint8_t, 16> kPasskeyBytes = {
  28. 0x02, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D,
  29. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6};
  30. class FakeQuickPairProcessManager
  31. : public ash::quick_pair::QuickPairProcessManager {
  32. public:
  33. class FakeQuickPairProcessReference
  34. : public ash::quick_pair::QuickPairProcessManager::ProcessReference {
  35. public:
  36. FakeQuickPairProcessReference(
  37. mojo::SharedRemote<ash::quick_pair::mojom::FastPairDataParser>
  38. data_parser_remote)
  39. : data_parser_remote_(data_parser_remote) {}
  40. ~FakeQuickPairProcessReference() override = default;
  41. const mojo::SharedRemote<ash::quick_pair::mojom::FastPairDataParser>&
  42. GetFastPairDataParser() const override {
  43. return data_parser_remote_;
  44. }
  45. private:
  46. mojo::SharedRemote<ash::quick_pair::mojom::FastPairDataParser>
  47. data_parser_remote_;
  48. };
  49. FakeQuickPairProcessManager() {
  50. data_parser_ = std::make_unique<ash::quick_pair::FastPairDataParser>(
  51. fast_pair_data_parser_.InitWithNewPipeAndPassReceiver());
  52. data_parser_remote_.Bind(std::move(fast_pair_data_parser_),
  53. task_enviornment_.GetMainThreadTaskRunner());
  54. }
  55. ~FakeQuickPairProcessManager() override = default;
  56. std::unique_ptr<ProcessReference> GetProcessReference(
  57. ProcessStoppedCallback on_process_stopped_callback) override {
  58. return std::make_unique<FakeQuickPairProcessReference>(data_parser_remote_);
  59. }
  60. private:
  61. base::test::SingleThreadTaskEnvironment task_enviornment_;
  62. mojo::SharedRemote<ash::quick_pair::mojom::FastPairDataParser>
  63. data_parser_remote_;
  64. mojo::PendingRemote<ash::quick_pair::mojom::FastPairDataParser>
  65. fast_pair_data_parser_;
  66. std::unique_ptr<ash::quick_pair::FastPairDataParser> data_parser_;
  67. ProcessStoppedCallback on_process_stopped_callback_;
  68. };
  69. } // namespace
  70. namespace ash {
  71. namespace quick_pair {
  72. namespace quick_pair_process {
  73. class QuickPairProcessTest : public testing::Test {};
  74. TEST_F(QuickPairProcessTest,
  75. GetHexModelIdFromServiceData_NoValueIfNoProcessManagerSet) {
  76. GetHexModelIdFromServiceData(
  77. std::vector<uint8_t>(),
  78. base::BindLambdaForTesting([](const absl::optional<std::string>& result) {
  79. EXPECT_FALSE(result.has_value());
  80. }),
  81. base::DoNothing());
  82. }
  83. TEST_F(QuickPairProcessTest,
  84. ParseDecryptedResponse_NoValueIfNoProcessManagerSet) {
  85. ParseDecryptedResponse(
  86. std::vector<uint8_t>(), std::vector<uint8_t>(),
  87. base::BindLambdaForTesting(
  88. [](const absl::optional<DecryptedResponse>& result) {
  89. EXPECT_FALSE(result.has_value());
  90. }),
  91. base::DoNothing());
  92. }
  93. TEST_F(QuickPairProcessTest,
  94. ParseDecryptedPasskey_NoValueIfNoProcessManagerSet) {
  95. ParseDecryptedPasskey(std::vector<uint8_t>(), std::vector<uint8_t>(),
  96. base::BindLambdaForTesting(
  97. [](const absl::optional<DecryptedPasskey>& result) {
  98. EXPECT_FALSE(result.has_value());
  99. }),
  100. base::DoNothing());
  101. }
  102. TEST_F(QuickPairProcessTest,
  103. ParseNotDiscoverableAdvertisement_NoValueIfNoProcessManagerSet) {
  104. ParseNotDiscoverableAdvertisement(
  105. /*service_data=*/std::vector<uint8_t>(), /*address=*/"",
  106. base::BindLambdaForTesting(
  107. [](const absl::optional<NotDiscoverableAdvertisement>& result) {
  108. EXPECT_FALSE(result.has_value());
  109. }),
  110. base::DoNothing());
  111. }
  112. TEST_F(QuickPairProcessTest,
  113. ParseMessageStreamMessages_NoValueIfNoProcessManagerSet) {
  114. ParseMessageStreamMessages(
  115. std::vector<uint8_t>(),
  116. base::BindLambdaForTesting(
  117. [](std::vector<mojom::MessageStreamMessagePtr> messages) {
  118. EXPECT_TRUE(messages.empty());
  119. }),
  120. base::DoNothing());
  121. }
  122. TEST_F(QuickPairProcessTest, ParseDecryptedResponse_ValueIfProcessManagerSet) {
  123. auto process_manager = std::make_unique<FakeQuickPairProcessManager>();
  124. quick_pair_process::SetProcessManager(process_manager.get());
  125. const std::array<uint8_t, 16> encrypted_response_bytes =
  126. fast_pair_encryption::EncryptBytes(kAesKeyBytes, kResponseBytes);
  127. base::RunLoop run_loop;
  128. ParseDecryptedResponse(
  129. std::vector<uint8_t>(kAesKeyBytes.begin(), kAesKeyBytes.end()),
  130. std::vector<uint8_t>(encrypted_response_bytes.begin(),
  131. encrypted_response_bytes.end()),
  132. base::BindLambdaForTesting(
  133. [&run_loop](const absl::optional<DecryptedResponse>& result) {
  134. EXPECT_TRUE(result.has_value());
  135. run_loop.Quit();
  136. }),
  137. base::DoNothing());
  138. run_loop.Run();
  139. }
  140. TEST_F(QuickPairProcessTest, ParseDecryptedPasskey_ValueIfProcessManagerSet) {
  141. auto process_manager = std::make_unique<FakeQuickPairProcessManager>();
  142. quick_pair_process::SetProcessManager(process_manager.get());
  143. const std::array<uint8_t, 16> encrypted_passkey_bytes =
  144. fast_pair_encryption::EncryptBytes(kAesKeyBytes, kPasskeyBytes);
  145. base::RunLoop run_loop;
  146. ParseDecryptedPasskey(
  147. std::vector<uint8_t>(kAesKeyBytes.begin(), kAesKeyBytes.end()),
  148. std::vector<uint8_t>(encrypted_passkey_bytes.begin(),
  149. encrypted_passkey_bytes.end()),
  150. base::BindLambdaForTesting(
  151. [&run_loop](const absl::optional<DecryptedPasskey>& result) {
  152. EXPECT_TRUE(result.has_value());
  153. run_loop.Quit();
  154. }),
  155. base::DoNothing());
  156. run_loop.Run();
  157. }
  158. } // namespace quick_pair_process
  159. } // namespace quick_pair
  160. } // namespace ash