]> sourceware.org Git - glibc.git/blame - io/tst-file_change_detection.c
Add internal <file_change_detection.h> header file
[glibc.git] / io / tst-file_change_detection.c
CommitLineData
6c80c6e8
FW
1/* Test for <file_change_detection.c>.
2 Copyright (C) 2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19/* The header uses the internal __fileno symbol, which is not
20 available outside of libc (even to internal tests). */
21#define __fileno(fp) fileno (fp)
22
23#include <file_change_detection.h>
24
25#include <array_length.h>
26#include <stdlib.h>
27#include <support/check.h>
28#include <support/support.h>
29#include <support/temp_file.h>
30#include <support/test-driver.h>
31#include <support/xstdio.h>
32#include <support/xunistd.h>
33#include <unistd.h>
34
35static void
36all_same (struct file_change_detection *array, size_t length)
37{
38 for (size_t i = 0; i < length; ++i)
39 for (size_t j = 0; j < length; ++j)
40 {
41 if (test_verbose > 0)
42 printf ("info: comparing %zu and %zu\n", i, j);
43 TEST_VERIFY (file_is_unchanged (array + i, array + j));
44 }
45}
46
47static void
48all_different (struct file_change_detection *array, size_t length)
49{
50 for (size_t i = 0; i < length; ++i)
51 for (size_t j = 0; j < length; ++j)
52 {
53 if (i == j)
54 continue;
55 if (test_verbose > 0)
56 printf ("info: comparing %zu and %zu\n", i, j);
57 TEST_VERIFY (!file_is_unchanged (array + i, array + j));
58 }
59}
60
61static int
62do_test (void)
63{
64 /* Use a temporary directory with various paths. */
65 char *tempdir = support_create_temp_directory ("tst-file_change_detection-");
66
67 char *path_dangling = xasprintf ("%s/dangling", tempdir);
68 char *path_does_not_exist = xasprintf ("%s/does-not-exist", tempdir);
69 char *path_empty1 = xasprintf ("%s/empty1", tempdir);
70 char *path_empty2 = xasprintf ("%s/empty2", tempdir);
71 char *path_fifo = xasprintf ("%s/fifo", tempdir);
72 char *path_file1 = xasprintf ("%s/file1", tempdir);
73 char *path_file2 = xasprintf ("%s/file2", tempdir);
74 char *path_loop = xasprintf ("%s/loop", tempdir);
75 char *path_to_empty1 = xasprintf ("%s/to-empty1", tempdir);
76 char *path_to_file1 = xasprintf ("%s/to-file1", tempdir);
77
78 add_temp_file (path_dangling);
79 add_temp_file (path_empty1);
80 add_temp_file (path_empty2);
81 add_temp_file (path_fifo);
82 add_temp_file (path_file1);
83 add_temp_file (path_file2);
84 add_temp_file (path_loop);
85 add_temp_file (path_to_empty1);
86 add_temp_file (path_to_file1);
87
88 xsymlink ("target-does-not-exist", path_dangling);
89 support_write_file_string (path_empty1, "");
90 support_write_file_string (path_empty2, "");
91 TEST_COMPARE (mknod (path_fifo, 0777 | S_IFIFO, 0), 0);
92 support_write_file_string (path_file1, "line\n");
93 support_write_file_string (path_file2, "line\n");
94 xsymlink ("loop", path_loop);
95 xsymlink ("empty1", path_to_empty1);
96 xsymlink ("file1", path_to_file1);
97
98 FILE *fp_file1 = xfopen (path_file1, "r");
99 FILE *fp_file2 = xfopen (path_file2, "r");
100 FILE *fp_empty1 = xfopen (path_empty1, "r");
101 FILE *fp_empty2 = xfopen (path_empty2, "r");
102
103 /* Test for the same (empty) files. */
104 {
105 struct file_change_detection fcd[10];
106 int i = 0;
107 /* Two empty files always have the same contents. */
108 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_empty1));
109 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_empty2));
110 /* So does a missing file (which is treated as empty). */
111 TEST_VERIFY (file_change_detection_for_path (&fcd[i++],
112 path_does_not_exist));
113 /* And a symbolic link loop. */
114 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_loop));
115 /* And a dangling symbolic link. */
116 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_dangling));
117 /* And a directory. */
118 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], tempdir));
119 /* And a symbolic link to an empty file. */
120 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_to_empty1));
121 /* Likewise for access the file via a FILE *. */
122 TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], fp_empty1));
123 TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], fp_empty2));
124 /* And a NULL FILE * (missing file). */
125 TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], NULL));
126 TEST_COMPARE (i, array_length (fcd));
127
128 all_same (fcd, array_length (fcd));
129 }
130
131 /* Symbolic links are resolved. */
132 {
133 struct file_change_detection fcd[3];
134 int i = 0;
135 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_file1));
136 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_to_file1));
137 TEST_VERIFY (file_change_detection_for_fp (&fcd[i++], fp_file1));
138 TEST_COMPARE (i, array_length (fcd));
139 all_same (fcd, array_length (fcd));
140 }
141
142 /* Test for different files. */
143 {
144 struct file_change_detection fcd[5];
145 int i = 0;
146 /* The other files are not empty. */
147 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_empty1));
148 /* These two files have the same contents, but have different file
149 identity. */
150 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_file1));
151 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_file2));
152 /* FIFOs are always different, even with themselves. */
153 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_fifo));
154 TEST_VERIFY (file_change_detection_for_path (&fcd[i++], path_fifo));
155 TEST_COMPARE (i, array_length (fcd));
156 all_different (fcd, array_length (fcd));
157
158 /* Replacing the file with its symbolic link does not make a
159 difference. */
160 TEST_VERIFY (file_change_detection_for_path (&fcd[1], path_to_file1));
161 all_different (fcd, array_length (fcd));
162 }
163
164 /* Wait for a file change. Depending on file system time stamp
165 resolution, this subtest blocks for a while. */
166 for (int use_stdio = 0; use_stdio < 2; ++use_stdio)
167 {
168 struct file_change_detection initial;
169 TEST_VERIFY (file_change_detection_for_path (&initial, path_file1));
170 while (true)
171 {
172 support_write_file_string (path_file1, "line\n");
173 struct file_change_detection current;
174 if (use_stdio)
175 TEST_VERIFY (file_change_detection_for_fp (&current, fp_file1));
176 else
177 TEST_VERIFY (file_change_detection_for_path (&current, path_file1));
178 if (!file_is_unchanged (&initial, &current))
179 break;
180 /* Wait for a bit to reduce system load. */
181 usleep (100 * 1000);
182 }
183 }
184
185 fclose (fp_empty1);
186 fclose (fp_empty2);
187 fclose (fp_file1);
188 fclose (fp_file2);
189
190 free (path_dangling);
191 free (path_does_not_exist);
192 free (path_empty1);
193 free (path_empty2);
194 free (path_fifo);
195 free (path_file1);
196 free (path_file2);
197 free (path_loop);
198 free (path_to_empty1);
199 free (path_to_file1);
200
201 free (tempdir);
202
203 return 0;
204}
205
206#include <support/test-driver.c>
This page took 0.044614 seconds and 5 git commands to generate.