Skip to content

Commit 8b2f4c2

Browse files
Getaddrinfo interface for multiple DNS adresses added.
New members are added to the network interface -getaddrinfo -getaddrinfo_async gethostbyname is unchanged but gethostbyname_async result param now contains results od DNS records found. Test cases for sync/async added added to DNS test folder.
1 parent f77f4ea commit 8b2f4c2

File tree

30 files changed

+556
-52
lines changed

30 files changed

+556
-52
lines changed

TESTS/netsocket/dns/asynchronous_dns_cache.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "utest.h"
2323
#include "dns_tests.h"
2424

25+
#define RESULTS_NUM 1
2526
using namespace utest::v1;
2627

2728
namespace {
@@ -53,7 +54,7 @@ void ASYNCHRONOUS_DNS_CACHE()
5354

5455
semaphore.acquire();
5556

56-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);
57+
TEST_ASSERT_EQUAL(RESULTS_NUM, data.result);
5758
TEST_ASSERT(strlen(data.addr.get_ip_address()) > 1);
5859

5960
int delay_ms = (ticker_us - started_us) / 1000;

TESTS/netsocket/dns/asynchronous_dns_cancel.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ void ASYNCHRONOUS_DNS_CANCEL()
3737
data[i].semaphore = &semaphore;
3838
data[i].req_result = get_interface()->gethostbyname_async(dns_test_hosts[i],
3939
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data[i]));
40-
TEST_ASSERT(data[i].req_result >= 0 || data[i].req_result == NSAPI_ERROR_NO_MEMORY || data[i].req_result == NSAPI_ERROR_BUSY);
40+
TEST_ASSERT(data[i].req_result > 0 || data[i].req_result == NSAPI_ERROR_NO_MEMORY || data[i].req_result == NSAPI_ERROR_BUSY);
4141

42-
if (data[i].req_result >= 0) {
42+
if (data[i].req_result > 0) {
4343
// Callback will be called
4444
count++;
4545
} else {
@@ -68,10 +68,10 @@ void ASYNCHRONOUS_DNS_CANCEL()
6868
tr_info("DNS: query \"%s\" => cancel", dns_test_hosts[i]);
6969
continue;
7070
}
71-
TEST_ASSERT(data[i].result == NSAPI_ERROR_OK || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_BUSY || data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
72-
if (data[i].result == NSAPI_ERROR_OK) {
73-
tr_info("DNS: query \"%s\" => \"%s\"",
74-
dns_test_hosts[i], data[i].addr.get_ip_address());
71+
TEST_ASSERT(data[i].result == 1 || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_BUSY || data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
72+
if (data[i].result == 1) {
73+
printf("DNS: query \"%s\" => \"%s\"\n",
74+
dns_test_hosts[i], data[i].addr.get_ip_address());
7575
} else if (data[i].result == NSAPI_ERROR_DNS_FAILURE) {
7676
tr_error("DNS: query \"%s\" => DNS failure", dns_test_hosts[i]);
7777
} else if (data[i].result == NSAPI_ERROR_TIMEOUT) {
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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_number;
28+
int result_no_mem;
29+
int result_dns_failure;
30+
int result_exp_timeout;
31+
}
32+
33+
// Callback used for asynchronous DNS result
34+
void getaddrinfo_cb(void *data, nsapi_error_t result, SocketAddress *address)
35+
{
36+
dns_application_data_multi_ip *app_data = static_cast<dns_application_data_multi_ip *>(data);
37+
app_data->result = result;
38+
for (unsigned int i = 0; i < result; i++) {
39+
if (address) {
40+
app_data->addr[i] = address[i];
41+
}
42+
}
43+
app_data->semaphore->release();
44+
app_data->value_set = true;
45+
}
46+
47+
void do_getaddrinfo_async(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)
48+
{
49+
// Verify that there is enough hosts in the host list
50+
TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM)
51+
52+
// Reset counters
53+
(*exp_ok) = 0;
54+
(*exp_no_mem) = 0;
55+
(*exp_dns_failure) = 0;
56+
(*exp_timeout) = 0;
57+
58+
// Create callback semaphore and data
59+
rtos::Semaphore semaphore;
60+
dns_application_data_multi_ip *data = new dns_application_data_multi_ip[op_count];
61+
SocketAddress hints{{NSAPI_IPv4}, 80};
62+
63+
unsigned int count = 0;
64+
for (unsigned int i = 0; i < op_count; i++) {
65+
data[i].semaphore = &semaphore;
66+
nsapi_error_t err = NetworkInterface::get_default_instance()->getaddrinfo_async(hosts[i], &hints, mbed::Callback<void(nsapi_error_t, SocketAddress *)>(getaddrinfo_cb, (void *) &data[i]));
67+
TEST_ASSERT(err >= 0 || err == NSAPI_ERROR_NO_MEMORY || err == NSAPI_ERROR_BUSY);
68+
if (err >= 0) {
69+
// Callback will be called
70+
count++;
71+
} else {
72+
// No memory to initiate DNS query, callback will not be called
73+
data[i].result = err;
74+
}
75+
}
76+
77+
// Wait for callback(s) to complete
78+
for (unsigned int i = 0; i < count; i++) {
79+
semaphore.acquire();
80+
}
81+
// Print result
82+
for (unsigned int i = 0; i < op_count; i++) {
83+
TEST_ASSERT(data[i].result > 0 || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_BUSY
84+
|| data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
85+
if (data[i].result > 0) {
86+
(*exp_ok)++;
87+
for (unsigned int results = 0; results < data[i].result; results++) {
88+
printf("DNS: query \"%s\" => \"%s\"\n",
89+
hosts[i], data[i].addr[results].get_ip_address());
90+
}
91+
} else if (data[i].result == NSAPI_ERROR_DNS_FAILURE) {
92+
(*exp_dns_failure)++;
93+
printf("DNS: query \"%s\" => DNS failure\n", hosts[i]);
94+
} else if (data[i].result == NSAPI_ERROR_TIMEOUT) {
95+
(*exp_timeout)++;
96+
printf("DNS: query \"%s\" => timeout\n", hosts[i]);
97+
} else if (data[i].result == NSAPI_ERROR_NO_MEMORY) {
98+
(*exp_no_mem)++;
99+
printf("DNS: query \"%s\" => no memory\n", hosts[i]);
100+
} else if (data[i].result == NSAPI_ERROR_BUSY) {
101+
(*exp_no_mem)++;
102+
printf("DNS: query \"%s\" => busy\n", hosts[i]);
103+
}
104+
}
105+
106+
delete[] data;
107+
}
108+
109+
void ASYNCHRONOUS_DNS_MULTI_IP()
110+
{
111+
nsapi_dns_reset();
112+
do_getaddrinfo_async(dns_test_hosts_multi_ip, MBED_CONF_APP_DNS_SIMULT_QUERIES, &result_number, &result_no_mem, &result_dns_failure, &result_exp_timeout);
113+
114+
TEST_ASSERT_EQUAL(MBED_CONF_APP_DNS_SIMULT_QUERIES, result_number);
115+
TEST_ASSERT_EQUAL(0, result_no_mem);
116+
TEST_ASSERT_EQUAL(0, result_dns_failure);
117+
TEST_ASSERT_EQUAL(0, result_exp_timeout);
118+
}

TESTS/netsocket/dns/asynchronous_dns_non_async_and_async.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC()
5454
get_interface()->gethostbyname_async_cancel(err);
5555
}
5656

57-
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, data.result);
57+
TEST_ASSERT_EQUAL(1, data.result);
5858

5959
tr_info("DNS: query \"%s\" => \"%s\"",
6060
dns_test_hosts_second[0], data.addr.get_ip_address());

TESTS/netsocket/dns/dns_tests.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#define MBED_CONF_NSAPI_DNS_CACHE_SIZE 3
3636
#endif
3737

38+
#ifndef MBED_CONF_APP_DNS_TEST_HOSTS_NUM
39+
#define MBED_CONF_APP_DNS_TEST_HOSTS_NUM 6
40+
#endif
41+
3842
// Hostnames for testing against
3943
// Both lists must have A and AAAA records
4044
#ifndef MBED_CONF_APP_DNS_TEST_HOSTS
@@ -45,8 +49,8 @@
4549
#define MBED_CONF_APP_DNS_TEST_HOSTS_SECOND {"ipv6ready.org", "wireshark.org", "bbc.co.uk", "cnn.com", "www.flickr.com", "www.mozilla.org"}
4650
#endif
4751

48-
#ifndef MBED_CONF_APP_DNS_TEST_HOSTS_NUM
49-
#define MBED_CONF_APP_DNS_TEST_HOSTS_NUM 6
52+
#ifndef MBED_CONF_APP_DNS_TEST_MULTI_IP_HOSTS
53+
#define MBED_CONF_APP_DNS_TEST_MULTI_IP_HOSTS {"google.com", "www.mozilla.org", "yahoo.com", "instagram.com","www.flickr.com"}
5054
#endif
5155

5256
#define DNS_TEST_HOST_LEN 40
@@ -59,8 +63,17 @@ struct dns_application_data {
5963
bool value_set;
6064
};
6165

66+
struct dns_application_data_multi_ip {
67+
rtos::Semaphore *semaphore;
68+
nsapi_error_t result;
69+
SocketAddress addr[MBED_CONF_NSAPI_DNS_ADDRESSES_LIMIT];
70+
nsapi_error_t req_result;
71+
bool value_set;
72+
};
73+
6274
extern const char dns_test_hosts[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
6375
extern const char dns_test_hosts_second[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
76+
extern const char dns_test_hosts_multi_ip[MBED_CONF_APP_DNS_SIMULT_QUERIES][DNS_TEST_HOST_LEN];
6477

6578
/*
6679
* Utility functions
@@ -95,4 +108,6 @@ void SYNCHRONOUS_DNS();
95108
void SYNCHRONOUS_DNS_MULTIPLE();
96109
void SYNCHRONOUS_DNS_CACHE();
97110
void SYNCHRONOUS_DNS_INVALID();
111+
void SYNCHRONOUS_DNS_MULTI_IP();
112+
void ASYNCHRONOUS_DNS_MULTI_IP();
98113
#endif

TESTS/netsocket/dns/main.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ NetworkInterface *net;
4242

4343
const char dns_test_hosts[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_HOSTS;
4444
const char dns_test_hosts_second[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_HOSTS_SECOND;
45+
const char dns_test_hosts_multi_ip[MBED_CONF_APP_DNS_SIMULT_QUERIES][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_MULTI_IP_HOSTS;
4546

4647
// Callback used for asynchronous DNS result
4748
void hostbyname_cb(void *data, nsapi_error_t result, SocketAddress *address)
@@ -92,8 +93,8 @@ void do_asynchronous_gethostbyname(const char hosts[][DNS_TEST_HOST_LEN], unsign
9293

9394
// Print result
9495
for (unsigned int i = 0; i < op_count; i++) {
95-
TEST_ASSERT(data[i].result == NSAPI_ERROR_OK || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_BUSY || data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
96-
if (data[i].result == NSAPI_ERROR_OK) {
96+
TEST_ASSERT(data[i].result > 0 || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_BUSY || data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
97+
if (data[i].result > 0) {
9798
(*exp_ok)++;
9899
tr_info("DNS: query \"%s\" => \"%s\"",
99100
hosts[i], data[i].addr.get_ip_address());
@@ -220,6 +221,8 @@ Case cases[] = {
220221
Case("SYNCHRONOUS_DNS", SYNCHRONOUS_DNS),
221222
Case("SYNCHRONOUS_DNS_MULTIPLE", SYNCHRONOUS_DNS_MULTIPLE),
222223
Case("SYNCHRONOUS_DNS_INVALID", SYNCHRONOUS_DNS_INVALID),
224+
Case("SYNCHRONOUS_DNS_MULTI_IP", SYNCHRONOUS_DNS_MULTI_IP),
225+
Case("ASYNCHRONOUS_DNS_MULTI_IP", ASYNCHRONOUS_DNS_MULTI_IP),
223226
};
224227

225228
Specification specification(test_setup, cases, greentea_teardown, greentea_continue_handlers);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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_number;
28+
int result_no_mem;
29+
int result_dns_failure;
30+
int result_exp_timeout;
31+
}
32+
33+
34+
void do_getaddrinfo(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)
35+
{
36+
// Verify that there is enough hosts in the host list
37+
TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM)
38+
39+
// Reset counters
40+
(*exp_ok) = 0;
41+
(*exp_no_mem) = 0;
42+
(*exp_dns_failure) = 0;
43+
(*exp_timeout) = 0;
44+
45+
SocketAddress hints{{NSAPI_IPv4}, 80};
46+
for (unsigned int i = 0; i < op_count; i++) {
47+
SocketAddress *result;
48+
nsapi_error_t err = NetworkInterface::get_default_instance()->get_default_instance()->getaddrinfo(hosts[i], &hints, &result);
49+
50+
if (err == NSAPI_ERROR_DNS_FAILURE) {
51+
(*exp_dns_failure)++;
52+
printf("DNS: query \"%s\" => DNS failure\n", hosts[i]);
53+
} else if (err == NSAPI_ERROR_TIMEOUT) {
54+
(*exp_timeout)++;
55+
printf("DNS: query \"%s\" => timeout\n", hosts[i]);
56+
} else if (err == NSAPI_ERROR_NO_MEMORY) {
57+
(*exp_no_mem)++;
58+
printf("DNS: query \"%s\" => no memory\n", hosts[i]);
59+
} else if (err == NSAPI_ERROR_BUSY) {
60+
(*exp_no_mem)++;
61+
printf("DNS: query \"%s\" => busy\n", hosts[i]);
62+
} else if (err > NSAPI_ERROR_OK) {
63+
(*exp_ok)++;
64+
for (unsigned int results = 0; results < err; results++) {
65+
printf("DNS: query \"%s\" => \"%s\"\n", hosts[i], result[results].get_ip_address());
66+
}
67+
} else if (err == 0) {
68+
printf("DNS: query \"%s\", no results\n", hosts[i]);
69+
TEST_FAIL();
70+
} else if (err < 0) {
71+
printf("DNS: query \"%s\" => %d, unexpected answer\n", hosts[i], err);
72+
TEST_FAIL();
73+
}
74+
}
75+
}
76+
77+
void SYNCHRONOUS_DNS_MULTI_IP()
78+
{
79+
nsapi_dns_reset();
80+
do_getaddrinfo(dns_test_hosts_multi_ip, MBED_CONF_APP_DNS_SIMULT_QUERIES, &result_number, &result_no_mem, &result_dns_failure, &result_exp_timeout);
81+
82+
TEST_ASSERT_EQUAL(MBED_CONF_APP_DNS_SIMULT_QUERIES, result_number);
83+
TEST_ASSERT_EQUAL(0, result_no_mem);
84+
TEST_ASSERT_EQUAL(0, result_dns_failure);
85+
TEST_ASSERT_EQUAL(0, result_exp_timeout);
86+
}

UNITTESTS/MODULETESTS/features/netsocket/IfaceDnsSocket/test_IfaceDnsSocket.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,14 @@ TEST_F(Test_IfaceDnsSocket, multiple_queries)
332332

333333
SocketAddress *addr_cache;
334334
// Cache will only raturn one address.
335-
EXPECT_EQ(Test_IfaceDnsSocket::query_id, getaddrinfo(&stackMock(), "www.google.com", hints, &addr_cache));
335+
EXPECT_EQ(3, getaddrinfo(&stackMock(), "www.google.com", hints, &addr_cache));
336336

337-
// This is a bug which will be fixed in
338337
EXPECT_EQ(addr_cache[0].get_ip_version(), NSAPI_IPv4);
339338
EXPECT_FALSE(strncmp(addr_cache[0].get_ip_address(), "216.58.207.238", sizeof("216.58.207.238")));
339+
EXPECT_EQ(addr_cache[1].get_ip_version(), NSAPI_IPv4);
340+
EXPECT_FALSE(strncmp(addr_cache[1].get_ip_address(), "222.173.190.239", sizeof("222.173.190.239")));
341+
EXPECT_EQ(addr_cache[2].get_ip_version(), NSAPI_IPv4);
342+
EXPECT_FALSE(strncmp(addr_cache[2].get_ip_address(), "1.2.3.4", sizeof("1.2.3.4")));
340343
delete[] addr_cache;
341344
}
342345

@@ -400,7 +403,7 @@ TEST_F(Test_IfaceDnsSocket, simultaneous_query_async)
400403
// Run again to execute response_handler
401404
executeEventQueueCallbacks();
402405

403-
EXPECT_EQ(NSAPI_ERROR_OK, Test_IfaceDnsSocket::hostname_cb_result);
406+
EXPECT_EQ(1, Test_IfaceDnsSocket::hostname_cb_result);
404407
EXPECT_EQ(Test_IfaceDnsSocket::hostname_cb_address[0].get_ip_version(), NSAPI_IPv4);
405408
EXPECT_FALSE(strncmp(Test_IfaceDnsSocket::hostname_cb_address[0].get_ip_address(), "216.58.207.238", sizeof("216.58.207.238")));
406409

@@ -473,11 +476,12 @@ TEST_F(Test_IfaceDnsSocket, single_query_async_multiple)
473476
Test_IfaceDnsSocket::hostname_cb_result = NSAPI_ERROR_DEVICE_ERROR;
474477

475478
// Do not set any return values. Second call should use cache.
476-
// Cache will only return one address!
477-
EXPECT_EQ(0, getaddrinfo_async(&stackMock(), "www.google.com", &Test_IfaceDnsSocket::hostbyname_cb));
478-
EXPECT_EQ(NSAPI_ERROR_OK, Test_IfaceDnsSocket::hostname_cb_result);
479+
EXPECT_EQ(3, getaddrinfo_async(&stackMock(), "www.google.com", &Test_IfaceDnsSocket::hostbyname_cb));
480+
EXPECT_EQ(3, Test_IfaceDnsSocket::hostname_cb_result);
479481
EXPECT_EQ(Test_IfaceDnsSocket::hostname_cb_address[0].get_ip_version(), NSAPI_IPv4);
480482
EXPECT_FALSE(strncmp(Test_IfaceDnsSocket::hostname_cb_address[0].get_ip_address(), "216.58.207.238", sizeof("216.58.207.238")));
483+
EXPECT_FALSE(strncmp(Test_IfaceDnsSocket::hostname_cb_address[1].get_ip_address(), "222.173.190.239", sizeof("222.173.190.239")));
484+
EXPECT_FALSE(strncmp(Test_IfaceDnsSocket::hostname_cb_address[2].get_ip_address(), "1.2.3.4", sizeof("1.2.3.4")));
481485

482486
delete[] Test_IfaceDnsSocket::hostname_cb_address;
483487
}

UNITTESTS/features/netsocket/InternetSocket/unittest.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# UNIT TESTS
44
####################
55

6+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMBED_CONF_NSAPI_DNS_ADDRESSES_LIMIT=10")
7+
68
set(unittest-sources
79
../features/netsocket/SocketAddress.cpp
810
../features/netsocket/NetworkStack.cpp

0 commit comments

Comments
 (0)