• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2017 Google, Inc.
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 <bluetooth/uuid.h>
20 #include <gtest/gtest.h>
21 
22 using bluetooth::Uuid;
23 
24 static const Uuid ONES = Uuid::From128BitBE(
25     Uuid::UUID128Bit{{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
26                       0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}});
27 
28 static const Uuid SEQUENTIAL = Uuid::From128BitBE(
29     Uuid::UUID128Bit{{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xab,
30                       0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89}});
31 
32 constexpr Uuid kBase = Uuid::From128BitBE(
33     Uuid::UUID128Bit{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80,
34                       0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb}});
35 
TEST(UuidTest,IsEmpty)36 TEST(UuidTest, IsEmpty) {
37   EXPECT_TRUE(Uuid::kEmpty.IsEmpty());
38   EXPECT_FALSE(kBase.IsEmpty());
39 }
40 
TEST(UuidTest,GetShortestRepresentationSize)41 TEST(UuidTest, GetShortestRepresentationSize) {
42   EXPECT_TRUE(Uuid::kNumBytes16 == kBase.GetShortestRepresentationSize());
43   EXPECT_TRUE(Uuid::kNumBytes32 ==
44               Uuid::From32Bit(0x01234567).GetShortestRepresentationSize());
45   EXPECT_TRUE(Uuid::kNumBytes128 ==
46               Uuid::kEmpty.GetShortestRepresentationSize());
47 }
48 
TEST(UuidTest,As16Bit)49 TEST(UuidTest, As16Bit) {
50   // Even though this is is not 16bit UUID, we should be able to get proper bits
51   EXPECT_EQ((uint16_t)0x1111, ONES.As16Bit());
52   EXPECT_EQ((uint16_t)0x4567, SEQUENTIAL.As16Bit());
53   EXPECT_EQ((uint16_t)0x0000, kBase.As16Bit());
54 }
55 
TEST(UuidTest,As32Bit)56 TEST(UuidTest, As32Bit) {
57   // Even though this is is not 32bit UUID, we should be able to get proper bits
58   EXPECT_EQ((uint32_t)0x11111111, ONES.As32Bit());
59   EXPECT_EQ((uint32_t)0x01234567, SEQUENTIAL.As32Bit());
60   EXPECT_EQ((uint32_t)0x00000000, kBase.As32Bit());
61   EXPECT_EQ((uint32_t)0x12345678, Uuid::From32Bit(0x12345678).As32Bit());
62 }
63 
TEST(UuidTest,Is16Bit)64 TEST(UuidTest, Is16Bit) {
65   EXPECT_FALSE(ONES.Is16Bit());
66   EXPECT_FALSE(SEQUENTIAL.Is16Bit());
67   EXPECT_TRUE(kBase.Is16Bit());
68   EXPECT_TRUE(Uuid::FromString("1ae8").Is16Bit());
69 }
70 
TEST(UuidTest,From16Bit)71 TEST(UuidTest, From16Bit) {
72   EXPECT_EQ(Uuid::From16Bit(0x0000), kBase);
73 
74   const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00,
75                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
76   Uuid uuid = Uuid::From16Bit(0x0001);
77   EXPECT_TRUE(memcmp(&uuid, u2, sizeof(u2)) == 0);
78 
79   const uint8_t u3[] = {0x00, 0x00, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00,
80                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
81   uuid = Uuid::From16Bit(0x553e);
82   EXPECT_TRUE(memcmp(&uuid, u3, sizeof(u3)) == 0);
83 
84   const uint8_t u4[] = {0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00,
85                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
86   uuid = Uuid::From16Bit(0xffff);
87   EXPECT_TRUE(memcmp(&uuid, u4, sizeof(u4)) == 0);
88 }
89 
TEST(UuidTest,From32Bit)90 TEST(UuidTest, From32Bit) {
91   EXPECT_EQ(Uuid::From32Bit(0x00000000), kBase);
92 
93   const uint8_t u2[] = {0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00,
94                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
95   Uuid uuid = Uuid::From32Bit(0x00000001);
96   EXPECT_TRUE(memcmp(&uuid, u2, sizeof(u2)) == 0);
97 
98   const uint8_t u3[] = {0x33, 0x44, 0x55, 0x3e, 0x00, 0x00, 0x10, 0x00,
99                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
100   uuid = Uuid::From32Bit(0x3344553e);
101   EXPECT_TRUE(memcmp(&uuid, u3, sizeof(u3)) == 0);
102 
103   const uint8_t u4[] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x10, 0x00,
104                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
105   uuid = Uuid::From32Bit(0xffffffff);
106   EXPECT_TRUE(memcmp(&uuid, u4, sizeof(u4)) == 0);
107 }
108 
TEST(UuidTest,ToString)109 TEST(UuidTest, ToString) {
110   const std::string UUID_BASE_STR = "00000000-0000-1000-8000-00805f9b34fb";
111   const std::string UUID_EMP_STR = "00000000-0000-0000-0000-000000000000";
112   const std::string UUID_ONES_STR = "11111111-1111-1111-1111-111111111111";
113   const std::string UUID_SEQ_STR = "01234567-89ab-cdef-abcd-ef0123456789";
114 
115   EXPECT_EQ(UUID_BASE_STR, kBase.ToString());
116   EXPECT_EQ(UUID_EMP_STR, Uuid::kEmpty.ToString());
117   EXPECT_EQ(UUID_ONES_STR, ONES.ToString());
118   EXPECT_EQ(UUID_SEQ_STR, SEQUENTIAL.ToString());
119 
120   Uuid uuid = Uuid::From32Bit(0x12345678);
121   EXPECT_EQ("12345678-0000-1000-8000-00805f9b34fb", uuid.ToString());
122 }
123 
TEST(BtifStorageTest,test_string_to_uuid)124 TEST(BtifStorageTest, test_string_to_uuid) {
125   const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
126                         0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
127   bool is_valid = false;
128   Uuid uuid =
129       Uuid::FromString("e39c6285-867f-4b1d-9db0-35fbd9aebf22", &is_valid);
130   EXPECT_TRUE(is_valid);
131   EXPECT_TRUE(memcmp(&uuid, u1, sizeof(u1)) == 0);
132 
133   const uint8_t u2[] = {0x00, 0x00, 0x1a, 0xe8, 0x00, 0x00, 0x10, 0x00,
134                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
135   is_valid = false;
136   uuid = Uuid::FromString("1Ae8", &is_valid);
137   EXPECT_TRUE(is_valid);
138   EXPECT_TRUE(memcmp(&uuid, u2, sizeof(u2)) == 0);
139 
140   const uint8_t u3[] = {0x12, 0x34, 0x11, 0x28, 0x00, 0x00, 0x10, 0x00,
141                         0x80, 0x00, 0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb};
142   is_valid = false;
143   uuid = Uuid::FromString("12341128", &is_valid);
144   EXPECT_TRUE(is_valid);
145   EXPECT_TRUE(memcmp(&uuid, u3, sizeof(u3)) == 0);
146 }
147 
TEST(BtifStorageTest,test_string_to_uuid_invalid)148 TEST(BtifStorageTest, test_string_to_uuid_invalid) {
149   bool is_valid = false;
150   Uuid uuid = Uuid::FromString("This is not a UUID", &is_valid);
151   EXPECT_FALSE(is_valid);
152 
153   uuid = Uuid::FromString("11212", &is_valid);
154   EXPECT_FALSE(is_valid);
155 
156   uuid = Uuid::FromString("1121 ", &is_valid);
157   EXPECT_FALSE(is_valid);
158 
159   uuid = Uuid::FromString("AGFE", &is_valid);
160   EXPECT_FALSE(is_valid);
161 
162   uuid = Uuid::FromString("ABFG", &is_valid);
163   EXPECT_FALSE(is_valid);
164 
165   uuid = Uuid::FromString("e39c6285867f14b1d9db035fbd9aebf22", &is_valid);
166   EXPECT_FALSE(is_valid);
167 
168   uuid = Uuid::FromString("12234567-89ab-cdef-abcd-ef01234567ZZ", &is_valid);
169   EXPECT_FALSE(is_valid);
170 }
171