1 /*
2 * Test printpath/umovestr.
3 *
4 * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2017-2022 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 for (unsigned int i = 1; i < test_max; ++i) {
36 const unsigned int i_1 = i - 1;
37 memmove(eop - i, eop - i_1, i_1);
38 eop[-1] = "/.."[i_1 % 3];
39 if (chdir(eop - i))
40 perror_msg_and_fail("chdir");
41 printf("chdir(\"%s\") = 0\n", eop - i);
42 }
43 }
44
45 static void
46 test_efault(const unsigned int test_max)
47 {
48 char *p = tail_alloc(test_max);
49 const char *const efault = p + test_max;
50 memset(p, '/', test_max);
51
52 for (; p <= efault; ++p) {
53 if (p <= efault - PATH_MAX)
54 continue;
55 printf("chdir(%p) = %s\n", p, sprintrc(chdir(p)));
56 }
57 }
58
59 static void
60 test_enametoolong(void)
61 {
62 char *p = tail_alloc(PATH_MAX);
63 memset(p, '/', PATH_MAX);
64
65 printf("chdir(\"%.*s\"...) = %s\n",
66 PATH_MAX - 1, p, sprintrc(chdir(p)));
67 }
68
69 void
70 test_printpath(const unsigned int test_max)
71 {
72 /*
73 * /../..|
74 * /../.|.
75 * /../|..
76 * /..|/..
77 * /.|./..
78 * /|../..
79 * |/../..
80 */
81 const unsigned int page_size = get_page_size();
82 char *p = tail_alloc(test_max + page_size);
83 for (unsigned int i = 1; i < sizeof(long); ++i)
84 test_printpath_at(p + i, test_max);
85 for (unsigned int i = 0; i < sizeof(long); ++i)
86 test_printpath_at(p + page_size - i, test_max);
87 test_efault(test_max);
88 test_enametoolong();
89 }