Skip to content

Commit fd4288a

Browse files
authored
Merge pull request #12027 from michalpasztamobica/dns_module_test
Modify nsapi_dns tests to be module tests
2 parents 4aeaab4 + fd46af6 commit fd4288a

File tree

9 files changed

+304
-298
lines changed

9 files changed

+304
-298
lines changed

UNITTESTS/features/netsocket/nsapi_dns/test_nsapi_dns.cpp renamed to UNITTESTS/MODULETESTS/features/netsocket/IfaceDnsSocket/test_IfaceDnsSocket.cpp

Lines changed: 136 additions & 221 deletions
Large diffs are not rendered by default.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
####################
3+
# UNIT TESTS
4+
####################
5+
6+
set(unittest-sources
7+
../features/netsocket/SocketAddress.cpp
8+
../features/netsocket/NetworkInterface.cpp
9+
../features/netsocket/NetworkInterfaceDefaults.cpp
10+
../features/netsocket/NetworkStack.cpp #nsapi_create_stack
11+
../features/netsocket/InternetSocket.cpp
12+
../features/netsocket/TCPSocket.cpp
13+
../features/netsocket/InternetDatagramSocket.cpp
14+
../features/netsocket/UDPSocket.cpp
15+
../features/netsocket/SocketStats.cpp
16+
../features/netsocket/EthernetInterface.cpp
17+
../features/netsocket/EMACInterface.cpp
18+
../features/netsocket/nsapi_dns.cpp
19+
../features/frameworks/nanostack-libservice/source/libip4string/ip4tos.c
20+
../features/frameworks/nanostack-libservice/source/libip6string/ip6tos.c
21+
../features/frameworks/nanostack-libservice/source/libip4string/stoip4.c
22+
../features/frameworks/nanostack-libservice/source/libip6string/stoip6.c
23+
../features/frameworks/nanostack-libservice/source/libBits/common_functions.c
24+
../features/frameworks/nanostack-libservice/source/libBits/common_functions.c
25+
../features/frameworks/nanostack-libservice/source/libList/ns_list.c
26+
)
27+
28+
set(unittest-test-sources
29+
MODULETESTS/features/netsocket/IfaceDnsSocket/test_IfaceDnsSocket.cpp
30+
stubs/MeshInterface_stub.cpp
31+
stubs/CellularInterface_stub.cpp
32+
stubs/Mutex_stub.cpp
33+
stubs/mbed_assert_stub.cpp
34+
stubs/mbed_atomic_stub.c
35+
stubs/mbed_critical_stub.c
36+
stubs/mbed_rtos_rtx_stub.c
37+
stubs/equeue_stub.c
38+
stubs/EventQueue_stub.cpp
39+
stubs/Kernel_stub.cpp
40+
stubs/mbed_error.c
41+
stubs/mbed_shared_queues_stub.cpp
42+
stubs/rtx_mutex_stub.c
43+
stubs/EventFlags_stub.cpp
44+
)
45+
46+
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")
47+
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")

UNITTESTS/features/netsocket/EthernetInterface/test_EthernetInterface.cpp

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,7 @@
2222

2323
#include "features/netsocket/EthernetInterface.h"
2424
#include <iostream>
25-
26-
class MockEMAC : public EMAC {
27-
public:
28-
MOCK_METHOD0(power_up, bool());
29-
MOCK_METHOD0(power_down, void());
30-
MOCK_CONST_METHOD0(get_mtu_size, uint32_t());
31-
MOCK_CONST_METHOD0(get_align_preference, uint32_t());
32-
MOCK_CONST_METHOD2(get_ifname, void(char *name, uint8_t size));
33-
MOCK_CONST_METHOD0(get_hwaddr_size, uint8_t());
34-
MOCK_CONST_METHOD1(get_hwaddr, bool(uint8_t *addr));
35-
MOCK_METHOD1(set_hwaddr, void(const uint8_t *));
36-
MOCK_METHOD1(link_out, bool(emac_mem_buf_t *buf));
37-
MOCK_METHOD1(set_link_input_cb, void(emac_link_input_cb_t input_cb));
38-
MOCK_METHOD1(set_link_state_cb, void(emac_link_state_change_cb_t state_cb));
39-
MOCK_METHOD1(add_multicast_group, void(const uint8_t *address));
40-
MOCK_METHOD1(remove_multicast_group, void(const uint8_t *address));
41-
MOCK_METHOD1(set_all_multicast, void(bool all));
42-
MOCK_METHOD1(set_memory_manager, void(EMACMemoryManager &mem_mngr));
43-
44-
static MockEMAC &get_instance()
45-
{
46-
static MockEMAC emacMock1;
47-
return emacMock1;
48-
}
49-
};
50-
51-
MBED_WEAK EMAC &EMAC::get_default_instance()
52-
{
53-
return MockEMAC::get_instance();
54-
}
25+
#include "EMAC_mock.h"
5526

5627
OnboardNetworkStack &OnboardNetworkStack::get_default_instance()
5728
{
@@ -167,6 +138,8 @@ TEST_F(TestEthernetInterface, set_network)
167138
SocketAddress ipAddress("127.0.0.1");
168139
SocketAddress netmask("255.255.0.0");
169140
SocketAddress gateway("127.0.0.2");
141+
char macAddress[NSAPI_MAC_SIZE];
142+
memset(macAddress, '\0', NSAPI_MAC_SIZE);
170143

171144
SocketAddress ipAddressArg;
172145
SocketAddress netmaskArg;
@@ -204,6 +177,11 @@ TEST_F(TestEthernetInterface, set_network)
204177
EXPECT_EQ(gateway, gatewayArg);
205178

206179
// Testing the getters makes sense now.
180+
EXPECT_CALL(*netStackIface, get_mac_address(_, _))
181+
.Times(1)
182+
.WillOnce(DoAll(SetArrayArgument<0>(macAddress, macAddress+NSAPI_MAC_SIZE), Return(macAddress)));
183+
EXPECT_EQ(std::string(macAddress), std::string(iface->get_mac_address()));
184+
207185
EXPECT_CALL(*netStackIface, get_ip_address(_))
208186
.Times(1)
209187
.WillOnce(DoAll(SetArgPointee<0>(ipAddress), Return(NSAPI_ERROR_OK)));

UNITTESTS/features/netsocket/nsapi_dns/unittest.cmake

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Socket
2+
* Copyright (c) 2019 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "CellularInterface.h"
18+
19+
MBED_WEAK CellularInterface *CellularInterface::get_target_default_instance()
20+
{
21+
return NULL;
22+
}
23+
24+

UNITTESTS/stubs/EMAC_mock.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2019, 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+
#ifndef EMACMOCK_H
19+
#define EMACMOCK_H
20+
21+
#include "gtest/gtest.h"
22+
#include "gmock/gmock.h"
23+
#include "features/netsocket/EMAC.h"
24+
25+
class MockEMAC : public EMAC {
26+
public:
27+
MOCK_METHOD0(power_up, bool());
28+
MOCK_METHOD0(power_down, void());
29+
MOCK_CONST_METHOD0(get_mtu_size, uint32_t());
30+
MOCK_CONST_METHOD0(get_align_preference, uint32_t());
31+
MOCK_CONST_METHOD2(get_ifname, void(char *name, uint8_t size));
32+
MOCK_CONST_METHOD0(get_hwaddr_size, uint8_t());
33+
MOCK_CONST_METHOD1(get_hwaddr, bool(uint8_t *addr));
34+
MOCK_METHOD1(set_hwaddr, void(const uint8_t *));
35+
MOCK_METHOD1(link_out, bool(emac_mem_buf_t *buf));
36+
MOCK_METHOD1(set_link_input_cb, void(emac_link_input_cb_t input_cb));
37+
MOCK_METHOD1(set_link_state_cb, void(emac_link_state_change_cb_t state_cb));
38+
MOCK_METHOD1(add_multicast_group, void(const uint8_t *address));
39+
MOCK_METHOD1(remove_multicast_group, void(const uint8_t *address));
40+
MOCK_METHOD1(set_all_multicast, void(bool all));
41+
MOCK_METHOD1(set_memory_manager, void(EMACMemoryManager &mem_mngr));
42+
43+
static MockEMAC &get_instance()
44+
{
45+
static MockEMAC emacMock1;
46+
return emacMock1;
47+
}
48+
};
49+
50+
MBED_WEAK EMAC &EMAC::get_default_instance()
51+
{
52+
return MockEMAC::get_instance();
53+
}
54+
55+
#endif // EMACMOCK_H
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Socket
2+
* Copyright (c) 2019 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "MeshInterface.h"
18+
19+
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
20+
{
21+
return NULL;
22+
}
23+
24+

UNITTESTS/stubs/OnboardNetworkStack_mock.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class OnboardNetworkStackMock : public OnboardNetworkStack {
3232
public:
3333
MOCK_METHOD3(gethostbyname, nsapi_error_t(const char *host, SocketAddress *address, nsapi_version_t version));
3434
MOCK_METHOD1(add_dns_server, nsapi_error_t(const SocketAddress &address));
35+
MOCK_METHOD3(get_dns_server, nsapi_error_t(int index, SocketAddress *address, const char *interface_name));
3536
MOCK_METHOD2(call_in, nsapi_error_t(int delay, mbed::Callback<void()> func));
3637
MOCK_METHOD2(socket_open, nsapi_error_t(nsapi_socket_t *handle, nsapi_protocol_t proto));
3738
MOCK_METHOD1(socket_close, nsapi_error_t(nsapi_socket_t handle));
@@ -45,10 +46,17 @@ class OnboardNetworkStackMock : public OnboardNetworkStack {
4546
MOCK_METHOD4(socket_recvfrom, nsapi_error_t(nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size));
4647
MOCK_METHOD5(setsockopt, nsapi_error_t(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen));
4748
MOCK_METHOD5(getsockopt, nsapi_error_t(nsapi_socket_t handle, int level, int optname, const void *optval, unsigned *optlen));
48-
MOCK_METHOD3(socket_attach, void(nsapi_socket_t handle, void (*callback)(void *), void *data));
49+
// MOCK_METHOD3(socket_attach, void(nsapi_socket_t handle, void (*callback)(void *), void *data));
4950
MOCK_METHOD3(add_ethernet_interface, nsapi_error_t(EMAC &emac, bool default_if, OnboardNetworkStack::Interface **interface_out));
5051
MOCK_METHOD3(add_ppp_interface, nsapi_error_t(PPP &ppp, bool default_if, OnboardNetworkStack::Interface **interface_out));
5152
MOCK_METHOD1(set_default_interface, void (OnboardNetworkStack::Interface *interface));
53+
void *socket_cb;
54+
// No need to mock socket_attach really.
55+
void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data)
56+
{
57+
socket_cb = data;
58+
};
59+
5260

5361
static OnboardNetworkStackMock &get_instance()
5462
{

features/netsocket/EMACInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class EMACInterface : public virtual NetworkInterface {
106106
/** @copydoc NetworkInterface::get_gateway */
107107
virtual nsapi_error_t get_gateway(SocketAddress *address);
108108

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

0 commit comments

Comments
 (0)