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/test/common/mock_eatt.h"
22 #include "crypto_toolbox/crypto_toolbox.h"
23 #include "stack/gatt/gatt_int.h"
24 
25 using bluetooth::Uuid;
26 
27 tGATT_CB gatt_cb;
28 
29 std::map<std::string, int> mock_function_count_map;
30 bool MOCK_bluetooth_shim_is_gd_acl_enabled_ = false;
31 
add_item_to_list(std::list<tGATT_SRV_LIST_ELEM> & srv_list_info,tGATT_SVC_DB * db,bool is_primary)32 static void add_item_to_list(std::list<tGATT_SRV_LIST_ELEM>& srv_list_info,
33                       tGATT_SVC_DB* db, bool is_primary) {
34   srv_list_info.emplace_back();
35   tGATT_SRV_LIST_ELEM& elem = srv_list_info.back();
36   elem.p_db = db;
37   elem.is_primary = is_primary;
38 }
39 
40 // BT Spec 5.2, Vol 3, Part G, Appendix B
TEST(GattDatabaseTest,matchExampleInBtSpecV52)41 TEST(GattDatabaseTest, matchExampleInBtSpecV52) {
42   tGATT_SVC_DB local_db[4];
43   for (int i=0; i<4; i++) local_db[i] = tGATT_SVC_DB();
44   std::list<tGATT_SRV_LIST_ELEM> srv_list_info;
45 
46   // 0x1800
47   add_item_to_list(srv_list_info, &local_db[0], true);
48   gatts_init_service_db(local_db[0], Uuid::From16Bit(0x1800), true, 0x0001, 5);
49   gatts_add_characteristic(local_db[0],
50     GATT_PERM_READ | GATT_PERM_WRITE,
51     GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE,
52     Uuid::From16Bit(0x2A00));
53   gatts_add_characteristic(local_db[0], GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
54     Uuid::From16Bit(0x2A01));
55   // 0x1801
56   add_item_to_list(srv_list_info, &local_db[1], true);
57   gatts_init_service_db(local_db[1], Uuid::From16Bit(0x1801), true, 0x0006, 8);
58   gatts_add_characteristic(local_db[1], 0, GATT_CHAR_PROP_BIT_INDICATE,
59     Uuid::From16Bit(0x2A05));
60   gatts_add_char_descr(local_db[1], GATT_CHAR_PROP_BIT_READ, Uuid::From16Bit(0x2902));
61   gatts_add_characteristic(local_db[1],
62     GATT_PERM_READ | GATT_PERM_WRITE,
63     GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE,
64     Uuid::From16Bit(0x2B29));
65   gatts_add_characteristic(local_db[1], GATT_PERM_READ, GATT_CHAR_PROP_BIT_READ,
66     Uuid::From16Bit(0x2B2A));
67   // 0x1808
68   add_item_to_list(srv_list_info, &local_db[2], true);
69   gatts_init_service_db(local_db[2], Uuid::From16Bit(0x1808), true, 0x000E, 6);
70   gatts_add_included_service(local_db[2], 0x0014, 0x0016, Uuid::From16Bit(0x180F));
71   gatts_add_characteristic(local_db[2], GATT_PERM_READ,
72     GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_INDICATE | GATT_CHAR_PROP_BIT_EXT_PROP,
73     Uuid::From16Bit(0x2A18));
74   gatts_add_char_descr(local_db[2], 0x0000, Uuid::From16Bit(0x2902));
75   gatts_add_char_ext_prop_descr(local_db[2], 0x0000);
76   // 0x180F
77   add_item_to_list(srv_list_info, &local_db[3], false);
78   gatts_init_service_db(local_db[3], Uuid::From16Bit(0x180F), false, 0x0014, 3);
79   gatts_add_characteristic(local_db[3], GATT_PERM_READ,  GATT_CHAR_PROP_BIT_READ,
80     Uuid::From16Bit(0x2A19));
81 
82   Octet16 expected_hash{0xF1, 0xCA, 0x2D, 0x48, 0xEC, 0xF5, 0x8B, 0xAC,
83                         0x8A, 0x88, 0x30, 0xBB, 0xB9, 0xFB, 0xA9, 0x90};
84   std::reverse(expected_hash.begin(), expected_hash.end());
85 
86   Octet16 result_hash = gatts_calculate_database_hash(&srv_list_info);
87 
88   ASSERT_EQ(result_hash, expected_hash);
89 }
90