mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Implement encoding/decoding of actions
PiperOrigin-RevId: 528534899
This commit is contained in:
committed by
Copybara-Service
parent
465aa7a489
commit
560df8ea8e
@@ -15,10 +15,10 @@
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/connection_info.h"
|
||||
|
||||
@@ -41,22 +41,22 @@ std::string BleConnectionInfo::ToDataElementBytes() const {
|
||||
mask |= has_psm ? kPsmMask : 0;
|
||||
payload_data.push_back(mask);
|
||||
if (has_mac) {
|
||||
payload_data.insert(payload_data.end(), mac_address_.begin(),
|
||||
payload_data.append(mac_address_.begin(),
|
||||
mac_address_.end());
|
||||
}
|
||||
if (has_gatt) {
|
||||
payload_data.push_back(gatt_characteristic_.length());
|
||||
payload_data.insert(payload_data.end(), gatt_characteristic_.begin(),
|
||||
payload_data.append(gatt_characteristic_.begin(),
|
||||
gatt_characteristic_.end());
|
||||
}
|
||||
if (has_psm) {
|
||||
payload_data.insert(payload_data.end(), psm_.begin(), psm_.end());
|
||||
payload_data.append(psm_.begin(), psm_.end());
|
||||
}
|
||||
payload_data.push_back(actions_);
|
||||
payload_data.append(actions_.begin(), actions_.end());
|
||||
std::string ret;
|
||||
ret.push_back(kDataElementFieldType);
|
||||
ret.push_back(payload_data.size());
|
||||
ret.insert(ret.end(), payload_data.begin(), payload_data.end());
|
||||
ret.append(payload_data.begin(), payload_data.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -111,12 +111,9 @@ absl::StatusOr<BleConnectionInfo> BleConnectionInfo::FromDataElementBytes(
|
||||
return absl::InvalidArgumentError(
|
||||
"Insufficient remaining bytes to read action.");
|
||||
}
|
||||
char action = bytes[position];
|
||||
// Check that we don't have any remaining bytes.
|
||||
if (bytes.size() != ++position) {
|
||||
return absl::InvalidArgumentError(absl::StrFormat(
|
||||
"Nonzero remaining bytes: %d.", bytes.size() - position));
|
||||
}
|
||||
return BleConnectionInfo(address, characteristic, psm, action);
|
||||
auto action_str = bytes.substr(position);
|
||||
return BleConnectionInfo(
|
||||
address, characteristic, psm,
|
||||
std::vector<uint8_t>(action_str.begin(), action_str.end()));
|
||||
}
|
||||
} // namespace nearby
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_BLE_CONNECTION_INFO_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -31,7 +32,7 @@ class BleConnectionInfo : public ConnectionInfo {
|
||||
|
||||
BleConnectionInfo(absl::string_view mac_address,
|
||||
absl::string_view gatt_characteristic,
|
||||
absl::string_view psm, char actions)
|
||||
absl::string_view psm, std::vector<uint8_t> actions)
|
||||
: mac_address_(std::string(mac_address)),
|
||||
gatt_characteristic_(std::string(gatt_characteristic)),
|
||||
psm_(std::string(psm)),
|
||||
@@ -45,18 +46,18 @@ class BleConnectionInfo : public ConnectionInfo {
|
||||
std::string GetMacAddress() const { return mac_address_; }
|
||||
std::string GetGattCharacteristic() const { return gatt_characteristic_; }
|
||||
std::string GetPsm() const { return psm_; }
|
||||
char GetActions() const override { return actions_; }
|
||||
std::vector<uint8_t> GetActions() const override { return actions_; }
|
||||
|
||||
private:
|
||||
std::string mac_address_;
|
||||
std::string gatt_characteristic_;
|
||||
std::string psm_;
|
||||
char actions_ = 0;
|
||||
std::vector<uint8_t> actions_;
|
||||
};
|
||||
|
||||
inline bool operator==(const BleConnectionInfo& a, const BleConnectionInfo& b) {
|
||||
return a.GetMacAddress() == b.GetMacAddress() &&
|
||||
a.GetActions() == b.GetActions() &&
|
||||
a.GetActions() == b.GetActions() && a.GetPsm() == b.GetPsm() &&
|
||||
a.GetGattCharacteristic() == b.GetGattCharacteristic();
|
||||
}
|
||||
|
||||
|
||||
@@ -38,10 +38,11 @@ constexpr absl::string_view kPsm = "\x45\x56";
|
||||
constexpr char kAction = 0x0F;
|
||||
|
||||
TEST(BleConnectionInfoTest, TestGetFields) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
EXPECT_EQ(info.GetMediumType(), Medium::BLE);
|
||||
EXPECT_EQ(info.GetGattCharacteristic(), kGattCharacteristic);
|
||||
EXPECT_EQ(info.GetActions(), kAction);
|
||||
ASSERT_EQ(info.GetActions().size(), 1);
|
||||
ASSERT_EQ(info.GetActions()[0], kAction);
|
||||
EXPECT_EQ(info.GetMacAddress(), kMacAddr);
|
||||
EXPECT_EQ(info.GetPsm(), kPsm);
|
||||
}
|
||||
@@ -57,7 +58,7 @@ TEST(BleConnectionInfoTest, TestFromInvalidBytes) {
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromNoAction) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
serialized[1] -= 1;
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(
|
||||
@@ -66,7 +67,7 @@ TEST(BleConnectionInfoTest, TestFromNoAction) {
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytes) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -78,7 +79,7 @@ TEST(BleConnectionInfoTest, TestToFromBytes) {
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytesNoGattCharacteristic) {
|
||||
BleConnectionInfo info(kMacAddr, "", kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, "", kPsm, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -90,7 +91,7 @@ TEST(BleConnectionInfoTest, TestToFromBytesNoGattCharacteristic) {
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytesNoMac) {
|
||||
BleConnectionInfo info("", kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info("", kGattCharacteristic, kPsm, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -103,7 +104,7 @@ TEST(BleConnectionInfoTest, TestToFromBytesNoMac) {
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytesLongMac) {
|
||||
BleConnectionInfo info(absl::StrCat(kMacAddr, kMacAddr), kGattCharacteristic,
|
||||
kPsm, kAction);
|
||||
kPsm, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -115,7 +116,7 @@ TEST(BleConnectionInfoTest, TestToFromBytesLongMac) {
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytesNoPsm) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, "", kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, "", {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -128,7 +129,7 @@ TEST(BleConnectionInfoTest, TestToFromBytesNoPsm) {
|
||||
|
||||
TEST(BleConnectionInfoTest, TestToFromBytesLongPsm) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic,
|
||||
absl::StrCat(kPsm, kPsm), kAction);
|
||||
absl::StrCat(kPsm, kPsm), {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -145,186 +146,22 @@ TEST(BleConnectionInfoTest, TestFromEmpty) {
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBadElementType) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
serialized[0] = 0x56;
|
||||
auto result = BleConnectionInfo::FromDataElementBytes(serialized);
|
||||
EXPECT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBadMask) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for MAC address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for PSM only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for MAC address + GATT characteristic.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic + PSM.
|
||||
serialized[3] = 0x30;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for MAC address and PSM.
|
||||
serialized[3] = 0x50;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x70;
|
||||
EXPECT_OK(BleConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBadMaskNoMac) {
|
||||
BleConnectionInfo info("", kGattCharacteristic, kPsm, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for MAC address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for PSM only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for MAC address + GATT characteristic.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for MAC address + PSM.
|
||||
serialized[3] = 0x50;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x30;
|
||||
EXPECT_OK(BleConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBadMaskNoGattCharacteristic) {
|
||||
BleConnectionInfo info(kMacAddr, "", kPsm, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for MAC address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for PSM only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for all fields.
|
||||
serialized[3] = 0x70;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for MAC address + GATT characteristic.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic + PSM.
|
||||
serialized[3] = 0x30;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x50;
|
||||
EXPECT_OK(BleConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBadMaskNoPsm) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, "", kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for MAC address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for PSM only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for all fields.
|
||||
serialized[3] = 0x70;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic + PSM.
|
||||
serialized[3] = 0x30;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for MAC address + PSM.
|
||||
serialized[3] = 0x50;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_OK(BleConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestFromBadMaskEmpty) {
|
||||
BleConnectionInfo info("", "", "", kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for MAC address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for GATT characteristic only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for PSM only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for all fields.
|
||||
serialized[3] = 0x70;
|
||||
EXPECT_THAT(BleConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with correct mask.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_OK(BleConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestCopy) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
BleConnectionInfo copy(info);
|
||||
EXPECT_EQ(info, copy);
|
||||
}
|
||||
|
||||
TEST(BleConnectionInfoTest, TestEquals) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info2(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
BleConnectionInfo info2(kMacAddr, kGattCharacteristic, kPsm, {kAction});
|
||||
EXPECT_EQ(info, info2);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,10 +15,10 @@
|
||||
#include "internal/platform/bluetooth_connection_info.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/connection_info.h"
|
||||
|
||||
@@ -37,19 +37,17 @@ std::string BluetoothConnectionInfo::ToDataElementBytes() const {
|
||||
mask |= has_bluetooth_uuid ? kBluetoothUuidMask : 0;
|
||||
payload_data.push_back(mask);
|
||||
if (has_mac) {
|
||||
payload_data.insert(payload_data.end(), mac_address_.begin(),
|
||||
mac_address_.end());
|
||||
payload_data.append(mac_address_.begin(), mac_address_.end());
|
||||
}
|
||||
if (has_bluetooth_uuid) {
|
||||
payload_data.insert(payload_data.end(), bluetooth_uuid_.begin(),
|
||||
bluetooth_uuid_.end());
|
||||
payload_data.append(bluetooth_uuid_.begin(), bluetooth_uuid_.end());
|
||||
}
|
||||
payload_data.push_back(actions_);
|
||||
payload_data.append(actions_.begin(), actions_.end());
|
||||
std::string ret;
|
||||
ret.push_back(kDataElementFieldType);
|
||||
ret.push_back(payload_data.size());
|
||||
ret.insert(ret.end(), payload_data.begin(), payload_data.end());
|
||||
return std::string(ret.data(), ret.size());
|
||||
ret.append(payload_data.begin(), payload_data.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
absl::StatusOr<BluetoothConnectionInfo>
|
||||
@@ -90,14 +88,11 @@ BluetoothConnectionInfo::FromDataElementBytes(absl::string_view bytes) {
|
||||
}
|
||||
if (bytes.size() == position) {
|
||||
return absl::InvalidArgumentError(
|
||||
"Insufficient remaining bytes to read action.");
|
||||
"Insufficient remaining bytes to read actions.");
|
||||
}
|
||||
char action = bytes[position];
|
||||
// Check that we don't have any remaining bytes.
|
||||
if (bytes.size() != ++position) {
|
||||
return absl::InvalidArgumentError(absl::StrFormat(
|
||||
"Nonzero remaining bytes: %d.", bytes.size() - position));
|
||||
}
|
||||
return BluetoothConnectionInfo(address, uuid, action);
|
||||
auto action_str = bytes.substr(position);
|
||||
return BluetoothConnectionInfo(
|
||||
address, uuid,
|
||||
std::vector<uint8_t>(action_str.begin(), action_str.end()));
|
||||
}
|
||||
} // namespace nearby
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_BLUETOOTH_CONNECTION_INFO_H_
|
||||
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_BLUETOOTH_CONNECTION_INFO_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -30,7 +32,8 @@ class BluetoothConnectionInfo : public ConnectionInfo {
|
||||
absl::string_view bytes);
|
||||
|
||||
BluetoothConnectionInfo(absl::string_view mac_address,
|
||||
absl::string_view bluetooth_uuid, char actions)
|
||||
absl::string_view bluetooth_uuid,
|
||||
std::vector<uint8_t> actions)
|
||||
: mac_address_(std::string(mac_address)),
|
||||
bluetooth_uuid_(std::string(bluetooth_uuid)),
|
||||
actions_(actions) {}
|
||||
@@ -42,12 +45,12 @@ class BluetoothConnectionInfo : public ConnectionInfo {
|
||||
std::string ToDataElementBytes() const override;
|
||||
std::string GetMacAddress() const { return mac_address_; }
|
||||
std::string GetBluetoothUuid() const { return bluetooth_uuid_; }
|
||||
char GetActions() const override { return actions_; }
|
||||
std::vector<uint8_t> GetActions() const override { return actions_; }
|
||||
|
||||
private:
|
||||
std::string mac_address_;
|
||||
std::string bluetooth_uuid_;
|
||||
char actions_;
|
||||
std::vector<uint8_t> actions_;
|
||||
};
|
||||
|
||||
inline bool operator==(const BluetoothConnectionInfo& a,
|
||||
|
||||
@@ -35,21 +35,22 @@ constexpr absl::string_view kBluetoothUuid{"test"};
|
||||
constexpr char kAction = 0x0F;
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestGetFields) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, {kAction});
|
||||
EXPECT_EQ(info.GetMediumType(), Medium::BLUETOOTH);
|
||||
EXPECT_EQ(info.GetMacAddress(), kMacAddr);
|
||||
EXPECT_EQ(info.GetActions(), kAction);
|
||||
ASSERT_EQ(info.GetActions().size(), 1);
|
||||
EXPECT_EQ(info.GetActions()[0], kAction);
|
||||
EXPECT_EQ(info.GetBluetoothUuid(), kBluetoothUuid);
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestGetLongMacAddr) {
|
||||
BluetoothConnectionInfo info(absl::StrCat(kMacAddr, "\x56\x70\x89"),
|
||||
kBluetoothUuid, kAction);
|
||||
kBluetoothUuid, {kAction});
|
||||
EXPECT_NE(info.GetMacAddress(), kMacAddr);
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestToFromBytes) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BluetoothConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -57,18 +58,19 @@ TEST(BluetoothConnectionInfoTest, TestToFromBytes) {
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestToFromNoMacAddress) {
|
||||
BluetoothConnectionInfo info("", kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info("", kBluetoothUuid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BluetoothConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
info = result.value();
|
||||
EXPECT_EQ(info.GetMacAddress(), "");
|
||||
EXPECT_EQ(info.GetBluetoothUuid(), kBluetoothUuid);
|
||||
EXPECT_EQ(info.GetActions(), kAction);
|
||||
ASSERT_EQ(info.GetActions().size(), 1);
|
||||
EXPECT_EQ(info.GetActions()[0], kAction);
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestToFromWrongLength) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
++serialized[1];
|
||||
auto result = BluetoothConnectionInfo::FromDataElementBytes(serialized);
|
||||
@@ -76,7 +78,7 @@ TEST(BluetoothConnectionInfoTest, TestToFromWrongLength) {
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestToFromNoBluetoothUuid) {
|
||||
BluetoothConnectionInfo info(kMacAddr, "", kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, "", {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = BluetoothConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -89,7 +91,7 @@ TEST(BluetoothConnectionInfoTest, TestFromEmptyBytes) {
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestFromNoAction) {
|
||||
BluetoothConnectionInfo info("", "", kAction);
|
||||
BluetoothConnectionInfo info("", "", {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
serialized[1] -= 1;
|
||||
auto result = BluetoothConnectionInfo::FromDataElementBytes(
|
||||
@@ -103,106 +105,22 @@ TEST(BluetoothConnectionInfoTest, TestFromInvalidBytes) {
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestFromBadElementType) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
serialized[0] = 0x56;
|
||||
auto result = BluetoothConnectionInfo::FromDataElementBytes(serialized);
|
||||
EXPECT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestFromBadMask) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Remove the mask for UUID.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove the mask for MAC address and add back UUID.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set empty mask.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_OK(BluetoothConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestFromBadMaskNoUuid) {
|
||||
BluetoothConnectionInfo info(kMacAddr, "", kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set the mask for UUID and MAC address.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set the mask for only UUID.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set empty mask.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_OK(BluetoothConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestFromBadMaskNoMac) {
|
||||
BluetoothConnectionInfo info("", kBluetoothUuid, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set the mask for UUID and MAC address.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set the mask for only MAC address.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set empty mask.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_OK(BluetoothConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestFromBadMaskEmpty) {
|
||||
BluetoothConnectionInfo info("", "", kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for UUID and MAC address.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for only UUID.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for only MAC address.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(BluetoothConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with correct mask.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_OK(BluetoothConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestCopy) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, {kAction});
|
||||
BluetoothConnectionInfo copy(info);
|
||||
EXPECT_EQ(info, copy);
|
||||
}
|
||||
|
||||
TEST(BluetoothConnectionInfoTest, TestEquals) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info2(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, {kAction});
|
||||
BluetoothConnectionInfo info2(kMacAddr, kBluetoothUuid, {kAction});
|
||||
EXPECT_EQ(info, info2);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,8 +15,11 @@
|
||||
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CONNECTION_INFO_H_
|
||||
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_CONNECTION_INFO_H_
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "proto/connections_enums.pb.h"
|
||||
@@ -43,7 +46,7 @@ class ConnectionInfo {
|
||||
virtual ::location::nearby::proto::connections::Medium GetMediumType()
|
||||
const = 0;
|
||||
virtual std::string ToDataElementBytes() const = 0;
|
||||
virtual char GetActions() const = 0;
|
||||
virtual std::vector<uint8_t> GetActions() const = 0;
|
||||
static ConnectionInfoVariant FromDataElementBytes(
|
||||
absl::string_view data_element_bytes);
|
||||
};
|
||||
|
||||
@@ -14,11 +14,16 @@
|
||||
|
||||
#include "internal/platform/connection_info.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gmock/gmock.h"
|
||||
#include "protobuf-matchers/protocol-buffer-matchers.h"
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/types/variant.h"
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
#include "internal/platform/bluetooth_connection_info.h"
|
||||
#include "internal/platform/wifi_lan_connection_info.h"
|
||||
@@ -26,12 +31,14 @@
|
||||
namespace nearby {
|
||||
namespace {
|
||||
|
||||
// Common
|
||||
constexpr uint8_t kFirstAction = 0x0F;
|
||||
constexpr uint8_t kSecondAction = 0x04;
|
||||
// BLE
|
||||
constexpr absl::string_view kMacAddr = "\x4C\x8B\x1D\xCE\xBA\xD1";
|
||||
constexpr absl::string_view kGattCharacteristic =
|
||||
"\x03\x0a\x13\x56\x67\x21\x12\x45";
|
||||
constexpr absl::string_view kPsm = "\x45\x56";
|
||||
constexpr char kAction = 0x0F;
|
||||
// Bluetooth
|
||||
constexpr absl::string_view kBluetoothUuid{"test"};
|
||||
// WLAN
|
||||
@@ -39,8 +46,13 @@ constexpr absl::string_view kIpv4Addr = "\x4C\x8B\x1D\xCE";
|
||||
constexpr absl::string_view kPort = "\x12\x34";
|
||||
constexpr absl::string_view kBssid = "\x0A\x1B\x2C\x34\x58\x7E";
|
||||
|
||||
std::vector<uint8_t> GetDefaultActions() {
|
||||
return {kFirstAction, kSecondAction};
|
||||
}
|
||||
|
||||
TEST(ConnectionInfoTest, TestRestoreBle) {
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
BleConnectionInfo info(kMacAddr, kGattCharacteristic, kPsm,
|
||||
GetDefaultActions());
|
||||
auto serialized = info.ToDataElementBytes();
|
||||
auto connection_info = ConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_TRUE(absl::holds_alternative<BleConnectionInfo>(connection_info));
|
||||
@@ -49,7 +61,7 @@ TEST(ConnectionInfoTest, TestRestoreBle) {
|
||||
}
|
||||
|
||||
TEST(ConnectionInfoTest, TestRestoreBluetooth) {
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BluetoothConnectionInfo info(kMacAddr, kBluetoothUuid, GetDefaultActions());
|
||||
auto serialized = info.ToDataElementBytes();
|
||||
auto connection_info = ConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_TRUE(
|
||||
@@ -59,7 +71,7 @@ TEST(ConnectionInfoTest, TestRestoreBluetooth) {
|
||||
}
|
||||
|
||||
TEST(ConnectionInfoTest, TestRestoreMdns) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, GetDefaultActions());
|
||||
auto serialized = info.ToDataElementBytes();
|
||||
auto connection_info = ConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_TRUE(absl::holds_alternative<WifiLanConnectionInfo>(connection_info));
|
||||
@@ -68,9 +80,12 @@ TEST(ConnectionInfoTest, TestRestoreMdns) {
|
||||
}
|
||||
|
||||
TEST(ConnectionInfoTest, TestMonostate) {
|
||||
WifiLanConnectionInfo wifi_info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
BluetoothConnectionInfo bt_info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BleConnectionInfo ble_info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
WifiLanConnectionInfo wifi_info(kIpv4Addr, kPort, kBssid,
|
||||
GetDefaultActions());
|
||||
BluetoothConnectionInfo bt_info(kMacAddr, kBluetoothUuid,
|
||||
GetDefaultActions());
|
||||
BleConnectionInfo ble_info(kMacAddr, kGattCharacteristic, kPsm,
|
||||
GetDefaultActions());
|
||||
std::vector<ConnectionInfo*> infos = {&bt_info, &ble_info, &wifi_info};
|
||||
for (auto info : infos) {
|
||||
auto serialized = info->ToDataElementBytes();
|
||||
@@ -81,9 +96,12 @@ TEST(ConnectionInfoTest, TestMonostate) {
|
||||
}
|
||||
|
||||
TEST(ConnectionInfoTest, TestCannotRestoreAsOtherInfos) {
|
||||
WifiLanConnectionInfo wifi_info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
BluetoothConnectionInfo bt_info(kMacAddr, kBluetoothUuid, kAction);
|
||||
BleConnectionInfo ble_info(kMacAddr, kGattCharacteristic, kPsm, kAction);
|
||||
WifiLanConnectionInfo wifi_info(kIpv4Addr, kPort, kBssid,
|
||||
GetDefaultActions());
|
||||
BluetoothConnectionInfo bt_info(kMacAddr, kBluetoothUuid,
|
||||
GetDefaultActions());
|
||||
BleConnectionInfo ble_info(kMacAddr, kGattCharacteristic, kPsm,
|
||||
GetDefaultActions());
|
||||
EXPECT_THAT(
|
||||
BleConnectionInfo::FromDataElementBytes(wifi_info.ToDataElementBytes()),
|
||||
testing::status::StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
|
||||
@@ -15,12 +15,13 @@
|
||||
#include "internal/platform/wifi_lan_connection_info.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/connection_info.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace {
|
||||
@@ -42,19 +43,18 @@ std::string WifiLanConnectionInfo::ToDataElementBytes() const {
|
||||
mask |= has_bssid ? kBssidMask : 0;
|
||||
payload_data.push_back(mask);
|
||||
if (has_ipv4 || has_ipv6) {
|
||||
payload_data.insert(payload_data.end(), ip_address_.begin(),
|
||||
ip_address_.end());
|
||||
payload_data.append(ip_address_.begin(), ip_address_.end());
|
||||
}
|
||||
payload_data.insert(payload_data.end(), port_.begin(), port_.end());
|
||||
payload_data.append(port_.begin(), port_.end());
|
||||
if (has_bssid) {
|
||||
payload_data.insert(payload_data.end(), bssid_.begin(), bssid_.end());
|
||||
payload_data.append(bssid_.begin(), bssid_.end());
|
||||
}
|
||||
payload_data.push_back(actions_);
|
||||
payload_data.append(actions_.begin(), actions_.end());
|
||||
std::string ret;
|
||||
ret.push_back(kDataElementFieldType);
|
||||
ret.push_back(payload_data.size());
|
||||
ret.insert(ret.end(), payload_data.begin(), payload_data.end());
|
||||
return std::string(ret.data(), ret.size());
|
||||
ret.append(payload_data.begin(), payload_data.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
absl::StatusOr<WifiLanConnectionInfo>
|
||||
@@ -121,13 +121,10 @@ WifiLanConnectionInfo::FromDataElementBytes(absl::string_view bytes) {
|
||||
return absl::InvalidArgumentError(
|
||||
"Insufficient remaining bytes to read action.");
|
||||
}
|
||||
char action = bytes[position];
|
||||
// Check that we don't have any remaining bytes.
|
||||
if (bytes.size() != ++position) {
|
||||
return absl::InvalidArgumentError(absl::StrFormat(
|
||||
"Nonzero remaining bytes: %d.", bytes.size() - position));
|
||||
}
|
||||
return WifiLanConnectionInfo(address, port, bssid, action);
|
||||
auto action_str = bytes.substr(position);
|
||||
return WifiLanConnectionInfo(
|
||||
address, port, bssid,
|
||||
std::vector<uint8_t>(action_str.begin(), action_str.end()));
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
@@ -36,10 +37,10 @@ class WifiLanConnectionInfo : public ConnectionInfo {
|
||||
absl::string_view bytes);
|
||||
|
||||
WifiLanConnectionInfo(absl::string_view ip_address, absl::string_view port,
|
||||
char actions)
|
||||
std::vector<uint8_t> actions)
|
||||
: ip_address_(ip_address), port_(port), bssid_(""), actions_(actions) {}
|
||||
WifiLanConnectionInfo(absl::string_view ip_address, absl::string_view port,
|
||||
absl::string_view bssid, char actions)
|
||||
absl::string_view bssid, std::vector<uint8_t> actions)
|
||||
: ip_address_(ip_address),
|
||||
port_(port),
|
||||
bssid_(std::string(bssid)),
|
||||
@@ -56,13 +57,13 @@ class WifiLanConnectionInfo : public ConnectionInfo {
|
||||
// order (aka big-endian), so \x12\x34 will correspond to port 4660 (0x1234).
|
||||
std::string GetPort() const { return port_; }
|
||||
std::string GetBssid() const { return bssid_; }
|
||||
char GetActions() const override { return actions_; }
|
||||
std::vector<uint8_t> GetActions() const override { return actions_; }
|
||||
|
||||
private:
|
||||
std::string ip_address_;
|
||||
std::string port_;
|
||||
std::string bssid_;
|
||||
char actions_;
|
||||
std::vector<uint8_t> actions_;
|
||||
};
|
||||
|
||||
inline bool operator==(const WifiLanConnectionInfo& a,
|
||||
|
||||
@@ -38,20 +38,21 @@ using ::testing::status::StatusIs;
|
||||
using Medium = ::location::nearby::proto::connections::Medium;
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestMediumType) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, {kAction});
|
||||
EXPECT_EQ(info.GetMediumType(), Medium::WIFI_LAN);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestGetMembers) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
EXPECT_EQ(info.GetIpAddress(), kIpv4Addr);
|
||||
EXPECT_EQ(info.GetPort(), kPort);
|
||||
EXPECT_EQ(info.GetBssid(), kBssid);
|
||||
EXPECT_EQ(info.GetActions(), kAction);
|
||||
ASSERT_EQ(info.GetActions().size(), 1);
|
||||
EXPECT_EQ(info.GetActions()[0], kAction);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestToFromBytesIpv4) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -59,7 +60,7 @@ TEST(WifiLanConnectionInfoTest, TestToFromBytesIpv4) {
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestToFromBytesIpv6) {
|
||||
WifiLanConnectionInfo info(kIpv6Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv6Addr, kPort, kBssid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(serialized);
|
||||
ASSERT_OK(result);
|
||||
@@ -67,19 +68,19 @@ TEST(WifiLanConnectionInfoTest, TestToFromBytesIpv6) {
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestCopy) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
WifiLanConnectionInfo copy(info);
|
||||
EXPECT_EQ(info, copy);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestEquals) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info2(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
WifiLanConnectionInfo info2(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
EXPECT_EQ(info, info2);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestFromNoAction) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
serialized[1] -= 1;
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(
|
||||
@@ -89,37 +90,40 @@ TEST(WifiLanConnectionInfoTest, TestFromNoAction) {
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestToFromShortBssid) {
|
||||
std::string shortBssid = "\x0A\x1B\x2C";
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, shortBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, shortBssid, {kAction});
|
||||
auto bytes = info.ToDataElementBytes();
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(bytes);
|
||||
ASSERT_OK(result);
|
||||
EXPECT_EQ(result->GetIpAddress(), kIpv4Addr);
|
||||
EXPECT_EQ(result->GetPort(), kPort);
|
||||
EXPECT_TRUE(result->GetBssid().empty());
|
||||
EXPECT_EQ(result->GetActions(), kAction);
|
||||
ASSERT_EQ(result->GetActions().size(), 1);
|
||||
EXPECT_EQ(result->GetActions()[0], kAction);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestToFromNoIp) {
|
||||
WifiLanConnectionInfo info("", kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info("", kPort, kBssid, {kAction});
|
||||
auto bytes = info.ToDataElementBytes();
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(bytes);
|
||||
ASSERT_OK(result);
|
||||
EXPECT_TRUE(result->GetIpAddress().empty());
|
||||
EXPECT_EQ(result->GetPort(), kPort);
|
||||
EXPECT_EQ(result->GetBssid(), kBssid);
|
||||
EXPECT_EQ(result->GetActions(), kAction);
|
||||
ASSERT_EQ(result->GetActions().size(), 1);
|
||||
EXPECT_EQ(result->GetActions()[0], kAction);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestToFromLongIp) {
|
||||
WifiLanConnectionInfo info(absl::StrCat(kIpv4Addr, kIpv6Addr), kPort, kBssid,
|
||||
kAction);
|
||||
{kAction});
|
||||
auto bytes = info.ToDataElementBytes();
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(bytes);
|
||||
ASSERT_OK(result);
|
||||
EXPECT_TRUE(result->GetIpAddress().empty());
|
||||
EXPECT_EQ(result->GetPort(), kPort);
|
||||
EXPECT_EQ(result->GetBssid(), kBssid);
|
||||
EXPECT_EQ(result->GetActions(), kAction);
|
||||
ASSERT_EQ(result->GetActions().size(), 1);
|
||||
EXPECT_EQ(result->GetActions()[0], kAction);
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestFromIp) {
|
||||
@@ -130,7 +134,7 @@ TEST(WifiLanConnectionInfoTest, TestFromIp) {
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestBadBytesLength) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
std::string modified_short(
|
||||
serialized.substr(0, kIpv4AddressLength + kPortLength));
|
||||
@@ -144,128 +148,12 @@ TEST(WifiLanConnectionInfoTest, TestBadBytesLength) {
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestFromBadElementType) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, {kAction});
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
serialized[0] = 0x56;
|
||||
auto result = WifiLanConnectionInfo::FromDataElementBytes(serialized);
|
||||
EXPECT_THAT(result, StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestFromBadMaskIpv4) {
|
||||
WifiLanConnectionInfo info(kIpv4Addr, kPort, kBssid, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for IPV4 address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV6 address only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for port only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for BSSID only.
|
||||
serialized[3] = 0x08;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV4 + IPV6 addresses.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV4 + port.
|
||||
serialized[3] = 0x50;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV4 + BSSID.
|
||||
serialized[3] = 0x48;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for all fields present.
|
||||
serialized[3] = 0x78;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x58;
|
||||
EXPECT_OK(WifiLanConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestFromBadMaskIpv6) {
|
||||
WifiLanConnectionInfo info(kIpv6Addr, kPort, kBssid, kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for IPV4 address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV6 address only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for port only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for BSSID only.
|
||||
serialized[3] = 0x08;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Remove all masks.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV4 + IPV6 addresses.
|
||||
serialized[3] = 0x60;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV6 + port.
|
||||
serialized[3] = 0x30;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV6 + BSSID.
|
||||
serialized[3] = 0x28;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for all fields present.
|
||||
serialized[3] = 0x78;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with the correct mask.
|
||||
serialized[3] = 0x38;
|
||||
EXPECT_OK(WifiLanConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
TEST(WifiLanConnectionInfoTest, TestFromBadMaskEmpty) {
|
||||
WifiLanConnectionInfo info("", "", "", kAction);
|
||||
std::string serialized = info.ToDataElementBytes();
|
||||
// Set mask for IPV4 address only.
|
||||
serialized[3] = 0x40;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for IPV6 address only.
|
||||
serialized[3] = 0x20;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for port only.
|
||||
serialized[3] = 0x10;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for BSSID only.
|
||||
serialized[3] = 0x08;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Set mask for all fields.
|
||||
serialized[3] = 0x78;
|
||||
EXPECT_THAT(WifiLanConnectionInfo::FromDataElementBytes(serialized),
|
||||
StatusIs(absl::StatusCode::kInvalidArgument));
|
||||
// Verify OK with correct mask.
|
||||
serialized[3] = 0x00;
|
||||
EXPECT_OK(WifiLanConnectionInfo::FromDataElementBytes(serialized));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace nearby
|
||||
|
||||
@@ -51,8 +51,14 @@ PresenceDevice::PresenceDevice(DeviceMotion device_motion,
|
||||
|
||||
std::vector<nearby::ConnectionInfoVariant> PresenceDevice::GetConnectionInfos()
|
||||
const {
|
||||
std::vector<uint8_t> transformed_actions;
|
||||
transformed_actions.reserve(actions_.size());
|
||||
for (const auto& action : actions_) {
|
||||
transformed_actions.push_back(action.GetActionIdentifier());
|
||||
}
|
||||
return {nearby::BleConnectionInfo(metadata_.bluetooth_mac_address(),
|
||||
/*gatt_characteristic=*/"", /*psm=*/"", 0)};
|
||||
/*gatt_characteristic=*/"", /*psm=*/"",
|
||||
transformed_actions)};
|
||||
}
|
||||
} // namespace presence
|
||||
} // namespace nearby
|
||||
|
||||
@@ -72,13 +72,16 @@ TEST(PresenceDeviceTest, ExplicitInitNotEquals) {
|
||||
EXPECT_NE(device1, device2);
|
||||
}
|
||||
|
||||
TEST(PresenceDeviceTest, TestGetBluetoothAddress) {
|
||||
TEST(PresenceDeviceTest, TestGetBleConnectionInfo) {
|
||||
Metadata metadata = CreateTestMetadata();
|
||||
PresenceDevice device = PresenceDevice({kDefaultMotionType}, metadata);
|
||||
device.AddAction(PresenceAction(kTestAction));
|
||||
auto info = (device.GetConnectionInfos().at(0));
|
||||
ASSERT_TRUE(absl::holds_alternative<nearby::BleConnectionInfo>(info));
|
||||
EXPECT_EQ(absl::get<nearby::BleConnectionInfo>(info).GetMacAddress(),
|
||||
auto ble_info = absl::get<nearby::BleConnectionInfo>(info);
|
||||
EXPECT_EQ(ble_info.GetMacAddress(),
|
||||
kMacAddr);
|
||||
EXPECT_EQ(ble_info.GetActions(), std::vector<uint8_t>{kTestAction});
|
||||
}
|
||||
|
||||
TEST(PresenceDevicetest, TestGetAddExtendedProperties) {
|
||||
|
||||
Reference in New Issue
Block a user