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 _DEFAULT_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 (s == NULL || 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 static int test_string(void)
56 {
57 char b[32] = {0};
58 char c[32] = {0};
59 char *s = NULL;
60 int i;
61 int err=0;
62
63 c[0]='a'; c[1]='b'; c[2]='c'; c[3]=0;
64 TEST(s, strcpy(b, c), b, "wrong return %p != %p");
65 TEST_S(s, "abc", "strcpy gave incorrect string");
66 TEST(s, strcpy(b+1, c), b+1, "wrong return %p != %p");
67 TEST_S(s, "abc", "strcpy gave incorrect string");
68 TEST(s, strcpy(b+2, c), b+2, "wrong return %p != %p");
69 TEST_S(s, "abc", "strcpy gave incorrect string");
70 TEST(s, strcpy(b+3, c), b+3, "wrong return %p != %p");
71 TEST_S(s, "abc", "strcpy gave incorrect string");
72
73 TEST(s, strcpy(b+1, c+1), b+1, "wrong return %p != %p");
74 TEST_S(s, "bc", "strcpy gave incorrect string");
75 TEST(s, strcpy(b+2, c+2), b+2, "wrong return %p != %p");
76 TEST_S(s, "c", "strcpy gave incorrect string");
77 TEST(s, strcpy(b+3, c+3), b+3, "wrong return %p != %p");
78 TEST_S(s, "", "strcpy gave incorrect string");
79
80 TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
81 TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
82 TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
83 TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
84
85 b[3] = 'x'; b[4] = 0;
86 strncpy(b, "abc", 3);
87 TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
88 TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
89
90 TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
91 TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
92
93 strcpy(b, "abc");
94 TEST(s, strncat(b, "123456", 3), b, "%p != %p");
95 TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
96 TEST_S(s, "abc123", "strncat gave incorrect string");
97
98 strcpy(b, "aaababccdd0001122223");
99 TEST(s, strchr(b, 'b'), b+3, "%p != %p");
100 TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
101 TEST(i, strspn(b, "abcd"), 10, "%d != %d");
102 TEST(i, strcspn(b, "0123"), 10, "%d != %d");
103 TEST(s, strpbrk(b, "0123"), b+10, "%p != %p");
104
105 strcpy(b, "abc 123; xyz; foo");
106 TEST(s, strtok(b, " "), b, "%p != %p");
107 TEST_S(s, "abc", "strtok result");
108
109 TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
110 TEST_S(s, " 123", "strtok result");
111
112 TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
113 TEST_S(s, "xyz", "strtok result");
114
115 TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
116 TEST_S(s, "foo", "strtok result");
117
118 #ifdef HAVE_BSD_STRL
119 memset(b, 'x', sizeof b);
120 TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
121 TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
122 TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
123
124 memset(b, 'x', sizeof b);
125 TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
126 TEST(i, b[0], 'a', "strlcpy did not copy character %d");
127 TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
128
129 memset(b, 'x', sizeof b);
130 TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
131 TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
132
133 TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
134
135 memcpy(b, "abc\0\0\0x\0", 8);
136 TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
137 TEST_S(b, "abc123", "strlcat result");
138
139 memcpy(b, "abc\0\0\0x\0", 8);
140 TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
141 TEST_S(b, "abc12", "strlcat result");
142 TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
143
144 memcpy(b, "abc\0\0\0x\0", 8);
145 TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
146 TEST_S(b, "abc", "strlcat result");
147
148 memcpy(b, "abc\0\0\0x\0", 8);
149 TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
150 TEST_S(b, "abc", "strlcat result");
151 #endif
152
153 return err;
154 }
155
156 #define TEST_NAME string
157 #include "testcase.h"
158