Skip to content

Commit 7d871bf

Browse files
author
Cruz Monrreal
authored
Merge pull request #7879 from jarlamsa/synchronous_dns_tests
Add tests for synchronous dns
2 parents e649bea + 0c758b9 commit 7d871bf

File tree

7 files changed

+242
-2
lines changed

7 files changed

+242
-2
lines changed

TESTS/netsocket/dns/asynchronous_dns_cache.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ void ASYNCHRONOUS_DNS_CACHE()
5656
int delay_ms = (ticker_us - started_us) / 1000;
5757

5858
static int delay_first = delay_ms / 2;
59+
printf("Delays: first: %i, delay_ms: %i\n", delay_first, delay_ms);
5960
// Check that cached accesses are at least twice as fast as the first one
60-
TEST_ASSERT_FALSE(i != 0 && delay_ms > delay_first);
61+
TEST_ASSERT_TRUE(i == 0 || delay_ms <= delay_first);
6162

6263
printf("DNS: query \"%s\" => \"%s\", time %i ms\n",
6364
dns_test_hosts[0], data.addr.get_ip_address(), delay_ms);

TESTS/netsocket/dns/dns_tests.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ extern const char dns_test_hosts_second[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TE
5959
NetworkInterface *get_interface();
6060
void hostbyname_cb(void *data, nsapi_error_t result, SocketAddress *address);
6161
void do_asynchronous_gethostbyname(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op_count, int *exp_ok, int *exp_no_mem, int *exp_dns_failure, int *exp_timeout);
62+
void do_gethostbyname(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op_count, int *exp_ok, int *exp_no_mem, int *exp_dns_failure, int *exp_timeout);
6263

6364
/*
6465
* Test cases
@@ -73,5 +74,8 @@ void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE();
7374
void ASYNCHRONOUS_DNS_INVALID_HOST();
7475
void ASYNCHRONOUS_DNS_TIMEOUTS();
7576
void ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT();
76-
77+
void SYNCHRONOUS_DNS();
78+
void SYNCHRONOUS_DNS_MULTIPLE();
79+
void SYNCHRONOUS_DNS_CACHE();
80+
void SYNCHRONOUS_DNS_INVALID();
7781
#endif

TESTS/netsocket/dns/main.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,39 @@ void do_asynchronous_gethostbyname(const char hosts[][DNS_TEST_HOST_LEN], unsign
107107
delete[] data;
108108
}
109109

110+
void do_gethostbyname(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op_count, int *exp_ok, int *exp_no_mem, int *exp_dns_failure, int *exp_timeout)
111+
{
112+
// Verify that there is enough hosts in the host list
113+
TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM)
114+
115+
// Reset counters
116+
(*exp_ok) = 0;
117+
(*exp_no_mem) = 0;
118+
(*exp_dns_failure) = 0;
119+
(*exp_timeout) = 0;
120+
121+
for (unsigned int i = 0; i < op_count; i++) {
122+
SocketAddress address;
123+
nsapi_error_t err = net->gethostbyname(hosts[i], &address);
124+
125+
TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_NO_MEMORY || err == NSAPI_ERROR_DNS_FAILURE || err == NSAPI_ERROR_TIMEOUT);
126+
if (err == NSAPI_ERROR_OK) {
127+
(*exp_ok)++;
128+
printf("DNS: query \"%s\" => \"%s\"\n",
129+
hosts[i], address.get_ip_address());
130+
} else if (err == NSAPI_ERROR_DNS_FAILURE) {
131+
(*exp_dns_failure)++;
132+
printf("DNS: query \"%s\" => DNS failure\n", hosts[i]);
133+
} else if (err == NSAPI_ERROR_TIMEOUT) {
134+
(*exp_timeout)++;
135+
printf("DNS: query \"%s\" => timeout\n", hosts[i]);
136+
} else if (err == NSAPI_ERROR_NO_MEMORY) {
137+
(*exp_no_mem)++;
138+
printf("DNS: query \"%s\" => no memory\n", hosts[i]);
139+
}
140+
}
141+
}
142+
110143
NetworkInterface *get_interface()
111144
{
112145
return net;
@@ -143,6 +176,10 @@ Case cases[] = {
143176
#ifdef MBED_EXTENDED_TESTS
144177
Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT", ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT),
145178
#endif
179+
Case("SYNCHRONOUS_DNS", SYNCHRONOUS_DNS),
180+
Case("SYNCHRONOUS_DNS_MULTIPLE", SYNCHRONOUS_DNS_MULTIPLE),
181+
Case("SYNCHRONOUS_DNS_CACHE", SYNCHRONOUS_DNS_CACHE),
182+
Case("SYNCHRONOUS_DNS_INVALID", SYNCHRONOUS_DNS_INVALID),
146183
};
147184

148185
Specification specification(test_setup, cases);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace {
27+
int result_ok;
28+
int result_no_mem;
29+
int result_dns_failure;
30+
int result_exp_timeout;
31+
}
32+
33+
void SYNCHRONOUS_DNS()
34+
{
35+
do_gethostbyname(dns_test_hosts, 1, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);
36+
37+
TEST_ASSERT(result_ok == 1);
38+
TEST_ASSERT(result_no_mem == 0);
39+
TEST_ASSERT(result_dns_failure == 0);
40+
TEST_ASSERT(result_exp_timeout == 0);
41+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace {
27+
int ticker_us = 0;
28+
}
29+
30+
static void test_dns_query_ticker(void)
31+
{
32+
ticker_us += 100;
33+
}
34+
35+
void SYNCHRONOUS_DNS_CACHE()
36+
{
37+
rtos::Semaphore semaphore;
38+
39+
Ticker ticker;
40+
ticker.attach_us(&test_dns_query_ticker, 100);
41+
42+
for (unsigned int i = 0; i < 5; i++) {
43+
SocketAddress address;
44+
int started_us = ticker_us;
45+
nsapi_error_t err = get_interface()->gethostbyname(dns_test_hosts_second[0], &address);
46+
47+
int delay_ms = (ticker_us - started_us) / 1000;
48+
49+
static int delay_first = delay_ms / 2;
50+
// Check that cached accesses are at least twice as fast as the first one
51+
TEST_ASSERT_TRUE(i == 0 || delay_ms <= delay_first);
52+
53+
printf("DNS: query \"%s\" => \"%s\", time %i ms\n",
54+
dns_test_hosts_second[0], address.get_ip_address(), delay_ms);
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
25+
using namespace utest::v1;
26+
27+
namespace {
28+
int result_ok;
29+
int result_no_mem;
30+
int result_dns_failure;
31+
int result_exp_timeout;
32+
}
33+
34+
void SYNCHRONOUS_DNS_INVALID()
35+
{
36+
//Ensure that there are no addressess in cache
37+
do_gethostbyname(dns_test_hosts_second, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);
38+
39+
char dns_test_hosts_new[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
40+
memcpy(dns_test_hosts_new, dns_test_hosts, sizeof(dns_test_hosts_new));
41+
42+
int expected_failures = 0;
43+
int expected_successes = 0;
44+
45+
for (int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
46+
if ((i % 2) == 0) {
47+
expected_failures++;
48+
strcat(&(dns_test_hosts_new[i][0]), "_invalid");
49+
} else {
50+
expected_successes++;
51+
}
52+
}
53+
54+
do_gethostbyname(dns_test_hosts_new, MBED_CONF_APP_DNS_TEST_HOSTS_NUM, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);
55+
56+
TEST_ASSERT(result_ok == expected_successes);
57+
TEST_ASSERT(result_no_mem == 0);
58+
TEST_ASSERT(result_dns_failure == expected_failures);
59+
TEST_ASSERT(result_exp_timeout == 0);
60+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (c) 2018, ARM Limited, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "mbed.h"
19+
#include "greentea-client/test_env.h"
20+
#include "unity.h"
21+
#include "utest.h"
22+
#include "dns_tests.h"
23+
24+
using namespace utest::v1;
25+
26+
namespace {
27+
int result_ok;
28+
int result_no_mem;
29+
int result_dns_failure;
30+
int result_exp_timeout;
31+
}
32+
33+
void SYNCHRONOUS_DNS_MULTIPLE()
34+
{
35+
do_gethostbyname(dns_test_hosts, MBED_CONF_APP_DNS_TEST_HOSTS_NUM, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);
36+
37+
TEST_ASSERT(result_ok == MBED_CONF_APP_DNS_TEST_HOSTS_NUM);
38+
TEST_ASSERT(result_no_mem == 0);
39+
TEST_ASSERT(result_dns_failure == 0);
40+
TEST_ASSERT(result_exp_timeout == 0);
41+
}

0 commit comments

Comments
 (0)