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.
test_NetworkStack.cpp
00001 /* 00002 * Copyright (c) 2018, Arm Limited and affiliates 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may 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, 00013 * WITHOUT 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 #include "gtest/gtest.h" 00019 #include "features/netsocket/NetworkStack.h" 00020 #include "netsocket/nsapi_dns.h" 00021 #include "events/EventQueue.h" 00022 #include <string> 00023 00024 #include "equeue_stub.h" 00025 00026 // Control nsapi stub return value. See stubs/nsapi_dns_stub.cpp 00027 extern nsapi_error_t nsapi_stub_return_value; 00028 00029 // For testing async calls which require callback 00030 static bool callback_is_called; 00031 static SocketAddress address_received; 00032 static nsapi_error_t result_received; 00033 static void my_callback(nsapi_error_t result, SocketAddress *address) 00034 { 00035 result_received = result; 00036 address_received = *address; 00037 callback_is_called = true; 00038 } 00039 00040 static bool noarg_callback_is_called; 00041 static void noarg_callback(void) 00042 { 00043 noarg_callback_is_called = true; 00044 } 00045 00046 extern call_in_callback_cb_t callin_callback; 00047 00048 namespace mbed { 00049 extern events::EventQueue *mbed_shared_queue_stub; 00050 } 00051 00052 // NetworkStack is an abstract class we need to provide a child class for tests. 00053 class NetworkStackChild : public NetworkStack { 00054 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto) 00055 { 00056 return NSAPI_ERROR_OK ; 00057 } 00058 virtual nsapi_error_t socket_close(nsapi_socket_t handle) 00059 { 00060 return NSAPI_ERROR_OK ; 00061 } 00062 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address) 00063 { 00064 return NSAPI_ERROR_OK ; 00065 } 00066 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog) 00067 { 00068 return NSAPI_ERROR_OK ; 00069 } 00070 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address) 00071 { 00072 return NSAPI_ERROR_OK ; 00073 } 00074 virtual nsapi_error_t socket_accept(nsapi_socket_t server, 00075 nsapi_socket_t *handle, SocketAddress *address = 0) 00076 { 00077 return NSAPI_ERROR_OK ; 00078 } 00079 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, 00080 const void *data, nsapi_size_t size) 00081 { 00082 return NSAPI_ERROR_OK ; 00083 } 00084 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, 00085 void *data, nsapi_size_t size) 00086 { 00087 return NSAPI_ERROR_OK ; 00088 } 00089 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, 00090 const void *data, nsapi_size_t size) 00091 { 00092 return NSAPI_ERROR_OK ; 00093 } 00094 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, 00095 void *buffer, nsapi_size_t size) 00096 { 00097 return NSAPI_ERROR_OK ; 00098 } 00099 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data) 00100 { 00101 } 00102 public: 00103 std::string ip_address; 00104 const char *get_ip_address() 00105 { 00106 return ip_address.c_str(); 00107 00108 } 00109 }; 00110 00111 class TestNetworkStack : public testing::Test { 00112 protected: 00113 NetworkStackChild *stack; 00114 00115 virtual void SetUp() 00116 { 00117 stack = new NetworkStackChild(); 00118 address_received = SocketAddress(); 00119 result_received = NSAPI_ERROR_OK ; 00120 callback_is_called = false; 00121 noarg_callback_is_called = false; 00122 mbed::mbed_shared_queue_stub = 0; 00123 } 00124 00125 virtual void TearDown() 00126 { 00127 delete stack; 00128 if (mbed::mbed_shared_queue_stub) 00129 { 00130 delete mbed::mbed_shared_queue_stub; 00131 mbed::mbed_shared_queue_stub = 0; 00132 } 00133 } 00134 00135 void SetUpQueue() 00136 { 00137 mbed::mbed_shared_queue_stub = new events::EventQueue(); 00138 } 00139 }; 00140 00141 TEST_F(TestNetworkStack, constructor) 00142 { 00143 EXPECT_TRUE(stack); 00144 } 00145 00146 TEST_F(TestNetworkStack, get_ip_address_default) 00147 { 00148 EXPECT_EQ(stack->NetworkStack::get_ip_address(), (char*)NULL); 00149 } 00150 00151 /* gethostbyname */ 00152 00153 TEST_F(TestNetworkStack, gethostbyname) 00154 { 00155 SocketAddress a; 00156 stack->ip_address = std::string("127.0.0.1"); 00157 EXPECT_EQ(stack->gethostbyname("host", &a, NSAPI_UNSPEC ), NSAPI_ERROR_DNS_FAILURE ); 00158 } 00159 00160 TEST_F(TestNetworkStack, gethostbyname_simple_address) 00161 { 00162 SocketAddress a; 00163 EXPECT_EQ(stack->gethostbyname("127.0.0.1", &a, NSAPI_UNSPEC ), NSAPI_ERROR_OK ); 00164 } 00165 00166 TEST_F(TestNetworkStack, gethostbyname_simple_address_right_version) 00167 { 00168 SocketAddress a; 00169 EXPECT_EQ(stack->gethostbyname("127.0.0.1", &a, NSAPI_IPv4 ), NSAPI_ERROR_OK ); 00170 } 00171 00172 TEST_F(TestNetworkStack, gethostbyname_simple_address_wrong_version) 00173 { 00174 SocketAddress a; 00175 EXPECT_EQ(stack->gethostbyname("127.0.0.1", &a, NSAPI_IPv6 ), NSAPI_ERROR_DNS_FAILURE ); 00176 } 00177 00178 TEST_F(TestNetworkStack, gethostbyname_empty_host) 00179 { 00180 SocketAddress a; 00181 EXPECT_EQ(stack->gethostbyname("", &a, NSAPI_UNSPEC ), NSAPI_ERROR_PARAMETER ); 00182 } 00183 00184 /* gethostbyname_async */ 00185 00186 TEST_F(TestNetworkStack, gethostbyname_async_delay) 00187 { 00188 SocketAddress a; 00189 stack->ip_address = std::string("127.0.0.1"); 00190 SetUpQueue(); 00191 struct equeue_event ptr; 00192 equeue_stub.void_ptr = &ptr; 00193 equeue_stub.call_cb_immediately = true; 00194 EXPECT_EQ(stack->gethostbyname_async("localhost", mbed::callback(my_callback), NSAPI_UNSPEC ), NSAPI_ERROR_DNS_FAILURE ); 00195 EXPECT_EQ(callin_callback(1, mbed::callback(noarg_callback)), NSAPI_ERROR_OK ); 00196 EXPECT_TRUE(noarg_callback_is_called); 00197 EXPECT_FALSE(callback_is_called); 00198 } 00199 00200 TEST_F(TestNetworkStack, gethostbyname_async) 00201 { 00202 SocketAddress a; 00203 stack->ip_address = std::string("127.0.0.1"); 00204 SetUpQueue(); 00205 struct equeue_event ptr; 00206 equeue_stub.void_ptr = &ptr; 00207 equeue_stub.call_cb_immediately = true; 00208 EXPECT_EQ(stack->gethostbyname_async("localhost", mbed::callback(my_callback), NSAPI_UNSPEC ), NSAPI_ERROR_DNS_FAILURE ); 00209 EXPECT_EQ(callin_callback(0, mbed::callback(noarg_callback)), NSAPI_ERROR_OK ); 00210 EXPECT_TRUE(noarg_callback_is_called); 00211 EXPECT_FALSE(callback_is_called); 00212 } 00213 00214 TEST_F(TestNetworkStack, gethostbyname_async_eventqueue_simple_address) 00215 { 00216 SocketAddress a; 00217 stack->ip_address = std::string("127.0.0.1"); 00218 EXPECT_EQ(stack->gethostbyname_async("127.0.0.1", mbed::callback(my_callback), NSAPI_IPv6 ), NSAPI_ERROR_DNS_FAILURE ); 00219 EXPECT_FALSE(callback_is_called); 00220 EXPECT_EQ(stack->gethostbyname_async("127.0.0.1", mbed::callback(my_callback), NSAPI_IPv4 ), NSAPI_ERROR_OK ); 00221 EXPECT_TRUE(callback_is_called); 00222 EXPECT_EQ(result_received, NSAPI_ERROR_OK ); 00223 EXPECT_EQ(std::string(address_received.get_ip_address()), stack->ip_address); 00224 } 00225 00226 TEST_F(TestNetworkStack, gethostbyname_async_empty_host) 00227 { 00228 SocketAddress a; 00229 EXPECT_EQ(stack->gethostbyname_async("", mbed::callback(my_callback), NSAPI_UNSPEC ), NSAPI_ERROR_PARAMETER ); 00230 } 00231 00232 TEST_F(TestNetworkStack, getstackopt) 00233 { 00234 EXPECT_EQ(stack->getstackopt(0,0,0,0), NSAPI_ERROR_UNSUPPORTED ); 00235 } 00236 00237 TEST_F(TestNetworkStack, setstackopt) 00238 { 00239 EXPECT_EQ(stack->setstackopt(0,0,0,0), NSAPI_ERROR_UNSUPPORTED ); 00240 } 00241
Generated on Tue Aug 9 2022 00:37:22 by
1.7.2