|
| 1 | +#if !FEATURE_LWIP |
| 2 | + #error [NOT_SUPPORTED] LWIP not supported for this target |
| 3 | +#endif |
| 4 | +#if DEVICE_EMAC |
| 5 | + #error [NOT_SUPPORTED] Not supported for WiFi targets |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "mbed.h" |
| 9 | +#include "greentea-client/test_env.h" |
| 10 | +#include "unity.h" |
| 11 | +#include "utest.h" |
| 12 | +#include "ESP8266Interface.h" |
| 13 | + |
| 14 | +using namespace utest::v1; |
| 15 | + |
| 16 | +// Hostname for testing against |
| 17 | +// Must have A and AAAA records |
| 18 | +#ifndef MBED_DNS_TEST_HOST |
| 19 | +#define MBED_DNS_TEST_HOST "connector.mbed.com" |
| 20 | +#endif |
| 21 | + |
| 22 | +#ifndef MBED_CFG_ESP8266_TX |
| 23 | +#define MBED_CFG_ESP8266_TX D1 |
| 24 | +#endif |
| 25 | + |
| 26 | +#ifndef MBED_CFG_ESP8266_RX |
| 27 | +#define MBED_CFG_ESP8266_RX D0 |
| 28 | +#endif |
| 29 | + |
| 30 | +#ifndef MBED_CFG_ESP8266_DEBUG |
| 31 | +#define MBED_CFG_ESP8266_DEBUG false |
| 32 | +#endif |
| 33 | + |
| 34 | +// Address info from stack |
| 35 | +const char *ip_literal; |
| 36 | +nsapi_version_t ip_pref; |
| 37 | +const char *ip_pref_repr; |
| 38 | + |
| 39 | +// Network setup |
| 40 | +ESP8266Interface net(MBED_CFG_ESP8266_TX, MBED_CFG_ESP8266_RX, MBED_CFG_ESP8266_DEBUG); |
| 41 | +void net_bringup() { |
| 42 | + int err = net.connect(MBED_CFG_ESP8266_SSID, MBED_CFG_ESP8266_PASS); |
| 43 | + TEST_ASSERT_EQUAL(0, err); |
| 44 | + printf("MBED: Connected to network\n"); |
| 45 | + printf("MBED: IP Address: %s\n", net.get_ip_address()); |
| 46 | + |
| 47 | + ip_literal = net.get_ip_address(); |
| 48 | + ip_pref = SocketAddress(ip_literal).get_ip_version(); |
| 49 | + ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" : |
| 50 | + (ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec"; |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +// DNS tests |
| 55 | +void test_dns_query() { |
| 56 | + SocketAddress addr; |
| 57 | + int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr); |
| 58 | + printf("DNS: query \"%s\" => \"%s\"\n", |
| 59 | + MBED_DNS_TEST_HOST, addr.get_ip_address()); |
| 60 | + |
| 61 | + TEST_ASSERT_EQUAL(0, err); |
| 62 | + TEST_ASSERT((bool)addr); |
| 63 | + TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
| 64 | +} |
| 65 | + |
| 66 | +void test_dns_query_pref() { |
| 67 | + SocketAddress addr; |
| 68 | + int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref); |
| 69 | + printf("DNS: query %s \"%s\" => \"%s\"\n", |
| 70 | + ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address()); |
| 71 | + |
| 72 | + TEST_ASSERT_EQUAL(0, err); |
| 73 | + TEST_ASSERT((bool)addr); |
| 74 | + TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
| 75 | + TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version()); |
| 76 | +} |
| 77 | + |
| 78 | +void test_dns_literal() { |
| 79 | + SocketAddress addr; |
| 80 | + int err = net.gethostbyname(ip_literal, &addr); |
| 81 | + printf("DNS: literal \"%s\" => \"%s\"\n", |
| 82 | + ip_literal, addr.get_ip_address()); |
| 83 | + |
| 84 | + TEST_ASSERT_EQUAL(0, err); |
| 85 | + TEST_ASSERT((bool)addr); |
| 86 | + TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
| 87 | + TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0); |
| 88 | +} |
| 89 | + |
| 90 | +void test_dns_literal_pref() { |
| 91 | + SocketAddress addr; |
| 92 | + int err = net.gethostbyname(ip_literal, &addr, ip_pref); |
| 93 | + printf("DNS: literal %s \"%s\" => \"%s\"\n", |
| 94 | + ip_pref_repr, ip_literal, addr.get_ip_address()); |
| 95 | + |
| 96 | + TEST_ASSERT_EQUAL(0, err); |
| 97 | + TEST_ASSERT((bool)addr); |
| 98 | + TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
| 99 | + TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version()); |
| 100 | + TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0); |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +// Test setup |
| 105 | +utest::v1::status_t test_setup(const size_t number_of_cases) { |
| 106 | + char uuid[48] = {0}; |
| 107 | + GREENTEA_SETUP_UUID(120, "default_auto", uuid, 48); |
| 108 | + |
| 109 | + // create mac address based on uuid |
| 110 | + uint64_t mac = 0; |
| 111 | + for (int i = 0; i < sizeof(uuid); i++) { |
| 112 | + mac += uuid[i]; |
| 113 | + } |
| 114 | + mbed_set_mac_address((const char*)mac, /*coerce control bits*/ 1); |
| 115 | + net_bringup(); |
| 116 | + |
| 117 | + return verbose_test_setup_handler(number_of_cases); |
| 118 | +} |
| 119 | + |
| 120 | +Case cases[] = { |
| 121 | + Case("DNS query", test_dns_query), |
| 122 | + Case("DNS preference query", test_dns_query_pref), |
| 123 | + Case("DNS literal", test_dns_literal), |
| 124 | + Case("DNS preference literal", test_dns_literal_pref), |
| 125 | +}; |
| 126 | + |
| 127 | +Specification specification(test_setup, cases); |
| 128 | + |
| 129 | +int main() { |
| 130 | + return !Harness::run(specification); |
| 131 | +} |
0 commit comments