OSDN Git Service

RootCanal: New Directory Structure
[android-x86/system-bt.git] / vendor_libs / test_vendor_lib / test / packet_stream_unittest.cc
1 //
2 // Copyright 2015 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 "packet_stream.h"
18 #include "command_packet.h"
19 #include "event_packet.h"
20 #include "packet.h"
21
22 #include <gtest/gtest.h>
23 #include <cstdint>
24 #include <memory>
25 #include <vector>
26 using std::vector;
27
28 #include "hci/include/hci_hal.h"
29 #include "stack/include/hcidefs.h"
30
31 #include <sys/socket.h>
32
33 namespace {
34 const char small_payload[] = "foo bar baz";
35 const char large_payload[] =
36     "Aristotle's principles will then be no more principles to him, than those "
37     "of Epicurus and the Stoics: let this diversity of opinions be propounded "
38     "to, and laid before him; he will himself choose, if he be able; if not, "
39     "he will remain in doubt.";
40 }  // namespace
41
42 namespace test_vendor_lib {
43
44 class PacketStreamTest : public ::testing::Test {
45  public:
46   PacketStreamTest() {
47     socketpair(AF_LOCAL, SOCK_STREAM, 0, socketpair_fds_);
48     CheckSocketpairInit();
49   }
50
51   ~PacketStreamTest() {
52     close(socketpair_fds_[0]);
53     close(socketpair_fds_[1]);
54   }
55
56   void CheckedReceiveCommand(const char* payload, uint16_t opcode) {
57     uint8_t payload_size = strlen(payload);
58     vector<uint8_t> packet;
59
60     packet.push_back(DATA_TYPE_COMMAND);
61     packet.push_back(opcode);
62     packet.push_back(opcode >> 8);
63     packet.push_back(payload_size);
64
65     // Set the packet's payload.
66     for (int i = 0; i < payload_size; ++i) packet.push_back(payload[i]);
67
68     // Send the packet to |packet_stream_|.
69     write(socketpair_fds_[1], &packet[1], packet.size());
70
71     // Read the command packet.
72     std::unique_ptr<CommandPacket> command = packet_stream_.ReceiveCommand(socketpair_fds_[0]);
73
74     const vector<uint8_t> received_payload = command->GetPayload();
75
76     // Validate the packet by checking that it's the appropriate size and then
77     // checking each byte.
78     EXPECT_EQ(packet.size(), command->GetPacketSize());
79     EXPECT_EQ(DATA_TYPE_COMMAND, command->GetType());
80     EXPECT_EQ(opcode, command->GetOpcode());
81     EXPECT_EQ(static_cast<size_t>(payload_size + 1), command->GetPayloadSize());
82     EXPECT_EQ(payload_size, received_payload[0]);
83     for (int i = 0; i < payload_size; ++i) EXPECT_EQ(packet[4 + i], received_payload[i + 1]);
84   }
85
86   void CheckedSendEvent(std::unique_ptr<EventPacket> event) {
87     const vector<uint8_t> expected_payload = event->GetPayload();
88     auto expected_size = event->GetPacketSize();
89     auto expected_code = event->GetEventCode();
90     auto expected_payload_size = event->GetPayloadSize();
91
92     EXPECT_TRUE(packet_stream_.SendEvent(std::move(event), socketpair_fds_[0]));
93
94     // Read the packet sent by |packet_stream_|.
95     uint8_t event_header[2];
96     read(socketpair_fds_[1], event_header, 2);
97
98     uint8_t return_parameters_size;
99     read(socketpair_fds_[1], &return_parameters_size, 1);
100
101     uint8_t return_parameters[return_parameters_size];
102     read(socketpair_fds_[1], return_parameters, sizeof(return_parameters));
103
104     // Validate the packet by checking that it's the
105     // appropriate size and then checking each byte.
106     EXPECT_EQ(expected_size, sizeof(event_header) + return_parameters_size + 1);
107     EXPECT_EQ(DATA_TYPE_EVENT, event_header[0]);
108     EXPECT_EQ(expected_code, event_header[1]);
109     EXPECT_EQ(expected_payload_size, static_cast<size_t>(return_parameters_size) + 1);
110     for (int i = 0; i < return_parameters_size; ++i) EXPECT_EQ(expected_payload[i + 1], return_parameters[i]);
111   }
112
113  protected:
114   PacketStream packet_stream_;
115
116   int socketpair_fds_[2];
117
118  private:
119   // Workaround because ASSERT cannot be used directly in a constructor
120   void CheckSocketpairInit() {
121     ASSERT_TRUE(socketpair_fds_[0] > 0);
122     ASSERT_TRUE(socketpair_fds_[1] > 0);
123   }
124 };
125
126 TEST_F(PacketStreamTest, ReceivePacketType) {
127   serial_data_type_t command_type = DATA_TYPE_COMMAND;
128   write(socketpair_fds_[1], &command_type, 1);
129   EXPECT_EQ(command_type, packet_stream_.ReceivePacketType(socketpair_fds_[0]));
130 }
131
132 TEST_F(PacketStreamTest, ReceiveEmptyCommand) {
133   CheckedReceiveCommand("", HCI_RESET);
134 }
135
136 TEST_F(PacketStreamTest, ReceiveSmallCommand) {
137   CheckedReceiveCommand(small_payload, HCI_RESET);
138 }
139
140 TEST_F(PacketStreamTest, ReceiveLargeCommand) {
141   CheckedReceiveCommand(large_payload, HCI_RESET);
142 }
143
144 TEST_F(PacketStreamTest, SendEvent) {
145   const vector<uint8_t> return_parameters = {0};
146   CheckedSendEvent(EventPacket::CreateCommandCompleteEvent(HCI_RESET, return_parameters));
147 }
148
149 }  // namespace test_vendor_lib