Home | History | Annotate | Download | only in rfcomm
      1 /******************************************************************************
      2  *
      3  *  Copyright 2016 Google, Inc.
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 #include "adapter/bluetooth_test.h"
     20 #include "rfcomm/rfcomm_test.h"
     21 
     22 #include <sys/socket.h>
     23 #include <unistd.h>
     24 
     25 namespace {
     26 static const char HANDSHAKE_COMMAND[] = "AT+BRSF=29\r";
     27 }  // namespace
     28 
     29 namespace bttest {
     30 
     31 TEST_F(RFCommTest, RfcommConnectPairedDevice) {
     32   int fd = -1;
     33   int error = 0;
     34   size_t len = 0;
     35 
     36   error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM,
     37                                       &HFP_UUID, 0, &fd, 0, getuid());
     38   EXPECT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: "
     39                                           << error;
     40   EXPECT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd";
     41 
     42   int channel;
     43   sock_connect_signal_t signal;
     44   len = read(fd, &channel, sizeof(channel));
     45   EXPECT_TRUE(len == sizeof(channel))
     46       << "Channel not read from RFCOMM socket. Bytes read: " << len;
     47   len = read(fd, &signal, sizeof(signal));
     48   EXPECT_TRUE(len == sizeof(signal))
     49       << "Connection signal not read from RFCOMM socket. Bytes read: " << len;
     50 
     51   EXPECT_TRUE(signal.bd_addr == bt_remote_bdaddr_)
     52       << "Connected to a different bdaddr than expected.";
     53   EXPECT_TRUE(channel == signal.channel)
     54       << "Inconsistent channels returned: " << channel << " and "
     55       << signal.channel;
     56 
     57   len = write(fd, HANDSHAKE_COMMAND, sizeof(HANDSHAKE_COMMAND));
     58   EXPECT_TRUE(len == sizeof(HANDSHAKE_COMMAND))
     59       << "Unable to send HFP handshake. Bytes written: " << len;
     60 
     61   char response[1024];
     62   len = read(fd, response, sizeof(response));
     63   EXPECT_TRUE(len > 0) << "Read " << len << " bytes";
     64 
     65   close(fd);
     66 }
     67 
     68 TEST_F(RFCommTest, RfcommRepeatedConnectPairedDevice) {
     69   static const int max_iterations = 128;
     70   int channel_fail = 0, signal_fail = 0, handshake_fail = 0, read_fail = 0;
     71 
     72   for (int i = 0; i < max_iterations; ++i) {
     73     int fd = -1;
     74     int error = 0;
     75     size_t len = 0;
     76 
     77     error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM,
     78                                         &HFP_UUID, 0, &fd, 0, getuid());
     79     ASSERT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: "
     80                                             << error;
     81     ASSERT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd";
     82 
     83     int channel;
     84     sock_connect_signal_t signal;
     85     len = read(fd, &channel, sizeof(channel));
     86     if (len != sizeof(channel)) {
     87       ADD_FAILURE() << "Channel not read from RFCOMM socket. Bytes read: "
     88                     << len << ", Sizeof channel: " << sizeof(channel);
     89       channel_fail++;
     90     }
     91 
     92     len = read(fd, &signal, sizeof(signal));
     93     if (len != sizeof(signal)) {
     94       ADD_FAILURE()
     95           << "Connection signal not read from RFCOMM socket. Bytes read: "
     96           << len;
     97       signal_fail++;
     98     }
     99 
    100     EXPECT_TRUE(signal.bd_addr == bt_remote_bdaddr_)
    101         << "Connected to a different bdaddr than expected.";
    102     EXPECT_TRUE(channel == signal.channel)
    103         << "Inconsistent channels returned: " << channel << " and "
    104         << signal.channel;
    105     len = write(fd, HANDSHAKE_COMMAND, sizeof(HANDSHAKE_COMMAND));
    106     if (len != sizeof(HANDSHAKE_COMMAND)) {
    107       ADD_FAILURE() << "Unable to send HFP handshake. Bytes written: " << len;
    108       handshake_fail++;
    109     }
    110 
    111     char response[1024];
    112     len = read(fd, response, sizeof(response));
    113     if (len <= 0) {
    114       ADD_FAILURE() << "Read " << len << " bytes";
    115       read_fail++;
    116     }
    117 
    118     close(fd);
    119   }
    120 
    121   if (channel_fail > 0 || signal_fail > 0 || handshake_fail > 0 ||
    122       read_fail > 0) {
    123     ADD_FAILURE() << "Number of channel read fails: " << channel_fail << "\n"
    124                   << "Number of signal read fails: " << signal_fail << "\n"
    125                   << "Number of handshake send fails: " << handshake_fail
    126                   << "\n"
    127                   << "Number of read response fails: " << read_fail;
    128   }
    129 }
    130 
    131 }  // bttest
    132