Skip to content

Commit e2bf0c0

Browse files
committed
Add basic test case on characteristic permissions
1 parent 7322196 commit e2bf0c0

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

Diff for: extras/test/CMakeLists.txt

+21
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ set(TEST_TARGET_ADVERTISING_DATA_SRCS
9696
src/test_advertising_data/FakeBLELocalDevice.cpp
9797
)
9898

99+
set(TEST_TARGET_CHARACTERISTIC_SRCS
100+
# Test files
101+
${COMMON_TEST_SRCS}
102+
src/test_characteristic/test_permissions.cpp
103+
# DUT files
104+
${DUT_SRCS}
105+
# Fake classes files
106+
src/util/HCIFakeTransport.cpp
107+
src/test_advertising_data/FakeBLELocalDevice.cpp
108+
)
109+
99110
##########################################################################
100111

101112
set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage")
@@ -106,6 +117,7 @@ set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage")
106117
add_executable(TEST_TARGET_UUID ${TEST_TARGET_UUID_SRCS})
107118
add_executable(TEST_TARGET_DISC_DEVICE ${TEST_TARGET_DISC_DEVICE_SRCS})
108119
add_executable(TEST_TARGET_ADVERTISING_DATA ${TEST_TARGET_ADVERTISING_DATA_SRCS})
120+
add_executable(TEST_TARGET_CHARACTERISTIC_DATA ${TEST_TARGET_CHARACTERISTIC_SRCS})
109121

110122
##########################################################################
111123

@@ -118,11 +130,13 @@ include_directories(../../src/utility)
118130

119131
target_include_directories(TEST_TARGET_DISC_DEVICE PUBLIC include/test_discovered_device)
120132
target_include_directories(TEST_TARGET_ADVERTISING_DATA PUBLIC include/test_advertising_data)
133+
target_include_directories(TEST_TARGET_CHARACTERISTIC_DATA PUBLIC include/test_advertising_data)
121134

122135
##########################################################################
123136

124137
target_compile_definitions(TEST_TARGET_DISC_DEVICE PUBLIC FAKE_GAP)
125138
target_compile_definitions(TEST_TARGET_ADVERTISING_DATA PUBLIC FAKE_BLELOCALDEVICE)
139+
target_compile_definitions(TEST_TARGET_CHARACTERISTIC_DATA PUBLIC FAKE_BLELOCALDEVICE)
126140

127141
##########################################################################
128142

@@ -137,6 +151,13 @@ add_custom_command(TARGET TEST_TARGET_ADVERTISING_DATA POST_BUILD
137151
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_ADVERTISING_DATA
138152
)
139153

154+
add_custom_command(TARGET TEST_TARGET_CHARACTERISTIC_DATA POST_BUILD
155+
COMMAND ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/TEST_TARGET_CHARACTERISTIC_DATA
156+
)
157+
158+
##########################################################################
159+
140160
target_link_libraries( TEST_TARGET_UUID Catch2WithMain )
141161
target_link_libraries( TEST_TARGET_DISC_DEVICE Catch2WithMain )
142162
target_link_libraries( TEST_TARGET_ADVERTISING_DATA Catch2WithMain )
163+
target_link_libraries( TEST_TARGET_CHARACTERISTIC_DATA Catch2WithMain )
+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 <catch2/catch_test_macros.hpp>
21+
22+
#define private public
23+
#define protected public
24+
25+
#include "FakeBLELocalDevice.h"
26+
#include "BLEAdvertisingData.h"
27+
#include "BLETypedCharacteristics.h"
28+
#include "BLELocalCharacteristic.h"
29+
#include "BLEStringCharacteristic.h"
30+
#include "BLEProperty.h"
31+
#include <memory>
32+
33+
int property[] = {
34+
BLEBroadcast,
35+
BLERead,
36+
BLEWriteWithoutResponse,
37+
BLEWrite,
38+
BLENotify,
39+
BLEIndicate,
40+
BLEAuthSignedWrite,
41+
BLEExtProp,
42+
BLERead | BLEWrite | BLENotify
43+
};
44+
45+
int permission[] = {
46+
BLEEncryption,
47+
BLEAuthentication,
48+
BLEAuthorization,
49+
BLEEncryption | BLEAuthentication
50+
};
51+
52+
const char uuid[][31] = {
53+
"1 Bool",
54+
"2 Char",
55+
"3 UnsignedChar",
56+
"4 Byte",
57+
"5 Short",
58+
"6 UnsignedShort",
59+
"7 Word",
60+
"8 Int",
61+
"9 UnsignedInt",
62+
"A Long",
63+
"B UnsignedLong",
64+
"C Float",
65+
"D Double",
66+
"E String"
67+
};
68+
69+
std::unique_ptr<BLECharacteristic> createCharacteristic(const char* uuid, unsigned int properties)
70+
{
71+
switch(uuid[0])
72+
{
73+
case '1':
74+
return std::unique_ptr<BLECharacteristic>(new BLEBoolCharacteristic(uuid, properties));
75+
case '2':
76+
return std::unique_ptr<BLECharacteristic>(new BLECharCharacteristic(uuid, properties));
77+
case '3':
78+
return std::unique_ptr<BLECharacteristic>(new BLEUnsignedCharCharacteristic(uuid, properties));
79+
case '4':
80+
return std::unique_ptr<BLECharacteristic>(new BLEByteCharacteristic(uuid, properties));
81+
case '5':
82+
return std::unique_ptr<BLECharacteristic>(new BLEShortCharacteristic(uuid, properties));
83+
case '6':
84+
return std::unique_ptr<BLECharacteristic>(new BLEUnsignedShortCharacteristic(uuid, properties));
85+
case '7':
86+
return std::unique_ptr<BLECharacteristic>(new BLEWordCharacteristic(uuid, properties));
87+
case '8':
88+
return std::unique_ptr<BLECharacteristic>(new BLEIntCharacteristic(uuid, properties));
89+
case '9':
90+
return std::unique_ptr<BLECharacteristic>(new BLEUnsignedIntCharacteristic(uuid, properties));
91+
case 'A':
92+
return std::unique_ptr<BLECharacteristic>(new BLELongCharacteristic(uuid, properties));
93+
case 'B':
94+
return std::unique_ptr<BLECharacteristic>(new BLEUnsignedLongCharacteristic(uuid, properties));
95+
case 'C':
96+
return std::unique_ptr<BLECharacteristic>(new BLEFloatCharacteristic(uuid, properties));
97+
case 'D':
98+
return std::unique_ptr<BLECharacteristic>(new BLEDoubleCharacteristic(uuid, properties));
99+
case 'E':
100+
return std::unique_ptr<BLECharacteristic>(new BLEStringCharacteristic(uuid, properties, 2));
101+
default:
102+
break;
103+
}
104+
return nullptr;
105+
}
106+
107+
TEST_CASE("Test characteristic properties and permissions", "[ArduinoBLE::BLECharacteristic]")
108+
{
109+
WHEN("Create a characteristic")
110+
{
111+
for(int i = 0; i < sizeof(property)/sizeof(int); i++)
112+
{
113+
for(int j = 0; j < sizeof(permission)/sizeof(int); j++)
114+
{
115+
for(int k = 0; k < 14; k++)
116+
{
117+
std::unique_ptr<BLECharacteristic> ptr = createCharacteristic(uuid[k], property[i] | permission[j]);
118+
REQUIRE(ptr != nullptr);
119+
REQUIRE(ptr->properties() == (property[i]));
120+
BLELocalCharacteristic * local = ptr->local();
121+
REQUIRE(local->permissions() == (permission[j] >> 8));
122+
}
123+
}
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)