2 // Copyright 2015 The Android Open Source Project
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "packet_stream.h"
18 #include "command_packet.h"
19 #include "event_packet.h"
22 #include <gtest/gtest.h>
28 #include "hci/include/hci_hal.h"
29 #include "stack/include/hcidefs.h"
31 #include <sys/socket.h>
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.";
42 namespace test_vendor_lib {
44 class PacketStreamTest : public ::testing::Test {
47 socketpair(AF_LOCAL, SOCK_STREAM, 0, socketpair_fds_);
48 CheckSocketpairInit();
52 close(socketpair_fds_[0]);
53 close(socketpair_fds_[1]);
56 void CheckedReceiveCommand(const char* payload, uint16_t opcode) {
57 uint8_t payload_size = strlen(payload);
58 vector<uint8_t> packet;
60 packet.push_back(DATA_TYPE_COMMAND);
61 packet.push_back(opcode);
62 packet.push_back(opcode >> 8);
63 packet.push_back(payload_size);
65 // Set the packet's payload.
66 for (int i = 0; i < payload_size; ++i) packet.push_back(payload[i]);
68 // Send the packet to |packet_stream_|.
69 write(socketpair_fds_[1], &packet[1], packet.size());
71 // Read the command packet.
72 std::unique_ptr<CommandPacket> command = packet_stream_.ReceiveCommand(socketpair_fds_[0]);
74 const vector<uint8_t> received_payload = command->GetPayload();
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]);
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();
92 EXPECT_TRUE(packet_stream_.SendEvent(std::move(event), socketpair_fds_[0]));
94 // Read the packet sent by |packet_stream_|.
95 uint8_t event_header[2];
96 read(socketpair_fds_[1], event_header, 2);
98 uint8_t return_parameters_size;
99 read(socketpair_fds_[1], &return_parameters_size, 1);
101 uint8_t return_parameters[return_parameters_size];
102 read(socketpair_fds_[1], return_parameters, sizeof(return_parameters));
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]);
114 PacketStream packet_stream_;
116 int socketpair_fds_[2];
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);
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]));
132 TEST_F(PacketStreamTest, ReceiveEmptyCommand) {
133 CheckedReceiveCommand("", HCI_RESET);
136 TEST_F(PacketStreamTest, ReceiveSmallCommand) {
137 CheckedReceiveCommand(small_payload, HCI_RESET);
140 TEST_F(PacketStreamTest, ReceiveLargeCommand) {
141 CheckedReceiveCommand(large_payload, HCI_RESET);
144 TEST_F(PacketStreamTest, SendEvent) {
145 const vector<uint8_t> return_parameters = {0};
146 CheckedSendEvent(EventPacket::CreateCommandCompleteEvent(HCI_RESET, return_parameters));
149 } // namespace test_vendor_lib