1 /*
2  * Copyright © 2005-2020 Rich Felker
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #define _BSD_SOURCE
24 #include <stdio.h>
25 #include <string.h>
26 
27 /* r = place to store result
28  * f = function call to test (or any expression)
29  * x = expected result
30  * m = message to print on failure (with formats for r & x)
31 **/
32 
33 #pragma GCC diagnostic ignored "-Wpragmas"
34 #pragma GCC diagnostic ignored "-Wunknown-warning-option"
35 #pragma GCC diagnostic ignored "-Wformat-extra-args"
36 #pragma GCC diagnostic ignored "-Wformat"
37 #pragma GCC diagnostic ignored "-Wstringop-truncation"
38 
39 #define TEST(r, f, x, m) do {                                           \
40         (r) = (f);                                                      \
41         if ((r) != (x)) {                                               \
42             printf("%s:%d: %s failed (" m ")\n", __FILE__, __LINE__, #f, r, x ); \
43             err++;                                                      \
44         }                                                               \
45     } while(0)
46 
47 #define TEST_S(s, x, m) do {                                            \
48         if (strcmp((s),(x)) != 0) {                                     \
49             printf(__FILE__ ":%d: [%s] != [%s] (%s)\n", __LINE__, s, x, m); \
50             err++;                                                      \
51         }                                                               \
52     } while(0)
53 
54 
test_string(void)55 int test_string(void)
56 {
57 	char b[32];
58 	char *s;
59 	int i;
60 	int err=0;
61 
62 	b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0;
63 	TEST(s, strcpy(b, b+16), b, "wrong return %p != %p");
64 	TEST_S(s, "abc", "strcpy gave incorrect string");
65 	TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p");
66 	TEST_S(s, "abc", "strcpy gave incorrect string");
67 	TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p");
68 	TEST_S(s, "abc", "strcpy gave incorrect string");
69 	TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p");
70 	TEST_S(s, "abc", "strcpy gave incorrect string");
71 
72 	TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p");
73 	TEST_S(s, "bc", "strcpy gave incorrect string");
74 	TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p");
75 	TEST_S(s, "c", "strcpy gave incorrect string");
76 	TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p");
77 	TEST_S(s, "", "strcpy gave incorrect string");
78 
79 	TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
80 	TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
81 	TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
82 	TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
83 
84 	b[3] = 'x'; b[4] = 0;
85 	strncpy(b, "abc", 3);
86 	TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
87 	TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
88 
89 	TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
90 	TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
91 
92 	strcpy(b, "abc");
93 	TEST(s, strncat(b, "123456", 3), b, "%p != %p");
94 	TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
95 	TEST_S(s, "abc123", "strncat gave incorrect string");
96 
97 	strcpy(b, "aaababccdd0001122223");
98 	TEST(s, strchr(b, 'b'), b+3, "%p != %p");
99 	TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
100 	TEST(i, strspn(b, "abcd"), 10, "%d != %d");
101 	TEST(i, strcspn(b, "0123"), 10, "%d != %d");
102 	TEST(s, strpbrk(b, "0123"), b+10, "%p != %p");
103 
104 	strcpy(b, "abc   123; xyz; foo");
105 	TEST(s, strtok(b, " "), b, "%p != %p");
106 	TEST_S(s, "abc", "strtok result");
107 
108 	TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
109 	TEST_S(s, "  123", "strtok result");
110 
111 	TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
112 	TEST_S(s, "xyz", "strtok result");
113 
114 	TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
115 	TEST_S(s, "foo", "strtok result");
116 
117 #ifdef HAVE_BSD_STRL
118 	memset(b, 'x', sizeof b);
119 	TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
120 	TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
121 	TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
122 
123 	memset(b, 'x', sizeof b);
124 	TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
125 	TEST(i, b[0], 'a', "strlcpy did not copy character %d");
126 	TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
127 
128 	memset(b, 'x', sizeof b);
129 	TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
130 	TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
131 
132 	TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
133 
134 	memcpy(b, "abc\0\0\0x\0", 8);
135 	TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
136 	TEST_S(b, "abc123", "strlcat result");
137 
138 	memcpy(b, "abc\0\0\0x\0", 8);
139 	TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
140 	TEST_S(b, "abc12", "strlcat result");
141 	TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
142 
143 	memcpy(b, "abc\0\0\0x\0", 8);
144 	TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
145 	TEST_S(b, "abc", "strlcat result");
146 
147 	memcpy(b, "abc\0\0\0x\0", 8);
148 	TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
149 	TEST_S(b, "abc", "strlcat result");
150 #endif
151 
152 	return err;
153 }
154 
155 #define TEST_NAME string
156 #include "testcase.h"
157