diff options
author | 0x41CEA55 <167377970+41CEA55@users.noreply.github.com> | 2024-04-19 18:16:40 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-19 17:16:40 +0200 |
commit | 1b2e2cd968876ff4e7a772bf8df2517d583d9956 (patch) | |
tree | 8676f5651e41aa935b373a740477184d2b6b23e2 /example/ndpiReader.c | |
parent | e75d7a620e60fd35385793f09209f8a857d4b325 (diff) |
Add strlcpy implementation (#2395)
Diffstat (limited to 'example/ndpiReader.c')
-rw-r--r-- | example/ndpiReader.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/example/ndpiReader.c b/example/ndpiReader.c index 73d469952..f04ee9f1e 100644 --- a/example/ndpiReader.c +++ b/example/ndpiReader.c @@ -5630,6 +5630,42 @@ void strtonumUnitTest() { /* *********************************************** */ +void strlcpyUnitTest() { + // Test empty string + char dst_empty[10] = ""; + assert(ndpi_strlcpy(dst_empty, "", sizeof(dst_empty)) == 0); + assert(dst_empty[0] == '\0'); + + // Basic copy test + char dst1[10] = ""; + assert(ndpi_strlcpy(dst1, "abc", sizeof(dst1)) == 3); + assert(strcmp(dst1, "abc") == 0); + + // Test with dst_len smaller than src_len + char dst2[4] = ""; + assert(ndpi_strlcpy(dst2, "abcdef", sizeof(dst2)) == 6); + assert(strcmp(dst2, "abc") == 0); // Should truncate "abcdef" to "abc" + + // Test with dst_len bigger than src_len + char dst3[10] = ""; + assert(ndpi_strlcpy(dst3, "abc", sizeof(dst3)) == 3); + assert(strcmp(dst3, "abc") == 0); + + // Test with dst_len equal to 1 (only null terminator should be copied) + char dst4[1]; + assert(ndpi_strlcpy(dst4, "abc", sizeof(dst4)) == 3); + assert(dst4[0] == '\0'); // Should only contain the null terminator + + // Test with NULL source, expecting return value to be 0 + char dst5[10]; + assert(ndpi_strlcpy(dst5, NULL, sizeof(dst5)) == 0); + + // Test with NULL destination, should also return 0 without crashing + assert(ndpi_strlcpy(NULL, "abc", sizeof(dst5)) == 0); +} + +/* *********************************************** */ + void filterUnitTest() { ndpi_filter* f = ndpi_filter_alloc(); u_int32_t v, i; @@ -5995,6 +6031,7 @@ int main(int argc, char **argv) { analysisUnitTest(); compressedBitmapUnitTest(); strtonumUnitTest(); + strlcpyUnitTest(); #endif } |