|
| 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 | +} |
0 commit comments