Skip to content

Commit b04a8da

Browse files
committed
nsapi - Added test cases for gethostbyname
- test_dns_query - test_dns_query_pref - test_dns_literal - test_dns_literal_pref
1 parent a3ad618 commit b04a8da

File tree

1 file changed

+104
-0
lines changed
  • features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname

1 file changed

+104
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "unity.h"
4+
#include "utest.h"
5+
#include "EthernetInterface.h"
6+
7+
using namespace utest::v1;
8+
9+
10+
// Hostname for testing against
11+
// Must have A and AAAA records
12+
#ifndef MBED_DNS_TEST_HOST
13+
#define MBED_DNS_TEST_HOST "connector.mbed.com"
14+
#endif
15+
16+
// Address info from stack
17+
const char *ip_literal;
18+
nsapi_version_t ip_pref;
19+
const char *ip_pref_repr;
20+
21+
// Network setup
22+
EthernetInterface net;
23+
void net_bringup() {
24+
int err = net.connect();
25+
TEST_ASSERT_EQUAL(0, err);
26+
printf("MBED: Connected to network\n");
27+
printf("MBED: IP Address: %s\n", net.get_ip_address());
28+
29+
ip_literal = net.get_ip_address();
30+
ip_pref = SocketAddress(ip_literal).get_ip_version();
31+
ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" :
32+
(ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec";
33+
}
34+
35+
36+
// DNS tests
37+
void test_dns_query() {
38+
SocketAddress addr;
39+
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr);
40+
printf("DNS: query \"%s\" => \"%s\"\n",
41+
MBED_DNS_TEST_HOST, addr.get_ip_address());
42+
43+
TEST_ASSERT_EQUAL(0, err);
44+
TEST_ASSERT((bool)addr);
45+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
46+
}
47+
48+
void test_dns_query_pref() {
49+
SocketAddress addr;
50+
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref);
51+
printf("DNS: query %s \"%s\" => \"%s\"\n",
52+
ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address());
53+
54+
TEST_ASSERT_EQUAL(0, err);
55+
TEST_ASSERT((bool)addr);
56+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
57+
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
58+
}
59+
60+
void test_dns_literal() {
61+
SocketAddress addr;
62+
int err = net.gethostbyname(ip_literal, &addr);
63+
printf("DNS: literal \"%s\" => \"%s\"\n",
64+
ip_literal, addr.get_ip_address());
65+
66+
TEST_ASSERT_EQUAL(0, err);
67+
TEST_ASSERT((bool)addr);
68+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
69+
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
70+
}
71+
72+
void test_dns_literal_pref() {
73+
SocketAddress addr;
74+
int err = net.gethostbyname(ip_literal, &addr, ip_pref);
75+
printf("DNS: literal %s \"%s\" => \"%s\"\n",
76+
ip_pref_repr, ip_literal, addr.get_ip_address());
77+
78+
TEST_ASSERT_EQUAL(0, err);
79+
TEST_ASSERT((bool)addr);
80+
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
81+
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
82+
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
83+
}
84+
85+
86+
// Test setup
87+
utest::v1::status_t test_setup(const size_t number_of_cases) {
88+
GREENTEA_SETUP(20, "default_auto");
89+
net_bringup();
90+
return verbose_test_setup_handler(number_of_cases);
91+
}
92+
93+
Case cases[] = {
94+
Case("Testing DNS query", test_dns_query),
95+
Case("Testing DNS preference query", test_dns_query_pref),
96+
Case("Testing DNS literal", test_dns_literal),
97+
Case("Testing DNS preference literal", test_dns_literal_pref),
98+
};
99+
100+
Specification specification(test_setup, cases);
101+
102+
int main() {
103+
return !Harness::run(specification);
104+
}

0 commit comments

Comments
 (0)