xref: /illumos-gate/usr/src/cmd/sendmail/libsm/t-strl.c (revision 7c478bd9)
1 /*
2  * Copyright (c) 2000-2001 Sendmail, 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
12 
13 #include <sm/gen.h>
14 SM_IDSTR(id, "@(#)$Id: t-strl.c,v 1.13 2001/08/27 23:00:05 gshapiro Exp $")
15 
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <sm/heap.h>
19 #include <sm/string.h>
20 #include <sm/test.h>
21 
22 #define MAXL	16
23 #define N	5
24 #define SIZE	128
25 
26 int
27 main(argc, argv)
28 	int argc;
29 	char *argv[];
30 {
31 	char *s1, *s2, *s3;
32 	int one, two, k;
33 	char src1[N][SIZE], dst1[SIZE], dst2[SIZE];
34 	char *r;
35 
36 	sm_test_begin(argc, argv, "test strl* string functions");
37 	s1 = "abc";
38 	s2 = "123";
39 	s3 = sm_malloc_x(MAXL);
40 
41 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
42 	SM_TEST(strcmp(s1, s3) == 0);
43 
44 	SM_TEST(sm_strlcat(s3, s2, 8) == 6);
45 	r ="abc123";
46 	SM_TEST(strcmp(s3, r) == 0);
47 
48 	SM_TEST(sm_strlcpy(s3, s1, 2) == 3);
49 	r = "a";
50 	SM_TEST(strcmp(s3, r) == 0);
51 
52 	SM_TEST(sm_strlcat(s3, s2, 3) == 4);
53 	r = "a1";
54 	SM_TEST(strcmp(s3, r) == 0);
55 
56 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
57 	r = ":";
58 	SM_TEST(sm_strlcat2(s3, r, s2, MAXL) == 7);
59 	r = "abc:123";
60 	SM_TEST(strcmp(s3, r) == 0);
61 
62 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
63 	r = ":";
64 	SM_TEST(sm_strlcat2(s3, r, s2, 6) == 7);
65 	r = "abc:1";
66 	SM_TEST(strcmp(s3, r) == 0);
67 
68 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
69 	r = ":";
70 	SM_TEST(sm_strlcat2(s3, r, s2, 2) == 7);
71 	r = "abc";
72 	SM_TEST(strcmp(s3, r) == 0);
73 
74 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
75 	r = ":";
76 	SM_TEST(sm_strlcat2(s3, r, s2, 4) == 7);
77 	r = "abc";
78 	SM_TEST(strcmp(s3, r) == 0);
79 
80 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
81 	r = ":";
82 	SM_TEST(sm_strlcat2(s3, r, s2, 5) == 7);
83 	r = "abc:";
84 	SM_TEST(strcmp(s3, r) == 0);
85 
86 	SM_TEST(sm_strlcpy(s3, s1, 4) == 3);
87 	r = ":";
88 	SM_TEST(sm_strlcat2(s3, r, s2, 6) == 7);
89 	r = "abc:1";
90 	SM_TEST(strcmp(s3, r) == 0);
91 
92 	for (k = 0; k < N; k++)
93 	{
94 		(void) sm_strlcpy(src1[k], "abcdef", sizeof src1);
95 	}
96 
97 	one = sm_strlcpyn(dst1, sizeof dst1, 3, src1[0], "/", src1[1]);
98 	two = sm_snprintf(dst2, sizeof dst2, "%s/%s", src1[0], src1[1]);
99 	SM_TEST(one == two);
100 	SM_TEST(strcmp(dst1, dst2) == 0);
101 	one = sm_strlcpyn(dst1, 10, 3, src1[0], "/", src1[1]);
102 	two = sm_snprintf(dst2, 10, "%s/%s", src1[0], src1[1]);
103 	SM_TEST(one == two);
104 	SM_TEST(strcmp(dst1, dst2) == 0);
105 	one = sm_strlcpyn(dst1, 5, 3, src1[0], "/", src1[1]);
106 	two = sm_snprintf(dst2, 5, "%s/%s", src1[0], src1[1]);
107 	SM_TEST(one == two);
108 	SM_TEST(strcmp(dst1, dst2) == 0);
109 	one = sm_strlcpyn(dst1, 0, 3, src1[0], "/", src1[1]);
110 	two = sm_snprintf(dst2, 0, "%s/%s", src1[0], src1[1]);
111 	SM_TEST(one == two);
112 	SM_TEST(strcmp(dst1, dst2) == 0);
113 	one = sm_strlcpyn(dst1, sizeof dst1, 5, src1[0], "/", src1[1], "/", src1[2]);
114 	two = sm_snprintf(dst2, sizeof dst2, "%s/%s/%s", src1[0], src1[1], src1[2]);
115 	SM_TEST(one == two);
116 	SM_TEST(strcmp(dst1, dst2) == 0);
117 	one = sm_strlcpyn(dst1, 15, 5, src1[0], "/", src1[1], "/", src1[2]);
118 	two = sm_snprintf(dst2, 15, "%s/%s/%s", src1[0], src1[1], src1[2]);
119 	SM_TEST(one == two);
120 	SM_TEST(strcmp(dst1, dst2) == 0);
121 	one = sm_strlcpyn(dst1, 20, 5, src1[0], "/", src1[1], "/", src1[2]);
122 	two = sm_snprintf(dst2, 20, "%s/%s/%s", src1[0], src1[1], src1[2]);
123 	SM_TEST(one == two);
124 	SM_TEST(strcmp(dst1, dst2) == 0);
125 
126 	one = sm_strlcpyn(dst1, sizeof dst1, 0);
127 	SM_TEST(one == 0);
128 	r = "";
129 	SM_TEST(strcmp(dst1, r) == 0);
130 	one = sm_strlcpyn(dst1, 20, 1, src1[0]);
131 	two = sm_snprintf(dst2, 20, "%s", src1[0]);
132 	SM_TEST(one == two);
133 	one = sm_strlcpyn(dst1, 2, 1, src1[0]);
134 	two = sm_snprintf(dst2, 2, "%s", src1[0]);
135 	SM_TEST(one == two);
136 
137 	return sm_test_end();
138 }
139