takashi kadono
/
Nucleo_446
Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466
mbed-os/TESTS/netsocket/dns/main.cpp@3:f3764f852aa8, 2018-10-11 (annotated)
- Committer:
- kadonotakashi
- Date:
- Thu Oct 11 02:27:46 2018 +0000
- Revision:
- 3:f3764f852aa8
- Parent:
- 0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kadonotakashi | 0:8fdf9a60065b | 1 | /* |
kadonotakashi | 0:8fdf9a60065b | 2 | * Copyright (c) 2018, ARM Limited, All Rights Reserved |
kadonotakashi | 0:8fdf9a60065b | 3 | * SPDX-License-Identifier: Apache-2.0 |
kadonotakashi | 0:8fdf9a60065b | 4 | * |
kadonotakashi | 0:8fdf9a60065b | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
kadonotakashi | 0:8fdf9a60065b | 6 | * not use this file except in compliance with the License. |
kadonotakashi | 0:8fdf9a60065b | 7 | * You may obtain a copy of the License at |
kadonotakashi | 0:8fdf9a60065b | 8 | * |
kadonotakashi | 0:8fdf9a60065b | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
kadonotakashi | 0:8fdf9a60065b | 10 | * |
kadonotakashi | 0:8fdf9a60065b | 11 | * Unless required by applicable law or agreed to in writing, software |
kadonotakashi | 0:8fdf9a60065b | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
kadonotakashi | 0:8fdf9a60065b | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kadonotakashi | 0:8fdf9a60065b | 14 | * See the License for the specific language governing permissions and |
kadonotakashi | 0:8fdf9a60065b | 15 | * limitations under the License. |
kadonotakashi | 0:8fdf9a60065b | 16 | */ |
kadonotakashi | 0:8fdf9a60065b | 17 | |
kadonotakashi | 0:8fdf9a60065b | 18 | #define WIFI 2 |
kadonotakashi | 0:8fdf9a60065b | 19 | #if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \ |
kadonotakashi | 0:8fdf9a60065b | 20 | (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID)) |
kadonotakashi | 0:8fdf9a60065b | 21 | #error [NOT_SUPPORTED] No network configuration found for this target. |
kadonotakashi | 0:8fdf9a60065b | 22 | #endif |
kadonotakashi | 0:8fdf9a60065b | 23 | |
kadonotakashi | 0:8fdf9a60065b | 24 | #include "mbed.h" |
kadonotakashi | 0:8fdf9a60065b | 25 | #include "greentea-client/test_env.h" |
kadonotakashi | 0:8fdf9a60065b | 26 | #include "unity.h" |
kadonotakashi | 0:8fdf9a60065b | 27 | #include "utest.h" |
kadonotakashi | 0:8fdf9a60065b | 28 | #include "nsapi_dns.h" |
kadonotakashi | 0:8fdf9a60065b | 29 | #include "EventQueue.h" |
kadonotakashi | 0:8fdf9a60065b | 30 | #include "dns_tests.h" |
kadonotakashi | 0:8fdf9a60065b | 31 | |
kadonotakashi | 0:8fdf9a60065b | 32 | using namespace utest::v1; |
kadonotakashi | 0:8fdf9a60065b | 33 | |
kadonotakashi | 0:8fdf9a60065b | 34 | namespace { |
kadonotakashi | 0:8fdf9a60065b | 35 | NetworkInterface *net; |
kadonotakashi | 0:8fdf9a60065b | 36 | } |
kadonotakashi | 0:8fdf9a60065b | 37 | |
kadonotakashi | 0:8fdf9a60065b | 38 | const char dns_test_hosts[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_HOSTS; |
kadonotakashi | 0:8fdf9a60065b | 39 | const char dns_test_hosts_second[MBED_CONF_APP_DNS_TEST_HOSTS_NUM][DNS_TEST_HOST_LEN] = MBED_CONF_APP_DNS_TEST_HOSTS_SECOND; |
kadonotakashi | 0:8fdf9a60065b | 40 | |
kadonotakashi | 0:8fdf9a60065b | 41 | // Callback used for asynchronous DNS result |
kadonotakashi | 0:8fdf9a60065b | 42 | void hostbyname_cb(void *data, nsapi_error_t result, SocketAddress *address) |
kadonotakashi | 0:8fdf9a60065b | 43 | { |
kadonotakashi | 0:8fdf9a60065b | 44 | dns_application_data *app_data = static_cast<dns_application_data *>(data); |
kadonotakashi | 0:8fdf9a60065b | 45 | app_data->result = result; |
kadonotakashi | 0:8fdf9a60065b | 46 | if (address) { |
kadonotakashi | 0:8fdf9a60065b | 47 | app_data->addr = *address; |
kadonotakashi | 0:8fdf9a60065b | 48 | } |
kadonotakashi | 0:8fdf9a60065b | 49 | app_data->semaphore->release(); |
kadonotakashi | 0:8fdf9a60065b | 50 | app_data->value_set = true; |
kadonotakashi | 0:8fdf9a60065b | 51 | } |
kadonotakashi | 0:8fdf9a60065b | 52 | |
kadonotakashi | 0:8fdf9a60065b | 53 | // General function to do asynchronous DNS host name resolution |
kadonotakashi | 0:8fdf9a60065b | 54 | 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) |
kadonotakashi | 0:8fdf9a60065b | 55 | { |
kadonotakashi | 0:8fdf9a60065b | 56 | // Verify that there is enough hosts in the host list |
kadonotakashi | 0:8fdf9a60065b | 57 | TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM) |
kadonotakashi | 0:8fdf9a60065b | 58 | |
kadonotakashi | 0:8fdf9a60065b | 59 | // Reset counters |
kadonotakashi | 0:8fdf9a60065b | 60 | (*exp_ok) = 0; |
kadonotakashi | 0:8fdf9a60065b | 61 | (*exp_no_mem) = 0; |
kadonotakashi | 0:8fdf9a60065b | 62 | (*exp_dns_failure) = 0; |
kadonotakashi | 0:8fdf9a60065b | 63 | (*exp_timeout) = 0; |
kadonotakashi | 0:8fdf9a60065b | 64 | |
kadonotakashi | 0:8fdf9a60065b | 65 | // Create callback semaphore and data |
kadonotakashi | 0:8fdf9a60065b | 66 | rtos::Semaphore semaphore; |
kadonotakashi | 0:8fdf9a60065b | 67 | dns_application_data *data = new dns_application_data[op_count]; |
kadonotakashi | 0:8fdf9a60065b | 68 | |
kadonotakashi | 0:8fdf9a60065b | 69 | unsigned int count = 0; |
kadonotakashi | 0:8fdf9a60065b | 70 | for (unsigned int i = 0; i < op_count; i++) { |
kadonotakashi | 0:8fdf9a60065b | 71 | data[i].semaphore = &semaphore; |
kadonotakashi | 0:8fdf9a60065b | 72 | nsapi_error_t err = net->gethostbyname_async(hosts[i], mbed::Callback<void(nsapi_error_t, SocketAddress *)>(hostbyname_cb, (void *) &data[i])); |
kadonotakashi | 0:8fdf9a60065b | 73 | TEST_ASSERT(err >= 0 || err == NSAPI_ERROR_NO_MEMORY); |
kadonotakashi | 0:8fdf9a60065b | 74 | if (err >= 0) { |
kadonotakashi | 0:8fdf9a60065b | 75 | // Callback will be called |
kadonotakashi | 0:8fdf9a60065b | 76 | count++; |
kadonotakashi | 0:8fdf9a60065b | 77 | } else { |
kadonotakashi | 0:8fdf9a60065b | 78 | // No memory to initiate DNS query, callback will not be called |
kadonotakashi | 0:8fdf9a60065b | 79 | data[i].result = NSAPI_ERROR_NO_MEMORY; |
kadonotakashi | 0:8fdf9a60065b | 80 | } |
kadonotakashi | 0:8fdf9a60065b | 81 | } |
kadonotakashi | 0:8fdf9a60065b | 82 | |
kadonotakashi | 0:8fdf9a60065b | 83 | // Wait for callback(s) to complete |
kadonotakashi | 0:8fdf9a60065b | 84 | for (unsigned int i = 0; i < count; i++) { |
kadonotakashi | 0:8fdf9a60065b | 85 | semaphore.wait(); |
kadonotakashi | 0:8fdf9a60065b | 86 | } |
kadonotakashi | 0:8fdf9a60065b | 87 | |
kadonotakashi | 0:8fdf9a60065b | 88 | // Print result |
kadonotakashi | 0:8fdf9a60065b | 89 | for (unsigned int i = 0; i < op_count; i++) { |
kadonotakashi | 0:8fdf9a60065b | 90 | 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); |
kadonotakashi | 0:8fdf9a60065b | 91 | if (data[i].result == NSAPI_ERROR_OK) { |
kadonotakashi | 0:8fdf9a60065b | 92 | (*exp_ok)++; |
kadonotakashi | 0:8fdf9a60065b | 93 | printf("DNS: query \"%s\" => \"%s\"\n", |
kadonotakashi | 0:8fdf9a60065b | 94 | hosts[i], data[i].addr.get_ip_address()); |
kadonotakashi | 0:8fdf9a60065b | 95 | } else if (data[i].result == NSAPI_ERROR_DNS_FAILURE) { |
kadonotakashi | 0:8fdf9a60065b | 96 | (*exp_dns_failure)++; |
kadonotakashi | 0:8fdf9a60065b | 97 | printf("DNS: query \"%s\" => DNS failure\n", hosts[i]); |
kadonotakashi | 0:8fdf9a60065b | 98 | } else if (data[i].result == NSAPI_ERROR_TIMEOUT) { |
kadonotakashi | 0:8fdf9a60065b | 99 | (*exp_timeout)++; |
kadonotakashi | 0:8fdf9a60065b | 100 | printf("DNS: query \"%s\" => timeout\n", hosts[i]); |
kadonotakashi | 0:8fdf9a60065b | 101 | } else if (data[i].result == NSAPI_ERROR_NO_MEMORY) { |
kadonotakashi | 0:8fdf9a60065b | 102 | (*exp_no_mem)++; |
kadonotakashi | 0:8fdf9a60065b | 103 | printf("DNS: query \"%s\" => no memory\n", hosts[i]); |
kadonotakashi | 0:8fdf9a60065b | 104 | } |
kadonotakashi | 0:8fdf9a60065b | 105 | } |
kadonotakashi | 0:8fdf9a60065b | 106 | |
kadonotakashi | 0:8fdf9a60065b | 107 | delete[] data; |
kadonotakashi | 0:8fdf9a60065b | 108 | } |
kadonotakashi | 0:8fdf9a60065b | 109 | |
kadonotakashi | 0:8fdf9a60065b | 110 | 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) |
kadonotakashi | 0:8fdf9a60065b | 111 | { |
kadonotakashi | 0:8fdf9a60065b | 112 | // Verify that there is enough hosts in the host list |
kadonotakashi | 0:8fdf9a60065b | 113 | TEST_ASSERT(op_count <= MBED_CONF_APP_DNS_TEST_HOSTS_NUM) |
kadonotakashi | 0:8fdf9a60065b | 114 | |
kadonotakashi | 0:8fdf9a60065b | 115 | // Reset counters |
kadonotakashi | 0:8fdf9a60065b | 116 | (*exp_ok) = 0; |
kadonotakashi | 0:8fdf9a60065b | 117 | (*exp_no_mem) = 0; |
kadonotakashi | 0:8fdf9a60065b | 118 | (*exp_dns_failure) = 0; |
kadonotakashi | 0:8fdf9a60065b | 119 | (*exp_timeout) = 0; |
kadonotakashi | 0:8fdf9a60065b | 120 | |
kadonotakashi | 0:8fdf9a60065b | 121 | for (unsigned int i = 0; i < op_count; i++) { |
kadonotakashi | 0:8fdf9a60065b | 122 | SocketAddress address; |
kadonotakashi | 0:8fdf9a60065b | 123 | nsapi_error_t err = net->gethostbyname(hosts[i], &address); |
kadonotakashi | 0:8fdf9a60065b | 124 | |
kadonotakashi | 0:8fdf9a60065b | 125 | TEST_ASSERT(err == NSAPI_ERROR_OK || err == NSAPI_ERROR_NO_MEMORY || err == NSAPI_ERROR_DNS_FAILURE || err == NSAPI_ERROR_TIMEOUT); |
kadonotakashi | 0:8fdf9a60065b | 126 | if (err == NSAPI_ERROR_OK) { |
kadonotakashi | 0:8fdf9a60065b | 127 | (*exp_ok)++; |
kadonotakashi | 0:8fdf9a60065b | 128 | printf("DNS: query \"%s\" => \"%s\"\n", |
kadonotakashi | 0:8fdf9a60065b | 129 | hosts[i], address.get_ip_address()); |
kadonotakashi | 0:8fdf9a60065b | 130 | } else if (err == NSAPI_ERROR_DNS_FAILURE) { |
kadonotakashi | 0:8fdf9a60065b | 131 | (*exp_dns_failure)++; |
kadonotakashi | 0:8fdf9a60065b | 132 | printf("DNS: query \"%s\" => DNS failure\n", hosts[i]); |
kadonotakashi | 0:8fdf9a60065b | 133 | } else if (err == NSAPI_ERROR_TIMEOUT) { |
kadonotakashi | 0:8fdf9a60065b | 134 | (*exp_timeout)++; |
kadonotakashi | 0:8fdf9a60065b | 135 | printf("DNS: query \"%s\" => timeout\n", hosts[i]); |
kadonotakashi | 0:8fdf9a60065b | 136 | } else if (err == NSAPI_ERROR_NO_MEMORY) { |
kadonotakashi | 0:8fdf9a60065b | 137 | (*exp_no_mem)++; |
kadonotakashi | 0:8fdf9a60065b | 138 | printf("DNS: query \"%s\" => no memory\n", hosts[i]); |
kadonotakashi | 0:8fdf9a60065b | 139 | } |
kadonotakashi | 0:8fdf9a60065b | 140 | } |
kadonotakashi | 0:8fdf9a60065b | 141 | } |
kadonotakashi | 0:8fdf9a60065b | 142 | |
kadonotakashi | 0:8fdf9a60065b | 143 | NetworkInterface *get_interface() |
kadonotakashi | 0:8fdf9a60065b | 144 | { |
kadonotakashi | 0:8fdf9a60065b | 145 | return net; |
kadonotakashi | 0:8fdf9a60065b | 146 | } |
kadonotakashi | 0:8fdf9a60065b | 147 | |
kadonotakashi | 0:8fdf9a60065b | 148 | static void net_bringup() |
kadonotakashi | 0:8fdf9a60065b | 149 | { |
kadonotakashi | 0:8fdf9a60065b | 150 | 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); |
kadonotakashi | 0:8fdf9a60065b | 151 | |
kadonotakashi | 0:8fdf9a60065b | 152 | net = NetworkInterface::get_default_instance(); |
kadonotakashi | 0:8fdf9a60065b | 153 | nsapi_error_t err = net->connect(); |
kadonotakashi | 0:8fdf9a60065b | 154 | TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err); |
kadonotakashi | 0:8fdf9a60065b | 155 | printf("MBED: IP address is '%s'\n", net->get_ip_address()); |
kadonotakashi | 0:8fdf9a60065b | 156 | } |
kadonotakashi | 0:8fdf9a60065b | 157 | |
kadonotakashi | 0:8fdf9a60065b | 158 | // Test setup |
kadonotakashi | 0:8fdf9a60065b | 159 | utest::v1::status_t test_setup(const size_t number_of_cases) |
kadonotakashi | 0:8fdf9a60065b | 160 | { |
kadonotakashi | 0:8fdf9a60065b | 161 | GREENTEA_SETUP(120, "default_auto"); |
kadonotakashi | 0:8fdf9a60065b | 162 | net_bringup(); |
kadonotakashi | 0:8fdf9a60065b | 163 | return verbose_test_setup_handler(number_of_cases); |
kadonotakashi | 0:8fdf9a60065b | 164 | } |
kadonotakashi | 0:8fdf9a60065b | 165 | |
kadonotakashi | 0:8fdf9a60065b | 166 | Case cases[] = { |
kadonotakashi | 0:8fdf9a60065b | 167 | Case("ASYNCHRONOUS_DNS", ASYNCHRONOUS_DNS), |
kadonotakashi | 0:8fdf9a60065b | 168 | Case("ASYNCHRONOUS_DNS_SIMULTANEOUS", ASYNCHRONOUS_DNS_SIMULTANEOUS), |
kadonotakashi | 0:8fdf9a60065b | 169 | Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE", ASYNCHRONOUS_DNS_SIMULTANEOUS_CACHE), |
kadonotakashi | 0:8fdf9a60065b | 170 | Case("ASYNCHRONOUS_DNS_CACHE", ASYNCHRONOUS_DNS_CACHE), |
kadonotakashi | 0:8fdf9a60065b | 171 | Case("ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC", ASYNCHRONOUS_DNS_NON_ASYNC_AND_ASYNC), |
kadonotakashi | 0:8fdf9a60065b | 172 | Case("ASYNCHRONOUS_DNS_CANCEL", ASYNCHRONOUS_DNS_CANCEL), |
kadonotakashi | 0:8fdf9a60065b | 173 | Case("ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE", ASYNCHRONOUS_DNS_EXTERNAL_EVENT_QUEUE), |
kadonotakashi | 0:8fdf9a60065b | 174 | Case("ASYNCHRONOUS_DNS_INVALID_HOST", ASYNCHRONOUS_DNS_INVALID_HOST), |
kadonotakashi | 0:8fdf9a60065b | 175 | Case("ASYNCHRONOUS_DNS_TIMEOUTS", ASYNCHRONOUS_DNS_TIMEOUTS), |
kadonotakashi | 0:8fdf9a60065b | 176 | #ifdef MBED_EXTENDED_TESTS |
kadonotakashi | 0:8fdf9a60065b | 177 | Case("ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT", ASYNCHRONOUS_DNS_SIMULTANEOUS_REPEAT), |
kadonotakashi | 0:8fdf9a60065b | 178 | #endif |
kadonotakashi | 0:8fdf9a60065b | 179 | Case("SYNCHRONOUS_DNS", SYNCHRONOUS_DNS), |
kadonotakashi | 0:8fdf9a60065b | 180 | Case("SYNCHRONOUS_DNS_MULTIPLE", SYNCHRONOUS_DNS_MULTIPLE), |
kadonotakashi | 0:8fdf9a60065b | 181 | Case("SYNCHRONOUS_DNS_INVALID", SYNCHRONOUS_DNS_INVALID), |
kadonotakashi | 0:8fdf9a60065b | 182 | }; |
kadonotakashi | 0:8fdf9a60065b | 183 | |
kadonotakashi | 0:8fdf9a60065b | 184 | Specification specification(test_setup, cases); |
kadonotakashi | 0:8fdf9a60065b | 185 | |
kadonotakashi | 0:8fdf9a60065b | 186 | int main() |
kadonotakashi | 0:8fdf9a60065b | 187 | { |
kadonotakashi | 0:8fdf9a60065b | 188 | return !Harness::run(specification); |
kadonotakashi | 0:8fdf9a60065b | 189 | } |