"Fossies" - the Fresh Open Source Software Archive 
Member "checkinstall-1.6.2/installwatch/test-installwatch.c" (11 Nov 2008, 6618 Bytes) of package /linux/privat/old/checkinstall-1.6.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "test-installwatch.c" see the
Fossies "Dox" file reference documentation.
1 /* $Id: test-installwatch.c,v 0.6.3.2 2001/12/14 00:06:05 izto Exp $ */
2 /*
3 * Copyright (C) 1998-99 Pancrazio `Ezio' de Mauro <p@demauro.net>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <time.h>
26 #include <dlfcn.h>
27
28 #include "localdecls.h"
29
30 #ifndef LIBDIR
31 #define LIBDIR "/usr/local/lib"
32 #endif
33
34 #define TESTFILE "/tmp/installwatch-test"
35 #define TESTFILE2 TESTFILE "2"
36
37 int *refcount;
38 int *timecount;
39 int passed, failed;
40 void* libc_handle=NULL;
41
42 void check_installwatch(void) {
43 char *error;
44
45 time(NULL);
46
47 libc_handle=dlopen(LIBDIR"/installwatch.so",RTLD_LAZY);
48 if(!libc_handle) {
49 puts("Unable to open "LIBDIR"/installwatch.so");
50 exit(255);
51 }
52
53 time(NULL);
54
55 timecount=(int*)dlsym(libc_handle,"__installwatch_timecount");
56 if ((error = dlerror()) != NULL) {
57 fputs(error, stderr);
58 exit(255);
59 }
60
61 if((*timecount)<2) {
62 puts("This program must be run with installwatch");
63 dlclose(libc_handle);
64 exit(255);
65 }
66
67 refcount=(int*)dlsym(libc_handle,"__installwatch_refcount");
68 if ((error = dlerror()) != NULL) {
69 fputs(error, stderr);
70 exit(255);
71 }
72 }
73
74 void test_chmod(void) {
75 int fd;
76
77 fd = creat(TESTFILE, 0600);
78 close(fd);
79 chmod(TESTFILE, 0600);
80 unlink(TESTFILE);
81 }
82
83 void test_chown(void) {
84 int fd;
85
86 fd = creat(TESTFILE, 0600);
87 close(fd);
88 chown(TESTFILE, geteuid(), getegid());
89 unlink(TESTFILE);
90 }
91
92 void test_chroot(void) {
93 chroot("/");
94 }
95
96 void test_creat(void) {
97 int fd;
98
99 fd = creat(TESTFILE, 0600);
100 close(fd);
101 unlink(TESTFILE);
102 }
103
104 void test_fchmod(void) {
105 int fd;
106
107 fd = creat(TESTFILE, 0600);
108 fchmod(fd, 0600);
109 close(fd);
110 unlink(TESTFILE);
111 }
112
113 void test_fchown(void) {
114 int fd;
115
116 fd = creat(TESTFILE, 0600);
117 fchown(fd, geteuid(), getegid());
118 close(fd);
119 unlink(TESTFILE);
120 }
121
122 void test_fopen(void) {
123 FILE *fd;
124
125 fd = fopen(TESTFILE,"w");
126 fclose(fd);
127 unlink(TESTFILE);
128 }
129
130 void test_ftruncate(void) {
131 int fd;
132
133 fd = creat(TESTFILE, 0600);
134 ftruncate(fd, 0);
135 close(fd);
136 unlink(TESTFILE);
137 }
138
139 void test_lchown(void) {
140 int fd;
141
142 fd = creat(TESTFILE, 0600);
143 close(fd);
144 lchown(TESTFILE, geteuid(), getegid());
145 unlink(TESTFILE);
146 }
147
148 void test_link(void) {
149 int fd;
150
151 fd = creat(TESTFILE, 0600);
152 close(fd);
153 link(TESTFILE, TESTFILE2);
154 unlink(TESTFILE);
155 unlink(TESTFILE2);
156 }
157
158 void test_mkdir(void) {
159 mkdir(TESTFILE, 0700);
160 rmdir(TESTFILE);
161 }
162
163 void test_open(void) {
164 int fd;
165
166 fd = open(TESTFILE, O_CREAT, O_RDWR, 0700);
167 close(fd);
168 unlink(TESTFILE);
169 }
170
171 void test_rename(void) {
172 int fd;
173
174 fd = creat(TESTFILE, 0700);
175 close(fd);
176 rename(TESTFILE, TESTFILE2);
177 unlink(TESTFILE2);
178 }
179
180 void test_symlink(void) {
181 int fd;
182
183 fd = creat(TESTFILE, 0700);
184 close(fd);
185 symlink(TESTFILE, TESTFILE2);
186 unlink(TESTFILE);
187 unlink(TESTFILE2);
188 }
189
190 void test_truncate(void) {
191 int fd;
192
193 fd = creat(TESTFILE, 0700);
194 close(fd);
195 truncate(TESTFILE, 0);
196 unlink(TESTFILE);
197 }
198
199 void test_unlink(void) {
200 int fd;
201
202 fd = creat(TESTFILE, 0700);
203 close(fd);
204 unlink(TESTFILE);
205 }
206
207 #if(GLIBC_MINOR >= 1)
208
209 void test_creat64(void) {
210 int fd;
211
212 fd = creat64(TESTFILE, 0600);
213 close(fd);
214 unlink(TESTFILE);
215 }
216
217 void test_fopen64(void) {
218 FILE *fd;
219
220 fd = fopen64(TESTFILE,"w");
221 fclose(fd);
222 unlink(TESTFILE);
223 }
224
225 void test_ftruncate64(void) {
226 int fd;
227
228 fd = creat64(TESTFILE, 0600);
229 ftruncate64(fd, 0);
230 close(fd);
231 unlink(TESTFILE);
232 }
233
234 void test_open64(void) {
235 int fd;
236
237 fd = open64(TESTFILE, O_CREAT, O_RDWR, 0700);
238 close(fd);
239 unlink(TESTFILE);
240 }
241
242 void test_truncate64(void) {
243 int fd;
244
245 fd = creat64(TESTFILE, 0700);
246 close(fd);
247 truncate64(TESTFILE, 0);
248 unlink(TESTFILE);
249 }
250
251 #endif
252
253 int do_test(const char *name, void (*function)(void), int increment) {
254 int old_refcount;
255
256 printf("Testing %s... ", name);
257 old_refcount = *refcount;
258 function();
259 if(*refcount == old_refcount + increment) {
260 printf("wanted refcount=%d returned refcount=%d",
261 (old_refcount+increment),*refcount);
262 puts("passed");
263 passed++;
264 return 0;
265 } else {
266 printf("wanted refcount=%d returned refcount=%d",
267 (old_refcount+increment),*refcount);
268 puts("failed");
269 failed++;
270 return 1;
271 }
272 }
273
274 int main(int argc, char **argv) {
275 struct stat statbuf;
276
277 check_installwatch();
278
279 if(stat(TESTFILE, &statbuf) != -1) {
280 printf(TESTFILE " already exists. Please remove it and run %s again\n", argv[0]);
281 exit(254);
282 }
283 if(stat(TESTFILE2, &statbuf) != -1) {
284 printf(TESTFILE2 " already exists. Please remove it and run %s again\n", argv[0]);
285 exit(254);
286 }
287 puts("Testing installwatch " VERSION);
288 puts("Using " TESTFILE " and " TESTFILE2 " as a test files\n");
289 passed = failed = 0;
290 do_test("chmod", test_chmod, 4);
291 do_test("chown", test_chown, 3);
292 do_test("chroot", test_chroot, 1);
293 do_test("creat", test_creat, 2);
294 #if(GLIBC_MINOR >= 1)
295 do_test("creat64", test_creat64, 2);
296 #endif
297 do_test("fchmod", test_fchmod, 3);
298 do_test("fchown", test_fchown, 3);
299 do_test("fopen",test_fopen,2);
300 #if(GLIBC_MINOR >= 1)
301 do_test("fopen64",test_fopen64,2);
302 #endif
303 do_test("ftruncate", test_ftruncate, 3);
304 #if(GLIBC_MINOR >= 1)
305 do_test("ftruncate64", test_ftruncate64, 3);
306 #endif
307 do_test("lchown", test_lchown, 3);
308 do_test("link", test_link, 4);
309 do_test("mkdir", test_mkdir, 2);
310 /* do_test("mknod", test_mknod, 2); */
311 do_test("open", test_open, 2);
312 #if(GLIBC_MINOR >= 1)
313 do_test("open64", test_open64, 2);
314 #endif
315 do_test("rename", test_rename, 3);
316 do_test("rmdir", test_mkdir, 2);
317 do_test("symlink", test_symlink, 4);
318 do_test("truncate", test_truncate, 3);
319 #if(GLIBC_MINOR >= 1)
320 do_test("truncate64", test_truncate64, 3);
321 #endif
322 do_test("unlink", test_unlink, 2);
323
324 putchar('\n');
325 if(failed != 0) {
326 printf("%d tests were not successful!\n", failed);
327 printf("Please email this log to the maintainer with the output of\n");
328 printf("\tnm %s\n", argv[0]);
329 } else
330 printf("All tests successful!\n");
331
332 if(libc_handle!=NULL)
333 dlclose(libc_handle);
334
335 return failed;
336 }
337