Convert certificate ID bytes to a hex string for device ID when mac address is not available

PiperOrigin-RevId: 846851458
This commit is contained in:
Nick Bourdakos
2025-12-19 13:43:56 -08:00
committed by Copybara-Service
parent 14e14bc0a2
commit 9a25fc22ff
3 changed files with 92 additions and 1 deletions
+17
View File
@@ -738,6 +738,23 @@ cc_test(
],
)
cc_test(
name = "nearby_sharing_util_test",
srcs = ["nearby_sharing_util_test.cc"],
deps = [
":nearby_sharing_util",
"//internal/platform/implementation:platform_impl",
"//sharing/certificates",
"//sharing/proto:enums_cc_proto",
"//sharing/proto:share_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "payload_tracker_test",
srcs = ["payload_tracker_test.cc"],
+4 -1
View File
@@ -23,6 +23,7 @@
#include <vector>
#include "absl/hash/hash.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "internal/base/file_path.h"
@@ -106,7 +107,9 @@ std::string GetDeviceId(
}
if (!certificate->id().empty()) {
return std::string(certificate->id().begin(), certificate->id().end());
return absl::BytesToHexString(absl::string_view(
reinterpret_cast<const char*>(certificate->id().data()),
certificate->id().size()));
}
return std::string(endpoint_id);
+71
View File
@@ -0,0 +1,71 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sharing/nearby_sharing_util.h"
#include <optional>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "absl/strings/escaping.h"
#include "absl/strings/string_view.h"
#include "absl/time/clock.h"
#include "sharing/certificates/nearby_share_decrypted_public_certificate.h"
#include "sharing/certificates/nearby_share_encrypted_metadata_key.h"
#include "sharing/certificates/nearby_share_private_certificate.h"
#include "sharing/proto/encrypted_metadata.pb.h"
#include "sharing/proto/enums.pb.h"
#include "sharing/proto/rpc_resources.pb.h"
namespace nearby::sharing {
TEST(GetDeviceIdTest, CertificateAvailable_NoMacAddress_HasCertificateId) {
const std::string kEndpointId = "endpoint-123";
// Create metadata without Bluetooth MAC address
nearby::sharing::proto::EncryptedMetadata metadata;
metadata.set_device_name("TestDevice");
// Create private certificate
NearbySharePrivateCertificate private_cert(
nearby::sharing::proto::DeviceVisibility::DEVICE_VISIBILITY_SELF_SHARE,
absl::Now(), metadata);
// Generate EncryptedMetadataKey
std::optional<NearbyShareEncryptedMetadataKey> encrypted_metadata_key =
private_cert.EncryptMetadataKey();
ASSERT_TRUE(encrypted_metadata_key.has_value());
// Generate PublicCertificate
std::optional<nearby::sharing::proto::PublicCertificate> public_cert =
private_cert.ToPublicCertificate();
ASSERT_TRUE(public_cert.has_value());
// Decrypt PublicCertificate
std::optional<NearbyShareDecryptedPublicCertificate> certificate =
NearbyShareDecryptedPublicCertificate::DecryptPublicCertificate(
*public_cert, *encrypted_metadata_key);
ASSERT_TRUE(certificate.has_value());
// Calculate expected Device ID from certificate ID
std::string expected_device_id = absl::BytesToHexString(
absl::string_view(reinterpret_cast<const char*>(certificate->id().data()),
certificate->id().size()));
EXPECT_EQ(GetDeviceId(kEndpointId, certificate), expected_device_id);
}
} // namespace nearby::sharing