mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Use alphanumeric characters for PresenceDevice endpoint id generation
PiperOrigin-RevId: 540321021
This commit is contained in:
committed by
Copybara-Service
parent
2c90a61fca
commit
cf8afb73aa
+1
-2
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user