Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d022c356 authored by David Duarte's avatar David Duarte Committed by Gerrit Code Review
Browse files

Merge "Remove net_test_rfcomm_suite"

parents 5037033c c4b6128e
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -124,13 +124,3 @@ cc_test {
        "gatt/gatt_unittest.cc",
    ],
}

// Bluetooth test suite for target
cc_test {
    name: "net_test_rfcomm_suite",
    defaults: ["net_test_defaults"],
    srcs: [
        "rfcomm/rfcomm_test.cc",
        "rfcomm/rfcomm_unittest.cc",
    ],
}
+0 −82
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2016 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "rfcomm/rfcomm_test.h"

#include "adapter/bluetooth_test.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"

using bluetooth::Uuid;

namespace bttest {

const Uuid RFCommTest::HFP_UUID = Uuid::From16Bit(0x111E);

void RFCommTest::SetUp() {
  BluetoothTest::SetUp();

  ASSERT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);
  ASSERT_TRUE(GetState() == BT_STATE_ON);
  socket_interface_ =
      (const btsock_interface_t*)bt_interface()->get_profile_interface(
          BT_PROFILE_SOCKETS_ID);
  ASSERT_NE(socket_interface_, nullptr);

  // Find a bonded device that supports HFP
  bt_remote_bdaddr_ = RawAddress::kEmpty;

  bt_property_t* bonded_devices_prop =
      GetProperty(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
  RawAddress* devices = (RawAddress*)bonded_devices_prop->val;
  int num_bonded_devices = bonded_devices_prop->len / sizeof(RawAddress);

  for (int i = 0; i < num_bonded_devices && bt_remote_bdaddr_.IsEmpty(); i++) {
    ClearSemaphore(remote_device_properties_callback_sem_);
    bt_interface()->get_remote_device_property(&devices[i], BT_PROPERTY_UUIDS);
    semaphore_wait(remote_device_properties_callback_sem_);

    bt_property_t* uuid_prop =
        GetRemoteDeviceProperty(&devices[i], BT_PROPERTY_UUIDS);
    if (uuid_prop == nullptr) continue;
    Uuid* uuids = reinterpret_cast<Uuid*>(uuid_prop->val);
    int num_uuids = uuid_prop->len / sizeof(Uuid);

    for (int j = 0; j < num_uuids; j++) {
      if (!memcmp(uuids + j, &HFP_UUID, sizeof(Uuid))) {
        bt_remote_bdaddr_ = *(devices + i);
        break;
      }
    }
  }

  ASSERT_FALSE(bt_remote_bdaddr_.IsEmpty())
      << "Could not find paired device that supports HFP";
}

void RFCommTest::TearDown() {
  socket_interface_ = NULL;

  ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);

  BluetoothTest::TearDown();
}

}  // bttest
+0 −49
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2016 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "adapter/bluetooth_test.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"

namespace bttest {

class RFCommTest : public BluetoothTest {
 protected:
  RFCommTest() = default;
  virtual ~RFCommTest() = default;

  // Getter for the RFCOMM socket
  const btsock_interface_t* socket_interface() const {
    return socket_interface_;
  }

  // SetUp initializes the Bluetooth interfaces and the RFCOMM interface
  virtual void SetUp();

  // TearDown cleans up the Bluetooth and RFCOMM interfaces
  virtual void TearDown();

  RawAddress bt_remote_bdaddr_;

  static const bluetooth::Uuid HFP_UUID;

 private:
  const btsock_interface_t* socket_interface_;
};

}  // bttest
+0 −131
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2016 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "adapter/bluetooth_test.h"
#include "rfcomm/rfcomm_test.h"

#include <sys/socket.h>
#include <unistd.h>

namespace {
static const char HANDSHAKE_COMMAND[] = "AT+BRSF=29\r";
}  // namespace

namespace bttest {

TEST_F(RFCommTest, RfcommConnectPairedDevice) {
  int fd = -1;
  int error = 0;
  size_t len = 0;

  error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM,
                                      &HFP_UUID, 0, &fd, 0, getuid());
  EXPECT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: "
                                          << error;
  EXPECT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd";

  int channel;
  sock_connect_signal_t signal;
  len = read(fd, &channel, sizeof(channel));
  EXPECT_TRUE(len == sizeof(channel))
      << "Channel not read from RFCOMM socket. Bytes read: " << len;
  len = read(fd, &signal, sizeof(signal));
  EXPECT_TRUE(len == sizeof(signal))
      << "Connection signal not read from RFCOMM socket. Bytes read: " << len;

  EXPECT_TRUE(signal.bd_addr == bt_remote_bdaddr_)
      << "Connected to a different bdaddr than expected.";
  EXPECT_TRUE(channel == signal.channel)
      << "Inconsistent channels returned: " << channel << " and "
      << signal.channel;

  len = write(fd, HANDSHAKE_COMMAND, sizeof(HANDSHAKE_COMMAND));
  EXPECT_TRUE(len == sizeof(HANDSHAKE_COMMAND))
      << "Unable to send HFP handshake. Bytes written: " << len;

  char response[1024];
  len = read(fd, response, sizeof(response));
  EXPECT_TRUE(len > 0) << "Read " << len << " bytes";

  close(fd);
}

TEST_F(RFCommTest, RfcommRepeatedConnectPairedDevice) {
  static const int max_iterations = 128;
  int channel_fail = 0, signal_fail = 0, handshake_fail = 0, read_fail = 0;

  for (int i = 0; i < max_iterations; ++i) {
    int fd = -1;
    int error = 0;
    size_t len = 0;

    error = socket_interface()->connect(&bt_remote_bdaddr_, BTSOCK_RFCOMM,
                                        &HFP_UUID, 0, &fd, 0, getuid());
    ASSERT_TRUE(error == BT_STATUS_SUCCESS) << "Error creating RFCOMM socket: "
                                            << error;
    ASSERT_TRUE(fd != -1) << "Error creating RFCOMM socket: invalid fd";

    int channel;
    sock_connect_signal_t signal;
    len = read(fd, &channel, sizeof(channel));
    if (len != sizeof(channel)) {
      ADD_FAILURE() << "Channel not read from RFCOMM socket. Bytes read: "
                    << len << ", Sizeof channel: " << sizeof(channel);
      channel_fail++;
    }

    len = read(fd, &signal, sizeof(signal));
    if (len != sizeof(signal)) {
      ADD_FAILURE()
          << "Connection signal not read from RFCOMM socket. Bytes read: "
          << len;
      signal_fail++;
    }

    EXPECT_TRUE(signal.bd_addr == bt_remote_bdaddr_)
        << "Connected to a different bdaddr than expected.";
    EXPECT_TRUE(channel == signal.channel)
        << "Inconsistent channels returned: " << channel << " and "
        << signal.channel;
    len = write(fd, HANDSHAKE_COMMAND, sizeof(HANDSHAKE_COMMAND));
    if (len != sizeof(HANDSHAKE_COMMAND)) {
      ADD_FAILURE() << "Unable to send HFP handshake. Bytes written: " << len;
      handshake_fail++;
    }

    char response[1024];
    len = read(fd, response, sizeof(response));
    if (len <= 0) {
      ADD_FAILURE() << "Read " << len << " bytes";
      read_fail++;
    }

    close(fd);
  }

  if (channel_fail > 0 || signal_fail > 0 || handshake_fail > 0 ||
      read_fail > 0) {
    ADD_FAILURE() << "Number of channel read fails: " << channel_fail << "\n"
                  << "Number of signal read fails: " << signal_fail << "\n"
                  << "Number of handshake send fails: " << handshake_fail
                  << "\n"
                  << "Number of read response fails: " << read_fail;
  }
}

}  // bttest