Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-read-file.c
1 /*
2  * Copyright (C) 2006-2007 Free Software Foundation
3  * Written by Simon Josefsson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 #include <config.h>
21
22 #include "read-file.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <sys/stat.h>
27
28 #define FILE1 "/etc/resolv.conf"
29 #define FILE2 "/dev/null"
30
31 int
32 main (void)
33 {
34   struct stat statbuf;
35   int err = 0;
36
37   /* We can perform the test only if the file exists and is readable.
38      Test whether it exists, then assume it is world-readable.  */
39   if (stat (FILE1, &statbuf) >= 0)
40     {
41       size_t len;
42       char *out = read_file (FILE1, &len);
43
44       if (!out)
45         {
46           perror ("Could not read file");
47           err = 1;
48         }
49       else
50         {
51           if (out[len] != '\0')
52             {
53               perror ("BAD: out[len] not zero");
54               err = 1;
55             }
56
57           /* Assume FILE1 is a regular file or a symlink to a regular file.  */
58           if (len != statbuf.st_size)
59             {
60               fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE1);
61               err = 1;
62             }
63           free (out);
64         }
65     }
66
67   /* We can perform the test only if the file exists and is readable.
68      Test whether it exists, then assume it is world-readable.  */
69   if (stat (FILE2, &statbuf) >= 0)
70     {
71       size_t len;
72       char *out = read_file (FILE2, &len);
73
74       if (!out)
75         {
76           perror ("Could not read file");
77           err = 1;
78         }
79       else
80         {
81           if (out[len] != '\0')
82             {
83               perror ("BAD: out[len] not zero");
84               err = 1;
85             }
86
87           /* /dev/null should always be empty.  Ignore statbuf.st_size, since it
88              is not a regular file.  */
89           if (len != 0)
90             {
91               fprintf (stderr, "Read %ld from %s...\n", (unsigned long) len, FILE2);
92               err = 1;
93             }
94           free (out);
95         }
96     }
97
98   return err;
99 }