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/advertise_data.h"
20 #include "service/common/bluetooth/advertise_settings.h"
21 #include "service/common/bluetooth/binder/parcel_helpers.h"
22
23 using android::Parcel;
24
25 using bluetooth::AdvertiseData;
26 using bluetooth::AdvertiseSettings;
27 using bluetooth::GattIdentifier;
28 using bluetooth::ScanFilter;
29 using bluetooth::ScanResult;
30 using bluetooth::ScanSettings;
31 using bluetooth::UUID;
32
33 namespace ipc {
34 namespace binder {
35 namespace {
36
TestAdvertiseData(const AdvertiseData & adv_in)37 bool TestAdvertiseData(const AdvertiseData &adv_in) {
38 Parcel parcel;
39
40 WriteAdvertiseDataToParcel(adv_in, &parcel);
41 parcel.setDataPosition(0);
42 auto adv_out = CreateAdvertiseDataFromParcel(parcel);
43
44 return adv_in == *adv_out;
45 }
46
TestAdvertiseSettings(const AdvertiseSettings & settings_in)47 bool TestAdvertiseSettings(const AdvertiseSettings &settings_in) {
48 Parcel parcel;
49
50 WriteAdvertiseSettingsToParcel(settings_in, &parcel);
51 parcel.setDataPosition(0);
52 auto settings_out = CreateAdvertiseSettingsFromParcel(parcel);
53
54 return settings_in == *settings_out;
55 }
56
TestUUID(const UUID & uuid_in)57 bool TestUUID(const UUID& uuid_in) {
58 Parcel parcel;
59
60 WriteUUIDToParcel(uuid_in, &parcel);
61 parcel.setDataPosition(0);
62 auto uuid_out = CreateUUIDFromParcel(parcel);
63
64 return uuid_in == *uuid_out;
65 }
66
TestGattIdentifier(const GattIdentifier & id_in)67 bool TestGattIdentifier(const GattIdentifier& id_in) {
68 Parcel parcel;
69
70 WriteGattIdentifierToParcel(id_in, &parcel);
71 parcel.setDataPosition(0);
72 auto id_out = CreateGattIdentifierFromParcel(parcel);
73
74 return id_in == *id_out;
75 }
76
TestScanSettings(const ScanSettings & settings_in)77 bool TestScanSettings(const ScanSettings& settings_in) {
78 Parcel parcel;
79 WriteScanSettingsToParcel(settings_in, &parcel);
80 parcel.setDataPosition(0);
81 auto settings_out = CreateScanSettingsFromParcel(parcel);
82
83 return settings_in == *settings_out;
84 }
85
TestScanFilter(const ScanFilter & filter_in)86 bool TestScanFilter(const ScanFilter& filter_in) {
87 Parcel parcel;
88 WriteScanFilterToParcel(filter_in, &parcel);
89 parcel.setDataPosition(0);
90 auto filter_out = CreateScanFilterFromParcel(parcel);
91
92 return filter_in == *filter_out;
93 }
94
TestScanResult(const ScanResult & result_in)95 bool TestScanResult(const ScanResult& result_in) {
96 Parcel parcel;
97 WriteScanResultToParcel(result_in, &parcel);
98 parcel.setDataPosition(0);
99 auto result_out = CreateScanResultFromParcel(parcel);
100 assert(result_out.get() != nullptr);
101
102 return result_in == *result_out;
103 }
104
TEST(ParcelHelpersTest,EmptyAdvertiseData)105 TEST(ParcelHelpersTest, EmptyAdvertiseData) {
106 std::vector<uint8_t> data;
107 AdvertiseData adv(data);
108
109 EXPECT_TRUE(TestAdvertiseData(adv));
110 }
111
TEST(ParcelHelpersTest,NonEmptyAdvertiseData)112 TEST(ParcelHelpersTest, NonEmptyAdvertiseData) {
113 std::vector<uint8_t> data{ 0x02, 0x02, 0x00 };
114 AdvertiseData adv0(data);
115 adv0.set_include_tx_power_level(true);
116 EXPECT_TRUE(TestAdvertiseData(adv0));
117
118 AdvertiseData adv1(data);
119 adv1.set_include_device_name(true);
120 EXPECT_TRUE(TestAdvertiseData(adv1));
121
122 AdvertiseData adv2(data);
123 adv2.set_include_tx_power_level(true);
124 adv2.set_include_device_name(true);
125 EXPECT_TRUE(TestAdvertiseData(adv2));
126 }
127
TEST(ParcelHelpersTest,DefaultAdvertiseSettings)128 TEST(ParcelHelpersTest, DefaultAdvertiseSettings) {
129 AdvertiseSettings settings;
130 EXPECT_TRUE(TestAdvertiseSettings(settings));
131 }
132
TEST(ParcelHelpersTest,NonEmptyAdvertiseSettings)133 TEST(ParcelHelpersTest, NonEmptyAdvertiseSettings) {
134 AdvertiseSettings settings(
135 AdvertiseSettings::MODE_BALANCED,
136 base::TimeDelta::FromMilliseconds(150),
137 AdvertiseSettings::TX_POWER_LEVEL_HIGH,
138 false /* connectable */);
139 EXPECT_TRUE(TestAdvertiseSettings(settings));
140 }
141
TEST(ParcelHelpersTest,UUID)142 TEST(ParcelHelpersTest, UUID) {
143 // Try a whole bunch of UUIDs.
144 for (int i = 0; i < 10; i++) {
145 UUID uuid = UUID::GetRandom();
146 TestUUID(uuid);
147 }
148 }
149
TEST(ParcelHelpersTest,GattIdentifier)150 TEST(ParcelHelpersTest, GattIdentifier) {
151 UUID uuid0 = UUID::GetRandom();
152 UUID uuid1 = UUID::GetRandom();
153 UUID uuid2 = UUID::GetRandom();
154
155 auto service_id = GattIdentifier::CreateServiceId(
156 "01:23:45:67:89:ab", 5, uuid0, false);
157 auto char_id = GattIdentifier::CreateCharacteristicId(3, uuid1, *service_id);
158 auto desc_id = GattIdentifier::CreateDescriptorId(10, uuid2, *char_id);
159
160 TestGattIdentifier(*service_id);
161 TestGattIdentifier(*char_id);
162 TestGattIdentifier(*desc_id);
163 }
164
TEST(ParcelHelpersTest,ScanSettings)165 TEST(ParcelHelpersTest, ScanSettings) {
166 ScanSettings settings0;
167 ScanSettings settings1(
168 ScanSettings::MODE_BALANCED,
169 ScanSettings::CALLBACK_TYPE_FIRST_MATCH,
170 ScanSettings::RESULT_TYPE_ABBREVIATED,
171 base::TimeDelta::FromMilliseconds(150),
172 ScanSettings::MATCH_MODE_STICKY,
173 ScanSettings::MATCH_COUNT_FEW_ADVERTISEMENTS);
174
175 EXPECT_TRUE(TestScanSettings(settings0));
176 EXPECT_TRUE(TestScanSettings(settings1));
177 }
178
TEST(ParcelHelpersTest,ScanFilter)179 TEST(ParcelHelpersTest, ScanFilter) {
180 ScanFilter filter;
181
182 filter.set_device_name("Test Device Name");
183 ASSERT_TRUE(filter.SetDeviceAddress("01:02:04:AB:CD:EF"));
184 EXPECT_TRUE(TestScanFilter(filter));
185
186 UUID uuid = UUID::GetRandom();
187 filter.SetServiceUuid(uuid);
188 EXPECT_TRUE(TestScanFilter(filter));
189
190 UUID mask = UUID::GetRandom();
191 filter.SetServiceUuidWithMask(uuid, mask);
192 EXPECT_TRUE(TestScanFilter(filter));
193 }
194
TEST(ParcelHelpersTest,ScanResult)195 TEST(ParcelHelpersTest, ScanResult) {
196 const char kTestAddress[] = "01:02:03:AB:CD:EF";
197
198 const std::vector<uint8_t> kEmptyBytes;
199 const std::vector<uint8_t> kTestBytes{ 0x01, 0x02, 0x03 };
200
201 const int kTestRssi = 127;
202
203 ScanResult result0(kTestAddress, kEmptyBytes, kTestRssi);
204 ScanResult result1(kTestAddress, kTestBytes, kTestRssi);
205
206 EXPECT_TRUE(TestScanResult(result0));
207 EXPECT_TRUE(TestScanResult(result1));
208 }
209
210 } // namespace
211 } // namespace binder
212 } // namespace ipc
213