Skip to content

Modify nsapi_dns tests to be module tests #12027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

####################
# UNIT TESTS
####################

set(unittest-sources
../features/netsocket/SocketAddress.cpp
../features/netsocket/NetworkInterface.cpp
../features/netsocket/NetworkInterfaceDefaults.cpp
../features/netsocket/NetworkStack.cpp #nsapi_create_stack
../features/netsocket/InternetSocket.cpp
../features/netsocket/TCPSocket.cpp
../features/netsocket/InternetDatagramSocket.cpp
../features/netsocket/UDPSocket.cpp
../features/netsocket/SocketStats.cpp
../features/netsocket/EthernetInterface.cpp
../features/netsocket/EMACInterface.cpp
../features/netsocket/nsapi_dns.cpp
../features/frameworks/nanostack-libservice/source/libip4string/ip4tos.c
../features/frameworks/nanostack-libservice/source/libip6string/ip6tos.c
../features/frameworks/nanostack-libservice/source/libip4string/stoip4.c
../features/frameworks/nanostack-libservice/source/libip6string/stoip6.c
../features/frameworks/nanostack-libservice/source/libBits/common_functions.c
../features/frameworks/nanostack-libservice/source/libBits/common_functions.c
../features/frameworks/nanostack-libservice/source/libList/ns_list.c
)

set(unittest-test-sources
MODULETESTS/features/netsocket/IfaceDnsSocket/test_IfaceDnsSocket.cpp
stubs/MeshInterface_stub.cpp
stubs/CellularInterface_stub.cpp
stubs/Mutex_stub.cpp
stubs/mbed_assert_stub.cpp
stubs/mbed_atomic_stub.c
stubs/mbed_critical_stub.c
stubs/mbed_rtos_rtx_stub.c
stubs/equeue_stub.c
stubs/EventQueue_stub.cpp
stubs/Kernel_stub.cpp
stubs/mbed_error.c
stubs/mbed_shared_queues_stub.cpp
stubs/rtx_mutex_stub.c
stubs/EventFlags_stub.cpp
)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEVICE_EMAC -DMBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE=ETHERNET -DMBED_CONF_NSAPI_DNS_RESPONSE_WAIT_TIME=10000 -DMBED_CONF_NSAPI_DNS_RETRIES=1 -DMBED_CONF_NSAPI_DNS_TOTAL_ATTEMPTS=10 -DMBED_CONF_NSAPI_DNS_CACHE_SIZE=5")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEVICE_EMAC -DMBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE=ETHERNET -DMBED_CONF_NSAPI_DNS_RESPONSE_WAIT_TIME=10000 -DMBED_CONF_NSAPI_DNS_RETRIES=1 -DMBED_CONF_NSAPI_DNS_TOTAL_ATTEMPTS=10 -DMBED_CONF_NSAPI_DNS_CACHE_SIZE=5")
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,7 @@

#include "features/netsocket/EthernetInterface.h"
#include <iostream>

class MockEMAC : public EMAC {
public:
MOCK_METHOD0(power_up, bool());
MOCK_METHOD0(power_down, void());
MOCK_CONST_METHOD0(get_mtu_size, uint32_t());
MOCK_CONST_METHOD0(get_align_preference, uint32_t());
MOCK_CONST_METHOD2(get_ifname, void(char *name, uint8_t size));
MOCK_CONST_METHOD0(get_hwaddr_size, uint8_t());
MOCK_CONST_METHOD1(get_hwaddr, bool(uint8_t *addr));
MOCK_METHOD1(set_hwaddr, void(const uint8_t *));
MOCK_METHOD1(link_out, bool(emac_mem_buf_t *buf));
MOCK_METHOD1(set_link_input_cb, void(emac_link_input_cb_t input_cb));
MOCK_METHOD1(set_link_state_cb, void(emac_link_state_change_cb_t state_cb));
MOCK_METHOD1(add_multicast_group, void(const uint8_t *address));
MOCK_METHOD1(remove_multicast_group, void(const uint8_t *address));
MOCK_METHOD1(set_all_multicast, void(bool all));
MOCK_METHOD1(set_memory_manager, void(EMACMemoryManager &mem_mngr));

static MockEMAC &get_instance()
{
static MockEMAC emacMock1;
return emacMock1;
}
};

MBED_WEAK EMAC &EMAC::get_default_instance()
{
return MockEMAC::get_instance();
}
#include "EMAC_mock.h"

OnboardNetworkStack &OnboardNetworkStack::get_default_instance()
{
Expand Down Expand Up @@ -168,6 +139,8 @@ TEST_F(TestEthernetInterface, set_network)
SocketAddress ipAddress("127.0.0.1");
SocketAddress netmask("255.255.0.0");
SocketAddress gateway("127.0.0.2");
char macAddress[NSAPI_MAC_SIZE];
memset(macAddress, '\0', NSAPI_MAC_SIZE);

SocketAddress ipAddressArg;
SocketAddress netmaskArg;
Expand Down Expand Up @@ -205,6 +178,11 @@ TEST_F(TestEthernetInterface, set_network)
EXPECT_EQ(gateway, gatewayArg);

// Testing the getters makes sense now.
EXPECT_CALL(*netStackIface, get_mac_address(_, _))
.Times(1)
.WillOnce(DoAll(SetArrayArgument<0>(macAddress, macAddress+NSAPI_MAC_SIZE), Return(macAddress)));
EXPECT_EQ(std::string(macAddress), std::string(iface->get_mac_address()));

EXPECT_CALL(*netStackIface, get_ip_address(_))
.Times(1)
.WillOnce(DoAll(SetArgPointee<0>(ipAddress), Return(NSAPI_ERROR_OK)));
Expand Down
46 changes: 0 additions & 46 deletions UNITTESTS/features/netsocket/nsapi_dns/unittest.cmake

This file was deleted.

24 changes: 24 additions & 0 deletions UNITTESTS/stubs/CellularInterface_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Socket
* Copyright (c) 2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "CellularInterface.h"

MBED_WEAK CellularInterface *CellularInterface::get_target_default_instance()
{
return NULL;
}


55 changes: 55 additions & 0 deletions UNITTESTS/stubs/EMAC_mock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef EMACMOCK_H
#define EMACMOCK_H

#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "features/netsocket/EMAC.h"

class MockEMAC : public EMAC {
public:
MOCK_METHOD0(power_up, bool());
MOCK_METHOD0(power_down, void());
MOCK_CONST_METHOD0(get_mtu_size, uint32_t());
MOCK_CONST_METHOD0(get_align_preference, uint32_t());
MOCK_CONST_METHOD2(get_ifname, void(char *name, uint8_t size));
MOCK_CONST_METHOD0(get_hwaddr_size, uint8_t());
MOCK_CONST_METHOD1(get_hwaddr, bool(uint8_t *addr));
MOCK_METHOD1(set_hwaddr, void(const uint8_t *));
MOCK_METHOD1(link_out, bool(emac_mem_buf_t *buf));
MOCK_METHOD1(set_link_input_cb, void(emac_link_input_cb_t input_cb));
MOCK_METHOD1(set_link_state_cb, void(emac_link_state_change_cb_t state_cb));
MOCK_METHOD1(add_multicast_group, void(const uint8_t *address));
MOCK_METHOD1(remove_multicast_group, void(const uint8_t *address));
MOCK_METHOD1(set_all_multicast, void(bool all));
MOCK_METHOD1(set_memory_manager, void(EMACMemoryManager &mem_mngr));

static MockEMAC &get_instance()
{
static MockEMAC emacMock1;
return emacMock1;
}
};

MBED_WEAK EMAC &EMAC::get_default_instance()
{
return MockEMAC::get_instance();
}

#endif // EMACMOCK_H
24 changes: 24 additions & 0 deletions UNITTESTS/stubs/MeshInterface_stub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Socket
* Copyright (c) 2019 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "MeshInterface.h"

MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
{
return NULL;
}


10 changes: 9 additions & 1 deletion UNITTESTS/stubs/OnboardNetworkStack_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class OnboardNetworkStackMock : public OnboardNetworkStack {
public:
MOCK_METHOD3(gethostbyname, nsapi_error_t(const char *host, SocketAddress *address, nsapi_version_t version));
MOCK_METHOD1(add_dns_server, nsapi_error_t(const SocketAddress &address));
MOCK_METHOD3(get_dns_server, nsapi_error_t(int index, SocketAddress *address, const char *interface_name));
MOCK_METHOD2(call_in, nsapi_error_t(int delay, mbed::Callback<void()> func));
MOCK_METHOD2(socket_open, nsapi_error_t(nsapi_socket_t *handle, nsapi_protocol_t proto));
MOCK_METHOD1(socket_close, nsapi_error_t(nsapi_socket_t handle));
Expand All @@ -45,10 +46,17 @@ class OnboardNetworkStackMock : public OnboardNetworkStack {
MOCK_METHOD4(socket_recvfrom, nsapi_error_t(nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size));
MOCK_METHOD5(setsockopt, nsapi_error_t(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen));
MOCK_METHOD5(getsockopt, nsapi_error_t(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned *optlen));
MOCK_METHOD3(socket_attach, void(nsapi_socket_t handle, void (*callback)(void *), void *data));
// MOCK_METHOD3(socket_attach, void(nsapi_socket_t handle, void (*callback)(void *), void *data));
MOCK_METHOD3(add_ethernet_interface, nsapi_error_t(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out));
MOCK_METHOD3(add_ppp_interface, nsapi_error_t(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out));
MOCK_METHOD1(set_default_interface, void (OnboardNetworkStack::Interface *interface));
void *socket_cb;
// No need to mock socket_attach really.
void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data)
{
socket_cb = data;
};


static OnboardNetworkStackMock &get_instance()
{
Expand Down
1 change: 1 addition & 0 deletions features/netsocket/EMACInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class EMACInterface : public virtual NetworkInterface {
/** @copydoc NetworkInterface::get_gateway */
virtual nsapi_error_t get_gateway(SocketAddress *address);

/** @copydoc NetworkInterface::get_gateway */
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_gateway();

Expand Down