LCOV - code coverage report
Current view: top level - vhd - test-vhd-util-utilities.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 65 65 100.0 %
Date: 2024-04-16 18:53:15 Functions: 7 7 100.0 %
Branches: 2 2 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 <setjmp.h>
      34                 :            : #include <cmocka.h>
      35                 :            : #include <errno.h>
      36                 :            : #include <wrappers.h>
      37                 :            : #include <limits.h>
      38                 :            : 
      39                 :            : #include "test-suites.h"
      40                 :            : #include "libvhd.h"
      41                 :            : 
      42                 :            : int vhd_validate_header(vhd_header_t *header);
      43                 :            : 
      44                 :          3 : void set_platform_codes(vhd_header_t *header)
      45                 :            : {
      46                 :            :         int i;
      47                 :            :         int n;
      48                 :            : 
      49                 :          3 :         n = sizeof(header->loc) / sizeof(vhd_parent_locator_t);
      50         [ +  + ]:         27 :         for (i = 0; i < n; i++) {
      51                 :         24 :                 header->loc[i].code = PLAT_CODE_NONE;
      52                 :            :         }
      53                 :          3 : }
      54                 :            : 
      55                 :          1 : void test_vhd_validate_header_bad_cookie(void **state)
      56                 :            : {
      57                 :            :         vhd_header_t header;
      58                 :            : 
      59                 :          1 :         memset(header.cookie, 0, sizeof(header.cookie));
      60                 :            : 
      61                 :          1 :         header.checksum = vhd_checksum_header(&header);
      62                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
      63                 :          1 : }
      64                 :            : 
      65                 :            : 
      66                 :          1 : void test_vhd_validate_header_bad_version(void **state)
      67                 :            : {
      68                 :            :         vhd_header_t header;
      69                 :            : 
      70                 :          1 :         memcpy(header.cookie, DD_COOKIE, sizeof(header.cookie));
      71                 :            : 
      72                 :          1 :         header.hdr_ver = 0;
      73                 :            : 
      74                 :          1 :         header.checksum = vhd_checksum_header(&header);
      75                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
      76                 :          1 : }
      77                 :            : 
      78                 :          1 : void test_vhd_validate_header_bad_offset(void **state)
      79                 :            : {
      80                 :            :         vhd_header_t header;
      81                 :            : 
      82                 :          1 :         memcpy(header.cookie, DD_COOKIE, sizeof(header.cookie));
      83                 :          1 :         header.hdr_ver      = DD_VERSION;
      84                 :          1 :         header.data_offset  = 0;
      85                 :          1 :         header.block_size   = VHD_BLOCK_SIZE;
      86                 :          1 :         header.prt_ts       = 0;
      87                 :          1 :         header.res1         = 0;
      88                 :          1 :         header.checksum = vhd_checksum_header(&header);
      89                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
      90                 :          1 : }
      91                 :            : 
      92                 :          1 : void test_vhd_validate_header_bad_blocksize(void **state)
      93                 :            : {
      94                 :            :         vhd_header_t header;
      95                 :            : 
      96                 :          1 :         memcpy(header.cookie, DD_COOKIE, sizeof(header.cookie));
      97                 :          1 :         header.hdr_ver      = DD_VERSION;
      98                 :          1 :         header.data_offset  = (uint64_t) -1;
      99                 :          1 :         header.prt_ts       = 0;
     100                 :          1 :         header.res1         = 0;
     101                 :            : 
     102                 :          1 :         set_platform_codes(&header);
     103                 :            : 
     104                 :            :         /* 0 block size */
     105                 :          1 :         header.block_size = 0;
     106                 :          1 :         header.checksum = vhd_checksum_header(&header);
     107                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
     108                 :            : 
     109                 :            :         /* Too big */
     110                 :          1 :         header.block_size = (1ULL << 22);
     111                 :          1 :         header.checksum = vhd_checksum_header(&header);
     112                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
     113                 :            : 
     114                 :            :         /* Not Power of 2 */
     115                 :          1 :         header.block_size = (1ULL << 21) - 2;
     116                 :          1 :         header.checksum = vhd_checksum_header(&header);
     117                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
     118                 :          1 : }
     119                 :            : 
     120                 :          1 : void test_vhd_validate_header_bad_checksum(void **state)
     121                 :            : {
     122                 :            :         vhd_header_t header;
     123                 :            : 
     124                 :          1 :         memcpy(header.cookie, DD_COOKIE, sizeof(header.cookie));
     125                 :          1 :         header.hdr_ver      = DD_VERSION;
     126                 :          1 :         header.data_offset  = (uint64_t) -1;
     127                 :          1 :         header.prt_ts       = 0;
     128                 :          1 :         header.res1         = 0;
     129                 :            : 
     130                 :          1 :         set_platform_codes(&header);
     131                 :            : 
     132                 :          1 :         header.block_size = (1ULL << 21);
     133                 :          1 :         header.checksum = vhd_checksum_header(&header) - 1;
     134                 :          1 :         assert_int_equal(-EINVAL, vhd_validate_header(&header));
     135                 :          1 : }
     136                 :            : 
     137                 :          1 : void test_vhd_validate_header_success(void **state)
     138                 :            : {
     139                 :            :         vhd_header_t header;
     140                 :            : 
     141                 :          1 :         memcpy(header.cookie, DD_COOKIE, sizeof(header.cookie));
     142                 :          1 :         header.hdr_ver      = DD_VERSION;
     143                 :          1 :         header.data_offset  = (uint64_t) -1;
     144                 :          1 :         header.prt_ts       = 0;
     145                 :          1 :         header.res1         = 0;
     146                 :            : 
     147                 :          1 :         set_platform_codes(&header);
     148                 :            : 
     149                 :          1 :         header.block_size = (1ULL << 21);
     150                 :          1 :         header.checksum = vhd_checksum_header(&header);
     151                 :          1 :         assert_int_equal(0, vhd_validate_header(&header));
     152                 :          1 : }

Generated by: LCOV version 1.13