Skip to content

Commit 0ec9f24

Browse files
committed
CI - Add advertising data test
1 parent b586d0d commit 0ec9f24

File tree

8 files changed

+657
-7
lines changed

8 files changed

+657
-7
lines changed

Diff for: extras/test/CMakeLists.txt

+20-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ set(TEST_TARGET_DISC_DEVICE_SRCS
6969
src/test_discovered_device/FakeGAP.cpp
7070
)
7171

72+
set(TEST_TARGET_ADVERTISING_DATA_SRCS
73+
# Test files
74+
${COMMON_TEST_SRCS}
75+
src/test_advertising_data/test_advertising_data.cpp
76+
src/test_advertising_data/test_service.cpp
77+
src/test_advertising_data/test_local_name.cpp
78+
src/test_advertising_data/test_manufacturer.cpp
79+
# DUT files
80+
${DUT_SRCS}
81+
# Fake classes files
82+
src/util/HCIFakeTransport.cpp
83+
src/test_advertising_data/FakeBLELocalDevice.cpp
84+
)
85+
7286
##########################################################################
7387

7488
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage")
@@ -78,6 +92,7 @@ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage")
7892

7993
add_executable(TEST_TARGET_UUID ${TEST_TARGET_UUID_SRCS})
8094
add_executable(TEST_TARGET_DISC_DEVICE ${TEST_TARGET_DISC_DEVICE_SRCS})
95+
add_executable(TEST_TARGET_ADVERTISING_DATA ${TEST_TARGET_ADVERTISING_DATA_SRCS})
8196

8297
##########################################################################
8398

@@ -88,13 +103,14 @@ include_directories(../../src/local)
88103
include_directories(../../src/remote)
89104
include_directories(../../src/utility)
90105
include_directories(external/catch/v2.12.1/include)
91-
include_directories(external/fakeit/v2.0.5/include)
92106

93107
target_include_directories(TEST_TARGET_DISC_DEVICE PUBLIC include/test_discovered_device)
108+
target_include_directories(TEST_TARGET_ADVERTISING_DATA PUBLIC include/test_advertising_data)
94109

95110
##########################################################################
96111

97112
target_compile_definitions(TEST_TARGET_DISC_DEVICE PUBLIC FAKE_GAP)
113+
target_compile_definitions(TEST_TARGET_ADVERTISING_DATA PUBLIC FAKE_BLELOCALDEVICE)
98114

99115
##########################################################################
100116

@@ -105,3 +121,6 @@ add_custom_command(TARGET TEST_TARGET_UUID POST_BUILD
105121
add_custom_command(TARGET TEST_TARGET_DISC_DEVICE POST_BUILD
106122
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_DISC_DEVICE
107123
)
124+
add_custom_command(TARGET TEST_TARGET_ADVERTISING_DATA POST_BUILD
125+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_ADVERTISING_DATA
126+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#ifndef _FAKE_BLELOCALDEVICE_H_
21+
#define _FAKE_BLELOCALDEVICE_H_
22+
23+
#define private public
24+
#define protected public
25+
#include "BLELocalDevice.h"
26+
27+
class FakeBLELocalDevice : public BLELocalDevice {
28+
public:
29+
FakeBLELocalDevice();
30+
virtual ~FakeBLELocalDevice();
31+
32+
int advertise();
33+
};
34+
35+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "FakeBLELocalDevice.h"
21+
22+
FakeBLELocalDevice::FakeBLELocalDevice()
23+
{
24+
25+
}
26+
27+
FakeBLELocalDevice::~FakeBLELocalDevice()
28+
{
29+
30+
}
31+
32+
int FakeBLELocalDevice::advertise()
33+
{
34+
_advertisingData.updateData();
35+
_scanResponseData.updateData();
36+
return 1;
37+
}
38+
39+
FakeBLELocalDevice FakeBLEObj;
40+
BLELocalDevice& BLE = FakeBLEObj;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <catch.hpp>
21+
22+
#define private public
23+
#define protected public
24+
25+
#include "FakeBLELocalDevice.h"
26+
#include "BLEAdvertisingData.h"
27+
28+
TEST_CASE("Test flags override", "[ArduinoBLE::BLEAdvertisingData]")
29+
{
30+
// Mocking advertisement packet
31+
BLEAdvertisingData advData;
32+
// Expected results
33+
uint8_t defaultData[] = {0x02, BLEFieldFlags, 0x06};
34+
uint8_t goldenFlags[] = {0x02, 0x01, BLEFlagsBREDRNotSupported};
35+
36+
WHEN("Default options for flags")
37+
{
38+
BLE.advertise();
39+
REQUIRE( 0 == (memcmp(defaultData, BLE.getAdvertisingData().data(), sizeof(defaultData))) );
40+
REQUIRE( BLE.getScanResponseData().dataLength() == 0 );
41+
}
42+
43+
WHEN("Setting external advertising data which has flags")
44+
{
45+
advData.setFlags(BLEFlagsBREDRNotSupported);
46+
advData.updateData();
47+
BLE.setAdvertisingData(advData);
48+
BLE.advertise();
49+
REQUIRE( 3 == BLE.getAdvertisingData().dataLength());
50+
REQUIRE( 0 == (memcmp(goldenFlags, advData.data(), sizeof(goldenFlags))) );
51+
REQUIRE( 0 == (memcmp(goldenFlags, BLE.getAdvertisingData().data(), sizeof(goldenFlags))) );
52+
REQUIRE( 0 != (memcmp(defaultData, BLE.getAdvertisingData().data(), sizeof(defaultData))) );
53+
}
54+
55+
WHEN("Setting external advertising data without flags")
56+
{
57+
advData.clear();
58+
BLE.setAdvertisingData(advData);
59+
BLE.advertise();
60+
REQUIRE( 0 == (memcmp(defaultData, BLE.getAdvertisingData().data(), sizeof(defaultData))) );
61+
}
62+
63+
// Clear BLE advertising data
64+
advData.clear();
65+
BLE.setAdvertisingData(advData);
66+
}
67+
68+
TEST_CASE("Set default flags in an already full advertising data packet", "[ArduinoBLE::BLEAdvertisingData]")
69+
{
70+
BLEAdvertisingData advData;
71+
bool retVal;
72+
73+
const char* name = "tes";
74+
const uint8_t manufacturerData[24] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
75+
17, 18, 19, 20, 21, 22, 23};
76+
77+
uint8_t defaultFlags[3] = {0x02, 0x01, 0x06};
78+
79+
uint8_t goldenData[31] = {
80+
(sizeof(manufacturerData) + 1), BLEFieldManufacturerData, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
81+
17, 18, 19, 20, 21, 22, 23,
82+
((uint8_t)(strlen(name) + 1)), BLEFieldCompleteLocalName, 't', 'e', 's'
83+
};
84+
85+
WHEN("Test flags when advertising data is full")
86+
{
87+
retVal = advData.setLocalName("tes");
88+
retVal = advData.setManufacturerData(manufacturerData, sizeof(manufacturerData));
89+
90+
BLE.setAdvertisingData(advData);
91+
BLE.advertise();
92+
REQUIRE(0 != memcmp(defaultFlags, BLE.getAdvertisingData().data(), sizeof(defaultFlags)));
93+
REQUIRE(0 == memcmp(goldenData, BLE.getAdvertisingData().data(), sizeof(goldenData)));
94+
}
95+
96+
// Clear BLE advertising data
97+
advData.clear();
98+
BLE.setAdvertisingData(advData);
99+
}
100+
101+
TEST_CASE("BLE overwrite a full internal advertising data with an external built one", "[ArduinoBLE::BLEAdvertisingData]")
102+
{
103+
bool verify;
104+
BLEAdvertisingData advData;
105+
BLEAdvertisingData internalData;
106+
107+
WHEN("Copy empty external advertising data")
108+
{
109+
BLE.setLocalName("test");
110+
BLE.setAdvertisedServiceUuid("1818");
111+
BLE.advertise();
112+
//WARN("data length: " << BLE.getAdvertisingData().dataLength());
113+
verify = (BLE.getAdvertisingData().dataLength() == (3 + (2+2)));
114+
REQUIRE(verify);
115+
verify = (BLE.getScanResponseData().dataLength() == (4+2));
116+
REQUIRE(verify);
117+
118+
BLE.setAdvertisingData(advData);
119+
BLE.advertise();
120+
//WARN(BLE.getAdvertisingData().dataLength());
121+
verify = (BLE.getAdvertisingData().dataLength() == 3);
122+
REQUIRE(verify);
123+
124+
BLE.setScanResponseData(advData);
125+
BLE.advertise();
126+
verify = (BLE.getScanResponseData().dataLength() == 0);
127+
REQUIRE(verify);
128+
}
129+
130+
// Clear BLE advertising data
131+
advData.clear();
132+
BLE.setAdvertisingData(advData);
133+
}
134+
135+
TEST_CASE("BLE test raw data", "[ArduinoBLE::BLEAdvertisingData]")
136+
{
137+
BLEAdvertisingData advData;
138+
bool retVal;
139+
140+
WHEN("Set too large raw data")
141+
{
142+
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
143+
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
144+
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32 };
145+
advData.setRawData(data, sizeof(data));
146+
REQUIRE(!retVal);
147+
advData.clear();
148+
}
149+
150+
WHEN("Set correct raw data")
151+
{
152+
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
153+
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
154+
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};
155+
retVal = advData.setRawData(data, sizeof(data));
156+
REQUIRE(retVal);
157+
advData.updateData();
158+
REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) );
159+
advData.clear();
160+
}
161+
162+
WHEN("Hide other parameters by setting raw data")
163+
{
164+
uint8_t data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
165+
advData.setLocalName("test");
166+
advData.setRawData(data, sizeof(data));
167+
168+
advData.updateData();
169+
REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) );
170+
171+
advData.setLocalName("test");
172+
advData.updateData();
173+
REQUIRE( 0 == memcmp(data, advData.data(), sizeof(data)) );
174+
175+
advData.clear();
176+
advData.setLocalName("test");
177+
advData.updateData();
178+
REQUIRE( 0 != memcmp(data, advData.data(), sizeof(data)) );
179+
}
180+
181+
// Clear BLE advertising data
182+
advData.clear();
183+
BLE.setAdvertisingData(advData);
184+
}
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
This file is part of the ArduinoBLE library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include <catch.hpp>
21+
22+
#define private public
23+
#define protected public
24+
25+
#include "FakeBLELocalDevice.h"
26+
#include "BLEAdvertisingData.h"
27+
28+
TEST_CASE("Test local name setting", "[ArduinoBLE::BLEAdvertisingData]")
29+
{
30+
BLEAdvertisingData advData;
31+
int oldRemainingLength = advData.remainingLength();
32+
bool retVal;
33+
34+
WHEN("Set correct local name")
35+
{
36+
const char* name = "test";
37+
retVal = advData.setLocalName(name);
38+
REQUIRE(retVal);
39+
REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) );
40+
oldRemainingLength = advData.remainingLength();
41+
42+
advData.updateData();
43+
REQUIRE(advData.dataLength() == (strlen(name) + 2));
44+
}
45+
46+
WHEN("Set too long local name")
47+
{
48+
const char* name = "way too long local name (len 32)";
49+
retVal = advData.setLocalName(name);
50+
REQUIRE(!retVal);
51+
REQUIRE( oldRemainingLength == advData.remainingLength() );
52+
advData.updateData();
53+
REQUIRE( advData.dataLength() == (MAX_AD_DATA_LENGTH - oldRemainingLength) );
54+
}
55+
56+
WHEN("Overwrite local name with a name as long as max data length")
57+
{
58+
const char* name = "local name with full length ";
59+
retVal = advData.setLocalName(name);
60+
REQUIRE(retVal);
61+
REQUIRE( (strlen(name) + 2) == (oldRemainingLength - advData.remainingLength()) );
62+
oldRemainingLength = advData.remainingLength();
63+
64+
advData.updateData();
65+
REQUIRE(advData.dataLength() == (strlen(name) + 2));
66+
// advData should be full now
67+
REQUIRE( 0 == advData.remainingLength() );
68+
REQUIRE( 0 == advData.availableForWrite() );
69+
}
70+
71+
WHEN("Check consistency when setting the external advertising data")
72+
{
73+
auto goldenData = advData.data();
74+
BLE.setAdvertisingData(advData);
75+
BLE.advertise();
76+
REQUIRE( 0 == memcmp(goldenData, BLE.getAdvertisingData().data(), advData.dataLength()) );
77+
}
78+
79+
// Clear BLE advertising data
80+
advData.clear();
81+
BLE.setAdvertisingData(advData);
82+
}

0 commit comments

Comments
 (0)