* Copyright (c) 2025 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <string.h>
#include "test.h"
void test_strcpy_basic() {
char dest[10];
strcpy(dest, "abc");
if (strcmp(dest, "abc")) {
t_error("Expected 'abc', got '%s'\n", dest);
}
}
void test_strcpy_empty() {
char dest[10] = "xxxxxx";
strcpy(dest, "");
if (strcmp(dest, "")) {
t_error("Expected empty string, got '%s'\n", dest);
}
}
void test_strcpy_longer_string() {
char dest[100];
strcpy(dest, "This is a longer string with multiple words.");
if (strcmp(dest, "This is a longer string with multiple words.")) {
t_error("Long string copy failed\n");
}
}
void test_strcpy_special_characters() {
char dest[20];
strcpy(dest, "@#$%^&*()");
if (strcmp(dest, "@#$%^&*()")) {
t_error("Special characters copy failed\n");
}
}
void test_strcpy_single_char() {
char dest[10];
strcpy(dest, "Z");
if (strcmp(dest, "Z")) {
t_error("Single char copy failed\n");
}
}
void test_strcpy_case_mixed() {
char dest[20];
strcpy(dest, "AbCdEfGh");
if (strcmp(dest, "AbCdEfGh")) {
t_error("Mixed case copy failed\n");
}
}
void test_strcpy_with_spaces() {
char dest[20];
strcpy(dest, " ");
if (strcmp(dest, " ")) {
t_error("Spaces copy failed\n");
}
}
void test_strcpy_with_escapes() {
char dest[20];
strcpy(dest, "Line1\nLine2");
if (strcmp(dest, "Line1\nLine2")) {
t_error("Escapes copy failed\n");
}
}
void test_strcpy_overwrite_old_content() {
char dest[20] = "OldContentHere";
strcpy(dest, "New");
if (strcmp(dest, "New")) {
t_error("Overwrite failed\n");
}
}
void test_strcpy_with_embedded_null() {
char src[10] = {'a', 'b', '\0', 'c'};
char dest[10];
strcpy(dest, src);
if (dest[0] != 'a' || dest[1] != 'b' || dest[2] != '\0') {
t_error("Embedded null copy failed\n");
}
}
void test_strcpy_modify_src_after_copy() {
char src[] = "copyme";
char dest[20];
strcpy(dest, src);
src[0] = 'X';
if (strcmp(dest, "copyme")) {
t_error("Source modification affected destination\n");
}
}
void test_strcpy_utf8() {
char dest[20];
strcpy(dest, "café");
if (strcmp(dest, "café")) {
t_error("UTF-8 copy failed\n");
}
}
void test_strcpy_multiple_assignments() {
char dest[20];
strcpy(dest, "first");
if (strcmp(dest, "first")) {
t_error("First assignment failed\n");
}
strcpy(dest, "second");
if (strcmp(dest, "second")) {
t_error("Second assignment failed\n");
}
}
void test_strcpy_to_offset() {
char buffer[20] = "1234567890";
strcpy(buffer + 5, "abc");
if (strcmp(buffer + 5, "abc")) {
t_error("Offset copy failed\n");
}
}
void test_strcpy_array_vs_pointer() {
char a[10];
char *b = a;
strcpy(b, "test");
if (strcmp(a, "test")) {
t_error("Array vs pointer copy failed\n");
}
}
void test_strcpy_all_caps() {
char dest[30];
strcpy(dest, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
if (strcmp(dest, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")) {
t_error("All caps copy failed\n");
}
}
void test_strcpy_sentence() {
char dest[100];
strcpy(dest, "The quick brown fox jumps over the lazy dog.");
if (strcmp(dest, "The quick brown fox jumps over the lazy dog.")) {
t_error("Sentence copy failed\n");
}
}
void test_strcpy_series() {
char dest[50];
strcpy(dest, "one");
if (strcmp(dest, "one")) {
t_error("First in series failed\n");
}
strcpy(dest, "two");
if (strcmp(dest, "two")) {
t_error("Second in series failed\n");
}
strcpy(dest, "three");
if (strcmp(dest, "three")) {
t_error("Third in series failed\n");
}
}
void test_strcpy_manual_src_array() {
char src[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
char dest[10];
strcpy(dest, src);
if (strcmp(dest, "hello")) {
t_error("Manual array copy failed\n");
}
}
void test_strcpy_vs_memcpy() {
char dest1[10];
char dest2[10];
char *src = "abc";
strcpy(dest1, src);
memcpy(dest2, src, strlen(src) + 1);
if (strcmp(dest1, dest2)) {
t_error("strcpy vs memcpy comparison failed\n");
}
}
void test_strcpy_batch_group_1() {
char dest[100];
strcpy(dest, "batch1");
if (strcmp(dest, "batch1")) {
t_error("Batch1 first copy failed\n");
}
strcpy(dest, "batch2");
if (strcmp(dest, "batch2")) {
t_error("Batch1 second copy failed\n");
}
strcpy(dest, "batch3");
if (strcmp(dest, "batch3")) {
t_error("Batch1 third copy failed\n");
}
}
void test_strcpy_batch_group_2() {
char dest[100];
strcpy(dest, "hello world");
if (strcmp(dest, "hello world")) {
t_error("Batch2 first copy failed\n");
}
strcpy(dest, "C programming");
if (strcmp(dest, "C programming")) {
t_error("Batch2 second copy failed\n");
}
strcpy(dest, "libc test");
if (strcmp(dest, "libc test")) {
t_error("Batch2 third copy failed\n");
}
}
void test_strcpy_control_chars() {
char dest[20];
strcpy(dest, "\x01\x02\x03\x04");
if (memcmp(dest, "\x01\x02\x03\x04", 4)) {
t_error("Control chars copy failed\n");
}
}
void test_strcpy_long_strings() {
char long_str[256];
memset(long_str, 'a', 255);
long_str[255] = '\0';
char dest[256];
strcpy(dest, long_str);
if (strcmp(dest, long_str)) {
t_error("Long string copy failed\n");
}
}
void test_strcpy_non_ascii() {
char dest[20];
strcpy(dest, "éñüß");
if (strcmp(dest, "éñüß")) {
t_error("Non-ASCII copy failed\n");
}
}
void test_strcpy_hex_escape() {
char dest[20];
strcpy(dest, "\x41\x42\x43");
if (strcmp(dest, "ABC")) {
t_error("Hex escape copy failed\n");
}
}
void test_strcpy_octal_escape() {
char dest[20];
strcpy(dest, "\101\102\103");
if (strcmp(dest, "ABC")) {
t_error("Octal escape copy failed\n");
}
}
void test_strcpy_partial_overwrite() {
char dest[20] = "abcdefghij";
strcpy(dest + 3, "123");
if (!strcmp(dest, "abc123ghij")) {
t_error("Partial overwrite check failed\n");
}
}
void test_strcpy_pointer_arithmetic() {
char src[] = "abcdef";
char dest[10];
strcpy(dest, src + 2);
if (strcmp(dest, "cdef")) {
t_error("Pointer arithmetic copy failed\n");
}
}
void test_strcpy_large_buffer() {
char dest[1024];
strcpy(dest, "This is a large buffer test");
if (strcmp(dest, "This is a large buffer test")) {
t_error("Large buffer copy failed\n");
}
}
void test_strcpy_null_termination() {
char dest[10];
strcpy(dest, "12345");
if (dest[5] != '\0') {
t_error("Null termination failed\n");
}
}
void test_strcpy_special_whitespace() {
char dest[20];
strcpy(dest, "\t\n\v\f\r");
if (strcmp(dest, "\t\n\v\f\r")) {
t_error("Special whitespace copy failed\n");
}
}
void test_strcpy_alternating_chars() {
char dest[20];
strcpy(dest, "a\0b\0c");
if (strcmp(dest, "a")) {
t_error("Alternating chars copy failed\n");
}
}
void test_strcpy_all_null() {
char dest[10];
strcpy(dest, "\0\0\0");
if (strcmp(dest, "")) {
t_error("All null copy failed\n");
}
}
void test_strcpy_very_long() {
char very_long[2048];
memset(very_long, 'x', 2047);
very_long[2047] = '\0';
char dest[2048];
strcpy(dest, very_long);
if (strcmp(dest, very_long)) {
t_error("Very long copy failed\n");
}
}
void test_strcpy_non_terminated_src() {
char src[5] = {'a', 'b', 'c', 'd', 'e'};
char dest[10];
strcpy(dest, src);
if (strncmp(dest, "abcde", 5)) {
t_error("Non-terminated source copy failed\n");
}
}
void test_strcpy_different_sizes() {
char dest1[5], dest2[10], dest3[20];
strcpy(dest1, "test");
if (strcmp(dest1, "test")) {
t_error("Small size copy failed\n");
}
strcpy(dest2, "longer test");
if (strcmp(dest2, "longer test")) {
t_error("Medium size copy failed\n");
}
strcpy(dest3, "the longest test here");
if (strcmp(dest3, "the longest test here")) {
t_error("Large size copy failed\n");
}
}
void test_strcpy_boundary_exact() {
char dest[5];
strcpy(dest, "test");
if (strcmp(dest, "test")) {
t_error("Boundary exact copy failed\n");
}
}
void test_strcpy_repeated_chars() {
char dest[20];
strcpy(dest, "aaaaaaaaaa");
if (strcmp(dest, "aaaaaaaaaa")) {
t_error("Repeated chars copy failed\n");
}
}
void test_strcpy_same_src_dest() {
char dest[20] = "original";
strcpy(dest, dest);
if (strcmp(dest, "original")) {
t_error("Same src/dest copy failed\n");
}
}
void test_strcpy_numeric_string() {
char dest[20];
strcpy(dest, "1234567890");
if (strcmp(dest, "1234567890")) {
t_error("Numeric string copy failed\n");
}
}
void test_strcpy_mixed_alphanumeric() {
char dest[20];
strcpy(dest, "abc123xyz");
if (strcmp(dest, "abc123xyz")) {
t_error("Mixed alphanumeric copy failed\n");
}
}
void test_strcpy_with_quotes() {
char dest[30];
strcpy(dest, "\"quoted\"");
if (strcmp(dest, "\"quoted\"")) {
t_error("Quotes copy failed\n");
}
}
void test_strcpy_path_string() {
char dest[50];
strcpy(dest, "/data/local/tmp");
if (strcmp(dest, "/data/local/tmp")) {
t_error("Path string copy failed\n");
}
}
void test_strcpy_url_string() {
char dest[50];
strcpy(dest, "https://example.com");
if (strcmp(dest, "https://example.com")) {
t_error("URL string copy failed\n");
}
}
void test_strcpy_email_string() {
char dest[30];
strcpy(dest, "user@example.com");
if (strcmp(dest, "user@example.com")) {
t_error("Email string copy failed\n");
}
}
void test_strcpy_hex_string() {
char dest[20];
strcpy(dest, "0x1234abcd");
if (strcmp(dest, "0x1234abcd")) {
t_error("Hex string copy failed\n");
}
}
void test_strcpy_brackets() {
char dest[20];
strcpy(dest, "[test]");
if (strcmp(dest, "[test]")) {
t_error("Brackets copy failed\n");
}
}
void test_strcpy_curly_braces() {
char dest[20];
strcpy(dest, "{test}");
if (strcmp(dest, "{test}")) {
t_error("Curly braces copy failed\n");
}
}
void test_strcpy_underscore() {
char dest[20];
strcpy(dest, "test_variable");
if (strcmp(dest, "test_variable")) {
t_error("Underscore copy failed\n");
}
}
void test_strcpy_dash() {
char dest[20];
strcpy(dest, "test-variable");
if (strcmp(dest, "test-variable")) {
t_error("Dash copy failed\n");
}
}
void test_strcpy_plus_sign() {
char dest[20];
strcpy(dest, "test+value");
if (strcmp(dest, "test+value")) {
t_error("Plus sign copy failed\n");
}
}
void test_strcpy_ampersand() {
char dest[20];
strcpy(dest, "test&value");
if (strcmp(dest, "test&value")) {
t_error("Ampersand copy failed\n");
}
}
void test_strcpy_pipe() {
char dest[20];
strcpy(dest, "test|value");
if (strcmp(dest, "test|value")) {
t_error("Pipe copy failed\n");
}
}
void test_strcpy_backslash() {
char dest[20];
strcpy(dest, "test\\path");
if (strcmp(dest, "test\\path")) {
t_error("Backslash copy failed\n");
}
}
void test_strcpy_colon() {
char dest[20];
strcpy(dest, "name:value");
if (strcmp(dest, "name:value")) {
t_error("Colon copy failed\n");
}
}
void test_strcpy_semicolon() {
char dest[20];
strcpy(dest, "test;value");
if (strcmp(dest, "test;value")) {
t_error("Semicolon copy failed\n");
}
}
void test_strcpy_double_space() {
char dest[20];
strcpy(dest, "test value");
if (strcmp(dest, "test value")) {
t_error("Double space copy failed\n");
}
}
void test_strcpy_leading_space() {
char dest[20];
strcpy(dest, " test");
if (strcmp(dest, " test")) {
t_error("Leading space copy failed\n");
}
}
void test_strcpy_trailing_space() {
char dest[20];
strcpy(dest, "test ");
if (strcmp(dest, "test ")) {
t_error("Trailing space copy failed\n");
}
}
void test_strcpy_cjk_characters() {
char dest[20];
strcpy(dest, "测试");
if (strcmp(dest, "测试")) {
t_error("CJK characters copy failed\n");
}
}
void test_strcpy_uuid() {
char dest[40];
strcpy(dest, "550e8400-e29b-41d4-a716-446655440000");
if (strcmp(dest, "550e8400-e29b-41d4-a716-446655440000")) {
t_error("UUID copy failed\n");
}
}
void test_strcpy_xml_tag() {
char dest[20];
strcpy(dest, "<tag>");
if (strcmp(dest, "<tag>")) {
t_error("XML tag copy failed\n");
}
}
void test_strcpy_regex_pattern() {
char dest[30];
strcpy(dest, "[a-z]+");
if (strcmp(dest, "[a-z]+")) {
t_error("Regex pattern copy failed\n");
}
}
void test_strcpy_sql_query() {
char dest[50];
strcpy(dest, "SELECT * FROM table");
if (strcmp(dest, "SELECT * FROM table")) {
t_error("SQL query copy failed\n");
}
}
void test_strcpy_shell_command() {
char dest[30];
strcpy(dest, "ls -la");
if (strcmp(dest, "ls -la")) {
t_error("Shell command copy failed\n");
}
}
void test_strcpy_http_header() {
char dest[50];
strcpy(dest, "Content-Type: text/html");
if (strcmp(dest, "Content-Type: text/html")) {
t_error("HTTP header copy failed\n");
}
}
void test_strcpy_time_format() {
char dest[20];
strcpy(dest, "12:30:45");
if (strcmp(dest, "12:30:45")) {
t_error("Time format copy failed\n");
}
}
void test_strcpy_datetime_format() {
char dest[30];
strcpy(dest, "2025-01-15T12:30:45Z");
if (strcmp(dest, "2025-01-15T12:30:45Z")) {
t_error("DateTime format copy failed\n");
}
}
void test_strcpy_mime_boundary() {
char dest[50];
strcpy(dest, "----=_Part_0_1234567890");
if (strcmp(dest, "----=_Part_0_1234567890")) {
t_error("MIME boundary copy failed\n");
}
}
void test_strcpy_content_disposition() {
char dest[50];
strcpy(dest, "attachment; filename=test.txt");
if (strcmp(dest, "attachment; filename=test.txt")) {
t_error("Content disposition copy failed\n");
}
}
void test_strcpy_emoji() {
char dest[20];
strcpy(dest, "👋");
if (strcmp(dest, "👋")) {
t_error("Emoji copy failed\n");
}
}
void test_strcpy_russian() {
char dest[20];
strcpy(dest, "Привет");
if (strcmp(dest, "Привет")) {
t_error("Russian copy failed\n");
}
}
void test_strcpy_greek() {
char dest[20];
strcpy(dest, "Γειά");
if (strcmp(dest, "Γειά")) {
t_error("Greek copy failed\n");
}
}
void test_strcpy_arabic() {
char dest[20];
strcpy(dest, "مرحبا");
if (strcmp(dest, "مرحبا")) {
t_error("Arabic copy failed\n");
}
}
int main() {
test_strcpy_basic();
test_strcpy_empty();
test_strcpy_longer_string();
test_strcpy_special_characters();
test_strcpy_single_char();
test_strcpy_case_mixed();
test_strcpy_with_spaces();
test_strcpy_with_escapes();
test_strcpy_overwrite_old_content();
test_strcpy_with_embedded_null();
test_strcpy_modify_src_after_copy();
test_strcpy_utf8();
test_strcpy_multiple_assignments();
test_strcpy_to_offset();
test_strcpy_array_vs_pointer();
test_strcpy_all_caps();
test_strcpy_sentence();
test_strcpy_series();
test_strcpy_manual_src_array();
test_strcpy_vs_memcpy();
test_strcpy_batch_group_1();
test_strcpy_batch_group_2();
test_strcpy_control_chars();
test_strcpy_long_strings();
test_strcpy_non_ascii();
test_strcpy_hex_escape();
test_strcpy_octal_escape();
test_strcpy_partial_overwrite();
test_strcpy_pointer_arithmetic();
test_strcpy_large_buffer();
test_strcpy_null_termination();
test_strcpy_special_whitespace();
test_strcpy_alternating_chars();
test_strcpy_all_null();
test_strcpy_very_long();
test_strcpy_non_terminated_src();
test_strcpy_different_sizes();
test_strcpy_boundary_exact();
test_strcpy_repeated_chars();
test_strcpy_same_src_dest();
test_strcpy_numeric_string();
test_strcpy_mixed_alphanumeric();
test_strcpy_with_quotes();
test_strcpy_path_string();
test_strcpy_url_string();
test_strcpy_email_string();
test_strcpy_hex_string();
test_strcpy_brackets();
test_strcpy_curly_braces();
test_strcpy_underscore();
test_strcpy_dash();
test_strcpy_plus_sign();
test_strcpy_ampersand();
test_strcpy_pipe();
test_strcpy_backslash();
test_strcpy_colon();
test_strcpy_semicolon();
test_strcpy_double_space();
test_strcpy_leading_space();
test_strcpy_trailing_space();
test_strcpy_cjk_characters();
test_strcpy_uuid();
test_strcpy_xml_tag();
test_strcpy_regex_pattern();
test_strcpy_sql_query();
test_strcpy_shell_command();
test_strcpy_http_header();
test_strcpy_time_format();
test_strcpy_datetime_format();
test_strcpy_mime_boundary();
test_strcpy_content_disposition();
test_strcpy_emoji();
test_strcpy_russian();
test_strcpy_greek();
test_strcpy_arabic();
return 0;
}