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 #ifndef MBED_CONF_APP_CONNECT_STATEMENT 00019 #error [NOT_SUPPORTED] No network configuration found for this target. 00020 #endif 00021 00022 #include "mbed.h" 00023 #include "greentea-client/test_env.h" 00024 #include "unity.h" 00025 #include "utest.h" 00026 #include "nsapi_dns.h" 00027 #include "EventQueue.h" 00028 #include "dns_tests.h" 00029 00030 #include MBED_CONF_APP_HEADER_FILE 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 NetworkInterface *get_interface() 00111 { 00112 return net; 00113 } 00114 00115 static void net_bringup() 00116 { 00117 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); 00118 00119 net = MBED_CONF_APP_OBJECT_CONSTRUCTION; 00120 int err = MBED_CONF_APP_CONNECT_STATEMENT; 00121 TEST_ASSERT_EQUAL(0, err); 00122 00123 printf("MBED: Connected to network\n"); 00124 printf("MBED: IP Address: %s\n", net->get_ip_address()); 00125 } 00126 00127 // Test setup 00128 utest::v1::status_t test_setup(const size_t number_of_cases) 00129 { 00130 GREENTEA_SETUP(120, "default_auto"); 00131 net_bringup(); 00132 return verbose_test_setup_handler(number_of_cases); 00133 } 00134 00135 Case cases[] = { 00136 Case("ASYNCHRONOUS_DNS", ASYNCHRONOUS_DNS), 00137 Case("ASYNCHRONOUS_DNS_SIMULTANEOUS", ASYNCHRONOUS_DNS_SIMULTANEOUS), 00138 Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE", ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE), 00139 Case("ASYNCHRONOUS_DNS_CACHE", ASYNCHRONOUS_DNS_CACHE), 00140 Case("ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC", ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC), 00141 Case("ASYNCHRONOUS_DNS_CANCEL", ASYNCHRONOUS_DNS_CANCEL), 00142 Case("ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE", ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE), 00143 Case("ASYNCHRONOUS_DNS_INVALID_HOST", ASYNCHRONOUS_DNS_INVALID_HOST), 00144 Case("ASYNCHRONOUS_DNS_TIMEOUTS", ASYNCHRONOUS_DNS_TIMEOUTS), 00145 Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT", ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT), 00146 }; 00147 00148 Specification specification(test_setup, cases); 00149 00150 int main() 00151 { 00152 return !Harness::run(specification); 00153 }
Generated on Tue Jul 12 2022 12:45:24 by
1.7.2