LCOV - code coverage report
Current view: top level - control - test-tap-ctl-list.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 185 185 100.0 %
Date: 2022-01-12 12:43:54 Functions: 5 5 100.0 %
Branches: 8 8 100.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2020, Citrix Systems, Inc.
       3                 :            :  *
       4                 :            :  * All rights reserved.
       5                 :            :  *
       6                 :            :  * Redistribution and use in source and binary forms, with or without
       7                 :            :  * modification, are permitted provided that the following conditions are met:
       8                 :            :  *
       9                 :            :  *  1. Redistributions of source code must retain the above copyright
      10                 :            :  *     notice, this list of conditions and the following disclaimer.
      11                 :            :  *  2. Redistributions in binary form must reproduce the above copyright
      12                 :            :  *     notice, this list of conditions and the following disclaimer in the
      13                 :            :  *     documentation and/or other materials provided with the distribution.
      14                 :            :  *  3. Neither the name of the copyright holder nor the names of its
      15                 :            :  *     contributors may be used to endorse or promote products derived from
      16                 :            :  *     this software without specific prior written permission.
      17                 :            :  *
      18                 :            :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      19                 :            :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      20                 :            :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      21                 :            :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
      22                 :            :  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
      23                 :            :  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
      24                 :            :  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
      25                 :            :  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
      26                 :            :  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
      27                 :            :  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
      28                 :            :  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      29                 :            :  */
      30                 :            : 
      31                 :            : #include <stddef.h>
      32                 :            : #include <stdarg.h>
      33                 :            : #include <string.h>
      34                 :            : #include <setjmp.h>
      35                 :            : #include <cmocka.h>
      36                 :            : #include <errno.h>
      37                 :            : 
      38                 :            : #include <string.h>
      39                 :            : #include <sys/types.h>
      40                 :            : 
      41                 :            : #include <wrappers.h>
      42                 :            : #include "control-wrappers.h"
      43                 :            : #include "util.h"
      44                 :            : #include "test-suites.h"
      45                 :            : 
      46                 :            : #include "tap-ctl.h"
      47                 :            : #include "blktap2.h"
      48                 :            : 
      49                 :          1 : void test_tap_ctl_list_success_no_results(void **state)
      50                 :            : {
      51                 :            :         int err;
      52                 :          1 :         struct list_head list = LIST_HEAD_INIT(list);
      53                 :            : 
      54                 :          1 :         expect_string(__wrap_glob, pattern, "/sys/class/blktap2/blktap*");
      55                 :          1 :         will_return(__wrap_glob, GLOB_NOMATCH);
      56                 :          1 :         expect_string(__wrap_glob, pattern, "/var/run/blktap-control/ctl*");
      57                 :          1 :         will_return(__wrap_glob, GLOB_NOMATCH);
      58                 :            : 
      59                 :          1 :         err = tap_ctl_list(&list);
      60                 :            : 
      61                 :          1 :         assert_int_equal(0, err);
      62                 :          1 :         assert_true(list_empty(&list));
      63                 :          1 : }
      64                 :            : 
      65                 :            : 
      66                 :          1 : void test_tap_ctl_list_success_one_minor_no_td(void **state)
      67                 :            : {
      68                 :            :         int err;
      69                 :            :         tap_list_t *entry;
      70                 :          1 :         struct list_head list = LIST_HEAD_INIT(list);
      71                 :            : 
      72                 :          1 :         char *sys_glob_path = "/sys/class/blktap2/blktap!blktap0";
      73                 :            :         char *sys_glob_data;
      74                 :          1 :         char **sys_pathv = &sys_glob_data;
      75                 :            : 
      76                 :          1 :         sys_glob_data = test_malloc(strlen(sys_glob_path) + 2);
      77                 :          1 :         memset(sys_glob_data, 0, strlen(sys_glob_path) + 2);
      78                 :          1 :         strcpy(sys_glob_data, sys_glob_path);
      79                 :            : 
      80                 :          1 :         expect_string(__wrap_glob, pattern, "/sys/class/blktap2/blktap*");
      81                 :          1 :         will_return(__wrap_glob, 0);
      82                 :          1 :         will_return(__wrap_glob, 1);
      83                 :          1 :         will_return(__wrap_glob, sys_pathv);
      84                 :          1 :         expect_string(__wrap_glob, pattern, "/var/run/blktap-control/ctl*");
      85                 :          1 :         will_return(__wrap_glob, GLOB_NOMATCH);
      86                 :            : 
      87                 :            :         /* Call API */
      88                 :          1 :         err = tap_ctl_list(&list);
      89                 :            : 
      90                 :          1 :         assert_int_equal(0, err);
      91                 :          1 :         assert_true(list_is_singular(&list));
      92                 :            : 
      93         [ +  + ]:          2 :         tap_list_for_each_entry(entry, &list) {
      94                 :          1 :                 assert_int_equal(0, entry->minor);
      95                 :            :         }
      96                 :          1 :         tap_ctl_list_free(&list);
      97                 :          1 : }
      98                 :            : 
      99                 :          1 : void test_tap_ctl_list_success_one_td_no_minor_no_path(void **state)
     100                 :            : {
     101                 :            :         int err;
     102                 :            :         tap_list_t *entry;
     103                 :          1 :         struct list_head list = LIST_HEAD_INIT(list);
     104                 :            : 
     105                 :          1 :         pid_t test_pid = 1236;
     106                 :          1 :         int ipc_socket = 7;
     107                 :          1 :         char *expected_sock_name = "/var/run/blktap-control/ctl1236";
     108                 :            :         tapdisk_message_t write_message;
     109                 :            :         tapdisk_message_t *read_message;
     110                 :            :         struct mock_ipc_params *pid_ipc_params;
     111                 :            :         struct mock_ipc_params *list_ipc_params;
     112                 :          1 :         char *glob_path = "/var/run/blktap-control/ctl1236";
     113                 :            :         char *glob_data;
     114                 :          1 :         char **pathv = &glob_data;
     115                 :            : 
     116                 :          1 :         glob_data = test_malloc(strlen(glob_path) + 2);
     117                 :          1 :         memset(glob_data, 0, strlen(glob_path) + 2);
     118                 :          1 :         strcpy(glob_data, glob_path);
     119                 :            : 
     120                 :          1 :         expect_string(__wrap_glob, pattern, "/sys/class/blktap2/blktap*");
     121                 :          1 :         will_return(__wrap_glob, GLOB_NOMATCH);
     122                 :          1 :         expect_string(__wrap_glob, pattern, "/var/run/blktap-control/ctl*");
     123                 :          1 :         will_return(__wrap_glob, 0);
     124                 :          1 :         will_return(__wrap_glob, 1);
     125                 :          1 :         will_return(__wrap_glob, pathv);
     126                 :            : 
     127                 :            :         /* IPC PID */
     128                 :            :         memset(&write_message, 0, sizeof(write_message));
     129                 :          1 :         write_message.type = TAPDISK_MESSAGE_PID;
     130                 :            : 
     131                 :          1 :         read_message = test_malloc(sizeof(*read_message));
     132                 :            :         memset(read_message, 0, sizeof(*read_message));
     133                 :          1 :         read_message->type = TAPDISK_MESSAGE_PID_RSP;
     134                 :          1 :         read_message->u.tapdisk_pid = test_pid;
     135                 :            : 
     136                 :          1 :         pid_ipc_params = setup_ipc(
     137                 :            :                 expected_sock_name, ipc_socket,
     138                 :            :                 &write_message, read_message, 1);
     139                 :            : 
     140                 :          1 :         test_free(read_message);
     141                 :            : 
     142                 :            :         /* IPC List */
     143                 :            :         memset(&write_message, 0, sizeof(write_message));
     144                 :          1 :         write_message.type = TAPDISK_MESSAGE_LIST;
     145                 :          1 :         write_message.cookie = -1;
     146                 :            : 
     147                 :          1 :         read_message = test_malloc(sizeof(*read_message) * 2);
     148                 :            :         memset(read_message, 0, sizeof(*read_message) * 2);
     149                 :          1 :         read_message[0].type = TAPDISK_MESSAGE_LIST_RSP;
     150                 :          1 :         read_message[0].u.list.count = 1;
     151                 :          1 :         read_message[0].u.list.minor = -1;
     152                 :          1 :         read_message[0].u.list.state = -1;
     153                 :          1 :         read_message[0].u.list.path[0] = 0;
     154                 :          1 :         read_message[1].type = TAPDISK_MESSAGE_LIST_RSP;
     155                 :          1 :         read_message[1].u.list.count = 0;
     156                 :          1 :         read_message[1].u.list.minor = -1;
     157                 :          1 :         read_message[1].u.list.state = -1;
     158                 :          1 :         read_message[1].u.list.path[0] = 0;
     159                 :            : 
     160                 :          1 :         list_ipc_params = setup_ipc(
     161                 :            :                 expected_sock_name, ipc_socket,
     162                 :            :                 &write_message, read_message, 2);
     163                 :            : 
     164                 :          1 :         test_free(read_message);
     165                 :            : 
     166                 :            :         /* Call API */
     167                 :          1 :         err = tap_ctl_list(&list);
     168                 :            : 
     169                 :          1 :         assert_int_equal(0, err);
     170                 :          1 :         assert_true(list_is_singular(&list));
     171                 :            : 
     172         [ +  + ]:          2 :         tap_list_for_each_entry(entry, &list) {
     173                 :          1 :                 assert_int_equal(-1, entry->minor);
     174                 :            :         }
     175                 :            : 
     176                 :          1 :         tap_ctl_list_free(&list);
     177                 :            : 
     178                 :          1 :         free_ipc_params(pid_ipc_params);
     179                 :          1 :         free_ipc_params(list_ipc_params);
     180                 :          1 : }
     181                 :            : 
     182                 :          1 : void test_tap_ctl_list_success_one_td_one_minor_no_path(void **state)
     183                 :            : {
     184                 :            :         int err;
     185                 :            :         tap_list_t *entry;
     186                 :          1 :         struct list_head list = LIST_HEAD_INIT(list);
     187                 :            : 
     188                 :          1 :         pid_t test_pid = 1236;
     189                 :          1 :         int ipc_socket = 7;
     190                 :          1 :         char *expected_sock_name = "/var/run/blktap-control/ctl1236";
     191                 :            :         tapdisk_message_t write_message;
     192                 :            :         tapdisk_message_t *read_message;
     193                 :            :         struct mock_ipc_params *pid_ipc_params;
     194                 :            :         struct mock_ipc_params *list_ipc_params;
     195                 :          1 :         char *sys_glob_path = "/sys/class/blktap2/blktap!blktap0";
     196                 :            :         char *sys_glob_data;
     197                 :          1 :         char **sys_pathv = &sys_glob_data;
     198                 :          1 :         char *glob_path = "/var/run/blktap-control/ctl1236";
     199                 :            :         char *glob_data;
     200                 :          1 :         char **pathv = &glob_data;
     201                 :            : 
     202                 :          1 :         sys_glob_data = test_malloc(strlen(sys_glob_path) + 2);
     203                 :          1 :         memset(sys_glob_data, 0, strlen(sys_glob_path) + 2);
     204                 :          1 :         strcpy(sys_glob_data, sys_glob_path);
     205                 :            : 
     206                 :          1 :         glob_data = test_malloc(strlen(glob_path) + 2);
     207                 :          1 :         memset(glob_data, 0, strlen(glob_path) + 2);
     208                 :          1 :         strcpy(glob_data, glob_path);
     209                 :            : 
     210                 :          1 :         expect_string(__wrap_glob, pattern, "/sys/class/blktap2/blktap*");
     211                 :          1 :         will_return(__wrap_glob, 0);
     212                 :          1 :         will_return(__wrap_glob, 1);
     213                 :          1 :         will_return(__wrap_glob, sys_pathv);
     214                 :          1 :         expect_string(__wrap_glob, pattern, "/var/run/blktap-control/ctl*");
     215                 :          1 :         will_return(__wrap_glob, 0);
     216                 :          1 :         will_return(__wrap_glob, 1);
     217                 :          1 :         will_return(__wrap_glob, pathv);
     218                 :            : 
     219                 :            :         /* IPC PID */
     220                 :            :         memset(&write_message, 0, sizeof(write_message));
     221                 :          1 :         write_message.type = TAPDISK_MESSAGE_PID;
     222                 :            : 
     223                 :          1 :         read_message = test_malloc(sizeof(*read_message));
     224                 :            :         memset(read_message, 0, sizeof(*read_message));
     225                 :          1 :         read_message->type = TAPDISK_MESSAGE_PID_RSP;
     226                 :          1 :         read_message->u.tapdisk_pid = test_pid;
     227                 :            : 
     228                 :          1 :         pid_ipc_params = setup_ipc(
     229                 :            :                 expected_sock_name, ipc_socket,
     230                 :            :                 &write_message, read_message, 1);
     231                 :            : 
     232                 :          1 :         test_free(read_message);
     233                 :            : 
     234                 :            :         /* IPC List */
     235                 :            :         memset(&write_message, 0, sizeof(write_message));
     236                 :          1 :         write_message.type = TAPDISK_MESSAGE_LIST;
     237                 :          1 :         write_message.cookie = -1;
     238                 :            : 
     239                 :          1 :         read_message = test_malloc(sizeof(*read_message) * 2);
     240                 :            :         memset(read_message, 0, sizeof(*read_message) * 2);
     241                 :          1 :         read_message[0].type = TAPDISK_MESSAGE_LIST_RSP;
     242                 :          1 :         read_message[0].u.list.count = 1;
     243                 :          1 :         read_message[0].u.list.minor = 0;
     244                 :          1 :         read_message[0].u.list.state = 0;
     245                 :          1 :         read_message[0].u.list.path[0] = 0;
     246                 :          1 :         read_message[1].type = TAPDISK_MESSAGE_LIST_RSP;
     247                 :          1 :         read_message[1].u.list.count = 0;
     248                 :          1 :         read_message[1].u.list.minor = -1;
     249                 :          1 :         read_message[1].u.list.state = -1;
     250                 :          1 :         read_message[1].u.list.path[0] = 0;
     251                 :            : 
     252                 :          1 :         list_ipc_params = setup_ipc(
     253                 :            :                 expected_sock_name, ipc_socket,
     254                 :            :                 &write_message, read_message, 2);
     255                 :            : 
     256                 :          1 :         test_free(read_message);
     257                 :            : 
     258                 :            :         /* Call API */
     259                 :          1 :         err = tap_ctl_list(&list);
     260                 :            : 
     261                 :          1 :         assert_int_equal(0, err);
     262                 :          1 :         assert_true(list_is_singular(&list));
     263                 :            : 
     264         [ +  + ]:          2 :         tap_list_for_each_entry(entry, &list) {
     265                 :          1 :                 assert_int_equal(0, entry->minor);
     266                 :            :         }
     267                 :            : 
     268                 :          1 :         tap_ctl_list_free(&list);
     269                 :            : 
     270                 :          1 :         free_ipc_params(pid_ipc_params);
     271                 :          1 :         free_ipc_params(list_ipc_params);
     272                 :          1 : }
     273                 :            : 
     274                 :          1 : void test_tap_ctl_list_success(void **state)
     275                 :            : {
     276                 :            :         int err;
     277                 :            :         tap_list_t *entry;
     278                 :          1 :         struct list_head list = LIST_HEAD_INIT(list);
     279                 :            : 
     280                 :          1 :         pid_t test_pid = 1236;
     281                 :          1 :         int ipc_socket = 7;
     282                 :          1 :         char *expected_sock_name = "/var/run/blktap-control/ctl1236";
     283                 :            :         tapdisk_message_t write_message;
     284                 :            :         tapdisk_message_t *read_message;
     285                 :            :         struct mock_ipc_params *pid_ipc_params;
     286                 :            :         struct mock_ipc_params *list_ipc_params;
     287                 :          1 :         char *sys_glob_path = "/sys/class/blktap2/blktap!blktap0";
     288                 :            :         char *sys_glob_data;
     289                 :          1 :         char **sys_pathv = &sys_glob_data;
     290                 :          1 :         char *glob_path = "/var/run/blktap-control/ctl1236";
     291                 :            :         char *glob_data;
     292                 :          1 :         char **pathv = &glob_data;
     293                 :          1 :         char *vdi_path =
     294                 :            :                 "vhd:/dev/VG_XenStorage-a201d430-bc48-fd74-1aeb-292514d9afd6/"
     295                 :            :                 "VHD-b1c552a0-e0e6-4156-b6fe-18d7d114c6be";
     296                 :            : 
     297                 :          1 :         sys_glob_data = test_malloc(strlen(sys_glob_path) + 2);
     298                 :          1 :         memset(sys_glob_data, 0, strlen(sys_glob_path) + 2);
     299                 :          1 :         strcpy(sys_glob_data, sys_glob_path);
     300                 :            : 
     301                 :          1 :         glob_data = test_malloc(strlen(glob_path) + 2);
     302                 :          1 :         memset(glob_data, 0, strlen(glob_path) + 2);
     303                 :          1 :         strcpy(glob_data, glob_path);
     304                 :            : 
     305                 :          1 :         expect_string(__wrap_glob, pattern, "/sys/class/blktap2/blktap*");
     306                 :          1 :         will_return(__wrap_glob, 0);
     307                 :          1 :         will_return(__wrap_glob, 1);
     308                 :          1 :         will_return(__wrap_glob, sys_pathv);
     309                 :          1 :         expect_string(__wrap_glob, pattern, "/var/run/blktap-control/ctl*");
     310                 :          1 :         will_return(__wrap_glob, 0);
     311                 :          1 :         will_return(__wrap_glob, 1);
     312                 :          1 :         will_return(__wrap_glob, pathv);
     313                 :            : 
     314                 :            :         /* IPC PID */
     315                 :            :         memset(&write_message, 0, sizeof(write_message));
     316                 :          1 :         write_message.type = TAPDISK_MESSAGE_PID;
     317                 :            : 
     318                 :          1 :         read_message = test_malloc(sizeof(*read_message));
     319                 :            :         memset(read_message, 0, sizeof(*read_message));
     320                 :          1 :         read_message->type = TAPDISK_MESSAGE_PID_RSP;
     321                 :          1 :         read_message->u.tapdisk_pid = test_pid;
     322                 :            : 
     323                 :          1 :         pid_ipc_params = setup_ipc(
     324                 :            :                 expected_sock_name, ipc_socket,
     325                 :            :                 &write_message, read_message, 1);
     326                 :            : 
     327                 :          1 :         test_free(read_message);
     328                 :            : 
     329                 :            :         /* IPC List */
     330                 :            :         memset(&write_message, 0, sizeof(write_message));
     331                 :          1 :         write_message.type = TAPDISK_MESSAGE_LIST;
     332                 :          1 :         write_message.cookie = -1;
     333                 :            : 
     334                 :          1 :         read_message = test_malloc(sizeof(*read_message) * 2);
     335                 :            :         memset(read_message, 0, sizeof(*read_message) * 2);
     336                 :          1 :         read_message[0].type = TAPDISK_MESSAGE_LIST_RSP;
     337                 :          1 :         read_message[0].u.list.count = 1;
     338                 :          1 :         read_message[0].u.list.minor = 0;
     339                 :          1 :         read_message[0].u.list.state = 0;
     340                 :          1 :         strcpy(read_message[0].u.list.path, vdi_path);
     341                 :          1 :         read_message[1].type = TAPDISK_MESSAGE_LIST_RSP;
     342                 :          1 :         read_message[1].u.list.count = 0;
     343                 :          1 :         read_message[1].u.list.minor = -1;
     344                 :          1 :         read_message[1].u.list.state = -1;
     345                 :          1 :         read_message[1].u.list.path[0] = 0;
     346                 :            : 
     347                 :          1 :         list_ipc_params = setup_ipc(
     348                 :            :                 expected_sock_name, ipc_socket,
     349                 :            :                 &write_message, read_message, 2);
     350                 :            : 
     351                 :          1 :         test_free(read_message);
     352                 :            : 
     353                 :            :         /* Call API */
     354                 :          1 :         err = tap_ctl_list(&list);
     355                 :            : 
     356                 :          1 :         assert_int_equal(0, err);
     357                 :          1 :         assert_true(list_is_singular(&list));
     358                 :            : 
     359         [ +  + ]:          2 :         tap_list_for_each_entry(entry, &list) {
     360                 :          1 :                 assert_int_equal(0, entry->minor);
     361                 :          1 :                 assert_string_equal("vhd", entry->type);
     362                 :          1 :                 assert_string_equal(
     363                 :            :                         "/dev/VG_XenStorage-a201d430-bc48-fd74-1aeb-292514d9afd6/"
     364                 :            :                         "VHD-b1c552a0-e0e6-4156-b6fe-18d7d114c6be", entry->path);
     365                 :            :         }
     366                 :            : 
     367                 :          1 :         tap_ctl_list_free(&list);
     368                 :            : 
     369                 :          1 :         free_ipc_params(pid_ipc_params);
     370                 :          1 :         free_ipc_params(list_ipc_params);
     371                 :          1 : }

Generated by: LCOV version 1.13