• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hci/uuid.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 namespace testing {
23 
24 using bluetooth::hci::Uuid;
25 
26 static const Uuid ONES = Uuid::From128BitBE(
27     Uuid::UUID128Bit{{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}});
28 
29 static const Uuid SEQUENTIAL = Uuid::From128BitBE(
30     Uuid::UUID128Bit{{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89}});
31 
32 static const Uuid kBase = Uuid::From128BitBE(
33     Uuid::UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}});
34 
35 static const Uuid kBaseLe = Uuid::From128BitLE(Uuid::UUID128Bit{
36     {0xfb,
37      0x34,
38      0x9b,
39      0x5f,
40      0x80,
41      0x00,
42      0x00,
43      0x80,
44      0x00,
45      0x10,
46      0x00,
47      0x00,
48      0x00,
49      0x00,
50      0x00,
51      0x00}});
52 
TEST(UuidTest,IsEmpty)53 TEST(UuidTest, IsEmpty) {
54   ASSERT_TRUE(Uuid::kEmpty.IsEmpty());
55   ASSERT_FALSE(kBase.IsEmpty());
56 }
57 
TEST(UuidTest,GetShortestRepresentationSize)58 TEST(UuidTest, GetShortestRepresentationSize) {
59   ASSERT_TRUE(Uuid::kNumBytes16 == kBase.GetShortestRepresentationSize());
60   ASSERT_TRUE(Uuid::kNumBytes32 == Uuid::From32Bit(0x01234567).GetShortestRepresentationSize());
61   ASSERT_TRUE(Uuid::kNumBytes128 == Uuid::kEmpty.GetShortestRepresentationSize());
62 }
63 
TEST(UuidTest,As16Bit)64 TEST(UuidTest, As16Bit) {
65   // Even though this is is not 16bit UUID, we should be able to get proper bits
66   ASSERT_EQ((uint16_t)0x1111, ONES.As16Bit());
67   ASSERT_EQ((uint16_t)0x4567, SEQUENTIAL.As16Bit());
68   ASSERT_EQ((uint16_t)0x0000, kBase.As16Bit());
69 }
70 
TEST(UuidTest,As32Bit)71 TEST(UuidTest, As32Bit) {
72   // Even though this is is not 32bit UUID, we should be able to get proper bits
73   ASSERT_EQ((uint32_t)0x11111111, ONES.As32Bit());
74   ASSERT_EQ((uint32_t)0x01234567, SEQUENTIAL.As32Bit());
75   ASSERT_EQ((uint32_t)0x00000000, kBase.As32Bit());
76   ASSERT_EQ((uint32_t)0x12345678, Uuid::From32Bit(0x12345678).As32Bit());
77 }
78 
TEST(UuidTest,Is16Bit)79 TEST(UuidTest, Is16Bit) {
80   ASSERT_FALSE(ONES.Is16Bit());
81   ASSERT_FALSE(SEQUENTIAL.Is16Bit());
82   ASSERT_TRUE(kBase.Is16Bit());
83   auto uuid = Uuid::FromString("1ae8");
84   ASSERT_TRUE(uuid);
85   ASSERT_TRUE(uuid->Is16Bit());
86 }
87 
TEST(UuidTest,From16Bit)88 TEST(UuidTest, From16Bit) {
89   ASSERT_EQ(Uuid::From16Bit(0x0000), kBase);
90 
91   const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
92   Uuid uuid = Uuid::From16Bit(0x0001);
93   ASSERT_TRUE(memcmp(uuid.data(), u2, sizeof(u2)) == 0);
94 
95   const uint8_t u3[] = {0x00, 0x00, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
96   uuid = Uuid::From16Bit(0x553e);
97   ASSERT_TRUE(memcmp(uuid.data(), u3, sizeof(u3)) == 0);
98 
99   const uint8_t u4[] = {0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
100   uuid = Uuid::From16Bit(0xffff);
101   ASSERT_TRUE(memcmp(uuid.data(), u4, sizeof(u4)) == 0);
102 }
103 
TEST(UuidTest,From32Bit)104 TEST(UuidTest, From32Bit) {
105   ASSERT_EQ(Uuid::From32Bit(0x00000000), kBase);
106 
107   const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
108   Uuid uuid = Uuid::From32Bit(0x00000001);
109   ASSERT_TRUE(memcmp(uuid.data(), u2, sizeof(u2)) == 0);
110 
111   const uint8_t u3[] = {0x33, 0x44, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
112   uuid = Uuid::From32Bit(0x3344553e);
113   ASSERT_TRUE(memcmp(uuid.data(), u3, sizeof(u3)) == 0);
114 
115   const uint8_t u4[] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
116   uuid = Uuid::From32Bit(0xffffffff);
117   ASSERT_TRUE(memcmp(uuid.data(), u4, sizeof(u4)) == 0);
118 }
119 
TEST(UuidTest,ToString)120 TEST(UuidTest, ToString) {
121   const std::string UUID_BASE_STR = "00000000-0000-1000-8000-00805f9b34fb";
122   const std::string UUID_EMP_STR = "00000000-0000-0000-0000-000000000000";
123   const std::string UUID_ONES_STR = "11111111-1111-1111-1111-111111111111";
124   const std::string UUID_SEQ_STR = "01234567-89ab-cdef-abcd-ef0123456789";
125 
126   ASSERT_EQ(UUID_BASE_STR, kBase.ToString());
127   ASSERT_EQ(UUID_EMP_STR, Uuid::kEmpty.ToString());
128   ASSERT_EQ(UUID_ONES_STR, ONES.ToString());
129   ASSERT_EQ(UUID_SEQ_STR, SEQUENTIAL.ToString());
130 
131   Uuid uuid = Uuid::From32Bit(0x12345678);
132   ASSERT_EQ("12345678-0000-1000-8000-00805f9b34fb", uuid.ToString());
133 }
134 
TEST(UuidTest,test_string_to_uuid)135 TEST(UuidTest, test_string_to_uuid) {
136   const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
137   auto uuid = Uuid::FromString("e39c6285-867f-4b1d-9db0-35fbd9aebf22");
138   ASSERT_TRUE(uuid);
139   ASSERT_TRUE(memcmp(uuid->data(), u1, sizeof(u1)) == 0);
140 
141   const uint8_t u2[] = {0x00, 0x00, 0x1a, 0xe8, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
142   uuid = Uuid::FromString("1Ae8");
143   ASSERT_TRUE(uuid);
144   ASSERT_TRUE(memcmp(uuid->data(), u2, sizeof(u2)) == 0);
145 
146   const uint8_t u3[] = {0x12, 0x34, 0x11, 0x28, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
147   uuid = Uuid::FromString("12341128");
148   ASSERT_TRUE(uuid);
149   ASSERT_TRUE(memcmp(uuid->data(), u3, sizeof(u3)) == 0);
150 }
151 
TEST(UuidTest,legacy)152 TEST(UuidTest, legacy) {
153   const std::string UUID_BASE_STR = "e39c6285-867f-4b1d-9db0-35fbd9aebf22";
154   auto uuid = Uuid::FromString("e39c6285-867f-4b1d-9db0-35fbd9aebf22");
155   ASSERT_EQ(UUID_BASE_STR, uuid->ToLegacyConfigString());
156   ASSERT_EQ(uuid, Uuid::FromLegacyConfigString(UUID_BASE_STR));
157 }
158 
TEST(UuidTest,inequalities)159 TEST(UuidTest, inequalities) {
160   auto uuid1 = Uuid::kEmpty;
161   auto uuid2 = Uuid::FromString("11111111-1111-1111-1111-111111111111");
162 
163   ASSERT_TRUE(uuid1 < uuid2);
164   ASSERT_TRUE(uuid1 != uuid2);
165 }
166 
TEST(UuidTest,endianness)167 TEST(UuidTest, endianness) {
168   Uuid uuidBe = kBase;
169   Uuid uuidLe = kBaseLe;
170   ASSERT_EQ(kBase, kBaseLe);
171 }
172 
TEST(UuidTest,test_string_to_uuid_invalid)173 TEST(UuidTest, test_string_to_uuid_invalid) {
174   ASSERT_FALSE(Uuid::FromString("This is not a UUID"));
175   ASSERT_FALSE(Uuid::FromString("11212"));
176   ASSERT_FALSE(Uuid::FromString("1121 "));
177   ASSERT_FALSE(Uuid::FromString("AGFE"));
178   ASSERT_FALSE(Uuid::FromString("ABFG"));
179   ASSERT_FALSE(Uuid::FromString("e39c6285867f14b1d9db035fbd9aebf22"));
180   ASSERT_FALSE(Uuid::FromString("12234567-89ab-cdef-abcd-ef01234567ZZ"));
181   ASSERT_FALSE(Uuid::FromString(std::string()));
182   ASSERT_FALSE(Uuid::FromString("e39c6285 867f 4b1d 9db0 35fbd9aebf22"));
183   ASSERT_FALSE(Uuid::FromString("ab34xx78"));
184 }
185 
186 }  // namespace testing
187