Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE: Add cmake unittest fakes for BLE and events #14737

Merged
merged 8 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UNITTESTS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ if (VALGRIND)
endif(VALGRIND)

add_subdirectory(stubs)
add_subdirectory(fakes)

65 changes: 65 additions & 0 deletions UNITTESTS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## Stubs, mocks and fakes in Mbed OS
Copy link
Contributor

@rajkan01 rajkan01 Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better we move this readme content into our official documentation https://github.com/ARMmbed/mbed-os-5-docs/blob/development/docs/debugging-testing/testing/unit_testing.md page

Note:
Already this PR ARMmbed/mbed-os-5-docs#1427 for updating unit testing content based on new unittest CMake build system

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already have a PR for documentation of ble unit testing: https://github.com/ARMmbed/mbed-os-5-docs/pull/1399/files

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I requested Readme to introduce stubs and fakes (I was thinking putting it into stubs/fakes folders). It can contain basic info and reference to our docs pages. Basically if I fetch the repo and start browsing sources, I can read readme and browse files and contribute.

@paul-szczepanek-arm Lets move this readme to your 1399 PR. It contains valuable information.

@rajkan01 Are you proposing to completely remove this readme (move everything to docs) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so you don't want the readme in mbed-os?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xc0170 @paul-szczepanek-arm Yes, I have only removed the existing Readme from the UNITTESTS directory as part of Refactor unittest cmake PR as content gets duplicated and also official vs local readme content mismatch so better to move this readme content into the official Mbed OS documentation


To make unit testing easier, Mbed OS provides some ready-made CMake libraries you can add in your `target_link_libraries`.
Libraries can be object files that contain stubs, mocks and fakes but also sets of include paths - this way you can gain access to normal headers from Mbed OS.

All the libraries available are in the `CMakeLists.txt` files in `mbed-os/UNITTESTS/stubs` and `mbed-os/UNITTESTS/fakes`.

The most common ones you might need are `mbed-os-fakes-ble` and `mbed-os-fakes-event-queue`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The most common ones you might need are `mbed-os-fakes-ble` and `mbed-os-fakes-event-queue`.

Shall we just remove this, I would add just that libraries are prefixed with mbed-os-fakes in the next section.


### Fakes

While stubs are self explanatory and don't offer anything beyond empty implementations, fakes allow for more complex unittests that can simulate whole subsystems.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a note, fakes library should be prefixed with mbed-os-fakes.


#### mbed-os-fakes-ble

This library provides a fake BLE implementation that uses mocks instead of real BLE components for `Gap`, `GattServer`, `GattClient`, `SecurityManager`.

There is no need to initialise a fake BLE instance; it is ready to go and can be used just like a normal BLE instance:

```c++
BLE *ble = &BLE::Instance();
```

This call also initialises mocks.
Do no cache the BLE instance pointer, or pointer to `GAP`, `GattServer` etc. between tests.
You must get the instance fresh at the start of the test.

You can retrieve all the BLE APIs from the instance just like with a normal one:

```c++
Gap &gap = ble->gap();
GattClient &client = ble->gattClient();
GattServer &server = ble->gattServer();
SecurityManager &sm = ble->securityManager();
```

Whenever an API call is made, the implementation will be called.
These are replaced in the fake BLE with google mocks.
This means you can set expectations on them.

```c++
EXPECT_CALL(ble::gap_mock(), reset());
```

This will set up an expectation that at some point during the test the `Gap::reset` method will be called.

The way GoogleTest works means that if you set expectations on your mocks they must be destroyed at the end of each test.
This is done through the fake BLE instance special method:

```c++
ble::delete_mocks();
```

#### mbed-os-fakes-event-queue

This is a fake event queue that doesn't bring in any dependencies from mbed-os.
Its API is simplified and it only offers limited functionality.

If you choose to use it you must not also include a library that brings in real headers for the event queue as they would conflict.

The API calls supported are for simple calls `call`, `call_in` and the `cancel` method.

The event queue is not run in real time and must be progressed manually.
You may use `dispatch(int milliseconds)` and `dispatch_forever()` to process events in the queue.
This way you can simulate the passage of time in your test.
5 changes: 5 additions & 0 deletions UNITTESTS/fakes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_subdirectory(events)
add_subdirectory(ble)
163 changes: 163 additions & 0 deletions UNITTESTS/fakes/ble/BLE.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* mbed Microcontroller Library
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* 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 "ble/BLE.h"
#include "GattServerImpl_mock.h"
#include "GattClientImpl_mock.h"
#include "GapImpl_mock.h"
#include "SecurityManagerImpl_mock.h"
#include "ble/GattClient.h"
#include "ble/GattServer.h"
#include "ble/SecurityManager.h"
#include "ble/Gap.h"
#include "ble_mocks.h"

namespace ble {

static GapMock *gap_impl = nullptr;
static GattServerMock *gatt_server_impl = nullptr;
static GattClientMock *gatt_client_impl = nullptr;
static SecurityManagerMock *security_manager_impl = nullptr;

static Gap *gap = nullptr;
static GattServer *gatt_server = nullptr;
static GattClient *gatt_client = nullptr;
static SecurityManager *security_manager = nullptr;

GapMock& gap_mock() {
return *ble::gap_impl;
}

GattServerMock& gatt_server_mock() {
return *ble::gatt_server_impl;
}

GattClientMock& gatt_client_mock() {
return *ble::gatt_client_impl;
}

SecurityManagerMock& security_manager_mock() {
return *ble::security_manager_impl;
}

void init_mocks() {
if (gap_impl) {
/* we are already initialised */
return;
}

/* mocks */
gap_impl = new GapMock();
gatt_server_impl = new GattServerMock();
gatt_client_impl = new GattClientMock();
security_manager_impl = new SecurityManagerMock();
/* user APIS */
gap = new Gap(gap_impl);
gatt_server = new GattServer(gatt_server_impl);
gatt_client = new GattClient(gatt_client_impl);
security_manager = new SecurityManager(security_manager_impl);
}

void delete_mocks() {
delete gap;
delete gap_impl;
delete gatt_server;
delete gatt_server_impl;
delete gatt_client;
delete gatt_client_impl;
delete security_manager;
delete security_manager_impl;

gap = nullptr;
gap_impl = nullptr;
gatt_server = nullptr;
gatt_server_impl = nullptr;
gatt_client = nullptr;
gatt_client_impl = nullptr;
security_manager = nullptr;
security_manager_impl = nullptr;
}

class BLEInstanceBase {
};

BLE::BLE(ble::BLEInstanceBase &transport) : transport(transport)
{
}

BLE& BLE::Instance()
{
static ble::BLEInstanceBase transport;
static BLE instance(transport);
init_mocks();
return instance;
}

ble::Gap &BLE::gap()
{
init_mocks();
return *ble::gap;
}

ble::GattServer &BLE::gattServer()
{
init_mocks();
return *ble::gatt_server;
}

ble::GattClient &BLE::gattClient()
{
init_mocks();
return *ble::gatt_client;
}

ble::SecurityManager &BLE::securityManager()
{
init_mocks();
return *ble::security_manager;
}

const ble::Gap &BLE::gap() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::Gap &>(self.gap());
}

const ble::GattServer &BLE::gattServer() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::GattServer &>(self.gattServer());
}

const ble::GattClient &BLE::gattClient() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::GattClient &>(self.gattClient());
}

const ble::SecurityManager &BLE::securityManager() const
{
auto &self = const_cast<BLE &>(*this);
return const_cast<const ble::SecurityManager &>(self.securityManager());
}

void BLE::processEvents()
{

}

}
41 changes: 41 additions & 0 deletions UNITTESTS/fakes/ble/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2020 ARM Limited. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

add_library(mbed-fakes-ble)

target_include_directories(mbed-fakes-ble
PUBLIC
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/include
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/include/ble
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source
PRIVATE
${gtest_SOURCE_DIR}/include
${gmock_SOURCE_DIR}/include
)

target_sources(mbed-fakes-ble
PRIVATE
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/AdvertisingDataBuilder.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/AdvertisingParameters.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gap/ConnectionParameters.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/gatt/DiscoveredCharacteristic.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/Gap.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/GattClient.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/GattServer.cpp
${mbed-os_SOURCE_DIR}/connectivity/FEATURE_BLE/source/SecurityManager.cpp
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/BLE.cpp
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/source/GattServerImpl_mock.cpp
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/ble_mocks.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GapImpl_mock.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GattClientImpl_mock.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/GattServerImpl_mock.h
${mbed-os_SOURCE_DIR}/UNITTESTS/fakes/ble/SecurityManagerImpl_mock.h
)

target_link_libraries(mbed-fakes-ble
PRIVATE
mbed-headers
mbed-stubs-headers
gcov
)
91 changes: 91 additions & 0 deletions UNITTESTS/fakes/ble/GapImpl_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* mbed Microcontroller Library
* Copyright (c) 2020 ARM Limited
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

#ifndef BLE_GAPMOCK_H
#define BLE_GAPMOCK_H

#include "gmock/gmock.h"
#include "source/generic/GapImpl.h"

namespace ble {

class GapMock : public ble::impl::Gap {
public:
GapMock() {};
GapMock(const GapMock&) = delete;
GapMock& operator=(const GapMock&) = delete;
virtual ~GapMock() {};

MOCK_METHOD((ble_error_t), reset, (), (override));
MOCK_METHOD(void, setEventHandler, (EventHandler *handler), (override));
MOCK_METHOD(bool, isFeatureSupported, (controller_supported_features_t feature), (override));
MOCK_METHOD(uint8_t, getMaxAdvertisingSetNumber, (), (override));
MOCK_METHOD(uint16_t, getMaxAdvertisingDataLength, (), (override));
MOCK_METHOD(uint16_t, getMaxConnectableAdvertisingDataLength, (), (override));
MOCK_METHOD(uint16_t, getMaxActiveSetAdvertisingDataLength, (), (override));
MOCK_METHOD(ble_error_t, createAdvertisingSet, (advertising_handle_t *handle, const AdvertisingParameters &parameters), (override));
MOCK_METHOD(ble_error_t, destroyAdvertisingSet, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, setAdvertisingParameters, (advertising_handle_t handle, const AdvertisingParameters &params), (override));
MOCK_METHOD(ble_error_t, setAdvertisingPayload, (advertising_handle_t handle, mbed::Span<const uint8_t> payload), (override));
MOCK_METHOD(ble_error_t, setAdvertisingScanResponse, (advertising_handle_t handle, mbed::Span<const uint8_t> response), (override));
MOCK_METHOD(ble_error_t, startAdvertising, (advertising_handle_t handle, adv_duration_t maxDuration, uint8_t maxEvents), (override));
MOCK_METHOD(ble_error_t, stopAdvertising, (advertising_handle_t handle), (override));
MOCK_METHOD(bool, isAdvertisingActive, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, setPeriodicAdvertisingParameters, (advertising_handle_t handle, periodic_interval_t periodicAdvertisingIntervalMin, periodic_interval_t periodicAdvertisingIntervalMax, bool advertiseTxPower), (override));
MOCK_METHOD(ble_error_t, setPeriodicAdvertisingPayload, (advertising_handle_t handle, mbed::Span<const uint8_t> payload), (override));
MOCK_METHOD(ble_error_t, startPeriodicAdvertising, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, stopPeriodicAdvertising, (advertising_handle_t handle), (override));
MOCK_METHOD(bool, isPeriodicAdvertisingActive, (advertising_handle_t handle), (override));
MOCK_METHOD(ble_error_t, setScanParameters, (const ScanParameters &params), (override));
MOCK_METHOD(ble_error_t, startScan, (scan_duration_t duration, duplicates_filter_t filtering, scan_period_t period), (override));
MOCK_METHOD(ble_error_t, initiate_scan, (), (override));
MOCK_METHOD(ble_error_t, stopScan, (), (override));
MOCK_METHOD(ble_error_t, createSync, (peer_address_type_t peerAddressType, const address_t &peerAddress, uint8_t sid, slave_latency_t maxPacketSkip, sync_timeout_t timeout), (override));
MOCK_METHOD(ble_error_t, createSync, (slave_latency_t maxPacketSkip, sync_timeout_t timeout), (override));
MOCK_METHOD(ble_error_t, cancelCreateSync, (), (override));
MOCK_METHOD(ble_error_t, terminateSync, (periodic_sync_handle_t handle), (override));
MOCK_METHOD(ble_error_t, addDeviceToPeriodicAdvertiserList, (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid), (override));
MOCK_METHOD(ble_error_t, removeDeviceFromPeriodicAdvertiserList, (peer_address_type_t peerAddressType, const address_t &peerAddress, advertising_sid_t sid), (override));
MOCK_METHOD(ble_error_t, clearPeriodicAdvertiserList, (), (override));
MOCK_METHOD(ble_error_t, connect, (peer_address_type_t peerAddressType, const address_t &peerAddress, const ConnectionParameters &connectionParams), (override));
MOCK_METHOD(ble_error_t, cancelConnect, (), (override));
MOCK_METHOD(ble_error_t, updateConnectionParameters, (connection_handle_t connectionHandle, conn_interval_t minConnectionInterval, conn_interval_t maxConnectionInterval, slave_latency_t slaveLatency, supervision_timeout_t supervision_timeout, conn_event_length_t minConnectionEventLength, conn_event_length_t maxConnectionEventLength), (override));
MOCK_METHOD(ble_error_t, manageConnectionParametersUpdateRequest, (bool userManageConnectionUpdateRequest), (override));
MOCK_METHOD(ble_error_t, acceptConnectionParametersUpdate, (connection_handle_t connectionHandle, conn_interval_t minConnectionInterval, conn_interval_t maxConnectionInterval, slave_latency_t slaveLatency, supervision_timeout_t supervision_timeout, conn_event_length_t minConnectionEventLength, conn_event_length_t maxConnectionEventLength), (override));
MOCK_METHOD(ble_error_t, rejectConnectionParametersUpdate, (connection_handle_t connectionHandle), (override));
MOCK_METHOD(ble_error_t, disconnect, (connection_handle_t connectionHandle, local_disconnection_reason_t reason), (override));
MOCK_METHOD(ble_error_t, readPhy, (connection_handle_t connection), (override));
MOCK_METHOD(ble_error_t, setPreferredPhys, (const phy_set_t *txPhys, const phy_set_t *rxPhys), (override));
MOCK_METHOD(ble_error_t, setPhy, (connection_handle_t connection, const phy_set_t *txPhys, const phy_set_t *rxPhys, coded_symbol_per_bit_t codedSymbol), (override));
MOCK_METHOD(ble_error_t, enablePrivacy, (bool enable), (override));
MOCK_METHOD(ble_error_t, setPeripheralPrivacyConfiguration, (const peripheral_privacy_configuration_t *configuration), (override));
MOCK_METHOD(ble_error_t, getPeripheralPrivacyConfiguration, (peripheral_privacy_configuration_t *configuration), (override));
MOCK_METHOD(ble_error_t, setCentralPrivacyConfiguration, (const central_privacy_configuration_t *configuration), (override));
MOCK_METHOD(ble_error_t, getCentralPrivacyConfiguration, (central_privacy_configuration_t *configuration), (override));
MOCK_METHOD(uint8_t, getMaxWhitelistSize, (), (const, override));
MOCK_METHOD(ble_error_t, getWhitelist, (whitelist_t &whitelist), (const, override));
MOCK_METHOD(ble_error_t, setWhitelist, (const whitelist_t &whitelist), (override));
MOCK_METHOD(ble_error_t, getAddress, (own_address_type_t &typeP, address_t &address), (override));
MOCK_METHOD(void, onShutdown, (const GapShutdownCallback_t &callback), (override));
MOCK_METHOD(GapShutdownCallbackChain_t&, onShutdown, (), (override));
MOCK_METHOD(ble_error_t, setRandomStaticAddress, (const ble::address_t &address), (override));
MOCK_METHOD(ble::address_t, getRandomStaticAddress, (), (override));
};

}

#endif //BLE_GAPMOCK_H
Loading