]> granicus.if.org Git - strace/blob - tests/test_printpath.c
Include <limits.h> instead of <sys/param.h>
[strace] / tests / test_printpath.c
1 /*
2  * Test printpath/umovestr.
3  *
4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "tests.h"
31
32 #include <limits.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include "test_ucopy.h"
38
39 static void
40 test_printpath_at(char *const p, const unsigned int test_max)
41 {
42         /*
43          *       /
44          *      /.
45          *     /..
46          *    /...
47          *   /../.
48          *  /../..
49          * /../../
50          */
51
52         char *const eop = p + (test_max - 1);
53         *eop = '\0';
54         unsigned int i;
55         for (i = 1; i < test_max; ++i) {
56                 const unsigned int i_1 = i - 1;
57                 memmove(eop - i, eop - i_1, i_1);
58                 eop[-1] = "/.."[i_1 % 3];
59                 if (chdir(eop - i))
60                         perror_msg_and_fail("chdir");
61                 printf("chdir(\"%s\") = 0\n", eop - i);
62         }
63 }
64
65 static void
66 test_efault(const unsigned int test_max)
67 {
68         char *p = tail_alloc(test_max);
69         const char *const efault = p + test_max;
70         memset(p, '/', test_max);
71
72         for (; p <= efault; ++p) {
73                 if (p <= efault - PATH_MAX)
74                         continue;
75                 printf("chdir(%p) = %s\n", p, sprintrc(chdir(p)));
76         }
77 }
78
79 static void
80 test_enametoolong(void)
81 {
82         char *p = tail_alloc(PATH_MAX);
83         memset(p, '/', PATH_MAX);
84
85         printf("chdir(\"%.*s\"...) = %s\n",
86                PATH_MAX - 1, p, sprintrc(chdir(p)));
87 }
88
89 void
90 test_printpath(const unsigned int test_max)
91 {
92         /*
93          * /../..|
94          * /../.|.
95          * /../|..
96          * /..|/..
97          * /.|./..
98          * /|../..
99          * |/../..
100          */
101         const unsigned int page_size = get_page_size();
102         char *p = tail_alloc(test_max + page_size);
103         unsigned int i;
104         for (i = 1; i < sizeof(long); ++i)
105                 test_printpath_at(p + i, test_max);
106         for (i = 0; i < sizeof(long); ++i)
107                 test_printpath_at(p + page_size - i, test_max);
108         test_efault(test_max);
109         test_enametoolong();
110 }