]> granicus.if.org Git - strace/blob - tests/readdir.c
tests: make test executables invocable outside current work directory
[strace] / tests / readdir.c
1 /*
2  * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "tests.h"
29 #include <asm/unistd.h>
30
31 #ifdef __NR_readdir
32
33 # include <assert.h>
34 # include <dirent.h>
35 # include <fcntl.h>
36 # include <stdio.h>
37 # include <string.h>
38 # include <sys/stat.h>
39 # include <unistd.h>
40
41 static const char fname[] =
42         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
43         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
44         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
45         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
46         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
47         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
48         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\n"
49         "A\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nA\nZ";
50 static const char qname[] =
51         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
52         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
53         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
54         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
55         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
56         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
57         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n"
58         "A\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nZ";
59
60 int
61 main(void)
62 {
63         static const char dname[] = "readdir.test.tmp.dir";
64         struct {
65                 unsigned long   d_ino;
66                 unsigned long   d_off;
67                 unsigned short  d_reclen;
68                 char d_name[1024];
69         } e;
70         int rc;
71
72         assert(!mkdir(dname, 0700));
73         assert(!chdir(dname));
74         (void) close(0);
75         assert(!creat(fname, 0600));
76         assert(!close(0));
77         assert(!open(".", O_RDONLY | O_DIRECTORY));
78         while ((rc = syscall(__NR_readdir, 0, &e, 1))) {
79                 if (rc < 0)
80                         perror_msg_and_skip("readdir");
81                 e.d_name[e.d_reclen] = '\0';
82                 printf("readdir(0, {d_ino=%lu, d_off=%lu, d_reclen=%u"
83                        ", d_name=\"%s\"}) = %d\n",
84                        e.d_ino, e.d_off, e.d_reclen,
85                        e.d_name[0] == '.' ? e.d_name : qname, rc);
86         }
87         printf("readdir(0, %p) = 0\n", &e);
88         puts("+++ exited with 0 +++");
89         assert(!unlink(fname));
90         assert(!chdir(".."));
91         assert(!rmdir(dname));
92
93         return 0;
94 }
95
96 #else
97
98 SKIP_MAIN_UNDEFINED("__NR_readdir")
99
100 #endif