]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <stdio.h> | |
4 | ||
5 | #include "strxcpyx.h" | |
6 | #include "tests.h" | |
7 | ||
8 | TEST(strpcpy) { | |
9 | char target[25]; | |
10 | char *s = target; | |
11 | size_t space_left; | |
12 | bool truncated; | |
13 | ||
14 | space_left = sizeof(target); | |
15 | space_left = strpcpy_full(&s, space_left, "12345", &truncated); | |
16 | assert_se(!truncated); | |
17 | space_left = strpcpy_full(&s, space_left, "hey hey hey", &truncated); | |
18 | assert_se(!truncated); | |
19 | space_left = strpcpy_full(&s, space_left, "waldo", &truncated); | |
20 | assert_se(!truncated); | |
21 | space_left = strpcpy_full(&s, space_left, "ba", &truncated); | |
22 | assert_se(!truncated); | |
23 | space_left = strpcpy_full(&s, space_left, "r", &truncated); | |
24 | assert_se(!truncated); | |
25 | assert_se(space_left == 1); | |
26 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); | |
27 | ||
28 | space_left = strpcpy_full(&s, space_left, "", &truncated); | |
29 | assert_se(!truncated); | |
30 | assert_se(space_left == 1); | |
31 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); | |
32 | ||
33 | space_left = strpcpy_full(&s, space_left, "f", &truncated); | |
34 | assert_se(truncated); | |
35 | assert_se(space_left == 0); | |
36 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); | |
37 | ||
38 | space_left = strpcpy_full(&s, space_left, "", &truncated); | |
39 | assert_se(!truncated); | |
40 | assert_se(space_left == 0); | |
41 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); | |
42 | ||
43 | space_left = strpcpy_full(&s, space_left, "foo", &truncated); | |
44 | assert_se(truncated); | |
45 | assert_se(space_left == 0); | |
46 | ASSERT_STREQ(target, "12345hey hey heywaldobar"); | |
47 | } | |
48 | ||
49 | TEST(strpcpyf) { | |
50 | char target[25]; | |
51 | char *s = target; | |
52 | size_t space_left; | |
53 | bool truncated; | |
54 | ||
55 | space_left = sizeof(target); | |
56 | space_left = strpcpyf_full(&s, space_left, &truncated, "space left: %zu. ", space_left); | |
57 | assert_se(!truncated); | |
58 | space_left = strpcpyf_full(&s, space_left, &truncated, "foo%s", "bar"); | |
59 | assert_se(!truncated); | |
60 | assert_se(space_left == 3); | |
61 | ASSERT_STREQ(target, "space left: 25. foobar"); | |
62 | ||
63 | space_left = strpcpyf_full(&s, space_left, &truncated, "%i", 42); | |
64 | assert_se(!truncated); | |
65 | assert_se(space_left == 1); | |
66 | ASSERT_STREQ(target, "space left: 25. foobar42"); | |
67 | ||
68 | space_left = strpcpyf_full(&s, space_left, &truncated, "%s", ""); | |
69 | assert_se(!truncated); | |
70 | assert_se(space_left == 1); | |
71 | ASSERT_STREQ(target, "space left: 25. foobar42"); | |
72 | ||
73 | space_left = strpcpyf_full(&s, space_left, &truncated, "%c", 'x'); | |
74 | assert_se(truncated); | |
75 | assert_se(space_left == 0); | |
76 | ASSERT_STREQ(target, "space left: 25. foobar42"); | |
77 | ||
78 | space_left = strpcpyf_full(&s, space_left, &truncated, "%s", ""); | |
79 | assert_se(!truncated); | |
80 | assert_se(space_left == 0); | |
81 | ASSERT_STREQ(target, "space left: 25. foobar42"); | |
82 | ||
83 | space_left = strpcpyf_full(&s, space_left, &truncated, "abc%s", "hoge"); | |
84 | assert_se(truncated); | |
85 | assert_se(space_left == 0); | |
86 | ASSERT_STREQ(target, "space left: 25. foobar42"); | |
87 | ||
88 | /* test overflow */ | |
89 | s = target; | |
90 | space_left = strpcpyf_full(&s, 12, &truncated, "00 left: %i. ", 999); | |
91 | assert_se(truncated); | |
92 | ASSERT_STREQ(target, "00 left: 99"); | |
93 | assert_se(space_left == 0); | |
94 | assert_se(target[12] == '2'); | |
95 | } | |
96 | ||
97 | TEST(strpcpyl) { | |
98 | char target[25]; | |
99 | char *s = target; | |
100 | size_t space_left; | |
101 | bool truncated; | |
102 | ||
103 | space_left = sizeof(target); | |
104 | space_left = strpcpyl_full(&s, space_left, &truncated, "waldo", " test", " waldo. ", NULL); | |
105 | assert_se(!truncated); | |
106 | space_left = strpcpyl_full(&s, space_left, &truncated, "Banana", NULL); | |
107 | assert_se(!truncated); | |
108 | assert_se(space_left == 1); | |
109 | ASSERT_STREQ(target, "waldo test waldo. Banana"); | |
110 | ||
111 | space_left = strpcpyl_full(&s, space_left, &truncated, "", "", "", NULL); | |
112 | assert_se(!truncated); | |
113 | assert_se(space_left == 1); | |
114 | ASSERT_STREQ(target, "waldo test waldo. Banana"); | |
115 | ||
116 | space_left = strpcpyl_full(&s, space_left, &truncated, "", "x", "", NULL); | |
117 | assert_se(truncated); | |
118 | assert_se(space_left == 0); | |
119 | ASSERT_STREQ(target, "waldo test waldo. Banana"); | |
120 | ||
121 | space_left = strpcpyl_full(&s, space_left, &truncated, "hoge", NULL); | |
122 | assert_se(truncated); | |
123 | assert_se(space_left == 0); | |
124 | ASSERT_STREQ(target, "waldo test waldo. Banana"); | |
125 | } | |
126 | ||
127 | TEST(strscpy) { | |
128 | char target[25]; | |
129 | size_t space_left; | |
130 | bool truncated; | |
131 | ||
132 | space_left = sizeof(target); | |
133 | space_left = strscpy_full(target, space_left, "12345", &truncated); | |
134 | assert_se(!truncated); | |
135 | ||
136 | ASSERT_STREQ(target, "12345"); | |
137 | assert_se(space_left == 20); | |
138 | } | |
139 | ||
140 | TEST(strscpyl) { | |
141 | char target[25]; | |
142 | size_t space_left; | |
143 | bool truncated; | |
144 | ||
145 | space_left = sizeof(target); | |
146 | space_left = strscpyl_full(target, space_left, &truncated, "12345", "waldo", "waldo", NULL); | |
147 | assert_se(!truncated); | |
148 | ||
149 | ASSERT_STREQ(target, "12345waldowaldo"); | |
150 | assert_se(space_left == 10); | |
151 | } | |
152 | ||
153 | TEST(sd_event_code_migration) { | |
154 | char b[100 * DECIMAL_STR_MAX(unsigned) + 1]; | |
155 | char c[100 * DECIMAL_STR_MAX(unsigned) + 1], *p; | |
156 | unsigned i; | |
157 | size_t l; | |
158 | int o, r; | |
159 | ||
160 | for (i = o = 0; i < 100; i++) { | |
161 | r = snprintf(&b[o], sizeof(b) - o, "%u ", i); | |
162 | assert_se(r >= 0 && r < (int) sizeof(b) - o); | |
163 | o += r; | |
164 | } | |
165 | ||
166 | p = c; | |
167 | l = sizeof(c); | |
168 | for (i = 0; i < 100; i++) | |
169 | l = strpcpyf(&p, l, "%u ", i); | |
170 | ||
171 | ASSERT_STREQ(b, c); | |
172 | } | |
173 | ||
174 | DEFINE_TEST_MAIN(LOG_INFO); |