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

Commit f41e0ee6 authored by Joshua Schwarz's avatar Joshua Schwarz
Browse files

Implementation of net_test_bluedroid as a GTest.

Change-Id: If9fc9a26fe58858a107b8ad6ac9f1e757ba07f90
parent 0d838ab4
Loading
Loading
Loading
Loading
+55 −0
Original line number Diff line number Diff line
#
#  Copyright (C) 2015 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.
#

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := gtest_net_test_bluedroid

LOCAL_C_INCLUDES += \
    $(LOCAL_PATH)/../../

LOCAL_SRC_FILES := \
    cases/adapter.c \
    cases/cases.c \
    cases/gatt.c \
    cases/pan.c \
    cases/rfcomm.c \
    support/adapter.c \
    support/callbacks.c \
    support/gatt.c \
    support/hal.c \
    support/pan.c \
    support/rfcomm.c \
    main.cpp

LOCAL_SHARED_LIBRARIES += \
    liblog \
    libhardware \
    libhardware_legacy \
    libcutils

LOCAL_STATIC_LIBRARIES += \
  libbtcore \
  libosi

LOCAL_CFLAGS += -Wall -Wno-unused-parameter -Wno-missing-field-initializers -Werror

LOCAL_MULTILIB := 32

include $(BUILD_NATIVE_TEST)
+39 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 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.
 *
 ******************************************************************************/

#pragma once

#include <malloc.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include <hardware/bluetooth.h>
#include <hardware/bt_gatt.h>
#include <hardware/bt_pan.h>
#include <hardware/bt_sock.h>
#include <hardware/hardware.h>

#ifndef ARRAY_SIZE
#  define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#endif

#define TASSERT(c, ...) if (!(c)) { fprintf(stderr, "%s:%d: ", __func__, __LINE__); fprintf(stderr, __VA_ARGS__); fprintf(stderr, "\n"); return false; }

extern const bt_interface_t *bt_interface;
extern bt_bdaddr_t bt_remote_bdaddr;
+97 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 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 "base.h"
#include "support/adapter.h"
#include "support/callbacks.h"
#include "btcore/include/property.h"

bool adapter_enable_disable() {
  int error;

  CALL_AND_WAIT(error = bt_interface->enable(), adapter_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error enabling Bluetooth: %d", error);
  TASSERT(adapter_get_state() == BT_STATE_ON, "Adapter did not turn on.");

  CALL_AND_WAIT(error = bt_interface->disable(), adapter_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error disabling Bluetooth: %d", error);
  TASSERT(adapter_get_state() == BT_STATE_OFF, "Adapter did not turn off.");

  return true;
}

bool adapter_repeated_enable_disable() {
  int i;
  for (i = 0; i < 10; ++i) {
    if (!adapter_enable_disable()) {
      return false;
    }
  }
  return true;
}

bool adapter_set_name() {
  int error;
  bt_property_t *name = property_new_name("set_name");

  CALL_AND_WAIT(error = bt_interface->set_adapter_property(name), adapter_properties);
  TASSERT(error == BT_STATUS_SUCCESS, "Error setting device name.");
  TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());
  TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");
  TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_as_name(adapter_get_property(BT_PROPERTY_BDNAME))->name);

  property_free(name);

  return true;
}

bool adapter_get_name() {
  int error;
  bt_property_t *name = property_new_name("get_name");

  CALL_AND_WAIT(bt_interface->set_adapter_property(name), adapter_properties);
  CALL_AND_WAIT(error = bt_interface->get_adapter_property(BT_PROPERTY_BDNAME), adapter_properties);
  TASSERT(error == BT_STATUS_SUCCESS, "Error getting device name.");
  TASSERT(adapter_get_property_count() == 1, "Expected 1 adapter property change, found %d instead.", adapter_get_property_count());
  TASSERT(adapter_get_property(BT_PROPERTY_BDNAME), "The Bluetooth name property did not change.");
  TASSERT(property_equals(adapter_get_property(BT_PROPERTY_BDNAME), name), "Bluetooth name '%s' does not match test value", property_as_name(adapter_get_property(BT_PROPERTY_BDNAME))->name);

  property_free(name);
  return true;
}

bool adapter_start_discovery() {
  int error;

  CALL_AND_WAIT(error = bt_interface->start_discovery(), discovery_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error calling start_discovery: %d", error);
  TASSERT(adapter_get_discovery_state() == BT_DISCOVERY_STARTED, "Unable to start discovery.");

  return true;
}

bool adapter_cancel_discovery() {
  int error;

  CALL_AND_WAIT(bt_interface->start_discovery(), discovery_state_changed);
  CALL_AND_WAIT(error = bt_interface->cancel_discovery(), discovery_state_changed);
  TASSERT(error == BT_STATUS_SUCCESS, "Error calling cancel_discovery: %d", error);
  TASSERT(adapter_get_discovery_state() == BT_DISCOVERY_STOPPED, "Unable to stop discovery.");

  return true;
}
+71 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 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 "base.h"
#include "cases/cases.h"

TEST_CASE_DECL(adapter_enable_disable);
TEST_CASE_DECL(adapter_repeated_enable_disable);
TEST_CASE_DECL(adapter_set_name);
TEST_CASE_DECL(adapter_get_name);
TEST_CASE_DECL(adapter_start_discovery);
TEST_CASE_DECL(adapter_cancel_discovery);

TEST_CASE_DECL(rfcomm_connect);
TEST_CASE_DECL(rfcomm_repeated_connect);

TEST_CASE_DECL(pan_enable);
TEST_CASE_DECL(pan_connect);
TEST_CASE_DECL(pan_disconnect);
TEST_CASE_DECL(pan_quick_reconnect);

TEST_CASE_DECL(gatt_client_register);
TEST_CASE_DECL(gatt_client_scan);
TEST_CASE_DECL(gatt_client_advertise);
TEST_CASE_DECL(gatt_server_register);
TEST_CASE_DECL(gatt_server_build);

// These are run with the Bluetooth adapter disabled.
const test_case_t sanity_suite[] = {
  TEST_CASE(adapter_enable_disable),
  TEST_CASE(adapter_repeated_enable_disable)
};

// The normal test suite is run with the adapter enabled.
const test_case_t test_suite[] = {
  TEST_CASE(adapter_set_name),
  TEST_CASE(adapter_get_name),
  TEST_CASE(adapter_start_discovery),
  TEST_CASE(adapter_cancel_discovery),

  TEST_CASE(rfcomm_connect),
  TEST_CASE(rfcomm_repeated_connect),

  TEST_CASE(pan_enable),
  TEST_CASE(pan_connect),
  TEST_CASE(pan_disconnect),

  TEST_CASE(gatt_client_register),
  TEST_CASE(gatt_client_scan),
  TEST_CASE(gatt_client_advertise),
  TEST_CASE(gatt_server_register),
  TEST_CASE(gatt_server_build)
};

const size_t sanity_suite_size = ARRAY_SIZE(sanity_suite);
const size_t test_suite_size = ARRAY_SIZE(test_suite);
+34 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2015 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.
 *
 ******************************************************************************/

#pragma once

#include "base.h"

#define TEST_CASE_DECL(x) bool x()
#define TEST_CASE(x) { x, #x }

typedef struct {
  bool (*function)();
  const char *function_name;
} test_case_t;

extern const test_case_t test_suite[];
extern const test_case_t sanity_suite[];
extern const size_t test_suite_size;
extern const size_t sanity_suite_size;
Loading