This source file includes following definitions.
- test_read_file
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 #include <config.h>
19
20 #include "read-file.h"
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <sys/stat.h>
25
26 #include "macros.h"
27
28 #define FILE1 "/etc/resolv.conf"
29 #define FILE2 "/dev/null"
30
31 static int
32 test_read_file (int flags)
33 {
34 struct stat statbuf;
35 int err = 0;
36
37
38
39 if (stat (FILE1, &statbuf) >= 0)
40 {
41 size_t len;
42 char *out = read_file (FILE1, flags, &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 if (S_ISREG (statbuf.st_mode))
58 {
59
60 if (len != statbuf.st_size)
61 {
62 fprintf (stderr, "Read %lu from %s...\n",
63 (unsigned long) len, FILE1);
64 err = 1;
65 }
66 }
67 else
68 {
69
70 if (len == 0)
71 {
72 fprintf (stderr, "Read nothing from %s\n", FILE1);
73 err = 1;
74 }
75 }
76 free (out);
77 }
78 }
79
80
81
82 if (stat (FILE2, &statbuf) >= 0)
83 {
84 size_t len;
85 char *out = read_file (FILE2, flags, &len);
86
87 if (!out)
88 {
89 perror ("Could not read file");
90 err = 1;
91 }
92 else
93 {
94 if (out[len] != '\0')
95 {
96 perror ("BAD: out[len] not zero");
97 err = 1;
98 }
99
100
101
102 if (len != 0)
103 {
104 fprintf (stderr, "Read %lu from %s...\n",
105 (unsigned long) len, FILE2);
106 err = 1;
107 }
108 free (out);
109 }
110 }
111
112 return err;
113 }
114
115 int
116 main (void)
117 {
118 ASSERT (!test_read_file (0));
119 ASSERT (!test_read_file (RF_BINARY));
120 ASSERT (!test_read_file (RF_SENSITIVE));
121 ASSERT (!test_read_file (RF_BINARY | RF_SENSITIVE));
122
123 return 0;
124 }