1 /*
2  *  Copyright 2021 The Android Open Source Project
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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include <cstring>
21 #include <utility>
22 
23 #include "stack/include/hcidefs.h"
24 #include "stack/include/l2cdefs.h"
25 #include "test/common/mock_functions.h"
26 
27 using testing::_;
28 using testing::DoAll;
29 using testing::NotNull;
30 using testing::Pointee;
31 using testing::Return;
32 using testing::SaveArg;
33 using testing::SaveArgPointee;
34 using testing::StrEq;
35 using testing::StrictMock;
36 using testing::Test;
37 
38 class StackHciTest : public Test {
39  public:
40  protected:
SetUp()41   void SetUp() override { reset_mock_function_count_map(); }
TearDown()42   void TearDown() override {}
43 };
44 
TEST_F(StackHciTest,hci_preamble)45 TEST_F(StackHciTest, hci_preamble) {
46   {
47     HciDataPreamble preamble;
48 
49     ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
50 
51     preamble.bits.handle = 0xfff;
52     preamble.bits.boundary = 0x3;
53     preamble.bits.broadcast = 0x1;
54     preamble.bits.unused15 = 0x0;
55     preamble.bits.length = 0xffff;
56 
57     ASSERT_EQ(0x7fff, preamble.raw.word0);
58     ASSERT_EQ(0xffff, preamble.raw.word1);
59 
60     const uint8_t exp[] = {0xff, 0x7f, 0xff, 0xff};
61     uint8_t act[sizeof(preamble)];
62     preamble.Serialize(act);
63     ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
64   }
65 
66   {
67     HciDataPreamble preamble;
68     preamble.raw.word0 =
69         0x123 | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT);
70     preamble.raw.word1 = 0x4567;
71 
72     ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
73 
74     ASSERT_EQ(0x0123, preamble.raw.word0);
75     ASSERT_EQ(0x4567, preamble.raw.word1);
76 
77     const uint8_t exp[] = {0x23, 0x01, 0x67, 0x45};
78     uint8_t act[sizeof(preamble)];
79     preamble.Serialize(act);
80     ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
81   }
82   {
83     HciDataPreamble preamble;
84     preamble.raw.word0 = 0x123 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
85     preamble.raw.word1 = 0x4567;
86 
87     ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
88 
89     ASSERT_EQ(0x2123, preamble.raw.word0);
90     ASSERT_EQ(0x4567, preamble.raw.word1);
91 
92     const uint8_t exp[] = {0x23, 0x21, 0x67, 0x45};
93     uint8_t act[sizeof(preamble)];
94     preamble.Serialize(act);
95     ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
96   }
97 
98   {
99     HciDataPreamble preamble;
100     preamble.raw.word0 = 0x0 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
101     preamble.raw.word1 = 0x0;
102 
103     ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
104 
105     ASSERT_EQ(0x2000, preamble.raw.word0);
106     ASSERT_EQ(0x0000, preamble.raw.word1);
107 
108     const uint8_t exp[] = {0x00, 0x20, 0x00, 0x00};
109     uint8_t act[sizeof(preamble)];
110     preamble.Serialize(act);
111     ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
112   }
113 
114   {
115     HciDataPreamble preamble;
116     preamble.raw.word0 = 0x0 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
117     preamble.raw.word1 = 0x0;
118 
119     ASSERT_TRUE(preamble.IsFlushable());
120 
121     preamble.raw.word0 =
122         0x0 | (L2CAP_PKT_START << L2CAP_PKT_START_NON_FLUSHABLE);
123     ASSERT_TRUE(!preamble.IsFlushable());
124   }
125 }
126 
TEST_F(StackHciTest,hci_error_code_text)127 TEST_F(StackHciTest, hci_error_code_text) {
128   std::vector<std::pair<tHCI_ERROR_CODE, std::string>> errors = {
129       std::make_pair(HCI_SUCCESS, "HCI_SUCCESS"),
130       std::make_pair(HCI_ERR_ILLEGAL_COMMAND, "HCI_ERR_ILLEGAL_COMMAND"),
131       std::make_pair(HCI_ERR_NO_CONNECTION, "HCI_ERR_NO_CONNECTION"),
132       std::make_pair(HCI_ERR_HW_FAILURE, "HCI_ERR_HW_FAILURE"),
133       std::make_pair(HCI_ERR_PAGE_TIMEOUT, "HCI_ERR_PAGE_TIMEOUT"),
134       std::make_pair(HCI_ERR_AUTH_FAILURE, "HCI_ERR_AUTH_FAILURE"),
135       std::make_pair(HCI_ERR_KEY_MISSING, "HCI_ERR_KEY_MISSING"),
136       std::make_pair(HCI_ERR_MEMORY_FULL, "HCI_ERR_MEMORY_FULL"),
137       std::make_pair(HCI_ERR_CONNECTION_TOUT, "HCI_ERR_CONNECTION_TOUT"),
138       std::make_pair(HCI_ERR_MAX_NUM_OF_CONNECTIONS,
139                      "HCI_ERR_MAX_NUM_OF_CONNECTIONS"),
140       std::make_pair(HCI_ERR_MAX_NUM_OF_SCOS, "HCI_ERR_MAX_NUM_OF_SCOS"),
141       std::make_pair(HCI_ERR_CONNECTION_EXISTS, "HCI_ERR_CONNECTION_EXISTS"),
142       std::make_pair(HCI_ERR_COMMAND_DISALLOWED, "HCI_ERR_COMMAND_DISALLOWED"),
143       std::make_pair(HCI_ERR_HOST_REJECT_RESOURCES,
144                      "HCI_ERR_HOST_REJECT_RESOURCES"),
145       std::make_pair(HCI_ERR_HOST_REJECT_SECURITY,
146                      "HCI_ERR_HOST_REJECT_SECURITY"),
147       std::make_pair(HCI_ERR_HOST_REJECT_DEVICE, "HCI_ERR_HOST_REJECT_DEVICE"),
148       std::make_pair(HCI_ERR_HOST_TIMEOUT, "HCI_ERR_HOST_TIMEOUT"),
149       std::make_pair(HCI_ERR_ILLEGAL_PARAMETER_FMT,
150                      "HCI_ERR_ILLEGAL_PARAMETER_FMT"),
151       std::make_pair(HCI_ERR_PEER_USER, "HCI_ERR_PEER_USER"),
152       std::make_pair(HCI_ERR_REMOTE_LOW_RESOURCE,
153                      "HCI_ERR_REMOTE_LOW_RESOURCE"),
154       std::make_pair(HCI_ERR_REMOTE_POWER_OFF, "HCI_ERR_REMOTE_POWER_OFF"),
155       std::make_pair(HCI_ERR_CONN_CAUSE_LOCAL_HOST,
156                      "HCI_ERR_CONN_CAUSE_LOCAL_HOST"),
157       std::make_pair(HCI_ERR_REPEATED_ATTEMPTS, "HCI_ERR_REPEATED_ATTEMPTS"),
158       std::make_pair(HCI_ERR_PAIRING_NOT_ALLOWED,
159                      "HCI_ERR_PAIRING_NOT_ALLOWED"),
160       std::make_pair(HCI_ERR_UNSUPPORTED_REM_FEATURE,
161                      "HCI_ERR_UNSUPPORTED_REM_FEATURE"),
162       std::make_pair(HCI_ERR_UNSPECIFIED, "HCI_ERR_UNSPECIFIED"),
163       std::make_pair(HCI_ERR_LMP_RESPONSE_TIMEOUT,
164                      "HCI_ERR_LMP_RESPONSE_TIMEOUT"),
165       std::make_pair(HCI_ERR_LMP_ERR_TRANS_COLLISION,
166                      "HCI_ERR_LMP_ERR_TRANS_COLLISION"),
167       std::make_pair(HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE,
168                      "HCI_ERR_ENCRY_MODE_NOT_ACCEPTABLE"),
169       std::make_pair(HCI_ERR_UNIT_KEY_USED, "HCI_ERR_UNIT_KEY_USED"),
170       std::make_pair(HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED,
171                      "HCI_ERR_PAIRING_WITH_UNIT_KEY_NOT_SUPPORTED"),
172       std::make_pair(HCI_ERR_DIFF_TRANSACTION_COLLISION,
173                      "HCI_ERR_DIFF_TRANSACTION_COLLISION"),
174       std::make_pair(HCI_ERR_INSUFFCIENT_SECURITY,
175                      "HCI_ERR_INSUFFCIENT_SECURITY"),
176       std::make_pair(HCI_ERR_ROLE_SWITCH_PENDING,
177                      "HCI_ERR_ROLE_SWITCH_PENDING"),
178       std::make_pair(HCI_ERR_ROLE_SWITCH_FAILED, "HCI_ERR_ROLE_SWITCH_FAILED"),
179       std::make_pair(HCI_ERR_HOST_BUSY_PAIRING, "HCI_ERR_HOST_BUSY_PAIRING"),
180       std::make_pair(HCI_ERR_UNACCEPT_CONN_INTERVAL,
181                      "HCI_ERR_UNACCEPT_CONN_INTERVAL"),
182       std::make_pair(HCI_ERR_ADVERTISING_TIMEOUT,
183                      "HCI_ERR_ADVERTISING_TIMEOUT"),
184       std::make_pair(HCI_ERR_CONN_FAILED_ESTABLISHMENT,
185                      "HCI_ERR_CONN_FAILED_ESTABLISHMENT"),
186       std::make_pair(HCI_ERR_LIMIT_REACHED, "HCI_ERR_LIMIT_REACHED"),
187   };
188   for (const auto& error : errors) {
189     ASSERT_STREQ(error.second.c_str(),
190                  hci_error_code_text(error.first).c_str());
191   }
192   for (const auto& error : errors) {
193     ASSERT_STREQ(error.second.c_str(),
194                  hci_error_code_text(error.first).c_str());
195   }
196   auto unknown = base::StringPrintf("UNKNOWN[0x%02hx]",
197                                     std::numeric_limits<std::uint8_t>::max());
198   ASSERT_STREQ(
199       unknown.c_str(),
200       hci_error_code_text(static_cast<tHCI_ERROR_CODE>(
201                               std::numeric_limits<std::uint8_t>::max()))
202           .c_str());
203 }
204