xref: /aosp_15_r20/external/pigweed/pw_bluetooth/uuid_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2022 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #include "pw_bluetooth/uuid.h"
15 
16 #include <array>
17 #include <cstdint>
18 
19 #include "pw_span/span.h"
20 #include "pw_unit_test/framework.h"
21 
22 namespace pw::bluetooth {
23 namespace {
24 
25 // 16-bit and 32-bit short form UUID.
26 constexpr Uuid kShortUuid16{0x1234};
27 constexpr Uuid kShortUuid32{0xabcd1234};
28 
29 // UUID initialized from a span.
30 constexpr Uuid kLongUuidArray{
31     pw::span<const uint8_t, 16>(std::array<uint8_t, 16>{
32         1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})};
33 
34 // UUID initialized from a string. This is the same as kLongUuidArray but in
35 // string form.
36 constexpr Uuid kLongUuidString{"100f0e0d-0c0b-0a09-0807-060504030201"};
37 
38 // UUID initialized combining another UUID with a 32-bit value.
39 constexpr Uuid kLongUuidComposed1 = Uuid(0xabcd, kLongUuidString);
40 constexpr Uuid kLongUuidComposed2 = Uuid(0x1234abcd, kLongUuidString);
41 
42 // Make sure that all these values are actually constexpr.
43 static_assert(kShortUuid16 != kShortUuid32, "constexpr check");
44 static_assert(kLongUuidArray == kLongUuidString, "constexpr check");
45 static_assert(kLongUuidComposed2.ToString() ==
46                   "1234abcd-0c0b-0a09-0807-060504030201",
47               "constexpr check");
48 
TEST(UuidTest,ConstructorTest)49 TEST(UuidTest, ConstructorTest) {
50   // Compare 16-bit with 128-bit.
51   EXPECT_EQ(kShortUuid16, Uuid("00001234-0000-1000-8000-00805f9b34fb"));
52   EXPECT_EQ(kShortUuid32, Uuid("abcd1234-0000-1000-8000-00805f9b34fb"));
53 
54   EXPECT_EQ(kShortUuid16.ToString(), "00001234-0000-1000-8000-00805f9b34fb");
55   auto short_span = kShortUuid16.As16BitSpan();
56   EXPECT_EQ(2u, short_span.size());
57   EXPECT_EQ(short_span[0], 0x34);  // UUIDs are encoded little endian.
58   EXPECT_EQ(short_span[1], 0x12);
59 
60   EXPECT_EQ(kLongUuidArray.ToString(), "100f0e0d-0c0b-0a09-0807-060504030201");
61   auto long_span = kLongUuidArray.As128BitSpan();
62   EXPECT_EQ(16u, long_span.size());
63   EXPECT_EQ(long_span[12], 0x0d);
64   EXPECT_EQ(long_span[13], 0x0e);
65 
66   // These two are the same UUID initialized in different ways.
67   EXPECT_EQ(kLongUuidArray, kLongUuidString);
68 
69   // Composed UUID always set the 32-bits in the first groups, regardless of
70   // whether we pass a 16-bit or 32-bit value, thus the first 0 chars in this
71   // string are 0.
72   static_assert(
73       kLongUuidComposed1.ToString() == "0000abcd-0c0b-0a09-0807-060504030201",
74       "constexpr check");
75   static_assert(
76       kLongUuidComposed2.ToString() == "1234abcd-0c0b-0a09-0807-060504030201",
77       "constexpr check");
78 
79   // Check that the standard Bluetooth Base is correct.
80   static_assert(Uuid::BluetoothBase().ToString() ==
81                     "00000000-0000-1000-8000-00805f9b34fb",
82                 "constexpr check");
83 }
84 
TEST(UuidTest,CombineShortUuidTest)85 TEST(UuidTest, CombineShortUuidTest) {
86   // The short 16-bit value can be represented as 16 or 32 bit, but the 32-bit
87   // one can only be represented as 32-bits.
88   EXPECT_TRUE(kShortUuid16.Is16BitUuid());
89   EXPECT_TRUE(kShortUuid16.Is32BitUuid());
90   EXPECT_FALSE(kShortUuid32.Is16BitUuid());
91   EXPECT_TRUE(kShortUuid32.Is32BitUuid());
92 
93   // The composed UUID is not a standard one, but matches the base it was used
94   // to construct it.
95   EXPECT_FALSE(kLongUuidComposed1.Is16BitUuid());
96   EXPECT_FALSE(kLongUuidComposed1.Is32BitUuid());
97 
98   EXPECT_TRUE(kLongUuidComposed1.Same96BitBase(kLongUuidString));
99   EXPECT_TRUE(kLongUuidComposed2.Same96BitBase(kLongUuidString));
100 
101   // Composing always uses 32-bit values (setting the high bits as 0s) so they
102   // don't match the base, unless it is zero extended.
103   EXPECT_FALSE(kLongUuidComposed1.Same112BitBase(kLongUuidString));
104   EXPECT_FALSE(kLongUuidComposed2.Same112BitBase(kLongUuidString));
105 
106   EXPECT_TRUE(kLongUuidComposed1.Same112BitBase(Uuid(0, kLongUuidString)));
107 }
108 
109 }  // namespace
110 }  // namespace pw::bluetooth
111