]> granicus.if.org Git - strace/blob - tests/test_printpath.c
tests: change the license to GPL-2.0-or-later
[strace] / tests / test_printpath.c
1 /*
2  * Test printpath/umovestr.
3  *
4  * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2017-2018 The strace developers.
6  * All rights reserved.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "tests.h"
12
13 #include <limits.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "test_ucopy.h"
19
20 static void
21 test_printpath_at(char *const p, const unsigned int test_max)
22 {
23         /*
24          *       /
25          *      /.
26          *     /..
27          *    /...
28          *   /../.
29          *  /../..
30          * /../../
31          */
32
33         char *const eop = p + (test_max - 1);
34         *eop = '\0';
35         unsigned int i;
36         for (i = 1; i < test_max; ++i) {
37                 const unsigned int i_1 = i - 1;
38                 memmove(eop - i, eop - i_1, i_1);
39                 eop[-1] = "/.."[i_1 % 3];
40                 if (chdir(eop - i))
41                         perror_msg_and_fail("chdir");
42                 printf("chdir(\"%s\") = 0\n", eop - i);
43         }
44 }
45
46 static void
47 test_efault(const unsigned int test_max)
48 {
49         char *p = tail_alloc(test_max);
50         const char *const efault = p + test_max;
51         memset(p, '/', test_max);
52
53         for (; p <= efault; ++p) {
54                 if (p <= efault - PATH_MAX)
55                         continue;
56                 printf("chdir(%p) = %s\n", p, sprintrc(chdir(p)));
57         }
58 }
59
60 static void
61 test_enametoolong(void)
62 {
63         char *p = tail_alloc(PATH_MAX);
64         memset(p, '/', PATH_MAX);
65
66         printf("chdir(\"%.*s\"...) = %s\n",
67                PATH_MAX - 1, p, sprintrc(chdir(p)));
68 }
69
70 void
71 test_printpath(const unsigned int test_max)
72 {
73         /*
74          * /../..|
75          * /../.|.
76          * /../|..
77          * /..|/..
78          * /.|./..
79          * /|../..
80          * |/../..
81          */
82         const unsigned int page_size = get_page_size();
83         char *p = tail_alloc(test_max + page_size);
84         unsigned int i;
85         for (i = 1; i < sizeof(long); ++i)
86                 test_printpath_at(p + i, test_max);
87         for (i = 0; i < sizeof(long); ++i)
88                 test_printpath_at(p + page_size - i, test_max);
89         test_efault(test_max);
90         test_enametoolong();
91 }