Use alphanumeric characters for PresenceDevice endpoint id generation

PiperOrigin-RevId: 540321021
This commit is contained in:
Anay Wadhera
2023-06-14 10:59:49 -07:00
committed by Copybara-Service
parent 2c90a61fca
commit cf8afb73aa
3 changed files with 18 additions and 5 deletions
+1 -2
View File
@@ -86,11 +86,10 @@ cc_library(
"scan_request_builder.h",
],
deps = [
"//internal/crypto",
"//internal/interop:device",
"//internal/platform:base",
"//internal/platform:connection_info",
"//internal/platform:logging",
"//internal/platform:types",
"//internal/platform/implementation:types",
"//internal/proto:credential_cc_proto",
"//internal/proto:metadata_cc_proto",
+10 -3
View File
@@ -17,20 +17,27 @@
#include <string>
#include <vector>
#include "internal/platform/implementation/crypto.h"
#include "internal/interop/device.h"
#include "internal/platform/ble_connection_info.h"
#include "internal/platform/implementation/system_clock.h"
#include "internal/platform/prng.h"
#include "presence/device_motion.h"
namespace nearby {
namespace presence {
namespace {
constexpr char kEndpointIdChars[] = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
std::string GenerateRandomEndpointId() {
std::string result(kEndpointIdLength, 0);
crypto::RandBytes(const_cast<std::string::value_type*>(result.data()),
result.size());
nearby::Prng prng;
for (int i = 0; i < kEndpointIdLength; i++) {
result[i] = kEndpointIdChars[prng.NextUint32() % sizeof(kEndpointIdChars)];
}
return result;
}
} // namespace
+7
View File
@@ -117,6 +117,13 @@ TEST(PresenceDeviceTest, TestEndpointIdIsCorrectLength) {
EXPECT_EQ(device.GetEndpointId().length(), kEndpointIdLength);
}
TEST(PresenceDeviceTest, TestEndpointIdIsRandom) {
Metadata metadata = CreateTestMetadata();
PresenceDevice device = PresenceDevice({kDefaultMotionType}, metadata);
EXPECT_EQ(device.GetEndpointId().length(), kEndpointIdLength);
EXPECT_NE(device.GetEndpointId(), std::string(kEndpointIdLength, 0));
}
} // namespace
} // namespace presence
} // namespace nearby