aboutsummaryrefslogtreecommitdiff
path: root/gd/common
diff options
context:
space:
mode:
authorMartin Brabham <optedoblivion@google.com>2019-03-29 10:24:52 -0700
committerMartin Brabham <optedoblivion@google.com>2019-08-22 09:03:12 -0700
commit94db40c6c98c426e5645b2e9811018d7af2311c7 (patch)
tree0dd97768aa53907e08753b8088895723afe96314 /gd/common
parent2ea2e8d2b05178d242a4c03f447542bfcc1187ab (diff)
downloadbt-94db40c6c98c426e5645b2e9811018d7af2311c7.tar.gz
GD: Move Address and ClassOfDevice to hci
Bug: 139135297 Test: atest --host -t bluetooth_test_gd Change-Id: I64c3ccbb3543a22a24f1b6b8a99f32e3d8aa3e24
Diffstat (limited to 'gd/common')
-rw-r--r--gd/common/Android.bp4
-rw-r--r--gd/common/address.cc92
-rw-r--r--gd/common/address.h93
-rw-r--r--gd/common/address_unittest.cc232
-rw-r--r--gd/common/class_of_device.cc98
-rw-r--r--gd/common/class_of_device.h59
-rw-r--r--gd/common/class_of_device_unittest.cc98
7 files changed, 0 insertions, 676 deletions
diff --git a/gd/common/Android.bp b/gd/common/Android.bp
index d8410f584..c9234278c 100644
--- a/gd/common/Android.bp
+++ b/gd/common/Android.bp
@@ -1,8 +1,6 @@
filegroup {
name: "BluetoothCommonSources",
srcs: [
- "address.cc",
- "class_of_device.cc",
"link_key.cc",
],
}
@@ -10,9 +8,7 @@ filegroup {
filegroup {
name: "BluetoothCommonTestSources",
srcs: [
- "address_unittest.cc",
"blocking_queue_unittest.cc",
- "class_of_device_unittest.cc",
"bidi_queue_unittest.cc",
"observer_registry_test.cc",
"link_key_unittest.cc",
diff --git a/gd/common/address.cc b/gd/common/address.cc
deleted file mode 100644
index cd8101a6a..000000000
--- a/gd/common/address.cc
+++ /dev/null
@@ -1,92 +0,0 @@
-/******************************************************************************
- *
- * Copyright 2019 The Android Open Source Project
- *
- * 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.
- *
- ******************************************************************************/
-
-#include "address.h"
-
-#include <stdint.h>
-#include <algorithm>
-#include <sstream>
-#include <vector>
-
-namespace bluetooth {
-namespace common {
-
-static_assert(sizeof(Address) == 6, "Address must be 6 bytes long!");
-
-const Address Address::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
-const Address Address::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
-
-Address::Address(const uint8_t (&addr)[6]) {
- std::copy(addr, addr + kLength, address);
-};
-
-std::string Address::ToString() const {
- char buffer[] = "00:00:00:00:00:00";
- std::snprintf(&buffer[0], sizeof(buffer),
- "%02x:%02x:%02x:%02x:%02x:%02x", address[5], address[4], address[3], address[2], address[1], address[0]);
- std::string str(buffer);
- return str;
-}
-
-bool Address::FromString(const std::string& from, Address& to) {
- Address new_addr;
- if (from.length() != 17) {
- return false;
- }
-
- std::istringstream stream(from);
- std::string token;
- int index = 0;
- while (getline(stream, token, ':')) {
- if (index >= 6) {
- return false;
- }
-
- if (token.length() != 2) {
- return false;
- }
-
- char* temp = nullptr;
- new_addr.address[5 - index] = strtol(token.c_str(), &temp, 16);
- if (*temp != '\0') {
- return false;
- }
-
- index++;
- }
-
- if (index != 6) {
- return false;
- }
-
- to = new_addr;
- return true;
-}
-
-size_t Address::FromOctets(const uint8_t* from) {
- std::copy(from, from + kLength, address);
- return kLength;
-};
-
-bool Address::IsValidAddress(const std::string& address) {
- Address tmp;
- return Address::FromString(address, tmp);
-}
-
-} // namespace common
-} // namespace bluetooth
diff --git a/gd/common/address.h b/gd/common/address.h
deleted file mode 100644
index f62464c3c..000000000
--- a/gd/common/address.h
+++ /dev/null
@@ -1,93 +0,0 @@
-/******************************************************************************
- *
- * Copyright 2019 The Android Open Source Project
- *
- * 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 Address final {
- public:
- static constexpr unsigned int kLength = 6;
-
- uint8_t address[kLength];
-
- Address() = default;
- Address(const uint8_t (&addr)[6]);
-
- bool operator<(const Address& rhs) const {
- return (std::memcmp(address, rhs.address, sizeof(address)) < 0);
- }
- bool operator==(const Address& rhs) const {
- return (std::memcmp(address, rhs.address, sizeof(address)) == 0);
- }
- bool operator>(const Address& rhs) const {
- return (rhs < *this);
- }
- bool operator<=(const Address& rhs) const {
- return !(*this > rhs);
- }
- bool operator>=(const Address& rhs) const {
- return !(*this < rhs);
- }
- bool operator!=(const Address& rhs) const {
- return !(*this == rhs);
- }
-
- bool IsEmpty() const {
- return *this == kEmpty;
- }
-
- std::string ToString() const;
-
- // Converts |string| to Address and places it in |to|. If |from| does
- // not represent a Bluetooth address, |to| is not modified and this function
- // returns false. Otherwise, it returns true.
- static bool FromString(const std::string& from, Address& to);
-
- // Copies |from| raw Bluetooth address octets to the local object.
- // Returns the number of copied octets - should be always Address::kLength
- size_t FromOctets(const uint8_t* from);
-
- static bool IsValidAddress(const std::string& address);
-
- static const Address kEmpty; // 00:00:00:00:00:00
- static const Address kAny; // FF:FF:FF:FF:FF:FF
-};
-
-inline std::ostream& operator<<(std::ostream& os, const Address& a) {
- os << a.ToString();
- return os;
-}
-
-} // namespace common
-} // namespace bluetooth
-
-namespace std {
-template <>
-struct hash<bluetooth::common::Address> {
- std::size_t operator()(const bluetooth::common::Address& val) const {
- static_assert(sizeof(uint64_t) >= bluetooth::common::Address::kLength);
- uint64_t int_addr = 0;
- memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address, bluetooth::common::Address::kLength);
- return std::hash<uint64_t>{}(int_addr);
- }
-};
-} // namespace std \ No newline at end of file
diff --git a/gd/common/address_unittest.cc b/gd/common/address_unittest.cc
deleted file mode 100644
index a16f70d8b..000000000
--- a/gd/common/address_unittest.cc
+++ /dev/null
@@ -1,232 +0,0 @@
-/******************************************************************************
- *
- * Copyright 2019 The Android Open Source Project
- *
- * 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.
- *
- ******************************************************************************/
-
-#include <unordered_map>
-
-#include <gtest/gtest.h>
-
-#include "common/address.h"
-
-using bluetooth::common::Address;
-
-static const char* test_addr = "bc:9a:78:56:34:12";
-static const char* test_addr2 = "21:43:65:87:a9:cb";
-
-TEST(AddressUnittest, test_constructor_array) {
- Address bdaddr({0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc});
-
- ASSERT_EQ(0x12, bdaddr.address[0]);
- ASSERT_EQ(0x34, bdaddr.address[1]);
- ASSERT_EQ(0x56, bdaddr.address[2]);
- ASSERT_EQ(0x78, bdaddr.address[3]);
- ASSERT_EQ(0x9A, bdaddr.address[4]);
- ASSERT_EQ(0xBC, bdaddr.address[5]);
-
- std::string ret = bdaddr.ToString();
-
- ASSERT_STREQ(test_addr, ret.c_str());
-}
-
-TEST(AddressUnittest, test_is_empty) {
- Address empty;
- Address::FromString("00:00:00:00:00:00", empty);
- ASSERT_TRUE(empty.IsEmpty());
-
- Address not_empty;
- Address::FromString("00:00:00:00:00:01", not_empty);
- ASSERT_FALSE(not_empty.IsEmpty());
-}
-
-TEST(AddressUnittest, test_to_from_str) {
- Address bdaddr;
- Address::FromString(test_addr, bdaddr);
-
- ASSERT_EQ(0x12, bdaddr.address[0]);
- ASSERT_EQ(0x34, bdaddr.address[1]);
- ASSERT_EQ(0x56, bdaddr.address[2]);
- ASSERT_EQ(0x78, bdaddr.address[3]);
- ASSERT_EQ(0x9A, bdaddr.address[4]);
- ASSERT_EQ(0xBC, bdaddr.address[5]);
-
- std::string ret = bdaddr.ToString();
-
- ASSERT_STREQ(test_addr, ret.c_str());
-}
-
-TEST(AddressUnittest, test_from_octets) {
- static const uint8_t test_addr_array[] = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc};
-
- Address bdaddr;
- size_t expected_result = Address::kLength;
- ASSERT_EQ(expected_result, bdaddr.FromOctets(test_addr_array));
-
- ASSERT_EQ(0x12, bdaddr.address[0]);
- ASSERT_EQ(0x34, bdaddr.address[1]);
- ASSERT_EQ(0x56, bdaddr.address[2]);
- ASSERT_EQ(0x78, bdaddr.address[3]);
- ASSERT_EQ(0x9A, bdaddr.address[4]);
- ASSERT_EQ(0xBC, bdaddr.address[5]);
-
- std::string ret = bdaddr.ToString();
-
- ASSERT_STREQ(test_addr, ret.c_str());
-}
-
-TEST(AddressTest, test_equals) {
- Address bdaddr1;
- Address bdaddr2;
- Address bdaddr3;
- Address::FromString(test_addr, bdaddr1);
- Address::FromString(test_addr, bdaddr2);
- EXPECT_TRUE(bdaddr1 == bdaddr2);
- EXPECT_FALSE(bdaddr1 != bdaddr2);
- EXPECT_TRUE(bdaddr1 == bdaddr1);
- EXPECT_FALSE(bdaddr1 != bdaddr1);
-
- Address::FromString(test_addr2, bdaddr3);
- EXPECT_FALSE(bdaddr2 == bdaddr3);
- EXPECT_TRUE(bdaddr2 != bdaddr3);
-}
-
-TEST(AddressTest, test_less_than) {
- Address bdaddr1;
- Address bdaddr2;
- Address bdaddr3;
- Address::FromString(test_addr, bdaddr1);
- Address::FromString(test_addr, bdaddr2);
- EXPECT_FALSE(bdaddr1 < bdaddr2);
- EXPECT_FALSE(bdaddr1 < bdaddr1);
-
- Address::FromString(test_addr2, bdaddr3);
- EXPECT_TRUE(bdaddr2 < bdaddr3);
- EXPECT_FALSE(bdaddr3 < bdaddr2);
-}
-
-TEST(AddressTest, test_more_than) {
- Address bdaddr1;
- Address bdaddr2;
- Address bdaddr3;
- Address::FromString(test_addr, bdaddr1);
- Address::FromString(test_addr, bdaddr2);
- EXPECT_FALSE(bdaddr1 > bdaddr2);
- EXPECT_FALSE(bdaddr1 > bdaddr1);
-
- Address::FromString(test_addr2, bdaddr3);
- EXPECT_FALSE(bdaddr2 > bdaddr3);
- EXPECT_TRUE(bdaddr3 > bdaddr2);
-}
-
-TEST(AddressTest, test_less_than_or_equal) {
- Address bdaddr1;
- Address bdaddr2;
- Address bdaddr3;
- Address::FromString(test_addr, bdaddr1);
- Address::FromString(test_addr, bdaddr2);
- EXPECT_TRUE(bdaddr1 <= bdaddr2);
- EXPECT_TRUE(bdaddr1 <= bdaddr1);
-
- Address::FromString(test_addr2, bdaddr3);
- EXPECT_TRUE(bdaddr2 <= bdaddr3);
- EXPECT_FALSE(bdaddr3 <= bdaddr2);
-}
-
-TEST(AddressTest, test_more_than_or_equal) {
- Address bdaddr1;
- Address bdaddr2;
- Address bdaddr3;
- Address::FromString(test_addr, bdaddr1);
- Address::FromString(test_addr, bdaddr2);
- EXPECT_TRUE(bdaddr1 >= bdaddr2);
- EXPECT_TRUE(bdaddr1 >= bdaddr1);
-
- Address::FromString(test_addr2, bdaddr3);
- EXPECT_FALSE(bdaddr2 >= bdaddr3);
- EXPECT_TRUE(bdaddr3 >= bdaddr2);
-}
-
-TEST(AddressTest, test_copy) {
- Address bdaddr1;
- Address bdaddr2;
- Address::FromString(test_addr, bdaddr1);
- bdaddr2 = bdaddr1;
-
- EXPECT_TRUE(bdaddr1 == bdaddr2);
-}
-
-TEST(AddressTest, IsValidAddress) {
- EXPECT_FALSE(Address::IsValidAddress(""));
- EXPECT_FALSE(Address::IsValidAddress("000000000000"));
- EXPECT_FALSE(Address::IsValidAddress("00:00:00:00:0000"));
- EXPECT_FALSE(Address::IsValidAddress("00:00:00:00:00:0"));
- EXPECT_FALSE(Address::IsValidAddress("00:00:00:00:00:0;"));
- EXPECT_TRUE(Address::IsValidAddress("00:00:00:00:00:00"));
- EXPECT_TRUE(Address::IsValidAddress("AB:cd:00:00:00:00"));
- EXPECT_FALSE(Address::IsValidAddress("aB:cD:eF:Gh:iJ:Kl"));
-}
-
-TEST(AddressTest, BdAddrFromString) {
- Address addr;
- memset(&addr, 0, sizeof(addr));
-
- EXPECT_TRUE(Address::FromString("00:00:00:00:00:00", addr));
- const Address result0 = {{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
- EXPECT_EQ(0, memcmp(&addr, &result0, sizeof(addr)));
-
- EXPECT_TRUE(Address::FromString("ab:01:4C:d5:21:9f", addr));
- const Address result1 = {{0x9f, 0x21, 0xd5, 0x4c, 0x01, 0xab}};
- EXPECT_EQ(0, memcmp(&addr, &result1, sizeof(addr)));
-}
-
-TEST(AddressTest, BdAddrFromStringToStringEquivalent) {
- std::string address = "c1:c2:c3:d1:d2:d3";
- Address addr;
-
- EXPECT_TRUE(Address::FromString(address, addr));
- EXPECT_EQ(addr.ToString(), address);
-}
-
-TEST(AddressTest, BdAddrSameValueSameOrder) {
- Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
- Address addr2{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
- // Test if two addresses with same byte value have the same hash
- struct std::hash<bluetooth::common::Address> hasher;
- EXPECT_EQ(hasher(addr1), hasher(addr2));
- // Test if two addresses with the same hash and the same value, they will
- // still map to the same value
- std::unordered_map<Address, int> data = {};
- data[addr1] = 5;
- data[addr2] = 8;
- EXPECT_EQ(data[addr1], data[addr2]);
-}
-
-TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAddr) {
- Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
- struct std::hash<Address> hasher;
- EXPECT_NE(hasher(addr1), hasher(Address::kEmpty));
-}
-
-TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesFullAddr) {
- Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
- struct std::hash<Address> hasher;
- EXPECT_NE(hasher(addr1), hasher(Address::kAny));
-}
-
-TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAndFullAddr) {
- struct std::hash<Address> hasher;
- EXPECT_NE(hasher(Address::kEmpty), hasher(Address::kAny));
-}
diff --git a/gd/common/class_of_device.cc b/gd/common/class_of_device.cc
deleted file mode 100644
index 8d7ff68c0..000000000
--- a/gd/common/class_of_device.cc
+++ /dev/null
@@ -1,98 +0,0 @@
-/******************************************************************************
- *
- * Copyright 2019 The Android Open Source Project
- *
- * 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.
- *
- ******************************************************************************/
-
-#include "class_of_device.h"
-
-#include <stdint.h>
-#include <algorithm>
-#include <sstream>
-#include <vector>
-
-#include "os/log.h"
-
-namespace bluetooth {
-namespace common {
-
-static_assert(sizeof(ClassOfDevice) == ClassOfDevice::kLength, "ClassOfDevice must be 3 bytes long!");
-
-ClassOfDevice::ClassOfDevice(const uint8_t (&class_of_device)[kLength]) {
- std::copy(class_of_device, class_of_device + kLength, cod);
-};
-
-std::string ClassOfDevice::ToString() const {
- char buffer[] = "000-0-00";
- std::snprintf(&buffer[0], sizeof(buffer),
- "%03x-%01x-%02x", (static_cast<uint16_t>(cod[2]) << 4) | cod[1] >> 4, cod[1] & 0x0f, cod[0]);
- std::string str(buffer);
- return str;
-
-}
-
-bool ClassOfDevice::FromString(const std::string& from, ClassOfDevice& to) {
- ClassOfDevice new_cod;
- if (from.length() != 8) return false;
-
- std::istringstream stream(from);
- std::string token;
- int index = 0;
- uint16_t values[3];
-
- while (getline(stream, token, '-')) {
- if (index >= 3) {
- return false;
- }
-
- if (index == 0 && token.length() != 3) {
- return false;
- } else if (index == 1 && token.length() != 1) {
- return false;
- } else if (index == 2 && token.length() != 2) {
- return false;
- }
- char* temp = nullptr;
- values[index] = strtol(token.c_str(), &temp, 16);
- if (*temp != '\0') {
- return false;
- }
-
- index++;
- }
-
- if (index != 3) {
- return false;
- }
-
- new_cod.cod[0] = values[2];
- new_cod.cod[1] = values[1] | ((values[0] & 0xf) << 4);
- new_cod.cod[2] = values[0] >> 4;
-
- to = new_cod;
- return true;
-}
-
-size_t ClassOfDevice::FromOctets(const uint8_t* from) {
- std::copy(from, from + kLength, cod);
- return kLength;
-};
-
-bool ClassOfDevice::IsValid(const std::string& cod) {
- ClassOfDevice tmp;
- return ClassOfDevice::FromString(cod, tmp);
-}
-} // namespace common
-} // namespace bluetooth
diff --git a/gd/common/class_of_device.h b/gd/common/class_of_device.h
deleted file mode 100644
index 983f128eb..000000000
--- a/gd/common/class_of_device.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/******************************************************************************
- *
- * Copyright 2019 The Android Open Source Project
- *
- * 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 ClassOfDevice final {
- public:
- static constexpr unsigned int kLength = 3;
-
- uint8_t cod[kLength];
-
- ClassOfDevice() = default;
- ClassOfDevice(const uint8_t (&class_of_device)[kLength]);
-
- bool operator==(const ClassOfDevice& rhs) const {
- return (std::memcmp(cod, rhs.cod, sizeof(cod)) == 0);
- }
-
- std::string ToString() const;
-
- // Converts |string| to ClassOfDevice and places it in |to|. If |from| does
- // not represent a Class of Device, |to| is not modified and this function
- // returns false. Otherwise, it returns true.
- static bool FromString(const std::string& from, ClassOfDevice& to);
-
- // Copies |from| raw Class of Device octets to the local object.
- // Returns the number of copied octets (always ClassOfDevice::kLength)
- size_t FromOctets(const uint8_t* from);
-
- static bool IsValid(const std::string& class_of_device);
-};
-
-inline std::ostream& operator<<(std::ostream& os, const ClassOfDevice& c) {
- os << c.ToString();
- return os;
-}
-
-} // namespace common
-} // namespace bluetooth
diff --git a/gd/common/class_of_device_unittest.cc b/gd/common/class_of_device_unittest.cc
deleted file mode 100644
index abd4a598a..000000000
--- a/gd/common/class_of_device_unittest.cc
+++ /dev/null
@@ -1,98 +0,0 @@
-/******************************************************************************
- *
- * Copyright 2019 The Android Open Source Project
- *
- * 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.
- *
- ******************************************************************************/
-
-#include <gtest/gtest.h>
-
-#include "common/class_of_device.h"
-
-using bluetooth::common::ClassOfDevice;
-
-static const char* test_class = "efc-d-ab";
-static const uint8_t test_bytes[]{0xab, 0xcd, 0xef};
-
-TEST(ClassOfDeviceUnittest, test_constructor_array) {
- ClassOfDevice cod(test_bytes);
-
- ASSERT_EQ(test_bytes[0], cod.cod[0]);
- ASSERT_EQ(test_bytes[1], cod.cod[1]);
- ASSERT_EQ(test_bytes[2], cod.cod[2]);
-
- std::string ret = cod.ToString();
-
- ASSERT_STREQ(test_class, ret.c_str());
-}
-
-TEST(ClassOfDeviceUnittest, test_to_from_str) {
- ClassOfDevice cod;
- ClassOfDevice::FromString(test_class, cod);
-
- ASSERT_EQ(test_bytes[0], cod.cod[0]);
- ASSERT_EQ(test_bytes[1], cod.cod[1]);
- ASSERT_EQ(test_bytes[2], cod.cod[2]);
-
- std::string ret = cod.ToString();
-
- ASSERT_STREQ(test_class, ret.c_str());
-}
-
-TEST(ClassOfDeviceUnittest, test_from_octets) {
- ClassOfDevice cod;
- size_t expected_result = ClassOfDevice::kLength;
- ASSERT_EQ(expected_result, cod.FromOctets(test_bytes));
-
- ASSERT_EQ(test_bytes[0], cod.cod[0]);
- ASSERT_EQ(test_bytes[1], cod.cod[1]);
- ASSERT_EQ(test_bytes[2], cod.cod[2]);
-
- std::string ret = cod.ToString();
-
- ASSERT_STREQ(test_class, ret.c_str());
-}
-
-TEST(ClassOfDeviceTest, test_copy) {
- ClassOfDevice cod1;
- ClassOfDevice cod2;
- ClassOfDevice::FromString(test_class, cod1);
- cod2 = cod1;
-
- ASSERT_EQ(cod1.cod[0], cod2.cod[0]);
- ASSERT_EQ(cod1.cod[1], cod2.cod[1]);
- ASSERT_EQ(cod1.cod[2], cod2.cod[2]);
-}
-
-TEST(ClassOfDeviceTest, IsValid) {
- EXPECT_FALSE(ClassOfDevice::IsValid(""));
- EXPECT_FALSE(ClassOfDevice::IsValid("000000"));
- EXPECT_FALSE(ClassOfDevice::IsValid("00-00-00"));
- EXPECT_FALSE(ClassOfDevice::IsValid("000-0-0"));
- EXPECT_TRUE(ClassOfDevice::IsValid("000-0-00"));
- EXPECT_TRUE(ClassOfDevice::IsValid("ABc-d-00"));
- EXPECT_TRUE(ClassOfDevice::IsValid("aBc-D-eF"));
-}
-
-TEST(ClassOfDeviceTest, classOfDeviceFromString) {
- ClassOfDevice cod;
-
- EXPECT_TRUE(ClassOfDevice::FromString("000-0-00", cod));
- const ClassOfDevice result0 = {{0x00, 0x00, 0x00}};
- EXPECT_EQ(0, memcmp(&cod, &result0, sizeof(cod)));
-
- EXPECT_TRUE(ClassOfDevice::FromString("ab2-1-4C", cod));
- const ClassOfDevice result1 = {{0x4c, 0x21, 0xab}};
- EXPECT_EQ(0, memcmp(&cod, &result1, sizeof(cod)));
-}