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_UDPSocket.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/UDPSocket.h" 00020 #include "features/netsocket/nsapi_dns.h" 00021 #include "NetworkStack_stub.h" 00022 00023 /** 00024 * This test needs to access a private function 00025 * which is why it is declared as friend 00026 * following the documentation of google test. 00027 * [*](https://github.com/google/googletest/blob/master/googletest/docs/advanced.md) 00028 */ 00029 class UDPSocketFriend : public UDPSocket { 00030 friend class TestUDPSocket; 00031 FRIEND_TEST(TestUDPSocket, get_proto); 00032 }; 00033 00034 // Control the rtos EventFlags stub. See EventFlags_stub.cpp 00035 extern std::list<uint32_t> eventFlagsStubNextRetval; 00036 00037 class TestUDPSocket : public testing::Test { 00038 protected: 00039 UDPSocket *socket; 00040 NetworkStackstub stack; 00041 unsigned int dataSize = 10; 00042 char dataBuf[10]; 00043 00044 virtual void SetUp() 00045 { 00046 socket = new UDPSocket; 00047 stack.return_value = NSAPI_ERROR_OK ; 00048 } 00049 00050 virtual void TearDown() 00051 { 00052 delete socket; 00053 } 00054 }; 00055 00056 TEST_F(TestUDPSocket, get_proto) 00057 { 00058 UDPSocketFriend udpFriend; 00059 EXPECT_EQ(udpFriend.get_proto(), NSAPI_UDP ); 00060 } 00061 00062 TEST_F(TestUDPSocket, sendto_addr_port) 00063 { 00064 // The code below causes a segfault. Should we add a check? 00065 //EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), NSAPI_ERROR_NO_SOCKET); 00066 00067 const SocketAddress a("127.0.0.1", 1024); 00068 EXPECT_EQ(socket->sendto(a, 0, 0), NSAPI_ERROR_NO_SOCKET ); 00069 00070 socket->open((NetworkStack *)&stack); 00071 00072 stack.return_value = NSAPI_ERROR_PARAMETER ; 00073 EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), NSAPI_ERROR_DNS_FAILURE ); 00074 00075 stack.return_value = NSAPI_ERROR_OK ; 00076 EXPECT_EQ(socket->sendto("127.0.0.1", 0, 0, 0), 0); 00077 } 00078 00079 TEST_F(TestUDPSocket, connect) 00080 { 00081 stack.return_value = NSAPI_ERROR_OK ; 00082 const nsapi_addr_t addr = {NSAPI_IPv4 , {127,0,0,1} }; 00083 const SocketAddress a(addr, 1024); 00084 00085 socket->open((NetworkStack *)&stack); 00086 EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_NO_ADDRESS ); 00087 00088 EXPECT_EQ(socket->connect(a), NSAPI_ERROR_OK ); 00089 00090 stack.return_value = 100; 00091 EXPECT_EQ(socket->send(dataBuf, dataSize), 100); 00092 } 00093 00094 TEST_F(TestUDPSocket, sendto_timeout) 00095 { 00096 const nsapi_addr_t saddr = {NSAPI_IPv4 , {127,0,0,1} }; 00097 const SocketAddress addr(saddr, 1024); 00098 00099 socket->open((NetworkStack *)&stack); 00100 00101 stack.return_value = NSAPI_ERROR_WOULD_BLOCK ; 00102 eventFlagsStubNextRetval.push_back(0); 00103 eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop 00104 EXPECT_EQ(socket->sendto(addr, 0, 100), NSAPI_ERROR_WOULD_BLOCK ); 00105 00106 stack.return_value = NSAPI_ERROR_NO_MEMORY ; 00107 socket->set_timeout(1); 00108 00109 EXPECT_EQ(socket->sendto(addr, 0, 100), NSAPI_ERROR_NO_MEMORY ); 00110 } 00111 00112 TEST_F(TestUDPSocket, recv) 00113 { 00114 EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET ); 00115 00116 socket->open((NetworkStack *)&stack); 00117 00118 stack.return_value = 100; 00119 EXPECT_EQ(socket->recv(&dataBuf, dataSize), 100); 00120 00121 stack.return_value = NSAPI_ERROR_WOULD_BLOCK ; 00122 eventFlagsStubNextRetval.push_back(0); 00123 eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop 00124 EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK ); 00125 } 00126 00127 TEST_F(TestUDPSocket, recv_address_filtering) 00128 { 00129 socket->open((NetworkStack *)&stack); 00130 const nsapi_addr_t addr1 = {NSAPI_IPv4 , {127,0,0,1} }; 00131 const nsapi_addr_t addr2 = {NSAPI_IPv4 , {127,0,0,2} }; 00132 SocketAddress a1(addr1, 1024); 00133 SocketAddress a2(addr2, 1024); 00134 00135 EXPECT_EQ(socket->connect(a1), NSAPI_ERROR_OK ); 00136 00137 stack.return_values.push_back(100); //This will not return, because wrong address is used. 00138 stack.return_values.push_back(NSAPI_ERROR_NO_MEMORY ); //Break the loop of waiting for data from a1. 00139 EXPECT_EQ(socket->recvfrom(&a2, &dataBuf, dataSize), NSAPI_ERROR_NO_MEMORY ); 00140 00141 stack.return_values.push_back(100); 00142 EXPECT_EQ(socket->recvfrom(&a1, &dataBuf, dataSize), 100); 00143 } 00144 00145 TEST_F(TestUDPSocket, unsupported_api) 00146 { 00147 nsapi_error_t error; 00148 EXPECT_EQ(socket->accept(&error), static_cast<Socket *>(NULL)); 00149 EXPECT_EQ(error, NSAPI_ERROR_UNSUPPORTED ); 00150 EXPECT_EQ(socket->listen(1), NSAPI_ERROR_UNSUPPORTED ); 00151 }
Generated on Tue Aug 9 2022 00:37:22 by
1.7.2