Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 * Copyright (c) 2018, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 #define WIFI 2 00019 #if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \ 00020 (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID)) 00021 #error [NOT_SUPPORTED] No network configuration found for this target. 00022 #endif 00023 00024 #include "mbed.h" 00025 #include "greentea-client/test_env.h" 00026 #include "unity.h" 00027 #include "utest.h" 00028 #include "nsapi_dns.h" 00029 #include "EventQueue.h" 00030 #include "dns_tests.h" 00031 00032 using namespace utest::v1; 00033 00034 namespace { 00035 NetworkInterface *net; 00036 } 00037 00038 const char dns_test_hosts[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_HOSTS; 00039 const char dns_test_hosts_second[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_HOSTS_SECOND; 00040 00041 // Callback used for asynchronous DNS result 00042 void hostbyname_cb(void *data, nsapi_error_t result, SocketAddress *address) 00043 { 00044 dns_application_data *app_data = static_cast<dns_application_data *>(data); 00045 app_data->result = result; 00046 if (address) { 00047 app_data->addr = *address; 00048 } 00049 app_data->semaphore->release(); 00050 app_data->value_set = true; 00051 } 00052 00053 // General function to do asynchronous DNS host name resolution 00054 void do_asynchronous_gethostbyname(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) 00055 { 00056 // Verify that there is enough hosts in the host list 00057 TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM) 00058 00059 // Reset counters 00060 (*exp_ok) = 0; 00061 (*exp_no_mem) = 0; 00062 (*exp_dns_failure) = 0; 00063 (*exp_timeout) = 0; 00064 00065 // Create callback semaphore and data 00066 rtos::Semaphore semaphore; 00067 dns_application_data *data = new dns_application_data[op_count]; 00068 00069 unsigned int count = 0; 00070 for (unsigned int i = 0; i < op_count; i++) { 00071 data[i].semaphore = &semaphore; 00072 nsapi_error_t err = net->gethostbyname_async(hosts[i], mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data[i])); 00073 TEST_ASSERT(err >= 0 || err == NSAPI_ERROR_NO_MEMORY ); 00074 if (err >= 0) { 00075 // Callback will be called 00076 count++; 00077 } else { 00078 // No memory to initiate DNS query, callback will not be called 00079 data[i].result = NSAPI_ERROR_NO_MEMORY ; 00080 } 00081 } 00082 00083 // Wait for callback(s) to complete 00084 for (unsigned int i = 0; i < count; i++) { 00085 semaphore.wait(); 00086 } 00087 00088 // Print result 00089 for (unsigned int i = 0; i < op_count; i++) { 00090 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 ); 00091 if (data[i].result == NSAPI_ERROR_OK ) { 00092 (*exp_ok)++; 00093 printf("DNS: query \"%s\" => \"%s\"\n", 00094 hosts[i], data[i].addr.get_ip_address()); 00095 } else if (data[i].result == NSAPI_ERROR_DNS_FAILURE ) { 00096 (*exp_dns_failure)++; 00097 printf("DNS: query \"%s\" => DNS failure\n", hosts[i]); 00098 } else if (data[i].result == NSAPI_ERROR_TIMEOUT ) { 00099 (*exp_timeout)++; 00100 printf("DNS: query \"%s\" => timeout\n", hosts[i]); 00101 } else if (data[i].result == NSAPI_ERROR_NO_MEMORY ) { 00102 (*exp_no_mem)++; 00103 printf("DNS: query \"%s\" => no memory\n", hosts[i]); 00104 } 00105 } 00106 00107 delete[] data; 00108 } 00109 00110 void do_gethostbyname(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) 00111 { 00112 // Verify that there is enough hosts in the host list 00113 TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM) 00114 00115 // Reset counters 00116 (*exp_ok) = 0; 00117 (*exp_no_mem) = 0; 00118 (*exp_dns_failure) = 0; 00119 (*exp_timeout) = 0; 00120 00121 for (unsigned int i = 0; i < op_count; i++) { 00122 SocketAddress address; 00123 nsapi_error_t err = net->gethostbyname(hosts[i], &address); 00124 00125 TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_NO_MEMORY || err == NSAPI_ERROR_DNS_FAILURE || err == NSAPI_ERROR_TIMEOUT ); 00126 if (err == NSAPI_ERROR_OK ) { 00127 (*exp_ok)++; 00128 printf("DNS: query \"%s\" => \"%s\"\n", 00129 hosts[i], address.get_ip_address()); 00130 } else if (err == NSAPI_ERROR_DNS_FAILURE ) { 00131 (*exp_dns_failure)++; 00132 printf("DNS: query \"%s\" => DNS failure\n", hosts[i]); 00133 } else if (err == NSAPI_ERROR_TIMEOUT ) { 00134 (*exp_timeout)++; 00135 printf("DNS: query \"%s\" => timeout\n", hosts[i]); 00136 } else if (err == NSAPI_ERROR_NO_MEMORY ) { 00137 (*exp_no_mem)++; 00138 printf("DNS: query \"%s\" => no memory\n", hosts[i]); 00139 } 00140 } 00141 } 00142 00143 NetworkInterface *get_interface() 00144 { 00145 return net; 00146 } 00147 00148 static void net_bringup() 00149 { 00150 MBED_ASSERT(MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_NSAPI_DNS_CACHE_SIZE && MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_APP_DNS_SIMULT_QUERIES + 1); 00151 00152 net = NetworkInterface::get_default_instance(); 00153 nsapi_error_t err = net->connect(); 00154 TEST_ASSERT_EQUAL(NSAPI_ERROR_OK , err); 00155 printf("MBED: IP address is '%s'\n", net->get_ip_address()); 00156 } 00157 00158 // Test setup 00159 utest::v1::status_t test_setup(const size_t number_of_cases) 00160 { 00161 GREENTEA_SETUP(120, "default_auto"); 00162 net_bringup(); 00163 return verbose_test_setup_handler(number_of_cases); 00164 } 00165 00166 Case cases[] = { 00167 Case("ASYNCHRONOUS_DNS", ASYNCHRONOUS_DNS), 00168 Case("ASYNCHRONOUS_DNS_SIMULTANEOUS", ASYNCHRONOUS_DNS_SIMULTANEOUS), 00169 Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE", ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE), 00170 Case("ASYNCHRONOUS_DNS_CACHE", ASYNCHRONOUS_DNS_CACHE), 00171 Case("ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC", ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC), 00172 Case("ASYNCHRONOUS_DNS_CANCEL", ASYNCHRONOUS_DNS_CANCEL), 00173 Case("ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE", ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE), 00174 Case("ASYNCHRONOUS_DNS_INVALID_HOST", ASYNCHRONOUS_DNS_INVALID_HOST), 00175 Case("ASYNCHRONOUS_DNS_TIMEOUTS", ASYNCHRONOUS_DNS_TIMEOUTS), 00176 #ifdef MBED_EXTENDED_TESTS 00177 Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT", ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT), 00178 #endif 00179 Case("SYNCHRONOUS_DNS", SYNCHRONOUS_DNS), 00180 Case("SYNCHRONOUS_DNS_MULTIPLE", SYNCHRONOUS_DNS_MULTIPLE), 00181 Case("SYNCHRONOUS_DNS_INVALID", SYNCHRONOUS_DNS_INVALID), 00182 }; 00183 00184 Specification specification(test_setup, cases); 00185 00186 int main() 00187 { 00188 return !Harness::run(specification); 00189 }
Generated on Tue Aug 9 2022 00:37:14 by
1.7.2