diff --git a/fastpair/dataparser/fast_pair_data_parser.cc b/fastpair/dataparser/fast_pair_data_parser.cc index 904dfd20..6275e762 100644 --- a/fastpair/dataparser/fast_pair_data_parser.cc +++ b/fastpair/dataparser/fast_pair_data_parser.cc @@ -65,10 +65,9 @@ void ConvertVectorsToArrays( void FastPairDataParser::GetHexModelIdFromServiceData( const std::vector& service_data, GetHexModelIdFromServiceDataCallback callback) { - callback.on_retrieved_cb( - FastPairDecoder::HasModelId(&service_data) - ? FastPairDecoder::GetHexModelIdFromServiceData(&service_data) - : std::nullopt); + callback(FastPairDecoder::HasModelId(&service_data) + ? FastPairDecoder::GetHexModelIdFromServiceData(&service_data) + : std::nullopt); } void FastPairDataParser::ParseDecryptedResponse( @@ -76,7 +75,7 @@ void FastPairDataParser::ParseDecryptedResponse( const std::vector& encrypted_response_bytes, ParseDecryptResponseCallback callback) { if (!ValidateInputSizes(aes_key_bytes, encrypted_response_bytes)) { - callback.on_decrypted_cb(std::nullopt); + callback(std::nullopt); return; } @@ -84,8 +83,7 @@ void FastPairDataParser::ParseDecryptedResponse( std::array bytes; ConvertVectorsToArrays(aes_key_bytes, encrypted_response_bytes, key, bytes); - callback.on_decrypted_cb( - FastPairDecryption::ParseDecryptResponse(key, bytes)); + callback(FastPairDecryption::ParseDecryptResponse(key, bytes)); } void FastPairDataParser::ParseDecryptedPasskey( @@ -93,7 +91,7 @@ void FastPairDataParser::ParseDecryptedPasskey( const std::vector& encrypted_passkey_bytes, ParseDecryptPasskeyCallback callback) { if (!ValidateInputSizes(aes_key_bytes, encrypted_passkey_bytes)) { - callback.on_decrypted_cb(std::nullopt); + callback(std::nullopt); return; } @@ -101,7 +99,7 @@ void FastPairDataParser::ParseDecryptedPasskey( std::array bytes; ConvertVectorsToArrays(aes_key_bytes, encrypted_passkey_bytes, key, bytes); - callback.on_decrypted_cb(FastPairDecryption::ParseDecryptPasskey(key, bytes)); + callback(FastPairDecryption::ParseDecryptPasskey(key, bytes)); } } // namespace fastpair diff --git a/fastpair/dataparser/fast_pair_data_parser.h b/fastpair/dataparser/fast_pair_data_parser.h index b3fb9247..926d9ec6 100644 --- a/fastpair/dataparser/fast_pair_data_parser.h +++ b/fastpair/dataparser/fast_pair_data_parser.h @@ -32,23 +32,19 @@ namespace nearby { namespace fastpair { -struct GetHexModelIdFromServiceDataCallback { - absl::AnyInvocable)> on_retrieved_cb = - [](std::optional) {}; -}; -struct ParseDecryptResponseCallback { - absl::AnyInvocable)> on_decrypted_cb = - [](std::optional) {}; -}; - -struct ParseDecryptPasskeyCallback { - absl::AnyInvocable)> on_decrypted_cb = - [](std::optional) {}; -}; // This class is responsible for parsing the untrusted bytes for Fast Pair. class FastPairDataParser { + using GetHexModelIdFromServiceDataCallback = + absl::AnyInvocable)>; + + using ParseDecryptResponseCallback = + absl::AnyInvocable)>; + + using ParseDecryptPasskeyCallback = + absl::AnyInvocable)>; + public: // Gets the hex string representation of the device's model ID from the // service data. diff --git a/fastpair/dataparser/fast_pair_data_parser_test.cc b/fastpair/dataparser/fast_pair_data_parser_test.cc index 1bab9f4b..9e661814 100644 --- a/fastpair/dataparser/fast_pair_data_parser_test.cc +++ b/fastpair/dataparser/fast_pair_data_parser_test.cc @@ -14,21 +14,16 @@ #include "fastpair/dataparser/fast_pair_data_parser.h" -#include #include #include #include -#include #include #include #include #include -#include #include -#include "gmock/gmock.h" -#include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" #include "absl/strings/string_view.h" #include "absl/synchronization/notification.h" @@ -55,16 +50,11 @@ TEST(FastPairDataParserTest, GetHexModelIdFromServiceDataUnsucessfully) { .Build() ->CreateServiceData(); absl::Notification notification; - GetHexModelIdFromServiceDataCallback callback; - callback.on_retrieved_cb = - [¬ification](std::optional model_id) { + FastPairDataParser::GetHexModelIdFromServiceData( + service_data, [¬ification](std::optional model_id) { EXPECT_EQ(model_id, std::nullopt); notification.Notify(); - }; - - FastPairDataParser::GetHexModelIdFromServiceData(service_data, - std::move(callback)); - + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -75,15 +65,11 @@ TEST(FastPairDataParserTest, GetHexModelIdFromServiceDataSuccessfully) { .Build() ->CreateServiceData(); absl::Notification notification; - GetHexModelIdFromServiceDataCallback callback; - callback.on_retrieved_cb = - [¬ification](std::optional model_id) { + FastPairDataParser::GetHexModelIdFromServiceData( + service_data, [¬ification](std::optional model_id) { EXPECT_EQ(model_id, kModelId); notification.Notify(); - }; - - FastPairDataParser::GetHexModelIdFromServiceData(service_data, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -119,15 +105,12 @@ TEST(FastPairDataParserTest, DecryptResponseUnsuccessfullyWithInvalidAesKey) { encrypted_bytes_array.end()); absl::Notification notification; - ParseDecryptResponseCallback callback; - callback.on_decrypted_cb = + FastPairDataParser::ParseDecryptedResponse( + kAesKeyBytes, encrypted_bytes, [¬ification](std::optional response) { EXPECT_FALSE(response.has_value()); notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedResponse(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -162,15 +145,12 @@ TEST(FastPairDataParserTest, DecryptResponseUnsuccessfullyWithInvalidResponse) { encrypted_bytes_array.end() - 1); absl::Notification notification; - ParseDecryptResponseCallback callback; - callback.on_decrypted_cb = + FastPairDataParser::ParseDecryptedResponse( + kAesKeyBytes, encrypted_bytes, [¬ification](std::optional response) { EXPECT_FALSE(response.has_value()); notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedResponse(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -204,19 +184,17 @@ TEST(FastPairDataParserTest, DecryptResponseSuccessfully) { encrypted_bytes_array.end()); absl::Notification notification; - ParseDecryptResponseCallback callback; - callback.on_decrypted_cb = [¬ification, &kAddressBytes, &kSalt]( - std::optional response) { - EXPECT_TRUE(response.has_value()); - EXPECT_EQ(response.value().message_type, - FastPairMessageType::kKeyBasedPairingResponse); - EXPECT_EQ(response.value().address_bytes, kAddressBytes); - EXPECT_EQ(response.value().salt, kSalt); - notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedResponse(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + FastPairDataParser::ParseDecryptedResponse( + kAesKeyBytes, encrypted_bytes, + [¬ification, &kAddressBytes, + &kSalt](std::optional response) { + EXPECT_TRUE(response.has_value()); + EXPECT_EQ(response.value().message_type, + FastPairMessageType::kKeyBasedPairingResponse); + EXPECT_EQ(response.value().address_bytes, kAddressBytes); + EXPECT_EQ(response.value().salt, kSalt); + notification.Notify(); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -251,15 +229,12 @@ TEST(FastPairDataParserTest, DecryptPasskeyUnsuccessfullyWithInvalidAesKey) { encrypted_bytes_array.end()); absl::Notification notification; - ParseDecryptPasskeyCallback callback; - callback.on_decrypted_cb = + FastPairDataParser::ParseDecryptedPasskey( + kAesKeyBytes, encrypted_bytes, [¬ification](std::optional decrypted_passkey) { EXPECT_FALSE(decrypted_passkey.has_value()); notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedPasskey(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -293,15 +268,12 @@ TEST(FastPairDataParserTest, DecryptPasskeyUnsuccessfullyWithInvalidPasskey) { encrypted_bytes_array.end() - 1); absl::Notification notification; - ParseDecryptPasskeyCallback callback; - callback.on_decrypted_cb = + FastPairDataParser::ParseDecryptedPasskey( + kAesKeyBytes, encrypted_bytes, [¬ification](std::optional decrypted_passkey) { EXPECT_FALSE(decrypted_passkey.has_value()); notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedPasskey(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -334,8 +306,8 @@ TEST(FastPairDataParserTest, DecryptSeekerPasskeySuccessfully) { encrypted_bytes_array.end()); absl::Notification notification; - ParseDecryptPasskeyCallback callback; - callback.on_decrypted_cb = + FastPairDataParser::ParseDecryptedPasskey( + kAesKeyBytes, encrypted_bytes, [¬ification, &kPasskey, &kSalt](std::optional decrypted_passkey) { EXPECT_TRUE(decrypted_passkey.has_value()); @@ -344,10 +316,7 @@ TEST(FastPairDataParserTest, DecryptSeekerPasskeySuccessfully) { EXPECT_EQ(decrypted_passkey.value().passkey, kPasskey); EXPECT_EQ(decrypted_passkey.value().salt, kSalt); notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedPasskey(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } @@ -380,8 +349,8 @@ TEST(FastPairDataParserTest, DecryptProviderPasskeySuccessfully) { encrypted_bytes_array.end()); absl::Notification notification; - ParseDecryptPasskeyCallback callback; - callback.on_decrypted_cb = + FastPairDataParser::ParseDecryptedPasskey( + kAesKeyBytes, encrypted_bytes, [¬ification, &kPasskey, &kSalt](std::optional decrypted_passkey) { EXPECT_TRUE(decrypted_passkey.has_value()); @@ -390,10 +359,7 @@ TEST(FastPairDataParserTest, DecryptProviderPasskeySuccessfully) { EXPECT_EQ(decrypted_passkey.value().passkey, kPasskey); EXPECT_EQ(decrypted_passkey.value().salt, kSalt); notification.Notify(); - }; - - FastPairDataParser::ParseDecryptedPasskey(kAesKeyBytes, encrypted_bytes, - std::move(callback)); + }); EXPECT_TRUE(notification.WaitForNotificationWithTimeout(kWaitTimeout)); } diff --git a/fastpair/handshake/fast_pair_data_encryptor_impl.cc b/fastpair/handshake/fast_pair_data_encryptor_impl.cc index cddf2918..09d107d9 100644 --- a/fastpair/handshake/fast_pair_data_encryptor_impl.cc +++ b/fastpair/handshake/fast_pair_data_encryptor_impl.cc @@ -151,10 +151,7 @@ void FastPairDataEncryptorImpl::ParseDecryptResponse( FastPairDataParser::ParseDecryptedResponse( std::vector(shared_secret_key_.begin(), shared_secret_key_.end()), - encrypted_response_bytes, - { - .on_decrypted_cb = std::move(callback), - }); + encrypted_response_bytes, std::move(callback)); } void FastPairDataEncryptorImpl::ParseDecryptPasskey( @@ -167,10 +164,7 @@ void FastPairDataEncryptorImpl::ParseDecryptPasskey( FastPairDataParser::ParseDecryptedPasskey( std::vector(shared_secret_key_.begin(), shared_secret_key_.end()), - encrypted_passkey_bytes, - { - .on_decrypted_cb = std::move(callback), - }); + encrypted_passkey_bytes, std::move(callback)); } } // namespace fastpair } // namespace nearby