Skip to content

Cellular: Unit tests for Non-IP socket #9226

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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* Copyright (c) 2018, 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.
*/

#include "gtest/gtest.h"
#include "features/netsocket/cellular/CellularNonIPSocket.h"
#include "CellularContext_stub.h"

using namespace mbed;

// Control the rtos EventFlags stub. See EventFlags_stub.cpp
extern std::list<uint32_t> eventFlagsStubNextRetval;

static bool callback_is_called;
static void my_callback()
{
callback_is_called = true;
}

class TestCellularNonIPSocket : public testing::Test {
protected:
CellularNonIPSocket *socket;
ControlPlane_netif_stub *cp_netif;
CellularContext_stub cellular_context;
nsapi_size_t dataSize;
char dataBuf[10];

virtual void SetUp()
{
socket = new CellularNonIPSocket();
cp_netif = NULL;
dataSize = 10;
}

virtual void TearDown()
{
delete socket;
}
};

TEST_F(TestCellularNonIPSocket, open_null_cp_netif)
{
EXPECT_EQ(socket->open(static_cast<ControlPlane_netif *>(NULL)), NSAPI_ERROR_PARAMETER);
}

TEST_F(TestCellularNonIPSocket, open_null_context)
{
EXPECT_EQ(socket->open(static_cast<CellularContext *>(NULL)), NSAPI_ERROR_PARAMETER);
}

TEST_F(TestCellularNonIPSocket, open_context)
{
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_OK);
}

TEST_F(TestCellularNonIPSocket, open_cp_netif)
{
cp_netif = cellular_context.get_cp_netif();
EXPECT_EQ(socket->open((ControlPlane_netif *)cp_netif), NSAPI_ERROR_OK);
}

TEST_F(TestCellularNonIPSocket, open_twice)
{
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_OK);
EXPECT_EQ(socket->open((CellularContext *)&cellular_context), NSAPI_ERROR_PARAMETER);
}

TEST_F(TestCellularNonIPSocket, close)
{
socket->open((CellularContext *)&cellular_context);
EXPECT_EQ(socket->close(), NSAPI_ERROR_OK);
}

TEST_F(TestCellularNonIPSocket, close_no_open)
{
EXPECT_EQ(socket->close(), NSAPI_ERROR_NO_SOCKET);
}

TEST_F(TestCellularNonIPSocket, sigio)
{
callback_is_called = false;
socket->open((CellularContext *)&cellular_context);
socket->sigio(mbed::callback(my_callback));
socket->close(); // Trigger event;
EXPECT_EQ(callback_is_called, true);
}

/* send */

TEST_F(TestCellularNonIPSocket, send_no_open)
{
EXPECT_EQ(socket->send((char *)dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);
}

TEST_F(TestCellularNonIPSocket, send_error_would_block)
{
socket->open((CellularContext *)&cellular_context);
cp_netif = cellular_context.get_cp_netif();
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
}

TEST_F(TestCellularNonIPSocket, send_error_other)
{
socket->open((CellularContext *)&cellular_context);
cp_netif = cellular_context.get_cp_netif();
cp_netif->return_value = NSAPI_ERROR_NO_MEMORY;
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_NO_MEMORY);
}

TEST_F(TestCellularNonIPSocket, send_error_no_timeout)
{
socket->open((CellularContext *)&cellular_context);
cp_netif = cellular_context.get_cp_netif();
cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
socket->set_blocking(false);
EXPECT_EQ(socket->send(dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
}

TEST_F(TestCellularNonIPSocket, send)
{
socket->open((CellularContext *)&cellular_context);
cp_netif = cellular_context.get_cp_netif();
cp_netif->return_value = dataSize;
EXPECT_EQ(socket->send(dataBuf, dataSize), dataSize);
}

TEST_F(TestCellularNonIPSocket, recv)
{
EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_NO_SOCKET);

socket->open((CellularContext *)&cellular_context);
cp_netif = cellular_context.get_cp_netif();

cp_netif->return_value = 100;
EXPECT_EQ(socket->recv(&dataBuf, dataSize), 100);

cp_netif->return_value = NSAPI_ERROR_WOULD_BLOCK;
eventFlagsStubNextRetval.push_back(0);
eventFlagsStubNextRetval.push_back(osFlagsError); // Break the wait loop
EXPECT_EQ(socket->recv(&dataBuf, dataSize), NSAPI_ERROR_WOULD_BLOCK);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

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

# Add test specific include paths
set(unittest-includes ${unittest-includes}
../features/netsocket/cellular
)

set(unittest-sources
../features/netsocket/cellular/CellularNonIPSocket.cpp
)

set(unittest-test-sources
features/netsocket/cellular/CellularNonIPSocket/test_CellularNonIPSocket.cpp
stubs/NetworkInterface_stub.cpp
stubs/NetworkStack_stub.cpp
stubs/EventFlags_stub.cpp
stubs/Mutex_stub.cpp
)
3 changes: 2 additions & 1 deletion UNITTESTS/stubs/AT_CellularContext_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ using namespace mbed;

AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, const char *apn, bool cp_req, bool nonip_req) :
AT_CellularBase(at), _is_blocking(true), _is_connected(false),
_current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false), _cp_netif(NULL)
_current_op(OP_INVALID), _device(device), _nw(0), _fh(0), _cp_req(cp_req), _nonip_req(nonip_req), _cp_in_use(false)
{
_stack = NULL;
_pdp_type = DEFAULT_PDP_TYPE;
Expand All @@ -36,6 +36,7 @@ AT_CellularContext::AT_CellularContext(ATHandler &at, CellularDevice *device, co
_cid = -1;
_new_context_set = false;
_next = NULL;
_cp_netif = NULL;
}

AT_CellularContext::~AT_CellularContext()
Expand Down
Loading