1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/macros.h"
6 #include "device/bluetooth/bluetooth_uuid.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(BluetoothUUIDTest
, BluetoothUUID
) {
12 const char kValid128Bit0
[] = "12345678-1234-5678-9abc-def123456789";
13 const char kValid128Bit1
[] = "00001101-0000-1000-8000-00805f9b34fb";
14 const char kInvalid36Char0
[] = "1234567-1234-5678-9abc-def123456789";
15 const char kInvalid36Char1
[] = "0x00001101-0000-1000-8000-00805f9b34fb";
16 const char kInvalid4Char
[] = "Z101";
17 const char kValid16Bit
[] = "0x1101";
18 const char kValid32Bit
[] = "00001101";
20 // Valid 128-bit custom UUID.
21 BluetoothUUID
uuid0(kValid128Bit0
);
22 EXPECT_TRUE(uuid0
.IsValid());
23 EXPECT_EQ(BluetoothUUID::kFormat128Bit
, uuid0
.format());
24 EXPECT_EQ(uuid0
.value(), uuid0
.canonical_value());
26 // Valid 128-bit UUID.
27 BluetoothUUID
uuid1(kValid128Bit1
);
28 EXPECT_TRUE(uuid1
.IsValid());
29 EXPECT_EQ(BluetoothUUID::kFormat128Bit
, uuid1
.format());
30 EXPECT_EQ(uuid1
.value(), uuid1
.canonical_value());
32 EXPECT_NE(uuid0
, uuid1
);
34 // Invalid 128-bit UUID.
35 BluetoothUUID
uuid2(kInvalid36Char0
);
36 EXPECT_FALSE(uuid2
.IsValid());
37 EXPECT_EQ(BluetoothUUID::kFormatInvalid
, uuid2
.format());
38 EXPECT_TRUE(uuid2
.value().empty());
39 EXPECT_TRUE(uuid2
.canonical_value().empty());
41 // Invalid 128-bit UUID.
42 BluetoothUUID
uuid3(kInvalid36Char1
);
43 EXPECT_FALSE(uuid3
.IsValid());
44 EXPECT_EQ(BluetoothUUID::kFormatInvalid
, uuid3
.format());
45 EXPECT_TRUE(uuid3
.value().empty());
46 EXPECT_TRUE(uuid3
.canonical_value().empty());
48 // Invalid 16-bit UUID.
49 BluetoothUUID
uuid4(kInvalid4Char
);
50 EXPECT_FALSE(uuid4
.IsValid());
51 EXPECT_EQ(BluetoothUUID::kFormatInvalid
, uuid4
.format());
52 EXPECT_TRUE(uuid4
.value().empty());
53 EXPECT_TRUE(uuid4
.canonical_value().empty());
56 BluetoothUUID
uuid5(kValid16Bit
);
57 EXPECT_TRUE(uuid5
.IsValid());
58 EXPECT_EQ(BluetoothUUID::kFormat16Bit
, uuid5
.format());
59 EXPECT_NE(uuid5
.value(), uuid5
.canonical_value());
60 EXPECT_EQ("1101", uuid5
.value());
61 EXPECT_EQ(kValid128Bit1
, uuid5
.canonical_value());
64 BluetoothUUID
uuid6(kValid32Bit
);
65 EXPECT_TRUE(uuid6
.IsValid());
66 EXPECT_EQ(BluetoothUUID::kFormat32Bit
, uuid6
.format());
67 EXPECT_NE(uuid6
.value(), uuid6
.canonical_value());
68 EXPECT_EQ("00001101", uuid6
.value());
69 EXPECT_EQ(kValid128Bit1
, uuid6
.canonical_value());
71 // uuid5, uuid6, and uuid1 are equivalent.
72 EXPECT_EQ(uuid5
, uuid6
);
73 EXPECT_EQ(uuid1
, uuid5
);
74 EXPECT_EQ(uuid1
, uuid6
);
77 // Verify that UUIDs are parsed case-insensitively
78 TEST(BluetoothUUIDTest
, BluetoothUUID_CaseInsensitive
) {
79 const char k16Bit
[] = "1abc";
80 const char k32Bit
[] = "00001abc";
81 const char k128Bit
[] = "00001abc-0000-1000-8000-00805f9b34fb";
84 const std::string input_uuid
;
85 const std::string expected_value
;
90 { "00001abc", k32Bit
},
91 { "00001ABC", k32Bit
},
92 { "00001aBc", k32Bit
},
93 { "00001abc-0000-1000-8000-00805f9b34fb", k128Bit
},
94 { "00001ABC-0000-1000-8000-00805F9B34FB", k128Bit
},
95 { "00001aBc-0000-1000-8000-00805F9b34fB", k128Bit
},
98 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(test_cases
); ++i
) {
99 SCOPED_TRACE("Input UUID: " + test_cases
[i
].input_uuid
);
100 BluetoothUUID
uuid(test_cases
[i
].input_uuid
);
101 EXPECT_TRUE(uuid
.IsValid());
102 EXPECT_EQ(test_cases
[i
].expected_value
, uuid
.value());
103 EXPECT_EQ(k128Bit
, uuid
.canonical_value());
107 } // namespace device