wifi test
Dependencies: X_NUCLEO_IKS01A2 mbed-http
easy-connect/wizfi310-driver/TESTS/net/gethostbyname/main.cpp@0:24d3eb812fd4, 2018-09-05 (annotated)
- Committer:
- JMF
- Date:
- Wed Sep 05 14:28:24 2018 +0000
- Revision:
- 0:24d3eb812fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JMF | 0:24d3eb812fd4 | 1 | #include "mbed.h" |
JMF | 0:24d3eb812fd4 | 2 | #include "greentea-client/test_env.h" |
JMF | 0:24d3eb812fd4 | 3 | #include "unity.h" |
JMF | 0:24d3eb812fd4 | 4 | #include "utest.h" |
JMF | 0:24d3eb812fd4 | 5 | #include "WizFi310Interface.h" |
JMF | 0:24d3eb812fd4 | 6 | |
JMF | 0:24d3eb812fd4 | 7 | using namespace utest::v1; |
JMF | 0:24d3eb812fd4 | 8 | |
JMF | 0:24d3eb812fd4 | 9 | // Hostname for testing against |
JMF | 0:24d3eb812fd4 | 10 | // Must have A and AAAA records |
JMF | 0:24d3eb812fd4 | 11 | #ifndef MBED_DNS_TEST_HOST |
JMF | 0:24d3eb812fd4 | 12 | #define MBED_DNS_TEST_HOST "connector.mbed.com" |
JMF | 0:24d3eb812fd4 | 13 | #endif |
JMF | 0:24d3eb812fd4 | 14 | |
JMF | 0:24d3eb812fd4 | 15 | #ifndef MBED_CFG_WIZFI310_TX |
JMF | 0:24d3eb812fd4 | 16 | #define MBED_CFG_WIZFI310_TX D1 |
JMF | 0:24d3eb812fd4 | 17 | #endif |
JMF | 0:24d3eb812fd4 | 18 | |
JMF | 0:24d3eb812fd4 | 19 | #ifndef MBED_CFG_WIZFI310_RX |
JMF | 0:24d3eb812fd4 | 20 | #define MBED_CFG_WIZFI310_RX D0 |
JMF | 0:24d3eb812fd4 | 21 | #endif |
JMF | 0:24d3eb812fd4 | 22 | |
JMF | 0:24d3eb812fd4 | 23 | #ifndef MBED_CFG_WIZFI310_DEBUG |
JMF | 0:24d3eb812fd4 | 24 | #define MBED_CFG_WIZFI310_DEBUG false |
JMF | 0:24d3eb812fd4 | 25 | #endif |
JMF | 0:24d3eb812fd4 | 26 | |
JMF | 0:24d3eb812fd4 | 27 | #define STRINGIZE(x) STRINGIZE2(x) |
JMF | 0:24d3eb812fd4 | 28 | #define STRINGIZE2(x) #x |
JMF | 0:24d3eb812fd4 | 29 | |
JMF | 0:24d3eb812fd4 | 30 | // Address info from stack |
JMF | 0:24d3eb812fd4 | 31 | const char *ip_literal; |
JMF | 0:24d3eb812fd4 | 32 | nsapi_version_t ip_pref; |
JMF | 0:24d3eb812fd4 | 33 | const char *ip_pref_repr; |
JMF | 0:24d3eb812fd4 | 34 | |
JMF | 0:24d3eb812fd4 | 35 | // Network setup |
JMF | 0:24d3eb812fd4 | 36 | WizFi310Interface net(MBED_CFG_WIZFI310_TX, MBED_CFG_WIZFI310_RX, MBED_CFG_WIZFI310_DEBUG); |
JMF | 0:24d3eb812fd4 | 37 | void net_bringup() { |
JMF | 0:24d3eb812fd4 | 38 | int err = net.connect(STRINGIZE(MBED_CFG_WIZFI310_SSID), STRINGIZE(MBED_CFG_WIZFI310_PASS)); |
JMF | 0:24d3eb812fd4 | 39 | TEST_ASSERT_EQUAL(0, err); |
JMF | 0:24d3eb812fd4 | 40 | printf("MBED: Connected to network\n"); |
JMF | 0:24d3eb812fd4 | 41 | printf("MBED: IP Address: %s\n", net.get_ip_address()); |
JMF | 0:24d3eb812fd4 | 42 | |
JMF | 0:24d3eb812fd4 | 43 | ip_literal = net.get_ip_address(); |
JMF | 0:24d3eb812fd4 | 44 | ip_pref = SocketAddress(ip_literal).get_ip_version(); |
JMF | 0:24d3eb812fd4 | 45 | ip_pref_repr = (ip_pref == NSAPI_IPv4) ? "ipv4" : |
JMF | 0:24d3eb812fd4 | 46 | (ip_pref == NSAPI_IPv6) ? "ipv6" : "unspec"; |
JMF | 0:24d3eb812fd4 | 47 | } |
JMF | 0:24d3eb812fd4 | 48 | |
JMF | 0:24d3eb812fd4 | 49 | |
JMF | 0:24d3eb812fd4 | 50 | // DNS tests |
JMF | 0:24d3eb812fd4 | 51 | void test_dns_query() { |
JMF | 0:24d3eb812fd4 | 52 | SocketAddress addr; |
JMF | 0:24d3eb812fd4 | 53 | int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr); |
JMF | 0:24d3eb812fd4 | 54 | printf("DNS: query \"%s\" => \"%s\"\n", |
JMF | 0:24d3eb812fd4 | 55 | MBED_DNS_TEST_HOST, addr.get_ip_address()); |
JMF | 0:24d3eb812fd4 | 56 | |
JMF | 0:24d3eb812fd4 | 57 | TEST_ASSERT_EQUAL(0, err); |
JMF | 0:24d3eb812fd4 | 58 | TEST_ASSERT((bool)addr); |
JMF | 0:24d3eb812fd4 | 59 | TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
JMF | 0:24d3eb812fd4 | 60 | } |
JMF | 0:24d3eb812fd4 | 61 | |
JMF | 0:24d3eb812fd4 | 62 | void test_dns_query_pref() { |
JMF | 0:24d3eb812fd4 | 63 | SocketAddress addr; |
JMF | 0:24d3eb812fd4 | 64 | int err = net.gethostbyname(MBED_DNS_TEST_HOST, &addr, ip_pref); |
JMF | 0:24d3eb812fd4 | 65 | printf("DNS: query %s \"%s\" => \"%s\"\n", |
JMF | 0:24d3eb812fd4 | 66 | ip_pref_repr, MBED_DNS_TEST_HOST, addr.get_ip_address()); |
JMF | 0:24d3eb812fd4 | 67 | |
JMF | 0:24d3eb812fd4 | 68 | TEST_ASSERT_EQUAL(0, err); |
JMF | 0:24d3eb812fd4 | 69 | TEST_ASSERT((bool)addr); |
JMF | 0:24d3eb812fd4 | 70 | TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
JMF | 0:24d3eb812fd4 | 71 | TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version()); |
JMF | 0:24d3eb812fd4 | 72 | } |
JMF | 0:24d3eb812fd4 | 73 | |
JMF | 0:24d3eb812fd4 | 74 | void test_dns_literal() { |
JMF | 0:24d3eb812fd4 | 75 | SocketAddress addr; |
JMF | 0:24d3eb812fd4 | 76 | int err = net.gethostbyname(ip_literal, &addr); |
JMF | 0:24d3eb812fd4 | 77 | printf("DNS: literal \"%s\" => \"%s\"\n", |
JMF | 0:24d3eb812fd4 | 78 | ip_literal, addr.get_ip_address()); |
JMF | 0:24d3eb812fd4 | 79 | |
JMF | 0:24d3eb812fd4 | 80 | TEST_ASSERT_EQUAL(0, err); |
JMF | 0:24d3eb812fd4 | 81 | TEST_ASSERT((bool)addr); |
JMF | 0:24d3eb812fd4 | 82 | TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
JMF | 0:24d3eb812fd4 | 83 | TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0); |
JMF | 0:24d3eb812fd4 | 84 | } |
JMF | 0:24d3eb812fd4 | 85 | |
JMF | 0:24d3eb812fd4 | 86 | void test_dns_literal_pref() { |
JMF | 0:24d3eb812fd4 | 87 | SocketAddress addr; |
JMF | 0:24d3eb812fd4 | 88 | int err = net.gethostbyname(ip_literal, &addr, ip_pref); |
JMF | 0:24d3eb812fd4 | 89 | printf("DNS: literal %s \"%s\" => \"%s\"\n", |
JMF | 0:24d3eb812fd4 | 90 | ip_pref_repr, ip_literal, addr.get_ip_address()); |
JMF | 0:24d3eb812fd4 | 91 | |
JMF | 0:24d3eb812fd4 | 92 | TEST_ASSERT_EQUAL(0, err); |
JMF | 0:24d3eb812fd4 | 93 | TEST_ASSERT((bool)addr); |
JMF | 0:24d3eb812fd4 | 94 | TEST_ASSERT(strlen(addr.get_ip_address()) > 1); |
JMF | 0:24d3eb812fd4 | 95 | TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version()); |
JMF | 0:24d3eb812fd4 | 96 | TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0); |
JMF | 0:24d3eb812fd4 | 97 | } |
JMF | 0:24d3eb812fd4 | 98 | |
JMF | 0:24d3eb812fd4 | 99 | |
JMF | 0:24d3eb812fd4 | 100 | // Test setup |
JMF | 0:24d3eb812fd4 | 101 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
JMF | 0:24d3eb812fd4 | 102 | GREENTEA_SETUP(120, "default_auto"); |
JMF | 0:24d3eb812fd4 | 103 | net_bringup(); |
JMF | 0:24d3eb812fd4 | 104 | return verbose_test_setup_handler(number_of_cases); |
JMF | 0:24d3eb812fd4 | 105 | } |
JMF | 0:24d3eb812fd4 | 106 | |
JMF | 0:24d3eb812fd4 | 107 | Case cases[] = { |
JMF | 0:24d3eb812fd4 | 108 | Case("DNS query", test_dns_query), |
JMF | 0:24d3eb812fd4 | 109 | Case("DNS preference query", test_dns_query_pref), |
JMF | 0:24d3eb812fd4 | 110 | Case("DNS literal", test_dns_literal), |
JMF | 0:24d3eb812fd4 | 111 | Case("DNS preference literal", test_dns_literal_pref), |
JMF | 0:24d3eb812fd4 | 112 | }; |
JMF | 0:24d3eb812fd4 | 113 | |
JMF | 0:24d3eb812fd4 | 114 | Specification specification(test_setup, cases); |
JMF | 0:24d3eb812fd4 | 115 | |
JMF | 0:24d3eb812fd4 | 116 | int main() { |
JMF | 0:24d3eb812fd4 | 117 | return !Harness::run(specification); |
JMF | 0:24d3eb812fd4 | 118 | } |