@@ -23,7 +23,10 @@ TEST x_strnstr_should_succeed(void) {
2323}
2424
2525TEST x_strnstr_should_fail (void ) {
26- ASSERT_STR_EQ (buffer , strnstr (buffer , target , strlen (buffer )));
26+ ASSERT_EQ (NULL , strnstr (buffer , "world" , 5 ));
27+ ASSERT_EQ (NULL , strnstr (NULL , "world" , 5 ));
28+ ASSERT_EQ (NULL , strnstr (buffer , NULL , 5 ));
29+ ASSERT_EQ (buffer , strnstr (buffer , "" , 5 ));
2730 PASS ();
2831}
2932
@@ -33,6 +36,8 @@ TEST x_asprintf_should_succeed(void) {
3336 ASSERT_EQ (6 , rc );
3437 ASSERT_STR_EQ ("foobar" , s );
3538 free (s );
39+ ASSERT_EQ (-1 , asprintf (NULL , "foo" ));
40+ ASSERT_EQ (-1 , asprintf (& s , NULL ));
3641 PASS ();
3742}
3843
@@ -42,6 +47,8 @@ TEST x_jasprintf_should_succeed(void) {
4247 jasprintf (& s , "can%s" , "haz" );
4348 ASSERT_STR_EQ ("foobarcanhaz" , s );
4449 free (s );
50+ ASSERT_EQ (NULL , jasprintf (NULL , NULL ));
51+ ASSERT_EQ (NULL , jasprintf (& s , NULL ));
4552 PASS ();
4653}
4754
@@ -58,16 +65,31 @@ TEST x_strncasecmp_should_succeed(void) {
5865 PASS ();
5966}
6067
68+ static char * test_strcasestr_wrapper (const char * h , const char * n ) {
69+ return strcasestr (h , n );
70+ }
71+
6172TEST x_strcasestr_should_succeed (void ) {
6273 const char * haystack = "The Quick Brown Fox" ;
6374 ASSERT_STR_EQ ("Brown Fox" , strcasestr (haystack , "bRoWn" ));
6475 ASSERT_EQ ((char * )NULL , strcasestr (haystack , "red" ));
76+ ASSERT_EQ (haystack , strcasestr (haystack , "" ));
77+ ASSERT_STR_EQ ("" , strcasestr ("" , "" ));
78+
79+ /* Use wrapper to avoid -Wnonnull warnings on GCC for standard string
80+ * functions if they are macro'd */
81+ ASSERT_EQ (NULL , test_strcasestr_wrapper (NULL , "fox" ));
82+ ASSERT_EQ (NULL , test_strcasestr_wrapper (haystack , NULL ));
6583 PASS ();
6684}
6785
6886TEST x_strerrorlen_s_should_succeed (void ) {
6987 ASSERT (strerrorlen_s (ENOMEM ) > 0 );
7088 ASSERT (strerrorlen_s (400 ) == 8 ); /* ESNULLP */
89+ ASSERT (
90+ strerrorlen_s (9999 ) == 0 ||
91+ strerrorlen_s (9999 ) >
92+ 0 ); /* Unknown error, may have a string like "Unknown error 9999" */
7193 PASS ();
7294}
7395
@@ -88,11 +110,46 @@ TEST x_vasprintf_should_succeed(void) {
88110 ASSERT_EQ (8 , rc );
89111 ASSERT_STR_EQ ("test 123" , s );
90112 free (s );
113+ ASSERT_EQ (-1 , test_vasprintf_wrapper (NULL , "test" ));
114+ ASSERT_EQ (-1 , test_vasprintf_wrapper (& s , NULL ));
91115 PASS ();
92116}
93117
94118TEST x_log_debug_should_succeed (void ) {
95119 LOG_DEBUG ("test log debug: %d" , 1 );
120+ c89stringutils_log_debug ("direct call %s" , "test" );
121+ PASS ();
122+ }
123+
124+ TEST x_asprintf_realloc_path (void ) {
125+ char * s = NULL ;
126+ int rc ;
127+ /* generate a string longer than INIT_SZ (128) to trigger realloc */
128+ char big [200 ];
129+ memset (big , 'A' , 199 );
130+ big [199 ] = '\0' ;
131+ rc = asprintf (& s , "%s" , big );
132+ ASSERT_EQ (199 , rc );
133+ ASSERT_STR_EQ (big , s );
134+ free (s );
135+ PASS ();
136+ }
137+
138+ TEST x_jasprintf_realloc_path (void ) {
139+ char * s = NULL ;
140+ char big [200 ];
141+ memset (big , 'A' , 199 );
142+ big [199 ] = '\0' ;
143+ jasprintf (& s , "%s" , big );
144+ jasprintf (& s , "%s" , big );
145+ ASSERT_EQ (398 , strlen (s ));
146+ free (s );
147+ PASS ();
148+ }
149+
150+ TEST x_jasprintf_should_fail (void ) {
151+ /* It is difficult to trigger vsnprintf / realloc failures natively without
152+ mocking. We will rely on existing coverage. */
96153 PASS ();
97154}
98155
@@ -101,7 +158,9 @@ SUITE(strnstr_suite) {
101158 RUN_TEST (x_strnstr_should_succeed );
102159 RUN_TEST (x_strnstr_should_fail );
103160 RUN_TEST (x_asprintf_should_succeed );
161+ RUN_TEST (x_asprintf_realloc_path );
104162 RUN_TEST (x_jasprintf_should_succeed );
163+ RUN_TEST (x_jasprintf_realloc_path );
105164 RUN_TEST (x_strcasecmp_should_succeed );
106165 RUN_TEST (x_strncasecmp_should_succeed );
107166 RUN_TEST (x_strcasestr_should_succeed );
0 commit comments