• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #define _DEFAULT_SOURCE 1
2 #define _BSD_SOURCE 1
3 #include <stdio.h>
4 #include <string.h>
5 #include "test.h"
6 
7 /* r = place to store result
8  * f = function call to test (or any expression)
9  * x = expected result
10  * m = message to print on failure (with formats for r & x)
11 **/
12 
13 #define TEST(r, f, x, m) ( \
14 	((r) = (f)) == (x) || \
15 	(t_error("%s failed (" m ")\n", #f, r, x), 0) )
16 
17 #define TEST_S(s, x, m) ( \
18 	!strcmp((s),(x)) || \
19 	(t_error("[%s] != [%s] (%s)\n", s, x, m), 0) )
20 
main(void)21 int main(void)
22 {
23 	char b[32];
24 	char *s;
25 	int i;
26 
27 	b[16]='a'; b[17]='b'; b[18]='c'; b[19]=0;
28 	TEST(s, strcpy(b, b+16), b, "wrong return %p != %p");
29 	TEST_S(s, "abc", "strcpy gave incorrect string");
30 	TEST(s, strcpy(b+1, b+16), b+1, "wrong return %p != %p");
31 	TEST_S(s, "abc", "strcpy gave incorrect string");
32 	TEST(s, strcpy(b+2, b+16), b+2, "wrong return %p != %p");
33 	TEST_S(s, "abc", "strcpy gave incorrect string");
34 	TEST(s, strcpy(b+3, b+16), b+3, "wrong return %p != %p");
35 	TEST_S(s, "abc", "strcpy gave incorrect string");
36 
37 	TEST(s, strcpy(b+1, b+17), b+1, "wrong return %p != %p");
38 	TEST_S(s, "bc", "strcpy gave incorrect string");
39 	TEST(s, strcpy(b+2, b+18), b+2, "wrong return %p != %p");
40 	TEST_S(s, "c", "strcpy gave incorrect string");
41 	TEST(s, strcpy(b+3, b+19), b+3, "wrong return %p != %p");
42 	TEST_S(s, "", "strcpy gave incorrect string");
43 
44 	TEST(s, memset(b, 'x', sizeof b), b, "wrong return %p != %p");
45 	TEST(s, strncpy(b, "abc", sizeof b - 1), b, "wrong return %p != %p");
46 	TEST(i, memcmp(b, "abc\0\0\0\0", 8), 0, "strncpy fails to zero-pad dest");
47 	TEST(i, b[sizeof b - 1], 'x', "strncpy overruns buffer when n > strlen(src)");
48 
49 	b[3] = 'x'; b[4] = 0;
50 	strncpy(b, "abc", 3);
51 	TEST(i, b[2], 'c', "strncpy fails to copy last byte: %hhu != %hhu");
52 	TEST(i, b[3], 'x', "strncpy overruns buffer to null-terminate: %hhu != %hhu");
53 
54 	TEST(i, !strncmp("abcd", "abce", 3), 1, "strncmp compares past n");
55 	TEST(i, !!strncmp("abc", "abd", 3), 1, "strncmp fails to compare n-1st byte");
56 
57 	strcpy(b, "abc");
58 	TEST(s, strncat(b, "123456", 3), b, "%p != %p");
59 	TEST(i, b[6], 0, "strncat failed to null-terminate (%d)");
60 	TEST_S(s, "abc123", "strncat gave incorrect string");
61 
62 	strcpy(b, "aaababccdd0001122223");
63 	TEST(s, strchr(b, 'b'), b+3, "%p != %p");
64 	TEST(s, strrchr(b, 'b'), b+5, "%p != %p");
65 	TEST(i, strspn(b, "abcd"), 10, "%d != %d");
66 	TEST(i, strcspn(b, "0123"), 10, "%d != %d");
67 	TEST(s, strpbrk(b, "0123"), b+10, "%d != %d");
68 
69 	strcpy(b, "abc   123; xyz; foo");
70 	TEST(s, strtok(b, " "), b, "%p != %p");
71 	TEST_S(s, "abc", "strtok result");
72 
73 	TEST(s, strtok(NULL, ";"), b+4, "%p != %p");
74 	TEST_S(s, "  123", "strtok result");
75 
76 	TEST(s, strtok(NULL, " ;"), b+11, "%p != %p");
77 	TEST_S(s, "xyz", "strtok result");
78 
79 	TEST(s, strtok(NULL, " ;"), b+16, "%p != %p");
80 	TEST_S(s, "foo", "strtok result");
81 
82 	memset(b, 'x', sizeof b);
83 	TEST(i, strlcpy(b, "abc", sizeof b - 1), 3, "length %d != %d");
84 	TEST(i, b[3], 0, "strlcpy did not null-terminate short string (%d)");
85 	TEST(i, b[4], 'x', "strlcpy wrote extra bytes (%d)");
86 
87 	memset(b, 'x', sizeof b);
88 	TEST(i, strlcpy(b, "abc", 2), 3, "length %d != %d");
89 	TEST(i, b[0], 'a', "strlcpy did not copy character %d");
90 	TEST(i, b[1], 0, "strlcpy did not null-terminate long string (%d)");
91 
92 	memset(b, 'x', sizeof b);
93 	TEST(i, strlcpy(b, "abc", 3), 3, "length %d != %d");
94 	TEST(i, b[2], 0, "strlcpy did not null-terminate l-length string (%d)");
95 
96 	TEST(i, strlcpy(NULL, "abc", 0), 3, "length %d != %d");
97 
98 	memcpy(b, "abc\0\0\0x\0", 8);
99 	TEST(i, strlcat(b, "123", sizeof b), 6, "length %d != %d");
100 	TEST_S(b, "abc123", "strlcat result");
101 
102 	memcpy(b, "abc\0\0\0x\0", 8);
103 	TEST(i, strlcat(b, "123", 6), 6, "length %d != %d");
104 	TEST_S(b, "abc12", "strlcat result");
105 	TEST(i, b[6], 'x', "strlcat wrote past string %d != %d");
106 
107 	memcpy(b, "abc\0\0\0x\0", 8);
108 	TEST(i, strlcat(b, "123", 4), 6, "length %d != %d");
109 	TEST_S(b, "abc", "strlcat result");
110 
111 	memcpy(b, "abc\0\0\0x\0", 8);
112 	TEST(i, strlcat(b, "123", 3), 6, "length %d != %d");
113 	TEST_S(b, "abc", "strlcat result");
114 
115 	return t_status;
116 }
117