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 #include <cstring>
20 #include <map>
21
22 #include "osi/include/log.h"
23 #include "stack/include/hcidefs.h"
24 #include "stack/include/l2cdefs.h"
25 #include "test/mock/mock_hcic_hcicmds.h"
26
27 std::map<std::string, int> mock_function_count_map;
28
29 namespace mock = test::mock::hcic_hcicmds;
30
31 namespace {
32
33 using testing::_;
34 using testing::DoAll;
35 using testing::NotNull;
36 using testing::Pointee;
37 using testing::Return;
38 using testing::SaveArg;
39 using testing::SaveArgPointee;
40 using testing::StrEq;
41 using testing::StrictMock;
42 using testing::Test;
43
44 class StackHciTest : public Test {
45 public:
46 protected:
SetUp()47 void SetUp() override { mock_function_count_map.clear(); }
TearDown()48 void TearDown() override {}
49 };
50
TEST_F(StackHciTest,hci_preamble)51 TEST_F(StackHciTest, hci_preamble) {
52 {
53 HciDataPreamble preamble;
54
55 ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
56
57 preamble.bits.handle = 0xfff;
58 preamble.bits.boundary = 0x3;
59 preamble.bits.broadcast = 0x1;
60 preamble.bits.unused15 = 0x0;
61 preamble.bits.length = 0xffff;
62
63 ASSERT_EQ(0x7fff, preamble.raw.word0);
64 ASSERT_EQ(0xffff, preamble.raw.word1);
65
66 const uint8_t exp[] = {0xff, 0x7f, 0xff, 0xff};
67 uint8_t act[sizeof(preamble)];
68 preamble.Serialize(act);
69 ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
70 }
71
72 {
73 HciDataPreamble preamble;
74 preamble.raw.word0 =
75 0x123 | (L2CAP_PKT_START_NON_FLUSHABLE << L2CAP_PKT_TYPE_SHIFT);
76 preamble.raw.word1 = 0x4567;
77
78 ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
79
80 ASSERT_EQ(0x0123, preamble.raw.word0);
81 ASSERT_EQ(0x4567, preamble.raw.word1);
82
83 const uint8_t exp[] = {0x23, 0x01, 0x67, 0x45};
84 uint8_t act[sizeof(preamble)];
85 preamble.Serialize(act);
86 ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
87 }
88 {
89 HciDataPreamble preamble;
90 preamble.raw.word0 = 0x123 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
91 preamble.raw.word1 = 0x4567;
92
93 ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
94
95 ASSERT_EQ(0x2123, preamble.raw.word0);
96 ASSERT_EQ(0x4567, preamble.raw.word1);
97
98 const uint8_t exp[] = {0x23, 0x21, 0x67, 0x45};
99 uint8_t act[sizeof(preamble)];
100 preamble.Serialize(act);
101 ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
102 }
103
104 {
105 HciDataPreamble preamble;
106 preamble.raw.word0 = 0x0 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
107 preamble.raw.word1 = 0x0;
108
109 ASSERT_EQ(sizeof(preamble), HCI_DATA_PREAMBLE_SIZE);
110
111 ASSERT_EQ(0x2000, preamble.raw.word0);
112 ASSERT_EQ(0x0000, preamble.raw.word1);
113
114 const uint8_t exp[] = {0x00, 0x20, 0x00, 0x00};
115 uint8_t act[sizeof(preamble)];
116 preamble.Serialize(act);
117 ASSERT_EQ(0, std::memcmp(exp, act, sizeof(preamble)));
118 }
119
120 {
121 HciDataPreamble preamble;
122 preamble.raw.word0 = 0x0 | (L2CAP_PKT_START << L2CAP_PKT_TYPE_SHIFT);
123 preamble.raw.word1 = 0x0;
124
125 ASSERT_TRUE(preamble.IsFlushable());
126
127 preamble.raw.word0 =
128 0x0 | (L2CAP_PKT_START << L2CAP_PKT_START_NON_FLUSHABLE);
129 ASSERT_TRUE(!preamble.IsFlushable());
130 }
131 }
132
133 } // namespace
134