Loading system/gd/common/interfaces/ILoggable.h 0 → 100644 +45 −0 Original line number Diff line number Diff line /****************************************************************************** * * Copyright 2022 Google, Inc. * * 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: * * http://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. * ******************************************************************************/ #pragma once #include <string> namespace bluetooth { namespace common { class ILoggable { public: // the interface for // converting an object to a string for feeding to loggers // e.g.. logcat virtual std::string ToStringForLogging() const = 0; virtual ~ILoggable() = default; }; class IRedactableLoggable : public ILoggable { public: // the interface for // converting an object to a string with sensitive info redacted // to avoid violating privacy virtual std::string ToRedactedStringForLogging() const = 0; virtual ~IRedactableLoggable() = default; }; } // namespace common } // namespace bluetooth system/gd/hci/address.cc +23 −2 Original line number Diff line number Diff line Loading @@ -42,10 +42,15 @@ Address::Address(std::initializer_list<uint8_t> l) { std::copy(l.begin(), std::min(l.begin() + kLength, l.end()), data()); } std::string Address::ToString() const { std::string Address::_ToMaskedColonSepHexString(int bytes_to_mask) const { std::stringstream ss; int count = 0; for (auto it = address.rbegin(); it != address.rend(); it++) { if (count++ < bytes_to_mask) { ss << "xx"; } else { ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0') << +*it; } if (std::next(it) != address.rend()) { ss << ':'; } Loading @@ -53,6 +58,22 @@ std::string Address::ToString() const { return ss.str(); } std::string Address::ToString() const { return _ToMaskedColonSepHexString(0); } std::string Address::ToColonSepHexString() const { return _ToMaskedColonSepHexString(0); } std::string Address::ToStringForLogging() const { return _ToMaskedColonSepHexString(0); } std::string Address::ToRedactedStringForLogging() const { return _ToMaskedColonSepHexString(4); } std::string Address::ToLegacyConfigString() const { return ToString(); } Loading system/gd/hci/address.h +12 −1 Original line number Diff line number Diff line Loading @@ -25,13 +25,16 @@ #include <ostream> #include <string> #include "common/interfaces/ILoggable.h" #include "packet/custom_field_fixed_size_interface.h" #include "storage/serializable.h" namespace bluetooth { namespace hci { class Address final : public packet::CustomFieldFixedSizeInterface<Address>, public storage::Serializable<Address> { class Address final : public packet::CustomFieldFixedSizeInterface<Address>, public storage::Serializable<Address>, public bluetooth::common::IRedactableLoggable { public: static constexpr size_t kLength = 6; Loading @@ -51,6 +54,10 @@ class Address final : public packet::CustomFieldFixedSizeInterface<Address>, pub // storage::Serializable methods std::string ToString() const override; std::string ToColonSepHexString() const; std::string ToStringForLogging() const override; std::string ToRedactedStringForLogging() const override; static std::optional<Address> FromString(const std::string& from); std::string ToLegacyConfigString() const override; static std::optional<Address> FromLegacyConfigString(const std::string& str); Loading Loading @@ -91,8 +98,12 @@ class Address final : public packet::CustomFieldFixedSizeInterface<Address>, pub static const Address kEmpty; // 00:00:00:00:00:00 static const Address kAny; // FF:FF:FF:FF:FF:FF private: std::string _ToMaskedColonSepHexString(int bytes_to_mask) const; }; // TODO: to fine-tune this. // we need an interface between the logger and ILoggable inline std::ostream& operator<<(std::ostream& os, const Address& a) { os << a.ToString(); return os; Loading system/gd/hci/address_unittest.cc +15 −2 Original line number Diff line number Diff line Loading @@ -16,12 +16,14 @@ * ******************************************************************************/ #include <unordered_map> #include "hci/address.h" #include <gmock/gmock.h> #include <gtest/gtest.h> #include "hci/address.h" #include <cstdint> #include <string> #include <unordered_map> using bluetooth::hci::Address; Loading Loading @@ -233,3 +235,14 @@ TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAndFullAddr) { struct std::hash<Address> hasher; ASSERT_NE(hasher(Address::kEmpty), hasher(Address::kAny)); } TEST(AddressTest, ToStringForLoggingTestOutputUnderDebuggablePropAndInitFlag) { Address addr{{0xab, 0x55, 0x44, 0x33, 0x22, 0x11}}; const std::string redacted_loggable_str = "xx:xx:xx:xx:55:ab"; const std::string loggable_str = "11:22:33:44:55:ab"; std::string ret1 = addr.ToStringForLogging(); ASSERT_STREQ(ret1.c_str(), loggable_str.c_str()); std::string ret2 = addr.ToRedactedStringForLogging(); ASSERT_STREQ(ret2.c_str(), redacted_loggable_str.c_str()); } system/gd/hci/address_with_type.h +10 −1 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <string> #include <utility> #include "common/interfaces/ILoggable.h" #include "crypto_toolbox/crypto_toolbox.h" #include "hci/address.h" #include "hci/hci_packets.h" Loading @@ -29,7 +30,7 @@ namespace bluetooth { namespace hci { class AddressWithType final { class AddressWithType final : public bluetooth::common::IRedactableLoggable { public: AddressWithType(Address address, AddressType address_type) : address_(std::move(address)), address_type_(address_type) {} Loading Loading @@ -119,6 +120,14 @@ class AddressWithType final { return ss.str(); } std::string ToStringForLogging() const override { return address_.ToStringForLogging() + "[" + AddressTypeText(address_type_) + "]"; } std::string ToRedactedStringForLogging() const override { return address_.ToStringForLogging() + "[" + AddressTypeText(address_type_) + "]"; } private: Address address_; AddressType address_type_; Loading Loading
system/gd/common/interfaces/ILoggable.h 0 → 100644 +45 −0 Original line number Diff line number Diff line /****************************************************************************** * * Copyright 2022 Google, Inc. * * 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: * * http://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. * ******************************************************************************/ #pragma once #include <string> namespace bluetooth { namespace common { class ILoggable { public: // the interface for // converting an object to a string for feeding to loggers // e.g.. logcat virtual std::string ToStringForLogging() const = 0; virtual ~ILoggable() = default; }; class IRedactableLoggable : public ILoggable { public: // the interface for // converting an object to a string with sensitive info redacted // to avoid violating privacy virtual std::string ToRedactedStringForLogging() const = 0; virtual ~IRedactableLoggable() = default; }; } // namespace common } // namespace bluetooth
system/gd/hci/address.cc +23 −2 Original line number Diff line number Diff line Loading @@ -42,10 +42,15 @@ Address::Address(std::initializer_list<uint8_t> l) { std::copy(l.begin(), std::min(l.begin() + kLength, l.end()), data()); } std::string Address::ToString() const { std::string Address::_ToMaskedColonSepHexString(int bytes_to_mask) const { std::stringstream ss; int count = 0; for (auto it = address.rbegin(); it != address.rend(); it++) { if (count++ < bytes_to_mask) { ss << "xx"; } else { ss << std::nouppercase << std::hex << std::setw(2) << std::setfill('0') << +*it; } if (std::next(it) != address.rend()) { ss << ':'; } Loading @@ -53,6 +58,22 @@ std::string Address::ToString() const { return ss.str(); } std::string Address::ToString() const { return _ToMaskedColonSepHexString(0); } std::string Address::ToColonSepHexString() const { return _ToMaskedColonSepHexString(0); } std::string Address::ToStringForLogging() const { return _ToMaskedColonSepHexString(0); } std::string Address::ToRedactedStringForLogging() const { return _ToMaskedColonSepHexString(4); } std::string Address::ToLegacyConfigString() const { return ToString(); } Loading
system/gd/hci/address.h +12 −1 Original line number Diff line number Diff line Loading @@ -25,13 +25,16 @@ #include <ostream> #include <string> #include "common/interfaces/ILoggable.h" #include "packet/custom_field_fixed_size_interface.h" #include "storage/serializable.h" namespace bluetooth { namespace hci { class Address final : public packet::CustomFieldFixedSizeInterface<Address>, public storage::Serializable<Address> { class Address final : public packet::CustomFieldFixedSizeInterface<Address>, public storage::Serializable<Address>, public bluetooth::common::IRedactableLoggable { public: static constexpr size_t kLength = 6; Loading @@ -51,6 +54,10 @@ class Address final : public packet::CustomFieldFixedSizeInterface<Address>, pub // storage::Serializable methods std::string ToString() const override; std::string ToColonSepHexString() const; std::string ToStringForLogging() const override; std::string ToRedactedStringForLogging() const override; static std::optional<Address> FromString(const std::string& from); std::string ToLegacyConfigString() const override; static std::optional<Address> FromLegacyConfigString(const std::string& str); Loading Loading @@ -91,8 +98,12 @@ class Address final : public packet::CustomFieldFixedSizeInterface<Address>, pub static const Address kEmpty; // 00:00:00:00:00:00 static const Address kAny; // FF:FF:FF:FF:FF:FF private: std::string _ToMaskedColonSepHexString(int bytes_to_mask) const; }; // TODO: to fine-tune this. // we need an interface between the logger and ILoggable inline std::ostream& operator<<(std::ostream& os, const Address& a) { os << a.ToString(); return os; Loading
system/gd/hci/address_unittest.cc +15 −2 Original line number Diff line number Diff line Loading @@ -16,12 +16,14 @@ * ******************************************************************************/ #include <unordered_map> #include "hci/address.h" #include <gmock/gmock.h> #include <gtest/gtest.h> #include "hci/address.h" #include <cstdint> #include <string> #include <unordered_map> using bluetooth::hci::Address; Loading Loading @@ -233,3 +235,14 @@ TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAndFullAddr) { struct std::hash<Address> hasher; ASSERT_NE(hasher(Address::kEmpty), hasher(Address::kAny)); } TEST(AddressTest, ToStringForLoggingTestOutputUnderDebuggablePropAndInitFlag) { Address addr{{0xab, 0x55, 0x44, 0x33, 0x22, 0x11}}; const std::string redacted_loggable_str = "xx:xx:xx:xx:55:ab"; const std::string loggable_str = "11:22:33:44:55:ab"; std::string ret1 = addr.ToStringForLogging(); ASSERT_STREQ(ret1.c_str(), loggable_str.c_str()); std::string ret2 = addr.ToRedactedStringForLogging(); ASSERT_STREQ(ret2.c_str(), redacted_loggable_str.c_str()); }
system/gd/hci/address_with_type.h +10 −1 Original line number Diff line number Diff line Loading @@ -22,6 +22,7 @@ #include <string> #include <utility> #include "common/interfaces/ILoggable.h" #include "crypto_toolbox/crypto_toolbox.h" #include "hci/address.h" #include "hci/hci_packets.h" Loading @@ -29,7 +30,7 @@ namespace bluetooth { namespace hci { class AddressWithType final { class AddressWithType final : public bluetooth::common::IRedactableLoggable { public: AddressWithType(Address address, AddressType address_type) : address_(std::move(address)), address_type_(address_type) {} Loading Loading @@ -119,6 +120,14 @@ class AddressWithType final { return ss.str(); } std::string ToStringForLogging() const override { return address_.ToStringForLogging() + "[" + AddressTypeText(address_type_) + "]"; } std::string ToRedactedStringForLogging() const override { return address_.ToStringForLogging() + "[" + AddressTypeText(address_type_) + "]"; } private: Address address_; AddressType address_type_; Loading