LCOV - code coverage report
Current view: top level - spdk/test/unit/lib/sock/posix.c - posix_ut.c (source / functions) Hit Total Coverage
Test: Combined Lines: 91 98 92.9 %
Date: 2024-07-13 02:54:37 Functions: 5 12 41.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 11 22 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2019 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : #include "spdk/util.h"
       8                 :            : 
       9                 :            : #include "spdk_internal/mock.h"
      10                 :            : 
      11                 :            : #include "spdk_internal/cunit.h"
      12                 :            : 
      13                 :            : #include "common/lib/test_env.c"
      14                 :            : #include "sock/posix/posix.c"
      15                 :            : 
      16                 :          0 : DEFINE_STUB(spdk_sock_map_insert, int, (struct spdk_sock_map *map, int placement_id,
      17                 :            :                                         struct spdk_sock_group_impl *group), 0);
      18                 :          0 : DEFINE_STUB_V(spdk_sock_map_release, (struct spdk_sock_map *map, int placement_id));
      19                 :          0 : DEFINE_STUB(spdk_sock_map_lookup, int, (struct spdk_sock_map *map, int placement_id,
      20                 :            :                                         struct spdk_sock_group_impl **group, struct spdk_sock_group_impl *hint), 0);
      21                 :          0 : DEFINE_STUB(spdk_sock_map_find_free, int, (struct spdk_sock_map *map), -1);
      22                 :          6 : DEFINE_STUB_V(spdk_sock_map_cleanup, (struct spdk_sock_map *map));
      23                 :            : 
      24                 :         12 : DEFINE_STUB_V(spdk_net_impl_register, (struct spdk_net_impl *impl, int priority));
      25                 :          0 : DEFINE_STUB(spdk_sock_close, int, (struct spdk_sock **s), 0);
      26                 :          0 : DEFINE_STUB(spdk_sock_group_provide_buf, int, (struct spdk_sock_group *group, void *buf,
      27                 :            :                 size_t len, void *ctx), 0);
      28                 :          0 : DEFINE_STUB(spdk_sock_group_get_buf, size_t, (struct spdk_sock_group *group, void **buf,
      29                 :            :                 void **ctx), 0);
      30                 :            : 
      31                 :            : static void
      32                 :         30 : _req_cb(void *cb_arg, int len)
      33                 :            : {
      34                 :         30 :         *(bool *)cb_arg = true;
      35                 :         30 :         CU_ASSERT(len == 0);
      36                 :         30 : }
      37                 :            : 
      38                 :            : static void
      39                 :          6 : flush(void)
      40                 :            : {
      41                 :          6 :         struct spdk_posix_sock_group_impl group = {};
      42                 :          6 :         struct spdk_posix_sock psock = {};
      43                 :          6 :         struct spdk_sock *sock = &psock.base;
      44                 :            :         struct spdk_sock_request *req1, *req2;
      45                 :          5 :         bool cb_arg1, cb_arg2;
      46                 :            :         int rc;
      47                 :            : 
      48                 :            :         /* Set up data structures */
      49                 :          6 :         TAILQ_INIT(&sock->queued_reqs);
      50                 :          6 :         TAILQ_INIT(&sock->pending_reqs);
      51                 :          6 :         sock->group_impl = &group.base;
      52                 :            : 
      53                 :          6 :         req1 = calloc(1, sizeof(struct spdk_sock_request) + 2 * sizeof(struct iovec));
      54         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(req1 != NULL);
      55                 :          6 :         SPDK_SOCK_REQUEST_IOV(req1, 0)->iov_base = (void *)100;
      56                 :          6 :         SPDK_SOCK_REQUEST_IOV(req1, 0)->iov_len = 32;
      57                 :          6 :         SPDK_SOCK_REQUEST_IOV(req1, 1)->iov_base = (void *)200;
      58                 :          6 :         SPDK_SOCK_REQUEST_IOV(req1, 1)->iov_len = 32;
      59                 :          6 :         req1->iovcnt = 2;
      60                 :          6 :         req1->cb_fn = _req_cb;
      61                 :          6 :         req1->cb_arg = &cb_arg1;
      62                 :            : 
      63                 :          6 :         req2 = calloc(1, sizeof(struct spdk_sock_request) + 2 * sizeof(struct iovec));
      64         [ -  + ]:          6 :         SPDK_CU_ASSERT_FATAL(req2 != NULL);
      65                 :          6 :         SPDK_SOCK_REQUEST_IOV(req2, 0)->iov_base = (void *)100;
      66                 :          6 :         SPDK_SOCK_REQUEST_IOV(req2, 0)->iov_len = 32;
      67                 :          6 :         SPDK_SOCK_REQUEST_IOV(req2, 1)->iov_base = (void *)200;
      68                 :          6 :         SPDK_SOCK_REQUEST_IOV(req2, 1)->iov_len = 32;
      69                 :          6 :         req2->iovcnt = 2;
      70                 :          6 :         req2->cb_fn = _req_cb;
      71                 :          6 :         req2->cb_arg = &cb_arg2;
      72                 :            : 
      73                 :            :         /* Simple test - a request with a 2 element iovec
      74                 :            :          * that gets submitted in a single sendmsg. */
      75                 :          6 :         spdk_sock_request_queue(sock, req1);
      76                 :          6 :         MOCK_SET(sendmsg, 64);
      77                 :          6 :         cb_arg1 = false;
      78                 :          6 :         rc = _sock_flush(sock);
      79                 :          6 :         CU_ASSERT(rc == 64);
      80         [ -  + ]:          6 :         CU_ASSERT(cb_arg1 == true);
      81                 :          6 :         CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs));
      82                 :            : 
      83                 :            :         /* Two requests, where both can fully send. */
      84                 :          6 :         spdk_sock_request_queue(sock, req1);
      85                 :          6 :         spdk_sock_request_queue(sock, req2);
      86                 :          6 :         MOCK_SET(sendmsg, 128);
      87                 :          6 :         cb_arg1 = false;
      88                 :          6 :         cb_arg2 = false;
      89                 :          6 :         rc = _sock_flush(sock);
      90                 :          6 :         CU_ASSERT(rc == 128);
      91         [ -  + ]:          6 :         CU_ASSERT(cb_arg1 == true);
      92         [ -  + ]:          6 :         CU_ASSERT(cb_arg2 == true);
      93                 :          6 :         CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs));
      94                 :            : 
      95                 :            :         /* Two requests. Only first one can send */
      96                 :          6 :         spdk_sock_request_queue(sock, req1);
      97                 :          6 :         spdk_sock_request_queue(sock, req2);
      98                 :          6 :         MOCK_SET(sendmsg, 64);
      99                 :          6 :         cb_arg1 = false;
     100                 :          6 :         cb_arg2 = false;
     101                 :          6 :         rc = _sock_flush(sock);
     102                 :          6 :         CU_ASSERT(rc == 64);
     103         [ -  + ]:          6 :         CU_ASSERT(cb_arg1 == true);
     104         [ -  + ]:          6 :         CU_ASSERT(cb_arg2 == false);
     105                 :          6 :         CU_ASSERT(TAILQ_FIRST(&sock->queued_reqs) == req2);
     106         [ -  + ]:          6 :         TAILQ_REMOVE(&sock->queued_reqs, req2, internal.link);
     107                 :          6 :         CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs));
     108                 :            : 
     109                 :            :         /* One request. Partial send. */
     110                 :          6 :         spdk_sock_request_queue(sock, req1);
     111                 :          6 :         MOCK_SET(sendmsg, 10);
     112                 :          6 :         cb_arg1 = false;
     113                 :          6 :         rc = _sock_flush(sock);
     114                 :          6 :         CU_ASSERT(rc == 10);
     115         [ -  + ]:          6 :         CU_ASSERT(cb_arg1 == false);
     116                 :          6 :         CU_ASSERT(TAILQ_FIRST(&sock->queued_reqs) == req1);
     117                 :            : 
     118                 :            :         /* Do a second flush that partial sends again. */
     119                 :          6 :         MOCK_SET(sendmsg, 24);
     120                 :          6 :         cb_arg1 = false;
     121                 :          6 :         rc = _sock_flush(sock);
     122                 :          6 :         CU_ASSERT(rc == 24);
     123         [ -  + ]:          6 :         CU_ASSERT(cb_arg1 == false);
     124                 :          6 :         CU_ASSERT(TAILQ_FIRST(&sock->queued_reqs) == req1);
     125                 :            : 
     126                 :            :         /* Flush the rest of the data */
     127                 :          6 :         MOCK_SET(sendmsg, 30);
     128                 :          6 :         cb_arg1 = false;
     129                 :          6 :         rc = _sock_flush(sock);
     130                 :          6 :         CU_ASSERT(rc == 30);
     131         [ -  + ]:          6 :         CU_ASSERT(cb_arg1 == true);
     132                 :          6 :         CU_ASSERT(TAILQ_EMPTY(&sock->queued_reqs));
     133                 :            : 
     134                 :          6 :         free(req1);
     135                 :          6 :         free(req2);
     136                 :          6 : }
     137                 :            : 
     138                 :            : int
     139                 :          6 : main(int argc, char **argv)
     140                 :            : {
     141                 :          6 :         CU_pSuite       suite = NULL;
     142                 :            :         unsigned int    num_failures;
     143                 :            : 
     144                 :          6 :         CU_initialize_registry();
     145                 :            : 
     146                 :          6 :         suite = CU_add_suite("posix", NULL, NULL);
     147                 :            : 
     148                 :          6 :         CU_ADD_TEST(suite, flush);
     149                 :            : 
     150                 :            : 
     151                 :          6 :         num_failures = spdk_ut_run_tests(argc, argv, NULL);
     152                 :            : 
     153                 :          6 :         CU_cleanup_registry();
     154                 :            : 
     155                 :          6 :         return num_failures;
     156                 :            : }

Generated by: LCOV version 1.14