LCOV - code coverage report
Current view: top level - cbt - test-cbt-util-set.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 173 173 100.0 %
Date: 2023-06-20 10:41:14 Functions: 14 14 100.0 %
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                 :            : #define __STDC_FORMAT_MACROS
      31                 :            : 
      32                 :            : #include <stdlib.h>
      33                 :            : #include <string.h>
      34                 :            : #include <stddef.h>
      35                 :            : #include <stdarg.h>
      36                 :            : #include <setjmp.h>
      37                 :            : #include <cmocka.h>
      38                 :            : #include <errno.h>
      39                 :            : #include <uuid/uuid.h>
      40                 :            : #include <inttypes.h>
      41                 :            : 
      42                 :            : #include <cbt-util-priv.h>
      43                 :            : #include <wrappers.h>
      44                 :            : #include "test-suites.h"
      45                 :            : 
      46                 :            : int __real_printf(const char *format, ...);
      47                 :            : 
      48                 :            : void
      49                 :          1 : test_cbt_util_set_parent(void **state)
      50                 :            : {
      51                 :            :         int result;
      52                 :            :         void *log_meta;
      53                 :            :         uuid_t parent;
      54                 :            :         char uuid_str[36];
      55                 :            :         char uuid_str_after[36];
      56                 :            : 
      57                 :          1 :         uuid_generate_random(parent);
      58                 :          1 :         uuid_unparse(parent, uuid_str);
      59                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-p", uuid_str};
      60                 :            : 
      61                 :          1 :         log_meta = malloc(sizeof(struct cbt_log_metadata));
      62                 :            : 
      63                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r+");
      64                 :            : 
      65                 :          1 :         will_return(__wrap_fopen, test_log);
      66                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
      67                 :            : 
      68                 :          1 :         result = cbt_util_set(5, args);
      69                 :          1 :         assert_int_equal(result, 0);
      70                 :            : 
      71                 :          1 :         uuid_unparse(((struct cbt_log_metadata*)log_meta)->parent, uuid_str_after);
      72                 :          1 :         assert_string_equal(uuid_str, uuid_str_after);
      73                 :            : 
      74                 :          1 :         free(log_meta);
      75                 :          1 : }
      76                 :            : 
      77                 :            : void
      78                 :          1 : test_cbt_util_set_child(void **state)
      79                 :            : {
      80                 :            :         int result;
      81                 :            :         void *log_meta;
      82                 :            :         uuid_t child;
      83                 :            :         char uuid_str[36];
      84                 :            :         char uuid_str_after[36];
      85                 :            : 
      86                 :          1 :         uuid_generate_random(child);
      87                 :          1 :         uuid_unparse(child, uuid_str);
      88                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-c", uuid_str};
      89                 :            : 
      90                 :          1 :         log_meta = malloc(sizeof(struct cbt_log_metadata));
      91                 :            : 
      92                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r+");
      93                 :            : 
      94                 :          1 :         will_return(__wrap_fopen, test_log);
      95                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
      96                 :            : 
      97                 :          1 :         result = cbt_util_set(5, args);
      98                 :          1 :         assert_int_equal(result, 0);
      99                 :            : 
     100                 :          1 :         uuid_unparse(((struct cbt_log_metadata*)log_meta)->child, uuid_str_after);
     101                 :          1 :         assert_string_equal(uuid_str, uuid_str_after);
     102                 :            : 
     103                 :          1 :         free(log_meta);
     104                 :          1 : }
     105                 :            : 
     106                 :            : void
     107                 :          1 : test_cbt_util_set_flag(void **state)
     108                 :            : {
     109                 :            :         int result;
     110                 :            :         void *log_meta;
     111                 :          1 :         char flag_string[1] = "2";
     112                 :          1 :         int flag_int = 2;
     113                 :            :         int log_flag;
     114                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-f", flag_string};
     115                 :            : 
     116                 :          1 :         log_meta = malloc(sizeof(struct cbt_log_metadata));
     117                 :            : 
     118                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r+");
     119                 :            : 
     120                 :          1 :         will_return(__wrap_fopen, test_log);
     121                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     122                 :            : 
     123                 :          1 :         result = cbt_util_set(5, args);
     124                 :          1 :         assert_int_equal(result, 0);
     125                 :            : 
     126                 :          1 :         log_flag = ((struct cbt_log_metadata*)log_meta)->consistent;
     127                 :          1 :         assert_int_equal(flag_int, log_flag);
     128                 :            : 
     129                 :          1 :         free(log_meta);
     130                 :          1 : }
     131                 :            : 
     132                 :            : void
     133                 :          1 : test_cbt_util_set_size(void **state)
     134                 :            : {
     135                 :            :         int result;
     136                 :            :         void *log_meta;
     137                 :          1 :         uint64_t size_int64 = 4194303;
     138                 :            :         char size_string[8];
     139                 :            :         snprintf(size_string, sizeof(size_string), "%" PRIu64, size_int64);
     140                 :            :         uint64_t log_size_int64;
     141                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-s", size_string};
     142                 :            : 
     143                 :          1 :         uint64_t btmsize = bitmap_size(size_int64);
     144                 :            : 
     145                 :          1 :         int file_size = sizeof(struct cbt_log_metadata) + btmsize;
     146                 :          1 :         log_meta = malloc(file_size);
     147                 :            : 
     148                 :          1 :         ((struct cbt_log_metadata*)log_meta)->size = 2048;
     149                 :            : 
     150                 :          1 :         memcpy(log_meta + sizeof(struct cbt_log_metadata), (void*)memcpy, btmsize);
     151                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, file_size, "r+");
     152                 :            : 
     153                 :          1 :         will_return(__wrap_fopen, test_log);
     154                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     155                 :            : 
     156                 :          1 :         result = cbt_util_set(5, args);
     157                 :          1 :         assert_int_equal(result, 0);
     158                 :            : 
     159                 :          1 :         log_size_int64 = ((struct cbt_log_metadata*)log_meta)->size;
     160                 :            :         
     161                 :          1 :         assert_true(log_size_int64 == size_int64);
     162                 :            : 
     163                 :          1 :         free(log_meta);
     164                 :          1 : }
     165                 :            : 
     166                 :            : void
     167                 :          1 : test_cbt_util_set_size_smaller_file_failure(void **state)
     168                 :            : {
     169                 :            :         int result;
     170                 :            :         void *log_meta;
     171                 :          1 :         uint64_t size_int64 = 2048;
     172                 :            :         char size_string[8];
     173                 :            :         snprintf(size_string, sizeof(size_string), "%" PRIu64, size_int64);
     174                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-s", size_string};
     175                 :            : 
     176                 :            : 
     177                 :          1 :         log_meta = malloc(sizeof(struct cbt_log_metadata));
     178                 :            : 
     179                 :          1 :         ((struct cbt_log_metadata*)log_meta)->size = 4096;
     180                 :            : 
     181                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r+");
     182                 :            : 
     183                 :          1 :         will_return(__wrap_fopen, test_log);
     184                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     185                 :            : 
     186                 :          1 :         result = cbt_util_set(5, args);
     187                 :          1 :         assert_int_equal(result, -EINVAL);
     188                 :            : 
     189                 :          1 :         free(log_meta);
     190                 :          1 : }
     191                 :            : 
     192                 :            : void
     193                 :          1 : test_cbt_util_set_size_malloc_failure(void **state)
     194                 :            : {
     195                 :            :         int result;
     196                 :          1 :         uint64_t size_int64 = 4096;
     197                 :            :         int file_size;
     198                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-s", "4194303"};
     199                 :            :         void *log_meta;
     200                 :            : 
     201                 :          1 :         uint64_t btmsize = bitmap_size(size_int64);
     202                 :            : 
     203                 :          1 :         file_size = sizeof(struct cbt_log_metadata) + 4096;
     204                 :          1 :         log_meta = malloc(file_size);
     205                 :            : 
     206                 :          1 :         ((struct cbt_log_metadata*)log_meta)->size = 4096;
     207                 :            :         
     208                 :          1 :         memcpy(log_meta + sizeof(struct cbt_log_metadata), (void*)memcpy, btmsize);
     209                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, file_size, "r+");
     210                 :            : 
     211                 :          1 :         will_return(__wrap_fopen, test_log);
     212                 :          1 :         expect_value(__wrap_fclose, fp, test_log);      
     213                 :            :                 
     214                 :          1 :         malloc_succeeds(true);
     215                 :          1 :         malloc_succeeds(false);
     216                 :            : 
     217                 :          1 :         result = cbt_util_set(5, args);
     218                 :          1 :         assert_int_equal(result, -ENOMEM);
     219                 :            : 
     220                 :          1 :         disable_malloc_mock();
     221                 :          1 :         free(log_meta);
     222                 :          1 : }
     223                 :            : 
     224                 :            : void
     225                 :          1 : test_cbt_util_set_size_no_bitmap_failure(void **state)
     226                 :            : {
     227                 :            :         int result;
     228                 :            :         void *log_meta;
     229                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-s", "4194304"};
     230                 :            : 
     231                 :          1 :         log_meta = malloc(sizeof(struct cbt_log_metadata));
     232                 :          1 :         ((struct cbt_log_metadata*)log_meta)->size = 4096;
     233                 :            : 
     234                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, sizeof(struct cbt_log_metadata), "r+");
     235                 :            : 
     236                 :          1 :         will_return(__wrap_fopen, test_log);
     237                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     238                 :            : 
     239                 :          1 :         result = cbt_util_set(5, args);
     240                 :          1 :         assert_int_equal(result, -EIO);
     241                 :            : 
     242                 :          1 :         free(log_meta);
     243                 :          1 : }
     244                 :            : 
     245                 :            : void
     246                 :          1 : test_cbt_util_set_size_write_failure(void **state)
     247                 :            : {
     248                 :            :         int result;
     249                 :            :         void *log_meta;
     250                 :          1 :         uint64_t size_int64 = 4194303;
     251                 :            :         char size_string[8];
     252                 :            :         snprintf(size_string, sizeof(size_string), "%" PRIu64, size_int64);
     253                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-s", size_string};
     254                 :            : 
     255                 :          1 :         uint64_t btmsize = bitmap_size(size_int64);
     256                 :            : 
     257                 :          1 :         int file_size = sizeof(struct cbt_log_metadata) + btmsize;
     258                 :          1 :         log_meta = malloc(file_size);
     259                 :            : 
     260                 :          1 :         ((struct cbt_log_metadata*)log_meta)->size = 4096;
     261                 :            : 
     262                 :          1 :         memcpy(log_meta + sizeof(struct cbt_log_metadata), (void*)memcpy, btmsize);
     263                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, file_size, "r");
     264                 :            : 
     265                 :          1 :         will_return(__wrap_fopen, test_log);
     266                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     267                 :            : 
     268                 :          1 :         result = cbt_util_set(5, args);
     269                 :          1 :         assert_int_equal(result, -EIO);
     270                 :            : 
     271                 :          1 :         free(log_meta);
     272                 :          1 : }
     273                 :            : 
     274                 :            : void
     275                 :          1 : test_cbt_util_set_size_reset_file_pointer_failure(void **state)
     276                 :            : {
     277                 :            :         int result;
     278                 :            :         void *log_meta;
     279                 :          1 :         uint64_t size_int64 = 4194303;
     280                 :            :         char size_string[8];
     281                 :            :         snprintf(size_string, sizeof(size_string), "%" PRIu64, size_int64);
     282                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-s", size_string};
     283                 :            : 
     284                 :          1 :         uint64_t btmsize = bitmap_size(size_int64);
     285                 :            : 
     286                 :          1 :         int file_size = sizeof(struct cbt_log_metadata) + btmsize;
     287                 :          1 :         log_meta = malloc(file_size);
     288                 :            : 
     289                 :          1 :         ((struct cbt_log_metadata*)log_meta)->size = 2048;
     290                 :            : 
     291                 :          1 :         memcpy(log_meta + sizeof(struct cbt_log_metadata), (void*)memcpy, btmsize);
     292                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, file_size, "r+");
     293                 :            : 
     294                 :          1 :         will_return(__wrap_fopen, test_log);
     295                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     296                 :            : 
     297                 :          1 :         fail_fseek(EIO);
     298                 :            : 
     299                 :          1 :         result = cbt_util_set(5, args);
     300                 :          1 :         assert_int_equal(result, -EIO);
     301                 :            : 
     302                 :          1 :         free(log_meta);
     303                 :          1 : }
     304                 :            : 
     305                 :            : void
     306                 :          1 : test_cbt_util_set_no_name_failure(void **state)
     307                 :            : {
     308                 :            :         int result;
     309                 :          1 :         char* args[] = {"cbt-util", "-p"};
     310                 :            : 
     311                 :            :         struct printf_data *output;
     312                 :            : 
     313                 :          1 :         output = setup_vprintf_mock(1024);
     314                 :            : 
     315                 :          1 :         result = cbt_util_set(2, args);
     316                 :          1 :         assert_int_equal(result, -EINVAL);
     317                 :          1 :         free_printf_data(output);
     318                 :          1 : }
     319                 :            : 
     320                 :            : void
     321                 :          1 : test_cbt_util_set_no_command_failure(void **state)
     322                 :            : {
     323                 :            :         int result;
     324                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log"};
     325                 :            :         struct printf_data *output;
     326                 :            : 
     327                 :          1 :         disable_malloc_mock();
     328                 :          1 :         output = setup_vprintf_mock(1024);
     329                 :            : 
     330                 :          1 :         result = cbt_util_set(3, args);
     331                 :          1 :         assert_int_equal(result, -EINVAL);
     332                 :          1 :         free_printf_data(output);
     333                 :          1 : }
     334                 :            : 
     335                 :            : void
     336                 :          1 : test_cbt_util_set_malloc_failure(void **state)
     337                 :            : {
     338                 :            :         int result;
     339                 :            :         uuid_t parent;
     340                 :            :         char uuid_str[36];
     341                 :          1 :         uuid_generate_random(parent);
     342                 :          1 :         uuid_unparse(parent, uuid_str);
     343                 :            : 
     344                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-p", uuid_str};
     345                 :            : 
     346                 :          1 :         malloc_succeeds(false);
     347                 :            : 
     348                 :          1 :         result = cbt_util_set(5, args);
     349                 :          1 :         assert_int_equal(result, -ENOMEM);
     350                 :            : 
     351                 :          1 :         disable_malloc_mock();
     352                 :          1 : }
     353                 :            : 
     354                 :            : void
     355                 :          1 : test_cbt_util_set_no_data_failure(void **state)
     356                 :            : {
     357                 :            :         int result;
     358                 :            :         uuid_t parent;
     359                 :            :         char uuid_str[36];
     360                 :          1 :         uuid_generate_random(parent);
     361                 :          1 :         uuid_unparse(parent, uuid_str);
     362                 :            : 
     363                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-p", uuid_str};
     364                 :            :         void *log_meta[1];
     365                 :            : 
     366                 :          1 :         FILE *test_log = fmemopen((void*)log_meta, 1, "r+");
     367                 :            : 
     368                 :          1 :         will_return(__wrap_fopen, test_log);
     369                 :          1 :         expect_value(__wrap_fclose, fp, test_log);
     370                 :            : 
     371                 :          1 :         result = cbt_util_set(5, args);
     372                 :          1 :         assert_int_equal(result, -EIO);
     373                 :          1 : }
     374                 :            : 
     375                 :            : void
     376                 :          1 : test_cbt_util_set_no_file_failure(void **state)
     377                 :            : {
     378                 :            :         int result;
     379                 :            :         uuid_t parent;
     380                 :            :         char uuid_str[36];
     381                 :          1 :         uuid_generate_random(parent);
     382                 :          1 :         uuid_unparse(parent, uuid_str);
     383                 :            : 
     384                 :            : 
     385                 :          1 :         char* args[] = {"cbt-util", "-n", "test_disk.log", "-p", uuid_str};
     386                 :            : 
     387                 :          1 :         will_return(__wrap_fopen, NULL);
     388                 :            : 
     389                 :          1 :         result = cbt_util_set(5, args);
     390                 :            : 
     391                 :          1 :         assert_int_equal(result, -ENOENT);
     392                 :          1 : }

Generated by: LCOV version 1.13