LCOV - code coverage report
Current view: top level - gltests - test-stat.h (source / functions) Hit Total Coverage
Test: GNU Libidn Lines: 46 50 92.0 %
Date: 2015-08-01 15:30:48 Functions: 1 1 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Tests of stat.
       2             :    Copyright (C) 2009-2015 Free Software Foundation, Inc.
       3             : 
       4             :    This program is free software: you can redistribute it and/or modify
       5             :    it under the terms of the GNU General Public License as published by
       6             :    the Free Software Foundation; either version 3 of the License, or
       7             :    (at your option) any later version.
       8             : 
       9             :    This program is distributed in the hope that it will be useful,
      10             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :    GNU General Public License for more details.
      13             : 
      14             :    You should have received a copy of the GNU General Public License
      15             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
      16             : 
      17             : /* Written by Eric Blake <ebb9@byu.net>, 2009.  */
      18             : 
      19             : /* This file is designed to test both stat(n,buf) and
      20             :    fstatat(AT_FDCWD,n,buf,0).  FUNC is the function to test.  Assumes
      21             :    that BASE and ASSERT are already defined, and that appropriate
      22             :    headers are already included.  If PRINT, warn before skipping
      23             :    symlink tests with status 77.  */
      24             : 
      25             : static int
      26           1 : test_stat_func (int (*func) (char const *, struct stat *), bool print)
      27             : {
      28             :   struct stat st1;
      29             :   struct stat st2;
      30           1 :   char *cwd = getcwd (NULL, 0);
      31             : 
      32           1 :   ASSERT (cwd);
      33           1 :   ASSERT (func (".", &st1) == 0);
      34           1 :   ASSERT (func ("./", &st2) == 0);
      35           1 :   ASSERT (SAME_INODE (st1, st2));
      36           1 :   ASSERT (func (cwd, &st2) == 0);
      37           1 :   ASSERT (SAME_INODE (st1, st2));
      38           1 :   ASSERT (func ("/", &st1) == 0);
      39           1 :   ASSERT (func ("///", &st2) == 0);
      40           1 :   ASSERT (SAME_INODE (st1, st2));
      41             : 
      42           1 :   errno = 0;
      43           1 :   ASSERT (func ("", &st1) == -1);
      44           1 :   ASSERT (errno == ENOENT);
      45           1 :   errno = 0;
      46           1 :   ASSERT (func ("nosuch", &st1) == -1);
      47           1 :   ASSERT (errno == ENOENT);
      48           1 :   errno = 0;
      49           1 :   ASSERT (func ("nosuch/", &st1) == -1);
      50           1 :   ASSERT (errno == ENOENT);
      51             : 
      52           1 :   ASSERT (close (creat (BASE "file", 0600)) == 0);
      53           1 :   ASSERT (func (BASE "file", &st1) == 0);
      54           1 :   errno = 0;
      55           1 :   ASSERT (func (BASE "file/", &st1) == -1);
      56           1 :   ASSERT (errno == ENOTDIR);
      57             : 
      58             :   /* Now for some symlink tests, where supported.  We set up:
      59             :      link1 -> directory
      60             :      link2 -> file
      61             :      link3 -> dangling
      62             :      link4 -> loop
      63             :      then test behavior with trailing slash.
      64             :   */
      65           1 :   if (symlink (".", BASE "link1") != 0)
      66             :     {
      67           0 :       ASSERT (unlink (BASE "file") == 0);
      68           0 :       if (print)
      69           0 :         fputs ("skipping test: symlinks not supported on this file system\n",
      70             :                stderr);
      71           0 :       return 77;
      72             :     }
      73           1 :   ASSERT (symlink (BASE "file", BASE "link2") == 0);
      74           1 :   ASSERT (symlink (BASE "nosuch", BASE "link3") == 0);
      75           1 :   ASSERT (symlink (BASE "link4", BASE "link4") == 0);
      76             : 
      77           1 :   ASSERT (func (BASE "link1/", &st1) == 0);
      78           1 :   ASSERT (S_ISDIR (st1.st_mode));
      79             : 
      80           1 :   errno = 0;
      81           1 :   ASSERT (func (BASE "link2/", &st1) == -1);
      82           1 :   ASSERT (errno == ENOTDIR);
      83             : 
      84           1 :   errno = 0;
      85           1 :   ASSERT (func (BASE "link3/", &st1) == -1);
      86           1 :   ASSERT (errno == ENOENT);
      87             : 
      88           1 :   errno = 0;
      89           1 :   ASSERT (func (BASE "link4/", &st1) == -1);
      90           1 :   ASSERT (errno == ELOOP);
      91             : 
      92             :   /* Cleanup.  */
      93           1 :   ASSERT (unlink (BASE "file") == 0);
      94           1 :   ASSERT (unlink (BASE "link1") == 0);
      95           1 :   ASSERT (unlink (BASE "link2") == 0);
      96           1 :   ASSERT (unlink (BASE "link3") == 0);
      97           1 :   ASSERT (unlink (BASE "link4") == 0);
      98             : 
      99           1 :   return 0;
     100             : }

Generated by: LCOV version 1.11