Skip to content

Commit d2c0591

Browse files
authored
Merge pull request #11276 from ARMmbed/sip-workshop
Add integration tests
2 parents b27aa8c + 701e1a7 commit d2c0591

File tree

13 files changed

+21865
-8
lines changed

13 files changed

+21865
-8
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2006-2019 ARM Limited
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
#define ETHERNET 1
21+
#define WIFI 2
22+
#define MESH 3
23+
#define CELLULAR 4
24+
25+
#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE)
26+
#error [NOT_SUPPORTED] No network interface found on this target.
27+
#endif
28+
29+
#if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == ETHERNET
30+
#define TEST_NETWORK_TYPE "Ethernet"
31+
#elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI
32+
#define TEST_NETWORK_TYPE "WiFi"
33+
#elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == MESH
34+
#define TEST_NETWORK_TYPE "Mesh"
35+
#elif MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == CELLULAR
36+
#define TEST_NETWORK_TYPE "Cellular"
37+
#else
38+
#error [NOT_SUPPORTED] Either WiFi, Ethernet or Cellular network interface need to be enabled
39+
#endif
40+
41+
#define FS_FAT 1
42+
#define FS_LFS 2
43+
44+
#if COMPONENT_SPIF
45+
#define TEST_BLOCK_DEVICE_TYPE "SPIF"
46+
#elif COMPONENT_QSPIF
47+
#define TEST_BLOCK_DEVICE_TYPE "QSPIF"
48+
#elif COMPONENT_DATAFLASH
49+
#define TEST_BLOCK_DEVICE_TYPE "DATAFLASH"
50+
#elif COMPONENT_SD
51+
#define TEST_BLOCK_DEVICE_TYPE "SD"
52+
#define TEST_USE_FILESYSTEM FS_FAT
53+
#elif COMPONENT_FLASHIAP
54+
#define TEST_BLOCK_DEVICE_TYPE "FLASHIAP"
55+
#elif COMPONENT_SDIO
56+
#define TEST_BLOCK_DEVICE_TYPE "SDIO"
57+
#elif COMPONENT_NUSD
58+
#define TEST_BLOCK_DEVICE_TYPE "NUSD"
59+
#define TEST_USE_FILESYSTEM FS_FAT
60+
#else
61+
#define TEST_BLOCK_DEVICE_TYPE "UNKNOWN"
62+
#endif
63+
64+
#if !defined(TEST_USE_FILESYSTEM)
65+
#define TEST_USE_FILESYSTEM FS_LFS
66+
#endif
67+
68+
#if TEST_USE_FILESYSTEM == FS_FAT
69+
#define TEST_FILESYSTEM_TYPE "FAT"
70+
#elif TEST_USE_FILESYSTEM == FS_LFS
71+
#define TEST_FILESYSTEM_TYPE "LFS"
72+
#else
73+
#define TEST_FILESYSTEM_TYPE "UNKNOWN"
74+
#endif
75+
76+
#define TEST_MEMORY_SIZE_10K 10240
77+
#define TEST_MEMORY_SIZE_20K 20480
78+
#define TEST_MEMORY_SIZE_40K 40960
79+
#define TEST_MEMORY_SIZE_60K 61440
80+
#define TEST_MEMORY_SIZE_80K 81920
81+
#define TEST_MEMORY_SIZE_100K 102400
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2006-2019 ARM Limited
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
/*
21+
* Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
22+
*/
23+
24+
#include "mbed.h"
25+
#include "unity/unity.h"
26+
#include "greentea-client/test_env.h"
27+
#include <string>
28+
29+
#define MAX_THREADS 5
30+
31+
#define MAX_RETRIES 3
32+
33+
#ifdef MBED_CONF_DOWNLOAD_TEST_URL_HOST
34+
const char dl_host[] = MBED_CONF_DOWNLOAD_TEST_URL_HOST;
35+
#else
36+
const char dl_host[] = "armmbed.github.io";
37+
#endif
38+
#ifdef MBED_CONF_DOWNLOAD_TEST_URL_PATH
39+
const char dl_path[] = MBED_CONF_DOWNLOAD_TEST_URL_PATH;
40+
#else
41+
const char dl_path[] = "/mbed-test-files/sample.txt";
42+
#endif
43+
44+
const char req_template[] = "GET %s HTTP/1.1\nHost: %s\n\n";
45+
46+
#define REQ_BUF_SIZE 256
47+
#define RECV_BUF_SIZE 1024
48+
49+
static char g_request_buffer[MAX_THREADS][REQ_BUF_SIZE]; // the default request is 65bytes long.
50+
static char g_receive_buffer[MAX_THREADS * RECV_BUF_SIZE];
51+
52+
static volatile bool event_fired[MAX_THREADS] = { };
53+
54+
static void socket_event_0(void)
55+
{
56+
event_fired[0] = true;
57+
}
58+
static void socket_event_1(void)
59+
{
60+
event_fired[1] = true;
61+
}
62+
static void socket_event_2(void)
63+
{
64+
event_fired[2] = true;
65+
}
66+
static void socket_event_3(void)
67+
{
68+
event_fired[3] = true;
69+
}
70+
static void socket_event_4(void)
71+
{
72+
event_fired[4] = true;
73+
}
74+
75+
76+
size_t download_test(NetworkInterface *interface, const unsigned char *data, size_t data_length, size_t buff_size, uint32_t thread_id)
77+
{
78+
int result = -1;
79+
80+
TEST_ASSERT_MESSAGE(MAX_THREADS > thread_id, "Unsupported thread ID");
81+
TEST_ASSERT_MESSAGE((MAX_THREADS * RECV_BUF_SIZE) >= buff_size, "Cannot test with the requested buffer size");
82+
83+
/* setup TCP socket */
84+
TCPSocket tcpsocket(interface);
85+
86+
for (int tries = 0; tries < MAX_RETRIES; tries++) {
87+
result = tcpsocket.connect(dl_host, 80);
88+
TEST_ASSERT_MESSAGE(result != NSAPI_ERROR_NO_SOCKET, "out of sockets");
89+
90+
if (result == 0) {
91+
break;
92+
}
93+
ThisThread::sleep_for(1000);
94+
printf("[NET-%d] Connection failed. Retry %d of %d\r\n", thread_id, tries, MAX_RETRIES);
95+
}
96+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "failed to connect");
97+
98+
if (thread_id == 0) {
99+
// technically this is non-threaded mode
100+
tcpsocket.sigio(socket_event_0);
101+
} else if (thread_id == 1) {
102+
tcpsocket.sigio(socket_event_1);
103+
} else if (thread_id == 2) {
104+
tcpsocket.sigio(socket_event_2);
105+
} else if (thread_id == 3) {
106+
tcpsocket.sigio(socket_event_3);
107+
} else if (thread_id == 4) {
108+
tcpsocket.sigio(socket_event_4);
109+
} else {
110+
TEST_ASSERT_MESSAGE(0, "wrong thread id");
111+
}
112+
printf("[NET-%d] Registered socket callback function\r\n", thread_id);
113+
event_fired[thread_id] = false;
114+
115+
/* setup request */
116+
char *request = g_request_buffer[thread_id];
117+
118+
/* construct request */
119+
size_t req_len = snprintf(request, REQ_BUF_SIZE - 1, req_template, dl_path, dl_host);
120+
request[req_len] = 0;
121+
printf("[NET-%d] Request header (%u): %s\r\n", thread_id, req_len, request);
122+
123+
/* send request to server */
124+
result = tcpsocket.send(request, req_len);
125+
TEST_ASSERT_EQUAL_INT_MESSAGE(req_len, result, "failed to send");
126+
127+
/* read response */
128+
char *receive_buffer = &g_receive_buffer[thread_id * RECV_BUF_SIZE];
129+
130+
tcpsocket.set_blocking(false);
131+
printf("[NET-%d] Non-blocking socket mode set\r\n", thread_id);
132+
133+
size_t received_bytes = 0;
134+
int body_index = -1;
135+
136+
float speed;
137+
float percent;
138+
uint32_t time_left;
139+
Timer timer;
140+
timer.start();
141+
142+
/* loop until all expected bytes have been received */
143+
while (received_bytes < data_length) {
144+
/* wait for async event */
145+
while (!event_fired[thread_id]) {
146+
if (thread_id > 0) {
147+
ThisThread::yield();
148+
}
149+
}
150+
event_fired[thread_id] = false;
151+
152+
/* loop until all data has been read from socket */
153+
do {
154+
result = tcpsocket.recv(receive_buffer, buff_size);
155+
TEST_ASSERT_MESSAGE((result == NSAPI_ERROR_WOULD_BLOCK) || (result >= 0),
156+
"failed to read socket");
157+
158+
if (result > 0) {
159+
/* skip HTTP header */
160+
if (body_index < 0) {
161+
/* note that there are no required Response headers and their length may greatly vary */
162+
std::string header(receive_buffer, result);
163+
body_index = header.find("\r\n\r\n");
164+
if (body_index < 0) {
165+
continue;
166+
} else {
167+
printf("[NET-%d] Found body index: %d\r\n", thread_id, body_index);
168+
169+
/* remove header before comparison */
170+
memmove(receive_buffer, &receive_buffer[body_index + 4], result - body_index - 4);
171+
172+
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(data, receive_buffer, result - body_index - 4,
173+
"character mismatch in header");
174+
175+
received_bytes += (result - body_index - 4);
176+
}
177+
} else {
178+
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(&data[received_bytes], receive_buffer, result,
179+
"character mismatch in body");
180+
181+
received_bytes += result;
182+
}
183+
184+
speed = float(received_bytes) / timer.read();
185+
percent = float(received_bytes) * 100 / float(data_length);
186+
time_left = (data_length - received_bytes) / speed;
187+
printf("[NET-%d] Received bytes: %u, (%.2f%%, %.2fKB/s, ETA: %02d:%02d:%02d)\r\n",
188+
thread_id, received_bytes, percent, speed / 1024,
189+
time_left / 3600, (time_left / 60) % 60, time_left % 60);
190+
}
191+
} while ((result > 0) && (received_bytes < data_length));
192+
}
193+
194+
TEST_ASSERT_MESSAGE(body_index >= 0, "failed to find body");
195+
196+
timer.stop();
197+
float f_received_bytes = float(received_bytes);
198+
printf("[NET-%d] Downloaded: %.2fKB (%.2fKB/s, %.2f secs)\r\n", thread_id,
199+
f_received_bytes / 1024.,
200+
f_received_bytes / (timer.read() * 1024.),
201+
timer.read());
202+
203+
return received_bytes;
204+
}
205+
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
/* mbed Microcontroller Library
2-
* Copyright (c) 2017 ARM Limited
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2006-2019 ARM Limited
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
36
*
47
* Licensed under the Apache License, Version 2.0 (the "License");
58
* you may not use this file except in compliance with the License.
@@ -13,10 +16,10 @@
1316
* See the License for the specific language governing permissions and
1417
* limitations under the License.
1518
*/
16-
#include "test_env.h"
1719

18-
int main()
19-
{
20-
GREENTEA_SETUP(15, "default_auto");
21-
GREENTEA_TESTSUITE_RESULT(true);
22-
}
20+
/*
21+
* Based on mbed-stress-test by Marcus Chang @ Arm Mbed - http://github.com/ARMmbed/mbed-stress-test
22+
*/
23+
24+
size_t download_test(NetworkInterface *interface, const unsigned char *data, size_t data_length, size_t buff_size, uint32_t thread_id = 0);
25+

0 commit comments

Comments
 (0)