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: 92 99 92.9 %
Date: 2024-07-15 19:24:52 Functions: 6 20 30.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 12 36 33.3 %

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

Generated by: LCOV version 1.14