Skip to content

Asynchronous DNS greentea tests #7216

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 1 commit into from
Jun 20, 2018
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
41 changes: 41 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "dns_tests.h"

using namespace utest::v1;

namespace {
int result_ok;
int result_no_mem;
int result_dns_failure;
int result_exp_timeout;
}

void ASYNCHRONOUS_DNS()
{
do_asynchronous_gethostbyname(dns_test_hosts, 1, &result_ok, &result_no_mem, &result_dns_failure, &result_exp_timeout);

TEST_ASSERT(result_ok == 1);
TEST_ASSERT(result_no_mem == 0);
TEST_ASSERT(result_dns_failure == 0);
TEST_ASSERT(result_exp_timeout == 0);
}
65 changes: 65 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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 "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "dns_tests.h"

using namespace utest::v1;

namespace {
int ticker_us = 0;
}

static void test_dns_query_ticker(void)
{
ticker_us += 100;
}

void ASYNCHRONOUS_DNS_CACHE()
{
rtos::Semaphore semaphore;
dns_application_data data;
data.semaphore = &semaphore;

Ticker ticker;
ticker.attach_us(&test_dns_query_ticker, 100);

for (unsigned int i = 0; i < 5; i++) {
int started_us = ticker_us;

nsapi_error_t err = get_interface()->gethostbyname_async(dns_test_hosts[0],
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data));
TEST_ASSERT(err >= 0);

semaphore.wait();

TEST_ASSERT(data.result == NSAPI_ERROR_OK);
TEST_ASSERT(strlen(data.addr.get_ip_address()) > 1);

int delay_ms = (ticker_us - started_us) / 1000;

static int delay_first = delay_ms / 2;
// Check that cached accesses are at least twice as fast as the first one
TEST_ASSERT_FALSE(i != 0 && delay_ms > delay_first);

printf("DNS: query \"%s\" => \"%s\", time %i ms\n",
dns_test_hosts[0], data.addr.get_ip_address(), delay_ms);
}
}
84 changes: 84 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns_cancel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "dns_tests.h"

using namespace utest::v1;

void ASYNCHRONOUS_DNS_CANCEL()
{
rtos::Semaphore semaphore;
dns_application_data *data = new dns_application_data[MBED_CONF_APP_DNS_TEST_HOSTS_NUM];

int count = 0;

for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
data[i].value_set = false;
data[i].semaphore = &semaphore;
data[i].req_result = get_interface()->gethostbyname_async(dns_test_hosts[i],
mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data[i]));
TEST_ASSERT(data[i].req_result >= 0 || data[i].req_result == NSAPI_ERROR_NO_MEMORY);

if (data[i].req_result >= 0) {
// Callback will be called
count++;
} else {
// No memory to initiate DNS query, callback will not be called
data[i].result = NSAPI_ERROR_NO_MEMORY;
data[i].value_set = true;
}
}

for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
if (data[i].req_result > 0) {
if (get_interface()->gethostbyname_async_cancel(data[i].req_result) == NSAPI_ERROR_OK) {
count--;
}
}
}

// Wait for callback(s) to complete
for (int i = 0; i < count; i++) {
semaphore.wait();
}

for (unsigned int i = 0; i < MBED_CONF_APP_DNS_TEST_HOSTS_NUM; i++) {
if (!data[i].value_set) {
printf("DNS: query \"%s\" => cancel\n", dns_test_hosts[i]);
continue;
}
TEST_ASSERT(data[i].result == NSAPI_ERROR_OK || data[i].result == NSAPI_ERROR_NO_MEMORY || data[i].result == NSAPI_ERROR_DNS_FAILURE || data[i].result == NSAPI_ERROR_TIMEOUT);
if (data[i].result == NSAPI_ERROR_OK) {
printf("DNS: query \"%s\" => \"%s\"\n",
dns_test_hosts[i], data[i].addr.get_ip_address());
} else if (data[i].result == NSAPI_ERROR_DNS_FAILURE) {
printf("DNS: query \"%s\" => DNS failure\n", dns_test_hosts[i]);
} else if (data[i].result == NSAPI_ERROR_TIMEOUT) {
printf("DNS: query \"%s\" => timeout\n", dns_test_hosts[i]);
} else if (data[i].result == NSAPI_ERROR_NO_MEMORY) {
printf("DNS: query \"%s\" => no memory\n", dns_test_hosts[i]);
}
}

delete[] data;

wait(5.0);
}
85 changes: 85 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns_external_event_queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* 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 "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "dns_tests.h"

#include "nsapi_dns.h"

using namespace utest::v1;

namespace {
int result_ok;
int result_no_mem;
int result_dns_failure;
int result_exp_timeout;

const int EXTERNAL_THREAD_SIZE = 2048;
const int EVENT_QUEUE_SIZE = 10;

events::EventQueue *event_queue;
}

static nsapi_error_t event_queue_call(int delay, mbed::Callback<void()> func)
{
if (delay) {
if (event_queue->call_in(delay, func) == 0) {
return NSAPI_ERROR_NO_MEMORY;
}
} else {
if (event_queue->call(func) == 0) {
return NSAPI_ERROR_NO_MEMORY;
}
}
return NSAPI_ERROR_OK;
}

void ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE()
{
// Ensures that cache does not contain entries
do_asynchronous_gethostbyname(dns_test_hosts, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

TEST_ASSERT(result_ok == MBED_CONF_NSAPI_DNS_CACHE_SIZE);
TEST_ASSERT(result_no_mem == 0);
TEST_ASSERT(result_dns_failure == 0);
TEST_ASSERT(result_exp_timeout == 0);

// Dispatch event queue
Thread eventThread(osPriorityNormal, EXTERNAL_THREAD_SIZE);
EventQueue queue(EVENT_QUEUE_SIZE * EVENTS_EVENT_SIZE);
eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
event_queue = &queue;

nsapi_dns_call_in_set(event_queue_call);

do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

TEST_ASSERT(result_ok == MBED_CONF_APP_DNS_SIMULT_QUERIES);
TEST_ASSERT(result_no_mem == 1); // last query fails for no memory as expected
TEST_ASSERT(result_dns_failure == 0);
TEST_ASSERT(result_exp_timeout == 0);

// Give event queue time to finalise before destructors
wait(2.0);

nsapi_dns_call_in_set(0);
}
68 changes: 68 additions & 0 deletions TESTS/netsocket/dns/asynchronous_dns_invalid_host.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "dns_tests.h"

using namespace utest::v1;

namespace {
int result_ok;
int result_no_mem;
int result_dns_failure;
int result_exp_timeout;
}

void ASYNCHRONOUS_DNS_INVALID_HOST()
{
// Ensures that cache does not contain entries
do_asynchronous_gethostbyname(dns_test_hosts_second, MBED_CONF_NSAPI_DNS_CACHE_SIZE, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

char dns_test_hosts_new[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN];
memcpy(dns_test_hosts_new, dns_test_hosts, sizeof(dns_test_hosts_new));

int exp_dns_failure = 0;
int exp_ok = 0;

// Invalidate 1st and 3th etc. entries
for (unsigned int i = 0; i < MBED_CONF_APP_DNS_SIMULT_QUERIES + 1; i++) {
if ((i % 2) == 0) {
// Last query fails to no memory so do not increase
if (i != MBED_CONF_APP_DNS_SIMULT_QUERIES) {
exp_dns_failure++;
}
strcat(&(dns_test_hosts_new[i][0]), "_invalid");
} else {
// Last query fails to no memory so do not increase
if (i != MBED_CONF_APP_DNS_SIMULT_QUERIES) {
exp_ok++;
}
}
}

do_asynchronous_gethostbyname(dns_test_hosts_new, MBED_CONF_APP_DNS_SIMULT_QUERIES + 1, &result_ok, &result_no_mem,
&result_dns_failure, &result_exp_timeout);

TEST_ASSERT(result_ok == exp_ok);
TEST_ASSERT(result_no_mem == 1); // last query fails for no memory as expected
TEST_ASSERT(result_dns_failure == exp_dns_failure || result_dns_failure == exp_dns_failure + 1);
TEST_ASSERT(result_exp_timeout == 0);
}
Loading