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 <gtest/gtest.h>
18 
19 #include "service/common/bluetooth/gatt_identifier.h"
20 #include "service/common/bluetooth/uuid.h"
21 
22 namespace bluetooth {
23 namespace {
24 
25 const std::string kAddr0 = "00:01:02:03:04:05";
26 const std::string kAddr1 = "06:07:08:08:0a:0b";
27 
28 const UUID kUUID0;
29 const UUID kUUID1("180d");
30 
31 const int kId0 = 0;
32 const int kId1 = 1;
33 
TEST(GattIdentifierTest,ServiceId)34 TEST(GattIdentifierTest, ServiceId) {
35   auto service0 = GattIdentifier::CreateServiceId(kAddr0, kId0, kUUID0, true);
36 
37   EXPECT_TRUE(service0->IsService());
38   EXPECT_FALSE(service0->IsCharacteristic());
39   EXPECT_FALSE(service0->IsDescriptor());
40 
41   EXPECT_FALSE(service0->GetOwningServiceId());
42   EXPECT_FALSE(service0->GetOwningCharacteristicId());
43 
44   // Create different variants, swapping one entry at a time.
45   auto service1 = GattIdentifier::CreateServiceId(kAddr1, kId0, kUUID0, true);
46   auto service2 = GattIdentifier::CreateServiceId(kAddr0, kId1, kUUID0, true);
47   auto service3 = GattIdentifier::CreateServiceId(kAddr0, kId0, kUUID1, true);
48   auto service4 = GattIdentifier::CreateServiceId(kAddr0, kId0, kUUID0, false);
49 
50   EXPECT_TRUE(*service1 != *service0);
51   EXPECT_TRUE(*service2 != *service0);
52   EXPECT_TRUE(*service3 != *service0);
53   EXPECT_TRUE(*service4 != *service0);
54 
55   GattIdentifier service_copy = *service0;
56   EXPECT_TRUE(service_copy == *service0);
57 }
58 
TEST(GattIdentifierTest,CharacteristicId)59 TEST(GattIdentifierTest, CharacteristicId) {
60   auto service0 = GattIdentifier::CreateServiceId(kAddr0, kId0, kUUID0, true);
61   auto char0 = GattIdentifier::CreateCharacteristicId(kId1, kUUID1, *service0);
62 
63   EXPECT_FALSE(char0->IsService());
64   EXPECT_TRUE(char0->IsCharacteristic());
65   EXPECT_FALSE(char0->IsDescriptor());
66 
67   EXPECT_FALSE(char0->GetOwningCharacteristicId());
68   EXPECT_TRUE(*char0->GetOwningServiceId() == *service0);
69 
70   auto service1 = GattIdentifier::CreateServiceId(kAddr1, kId0, kUUID0, true);
71 
72   auto char1 = GattIdentifier::CreateCharacteristicId(kId0, kUUID1, *service0);
73   auto char2 = GattIdentifier::CreateCharacteristicId(kId1, kUUID0, *service0);
74   auto char3 = GattIdentifier::CreateCharacteristicId(kId1, kUUID1, *service1);
75 
76   EXPECT_TRUE(*char1 != *char0);
77   EXPECT_TRUE(*char2 != *char0);
78   EXPECT_TRUE(*char3 != *char0);
79 
80   GattIdentifier char_copy = *char0;
81   EXPECT_TRUE(char_copy == *char0);
82 
83   EXPECT_TRUE(*service0 != *char0);
84 }
85 
TEST(GattIdentifierTest,DescriptorId)86 TEST(GattIdentifierTest, DescriptorId) {
87   auto service0 = GattIdentifier::CreateServiceId(kAddr0, kId0, kUUID0, true);
88   auto char0 = GattIdentifier::CreateCharacteristicId(kId1, kUUID1, *service0);
89   auto desc0 = GattIdentifier::CreateDescriptorId(kId0, kUUID0, *char0);
90 
91   EXPECT_FALSE(desc0->IsService());
92   EXPECT_FALSE(desc0->IsCharacteristic());
93   EXPECT_TRUE(desc0->IsDescriptor());
94 
95   EXPECT_TRUE(*desc0->GetOwningCharacteristicId() == *char0);
96   EXPECT_TRUE(*desc0->GetOwningServiceId() == *service0);
97 
98   auto char1 = GattIdentifier::CreateCharacteristicId(kId0, kUUID1, *service0);
99 
100   auto desc1 = GattIdentifier::CreateDescriptorId(kId1, kUUID0, *char0);
101   auto desc2 = GattIdentifier::CreateDescriptorId(kId0, kUUID1, *char0);
102   auto desc3 = GattIdentifier::CreateDescriptorId(kId0, kUUID0, *char1);
103 
104   EXPECT_TRUE(*desc1 != *desc0);
105   EXPECT_TRUE(*desc2 != *desc0);
106   EXPECT_TRUE(*desc3 != *desc0);
107 
108   GattIdentifier desc_copy = *desc0;
109   EXPECT_TRUE(desc_copy == *desc0);
110 
111   EXPECT_TRUE(*service0 != *char0);
112   EXPECT_TRUE(*service0 != *desc0);
113   EXPECT_TRUE(*char0 != *desc0);
114 }
115 
116 }  // namespace
117 }  // namespace bluetooth
118