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 "vendor_libs/test_vendor_lib/include/packet_stream.h"
18 #include "vendor_libs/test_vendor_lib/include/command_packet.h"
19 #include "vendor_libs/test_vendor_lib/include/event_packet.h"
20 #include "vendor_libs/test_vendor_lib/include/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>
35 const char small_payload[] = "foo bar baz";
36 const char large_payload[] =
37 "Aristotle's principles will then be no more principles to him, than those "
38 "of Epicurus and the Stoics: let this diversity of opinions be propounded "
39 "to, and laid before him; he will himself choose, if he be able; if not, "
40 "he will remain in doubt.";
43 namespace test_vendor_lib {
45 class PacketStreamTest : public ::testing::Test {
48 socketpair(AF_LOCAL, SOCK_STREAM, 0, socketpair_fds_);
49 CheckSocketpairInit();
53 close(socketpair_fds_[0]);
54 close(socketpair_fds_[1]);
57 void CheckedReceiveCommand(const char* payload, uint16_t opcode) {
58 uint8_t payload_size = strlen(payload);
59 std::vector<uint8_t> packet;
61 packet.push_back(DATA_TYPE_COMMAND);
62 packet.push_back(opcode);
63 packet.push_back(opcode >> 8);
64 packet.push_back(payload_size);
66 // Set the packet's payload.
67 for (int i = 0; i < payload_size; ++i)
68 packet.push_back(payload[i]);
70 // Send the packet to |packet_stream_|.
71 write(socketpair_fds_[1], &packet[1], packet.size());
73 // Read the command packet.
74 std::unique_ptr<CommandPacket> command =
75 packet_stream_.ReceiveCommand(socketpair_fds_[0]);
77 const std::vector<uint8_t> received_payload = command->GetPayload();
79 // Validate the packet by checking that it's the appropriate size and then
80 // checking each byte.
81 EXPECT_EQ(packet.size(), command->GetPacketSize());
82 EXPECT_EQ(DATA_TYPE_COMMAND, command->GetType());
83 EXPECT_EQ(opcode, command->GetOpcode());
84 EXPECT_EQ(payload_size, command->GetPayloadSize());
85 for (int i = 0; i < payload_size; ++i)
86 EXPECT_EQ(packet[4 + i], received_payload[i]);
89 void CheckedSendEvent(std::unique_ptr<EventPacket> event) {
90 EXPECT_TRUE(packet_stream_.SendEvent(*(event.get()), socketpair_fds_[0]));
92 // Read the packet sent by |packet_stream_|.
93 uint8_t event_header[3];
94 read(socketpair_fds_[1], event_header, 3);
96 uint8_t return_parameters_size = event_header[2];
97 uint8_t return_parameters[return_parameters_size];
98 read(socketpair_fds_[1], return_parameters, sizeof(return_parameters));
100 const std::vector<uint8_t> expected_payload = event->GetPayload();
102 // Validate the packet by checking that it's the
103 // appropriate size and then checking each byte.
104 EXPECT_EQ(event->GetPacketSize(),
105 sizeof(event_header) + sizeof(return_parameters));
106 EXPECT_EQ(DATA_TYPE_EVENT, event_header[0]);
107 EXPECT_EQ(event->GetEventCode(), event_header[1]);
108 EXPECT_EQ(event->GetPayloadSize(), return_parameters_size);
109 for (int i = 0; i < return_parameters_size; ++i)
110 EXPECT_EQ(expected_payload[i], 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);
127 TEST_F(PacketStreamTest, ReceivePacketType) {
128 serial_data_type_t command_type = DATA_TYPE_COMMAND;
129 write(socketpair_fds_[1], &command_type, 1);
130 EXPECT_EQ(command_type, packet_stream_.ReceivePacketType(socketpair_fds_[0]));
133 TEST_F(PacketStreamTest, ReceiveEmptyCommand) {
134 CheckedReceiveCommand("", HCI_RESET);
137 TEST_F(PacketStreamTest, ReceiveSmallCommand) {
138 CheckedReceiveCommand(small_payload, HCI_RESET);
141 TEST_F(PacketStreamTest, ReceiveLargeCommand) {
142 CheckedReceiveCommand(large_payload, HCI_RESET);
145 TEST_F(PacketStreamTest, SendEvent) {
146 const std::vector<uint8_t> return_parameters = {0};
148 EventPacket::CreateCommandCompleteEvent(1, HCI_RESET, return_parameters));
151 } // namespace test_vendor_lib