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 
TEST(UuidTest,IsEmpty)35 TEST(UuidTest, IsEmpty) {
36   ASSERT_TRUE(Uuid::kEmpty.IsEmpty());
37   ASSERT_FALSE(kBase.IsEmpty());
38 }
39 
TEST(UuidTest,GetShortestRepresentationSize)40 TEST(UuidTest, GetShortestRepresentationSize) {
41   ASSERT_TRUE(Uuid::kNumBytes16 == kBase.GetShortestRepresentationSize());
42   ASSERT_TRUE(Uuid::kNumBytes32 == Uuid::From32Bit(0x01234567).GetShortestRepresentationSize());
43   ASSERT_TRUE(Uuid::kNumBytes128 == Uuid::kEmpty.GetShortestRepresentationSize());
44 }
45 
TEST(UuidTest,As16Bit)46 TEST(UuidTest, As16Bit) {
47   // Even though this is is not 16bit UUID, we should be able to get proper bits
48   ASSERT_EQ((uint16_t)0x1111, ONES.As16Bit());
49   ASSERT_EQ((uint16_t)0x4567, SEQUENTIAL.As16Bit());
50   ASSERT_EQ((uint16_t)0x0000, kBase.As16Bit());
51 }
52 
TEST(UuidTest,As32Bit)53 TEST(UuidTest, As32Bit) {
54   // Even though this is is not 32bit UUID, we should be able to get proper bits
55   ASSERT_EQ((uint32_t)0x11111111, ONES.As32Bit());
56   ASSERT_EQ((uint32_t)0x01234567, SEQUENTIAL.As32Bit());
57   ASSERT_EQ((uint32_t)0x00000000, kBase.As32Bit());
58   ASSERT_EQ((uint32_t)0x12345678, Uuid::From32Bit(0x12345678).As32Bit());
59 }
60 
TEST(UuidTest,Is16Bit)61 TEST(UuidTest, Is16Bit) {
62   ASSERT_FALSE(ONES.Is16Bit());
63   ASSERT_FALSE(SEQUENTIAL.Is16Bit());
64   ASSERT_TRUE(kBase.Is16Bit());
65   auto uuid = Uuid::FromString("1ae8");
66   ASSERT_TRUE(uuid);
67   ASSERT_TRUE(uuid->Is16Bit());
68 }
69 
TEST(UuidTest,From16Bit)70 TEST(UuidTest, From16Bit) {
71   ASSERT_EQ(Uuid::From16Bit(0x0000), kBase);
72 
73   const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
74   Uuid uuid = Uuid::From16Bit(0x0001);
75   ASSERT_TRUE(memcmp(uuid.data(), u2, sizeof(u2)) == 0);
76 
77   const uint8_t u3[] = {0x00, 0x00, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
78   uuid = Uuid::From16Bit(0x553e);
79   ASSERT_TRUE(memcmp(uuid.data(), u3, sizeof(u3)) == 0);
80 
81   const uint8_t u4[] = {0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
82   uuid = Uuid::From16Bit(0xffff);
83   ASSERT_TRUE(memcmp(uuid.data(), u4, sizeof(u4)) == 0);
84 }
85 
TEST(UuidTest,From32Bit)86 TEST(UuidTest, From32Bit) {
87   ASSERT_EQ(Uuid::From32Bit(0x00000000), kBase);
88 
89   const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
90   Uuid uuid = Uuid::From32Bit(0x00000001);
91   ASSERT_TRUE(memcmp(uuid.data(), u2, sizeof(u2)) == 0);
92 
93   const uint8_t u3[] = {0x33, 0x44, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
94   uuid = Uuid::From32Bit(0x3344553e);
95   ASSERT_TRUE(memcmp(uuid.data(), u3, sizeof(u3)) == 0);
96 
97   const uint8_t u4[] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
98   uuid = Uuid::From32Bit(0xffffffff);
99   ASSERT_TRUE(memcmp(uuid.data(), u4, sizeof(u4)) == 0);
100 }
101 
TEST(UuidTest,ToString)102 TEST(UuidTest, ToString) {
103   const std::string UUID_BASE_STR = "00000000-0000-1000-8000-00805f9b34fb";
104   const std::string UUID_EMP_STR = "00000000-0000-0000-0000-000000000000";
105   const std::string UUID_ONES_STR = "11111111-1111-1111-1111-111111111111";
106   const std::string UUID_SEQ_STR = "01234567-89ab-cdef-abcd-ef0123456789";
107 
108   ASSERT_EQ(UUID_BASE_STR, kBase.ToString());
109   ASSERT_EQ(UUID_EMP_STR, Uuid::kEmpty.ToString());
110   ASSERT_EQ(UUID_ONES_STR, ONES.ToString());
111   ASSERT_EQ(UUID_SEQ_STR, SEQUENTIAL.ToString());
112 
113   Uuid uuid = Uuid::From32Bit(0x12345678);
114   ASSERT_EQ("12345678-0000-1000-8000-00805f9b34fb", uuid.ToString());
115 }
116 
TEST(UuidTest,test_string_to_uuid)117 TEST(UuidTest, test_string_to_uuid) {
118   const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d, 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
119   auto uuid = Uuid::FromString("e39c6285-867f-4b1d-9db0-35fbd9aebf22");
120   ASSERT_TRUE(uuid);
121   ASSERT_TRUE(memcmp(uuid->data(), u1, sizeof(u1)) == 0);
122 
123   const uint8_t u2[] = {0x00, 0x00, 0x1a, 0xe8, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
124   uuid = Uuid::FromString("1Ae8");
125   ASSERT_TRUE(uuid);
126   ASSERT_TRUE(memcmp(uuid->data(), u2, sizeof(u2)) == 0);
127 
128   const uint8_t u3[] = {0x12, 0x34, 0x11, 0x28, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
129   uuid = Uuid::FromString("12341128");
130   ASSERT_TRUE(uuid);
131   ASSERT_TRUE(memcmp(uuid->data(), u3, sizeof(u3)) == 0);
132 }
133 
TEST(UuidTest,test_string_to_uuid_invalid)134 TEST(UuidTest, test_string_to_uuid_invalid) {
135   ASSERT_FALSE(Uuid::FromString("This is not a UUID"));
136   ASSERT_FALSE(Uuid::FromString("11212"));
137   ASSERT_FALSE(Uuid::FromString("1121 "));
138   ASSERT_FALSE(Uuid::FromString("AGFE"));
139   ASSERT_FALSE(Uuid::FromString("ABFG"));
140   ASSERT_FALSE(Uuid::FromString("e39c6285867f14b1d9db035fbd9aebf22"));
141   ASSERT_FALSE(Uuid::FromString("12234567-89ab-cdef-abcd-ef01234567ZZ"));
142 }
143 
144 }  // namespace testing
145