/**************************************************************************** * examples/usrsocktest/usrsocktest_basic_connect.c * Basic connect tests with socket daemon * * Copyright (C) 2015, 2017 Haltian Ltd. All rights reserved. * Authors: Roman Saveljev * Jussi Kivilinna * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * ****************************************************************************/ /**************************************************************************** * Included Files ****************************************************************************/ #include #include #include #include #include #include #include #include "defines.h" /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ /**************************************************************************** * Private Types ****************************************************************************/ /**************************************************************************** * Private Function Prototypes ****************************************************************************/ /**************************************************************************** * Private Data ****************************************************************************/ static bool started; static int sd; /**************************************************************************** * Public Data ****************************************************************************/ /**************************************************************************** * Private Functions ****************************************************************************/ /**************************************************************************** * Name: setup * * Description: * Run before every testcase * * Input Parameters: * dconf - test daemon configuration * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void setup(FAR struct usrsocktest_daemon_conf_s *dconf) { dconf->endpoint_addr = "127.0.0.1"; dconf->endpoint_port = 255; TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_start(dconf)); started = true; sd = socket(AF_INET, SOCK_STREAM, 0); TEST_ASSERT_TRUE(sd >= 0); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); } /**************************************************************************** * Name: Teardown * * Description: * Run after every testcase * * Input Parameters: * None * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void teardown(void) { int ret; if (sd >= 0) { ret = close(sd); assert(ret >= 0); } if (started) { ret = usrsocktest_daemon_stop(); assert(ret == OK); } } /**************************************************************************** * Name: not_connected * * Description: * Opened socket is not connected * * Input Parameters: * None * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void not_connected(void) { TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); } /**************************************************************************** * Name: basic_connect_connect * * Description: * Open and connect the socket * * Input Parameters: * None * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void basic_connect_connect(void) { int ret; struct sockaddr_in addr; inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(255); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(0, ret); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets()); /* Attempt reconnect to same address */ inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(255); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(-1, ret); TEST_ASSERT_EQUAL(EISCONN, errno); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); /* Attempt reconnect to different address */ inet_pton(AF_INET, "1.1.1.1", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(1); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(-1, ret); TEST_ASSERT_EQUAL(EISCONN, errno); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); /* Close socket */ TEST_ASSERT_TRUE(close(sd) >= 0); sd = -1; TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); /* Stopping daemon should succeed. */ TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop()); started = false; TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt); TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt); } /**************************************************************************** * Name: wrong_af * * Description: * Open and connect the socket with wrong AF * * Input Parameters: * None * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void wrong_af(void) { int ret; struct sockaddr_in addr; /* Try connect socket with wrong AF */ inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr); addr.sin_family = AF_UNIX; addr.sin_port = htons(255); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(-1, ret); TEST_ASSERT_EQUAL(EAFNOSUPPORT, errno); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); /* Close socket */ TEST_ASSERT_TRUE(close(sd) >= 0); sd = -1; TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); /* Stopping daemon should succeed. */ TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop()); started = false; TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt); TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt); } /**************************************************************************** * Name: wrong_port * * Description: * Open and connect the socket with wrong port * * Input Parameters: * None * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void wrong_port(void) { int ret; struct sockaddr_in addr; /* Try connect socket to wrong port */ inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(127); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(-1, ret); TEST_ASSERT_EQUAL(ECONNREFUSED, errno); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); /* Close socket */ TEST_ASSERT_TRUE(close(sd) >= 0); sd = -1; TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); /* Stopping daemon should succeed. */ TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop()); started = false; TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt); TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt); } /**************************************************************************** * Name: wrong_endpoint * * Description: * Open and connect the socket with wrong endpoint * * Input Parameters: * None * * Returned Value: * None * * Assumptions/Limitations: * None * ****************************************************************************/ static void wrong_endpoint(void) { int ret; struct sockaddr_in addr; /* Try connect socket to unreachable endpoint */ inet_pton(AF_INET, "1.1.1.1", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(1); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(-1, ret); TEST_ASSERT_EQUAL(ENETUNREACH, errno); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); /* Connect */ inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr); addr.sin_family = AF_INET; addr.sin_port = htons(255); ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr)); TEST_ASSERT_EQUAL(0, ret); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets()); /* Close socket */ TEST_ASSERT_TRUE(close(sd) >= 0); sd = -1; TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets()); /* Stopping daemon should succeed. */ TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop()); started = false; TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets()); TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets()); TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt); TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt); } TEST_SETUP(basic_connect) { usrsocktest_daemon_config = usrsocktest_daemon_defconf; setup(&usrsocktest_daemon_config); } TEST_TEAR_DOWN(basic_connect) { teardown(); } TEST(basic_connect, not_connected) { not_connected(); } TEST(basic_connect, basic_connect_connect) { basic_connect_connect(); } TEST(basic_connect, wrong_af) { wrong_af(); } TEST(basic_connect, wrong_port) { wrong_port(); } TEST(basic_connect, wrong_endpoint) { wrong_endpoint(); } TEST_SETUP(basic_connect_delay) { usrsocktest_daemon_config = usrsocktest_daemon_defconf; usrsocktest_daemon_config.delay_all_responses = true; setup(&usrsocktest_daemon_config); } TEST_TEAR_DOWN(basic_connect_delay) { teardown(); } TEST(basic_connect_delay, not_connected) { not_connected(); } TEST(basic_connect_delay, basic_connect_connect) { basic_connect_connect(); } TEST(basic_connect_delay, wrong_af) { wrong_af(); } TEST(basic_connect_delay, wrong_port) { wrong_port(); } TEST(basic_connect_delay, wrong_endpoint) { wrong_endpoint(); } /**************************************************************************** * Public Functions ****************************************************************************/ TEST_GROUP(basic_connect) { RUN_TEST_CASE(basic_connect, not_connected); RUN_TEST_CASE(basic_connect, basic_connect_connect); RUN_TEST_CASE(basic_connect, wrong_af); RUN_TEST_CASE(basic_connect, wrong_port); RUN_TEST_CASE(basic_connect, wrong_endpoint); } TEST_GROUP(basic_connect_delay) { RUN_TEST_CASE(basic_connect_delay, not_connected); RUN_TEST_CASE(basic_connect_delay, basic_connect_connect); RUN_TEST_CASE(basic_connect_delay, wrong_af); RUN_TEST_CASE(basic_connect_delay, wrong_port); RUN_TEST_CASE(basic_connect_delay, wrong_endpoint); }