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

           Branch data     Line data    Source code
       1                 :            : /*-
       2                 :            :  * Copyright (c) 2013-2020 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 <pkg.h>
      29                 :            : #include <private/pkg.h>
      30                 :            : 
      31                 :            : ATF_TC(parse_mode);
      32                 :            : 
      33                 :         65 : ATF_TC_HEAD(parse_mode, tc)
      34                 :            : {
      35                 :         65 :         atf_tc_set_md_var(tc, "descr",
      36                 :            :             "parse_mode()");
      37                 :         65 : }
      38                 :            : 
      39                 :         13 : ATF_TC_BODY(parse_mode, tc)
      40                 :            : {
      41                 :            :         void *set;
      42                 :            : 
      43                 :         13 :         set = parse_mode("u+x");
      44         [ +  - ]:         13 :         ATF_REQUIRE(set == NULL);
      45                 :            : 
      46                 :         13 :         set = parse_mode("plop");
      47         [ +  - ]:         13 :         ATF_REQUIRE(set == NULL);
      48                 :            : 
      49                 :         13 :         set = parse_mode("0755");
      50         [ +  - ]:         13 :         ATF_REQUIRE(set != NULL);
      51                 :            : 
      52                 :         13 :         free(set);
      53                 :            : 
      54                 :         13 :         set = parse_mode("u=r,g=rX");
      55         [ -  + ]:         13 :         ATF_REQUIRE(set != NULL);
      56                 :            : 
      57                 :         13 :         free(set);
      58                 :         13 : }
      59                 :            : 
      60                 :            : ATF_TC(parse_keyword_attributes);
      61                 :            : 
      62                 :         65 : ATF_TC_HEAD(parse_keyword_attributes, tc)
      63                 :            : {
      64                 :         65 :         atf_tc_set_md_var(tc, "descr",
      65                 :            :             "parse_keyword_attributes()");
      66                 :         65 : }
      67                 :            : 
      68                 :         13 : ATF_TC_BODY(parse_keyword_attributes, tc)
      69                 :            : {
      70                 :            :         char buf[BUFSIZ];
      71                 :            :         struct file_attr *a;
      72                 :            : 
      73                 :         13 :         strlcpy(buf, "()", BUFSIZ);
      74         [ +  - ]:         13 :         ATF_REQUIRE(parse_keyword_args(buf, "plop") == NULL);
      75                 :            : 
      76                 :         13 :         strlcpy(buf, "(root, wheel)", BUFSIZ);
      77         [ +  - ]:         13 :         ATF_REQUIRE((a = parse_keyword_args(buf, "plop")) != NULL);
      78         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(a->owner, "root");
      79         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(a->group, "wheel");
      80                 :         13 :         free_file_attr(a);
      81                 :            : 
      82                 :         13 :         strlcpy(buf, "(root, wheel, 0755)", BUFSIZ);
      83         [ +  - ]:         13 :         ATF_REQUIRE((a = parse_keyword_args(buf, "plop")) != NULL);
      84         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(a->owner, "root");
      85         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(a->group, "wheel");
      86                 :         13 :         free_file_attr(a);
      87                 :            : 
      88                 :         13 :         strlcpy(buf, "(root, wheel, 0755,)", BUFSIZ);
      89         [ +  - ]:         13 :         ATF_REQUIRE((a = parse_keyword_args(buf, "plop")) != NULL);
      90         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(a->owner, "root");
      91         [ -  + ]:         13 :         ATF_REQUIRE_STREQ(a->group, "wheel");
      92                 :         13 :         free_file_attr(a);
      93                 :         13 : }
      94                 :            : 
      95                 :            : ATF_TC(parse_keyword);
      96                 :            : 
      97                 :         65 : ATF_TC_HEAD(parse_keyword, tc)
      98                 :            : {
      99                 :         65 :         atf_tc_set_md_var(tc, "descr",
     100                 :            :             "parse_keyword()");
     101                 :         65 : }
     102                 :            : 
     103                 :         13 : ATF_TC_BODY(parse_keyword, tc)
     104                 :            : {
     105                 :            :         char *keyword;
     106                 :            :         struct file_attr *attr;
     107                 :            :         char buf[BUFSIZ];
     108                 :            : 
     109                 :         13 :         strlcpy(buf, "something", BUFSIZ);
     110                 :         13 :         keyword = NULL;
     111                 :         13 :         attr = NULL;
     112         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(extract_keywords(buf, &keyword, &attr), "");
     113         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(keyword, "something");
     114         [ +  - ]:         13 :         ATF_REQUIRE_EQ(attr, NULL);
     115                 :            : 
     116                 :            :         /* empty keyword */
     117                 :         13 :         strlcpy(buf, "", BUFSIZ);
     118                 :         13 :         keyword = NULL;
     119                 :         13 :         attr = NULL;
     120         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(extract_keywords(buf, &keyword, &attr), "");
     121         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(keyword, "");
     122         [ +  - ]:         13 :         ATF_REQUIRE_EQ(attr, NULL);
     123                 :            : 
     124                 :            :         /* bad keyword */
     125                 :         13 :         strlcpy(buf, "(", BUFSIZ);
     126                 :         13 :         keyword = NULL;
     127                 :         13 :         attr = NULL;
     128         [ +  - ]:         13 :         ATF_REQUIRE_EQ(extract_keywords(buf, &keyword, &attr), NULL);
     129         [ +  - ]:         13 :         ATF_REQUIRE_EQ(keyword, NULL);
     130         [ +  - ]:         13 :         ATF_REQUIRE_EQ(attr, NULL);
     131                 :            : 
     132                 :            :         /* bad: empty keyword */
     133                 :         13 :         strlcpy(buf, "()", BUFSIZ);
     134                 :         13 :         keyword = NULL;
     135                 :         13 :         attr = NULL;
     136         [ +  - ]:         13 :         ATF_REQUIRE_EQ(extract_keywords(buf, &keyword, &attr), NULL);
     137         [ +  - ]:         13 :         ATF_REQUIRE_EQ(keyword, NULL);
     138         [ +  - ]:         13 :         ATF_REQUIRE_EQ(attr, NULL);
     139                 :            : 
     140                 :            :         /* ok only user keyword */
     141                 :         13 :         strlcpy(buf, "(root) that", BUFSIZ);
     142                 :         13 :         keyword = NULL;
     143                 :         13 :         attr = NULL;
     144         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(extract_keywords(buf, &keyword, &attr), "that");
     145         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(keyword, "");
     146         [ +  - ]:         13 :         ATF_REQUIRE(attr != NULL);
     147         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(attr->owner, "root");
     148                 :            : 
     149                 :            :         /* ok only group keyword */
     150                 :         13 :         strlcpy(buf, "(,wheel) that", BUFSIZ);
     151                 :         13 :         keyword = NULL;
     152                 :         13 :         attr = NULL;
     153         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(extract_keywords(buf, &keyword, &attr), "that");
     154         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(keyword, "");
     155         [ +  - ]:         13 :         ATF_REQUIRE(attr != NULL);
     156         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(attr->group, "wheel");
     157                 :            : 
     158                 :            :         /* ok only group with space keyword */
     159                 :         13 :         strlcpy(buf, "( , wheel ,) that", BUFSIZ);
     160                 :         13 :         keyword = NULL;
     161                 :         13 :         attr = NULL;
     162         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(extract_keywords(buf, &keyword, &attr), "that");
     163         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(keyword, "");
     164         [ +  - ]:         13 :         ATF_REQUIRE(attr != NULL);
     165         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(attr->group, "wheel");
     166         [ +  - ]:         13 :         ATF_REQUIRE_EQ(attr->owner, NULL);
     167                 :            : 
     168                 :         13 :         strlcpy(buf, "(, wheel ,perm,ffags,)", BUFSIZ);
     169         [ +  - ]:         13 :         ATF_REQUIRE_EQ(parse_keyword_args(buf, "plop"), NULL);
     170                 :         13 : }
     171                 :            : 
     172                 :            : ATF_TC(parse_plist);
     173                 :            : 
     174                 :         65 : ATF_TC_HEAD(parse_plist, tc)
     175                 :            : {
     176                 :         65 :         atf_tc_set_md_var(tc, "descr",
     177                 :            :             "parse_plist()");
     178                 :         65 : }
     179                 :            : 
     180                 :         13 : ATF_TC_BODY(parse_plist, tc)
     181                 :            : {
     182                 :            :         struct pkg *p;
     183                 :            :         struct plist *plist;
     184                 :            :         char buf[BUFSIZ];
     185                 :            : 
     186         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, pkg_new(&p, PKG_INSTALLED));
     187                 :            : 
     188                 :            :         /* On a non existing directory this should not work */
     189                 :         13 :         plist = plist_new(p, "/nonexist");
     190         [ +  - ]:         13 :         ATF_REQUIRE(plist == NULL);
     191                 :            : 
     192                 :         13 :         plist = plist_new(p, NULL);
     193         [ +  - ]:         13 :         ATF_REQUIRE(plist != NULL);
     194                 :         13 :         plist_free(plist);
     195                 :            : 
     196                 :         13 :         plist = plist_new(p, "/tmp");
     197         [ +  - ]:         13 :         ATF_REQUIRE(plist != NULL);
     198                 :            : 
     199         [ +  - ]:         13 :         ATF_REQUIRE(plist->pkg == p);
     200         [ +  - ]:         13 :         ATF_REQUIRE_EQ(plist->prefix[0], '\0');
     201                 :            : 
     202                 :         13 :         strlcpy(buf, "@cwd /myprefix", BUFSIZ);
     203         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     204         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(p->prefix, "/myprefix");
     205                 :            : 
     206         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->prefix, "/myprefix");
     207                 :            : 
     208         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->uname, "root");
     209         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->gname, "wheel");
     210                 :            : 
     211                 :         13 :         strlcpy(buf, "@owner bob", BUFSIZ);
     212         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     213         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->uname, "bob");
     214                 :            : 
     215                 :         13 :         strlcpy(buf, "@group sponge", BUFSIZ);
     216         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     217         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->gname, "sponge");
     218                 :            : 
     219                 :         13 :         strlcpy(buf, "@group", BUFSIZ);
     220         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     221         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->gname, "wheel");
     222                 :            : 
     223                 :         13 :         strlcpy(buf, "@owner", BUFSIZ);
     224         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     225         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->uname, "root");
     226                 :            : 
     227                 :         13 :         strlcpy(buf, "@cwd plop", BUFSIZ);
     228         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     229         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->prefix, "plop");
     230                 :            : 
     231                 :         13 :         strlcpy(buf, "@cwd", BUFSIZ);
     232         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     233         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->prefix, "/myprefix");
     234         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->slash, "/");
     235                 :            : 
     236                 :         13 :         strlcpy(buf, "@cwd /another/prefix/", BUFSIZ);
     237         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     238         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->prefix, "/another/prefix/");
     239         [ +  - ]:         13 :         ATF_REQUIRE_STREQ(plist->slash, "");
     240                 :            : 
     241         [ +  - ]:         13 :         ATF_REQUIRE_EQ(0, plist->perm);
     242                 :         13 :         strlcpy(buf, "@mode 0755", BUFSIZ);
     243         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     244         [ +  - ]:         13 :         ATF_REQUIRE_EQ(0755, plist->perm);
     245                 :            : 
     246                 :         13 :         strlcpy(buf, "@mode", BUFSIZ);
     247         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_OK, plist_parse_line(plist, buf));
     248         [ +  - ]:         13 :         ATF_REQUIRE_EQ(0, plist->perm);
     249                 :            : 
     250                 :         13 :         strlcpy(buf, "@blabla", BUFSIZ);
     251         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_FATAL, plist_parse_line(plist, buf));
     252                 :            : 
     253                 :         13 :         strlcpy(buf, "nonexisting/file", BUFSIZ);
     254         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_FATAL, plist_parse_line(plist, buf));
     255                 :            : 
     256                 :         13 :         strlcpy(buf, "@dir nonexisting", BUFSIZ);
     257         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_FATAL, plist_parse_line(plist, buf));
     258                 :            : 
     259                 :         13 :         strlcpy(buf, "@dirrm nonexisting", BUFSIZ);
     260         [ +  - ]:         13 :         ATF_REQUIRE_EQ(EPKG_FATAL, plist_parse_line(plist, buf));
     261                 :            : 
     262                 :         13 :         pkg_free(p);
     263                 :         13 :         plist_free(plist);
     264                 :         13 : }
     265                 :            : 
     266                 :        130 : ATF_TP_ADD_TCS(tp)
     267                 :            : {
     268   [ -  +  -  +  :         65 :         ATF_TP_ADD_TC(tp, parse_mode);
                   -  + ]
     269   [ +  -  -  +  :         65 :         ATF_TP_ADD_TC(tp, parse_plist);
                   -  + ]
     270   [ +  -  -  +  :         65 :         ATF_TP_ADD_TC(tp, parse_keyword_attributes);
                   -  + ]
     271   [ +  -  -  +  :         65 :         ATF_TP_ADD_TC(tp, parse_keyword);
                   -  + ]
     272                 :            : 
     273                 :         65 :         return (atf_no_error());
     274                 :         65 : }

Generated by: LCOV version 1.15