1 //
2 // Copyright (C) 2015 Google, Inc.
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 <stdint.h>
18 #include <algorithm>
19 #include <array>
20
21 #include <gtest/gtest.h>
22
23 #include "service/common/bluetooth/uuid.h"
24
25 using namespace bluetooth;
26
27 namespace {
28
29 const std::array<uint8_t, UUID::kNumBytes128> kBtSigBaseUUID = {{
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0x80,
31 0x5f, 0x9b, 0x34, 0xfb,
32 }};
33
34 } // namespace
35
36 // Verify that an uninitialized UUID is equal
37 // To the BT SIG Base UUID.
TEST(UUIDTest,DefaultUUID)38 TEST(UUIDTest, DefaultUUID) {
39 UUID uuid;
40 ASSERT_TRUE(uuid.is_valid());
41 ASSERT_TRUE(uuid.GetFullBigEndian() == kBtSigBaseUUID);
42 }
43
44 // Verify that we initialize a 16-bit UUID in a
45 // way consistent with how we read it.
TEST(UUIDTest,Init16Bit)46 TEST(UUIDTest, Init16Bit) {
47 auto my_uuid_16 = kBtSigBaseUUID;
48 my_uuid_16[2] = 0xde;
49 my_uuid_16[3] = 0xad;
50 UUID uuid(UUID::UUID16Bit({{0xde, 0xad}}));
51 ASSERT_TRUE(uuid.is_valid());
52 ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_16);
53 ASSERT_TRUE(UUID::kNumBytes16 == uuid.GetShortestRepresentationSize());
54 }
55
56 // Verify that we initialize a 16-bit UUID in a
57 // way consistent with how we read it.
TEST(UUIDTest,Init16BitString)58 TEST(UUIDTest, Init16BitString) {
59 auto my_uuid_16 = kBtSigBaseUUID;
60 my_uuid_16[2] = 0xde;
61 my_uuid_16[3] = 0xad;
62 UUID uuid("dead");
63 ASSERT_TRUE(uuid.is_valid());
64 ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_16);
65 ASSERT_TRUE(UUID::kNumBytes16 == uuid.GetShortestRepresentationSize());
66
67 uuid = UUID("0xdead");
68 ASSERT_TRUE(uuid.is_valid());
69 ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_16);
70 ASSERT_TRUE(UUID::kNumBytes16 == uuid.GetShortestRepresentationSize());
71 }
72
73 // Verify that we initialize a 32-bit UUID in a
74 // way consistent with how we read it.
TEST(UUIDTest,Init32Bit)75 TEST(UUIDTest, Init32Bit) {
76 auto my_uuid_32 = kBtSigBaseUUID;
77 my_uuid_32[0] = 0xde;
78 my_uuid_32[1] = 0xad;
79 my_uuid_32[2] = 0xbe;
80 my_uuid_32[3] = 0xef;
81 UUID uuid(UUID::UUID32Bit({{0xde, 0xad, 0xbe, 0xef}}));
82 ASSERT_TRUE(uuid.is_valid());
83 ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_32);
84 ASSERT_TRUE(UUID::kNumBytes32 == uuid.GetShortestRepresentationSize());
85 }
86
87 // Verify correct reading of a 32-bit UUID initialized from string.
TEST(UUIDTest,Init32BitString)88 TEST(UUIDTest, Init32BitString) {
89 auto my_uuid_32 = kBtSigBaseUUID;
90 my_uuid_32[0] = 0xde;
91 my_uuid_32[1] = 0xad;
92 my_uuid_32[2] = 0xbe;
93 my_uuid_32[3] = 0xef;
94 UUID uuid("deadbeef");
95 ASSERT_TRUE(uuid.is_valid());
96 ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_32);
97 ASSERT_TRUE(UUID::kNumBytes32 == uuid.GetShortestRepresentationSize());
98 }
99
100 // Verify that we initialize a 128-bit UUID in a
101 // way consistent with how we read it.
TEST(UUIDTest,Init128Bit)102 TEST(UUIDTest, Init128Bit) {
103 auto my_uuid_128 = kBtSigBaseUUID;
104 for (int i = 0; i < static_cast<int>(my_uuid_128.size()); ++i) {
105 my_uuid_128[i] = i;
106 }
107
108 UUID uuid(my_uuid_128);
109 ASSERT_TRUE(uuid.is_valid());
110 ASSERT_TRUE(uuid.GetFullBigEndian() == my_uuid_128);
111 ASSERT_TRUE(UUID::kNumBytes128 == uuid.GetShortestRepresentationSize());
112 }
113
114 // Verify that we initialize a 128-bit UUID in a
115 // way consistent with how we read it as LE.
TEST(UUIDTest,Init128BitLittleEndian)116 TEST(UUIDTest, Init128BitLittleEndian) {
117 auto my_uuid_128 = kBtSigBaseUUID;
118 for (int i = 0; i < static_cast<int>(my_uuid_128.size()); ++i) {
119 my_uuid_128[i] = i;
120 }
121
122 UUID uuid(my_uuid_128);
123 std::reverse(my_uuid_128.begin(), my_uuid_128.end());
124 ASSERT_TRUE(uuid.is_valid());
125 ASSERT_TRUE(uuid.GetFullLittleEndian() == my_uuid_128);
126 }
127
128 // Verify that we initialize a 128-bit UUID in a
129 // way consistent with how we read it.
TEST(UUIDTest,Init128BitString)130 TEST(UUIDTest, Init128BitString) {
131 UUID::UUID128Bit my_uuid{
132 {7, 1, 6, 8, 14, 255, 16, 2, 3, 4, 5, 6, 7, 8, 9, 10}};
133 std::string my_uuid_string("07010608-0eff-1002-0304-05060708090a");
134
135 UUID uuid0(my_uuid);
136 UUID uuid1(my_uuid_string);
137
138 ASSERT_TRUE(uuid0.is_valid());
139 ASSERT_TRUE(uuid1.is_valid());
140 ASSERT_TRUE(uuid0 == uuid1);
141 ASSERT_TRUE(UUID::kNumBytes128 == uuid0.GetShortestRepresentationSize());
142 }
143
TEST(UUIDTest,InitInvalid)144 TEST(UUIDTest, InitInvalid) {
145 UUID uuid0("000102030405060708090A0B0C0D0E0F");
146 ASSERT_FALSE(uuid0.is_valid());
147
148 UUID uuid1("1*90");
149 ASSERT_FALSE(uuid1.is_valid());
150
151 UUID uuid2("109g");
152 ASSERT_FALSE(uuid1.is_valid());
153 }
154
TEST(UUIDTest,ToString)155 TEST(UUIDTest, ToString) {
156 const UUID::UUID16Bit data{{0x18, 0x0d}};
157 UUID uuid(data);
158 std::string uuid_string = uuid.ToString();
159 EXPECT_EQ("0000180d-0000-1000-8000-00805f9b34fb", uuid_string);
160 }
161