-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathtcpsocket_recv_100k.cpp
186 lines (160 loc) · 5.48 KB
/
tcpsocket_recv_100k.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2018, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
#include "TCPSocket.h"
#include "greentea-client/test_env.h"
#include "unity/unity.h"
#include "utest.h"
#include "tcp_tests.h"
using namespace utest::v1;
namespace {
static const int SIGNAL_SIGIO = 0x1;
static const int SIGIO_TIMEOUT = 20000; //[ms]
}
static nsapi_error_t _tcpsocket_connect_to_chargen_srv(TCPSocket &sock)
{
SocketAddress tcp_addr;
NetworkInterface::get_default_instance()->gethostbyname(MBED_CONF_APP_ECHO_SERVER_ADDR, &tcp_addr);
tcp_addr.set_port(19);
nsapi_error_t err = sock.open(NetworkInterface::get_default_instance());
if (err != NSAPI_ERROR_OK) {
return err;
}
return sock.connect(tcp_addr);
}
/** Generate RFC 864 example pattern.
*
* Pattern is 72 character lines of the ASCII printing characters ending with "\r\n".
* There are 95 printing characters in the ASCII character set.
* Example: `nc echo.mbedcloudtesting.com 19 | dd bs=1 count=222`
* !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg
* !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
* "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghi
*
* NOTE: Pattern starts with space, not !
*
* \param offset Start pattern from offset
* \param len Length of pattern to generate.
*/
static void generate_RFC_864_pattern(size_t offset, uint8_t *buf, size_t len, bool is_xinetd)
{
const int row_size = 74; // Number of chars in single row
const int row_count = 95; // Number of rows in pattern after which pattern start from beginning
const int chars_scope = is_xinetd ? 93 : 95; // Number of chars from ASCII table used in pattern
const char first_char = is_xinetd ? '!' : ' '; // First char from ASCII table used in pattern
while (len--) {
if (offset % row_size == (row_size - 2)) {
*buf++ = '\r';
} else if (offset % row_size == (row_size - 1)) {
*buf++ = '\n';
} else {
*buf++ = first_char + (offset % row_size + ((offset / row_size) % row_count)) % chars_scope;
}
offset++;
}
}
static void check_RFC_864_pattern(void *rx_buff, const size_t len, const size_t offset)
{
static bool is_xinetd = false;
void *ref_buff = malloc(len);
TEST_ASSERT_NOT_NULL(ref_buff);
if (offset == 0) {
is_xinetd = ((uint8_t *)rx_buff)[0] == '!';
}
generate_RFC_864_pattern(offset, (uint8_t *)ref_buff, len, is_xinetd);
bool match = memcmp(ref_buff, rx_buff, len) == 0;
free(ref_buff);
TEST_ASSERT(match);
}
void rcv_n_chk_against_rfc864_pattern(TCPSocket &sock)
{
static const size_t total_size = 1024 * 100;
static const size_t buff_size = 1220;
uint8_t buff[buff_size];
size_t recvd_size = 0;
Timer timer;
timer.start();
// Verify received data
while (recvd_size < total_size) {
int rd = sock.recv(buff, buff_size);
TEST_ASSERT(rd > 0);
if (rd < 0) {
break;
}
check_RFC_864_pattern(buff, rd, recvd_size);
recvd_size += rd;
}
timer.stop();
greentea_serial->printf("MBED: Time taken: %fs\n", timer.read());
}
void TCPSOCKET_RECV_100K()
{
TCPSocket sock;
if (_tcpsocket_connect_to_chargen_srv(sock) != NSAPI_ERROR_OK) {
TEST_FAIL();
return;
}
rcv_n_chk_against_rfc864_pattern(sock);
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}
void rcv_n_chk_against_rfc864_pattern_nonblock(TCPSocket &sock)
{
static const size_t total_size = 1024 * 100;
static const size_t buff_size = 1220;
uint8_t buff[buff_size];
size_t recvd_size = 0;
int time_allotted = split2half_rmng_tcp_test_time(); // [s]
Timer timer;
timer.start();
// Verify received data
while (recvd_size < total_size) {
int rd = sock.recv(buff, buff_size);
TEST_ASSERT(rd > 0 || rd == NSAPI_ERROR_WOULD_BLOCK);
if (rd > 0) {
check_RFC_864_pattern(buff, rd, recvd_size);
recvd_size += rd;
} else if (rd == NSAPI_ERROR_WOULD_BLOCK) {
if (timer.read() >= time_allotted) {
TEST_FAIL();
break;
}
TEST_ASSERT_NOT_EQUAL(osEventTimeout, osSignalWait(SIGNAL_SIGIO, SIGIO_TIMEOUT).status);
} else {
TEST_FAIL();
break;
}
}
timer.stop();
greentea_serial->printf("MBED: Time taken: %fs\n", timer.read());
}
static void _sigio_handler(osThreadId id)
{
osSignalSet(id, SIGNAL_SIGIO);
}
void TCPSOCKET_RECV_100K_NONBLOCK()
{
TCPSocket sock;
nsapi_error_t err = _tcpsocket_connect_to_chargen_srv(sock);
if (err != NSAPI_ERROR_OK) {
TEST_FAIL();
return;
}
sock.set_blocking(false);
sock.sigio(callback(_sigio_handler, ThisThread::get_id()));
rcv_n_chk_against_rfc864_pattern_nonblock(sock);
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.close());
}