LCOV - code coverage report
Current view: top level - mockatests/control - test-tap-ctl-allocate.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 153 153
Test Date: 2026-06-26 00:00:00 Functions: 100.0 % 5 5
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2017, 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                 :             : #include <sys/file.h>
      38                 :             : #include <sys/stat.h>
      39                 :             : 
      40                 :             : #include <wrappers.h>
      41                 :             : #include "control-wrappers.h"
      42                 :             : #include "test-suites.h"
      43                 :             : 
      44                 :             : #include "tap-ctl.h"
      45                 :             : #include "blktap.h"
      46                 :             : 
      47                 :             : 
      48                 :           1 : void test_tap_ctl_allocate_prep_dir_no_access(void **state)
      49                 :             : {
      50                 :           1 :         int result;
      51                 :           1 :         int minor;
      52                 :           1 :         char *devname;
      53                 :             : 
      54                 :           1 :         will_return(__wrap_access, ENOENT);
      55                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control");
      56                 :           1 :         will_return(__wrap_mkdir, EEXIST);
      57                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
      58                 :           1 :         will_return(__wrap_mkdir, EACCES);
      59                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
      60                 :             : 
      61                 :           1 :         result = tap_ctl_allocate(&minor, &devname);
      62                 :             : 
      63                 :           1 :         assert_int_equal(-EACCES, result);
      64                 :           1 : }
      65                 :             : 
      66                 :           1 : void test_tap_ctl_allocate_prep_runtime_dir_no_access(void **state)
      67                 :             : {
      68                 :           1 :         int result;
      69                 :           1 :         int minor;
      70                 :           1 :         char *devname;
      71                 :             : 
      72                 :           1 :         will_return(__wrap_access, ENOENT);
      73                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control");
      74                 :           1 :         will_return(__wrap_mkdir, EEXIST);
      75                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
      76                 :           1 :         will_return(__wrap_mkdir, 0);
      77                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
      78                 :             : 
      79                 :             : 
      80                 :           1 :         will_return(__wrap_access, ENOENT);
      81                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
      82                 :           1 :         will_return(__wrap_mkdir, EEXIST);
      83                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
      84                 :           1 :         will_return(__wrap_mkdir, 0);
      85                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
      86                 :           1 :             will_return(__wrap_mkdir, EACCES);
      87                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
      88                 :             : 
      89                 :           1 :         result = tap_ctl_allocate(&minor, &devname);
      90                 :             : 
      91                 :           1 :         assert_int_equal(-EACCES, result);
      92                 :           1 : }
      93                 :             : 
      94                 :           1 : void test_tap_ctl_allocate_first_success(void **state)
      95                 :             : {
      96                 :           1 :         int dev_fd = 12;
      97                 :           1 :         int result;
      98                 :           1 :         int minor;
      99                 :           1 :         char *devname;
     100                 :             : 
     101                 :           1 :         will_return(__wrap_access, ENOENT);
     102                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control");
     103                 :           1 :         will_return(__wrap_mkdir, EEXIST);
     104                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
     105                 :           1 :         will_return(__wrap_mkdir, 0);
     106                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
     107                 :             : 
     108                 :             : 
     109                 :           1 :         will_return(__wrap_access, ENOENT);
     110                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
     111                 :           1 :         will_return(__wrap_mkdir, EEXIST);
     112                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
     113                 :           1 :         will_return(__wrap_mkdir, 0);
     114                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
     115                 :           1 :             will_return(__wrap_mkdir, 0);
     116                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
     117                 :             : 
     118                 :           1 :         will_return(__wrap_open, dev_fd);
     119                 :           1 :         expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
     120                 :             : 
     121                 :           1 :         will_return(__wrap_flock, 0);
     122                 :           1 :         expect_value(__wrap_flock, fd, dev_fd);
     123                 :           1 :         expect_value(__wrap_flock, operation, LOCK_EX);
     124                 :             : 
     125                 :           1 :         will_return(__wrap_stat, -1);
     126                 :           1 :         will_return(__wrap_stat, ENOENT);
     127                 :           1 :         expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
     128                 :             : 
     129                 :           1 :         will_return(__wrap_open, 13);
     130                 :           1 :         expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
     131                 :             : 
     132                 :           1 :         will_return(__wrap_close, 0);
     133                 :           1 :         expect_value(__wrap_close, fd, 13);
     134                 :             : 
     135                 :           1 :         will_return(__wrap_flock, 0);
     136                 :           1 :         expect_value(__wrap_flock, fd, 12);
     137                 :           1 :         expect_value(__wrap_flock, operation, LOCK_UN);
     138                 :             : 
     139                 :           1 :         will_return(__wrap_close, 0);
     140                 :           1 :         expect_value(__wrap_close, fd, 12);
     141                 :             : 
     142                 :           1 :         result = tap_ctl_allocate(&minor, &devname);
     143                 :             : 
     144                 :           1 :         assert_int_equal(0, result);
     145                 :           1 : }
     146                 :             : 
     147                 :           1 : void test_tap_ctl_allocate_create_failed(void **state)
     148                 :             : {
     149                 :           1 :         int dev_fd = 12;
     150                 :           1 :         int result;
     151                 :           1 :         int minor;
     152                 :           1 :         char *devname;
     153                 :             : 
     154                 :           1 :         will_return(__wrap_access, ENOENT);
     155                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control");
     156                 :           1 :         will_return(__wrap_mkdir, EEXIST);
     157                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
     158                 :           1 :         will_return(__wrap_mkdir, 0);
     159                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
     160                 :             : 
     161                 :             : 
     162                 :           1 :         will_return(__wrap_access, ENOENT);
     163                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
     164                 :           1 :         will_return(__wrap_mkdir, EEXIST);
     165                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
     166                 :           1 :         will_return(__wrap_mkdir, 0);
     167                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
     168                 :           1 :         will_return(__wrap_mkdir, 0);
     169                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
     170                 :             : 
     171                 :           1 :         will_return(__wrap_open, dev_fd);
     172                 :           1 :         expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
     173                 :             : 
     174                 :           1 :         will_return(__wrap_flock, 0);
     175                 :           1 :         expect_value(__wrap_flock, fd, dev_fd);
     176                 :           1 :         expect_value(__wrap_flock, operation, LOCK_EX);
     177                 :             : 
     178                 :           1 :         will_return(__wrap_stat, -1);
     179                 :           1 :         will_return(__wrap_stat, ENOENT);
     180                 :           1 :         expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
     181                 :             : 
     182                 :           1 :         will_return(__wrap_open, -1);
     183                 :           1 :         expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
     184                 :             : 
     185                 :           1 :         will_return(__wrap_flock, 0);
     186                 :           1 :         expect_value(__wrap_flock, fd, 12);
     187                 :           1 :         expect_value(__wrap_flock, operation, LOCK_UN);
     188                 :             : 
     189                 :           1 :         will_return(__wrap_close, 0);
     190                 :           1 :         expect_value(__wrap_close, fd, 12);
     191                 :             : 
     192                 :           1 :         result = tap_ctl_allocate(&minor, &devname);
     193                 :             : 
     194                 :           1 :         assert_int_equal(-ENOENT, result);
     195                 :           1 : }
     196                 :             : 
     197                 :           1 : void test_tap_ctl_allocate_one_exists_success(void **state)
     198                 :             : {
     199                 :           1 :         int dev_fd = 12;
     200                 :           1 :         int result;
     201                 :           1 :         int minor;
     202                 :           1 :         char *devname;
     203                 :           1 :         struct stat st_buf;
     204                 :             : 
     205                 :           1 :         will_return(__wrap_access, ENOENT);
     206                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control");
     207                 :           1 :         will_return(__wrap_mkdir, EEXIST);
     208                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
     209                 :           1 :         will_return(__wrap_mkdir, 0);
     210                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
     211                 :             : 
     212                 :             : 
     213                 :           1 :         will_return(__wrap_access, ENOENT);
     214                 :           1 :         expect_string(__wrap_access, pathname, "/run/blktap-control/tapdisk");
     215                 :           1 :         will_return(__wrap_mkdir, EEXIST);
     216                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run");
     217                 :           1 :         will_return(__wrap_mkdir, 0);
     218                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control");
     219                 :           1 :             will_return(__wrap_mkdir, 0);
     220                 :           1 :         expect_string(__wrap_mkdir, pathname, "/run/blktap-control/tapdisk");
     221                 :             : 
     222                 :           1 :         will_return(__wrap_open, dev_fd);
     223                 :           1 :         expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk");
     224                 :             : 
     225                 :           1 :         will_return(__wrap_flock, 0);
     226                 :           1 :         expect_value(__wrap_flock, fd, dev_fd);
     227                 :           1 :         expect_value(__wrap_flock, operation, LOCK_EX);
     228                 :             : 
     229                 :           1 :         will_return(__wrap_stat, 0);
     230                 :           1 :         will_return(__wrap_stat, &st_buf);
     231                 :           1 :         expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-0");
     232                 :             : 
     233                 :           1 :         will_return(__wrap_stat, -1);
     234                 :           1 :         will_return(__wrap_stat, ENOENT);
     235                 :           1 :         expect_string(__wrap_stat, pathname, "/run/blktap-control/tapdisk/tapdisk-1");
     236                 :             : 
     237                 :           1 :         will_return(__wrap_open, 13);
     238                 :           1 :         expect_string(__wrap_open, pathname, "/run/blktap-control/tapdisk/tapdisk-1");
     239                 :             : 
     240                 :           1 :         will_return(__wrap_close, 0);
     241                 :           1 :         expect_value(__wrap_close, fd, 13);
     242                 :             : 
     243                 :           1 :         will_return(__wrap_flock, 0);
     244                 :           1 :         expect_value(__wrap_flock, fd, 12);
     245                 :           1 :         expect_value(__wrap_flock, operation, LOCK_UN);
     246                 :             : 
     247                 :           1 :         will_return(__wrap_close, 0);
     248                 :           1 :         expect_value(__wrap_close, fd, 12);
     249                 :             : 
     250                 :           1 :         result = tap_ctl_allocate(&minor, &devname);
     251                 :             : 
     252                 :           1 :         assert_int_equal(0, result);
     253                 :           1 : }
        

Generated by: LCOV version 2.0-1