LCOV - code coverage report
Current view: top level - tests/lib - checksum.c (source / functions) Hit Total Coverage
Test: rapport Lines: 41 41 100.0 %
Date: 2021-12-10 16:22:55 Functions: 6 6 100.0 %
Branches: 18 36 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*-
       2                 :            :  * Copyright (c) 2015 Baptiste Daroussin <bapt@FreeBSD.org>
       3                 :            :  * All rights reserved.
       4                 :            :  *~
       5                 :            :  * Redistribution and use in source and binary forms, with or without
       6                 :            :  * modification, are permitted provided that the following conditions
       7                 :            :  * are met:
       8                 :            :  * 1. Redistributions of source code must retain the above copyright
       9                 :            :  *    notice, this list of conditions and the following disclaimer
      10                 :            :  *    in this position and unchanged.
      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                 :            :  *~
      15                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      16                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      17                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      18                 :            :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      19                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      20                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      21                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      22                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      23                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      24                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include <atf-c.h>
      28                 :            : #include <err.h>
      29                 :            : #include <unistd.h>
      30                 :            : #include <pkg.h>
      31                 :            : #include <private/pkg.h>
      32                 :            : 
      33                 :            : ATF_TC(check_symlinks);
      34                 :            : 
      35                 :         39 : ATF_TC_HEAD(check_symlinks, tc)
      36                 :            : {
      37                 :         39 :         atf_tc_set_md_var(tc, "descr", "testing checksums on symlinks");
      38                 :         39 : }
      39                 :            : 
      40                 :         13 : ATF_TC_BODY(check_symlinks, tc)
      41                 :            : {
      42                 :            :         unsigned char *sum;
      43                 :            : 
      44         [ +  - ]:         13 :         ATF_REQUIRE_EQ(symlink("foo", "bar"), 0);
      45                 :            : 
      46                 :         13 :         sum = pkg_checksum_symlink("bar", PKG_HASH_TYPE_SHA256_HEX);
      47         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(sum, "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");
      48                 :            : 
      49         [ +  - ]:         13 :         ATF_CHECK(pkg_checksum_validate_file("bar", "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae") == 0);
      50                 :         13 :         free(sum);
      51                 :            : 
      52                 :         13 :         sum = pkg_checksum_generate_file("bar", PKG_HASH_TYPE_SHA256_HEX);
      53         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(sum, "1$2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae");
      54                 :         13 :         free(sum);
      55                 :            : 
      56                 :         13 :         sum = pkg_checksum_generate_file("bar", PKG_HASH_TYPE_BLAKE2_BASE32);
      57         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(sum, "2$kgygnaah7wxsgn1wkuic4j78zq8dicmx53picmma99ogmkbd7k5nhuxr5xxemz6yzjab15oor3tjt7nupj8mh764y7kddbne7qw9agn");
      58                 :         13 :         free(sum);
      59                 :         13 :         sum = pkg_checksum_generate_file("bar", PKG_HASH_TYPE_BLAKE2S_BASE32);
      60         [ -  + ]:         13 :         ATF_REQUIRE_STREQ(sum, "5$eoiiccdoiuz9acwfo7fxi6abnrfdtg81mz5ccx7tbg5ny9755g7y");
      61                 :         13 :         free(sum);
      62                 :            : 
      63         [ +  - ]:         13 :         ATF_CHECK(pkg_checksum_validate_file("bar", "2$kgygnaah7wxsgn1wkuic4j78zq8dicmx53picmma99ogmkbd7k5nhuxr5xxemz6yzjab15oor3tjt7nupj8mh764y7kddbne7qw9agn") == 0);
      64         [ +  - ]:         13 :         ATF_CHECK(pkg_checksum_validate_file("bar", "5$eoiiccdoiuz9acwfo7fxi6abnrfdtg81mz5ccx7tbg5ny9755g7y") == 0);
      65         [ +  - ]:         13 :         ATF_CHECK(pkg_checksum_validate_file("bar", "1$2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae") == 0);
      66                 :         13 : }
      67                 :            : 
      68                 :            : ATF_TC(check_files);
      69                 :            : 
      70                 :         39 : ATF_TC_HEAD(check_files, tc)
      71                 :            : {
      72                 :         39 :         atf_tc_set_md_var(tc, "descr", "testing checksums on files");
      73                 :         39 : }
      74                 :            : 
      75                 :         13 : ATF_TC_BODY(check_files, tc)
      76                 :            : {
      77                 :            :         FILE *f;
      78                 :            :         unsigned char *sum;
      79                 :            : 
      80                 :         13 :         f = fopen("foo", "w");
      81                 :         13 :         fprintf(f, "bar\n");
      82                 :         13 :         fclose(f);
      83                 :            : 
      84                 :         13 :         sum = pkg_checksum_file("foo", PKG_HASH_TYPE_SHA256_HEX);
      85         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(sum, "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730");
      86                 :            : 
      87         [ +  - ]:         13 :         ATF_CHECK(pkg_checksum_validate_file("foo", "7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730") == 0);
      88                 :         13 :         free(sum);
      89                 :            : 
      90                 :         13 :         sum=pkg_checksum_generate_file("foo", PKG_HASH_TYPE_SHA256_HEX);
      91         [ -  + ]:         13 :         ATF_REQUIRE_STREQ(sum, "1$7d865e959b2466918c9863afca942d0fb89d7c9ac0c99bafc3749504ded97730");
      92                 :         13 : }
      93                 :            : 
      94                 :         78 : ATF_TP_ADD_TCS(tp)
      95                 :            : {
      96   [ +  -  -  +  :         39 :         ATF_TP_ADD_TC(tp, check_symlinks);
                   -  + ]
      97   [ +  -  -  +  :         39 :         ATF_TP_ADD_TC(tp, check_files);
                   -  + ]
      98                 :            : 
      99                 :         39 :         return (atf_no_error());
     100                 :         39 : }

Generated by: LCOV version 1.15