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