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 = 73 packet_stream_.ReceiveCommand(socketpair_fds_[0]); 74 75 const vector<uint8_t> received_payload = command->GetPayload(); 76 77 // Validate the packet by checking that it's the appropriate size and then 78 // checking each byte. 79 EXPECT_EQ(packet.size(), command->GetPacketSize()); 80 EXPECT_EQ(DATA_TYPE_COMMAND, command->GetType()); 81 EXPECT_EQ(opcode, command->GetOpcode()); 82 EXPECT_EQ(static_cast<size_t>(payload_size + 1), command->GetPayloadSize()); 83 EXPECT_EQ(payload_size, received_payload[0]); 84 for (int i = 0; i < payload_size; ++i) 85 EXPECT_EQ(packet[4 + i], received_payload[i + 1]); 86 } 87 88 void CheckedSendEvent(std::unique_ptr<EventPacket> event) { 89 const vector<uint8_t> expected_payload = event->GetPayload(); 90 auto expected_size = event->GetPacketSize(); 91 auto expected_code = event->GetEventCode(); 92 auto expected_payload_size = event->GetPayloadSize(); 93 94 EXPECT_TRUE(packet_stream_.SendEvent(std::move(event), socketpair_fds_[0])); 95 96 // Read the packet sent by |packet_stream_|. 97 uint8_t event_header[2]; 98 read(socketpair_fds_[1], event_header, 2); 99 100 uint8_t return_parameters_size; 101 read(socketpair_fds_[1], &return_parameters_size, 1); 102 103 uint8_t return_parameters[return_parameters_size]; 104 read(socketpair_fds_[1], return_parameters, sizeof(return_parameters)); 105 106 // Validate the packet by checking that it's the 107 // appropriate size and then checking each byte. 108 EXPECT_EQ(expected_size, sizeof(event_header) + return_parameters_size + 1); 109 EXPECT_EQ(DATA_TYPE_EVENT, event_header[0]); 110 EXPECT_EQ(expected_code, event_header[1]); 111 EXPECT_EQ(expected_payload_size, 112 static_cast<size_t>(return_parameters_size) + 1); 113 for (int i = 0; i < return_parameters_size; ++i) 114 EXPECT_EQ(expected_payload[i + 1], return_parameters[i]); 115 } 116 117 protected: 118 PacketStream packet_stream_; 119 120 int socketpair_fds_[2]; 121 122 private: 123 // Workaround because ASSERT cannot be used directly in a constructor 124 void CheckSocketpairInit() { 125 ASSERT_TRUE(socketpair_fds_[0] > 0); 126 ASSERT_TRUE(socketpair_fds_[1] > 0); 127 } 128 }; 129 130 TEST_F(PacketStreamTest, ReceivePacketType) { 131 serial_data_type_t command_type = DATA_TYPE_COMMAND; 132 write(socketpair_fds_[1], &command_type, 1); 133 EXPECT_EQ(command_type, packet_stream_.ReceivePacketType(socketpair_fds_[0])); 134 } 135 136 TEST_F(PacketStreamTest, ReceiveEmptyCommand) { 137 CheckedReceiveCommand("", HCI_RESET); 138 } 139 140 TEST_F(PacketStreamTest, ReceiveSmallCommand) { 141 CheckedReceiveCommand(small_payload, HCI_RESET); 142 } 143 144 TEST_F(PacketStreamTest, ReceiveLargeCommand) { 145 CheckedReceiveCommand(large_payload, HCI_RESET); 146 } 147 148 TEST_F(PacketStreamTest, SendEvent) { 149 const vector<uint8_t> return_parameters = {0}; 150 CheckedSendEvent( 151 EventPacket::CreateCommandCompleteEvent(HCI_RESET, return_parameters)); 152 } 153 154 } // namespace test_vendor_lib 155