1 /******************************************************************************
2 *
3 * Copyright 2020 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #include <gtest/gtest.h>
20
21 #include "stack/gatt/gatt_int.h"
22 #include "types/bluetooth/uuid.h"
23
24 using bluetooth::Uuid;
25
26 tGATT_CB gatt_cb;
27
add_item_to_list(std::list<tGATT_SRV_LIST_ELEM> & srv_list_info,tGATT_SVC_DB * db,bool is_primary)28 static void add_item_to_list(std::list<tGATT_SRV_LIST_ELEM>& srv_list_info,
29 tGATT_SVC_DB* db, bool is_primary) {
30 srv_list_info.emplace_back();
31 tGATT_SRV_LIST_ELEM& elem = srv_list_info.back();
32 elem.p_db = db;
33 elem.is_primary = is_primary;
34 }
35
36 // BT Spec 5.2, Vol 3, Part G, Appendix B
TEST(GattDatabaseTest,matchExampleInBtSpecV52)37 TEST(GattDatabaseTest, matchExampleInBtSpecV52) {
38 tGATT_SVC_DB local_db[4];
39 for (int i=0; i<4; i++) local_db[i] = tGATT_SVC_DB();
40 std::list<tGATT_SRV_LIST_ELEM> srv_list_info;
41
42 // 0x1800
43 add_item_to_list(srv_list_info, &local_db[0], true);
44 gatts_init_service_db(local_db[0], Uuid::From16Bit(0x1800), true, 0x0001, 5);
45 gatts_add_characteristic(local_db[0],
46 GATT_PERM_READ | GATT_PERM_WRITE,
47 GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE,
48 Uuid::From16Bit(0x2A00));
49 gatts_add_characteristic(local_db[0], GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
50 Uuid::From16Bit(0x2A01));
51 // 0x1801
52 add_item_to_list(srv_list_info, &local_db[1], true);
53 gatts_init_service_db(local_db[1], Uuid::From16Bit(0x1801), true, 0x0006, 8);
54 gatts_add_characteristic(local_db[1], 0, GATT_CHAR_PROP_BIT_INDICATE,
55 Uuid::From16Bit(0x2A05));
56 gatts_add_char_descr(local_db[1], GATT_CHAR_PROP_BIT_READ, Uuid::From16Bit(0x2902));
57 gatts_add_characteristic(local_db[1],
58 GATT_PERM_READ | GATT_PERM_WRITE,
59 GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE,
60 Uuid::From16Bit(0x2B29));
61 gatts_add_characteristic(local_db[1], GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
62 Uuid::From16Bit(0x2B2A));
63 // 0x1808
64 add_item_to_list(srv_list_info, &local_db[2], true);
65 gatts_init_service_db(local_db[2], Uuid::From16Bit(0x1808), true, 0x000E, 6);
66 gatts_add_included_service(local_db[2], 0x0014, 0x0016, Uuid::From16Bit(0x180F));
67 gatts_add_characteristic(local_db[2], GATT_PERM_READ,
68 GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_INDICATE | GATT_CHAR_PROP_BIT_EXT_PROP,
69 Uuid::From16Bit(0x2A18));
70 gatts_add_char_descr(local_db[2], 0x0000, Uuid::From16Bit(0x2902));
71 gatts_add_char_ext_prop_descr(local_db[2], 0x0000);
72 // 0x180F
73 add_item_to_list(srv_list_info, &local_db[3], false);
74 gatts_init_service_db(local_db[3], Uuid::From16Bit(0x180F), false, 0x0014, 3);
75 gatts_add_characteristic(local_db[3], GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
76 Uuid::From16Bit(0x2A19));
77
78 Octet16 expected_hash{0xF1, 0xCA, 0x2D, 0x48, 0xEC, 0xF5, 0x8B, 0xAC,
79 0x8A, 0x88, 0x30, 0xBB, 0xB9, 0xFB, 0xA9, 0x90};
80 std::reverse(expected_hash.begin(), expected_hash.end());
81
82 Octet16 result_hash = gatts_calculate_database_hash(&srv_list_info);
83
84 ASSERT_EQ(result_hash, expected_hash);
85 }
86