1 /******************************************************************************
2 *
3 * Copyright 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21
22 #include "hci/class_of_device.h"
23
24 using bluetooth::hci::ClassOfDevice;
25
26 static const char* test_class = "efc-d-ab";
27 static const uint8_t test_bytes[]{0xab, 0xcd, 0xef};
28
TEST(ClassOfDeviceUnittest,test_constructor_array)29 TEST(ClassOfDeviceUnittest, test_constructor_array) {
30 ClassOfDevice cod(test_bytes);
31
32 ASSERT_EQ(test_bytes[0], cod.cod[0]);
33 ASSERT_EQ(test_bytes[1], cod.cod[1]);
34 ASSERT_EQ(test_bytes[2], cod.cod[2]);
35
36 std::string ret = cod.ToString();
37
38 ASSERT_STREQ(test_class, ret.c_str());
39 }
40
TEST(ClassOfDeviceUnittest,test_to_from_str)41 TEST(ClassOfDeviceUnittest, test_to_from_str) {
42 ClassOfDevice cod;
43 ClassOfDevice::FromString(test_class, cod);
44
45 ASSERT_EQ(test_bytes[0], cod.cod[0]);
46 ASSERT_EQ(test_bytes[1], cod.cod[1]);
47 ASSERT_EQ(test_bytes[2], cod.cod[2]);
48
49 std::string ret = cod.ToString();
50
51 ASSERT_STREQ(test_class, ret.c_str());
52 }
53
TEST(ClassOfDeviceUnittest,test_from_octets)54 TEST(ClassOfDeviceUnittest, test_from_octets) {
55 ClassOfDevice cod;
56 size_t expected_result = ClassOfDevice::kLength;
57 ASSERT_EQ(expected_result, cod.FromOctets(test_bytes));
58
59 ASSERT_EQ(test_bytes[0], cod.cod[0]);
60 ASSERT_EQ(test_bytes[1], cod.cod[1]);
61 ASSERT_EQ(test_bytes[2], cod.cod[2]);
62
63 std::string ret = cod.ToString();
64
65 ASSERT_STREQ(test_class, ret.c_str());
66 }
67
TEST(ClassOfDeviceTest,test_copy)68 TEST(ClassOfDeviceTest, test_copy) {
69 ClassOfDevice cod1;
70 ClassOfDevice cod2;
71 ClassOfDevice::FromString(test_class, cod1);
72 cod2 = cod1;
73
74 ASSERT_EQ(cod1.cod[0], cod2.cod[0]);
75 ASSERT_EQ(cod1.cod[1], cod2.cod[1]);
76 ASSERT_EQ(cod1.cod[2], cod2.cod[2]);
77 }
78
TEST(ClassOfDeviceTest,IsValid)79 TEST(ClassOfDeviceTest, IsValid) {
80 ASSERT_FALSE(ClassOfDevice::IsValid(""));
81 ASSERT_FALSE(ClassOfDevice::IsValid("000000"));
82 ASSERT_FALSE(ClassOfDevice::IsValid("00-00-00"));
83 ASSERT_FALSE(ClassOfDevice::IsValid("000-0-0"));
84 ASSERT_TRUE(ClassOfDevice::IsValid("000-0-00"));
85 ASSERT_TRUE(ClassOfDevice::IsValid("ABc-d-00"));
86 ASSERT_TRUE(ClassOfDevice::IsValid("aBc-D-eF"));
87 }
88
TEST(ClassOfDeviceTest,classOfDeviceFromString)89 TEST(ClassOfDeviceTest, classOfDeviceFromString) {
90 ClassOfDevice cod;
91
92 ASSERT_TRUE(ClassOfDevice::FromString("000-0-00", cod));
93 const ClassOfDevice result0 = {{0x00, 0x00, 0x00}};
94 ASSERT_EQ(0, memcmp(cod.data(), result0.data(), ClassOfDevice::kLength));
95
96 ASSERT_TRUE(ClassOfDevice::FromString("ab2-1-4C", cod));
97 const ClassOfDevice result1 = {{0x4c, 0x21, 0xab}};
98 ASSERT_EQ(0, memcmp(cod.data(), result1.data(), ClassOfDevice::kLength));
99 }
100
TEST(ClassOfDeviceTest,classOfDeviceFromUint32Legacy)101 TEST(ClassOfDeviceTest, classOfDeviceFromUint32Legacy) {
102 auto cod = ClassOfDevice::FromUint32Legacy(0);
103 ASSERT_TRUE(cod);
104 ASSERT_THAT(cod->cod, testing::ElementsAre(0x00, 0x00, 0x00));
105 ASSERT_EQ(cod->ToUint32Legacy(), 0);
106
107 cod = ClassOfDevice::FromUint32Legacy(0xab214c);
108 ASSERT_TRUE(cod);
109 ASSERT_THAT(cod->cod, testing::ElementsAre(0xab, 0x21, 0x4c));
110 ASSERT_EQ(cod->ToUint32Legacy(), 0xab214c);
111
112 ASSERT_FALSE(ClassOfDevice::FromUint32Legacy(0x1ab214c));
113 }
114