xref: /freebsd/contrib/sendmail/libsm/t-strl.c (revision cc426dd31990b8b50b210efc450e404596548ca1)
1 /*
2  * Copyright (c) 2000-2001 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  *
9  */
10 
11 #include <sm/gen.h>
12 SM_IDSTR(id, "@(#)$Id: t-strl.c,v 1.16 2013-11-22 20:51:44 ca Exp $")
13 
14 #include <stdlib.h>
15 #include <stdio.h>
16 #include <sm/heap.h>
17 #include <sm/string.h>
18 #include <sm/test.h>
19 
20 #define MAXL	16
21 #define N	5
22 #define SIZE	128
23 
24 int
25 main(argc, argv)
26 	int argc;
27 	char *argv[];
28 {
29 	char *s1, *s2, *s3;
30 	int one, two, k;
31 	char src1[N][SIZE], dst1[SIZE], dst2[SIZE];
32 	char *r;
33 
34 	sm_test_begin(argc, argv, "test strl* string functions");
35 	s1 = "abc";
36 	s2 = "123";
37 	s3 = sm_malloc_x(MAXL);
38 
39 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
40 	SM_TEST(strcmp(s1, s3) == 0);
41 
42 	SM_TEST(sm_strlcat(s3, s2, 8) == 6);
43 	r ="abc123";
44 	SM_TEST(strcmp(s3, r) == 0);
45 
46 	SM_TEST(sm_strlcpy(s3, s1, 2) == 3);
47 	r = "a";
48 	SM_TEST(strcmp(s3, r) == 0);
49 
50 	SM_TEST(sm_strlcat(s3, s2, 3) == 4);
51 	r = "a1";
52 	SM_TEST(strcmp(s3, r) == 0);
53 
54 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
55 	r = ":";
56 	SM_TEST(sm_strlcat2(s3, r, s2, MAXL) == 7);
57 	r = "abc:123";
58 	SM_TEST(strcmp(s3, r) == 0);
59 
60 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
61 	r = ":";
62 	SM_TEST(sm_strlcat2(s3, r, s2, 6) == 7);
63 	r = "abc:1";
64 	SM_TEST(strcmp(s3, r) == 0);
65 
66 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
67 	r = ":";
68 	SM_TEST(sm_strlcat2(s3, r, s2, 2) == 7);
69 	r = "abc";
70 	SM_TEST(strcmp(s3, r) == 0);
71 
72 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
73 	r = ":";
74 	SM_TEST(sm_strlcat2(s3, r, s2, 4) == 7);
75 	r = "abc";
76 	SM_TEST(strcmp(s3, r) == 0);
77 
78 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
79 	r = ":";
80 	SM_TEST(sm_strlcat2(s3, r, s2, 5) == 7);
81 	r = "abc:";
82 	SM_TEST(strcmp(s3, r) == 0);
83 
84 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
85 	r = ":";
86 	SM_TEST(sm_strlcat2(s3, r, s2, 6) == 7);
87 	r = "abc:1";
88 	SM_TEST(strcmp(s3, r) == 0);
89 
90 	for (k = 0; k < N; k++)
91 	{
92 		(void) sm_strlcpy(src1[k], "abcdef", sizeof src1);
93 	}
94 
95 	one = sm_strlcpyn(dst1, sizeof dst1, 3, src1[0], "/", src1[1]);
96 	two = sm_snprintf(dst2, sizeof dst2, "%s/%s", src1[0], src1[1]);
97 	SM_TEST(one == two);
98 	SM_TEST(strcmp(dst1, dst2) == 0);
99 	one = sm_strlcpyn(dst1, 10, 3, src1[0], "/", src1[1]);
100 	two = sm_snprintf(dst2, 10, "%s/%s", src1[0], src1[1]);
101 	SM_TEST(one == two);
102 	SM_TEST(strcmp(dst1, dst2) == 0);
103 	one = sm_strlcpyn(dst1, 5, 3, src1[0], "/", src1[1]);
104 	two = sm_snprintf(dst2, 5, "%s/%s", src1[0], src1[1]);
105 	SM_TEST(one == two);
106 	SM_TEST(strcmp(dst1, dst2) == 0);
107 	one = sm_strlcpyn(dst1, 0, 3, src1[0], "/", src1[1]);
108 	two = sm_snprintf(dst2, 0, "%s/%s", src1[0], src1[1]);
109 	SM_TEST(one == two);
110 	SM_TEST(strcmp(dst1, dst2) == 0);
111 	one = sm_strlcpyn(dst1, sizeof dst1, 5, src1[0], "/", src1[1], "/", src1[2]);
112 	two = sm_snprintf(dst2, sizeof dst2, "%s/%s/%s", src1[0], src1[1], src1[2]);
113 	SM_TEST(one == two);
114 	SM_TEST(strcmp(dst1, dst2) == 0);
115 	one = sm_strlcpyn(dst1, 15, 5, src1[0], "/", src1[1], "/", src1[2]);
116 	two = sm_snprintf(dst2, 15, "%s/%s/%s", src1[0], src1[1], src1[2]);
117 	SM_TEST(one == two);
118 	SM_TEST(strcmp(dst1, dst2) == 0);
119 	one = sm_strlcpyn(dst1, 20, 5, src1[0], "/", src1[1], "/", src1[2]);
120 	two = sm_snprintf(dst2, 20, "%s/%s/%s", src1[0], src1[1], src1[2]);
121 	SM_TEST(one == two);
122 	SM_TEST(strcmp(dst1, dst2) == 0);
123 
124 	one = sm_strlcpyn(dst1, sizeof dst1, 0);
125 	SM_TEST(one == 0);
126 	r = "";
127 	SM_TEST(strcmp(dst1, r) == 0);
128 	one = sm_strlcpyn(dst1, 20, 1, src1[0]);
129 	two = sm_snprintf(dst2, 20, "%s", src1[0]);
130 	SM_TEST(one == two);
131 	one = sm_strlcpyn(dst1, 2, 1, src1[0]);
132 	two = sm_snprintf(dst2, 2, "%s", src1[0]);
133 	SM_TEST(one == two);
134 
135 	return sm_test_end();
136 }
137