Skip to content

nsapi - Fix missing NSAPI_UNSPEC check in gethostbyname for ip literals #3232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions features/FEATURE_LWIP/TESTS/mbedmicro-net/gethostbyname/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "EthernetInterface.h"

using namespace utest::v1;


// Hostname for testing against
// Must have A and AAAA records
#ifndef MBED_DNS_TEST_HOST
#define MBED_DNS_TEST_HOST "connector.mbed.com"
#endif

// Address info from stack
const char *ip_literal;
nsapi_version_t ip_pref;
const char *ip_pref_repr;

// Network setup
EthernetInterface net;
void net_bringup() {
int err = net.connect();
TEST_ASSERT_EQUAL(0, err);
printf("MBED: Connected to network\n");
printf("MBED: IP Address: %s\n", net.get_ip_address());

ip_literal = net.get_ip_address();
ip_pref = SocketAddress(ip_literal).get_ip_version();
ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" :
(ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec";
}


// DNS tests
void test_dns_query() {
SocketAddress addr;
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr);
printf("DNS: query \"%s\" => \"%s\"\n",
MBED_DNS_TEST_HOST, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
}

void test_dns_query_pref() {
SocketAddress addr;
int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref);
printf("DNS: query %s \"%s\" => \"%s\"\n",
ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
}

void test_dns_literal() {
SocketAddress addr;
int err = net.gethostbyname(ip_literal, &addr);
printf("DNS: literal \"%s\" => \"%s\"\n",
ip_literal, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
}

void test_dns_literal_pref() {
SocketAddress addr;
int err = net.gethostbyname(ip_literal, &addr, ip_pref);
printf("DNS: literal %s \"%s\" => \"%s\"\n",
ip_pref_repr, ip_literal, addr.get_ip_address());

TEST_ASSERT_EQUAL(0, err);
TEST_ASSERT((bool)addr);
TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
}


// Test setup
utest::v1::status_t test_setup(const size_t number_of_cases) {
GREENTEA_SETUP(20, "default_auto");
net_bringup();
return verbose_test_setup_handler(number_of_cases);
}

Case cases[] = {
Case("Testing DNS query", test_dns_query),
Case("Testing DNS preference query", test_dns_query_pref),
Case("Testing DNS literal", test_dns_literal),
Case("Testing DNS preference literal", test_dns_literal_pref),
};

Specification specification(test_setup, cases);

int main() {
return !Harness::run(specification);
}
2 changes: 1 addition & 1 deletion features/netsocket/NetworkStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ nsapi_error_t NetworkStack::gethostbyname(const char *name, SocketAddress *addre
{
// check for simple ip addresses
if (address->set_ip_address(name)) {
if (address->get_ip_version() != version) {
if (version != NSAPI_UNSPEC && address->get_ip_version() != version) {
return NSAPI_ERROR_DNS_FAILURE;
}

Expand Down