1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <stdio.h>
16 #include <string.h>
17 #include "test.h"
18
test_strcpy_basic()19 void test_strcpy_basic() {
20 char dest[10];
21 strcpy(dest, "abc");
22 if (strcmp(dest, "abc")) {
23 t_error("Expected 'abc', got '%s'\n", dest);
24 }
25 }
26
test_strcpy_empty()27 void test_strcpy_empty() {
28 char dest[10] = "xxxxxx";
29 strcpy(dest, "");
30 if (strcmp(dest, "")) {
31 t_error("Expected empty string, got '%s'\n", dest);
32 }
33 }
34
test_strcpy_longer_string()35 void test_strcpy_longer_string() {
36 char dest[100];
37 strcpy(dest, "This is a longer string with multiple words.");
38 if (strcmp(dest, "This is a longer string with multiple words.")) {
39 t_error("Long string copy failed\n");
40 }
41 }
42
test_strcpy_special_characters()43 void test_strcpy_special_characters() {
44 char dest[20];
45 strcpy(dest, "@#$%^&*()");
46 if (strcmp(dest, "@#$%^&*()")) {
47 t_error("Special characters copy failed\n");
48 }
49 }
50
test_strcpy_single_char()51 void test_strcpy_single_char() {
52 char dest[10];
53 strcpy(dest, "Z");
54 if (strcmp(dest, "Z")) {
55 t_error("Single char copy failed\n");
56 }
57 }
58
test_strcpy_case_mixed()59 void test_strcpy_case_mixed() {
60 char dest[20];
61 strcpy(dest, "AbCdEfGh");
62 if (strcmp(dest, "AbCdEfGh")) {
63 t_error("Mixed case copy failed\n");
64 }
65 }
66
test_strcpy_with_spaces()67 void test_strcpy_with_spaces() {
68 char dest[20];
69 strcpy(dest, " ");
70 if (strcmp(dest, " ")) {
71 t_error("Spaces copy failed\n");
72 }
73 }
74
test_strcpy_with_escapes()75 void test_strcpy_with_escapes() {
76 char dest[20];
77 strcpy(dest, "Line1\nLine2");
78 if (strcmp(dest, "Line1\nLine2")) {
79 t_error("Escapes copy failed\n");
80 }
81 }
82
test_strcpy_overwrite_old_content()83 void test_strcpy_overwrite_old_content() {
84 char dest[20] = "OldContentHere";
85 strcpy(dest, "New");
86 if (strcmp(dest, "New")) {
87 t_error("Overwrite failed\n");
88 }
89 }
90
test_strcpy_with_embedded_null()91 void test_strcpy_with_embedded_null() {
92 char src[10] = {'a', 'b', '\0', 'c'};
93 char dest[10];
94 strcpy(dest, src);
95 if (dest[0] != 'a' || dest[1] != 'b' || dest[2] != '\0') {
96 t_error("Embedded null copy failed\n");
97 }
98 }
99
test_strcpy_modify_src_after_copy()100 void test_strcpy_modify_src_after_copy() {
101 char src[] = "copyme";
102 char dest[20];
103 strcpy(dest, src);
104 src[0] = 'X';
105 if (strcmp(dest, "copyme")) {
106 t_error("Source modification affected destination\n");
107 }
108 }
109
test_strcpy_utf8()110 void test_strcpy_utf8() {
111 char dest[20];
112 strcpy(dest, "café");
113 if (strcmp(dest, "café")) {
114 t_error("UTF-8 copy failed\n");
115 }
116 }
117
test_strcpy_multiple_assignments()118 void test_strcpy_multiple_assignments() {
119 char dest[20];
120 strcpy(dest, "first");
121 if (strcmp(dest, "first")) {
122 t_error("First assignment failed\n");
123 }
124 strcpy(dest, "second");
125 if (strcmp(dest, "second")) {
126 t_error("Second assignment failed\n");
127 }
128 }
129
test_strcpy_to_offset()130 void test_strcpy_to_offset() {
131 char buffer[20] = "1234567890";
132 strcpy(buffer + 5, "abc");
133 if (strcmp(buffer + 5, "abc")) {
134 t_error("Offset copy failed\n");
135 }
136 }
137
test_strcpy_array_vs_pointer()138 void test_strcpy_array_vs_pointer() {
139 char a[10];
140 char *b = a;
141 strcpy(b, "test");
142 if (strcmp(a, "test")) {
143 t_error("Array vs pointer copy failed\n");
144 }
145 }
146
test_strcpy_all_caps()147 void test_strcpy_all_caps() {
148 char dest[30];
149 strcpy(dest, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
150 if (strcmp(dest, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
151 t_error("All caps copy failed\n");
152 }
153 }
154
test_strcpy_sentence()155 void test_strcpy_sentence() {
156 char dest[100];
157 strcpy(dest, "The quick brown fox jumps over the lazy dog.");
158 if (strcmp(dest, "The quick brown fox jumps over the lazy dog.")) {
159 t_error("Sentence copy failed\n");
160 }
161 }
162
test_strcpy_series()163 void test_strcpy_series() {
164 char dest[50];
165 strcpy(dest, "one");
166 if (strcmp(dest, "one")) {
167 t_error("First in series failed\n");
168 }
169 strcpy(dest, "two");
170 if (strcmp(dest, "two")) {
171 t_error("Second in series failed\n");
172 }
173 strcpy(dest, "three");
174 if (strcmp(dest, "three")) {
175 t_error("Third in series failed\n");
176 }
177 }
178
test_strcpy_manual_src_array()179 void test_strcpy_manual_src_array() {
180 char src[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
181 char dest[10];
182 strcpy(dest, src);
183 if (strcmp(dest, "hello")) {
184 t_error("Manual array copy failed\n");
185 }
186 }
187
test_strcpy_vs_memcpy()188 void test_strcpy_vs_memcpy() {
189 char dest1[10];
190 char dest2[10];
191 char *src = "abc";
192 strcpy(dest1, src);
193 memcpy(dest2, src, strlen(src) + 1);
194 if (strcmp(dest1, dest2)) {
195 t_error("strcpy vs memcpy comparison failed\n");
196 }
197 }
198
test_strcpy_batch_group_1()199 void test_strcpy_batch_group_1() {
200 char dest[100];
201 strcpy(dest, "batch1");
202 if (strcmp(dest, "batch1")) {
203 t_error("Batch1 first copy failed\n");
204 }
205 strcpy(dest, "batch2");
206 if (strcmp(dest, "batch2")) {
207 t_error("Batch1 second copy failed\n");
208 }
209 strcpy(dest, "batch3");
210 if (strcmp(dest, "batch3")) {
211 t_error("Batch1 third copy failed\n");
212 }
213 }
214
test_strcpy_batch_group_2()215 void test_strcpy_batch_group_2() {
216 char dest[100];
217 strcpy(dest, "hello world");
218 if (strcmp(dest, "hello world")) {
219 t_error("Batch2 first copy failed\n");
220 }
221 strcpy(dest, "C programming");
222 if (strcmp(dest, "C programming")) {
223 t_error("Batch2 second copy failed\n");
224 }
225 strcpy(dest, "libc test");
226 if (strcmp(dest, "libc test")) {
227 t_error("Batch2 third copy failed\n");
228 }
229 }
230
test_strcpy_control_chars()231 void test_strcpy_control_chars() {
232 char dest[20];
233 strcpy(dest, "\x01\x02\x03\x04");
234 if (memcmp(dest, "\x01\x02\x03\x04", 4)) {
235 t_error("Control chars copy failed\n");
236 }
237 }
238
test_strcpy_long_strings()239 void test_strcpy_long_strings() {
240 char long_str[256];
241 memset(long_str, 'a', 255);
242 long_str[255] = '\0';
243 char dest[256];
244 strcpy(dest, long_str);
245 if (strcmp(dest, long_str)) {
246 t_error("Long string copy failed\n");
247 }
248 }
249
test_strcpy_non_ascii()250 void test_strcpy_non_ascii() {
251 char dest[20];
252 strcpy(dest, "éñüß");
253 if (strcmp(dest, "éñüß")) {
254 t_error("Non-ASCII copy failed\n");
255 }
256 }
257
test_strcpy_hex_escape()258 void test_strcpy_hex_escape() {
259 char dest[20];
260 strcpy(dest, "\x41\x42\x43");
261 if (strcmp(dest, "ABC")) {
262 t_error("Hex escape copy failed\n");
263 }
264 }
265
test_strcpy_octal_escape()266 void test_strcpy_octal_escape() {
267 char dest[20];
268 strcpy(dest, "\101\102\103");
269 if (strcmp(dest, "ABC")) {
270 t_error("Octal escape copy failed\n");
271 }
272 }
273
test_strcpy_partial_overwrite()274 void test_strcpy_partial_overwrite() {
275 char dest[20] = "abcdefghij";
276 strcpy(dest + 3, "123");
277 if (!strcmp(dest, "abc123ghij")) {
278 t_error("Partial overwrite check failed\n");
279 }
280 }
281
test_strcpy_pointer_arithmetic()282 void test_strcpy_pointer_arithmetic() {
283 char src[] = "abcdef";
284 char dest[10];
285 strcpy(dest, src + 2);
286 if (strcmp(dest, "cdef")) {
287 t_error("Pointer arithmetic copy failed\n");
288 }
289 }
290
test_strcpy_large_buffer()291 void test_strcpy_large_buffer() {
292 char dest[1024];
293 strcpy(dest, "This is a large buffer test");
294 if (strcmp(dest, "This is a large buffer test")) {
295 t_error("Large buffer copy failed\n");
296 }
297 }
298
test_strcpy_null_termination()299 void test_strcpy_null_termination() {
300 char dest[10];
301 strcpy(dest, "12345");
302 if (dest[5] != '\0') {
303 t_error("Null termination failed\n");
304 }
305 }
306
test_strcpy_special_whitespace()307 void test_strcpy_special_whitespace() {
308 char dest[20];
309 strcpy(dest, "\t\n\v\f\r");
310 if (strcmp(dest, "\t\n\v\f\r")) {
311 t_error("Special whitespace copy failed\n");
312 }
313 }
314
test_strcpy_alternating_chars()315 void test_strcpy_alternating_chars() {
316 char dest[20];
317 strcpy(dest, "a\0b\0c");
318 if (strcmp(dest, "a")) {
319 t_error("Alternating chars copy failed\n");
320 }
321 }
322
test_strcpy_all_null()323 void test_strcpy_all_null() {
324 char dest[10];
325 strcpy(dest, "\0\0\0");
326 if (strcmp(dest, "")) {
327 t_error("All null copy failed\n");
328 }
329 }
330
test_strcpy_very_long()331 void test_strcpy_very_long() {
332 char very_long[2048];
333 memset(very_long, 'x', 2047);
334 very_long[2047] = '\0';
335 char dest[2048];
336 strcpy(dest, very_long);
337 if (strcmp(dest, very_long)) {
338 t_error("Very long copy failed\n");
339 }
340 }
341
test_strcpy_non_terminated_src()342 void test_strcpy_non_terminated_src() {
343 char src[5] = {'a', 'b', 'c', 'd', 'e'};
344 char dest[10];
345 strcpy(dest, src);
346 if (strncmp(dest, "abcde", 5)) {
347 t_error("Non-terminated source copy failed\n");
348 }
349 }
350
test_strcpy_different_sizes()351 void test_strcpy_different_sizes() {
352 char dest1[5], dest2[10], dest3[20];
353 strcpy(dest1, "test");
354 if (strcmp(dest1, "test")) {
355 t_error("Small size copy failed\n");
356 }
357 strcpy(dest2, "longer test");
358 if (strcmp(dest2, "longer test")) {
359 t_error("Medium size copy failed\n");
360 }
361 strcpy(dest3, "the longest test here");
362 if (strcmp(dest3, "the longest test here")) {
363 t_error("Large size copy failed\n");
364 }
365 }
366
main()367 int main() {
368 test_strcpy_basic();
369 test_strcpy_empty();
370 test_strcpy_longer_string();
371 test_strcpy_special_characters();
372 test_strcpy_single_char();
373 test_strcpy_case_mixed();
374 test_strcpy_with_spaces();
375 test_strcpy_with_escapes();
376 test_strcpy_overwrite_old_content();
377 test_strcpy_with_embedded_null();
378 test_strcpy_modify_src_after_copy();
379 test_strcpy_utf8();
380 test_strcpy_multiple_assignments();
381 test_strcpy_to_offset();
382 test_strcpy_array_vs_pointer();
383 test_strcpy_all_caps();
384 test_strcpy_sentence();
385 test_strcpy_series();
386 test_strcpy_manual_src_array();
387 test_strcpy_vs_memcpy();
388 test_strcpy_batch_group_1();
389 test_strcpy_batch_group_2();
390 test_strcpy_control_chars();
391 test_strcpy_long_strings();
392 test_strcpy_non_ascii();
393 test_strcpy_hex_escape();
394 test_strcpy_octal_escape();
395 test_strcpy_partial_overwrite();
396 test_strcpy_pointer_arithmetic();
397 test_strcpy_large_buffer();
398 test_strcpy_null_termination();
399 test_strcpy_special_whitespace();
400 test_strcpy_alternating_chars();
401 test_strcpy_all_null();
402 test_strcpy_very_long();
403 test_strcpy_non_terminated_src();
404 test_strcpy_different_sizes();
405
406 return 0;
407 }