1 /*
2 * Test strace's -x option.
3 *
4 * Copyright (c) 2020-2021 The strace developers.
5 * All rights reserved.
6 *
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 */
9
10 #include "tests.h"
11
12 #include <stdio.h>
13 #include <unistd.h>
14
15 #ifndef STRACE_X
16 # define STRACE_X 1
17 #endif
18
19 #if STRACE_X == 1
20 # define XOUT(_, x_chars_, x_, xx_) x_
21 #elif STRACE_X == 2
22 # define XOUT(_, x_chars_, x_, xx_) xx_
23 #elif STRACE_X == 3
24 # define XOUT(_, x_chars_, x_, xx_) x_chars_
25 #elif STRACE_X == 0
26 # define XOUT(_, x_chars_, x_, xx_) _
27 #endif
28
29 int
30 main(void)
31 {
32 static const struct {
33 const char *path;
34 const char *out;
35 } test_vecs[] = {
36 { "test",
37 XOUT("test", "test", "test", "\\x74\\x65\\x73\\x74") },
38 { "\t\n\v\f\r hi~", XOUT("\\t\\n\\v\\f\\r hi~",
39 "\\t\\n\\v\\f\\r hi~", "\\t\\n\\v\\f\\r hi~",
40 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e") },
41 { "\t\n\v\f\r\16 hi~", XOUT("\\t\\n\\v\\f\\r\\16 hi~",
42 "\\t\\n\\v\\f\\r\\x0e hi~",
43 "\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x20\\x68\\x69\\x7e",
44 "\\x09\\x0a\\x0b\\x0c\\x0d\\x0e\\x20\\x68\\x69\\x7e") },
45 { "\10\t\n\v\f\r hi~", XOUT("\\10\\t\\n\\v\\f\\r hi~",
46 "\\x08\\t\\n\\v\\f\\r hi~",
47 "\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e",
48 "\\x08\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e") },
49 { "\t\n\v\f\r\37 hi~", XOUT("\\t\\n\\v\\f\\r\\37 hi~",
50 "\\t\\n\\v\\f\\r\\x1f hi~",
51 "\\x09\\x0a\\x0b\\x0c\\x0d\\x1f\\x20\\x68\\x69\\x7e",
52 "\\x09\\x0a\\x0b\\x0c\\x0d\\x1f\\x20\\x68\\x69\\x7e") },
53 { "\t\n\v\f\r hi~\177", XOUT("\\t\\n\\v\\f\\r hi~\\177",
54 "\\t\\n\\v\\f\\r hi~\\x7f",
55 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e\\x7f",
56 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e\\x7f") },
57 { "\t\n\v\f\r hi~\222", XOUT("\\t\\n\\v\\f\\r hi~\\222",
58 "\\t\\n\\v\\f\\r hi~\\x92",
59 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e\\x92",
60 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e\\x92") },
61 { "\t\n\v\f\r hi~\377", XOUT("\\t\\n\\v\\f\\r hi~\\377",
62 "\\t\\n\\v\\f\\r hi~\\xff",
63 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e\\xff",
64 "\\x09\\x0a\\x0b\\x0c\\x0d\\x20\\x68\\x69\\x7e\\xff") },
65 };
66 static char path[] = " ";
67
68 const char *rc_str;
69
70 for (size_t i = 0; i < ARRAY_SIZE(test_vecs); i++) {
71 rc_str = sprintrc(chdir(test_vecs[i].path));
72 printf("chdir(\"%s\") = %s\n", test_vecs[i].out, rc_str);
73 }
74
75 for (unsigned char c = 1; c < 255; c++) {
76 path[1] = c;
77 rc_str = sprintrc(chdir(path));
78
79 printf("chdir(");
80 #if STRACE_X == 2
81 print_quoted_hex(path, sizeof(path) - 1);
82 #else
83 # if STRACE_X != 0
84 if (((c < ' ') || (c >= 0x7f)) && (c != '\t') && (c != '\n') &&
85 (c != '\v') && (c != '\f') && (c != '\r'))
86 # if STRACE_X == 3
87 printf("\"%c\\x%02hhx\"", path[0], path[1]);
88 # else
89 print_quoted_hex(path, sizeof(path) - 1);
90 # endif
91 else
92 # endif
93 print_quoted_string(path);
94 #endif
95 printf(") = %s\n", rc_str);
96 }
97
98 puts("+++ exited with 0 +++");
99 return 0;
100 }