-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathtest_local_name.cpp
95 lines (80 loc) · 3.1 KB
/
test_local_name.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
This file is part of the ArduinoBLE library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <catch.hpp>
#define private public
#define protected public
#include "FakeBLELocalDevice.h"
#include "BLEAdvertisingData.h"
TEST_CASE("Test local name setting", "[ArduinoBLE::BLEAdvertisingData]")
{
BLEAdvertisingData advData;
int oldRemainingLength = advData.remainingLength();
bool retVal;
WHEN("Set correct local name")
{
const char* name = "test";
retVal = advData.setLocalName(name);
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();
REQUIRE(advData.dataLength() == (strlen(name) + 2));
}
WHEN("Set too long local name")
{
const char* name = "way too long local name (len 32)";
retVal = advData.setLocalName(name);
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);
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() );
}
}
WHEN("Check consistency when setting the external advertising data")
{
const auto goldenData = advData.data();
BLE.setAdvertisingData(advData);
BLE.advertise();
REQUIRE( 0 == memcmp(goldenData, BLE.getAdvertisingData().data(), advData.dataLength()) );
}
// Clear BLE advertising data
advData.clear();
BLE.setAdvertisingData(advData);
}