Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2013-2017, 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 MBED_CONF_APP_HEADER_FILE
00027 
00028 using namespace utest::v1;
00029 
00030 // Hostname for testing against
00031 // Must have A and AAAA records
00032 #ifndef MBED_CONF_APP_DNS_TEST_HOST
00033 #define MBED_CONF_APP_DNS_TEST_HOST "connector.mbed.com"
00034 #endif
00035 
00036 
00037 // Address info from stack
00038 const char *ip_literal;
00039 nsapi_version_t ip_pref;
00040 const char *ip_pref_repr;
00041 
00042 // Network setup
00043 NetworkInterface *net;
00044 
00045 void net_bringup() {
00046     net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
00047     int err =  MBED_CONF_APP_CONNECT_STATEMENT;
00048     TEST_ASSERT_EQUAL(0, err);
00049     printf("MBED: Connected to network\n");
00050     printf("MBED: IP Address: %s\n", net->get_ip_address());
00051 
00052     ip_literal = net->get_ip_address();
00053     ip_pref = SocketAddress(ip_literal).get_ip_version();
00054     ip_pref_repr = (ip_pref == NSAPI_IPv4 ) ? "ipv4" :
00055                    (ip_pref == NSAPI_IPv6 ) ? "ipv6" : "unspec";
00056 }
00057 
00058 
00059 // DNS tests
00060 void test_dns_query() {
00061     SocketAddress addr;
00062     int err = net->gethostbyname(MBED_CONF_APP_DNS_TEST_HOST, &addr);
00063     printf("DNS: query \"%s\" => \"%s\"\n",
00064             MBED_CONF_APP_DNS_TEST_HOST, addr.get_ip_address());
00065 
00066     TEST_ASSERT_EQUAL(0, err);
00067     TEST_ASSERT((bool)addr);
00068     TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
00069 }
00070 
00071 void test_dns_query_pref() {
00072     SocketAddress addr;
00073     int err = net->gethostbyname(MBED_CONF_APP_DNS_TEST_HOST, &addr, ip_pref);
00074     printf("DNS: query %s \"%s\" => \"%s\"\n",
00075             ip_pref_repr, MBED_CONF_APP_DNS_TEST_HOST, addr.get_ip_address());
00076 
00077     TEST_ASSERT_EQUAL(0, err);
00078     TEST_ASSERT((bool)addr);
00079     TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
00080     TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
00081 }
00082 
00083 void test_dns_literal() {
00084     SocketAddress addr;
00085     int err = net->gethostbyname(ip_literal, &addr);
00086     printf("DNS: literal \"%s\" => \"%s\"\n",
00087             ip_literal, addr.get_ip_address());
00088 
00089     TEST_ASSERT_EQUAL(0, err);
00090     TEST_ASSERT((bool)addr);
00091     TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
00092     TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
00093 }
00094 
00095 void test_dns_literal_pref() {
00096     SocketAddress addr;
00097     int err = net->gethostbyname(ip_literal, &addr, ip_pref);
00098     printf("DNS: literal %s \"%s\" => \"%s\"\n",
00099             ip_pref_repr, ip_literal, addr.get_ip_address());
00100 
00101     TEST_ASSERT_EQUAL(0, err);
00102     TEST_ASSERT((bool)addr);
00103     TEST_ASSERT(strlen(addr.get_ip_address()) > 1);
00104     TEST_ASSERT_EQUAL(ip_pref, addr.get_ip_version());
00105     TEST_ASSERT(strcmp(ip_literal, addr.get_ip_address()) == 0);
00106 }
00107 
00108 
00109 // Test setup
00110 utest::v1::status_t test_setup(const size_t number_of_cases) {
00111     GREENTEA_SETUP(120, "default_auto");
00112     net_bringup();
00113     return verbose_test_setup_handler(number_of_cases);
00114 }
00115 
00116 Case cases[] = {
00117     Case("DNS query",               test_dns_query),
00118     Case("DNS preference query",    test_dns_query_pref),
00119     Case("DNS literal",             test_dns_literal),
00120     Case("DNS preference literal",  test_dns_literal_pref),
00121 };
00122 
00123 Specification specification(test_setup, cases);
00124 
00125 int main() {
00126     return !Harness::run(specification);
00127 }