LCOV - code coverage report
Current view: top level - tests/lib - lua.c (source / functions) Hit Total Coverage
Test: rapport Lines: 85 368 23.1 %
Date: 2021-12-10 16:22:55 Functions: 10 16 62.5 %
Branches: 57 270 21.1 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2021 Baptiste Daroussin <bapt@FreeBSD.org>
       3                 :            :  *
       4                 :            :  * Redistribution and use in source and binary forms, with or without
       5                 :            :  * modification, are permitted provided that the following conditions
       6                 :            :  * are met:
       7                 :            :  * 1. Redistributions of source code must retain the above copyright
       8                 :            :  *    notice, this list of conditions and the following disclaimer
       9                 :            :  *    in this position and unchanged.
      10                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      11                 :            :  *    notice, this list of conditions and the following disclaimer in the
      12                 :            :  *    documentation and/or other materials provided with the distribution.
      13                 :            :  *
      14                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      15                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      16                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      17                 :            :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      18                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      19                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      20                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      21                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      22                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      23                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      24                 :            :  */
      25                 :            : 
      26                 :            : #include <atf-c.h>
      27                 :            : #include <err.h>
      28                 :            : #include <unistd.h>
      29                 :            : #include <pkg.h>
      30                 :            : #include <private/lua.h>
      31                 :            : #include <fcntl.h>
      32                 :            : #include <stdlib.h>
      33                 :            : 
      34                 :            : ATF_TC(readdir);
      35                 :            : ATF_TC(stat);
      36                 :            : ATF_TC(print_msg);
      37                 :            : ATF_TC(execute);
      38                 :            : ATF_TC(override);
      39                 :            : ATF_TC(fileops);
      40                 :            : ATF_TC(prefix_path);
      41                 :            : 
      42                 :         11 : ATF_TC_HEAD(readdir, tc) { }
      43                 :         11 : ATF_TC_HEAD(stat, tc) {}
      44                 :         11 : ATF_TC_HEAD(print_msg, tc) {}
      45                 :         11 : ATF_TC_HEAD(execute, tc) {}
      46                 :         11 : ATF_TC_HEAD(override, tc) {}
      47                 :         11 : ATF_TC_HEAD(fileops, tc) {}
      48                 :         11 : ATF_TC_HEAD(prefix_path, tc) {}
      49                 :            : 
      50                 :          0 : ATF_TC_BODY(readdir, tc)
      51                 :            : {
      52                 :          0 :         int rootfd = open(getcwd(NULL, 0), O_DIRECTORY);
      53                 :          0 :         lua_State *L = luaL_newstate();
      54                 :            :         static const luaL_Reg test_lib[] = {
      55                 :            :                 { "readdir", lua_readdir },
      56                 :            :                 { NULL, NULL },
      57                 :            :         };
      58                 :          0 :         luaL_openlibs(L);
      59                 :          0 :         lua_override_ios(L, false);
      60                 :          0 :         luaL_newlib(L, test_lib);
      61                 :          0 :         lua_setglobal(L, "test");
      62                 :          0 :         lua_pushinteger(L, rootfd);
      63                 :          0 :         lua_setglobal(L, "rootfd");
      64                 :            : 
      65                 :          0 :         pid_t p = atf_utils_fork();
      66         [ #  # ]:          0 :         if (p == 0) {
      67   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.readdir(\".\", \"plop\")")) {
      68                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
      69                 :          0 :                 }
      70                 :          0 :                 exit(lua_tonumber(L, -1));
      71                 :            :         }
      72                 :          0 :         atf_utils_wait(p, 0, "[string \"test.readdir(\".\", \"plop\")\"]:1: bad argument #2 to 'readdir' (pkg.readdir takes exactly one argument)\n", "");
      73                 :            : 
      74                 :          0 :         p = atf_utils_fork();
      75         [ #  # ]:          0 :         if (p == 0) {
      76   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.readdir()")) {
      77                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
      78                 :          0 :                 }
      79                 :          0 :                 exit(lua_tonumber(L, -1));
      80                 :            :         }
      81                 :          0 :         atf_utils_wait(p, 0, "[string \"test.readdir()\"]:1: bad argument #0 to 'readdir' (pkg.readdir takes exactly one argument)\n", "");
      82                 :            : 
      83                 :          0 :         p = atf_utils_fork();
      84         [ #  # ]:          0 :         if (p == 0) {
      85   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "res = test.readdir(\".\")\nif res ~= nil then print(#res) end")) {
      86                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
      87                 :          0 :                 }
      88                 :          0 :                 exit(lua_tonumber(L, -1));
      89                 :            :         }
      90                 :          0 :         atf_utils_wait(p, 0, "2\n", "");
      91                 :          0 :         p = atf_utils_fork();
      92         [ #  # ]:          0 :         if (p == 0) {
      93   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "if test.readdir(\"nonexistent\") ~= nil then print(\"non nil output\") end")) {
      94                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
      95                 :          0 :                 }
      96                 :          0 :                 exit(lua_tonumber(L, -1));
      97                 :            :         }
      98                 :          0 :         atf_utils_wait(p, 0, "", "");
      99                 :            : 
     100                 :          0 :         p = atf_utils_fork();
     101         [ #  # ]:          0 :         if (p == 0) {
     102   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "if test.readdir(\"/\") ~= nil then print(\"nil output\") end")) {
     103                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     104                 :          0 :                 }
     105                 :          0 :                 exit(lua_tonumber(L, -1));
     106                 :            :         }
     107                 :          0 :         atf_utils_wait(p, 0, "", "");
     108                 :            : 
     109                 :          0 :         close(open("testfile", O_CREAT|O_TRUNC));
     110                 :          0 :         p = atf_utils_fork();
     111         [ #  # ]:          0 :         if (p == 0) {
     112   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "if test.readdir(\"testfile\") ~= nil then print(\"nil output\") end")) {
     113                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     114                 :          0 :                 }
     115                 :          0 :                 exit(lua_tonumber(L, -1));
     116                 :            :         }
     117                 :          0 :         atf_utils_wait(p, 0, "", "");
     118                 :            : 
     119                 :          0 :         p = atf_utils_fork();
     120         [ #  # ]:          0 :         if (p == 0) {
     121   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "res = test.readdir(\".\")\n print(#res)")) {
     122                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     123                 :          0 :                 }
     124                 :          0 :                 exit(lua_tonumber(L, -1));
     125                 :            :         }
     126                 :          0 :         atf_utils_wait(p, 0, "3\n", "");
     127                 :          0 : }
     128                 :            : 
     129                 :          0 : ATF_TC_BODY(stat, tc)
     130                 :            : {
     131                 :          0 :         int rootfd = open(getcwd(NULL, 0), O_DIRECTORY);
     132                 :          0 :         lua_State *L = luaL_newstate();
     133                 :            :         static const luaL_Reg test_lib[] = {
     134                 :            :                 { "stat", lua_stat },
     135                 :            :                 { NULL, NULL },
     136                 :            :         };
     137                 :          0 :         luaL_openlibs(L);
     138                 :          0 :         lua_override_ios(L, false);
     139                 :          0 :         luaL_newlib(L, test_lib);
     140                 :          0 :         lua_setglobal(L, "test");
     141                 :          0 :         lua_pushinteger(L, rootfd);
     142                 :          0 :         lua_setglobal(L, "rootfd");
     143                 :            : 
     144                 :          0 :         pid_t p = atf_utils_fork();
     145         [ #  # ]:          0 :         if (p == 0) {
     146   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.stat(\".\", \"plop\")")) {
     147                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     148                 :          0 :                 }
     149                 :          0 :                 exit(lua_tonumber(L, -1));
     150                 :            :         }
     151                 :          0 :         atf_utils_wait(p, 0, "[string \"test.stat(\".\", \"plop\")\"]:1: bad argument #2 to 'stat' (pkg.stat takes exactly one argument)\n", "");
     152                 :            : 
     153                 :          0 :         p = atf_utils_fork();
     154         [ #  # ]:          0 :         if (p == 0) {
     155   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.stat()")) {
     156                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     157                 :          0 :                 }
     158                 :          0 :                 exit(lua_tonumber(L, -1));
     159                 :            :         }
     160                 :          0 :         atf_utils_wait(p, 0, "[string \"test.stat()\"]:1: bad argument #0 to 'stat' (pkg.stat takes exactly one argument)\n", "");
     161                 :            : 
     162                 :          0 :         p = atf_utils_fork();
     163         [ #  # ]:          0 :         if (p == 0) {
     164   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "st = test.stat(\".\")\nprint(st.type)")) {
     165                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     166                 :          0 :                 }
     167                 :          0 :                 exit(lua_tonumber(L, -1));
     168                 :            :         }
     169                 :          0 :         atf_utils_wait(p, 0, "dir\n", "");
     170                 :            : 
     171                 :          0 :         close(open("testfile", O_CREAT|O_TRUNC));
     172                 :          0 :         p = atf_utils_fork();
     173         [ #  # ]:          0 :         if (p == 0) {
     174   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "st = test.stat(\"testfile\")\nprint(st.type)")) {
     175                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     176                 :          0 :                 }
     177                 :          0 :                 exit(lua_tonumber(L, -1));
     178                 :            :         }
     179                 :          0 :         atf_utils_wait(p, 0, "reg\n", "");
     180                 :            : 
     181                 :          0 :         symlink("testfile", "plop");
     182                 :          0 :         p = atf_utils_fork();
     183         [ #  # ]:          0 :         if (p == 0) {
     184   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "st = test.stat(\"plop\")\nprint(st.type)")) {
     185                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     186                 :          0 :                 }
     187                 :          0 :                 exit(lua_tonumber(L, -1));
     188                 :            :         }
     189                 :          0 :         atf_utils_wait(p, 0, "lnk\n", "");
     190                 :            : 
     191                 :          0 :         lua_pushinteger(L, -1);
     192                 :          0 :         lua_setglobal(L, "rootfd");
     193                 :          0 :         p = atf_utils_fork();
     194         [ #  # ]:          0 :         if (p == 0) {
     195   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "st = test.stat(\".\")\nprint(st)")) {
     196                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     197                 :          0 :                 }
     198                 :          0 :                 exit(lua_tonumber(L, -1));
     199                 :            :         }
     200                 :          0 :         atf_utils_wait(p, 0, "nil\n", "");
     201                 :          0 : }
     202                 :            : 
     203                 :          0 : ATF_TC_BODY(print_msg, tc)
     204                 :            : {
     205                 :          0 :         lua_State *L = luaL_newstate();
     206                 :            :         static const luaL_Reg test_lib[] = {
     207                 :            :                 { "print_msg", lua_print_msg },
     208                 :            :                 { NULL, NULL },
     209                 :            :         };
     210                 :          0 :         int fd = open("testfile", O_CREAT|O_TRUNC);
     211                 :          0 :         luaL_openlibs(L);
     212                 :          0 :         lua_override_ios(L, false);
     213                 :          0 :         luaL_newlib(L, test_lib);
     214                 :          0 :         lua_setglobal(L, "test");
     215                 :          0 :         lua_pushinteger(L, fd);
     216                 :          0 :         lua_setglobal(L, "msgfd");
     217                 :            : 
     218                 :          0 :         pid_t p = atf_utils_fork();
     219         [ #  # ]:          0 :         if (p == 0) {
     220   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.print_msg()")) {
     221                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     222                 :          0 :                 }
     223                 :          0 :                 exit(lua_tonumber(L, -1));
     224                 :            :         }
     225                 :          0 :         atf_utils_wait(p, 0, "[string \"test.print_msg()\"]:1: bad argument #0 to 'print_msg' (pkg.print_msg takes exactly one argument)\n", "");
     226                 :            : 
     227                 :          0 :         p = atf_utils_fork();
     228         [ #  # ]:          0 :         if (p == 0) {
     229   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.print_msg(1, 2)")) {
     230                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     231                 :          0 :                 }
     232                 :          0 :                 exit(lua_tonumber(L, -1));
     233                 :            :         }
     234                 :          0 :         atf_utils_wait(p, 0, "[string \"test.print_msg(1, 2)\"]:1: bad argument #2 to 'print_msg' (pkg.print_msg takes exactly one argument)\n", "");
     235                 :            : 
     236                 :          0 :         p = atf_utils_fork();
     237         [ #  # ]:          0 :         if (p == 0) {
     238   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.print_msg(\"bla\")")) {
     239                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     240                 :          0 :                 }
     241                 :          0 :                 exit(lua_tonumber(L, -1));
     242                 :            :         }
     243                 :          0 :         atf_utils_wait(p, 0, "", "");
     244                 :          0 :         close(fd);
     245                 :          0 :         atf_utils_compare_file("testfile", "bla\n");
     246                 :          0 : }
     247                 :            : 
     248                 :          0 : ATF_TC_BODY(execute, tc)
     249                 :            : {
     250                 :          0 :         lua_State *L = luaL_newstate();
     251                 :            :         static const luaL_Reg test_lib[] = {
     252                 :            :                 { "exec", lua_exec },
     253                 :            :                 { NULL, NULL },
     254                 :            :         };
     255                 :          0 :         luaL_openlibs(L);
     256                 :          0 :         lua_override_ios(L, false);
     257                 :          0 :         luaL_newlib(L, test_lib);
     258                 :          0 :         lua_setglobal(L, "test");
     259                 :            : 
     260                 :          0 :         pid_t p = atf_utils_fork();
     261         [ #  # ]:          0 :         if (p == 0) {
     262   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.exec()")) {
     263                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     264                 :          0 :                 }
     265                 :          0 :                 exit(lua_tonumber(L, -1));
     266                 :            :         }
     267                 :          0 :         atf_utils_wait(p, 0, "[string \"test.exec()\"]:1: bad argument #0 to 'exec' (pkg.exec takes exactly one argument)\n", "");
     268                 :            : 
     269                 :          0 :         p = atf_utils_fork();
     270         [ #  # ]:          0 :         if (p == 0) {
     271   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.exec(plop)")) {
     272                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     273                 :          0 :                 }
     274                 :          0 :                 exit(lua_tonumber(L, -1));
     275                 :            :         }
     276                 :          0 :         atf_utils_wait(p, 0, "[string \"test.exec(plop)\"]:1: bad argument #1 to 'exec' (table expected, got nil)\n", "");
     277                 :            : 
     278                 :          0 :         p = atf_utils_fork();
     279         [ #  # ]:          0 :         if (p == 0) {
     280   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.exec(plop, meh)")) {
     281                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     282                 :          0 :                 }
     283                 :          0 :                 exit(lua_tonumber(L, -1));
     284                 :            :         }
     285                 :          0 :         atf_utils_wait(p, 0, "[string \"test.exec(plop, meh)\"]:1: bad argument #2 to 'exec' (pkg.exec takes exactly one argument)\n", "");
     286                 :            : 
     287                 :          0 :         p = atf_utils_fork();
     288         [ #  # ]:          0 :         if (p == 0) {
     289   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "test.exec({\"/bin/echo\", \"1\"})")) {
     290                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     291                 :          0 :                 }
     292                 :          0 :                 exit(lua_tonumber(L, -1));
     293                 :            :         }
     294                 :          0 :         atf_utils_wait(p, 0, "1\n", "");
     295                 :          0 : }
     296                 :            : 
     297                 :          0 : ATF_TC_BODY(override, tc)
     298                 :            : {
     299                 :          0 :         lua_State *L = luaL_newstate();
     300                 :          0 :         luaL_openlibs(L);
     301                 :          0 :         lua_override_ios(L, true);
     302                 :            : 
     303                 :          0 :         pid_t p = atf_utils_fork();
     304         [ #  # ]:          0 :         if (p == 0) {
     305   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "os.execute(\"/usr/bin/true\")")) {
     306                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     307                 :          0 :                 }
     308                 :          0 :                 exit(lua_tonumber(L, -1));
     309                 :            :         }
     310                 :          0 :         atf_utils_wait(p, 0, "[string \"os.execute(\"/usr/bin/true\")\"]:1: os.execute not available\n", "");
     311                 :            : 
     312                 :          0 :         p = atf_utils_fork();
     313         [ #  # ]:          0 :         if (p == 0) {
     314   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "os.exit(1)")) {
     315                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     316                 :          0 :                 }
     317                 :          0 :                 exit(lua_tonumber(L, -1));
     318                 :            :         }
     319                 :          0 :         atf_utils_wait(p, 0, "[string \"os.exit(1)\"]:1: os.exit not available\n", "");
     320                 :            : 
     321                 :          0 :         int rootfd = open(getcwd(NULL, 0), O_DIRECTORY);
     322                 :          0 :         lua_pushinteger(L, rootfd);
     323                 :          0 :         lua_setglobal(L, "rootfd");
     324                 :          0 :         p = atf_utils_fork();
     325         [ #  # ]:          0 :         if (p == 0) {
     326   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "io.close(io.open(\"/plop\", \"w+\"))")) {
     327                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     328                 :          0 :                 }
     329                 :          0 :                 exit(lua_tonumber(L, -1));
     330                 :            :         }
     331                 :          0 :         atf_utils_wait(p, 0, "", "");
     332                 :          0 :         atf_utils_file_exists("plop");
     333                 :            : 
     334                 :          0 :         p = atf_utils_fork();
     335         [ #  # ]:          0 :         if (p == 0) {
     336   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "os.rename(\"/plop\", \"/bob\")")) {
     337                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     338                 :          0 :                 }
     339                 :          0 :                 exit(lua_tonumber(L, -1));
     340                 :            :         }
     341                 :          0 :         atf_utils_wait(p, 0, "", "");
     342                 :          0 :         atf_utils_file_exists("bob");
     343                 :            : 
     344                 :          0 :         p = atf_utils_fork();
     345         [ #  # ]:          0 :         if (p == 0) {
     346   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "os.remove(\"/bob\")\nassert(io.open(\"/bob\", \"r\"))")) {
     347                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     348                 :          0 :                 }
     349                 :          0 :                 exit(lua_tonumber(L, -1));
     350                 :            :         }
     351                 :          0 :         atf_utils_wait(p, 0, "[string \"os.remove(\"/bob\")...\"]:2: /bob: No such file or directory\n", "");
     352                 :          0 : }
     353                 :            : 
     354                 :         10 : ATF_TC_BODY(fileops, tc)
     355                 :            : {
     356                 :         10 :         int rootfd = open(getcwd(NULL, 0), O_DIRECTORY);
     357                 :         10 :         lua_State *L = luaL_newstate();
     358                 :         10 :         luaL_openlibs(L);
     359                 :         10 :         lua_pushinteger(L, rootfd);
     360                 :         10 :         lua_setglobal(L, "rootfd");
     361                 :         10 :         lua_override_ios(L, true);
     362                 :            :         static const luaL_Reg test_lib[] = {
     363                 :            :                 { "copy", lua_pkg_copy },
     364                 :            :                 { "cmp", lua_pkg_filecmp},
     365                 :            :                 { NULL, NULL },
     366                 :            :         };
     367                 :         10 :         luaL_newlib(L, test_lib);
     368                 :         10 :         lua_setglobal(L, "test");
     369                 :            :         pid_t p;
     370                 :            : 
     371                 :         10 :         FILE *f1 = fopen("test1", "w+");
     372                 :         10 :         FILE *f2 = fopen("test2", "w+");
     373                 :         10 :         FILE *f3 = fopen("test3", "w+");
     374                 :            : 
     375                 :         10 :         fputs("test", f1);
     376                 :         10 :         fputs("test2", f2);
     377                 :         10 :         fputs("test", f3);
     378                 :         10 :         fclose(f1);
     379                 :         10 :         fclose(f2);
     380                 :         10 :         fclose(f3);
     381                 :            : 
     382                 :         10 :         p = atf_utils_fork();
     383         [ +  + ]:         10 :         if (p == 0) {
     384   [ +  -  +  - ]:          1 :                 if (luaL_dostring(L, "test.cmp(1)")) {
     385                 :          1 :                         printf("%s\n", lua_tostring(L, -1));
     386                 :          1 :                 }
     387                 :          1 :                 exit(lua_tonumber(L, -1));
     388                 :            :         }
     389                 :          9 :         atf_utils_wait(p, 0, "[string \"test.cmp(1)\"]:1: bad argument #1 to 'cmp' (pkg.filecmp takes exactly two arguments)\n", "");
     390                 :            : 
     391                 :          9 :         p = atf_utils_fork();
     392         [ +  + ]:          9 :         if (p == 0) {
     393   [ +  -  +  - ]:          1 :                 if (luaL_dostring(L, "test.cmp(1, 2, 3)")) {
     394                 :          1 :                         printf("%s\n", lua_tostring(L, -1));
     395                 :          1 :                 }
     396                 :          1 :                 exit(lua_tonumber(L, -1));
     397                 :            :         }
     398                 :          8 :         atf_utils_wait(p, 0, "[string \"test.cmp(1, 2, 3)\"]:1: bad argument #3 to 'cmp' (pkg.filecmp takes exactly two arguments)\n", "");
     399                 :            : 
     400                 :          8 :         p = atf_utils_fork();
     401         [ +  + ]:          8 :         if (p == 0) {
     402   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "return test.cmp(1, 2)")) {
     403                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     404                 :          0 :                 }
     405                 :          1 :                 exit(lua_tonumber(L, -1));
     406                 :            :         }
     407                 :          7 :         atf_utils_wait(p, 2, "", "");
     408                 :            : 
     409                 :          7 :         p = atf_utils_fork();
     410         [ +  + ]:          7 :         if (p == 0) {
     411   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "return test.cmp(\"test1\", 2)")) {
     412                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     413                 :          0 :                 }
     414                 :          1 :                 exit(lua_tonumber(L, -1));
     415                 :            :         }
     416                 :          6 :         atf_utils_wait(p, 2, "", "");
     417                 :            : 
     418                 :          6 :         p = atf_utils_fork();
     419         [ +  + ]:          6 :         if (p == 0) {
     420   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "return test.cmp(\"test1\", \"test2\")")) {
     421                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     422                 :          0 :                 }
     423                 :          1 :                 exit(lua_tonumber(L, -1));
     424                 :            :         }
     425                 :          5 :         atf_utils_wait(p, 1, "", "");
     426                 :            : 
     427                 :          5 :         p = atf_utils_fork();
     428         [ +  + ]:          5 :         if (p == 0) {
     429   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "return test.cmp(\"test1\", \"test3\")")) {
     430                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     431                 :          0 :                 }
     432                 :          1 :                 exit(lua_tonumber(L, -1));
     433                 :            :         }
     434                 :          4 :         atf_utils_wait(p, 0, "", "");
     435                 :            : 
     436                 :          4 :         p = atf_utils_fork();
     437         [ +  + ]:          4 :         if (p == 0) {
     438   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "return(test.copy(1, 2))")) {
     439                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     440                 :          0 :                 }
     441                 :          1 :                 exit(lua_tonumber(L, -1));
     442                 :            :         }
     443                 :          3 :         atf_utils_wait(p, 2, "", "");
     444                 :            : 
     445                 :          3 :         p = atf_utils_fork();
     446         [ +  + ]:          3 :         if (p == 0) {
     447   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "return(test.copy(\"test1\", \"nonexistent/2\"))")) {
     448                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     449                 :          0 :                 }
     450                 :          1 :                 exit(lua_tonumber(L, -1));
     451                 :            :         }
     452                 :          2 :         atf_utils_wait(p, 2, "", "");
     453                 :            : 
     454                 :          2 :         p = atf_utils_fork();
     455         [ +  + ]:          2 :         if (p == 0) {
     456   [ +  -  -  + ]:          1 :                 if (luaL_dostring(L, "test.copy(\"test1\", \"test4\")\nreturn test.cmp(\"test1\", \"test4\")")) {
     457                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     458                 :          0 :                 }
     459                 :          1 :                 exit(lua_tonumber(L, -1));
     460                 :            :         }
     461                 :          1 :         atf_utils_wait(p, 0, "", "");
     462                 :          1 : }
     463                 :            : 
     464                 :          0 : ATF_TC_BODY(prefix_path, tc)
     465                 :            : {
     466                 :          0 :         struct pkg *pkg = NULL;
     467                 :          0 :         pkg_new(&pkg, PKG_INSTALLED);
     468                 :          0 :         pkg_set(pkg, PKG_PREFIX, "/myprefix");
     469                 :          0 :         lua_State *L = luaL_newstate();
     470                 :            :         static const luaL_Reg test_lib[] = {
     471                 :            :                 { "prefix_path", lua_prefix_path },
     472                 :            :                 { NULL, NULL },
     473                 :            :         };
     474                 :          0 :         luaL_openlibs(L);
     475                 :          0 :         lua_override_ios(L, false);
     476                 :          0 :         luaL_newlib(L, test_lib);
     477                 :          0 :         lua_setglobal(L, "test");
     478                 :          0 :         lua_pushlightuserdata(L, pkg);
     479                 :          0 :         lua_setglobal(L, "package");
     480                 :            :         pid_t p;
     481                 :            : 
     482                 :          0 :         p = atf_utils_fork();
     483         [ #  # ]:          0 :         if (p == 0) {
     484   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "print(test.prefix_path())")) {
     485                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     486                 :          0 :                 }
     487                 :          0 :                 exit(lua_tonumber(L, -1));
     488                 :            :         }
     489                 :          0 :         atf_utils_wait(p, 0, "[string \"print(test.prefix_path())\"]:1: bad argument #0 to 'prefix_path' (pkg.prefix_path takes exactly one argument)\n", "");
     490                 :            : 
     491                 :          0 :         p = atf_utils_fork();
     492         [ #  # ]:          0 :         if (p == 0) {
     493   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "print(test.prefix_path(1, 2))")) {
     494                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     495                 :          0 :                 }
     496                 :          0 :                 exit(lua_tonumber(L, -1));
     497                 :            :         }
     498                 :          0 :         atf_utils_wait(p, 0, "[string \"print(test.prefix_path(1, 2))\"]:1: bad argument #2 to 'prefix_path' (pkg.prefix_path takes exactly one argument)\n", "");
     499                 :            : 
     500                 :          0 :         p = atf_utils_fork();
     501         [ #  # ]:          0 :         if (p == 0) {
     502   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "print(test.prefix_path(1))")) {
     503                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     504                 :          0 :                 }
     505                 :          0 :                 exit(lua_tonumber(L, -1));
     506                 :            :         }
     507                 :          0 :         atf_utils_wait(p, 0, "/myprefix/1\n", "");
     508                 :            : 
     509                 :          0 :         p = atf_utils_fork();
     510         [ #  # ]:          0 :         if (p == 0) {
     511   [ #  #  #  # ]:          0 :                 if (luaL_dostring(L, "print(test.prefix_path(\"/1\"))")) {
     512                 :          0 :                         printf("%s\n", lua_tostring(L, -1));
     513                 :          0 :                 }
     514                 :          0 :                 exit(lua_tonumber(L, -1));
     515                 :            :         }
     516                 :          0 :         atf_utils_wait(p, 0, "/1\n", "");
     517                 :          0 : }
     518                 :            : 
     519                 :         22 : ATF_TP_ADD_TCS(tp)
     520                 :            : {
     521   [ +  -  -  +  :         11 :         ATF_TP_ADD_TC(tp, readdir);
                   -  + ]
     522   [ +  -  -  +  :         11 :         ATF_TP_ADD_TC(tp, stat);
                   -  + ]
     523   [ +  -  -  +  :         11 :         ATF_TP_ADD_TC(tp, print_msg);
                   -  + ]
     524   [ -  +  -  +  :         11 :         ATF_TP_ADD_TC(tp, execute);
                   -  + ]
     525   [ +  -  -  +  :         11 :         ATF_TP_ADD_TC(tp, override);
                   -  + ]
     526   [ +  -  -  +  :         11 :         ATF_TP_ADD_TC(tp, fileops);
                   -  + ]
     527   [ +  -  -  +  :         11 :         ATF_TP_ADD_TC(tp, prefix_path);
                   -  + ]
     528                 :            : 
     529                 :         11 :         return (atf_no_error());
     530                 :         11 : }

Generated by: LCOV version 1.15