bluetooth_gatt_application_service_provider_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Copyright 2016 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.
  4. #include <string>
  5. #include <vector>
  6. #include "base/bind.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "dbus/bus.h"
  9. #include "dbus/message.h"
  10. #include "dbus/object_path.h"
  11. #include "device/bluetooth/dbus/bluetooth_gatt_application_service_provider.h"
  12. #include "device/bluetooth/dbus/bluetooth_gatt_application_service_provider_impl.h"
  13. #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider_impl.h"
  14. #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_service_provider_impl.h"
  15. #include "device/bluetooth/dbus/bluetooth_gatt_service_service_provider_impl.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. namespace bluez {
  18. namespace {
  19. const char kAppObjectPath[] = "/fake/hci0/gatt_application";
  20. const char kFakeServiceUuid[] = "00000000-0000-0000-0000-010040008000";
  21. const char kFakeCharacteristicUuid[] = "00000000-0000-0000-0000-010040908000";
  22. const char kFakeDescriptorUuid[] = "00000000-0000-0000-0000-018390008000";
  23. // This is really ugly, but it really is the best way to verify our message
  24. // was constructed correctly. This string was generated from the test data
  25. // and then manually verified to match the expected signature.
  26. const char kExpectedMessage[] =
  27. "message_type: MESSAGE_METHOD_RETURN\n"
  28. "signature: a{oa{sa{sv}}}\n"
  29. "reply_serial: 123\n"
  30. "\n"
  31. "array [\n"
  32. " dict entry {\n"
  33. " object_path \"/fake/hci0/gatt_application/service0\"\n"
  34. " array [\n"
  35. " dict entry {\n"
  36. " string \"org.bluez.GattService1\"\n"
  37. " array [\n"
  38. " dict entry {\n"
  39. " string \"UUID\"\n"
  40. " variant string "
  41. "\"00000000-0000-0000-0000-010040008000\"\n"
  42. " }\n"
  43. " dict entry {\n"
  44. " string \"Primary\"\n"
  45. " variant bool true\n"
  46. " }\n"
  47. " dict entry {\n"
  48. " string \"Includes\"\n"
  49. " variant array [\n"
  50. " ]\n"
  51. " }\n"
  52. " ]\n"
  53. " }\n"
  54. " ]\n"
  55. " }\n"
  56. " dict entry {\n"
  57. " object_path \"/fake/hci0/gatt_application/service1\"\n"
  58. " array [\n"
  59. " dict entry {\n"
  60. " string \"org.bluez.GattService1\"\n"
  61. " array [\n"
  62. " dict entry {\n"
  63. " string \"UUID\"\n"
  64. " variant string "
  65. "\"00000000-0000-0000-0000-010040008000\"\n"
  66. " }\n"
  67. " dict entry {\n"
  68. " string \"Primary\"\n"
  69. " variant bool true\n"
  70. " }\n"
  71. " dict entry {\n"
  72. " string \"Includes\"\n"
  73. " variant array [\n"
  74. " ]\n"
  75. " }\n"
  76. " ]\n"
  77. " }\n"
  78. " ]\n"
  79. " }\n"
  80. " dict entry {\n"
  81. " object_path \"/fake/hci0/gatt_application/service0/characteristic0\"\n"
  82. " array [\n"
  83. " dict entry {\n"
  84. " string \"org.bluez.GattCharacteristic1\"\n"
  85. " array [\n"
  86. " dict entry {\n"
  87. " string \"UUID\"\n"
  88. " variant string "
  89. "\"00000000-0000-0000-0000-010040908000\"\n"
  90. " }\n"
  91. " dict entry {\n"
  92. " string \"Service\"\n"
  93. " variant object_path "
  94. "\"/fake/hci0/gatt_application/service0\"\n"
  95. " }\n"
  96. " dict entry {\n"
  97. " string \"Flags\"\n"
  98. " variant array [\n"
  99. " string \"read\"\n"
  100. " string \"write\"\n"
  101. " ]\n"
  102. " }\n"
  103. " ]\n"
  104. " }\n"
  105. " ]\n"
  106. " }\n"
  107. " dict entry {\n"
  108. " object_path \"/fake/hci0/gatt_application/service0/characteristic1\"\n"
  109. " array [\n"
  110. " dict entry {\n"
  111. " string \"org.bluez.GattCharacteristic1\"\n"
  112. " array [\n"
  113. " dict entry {\n"
  114. " string \"UUID\"\n"
  115. " variant string "
  116. "\"00000000-0000-0000-0000-010040908000\"\n"
  117. " }\n"
  118. " dict entry {\n"
  119. " string \"Service\"\n"
  120. " variant object_path "
  121. "\"/fake/hci0/gatt_application/service0\"\n"
  122. " }\n"
  123. " dict entry {\n"
  124. " string \"Flags\"\n"
  125. " variant array [\n"
  126. " string \"read\"\n"
  127. " string \"write\"\n"
  128. " ]\n"
  129. " }\n"
  130. " ]\n"
  131. " }\n"
  132. " ]\n"
  133. " }\n"
  134. " dict entry {\n"
  135. " object_path \"/fake/hci0/gatt_application/service1/characteristic0\"\n"
  136. " array [\n"
  137. " dict entry {\n"
  138. " string \"org.bluez.GattCharacteristic1\"\n"
  139. " array [\n"
  140. " dict entry {\n"
  141. " string \"UUID\"\n"
  142. " variant string "
  143. "\"00000000-0000-0000-0000-010040908000\"\n"
  144. " }\n"
  145. " dict entry {\n"
  146. " string \"Service\"\n"
  147. " variant object_path "
  148. "\"/fake/hci0/gatt_application/service1\"\n"
  149. " }\n"
  150. " dict entry {\n"
  151. " string \"Flags\"\n"
  152. " variant array [\n"
  153. " string \"read\"\n"
  154. " string \"write\"\n"
  155. " ]\n"
  156. " }\n"
  157. " ]\n"
  158. " }\n"
  159. " ]\n"
  160. " }\n"
  161. " dict entry {\n"
  162. " object_path "
  163. "\"/fake/hci0/gatt_application/service0/characteristic0/descriptor0\"\n"
  164. " array [\n"
  165. " dict entry {\n"
  166. " string \"org.bluez.GattDescriptor1\"\n"
  167. " array [\n"
  168. " dict entry {\n"
  169. " string \"UUID\"\n"
  170. " variant string "
  171. "\"00000000-0000-0000-0000-018390008000\"\n"
  172. " }\n"
  173. " dict entry {\n"
  174. " string \"Characteristic\"\n"
  175. " variant object_path "
  176. "\"/fake/hci0/gatt_application/service0/characteristic0\"\n"
  177. " }\n"
  178. " dict entry {\n"
  179. " string \"Flags\"\n"
  180. " variant array [\n"
  181. " string \"read\"\n"
  182. " string \"write\"\n"
  183. " ]\n"
  184. " }\n"
  185. " ]\n"
  186. " }\n"
  187. " ]\n"
  188. " }\n"
  189. " dict entry {\n"
  190. " object_path "
  191. "\"/fake/hci0/gatt_application/service0/characteristic1/descriptor1\"\n"
  192. " array [\n"
  193. " dict entry {\n"
  194. " string \"org.bluez.GattDescriptor1\"\n"
  195. " array [\n"
  196. " dict entry {\n"
  197. " string \"UUID\"\n"
  198. " variant string "
  199. "\"00000000-0000-0000-0000-018390008000\"\n"
  200. " }\n"
  201. " dict entry {\n"
  202. " string \"Characteristic\"\n"
  203. " variant object_path "
  204. "\"/fake/hci0/gatt_application/service0/characteristic1\"\n"
  205. " }\n"
  206. " dict entry {\n"
  207. " string \"Flags\"\n"
  208. " variant array [\n"
  209. " string \"read\"\n"
  210. " string \"write\"\n"
  211. " ]\n"
  212. " }\n"
  213. " ]\n"
  214. " }\n"
  215. " ]\n"
  216. " }\n"
  217. " dict entry {\n"
  218. " object_path "
  219. "\"/fake/hci0/gatt_application/service1/characteristic0/descriptor2\"\n"
  220. " array [\n"
  221. " dict entry {\n"
  222. " string \"org.bluez.GattDescriptor1\"\n"
  223. " array [\n"
  224. " dict entry {\n"
  225. " string \"UUID\"\n"
  226. " variant string "
  227. "\"00000000-0000-0000-0000-018390008000\"\n"
  228. " }\n"
  229. " dict entry {\n"
  230. " string \"Characteristic\"\n"
  231. " variant object_path "
  232. "\"/fake/hci0/gatt_application/service1/characteristic0\"\n"
  233. " }\n"
  234. " dict entry {\n"
  235. " string \"Flags\"\n"
  236. " variant array [\n"
  237. " string \"read\"\n"
  238. " string \"write\"\n"
  239. " ]\n"
  240. " }\n"
  241. " ]\n"
  242. " }\n"
  243. " ]\n"
  244. " }\n"
  245. " dict entry {\n"
  246. " object_path "
  247. "\"/fake/hci0/gatt_application/service0/characteristic0/descriptor3\"\n"
  248. " array [\n"
  249. " dict entry {\n"
  250. " string \"org.bluez.GattDescriptor1\"\n"
  251. " array [\n"
  252. " dict entry {\n"
  253. " string \"UUID\"\n"
  254. " variant string "
  255. "\"00000000-0000-0000-0000-018390008000\"\n"
  256. " }\n"
  257. " dict entry {\n"
  258. " string \"Characteristic\"\n"
  259. " variant object_path "
  260. "\"/fake/hci0/gatt_application/service0/characteristic0\"\n"
  261. " }\n"
  262. " dict entry {\n"
  263. " string \"Flags\"\n"
  264. " variant array [\n"
  265. " string \"read\"\n"
  266. " string \"write\"\n"
  267. " ]\n"
  268. " }\n"
  269. " ]\n"
  270. " }\n"
  271. " ]\n"
  272. " }\n"
  273. "]\n";
  274. void ResponseSenderCallback(const std::string& expected_message,
  275. std::unique_ptr<dbus::Response> response) {
  276. EXPECT_EQ(expected_message, response->ToString());
  277. }
  278. } // namespace
  279. class BluetoothGattApplicationServiceProviderTest : public testing::Test {
  280. public:
  281. std::string CreateFakeService(
  282. BluetoothGattApplicationServiceProviderImpl* app_provider,
  283. const std::string& service_path) {
  284. const std::string& full_service_path =
  285. std::string(kAppObjectPath) + "/" + service_path;
  286. app_provider->service_providers_.push_back(
  287. std::make_unique<BluetoothGattServiceServiceProviderImpl>(
  288. nullptr, dbus::ObjectPath(full_service_path), kFakeServiceUuid,
  289. true, std::vector<dbus::ObjectPath>()));
  290. return full_service_path;
  291. }
  292. std::string CreateFakeCharacteristic(
  293. BluetoothGattApplicationServiceProviderImpl* app_provider,
  294. const std::string& characteristic_path,
  295. const std::string& service_path) {
  296. const std::string& full_characteristic_path =
  297. service_path + "/" + characteristic_path;
  298. app_provider->characteristic_providers_.push_back(
  299. base::WrapUnique(new BluetoothGattCharacteristicServiceProviderImpl(
  300. nullptr, dbus::ObjectPath(full_characteristic_path), nullptr,
  301. kFakeCharacteristicUuid,
  302. std::vector<std::string>({"read", "write"}),
  303. dbus::ObjectPath(service_path))));
  304. return full_characteristic_path;
  305. }
  306. void CreateFakeDescriptor(
  307. BluetoothGattApplicationServiceProviderImpl* app_provider,
  308. const std::string& descriptor_path,
  309. const std::string& characteristic_path) {
  310. const std::string& full_descriptor_path =
  311. characteristic_path + "/" + descriptor_path;
  312. app_provider->descriptor_providers_.push_back(
  313. base::WrapUnique(new BluetoothGattDescriptorServiceProviderImpl(
  314. nullptr, dbus::ObjectPath(full_descriptor_path), nullptr,
  315. kFakeDescriptorUuid, std::vector<std::string>({"read", "write"}),
  316. dbus::ObjectPath(characteristic_path))));
  317. }
  318. void CreateFakeAttributes(
  319. BluetoothGattApplicationServiceProviderImpl* app_provider) {
  320. const std::string& kServicePath1 =
  321. CreateFakeService(app_provider, "service0");
  322. const std::string& kServicePath2 =
  323. CreateFakeService(app_provider, "service1");
  324. const std::string& kCharacteristicPath1 = CreateFakeCharacteristic(
  325. app_provider, "characteristic0", kServicePath1);
  326. const std::string& kCharacteristicPath2 = CreateFakeCharacteristic(
  327. app_provider, "characteristic1", kServicePath1);
  328. const std::string& kCharacteristicPath3 = CreateFakeCharacteristic(
  329. app_provider, "characteristic0", kServicePath2);
  330. CreateFakeDescriptor(app_provider, "descriptor0", kCharacteristicPath1);
  331. CreateFakeDescriptor(app_provider, "descriptor1", kCharacteristicPath2);
  332. CreateFakeDescriptor(app_provider, "descriptor2", kCharacteristicPath3);
  333. CreateFakeDescriptor(app_provider, "descriptor3", kCharacteristicPath1);
  334. }
  335. };
  336. TEST_F(BluetoothGattApplicationServiceProviderTest, GetManagedObjects) {
  337. std::unique_ptr<BluetoothGattApplicationServiceProviderImpl> app_provider =
  338. std::make_unique<BluetoothGattApplicationServiceProviderImpl>(
  339. nullptr, dbus::ObjectPath(kAppObjectPath),
  340. std::map<dbus::ObjectPath, BluetoothLocalGattServiceBlueZ*>());
  341. CreateFakeAttributes(app_provider.get());
  342. dbus::MethodCall method_call("com.example.Interface", "SomeMethod");
  343. // Not setting the serial causes a crash.
  344. method_call.SetSerial(123);
  345. app_provider->GetManagedObjects(
  346. &method_call, base::BindOnce(&ResponseSenderCallback, kExpectedMessage));
  347. }
  348. TEST_F(BluetoothGattApplicationServiceProviderTest, SendValueChanged) {
  349. std::unique_ptr<BluetoothGattApplicationServiceProviderImpl> app_provider =
  350. std::make_unique<BluetoothGattApplicationServiceProviderImpl>(
  351. nullptr, dbus::ObjectPath(kAppObjectPath),
  352. std::map<dbus::ObjectPath, BluetoothLocalGattServiceBlueZ*>());
  353. const std::string& kServicePath =
  354. CreateFakeService(app_provider.get(), "service0");
  355. const std::string& kCharacteristicPath = CreateFakeCharacteristic(
  356. app_provider.get(), "characteristic0", kServicePath);
  357. std::vector<uint8_t> kNewValue = {0x13, 0x37, 0xba, 0xad, 0xf0};
  358. app_provider->SendValueChanged(dbus::ObjectPath(kCharacteristicPath),
  359. kNewValue);
  360. // TODO(rkc): Write a test implementation of dbus::Bus and
  361. // dbus::ExportedObject so we can capture the actual signal that is sent and
  362. // verify its contents.
  363. }
  364. } // namespace bluez