1 /*
2 * Test printstrn/umoven.
3 *
4 * Copyright (c) 2015-2017 Dmitry V. Levin <ldv@strace.io>
5 * Copyright (c) 2017-2021 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 <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16
17 #include "scno.h"
18 #include "test_ucopy.h"
19
20 static const char *errstr;
21
22 static void add_key(const char *addr, const unsigned int len)
23 {
24 errstr = sprintrc(syscall(__NR_add_key, 0, 0, addr, len, -1));
25 }
26
27 static void
28 test_printstrn_at(char *const p, const unsigned int test_max)
29 {
30 for (unsigned int i = 0; i <= test_max; ++i) {
31 add_key(p + (test_max - i), i);
32 printf("add_key(NULL, NULL, \"%.*s\", %u"
33 ", KEY_SPEC_THREAD_KEYRING) = %s\n",
34 (int) i, p + (test_max - i), i, errstr);
35 }
36 }
37
38 static void
39 test_efault(const unsigned int test_max)
40 {
41 char *p = tail_alloc(test_max);
42 memset(p, '/', test_max);
43
44 for (unsigned int i = 0; i <= test_max; ++i) {
45 for (unsigned int j = 1; j <= sizeof(long); ++j) {
46 add_key(p + (test_max - i), i + j);
47 printf("add_key(NULL, NULL, %p, %u"
48 ", KEY_SPEC_THREAD_KEYRING) = %s\n",
49 p + (test_max - i), i + j, errstr);
50 }
51 }
52 }
53
54 static void
55 test_print_memory(char *const p, const unsigned int test_max)
56 {
57 add_key(p, test_max);
58 printf("add_key(NULL, NULL, ");
59 print_quoted_memory(p, test_max);
60 printf(", %u, KEY_SPEC_THREAD_KEYRING) = %s\n", test_max, errstr);
61 }
62
63 void
64 test_printstrn(const unsigned int test_max)
65 {
66 /*
67 * abcdefgh|
68 * abcdefg|h
69 * abcdef|gh
70 * abcde|fgh
71 * abcd|efgh
72 * abc|defgh
73 * ab|cdefgh
74 * a|bcdefgh
75 * |abcdefgh
76 */
77 const unsigned int page_size = get_page_size();
78 char *p = tail_alloc(test_max + page_size);
79 fill_memory_ex(p, test_max + page_size, 'a', 'z' - 'a' + 1);
80
81 for (unsigned int i = 1; i <= sizeof(long); ++i)
82 test_printstrn_at(p + i, test_max);
83 for (unsigned int i = 0; i < sizeof(long); ++i)
84 test_printstrn_at(p + page_size - i, test_max);
85 test_efault(test_max);
86
87 fill_memory_ex(p, test_max + page_size, 0x00, 0xFF);
88 /* Test corner cases when octal quoting goes before digit */
89 for (unsigned int i = 0; i < 11; ++i)
90 p[2 + 3 * i] = '0' + i - 1;
91
92 test_print_memory(p, test_max);
93 }