Skip to content

Commit 8ee2432

Browse files
Marcelo SalazarMarcelo Salazar
authored andcommitted
Add tests (not yet working)
1 parent 2c1c4a8 commit 8ee2432

File tree

19 files changed

+184166
-0
lines changed

19 files changed

+184166
-0
lines changed

TESTS/COMMON/alice.h

Lines changed: 16781 additions & 0 deletions
Large diffs are not rendered by default.

TESTS/COMMON/alice.txt

Lines changed: 3736 additions & 0 deletions
Large diffs are not rendered by default.

TESTS/COMMON/bogdan.h

Lines changed: 72293 additions & 0 deletions
Large diffs are not rendered by default.

TESTS/COMMON/bogdan.txt

Lines changed: 15974 additions & 0 deletions
Large diffs are not rendered by default.

TESTS/COMMON/common_defines_test.h

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-2018 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

TESTS/COMMON/download_test.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2006-2018 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/alice.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) { event_fired[0] = true; }
55+
static void socket_event_1(void) { event_fired[1] = true; }
56+
static void socket_event_2(void) { event_fired[2] = true; }
57+
static void socket_event_3(void) { event_fired[3] = true; }
58+
static void socket_event_4(void) { event_fired[4] = true; }
59+
60+
61+
size_t download_test(NetworkInterface* interface, const unsigned char* data, size_t data_length, size_t buff_size, uint32_t thread_id) {
62+
int result = -1;
63+
64+
TEST_ASSERT_MESSAGE(MAX_THREADS > thread_id, "Unsupported thread ID");
65+
TEST_ASSERT_MESSAGE((MAX_THREADS * RECV_BUF_SIZE) >= buff_size, "Cannot test with the requested buffer size");
66+
67+
/* setup TCP socket */
68+
TCPSocket tcpsocket(interface);
69+
70+
for (int tries = 0; tries < MAX_RETRIES; tries++) {
71+
result = tcpsocket.connect(dl_host, 80);
72+
TEST_ASSERT_MESSAGE(result != NSAPI_ERROR_NO_SOCKET, "out of sockets");
73+
74+
if (result == 0) {
75+
break;
76+
}
77+
ThisThread::sleep_for(1000);
78+
printf("[NET-%d] Connection failed. Retry %d of %d\r\n", thread_id, tries, MAX_RETRIES);
79+
}
80+
TEST_ASSERT_EQUAL_INT_MESSAGE(0, result, "failed to connect");
81+
82+
if (thread_id == 0) {
83+
// technically this is non-threaded mode
84+
tcpsocket.sigio(socket_event_0);
85+
} else if (thread_id == 1) {
86+
tcpsocket.sigio(socket_event_1);
87+
} else if (thread_id == 2) {
88+
tcpsocket.sigio(socket_event_2);
89+
} else if (thread_id == 3) {
90+
tcpsocket.sigio(socket_event_3);
91+
} else if (thread_id == 4) {
92+
tcpsocket.sigio(socket_event_4);
93+
} else {
94+
TEST_ASSERT_MESSAGE(0, "wrong thread id");
95+
}
96+
printf("[NET-%d] Registered socket callback function\r\n", thread_id);
97+
event_fired[thread_id] = false;
98+
99+
/* setup request */
100+
char *request = g_request_buffer[thread_id];
101+
102+
/* construct request */
103+
size_t req_len = snprintf(request, REQ_BUF_SIZE-1, req_template, dl_path, dl_host);
104+
request[req_len] = 0;
105+
printf("[NET-%d] Request header (%u): %s\r\n", thread_id, req_len, request);
106+
107+
/* send request to server */
108+
result = tcpsocket.send(request, req_len);
109+
TEST_ASSERT_EQUAL_INT_MESSAGE(req_len, result, "failed to send");
110+
111+
/* read response */
112+
char* receive_buffer = &g_receive_buffer[thread_id * RECV_BUF_SIZE];
113+
114+
tcpsocket.set_blocking(false);
115+
printf("[NET-%d] Non-blocking socket mode set\r\n", thread_id);
116+
117+
size_t received_bytes = 0;
118+
int body_index = -1;
119+
120+
float speed;
121+
float percent;
122+
uint32_t time_left;
123+
Timer timer;
124+
timer.start();
125+
126+
/* loop until all expected bytes have been received */
127+
while (received_bytes < data_length) {
128+
/* wait for async event */
129+
while(!event_fired[thread_id]) {
130+
if (thread_id > 0) {
131+
ThisThread::yield();
132+
}
133+
}
134+
event_fired[thread_id] = false;
135+
136+
/* loop until all data has been read from socket */
137+
do {
138+
result = tcpsocket.recv(receive_buffer, buff_size);
139+
TEST_ASSERT_MESSAGE((result == NSAPI_ERROR_WOULD_BLOCK) || (result >= 0),
140+
"failed to read socket");
141+
142+
if (result > 0) {
143+
/* skip HTTP header */
144+
if (body_index < 0) {
145+
/* note that there are no required Response headers and their length may greatly vary */
146+
std::string header(receive_buffer, result);
147+
body_index = header.find("\r\n\r\n");
148+
if (body_index < 0) {
149+
continue;
150+
} else {
151+
printf("[NET-%d] Found body index: %d\r\n", thread_id, body_index);
152+
153+
/* remove header before comparison */
154+
memmove(receive_buffer, &receive_buffer[body_index + 4], result - body_index - 4);
155+
156+
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(data, receive_buffer, result - body_index - 4,
157+
"character mismatch in header");
158+
159+
received_bytes += (result - body_index - 4);
160+
}
161+
} else {
162+
TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(&data[received_bytes], receive_buffer, result,
163+
"character mismatch in body");
164+
165+
received_bytes += result;
166+
}
167+
168+
speed = float(received_bytes) / timer.read();
169+
percent = float(received_bytes) * 100 / float(data_length);
170+
time_left = (data_length - received_bytes) / speed;
171+
printf("[NET-%d] Received bytes: %u, (%.2f%%, %.2fKB/s, ETA: %02d:%02d:%02d)\r\n",
172+
thread_id, received_bytes, percent, speed / 1024,
173+
time_left / 3600, (time_left / 60) % 60, time_left % 60);
174+
}
175+
}
176+
while ((result > 0) && (received_bytes < data_length));
177+
}
178+
179+
TEST_ASSERT_MESSAGE(body_index >= 0, "failed to find body");
180+
181+
timer.stop();
182+
float f_received_bytes = float(received_bytes);
183+
printf("[NET-%d] Downloaded: %.2fKB (%.2fKB/s, %.2f secs)\r\n", thread_id,
184+
f_received_bytes / 1024.,
185+
f_received_bytes / (timer.read() * 1024.),
186+
timer.read());
187+
188+
return received_bytes;
189+
}
190+

TESTS/COMMON/download_test.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* mbed Microcontroller Library
3+
* Copyright (c) 2006-2018 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+
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)