mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 07:36:10 -04:00
Pure Refactor callback structure in FastPairDaraParser
PiperOrigin-RevId: 534131868
This commit is contained in:
committed by
Copybara-Service
parent
d31a39249c
commit
9ee02c43ad
@@ -65,10 +65,9 @@ void ConvertVectorsToArrays(
|
||||
void FastPairDataParser::GetHexModelIdFromServiceData(
|
||||
const std::vector<uint8_t>& 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<uint8_t>& 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<uint8_t, kEncryptedDataByteSize> 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<uint8_t>& 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<uint8_t, kEncryptedDataByteSize> 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
|
||||
|
||||
@@ -32,23 +32,19 @@
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
struct GetHexModelIdFromServiceDataCallback {
|
||||
absl::AnyInvocable<void(std::optional<absl::string_view>)> on_retrieved_cb =
|
||||
[](std::optional<absl::string_view>) {};
|
||||
};
|
||||
|
||||
struct ParseDecryptResponseCallback {
|
||||
absl::AnyInvocable<void(std::optional<DecryptedResponse>)> on_decrypted_cb =
|
||||
[](std::optional<DecryptedResponse>) {};
|
||||
};
|
||||
|
||||
struct ParseDecryptPasskeyCallback {
|
||||
absl::AnyInvocable<void(std::optional<DecryptedPasskey>)> on_decrypted_cb =
|
||||
[](std::optional<DecryptedPasskey>) {};
|
||||
};
|
||||
|
||||
// This class is responsible for parsing the untrusted bytes for Fast Pair.
|
||||
class FastPairDataParser {
|
||||
using GetHexModelIdFromServiceDataCallback =
|
||||
absl::AnyInvocable<void(std::optional<absl::string_view>)>;
|
||||
|
||||
using ParseDecryptResponseCallback =
|
||||
absl::AnyInvocable<void(std::optional<DecryptedResponse>)>;
|
||||
|
||||
using ParseDecryptPasskeyCallback =
|
||||
absl::AnyInvocable<void(std::optional<DecryptedPasskey>)>;
|
||||
|
||||
public:
|
||||
// Gets the hex string representation of the device's model ID from the
|
||||
// service data.
|
||||
|
||||
@@ -14,21 +14,16 @@
|
||||
|
||||
#include "fastpair/dataparser/fast_pair_data_parser.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <iterator>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#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<absl::string_view> model_id) {
|
||||
FastPairDataParser::GetHexModelIdFromServiceData(
|
||||
service_data, [¬ification](std::optional<absl::string_view> 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<absl::string_view> model_id) {
|
||||
FastPairDataParser::GetHexModelIdFromServiceData(
|
||||
service_data, [¬ification](std::optional<absl::string_view> 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<DecryptedResponse> 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<DecryptedResponse> 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<DecryptedResponse> 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<DecryptedResponse> 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<DecryptedPasskey> 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<DecryptedPasskey> 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<DecryptedPasskey> 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<DecryptedPasskey> 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));
|
||||
}
|
||||
|
||||
|
||||
@@ -151,10 +151,7 @@ void FastPairDataEncryptorImpl::ParseDecryptResponse(
|
||||
FastPairDataParser::ParseDecryptedResponse(
|
||||
std::vector<uint8_t>(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<uint8_t>(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
|
||||
|
||||
Reference in New Issue
Block a user