|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "gtest/gtest.h" |
| 19 | +#include "gmock/gmock.h" |
| 20 | + |
| 21 | +#include "OnboardNetworkStack_mock.h" |
| 22 | + |
| 23 | +#include "features/netsocket/PPPInterface.h" |
| 24 | +#include <iostream> |
| 25 | +#include "FileHandle_stub.h" |
| 26 | + |
| 27 | + |
| 28 | +OnboardNetworkStack &OnboardNetworkStack::get_default_instance() |
| 29 | +{ |
| 30 | + return OnboardNetworkStackMock::get_instance(); |
| 31 | +} |
| 32 | + |
| 33 | +using ::testing::_; |
| 34 | +using ::testing::Return; |
| 35 | +using ::testing::ReturnArg; |
| 36 | +using ::testing::SaveArg; |
| 37 | +using ::testing::SaveArgPointee; |
| 38 | +using ::testing::SetArrayArgument; |
| 39 | +using ::testing::SetArgPointee; |
| 40 | +using ::testing::SetArgReferee; |
| 41 | + |
| 42 | +class MockPPP : public PPP { |
| 43 | +public: |
| 44 | + MOCK_METHOD0(power_up, bool()); |
| 45 | + MOCK_METHOD0(power_down, void()); |
| 46 | + MOCK_METHOD0(get_mtu_size, uint32_t()); |
| 47 | + MOCK_CONST_METHOD0(get_align_preference, uint32_t()); |
| 48 | + MOCK_CONST_METHOD2(get_ifname, void(char *name, uint8_t size)); |
| 49 | + MOCK_METHOD2(link_out, bool(net_stack_mem_buf_t *buf, nsapi_ip_stack_t ip_stack)); |
| 50 | + MOCK_METHOD1(set_stream, void(mbed::FileHandle *stream)); |
| 51 | + MOCK_METHOD1(set_ip_stack, void(nsapi_ip_stack_t ip_stack)); |
| 52 | + MOCK_METHOD2(set_credentials, void(const char *uname, const char *password)); |
| 53 | + MOCK_METHOD1(get_ip_address, const nsapi_addr_t *(nsapi_version_t version)); |
| 54 | + MOCK_METHOD0(get_netmask, const nsapi_addr_t *()); |
| 55 | + MOCK_METHOD0(get_gateway, const nsapi_addr_t *()); |
| 56 | + MOCK_METHOD1(get_dns_server, const nsapi_addr_t *(uint8_t index)); |
| 57 | + MOCK_METHOD1(set_link_input_cb, void(ppp_link_input_cb_t input_cb)); |
| 58 | + MOCK_METHOD1(set_link_state_cb, void(ppp_link_state_change_cb_t state_cb)); |
| 59 | + MOCK_METHOD1(set_memory_manager, void(NetStackMemoryManager &mem_mngr)); |
| 60 | + |
| 61 | + static MockPPP &get_instance() |
| 62 | + { |
| 63 | + static MockPPP pppMock1; |
| 64 | + return pppMock1; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +MBED_WEAK PPP &PPP::get_default_instance() |
| 69 | +{ |
| 70 | + return MockPPP::get_instance(); |
| 71 | +} |
| 72 | + |
| 73 | +class TestPPPInterface: public testing::Test { |
| 74 | +protected: |
| 75 | + PPPInterface *iface; |
| 76 | + OnboardNetworkStackMock *stackMock; |
| 77 | + MockPPP *pppMock; |
| 78 | + OnboardNetworkStackMock::InterfaceMock *netStackIface; |
| 79 | + virtual void SetUp() |
| 80 | + { |
| 81 | + stackMock = &OnboardNetworkStackMock::get_instance(); |
| 82 | + pppMock = &MockPPP::get_instance(); |
| 83 | + netStackIface = &OnboardNetworkStackMock::InterfaceMock::get_instance(); |
| 84 | + iface = new PPPInterface(MockPPP::get_instance(), OnboardNetworkStackMock::get_instance()); |
| 85 | + } |
| 86 | + |
| 87 | + virtual void TearDown() |
| 88 | + { |
| 89 | + // Do not delete the mocks pointers, as they point to statically allocated singletons. |
| 90 | + delete iface; |
| 91 | + } |
| 92 | + |
| 93 | + /* Enclose the heavily-used connection procedure to improve code redability */ |
| 94 | + void doConnect(bool blocking = true) |
| 95 | + { |
| 96 | + EXPECT_CALL(*pppMock, set_stream(_)); |
| 97 | + EXPECT_CALL(*pppMock, set_ip_stack(_)); |
| 98 | + EXPECT_CALL(*pppMock, set_credentials(_, _)); |
| 99 | + EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _)) |
| 100 | + .Times(1) |
| 101 | + .WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_OK))); |
| 102 | + EXPECT_CALL(*netStackIface, attach(_)) |
| 103 | + .Times(1) |
| 104 | + .RetiresOnSaturation(); |
| 105 | + EXPECT_CALL(*netStackIface, bringup(false, NULL, NULL, NULL, DEFAULT_STACK, blocking)) |
| 106 | + .Times(1) |
| 107 | + .WillOnce(Return(NSAPI_ERROR_OK)); |
| 108 | + EXPECT_EQ(NSAPI_ERROR_OK, iface->connect()); |
| 109 | + } |
| 110 | + |
| 111 | + static void cb(nsapi_event_t ev, intptr_t ptr) |
| 112 | + { |
| 113 | + |
| 114 | + } |
| 115 | +}; |
| 116 | + |
| 117 | +TEST_F(TestPPPInterface, constructor_default) |
| 118 | +{ |
| 119 | + EXPECT_TRUE(iface); |
| 120 | + // Test that this clas presents itself correctly |
| 121 | + EXPECT_NE(nullptr, iface->pppInterface()); |
| 122 | + |
| 123 | + EXPECT_EQ(nullptr, iface->emacInterface()); |
| 124 | + EXPECT_EQ(nullptr, iface->ethInterface()); |
| 125 | + EXPECT_EQ(nullptr, iface->wifiInterface()); |
| 126 | + EXPECT_EQ(nullptr, iface->cellularBase()); |
| 127 | + EXPECT_EQ(nullptr, iface->cellularInterface()); |
| 128 | + EXPECT_EQ(nullptr, iface->meshInterface()); |
| 129 | +} |
| 130 | + |
| 131 | +TEST_F(TestPPPInterface, connect) |
| 132 | +{ |
| 133 | + mbed::FileHandle_stub handle; |
| 134 | + nsapi_ip_stack_t stack = IPV4_STACK; |
| 135 | + iface->set_credentials("uname", "passwd"); |
| 136 | + iface->set_stream(&handle); |
| 137 | + iface->set_ip_stack(stack); |
| 138 | + |
| 139 | + EXPECT_CALL(*pppMock, set_stream(&handle)); |
| 140 | + EXPECT_CALL(*pppMock, set_ip_stack(stack)); |
| 141 | + EXPECT_CALL(*pppMock, set_credentials("uname", "passwd")); |
| 142 | + EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _)) |
| 143 | + .Times(1) |
| 144 | + .WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_OK))); |
| 145 | + EXPECT_CALL(*netStackIface, attach(_)) |
| 146 | + .Times(1) |
| 147 | + .RetiresOnSaturation(); |
| 148 | + EXPECT_CALL(*netStackIface, bringup(false, NULL, NULL, NULL, stack, true)) |
| 149 | + .Times(1) |
| 150 | + .WillOnce(Return(NSAPI_ERROR_OK)); |
| 151 | + EXPECT_EQ(NSAPI_ERROR_OK, iface->connect()); |
| 152 | +} |
| 153 | + |
| 154 | +TEST_F(TestPPPInterface, connect_failure) |
| 155 | +{ |
| 156 | + EXPECT_CALL(*pppMock, set_stream(_)); |
| 157 | + EXPECT_CALL(*pppMock, set_ip_stack(_)); |
| 158 | + EXPECT_CALL(*pppMock, set_credentials(_, _)); |
| 159 | + EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _)) |
| 160 | + .Times(1) |
| 161 | + .WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_NO_MEMORY))); |
| 162 | + EXPECT_EQ(NSAPI_ERROR_NO_MEMORY, iface->connect()); |
| 163 | +} |
| 164 | + |
| 165 | +TEST_F(TestPPPInterface, disconnect_without_connecting) |
| 166 | +{ |
| 167 | + EXPECT_EQ(NSAPI_ERROR_NO_CONNECTION, iface->disconnect()); |
| 168 | +} |
| 169 | + |
| 170 | +TEST_F(TestPPPInterface, disconnect) |
| 171 | +{ |
| 172 | + doConnect(); |
| 173 | + |
| 174 | + EXPECT_CALL(*netStackIface, bringdown()) |
| 175 | + .Times(1) |
| 176 | + .WillOnce(Return(NSAPI_ERROR_OK)); |
| 177 | + EXPECT_EQ(NSAPI_ERROR_OK, iface->disconnect()); |
| 178 | +} |
| 179 | + |
| 180 | +TEST_F(TestPPPInterface, set_network) |
| 181 | +{ |
| 182 | + char ipAddress[NSAPI_IPv4_SIZE] = "127.0.0.1"; |
| 183 | + char netmask[NSAPI_IPv4_SIZE] = "255.255.0.0"; |
| 184 | + char gateway[NSAPI_IPv4_SIZE] = "127.0.0.2"; |
| 185 | + |
| 186 | + const char *ipAddressArg; |
| 187 | + const char *netmaskArg; |
| 188 | + const char *gatewayArg; |
| 189 | + |
| 190 | + EXPECT_EQ(0, iface->get_ip_address()); |
| 191 | + EXPECT_EQ(0, iface->get_netmask()); |
| 192 | + EXPECT_EQ(0, iface->get_gateway()); |
| 193 | + |
| 194 | + EXPECT_CALL(*pppMock, set_stream(_)); |
| 195 | + EXPECT_CALL(*pppMock, set_ip_stack(_)); |
| 196 | + EXPECT_CALL(*pppMock, set_credentials(_, _)); |
| 197 | + |
| 198 | + // Set the network data |
| 199 | + EXPECT_EQ(NSAPI_ERROR_OK, iface->set_network(ipAddress, netmask, gateway)); |
| 200 | + |
| 201 | + // Now the bringup should have different arguments. We can't use doConnect method. |
| 202 | + EXPECT_CALL(*stackMock, add_ppp_interface(testing::Ref(*pppMock), true, _)) |
| 203 | + .Times(1) |
| 204 | + .WillOnce(DoAll(SetArgPointee<2>(netStackIface), Return(NSAPI_ERROR_OK))); |
| 205 | + EXPECT_CALL(*netStackIface, attach(_)) |
| 206 | + .Times(1) |
| 207 | + .RetiresOnSaturation(); |
| 208 | + // Do not put the expected char * arguments, as they are pointers and would not match |
| 209 | + EXPECT_CALL(*netStackIface, bringup(false, _, _, _, DEFAULT_STACK, true)) |
| 210 | + .Times(1) |
| 211 | + .WillOnce(DoAll(SaveArg<1>(&ipAddressArg), |
| 212 | + SaveArg<2>(&netmaskArg), |
| 213 | + SaveArg<3>(&gatewayArg), |
| 214 | + Return(NSAPI_ERROR_OK))); |
| 215 | + EXPECT_EQ(NSAPI_ERROR_OK, iface->connect()); |
| 216 | + // Check the contents of the stored pointer arguments. |
| 217 | + EXPECT_TRUE(0 == strcmp(ipAddress, ipAddressArg)); |
| 218 | + EXPECT_TRUE(0 == strcmp(netmask, netmaskArg)); |
| 219 | + EXPECT_TRUE(0 == strcmp(gateway, gatewayArg)); |
| 220 | + |
| 221 | + // Testing the getters makes sense now. |
| 222 | + EXPECT_CALL(*netStackIface, get_ip_address(_, _)) |
| 223 | + .Times(1) |
| 224 | + .WillOnce(DoAll(SetArgPointee<0>(*ipAddress), Return(ipAddress))); |
| 225 | + EXPECT_EQ(std::string(ipAddress), std::string(iface->get_ip_address())); |
| 226 | + |
| 227 | + EXPECT_CALL(*netStackIface, get_netmask(_, _)) |
| 228 | + .Times(1) |
| 229 | + .WillOnce(DoAll(SetArgPointee<0>(*netmask), Return(netmask))); |
| 230 | + EXPECT_EQ(std::string(netmask), std::string(iface->get_netmask())); |
| 231 | + |
| 232 | + EXPECT_CALL(*netStackIface, get_gateway(_, _)) |
| 233 | + .Times(1) |
| 234 | + .WillOnce(DoAll(SetArgPointee<0>(*gateway), Return(gateway))); |
| 235 | + EXPECT_EQ(std::string(gateway), std::string(iface->get_gateway())); |
| 236 | +} |
| 237 | + |
| 238 | +TEST_F(TestPPPInterface, get_connection_status) |
| 239 | +{ |
| 240 | + EXPECT_EQ(NSAPI_STATUS_DISCONNECTED, iface->get_connection_status()); |
| 241 | + |
| 242 | + doConnect(); |
| 243 | + |
| 244 | + EXPECT_CALL(*netStackIface, get_connection_status()) |
| 245 | + .Times(1) |
| 246 | + .WillOnce(Return(NSAPI_STATUS_LOCAL_UP)); |
| 247 | + EXPECT_EQ(NSAPI_STATUS_LOCAL_UP, iface->get_connection_status()); |
| 248 | +} |
| 249 | + |
| 250 | +TEST_F(TestPPPInterface, get_interface_name) |
| 251 | +{ |
| 252 | + char name[100]; |
| 253 | + memset(name, '\0', 100); |
| 254 | + EXPECT_EQ(NULL, iface->get_interface_name(name)); |
| 255 | + |
| 256 | + doConnect(); |
| 257 | + |
| 258 | + EXPECT_EQ(NULL, iface->get_interface_name(name)); |
| 259 | + |
| 260 | + EXPECT_CALL(*netStackIface, get_interface_name(name)) |
| 261 | + .Times(1) |
| 262 | + .WillOnce(Return(name)); |
| 263 | + EXPECT_EQ(name, iface->get_interface_name(name)); |
| 264 | +} |
| 265 | + |
| 266 | +TEST_F(TestPPPInterface, set_as_default) |
| 267 | +{ |
| 268 | + doConnect(); |
| 269 | + |
| 270 | + EXPECT_CALL(*stackMock, set_default_interface(netStackIface)) |
| 271 | + .Times(1); |
| 272 | + iface->set_as_default(); |
| 273 | +} |
| 274 | + |
| 275 | +TEST_F(TestPPPInterface, attach) |
| 276 | +{ |
| 277 | + doConnect(); |
| 278 | + EXPECT_CALL(*netStackIface, attach(_)) // TODO: check that the correct function is passed. |
| 279 | + .Times(1); |
| 280 | + iface->attach(cb); |
| 281 | +} |
| 282 | + |
| 283 | +TEST_F(TestPPPInterface, set_dhcp) |
| 284 | +{ |
| 285 | + EXPECT_EQ(NSAPI_ERROR_UNSUPPORTED, iface->set_dhcp(false)); |
| 286 | + doConnect(true); |
| 287 | +} |
| 288 | + |
| 289 | +TEST_F(TestPPPInterface, set_blocking) |
| 290 | +{ |
| 291 | + EXPECT_EQ(NSAPI_ERROR_OK, iface->set_blocking(false)); |
| 292 | + doConnect(false); |
| 293 | +} |
0 commit comments