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

[WIP] Add unit test workflow #135

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
45 changes: 45 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Unit Tests

on:
pull_request:
paths:
- ".github/workflows/unit-tests.yml"
- 'extras/test/**'
- 'src/**'

push:
paths:
- ".github/workflows/unit-tests.yml"
- 'extras/test/**'
- 'src/**'

jobs:
test:
name: Run unit tests
runs-on: ubuntu-latest

env:
COVERAGE_DATA_PATH: extras/coverage-data/coverage.info

steps:
- name: Checkout
uses: actions/checkout@v2

- uses: arduino/cpp-test-action@main
with:
runtime-paths: |
- extras/test/build/bin/TEST_TARGET_UUID
- extras/test/build/bin/TEST_TARGET_DISC_DEVICE
- extras/test/build/bin/TEST_TARGET_ADVERTISING_DATA
coverage-exclude-paths: |
- '*/extras/test/*'
- '/usr/*'
coverage-data-path: ${{ env.COVERAGE_DATA_PATH }}

- name: Upload coverage report to Codecov
uses: codecov/codecov-action@v1
with:
file: "${{ env.COVERAGE_DATA_PATH }}"
fail_ci_if_error: true


60 changes: 40 additions & 20 deletions extras/test/src/test_advertising_data/test_advertising_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ TEST_CASE("Test flags override", "[ArduinoBLE::BLEAdvertisingData]")
// Mocking advertisement packet
BLEAdvertisingData advData;
// Expected results
uint8_t defaultData[] = {0x02, BLEFieldFlags, 0x06};
uint8_t goldenFlags[] = {0x02, 0x01, BLEFlagsBREDRNotSupported};
const uint8_t defaultData[] = {0x02, BLEFieldFlags, 0x06};
const uint8_t goldenFlags[] = {0x02, 0x01, BLEFlagsBREDRNotSupported};

WHEN("Default options for flags")
{
Expand Down Expand Up @@ -74,9 +74,9 @@ TEST_CASE("Set default flags in an already full advertising data packet", "[Ardu
const uint8_t manufacturerData[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23};

uint8_t defaultFlags[3] = {0x02, 0x01, 0x06};
const uint8_t defaultFlags[3] = {0x02, 0x01, 0x06};

uint8_t goldenData[31] = {
const uint8_t goldenData[31] = {
(sizeof(manufacturerData) + 1), BLEFieldManufacturerData, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23,
((uint8_t)(strlen(name) + 1)), BLEFieldCompleteLocalName, 't', 'e', 's'
Expand Down Expand Up @@ -109,22 +109,36 @@ TEST_CASE("BLE overwrite a full internal advertising data with an external built
BLE.setLocalName("test");
BLE.setAdvertisedServiceUuid("1818");
BLE.advertise();
//WARN("data length: " << BLE.getAdvertisingData().dataLength());
verify = (BLE.getAdvertisingData().dataLength() == (3 + (2+2)));
REQUIRE(verify);
verify = (BLE.getScanResponseData().dataLength() == (4+2));
REQUIRE(verify);

THEN("Check that BLE advertising data has been set")
{
verify = (BLE.getAdvertisingData().dataLength() == (3 + (2+2)));
REQUIRE(verify);
}

THEN("Check that BLE scan response data has been set")
{
verify = (BLE.getScanResponseData().dataLength() == (4+2));
REQUIRE(verify);
}

// Copy external empty adv data into advertising data
BLE.setAdvertisingData(advData);
BLE.advertise();
//WARN(BLE.getAdvertisingData().dataLength());
verify = (BLE.getAdvertisingData().dataLength() == 3);
REQUIRE(verify);
THEN("BLE advertising data should be erased")
{
verify = (BLE.getAdvertisingData().dataLength() == 3);
REQUIRE(verify);
}

// Copy external empty adv data into scan response data
BLE.setScanResponseData(advData);
BLE.advertise();
verify = (BLE.getScanResponseData().dataLength() == 0);
REQUIRE(verify);
THEN("BLE scan response data should be erased")
{
verify = (BLE.getScanResponseData().dataLength() == 0);
REQUIRE(verify);
}
}

// Clear BLE advertising data
Expand All @@ -139,29 +153,35 @@ TEST_CASE("BLE test raw data", "[ArduinoBLE::BLEAdvertisingData]")

WHEN("Set too large raw data")
{
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
const uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
advData.setRawData(data, sizeof(data));
REQUIRE(!retVal);
retVal = advData.setRawData(data, sizeof(data));
THEN("Set raw data should return false. The parameter should not be set")
{
REQUIRE(!retVal);
}
advData.clear();
}

WHEN("Set correct raw data")
{
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
const uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
retVal = advData.setRawData(data, sizeof(data));
REQUIRE(retVal);
THEN("Set raw data should return true. The parameter should be correctly set")
{
REQUIRE(retVal);
}
advData.updateData();
REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) );
advData.clear();
}

WHEN("Hide other parameters by setting raw data")
{
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
const uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
advData.setLocalName("test");
advData.setRawData(data, sizeof(data));

Expand Down
43 changes: 28 additions & 15 deletions extras/test/src/test_advertising_data/test_local_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@ TEST_CASE("Test local name setting", "[ArduinoBLE::BLEAdvertisingData]")
{
const char* name = "test";
retVal = advData.setLocalName(name);
REQUIRE(retVal);
REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) );
THEN("Set local name should return true. The name parameter should be correctly set")
{
REQUIRE(retVal);
}

THEN("Check the exact number of bytes occupied by the name just set")
{
REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) );
}
oldRemainingLength = advData.remainingLength();

advData.updateData();
Expand All @@ -47,30 +54,36 @@ TEST_CASE("Test local name setting", "[ArduinoBLE::BLEAdvertisingData]")
{
const char* name = "way too long local name (len 32)";
retVal = advData.setLocalName(name);
REQUIRE(!retVal);
REQUIRE( oldRemainingLength == advData.remainingLength() );
advData.updateData();
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH - oldRemainingLength) );
THEN("Set local name should return false. The name parameter should not be set")
{
REQUIRE(!retVal);
REQUIRE( oldRemainingLength == advData.remainingLength() );
advData.updateData();
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH - oldRemainingLength) );
}
}

WHEN("Overwrite local name with a name as long as max data length")
{
const char* name = "local name with full length ";
retVal = advData.setLocalName(name);
REQUIRE(retVal);
REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) );
oldRemainingLength = advData.remainingLength();
THEN("The name parameter should be correctly overwritten. The remaining length should be 0")
{
REQUIRE(retVal);
REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) );
oldRemainingLength = advData.remainingLength();

advData.updateData();
REQUIRE(advData.dataLength() == (strlen(name) + 2));
// advData should be full now
REQUIRE( 0 == advData.remainingLength() );
REQUIRE( 0 == advData.availableForWrite() );
advData.updateData();
REQUIRE(advData.dataLength() == (strlen(name) + 2));
// advData should be full now
REQUIRE( 0 == advData.remainingLength() );
REQUIRE( 0 == advData.availableForWrite() );
}
}

WHEN("Check consistency when setting the external advertising data")
{
auto goldenData = advData.data();
const auto goldenData = advData.data();
BLE.setAdvertisingData(advData);
BLE.advertise();
REQUIRE( 0 == memcmp(goldenData, BLE.getAdvertisingData().data(), advData.dataLength()) );
Expand Down
81 changes: 54 additions & 27 deletions extras/test/src/test_advertising_data/test_manufacturer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
{
BLEAdvertisingData advData;
int oldRemainingLength = advData.remainingLength();
uint16_t companyId = 0x1100;
const uint16_t companyId = 0x1100;
bool retVal;

WHEN("Set correct manufacturer data without id")
Expand All @@ -38,8 +38,11 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
const uint8_t goldenData[] = {(sizeof(data) + 1), BLEFieldManufacturerData,
0x00, 0x11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
retVal = advData.setManufacturerData(data, sizeof(data));
REQUIRE(retVal);
REQUIRE( (sizeof(data) + 2) == (oldRemainingLength - advData.remainingLength()) );
THEN("Check that the manufacturer data has been correctly set")
{
REQUIRE(retVal);
REQUIRE( (sizeof(data) + 2) == (oldRemainingLength - advData.remainingLength()) );
}
oldRemainingLength = advData.remainingLength();

advData.updateData();
Expand All @@ -53,8 +56,11 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
const uint8_t goldenData[] = {(sizeof(data) + sizeof(companyId) + 1), BLEFieldManufacturerData,
0x00, 0x11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
retVal = advData.setManufacturerData(companyId, data, sizeof(data));
REQUIRE(retVal);
REQUIRE( (sizeof(data) + sizeof(companyId) + 2) == (oldRemainingLength - advData.remainingLength()) );
THEN("Check that the manufacturer data has been correctly set")
{
REQUIRE(retVal);
REQUIRE( (sizeof(data) + sizeof(companyId) + 2) == (oldRemainingLength - advData.remainingLength()) );
}
oldRemainingLength = advData.remainingLength();

advData.updateData();
Expand All @@ -68,8 +74,11 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
const uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
retVal = advData.setManufacturerData(companyId, data, sizeof(data));
REQUIRE(!retVal);
REQUIRE( oldRemainingLength == advData.remainingLength() );
THEN("Manufacturer data was too long, check that it has not been set")
{
REQUIRE(!retVal);
REQUIRE( oldRemainingLength == advData.remainingLength() );
}
advData.updateData();
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH - oldRemainingLength) );
}
Expand All @@ -80,8 +89,11 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
const uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29};
retVal = advData.setManufacturerData(data, sizeof(data));
REQUIRE(!retVal);
REQUIRE( oldRemainingLength == advData.remainingLength() );
THEN("Manufacturer data was too long, check that it has not been set")
{
REQUIRE(!retVal);
REQUIRE( oldRemainingLength == advData.remainingLength() );
}
advData.updateData();
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH - oldRemainingLength) );
}
Expand All @@ -93,13 +105,19 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26};
const uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28};
retVal = advData.setManufacturerData(companyId, dataGivenId, sizeof(dataGivenId));
REQUIRE(retVal);
retVal = advData.setManufacturerData(data, sizeof(data));
REQUIRE(retVal);
REQUIRE( 0 == advData.remainingLength() );
advData.updateData();
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH) );
THEN("Check that first insertion of manufacturer data given id is correctly done")
{
retVal = advData.setManufacturerData(companyId, dataGivenId, sizeof(dataGivenId));
REQUIRE(retVal);
}
THEN("Check that the insertion of manufacturer data without after one with id is correctly done")
{
retVal = advData.setManufacturerData(data, sizeof(data));
REQUIRE(retVal);
REQUIRE( 0 == advData.remainingLength() );
advData.updateData();
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH) );
}
}

WHEN("Set manufacturer data given id after setting manufacturer data without id")
Expand All @@ -110,10 +128,16 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27};
const uint8_t data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28};
retVal = advData.setManufacturerData(data, sizeof(data));
REQUIRE(retVal);
retVal = advData.setManufacturerData(companyId, dataGivenId, sizeof(dataGivenId));
REQUIRE(!retVal);
THEN("Check that first insertion of manufacturer data WITHOUT id is correctly done")
{
retVal = advData.setManufacturerData(data, sizeof(data));
REQUIRE(retVal);
}
THEN("Check that the insertion of manufacturer data given id after one without id is correctly done")
{
retVal = advData.setManufacturerData(companyId, dataGivenId, sizeof(dataGivenId));
REQUIRE(!retVal);
}
}

WHEN("Overwrite manufacturer data with one as long as max data length")
Expand All @@ -124,17 +148,20 @@ TEST_CASE("Test manufacturer data setting", "[ArduinoBLE::BLEAdvertisingData]")
0x00, 0x11, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26};
retVal = advData.setManufacturerData(companyId, data, sizeof(data));
REQUIRE(retVal);

advData.updateData();
REQUIRE( 0 == advData.remainingLength() );
REQUIRE( 0 == advData.availableForWrite() );
REQUIRE( 0 == memcmp(goldenData, advData.data(), sizeof(goldenData)) );
THEN("Manufacturer data should be set correctly")
{
REQUIRE(retVal);

advData.updateData();
REQUIRE( 0 == advData.remainingLength() );
REQUIRE( 0 == advData.availableForWrite() );
REQUIRE( 0 == memcmp(goldenData, advData.data(), sizeof(goldenData)) );
}
}

WHEN("Check consistency when setting the external advertising data")
{
auto goldenData = advData.data();
const auto goldenData = advData.data();
BLE.setAdvertisingData(advData);
BLE.advertise();
REQUIRE( 0 == memcmp(goldenData, BLE.getAdvertisingData().data(), advData.dataLength()) );
Expand Down
Loading