1 | /* $Id: tstRTStrCatCopy.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - String Concatenation and Copy.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/string.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/test.h>
|
---|
45 |
|
---|
46 |
|
---|
47 | static void testCopy1(RTTEST hTest)
|
---|
48 | {
|
---|
49 | RTTestISub("RTStrCopy");
|
---|
50 |
|
---|
51 | char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
|
---|
52 | char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
|
---|
53 | RTTESTI_CHECK_RC(RTStrCopy(pszBuf4H, 4, "abc"), VINF_SUCCESS);
|
---|
54 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
55 | RTTESTI_CHECK_RC(RTStrCopy(pszBuf4T, 4, "abc"), VINF_SUCCESS);
|
---|
56 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
57 |
|
---|
58 | RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
|
---|
59 | RTTESTI_CHECK_RC(RTStrCopy(pszBuf4H, 4, "abcd"), VERR_BUFFER_OVERFLOW);
|
---|
60 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
61 | RTTESTI_CHECK_RC(RTStrCopy(pszBuf4T, 4, "abcd"), VERR_BUFFER_OVERFLOW);
|
---|
62 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | static void testCopyEx1(RTTEST hTest)
|
---|
67 | {
|
---|
68 | RTTestISub("RTStrCopyEx");
|
---|
69 |
|
---|
70 | char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
|
---|
71 | char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
|
---|
72 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
|
---|
73 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
74 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
|
---|
75 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
76 |
|
---|
77 | RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
|
---|
78 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abcd", RTSTR_MAX), VERR_BUFFER_OVERFLOW);
|
---|
79 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
80 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abcd", RTSTR_MAX), VERR_BUFFER_OVERFLOW);
|
---|
81 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
82 |
|
---|
83 | RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
|
---|
84 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abcd", 3), VINF_SUCCESS);
|
---|
85 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
86 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abcd", 3), VINF_SUCCESS);
|
---|
87 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
88 |
|
---|
89 | RT_BZERO(pszBuf4H, 4); RT_BZERO(pszBuf4T, 4);
|
---|
90 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4H, 4, "abcd", 2), VINF_SUCCESS);
|
---|
91 | RTTESTI_CHECK(strcmp(pszBuf4H, "ab") == 0);
|
---|
92 | RTTESTI_CHECK_RC(RTStrCopyEx(pszBuf4T, 4, "abcd", 2), VINF_SUCCESS);
|
---|
93 | RTTESTI_CHECK(strcmp(pszBuf4T, "ab") == 0);
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static void testCat1(RTTEST hTest)
|
---|
98 | {
|
---|
99 | RTTestISub("RTStrCat");
|
---|
100 |
|
---|
101 | char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
|
---|
102 | char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
|
---|
103 | memset(pszBuf4T, 0xff, 4); *pszBuf4T = '\0';
|
---|
104 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "abc"), VINF_SUCCESS);
|
---|
105 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
106 | memset(pszBuf4H, 0xff, 4); *pszBuf4H = '\0';
|
---|
107 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "abc"), VINF_SUCCESS);
|
---|
108 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
109 |
|
---|
110 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "a");
|
---|
111 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "a");
|
---|
112 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "bc"), VINF_SUCCESS);
|
---|
113 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
114 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "bc"), VINF_SUCCESS);
|
---|
115 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
116 |
|
---|
117 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
|
---|
118 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
|
---|
119 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "c"), VINF_SUCCESS);
|
---|
120 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
121 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "c"), VINF_SUCCESS);
|
---|
122 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
123 |
|
---|
124 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
|
---|
125 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
|
---|
126 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, ""), VINF_SUCCESS);
|
---|
127 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
128 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, ""), VINF_SUCCESS);
|
---|
129 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
130 |
|
---|
131 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "");
|
---|
132 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "");
|
---|
133 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "abcd"), VERR_BUFFER_OVERFLOW);
|
---|
134 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
135 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "abcd"), VERR_BUFFER_OVERFLOW);
|
---|
136 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
137 |
|
---|
138 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
|
---|
139 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
|
---|
140 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "cd"), VERR_BUFFER_OVERFLOW);
|
---|
141 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
142 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "cd"), VERR_BUFFER_OVERFLOW);
|
---|
143 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
144 |
|
---|
145 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
|
---|
146 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
|
---|
147 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4H, 4, "d"), VERR_BUFFER_OVERFLOW);
|
---|
148 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
149 | RTTESTI_CHECK_RC(RTStrCat(pszBuf4T, 4, "d"), VERR_BUFFER_OVERFLOW);
|
---|
150 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 | static void testCatEx1(RTTEST hTest)
|
---|
155 | {
|
---|
156 | RTTestISub("RTStrCatEx");
|
---|
157 |
|
---|
158 | char *pszBuf4H = (char *)RTTestGuardedAllocHead(hTest, 4);
|
---|
159 | char *pszBuf4T = (char *)RTTestGuardedAllocTail(hTest, 4);
|
---|
160 | memset(pszBuf4T, 0xff, 4); *pszBuf4T = '\0';
|
---|
161 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
|
---|
162 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
163 | memset(pszBuf4H, 0xff, 4); *pszBuf4H = '\0';
|
---|
164 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "abc", RTSTR_MAX), VINF_SUCCESS);
|
---|
165 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
166 |
|
---|
167 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "a");
|
---|
168 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "a");
|
---|
169 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "bc", 2), VINF_SUCCESS);
|
---|
170 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
171 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "bc", 2), VINF_SUCCESS);
|
---|
172 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
173 |
|
---|
174 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
|
---|
175 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
|
---|
176 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "c", 1), VINF_SUCCESS);
|
---|
177 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
178 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "c", 1), VINF_SUCCESS);
|
---|
179 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
180 |
|
---|
181 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
|
---|
182 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
|
---|
183 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "defg", 0), VINF_SUCCESS);
|
---|
184 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
185 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "defg", 0), VINF_SUCCESS);
|
---|
186 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
187 |
|
---|
188 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "");
|
---|
189 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "");
|
---|
190 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "abcd", 4), VERR_BUFFER_OVERFLOW);
|
---|
191 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
192 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "abcd", 4), VERR_BUFFER_OVERFLOW);
|
---|
193 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
194 |
|
---|
195 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "ab");
|
---|
196 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "ab");
|
---|
197 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "cdefg", 2), VERR_BUFFER_OVERFLOW);
|
---|
198 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
199 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "cdefg", 2), VERR_BUFFER_OVERFLOW);
|
---|
200 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
201 |
|
---|
202 | memset(pszBuf4T, 0xff, 4); strcpy(pszBuf4T, "abc");
|
---|
203 | memset(pszBuf4H, 0xff, 4); strcpy(pszBuf4H, "abc");
|
---|
204 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4H, 4, "de", 1), VERR_BUFFER_OVERFLOW);
|
---|
205 | RTTESTI_CHECK(strcmp(pszBuf4H, "abc") == 0);
|
---|
206 | RTTESTI_CHECK_RC(RTStrCatEx(pszBuf4T, 4, "de", 1), VERR_BUFFER_OVERFLOW);
|
---|
207 | RTTESTI_CHECK(strcmp(pszBuf4T, "abc") == 0);
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 |
|
---|
212 | int main()
|
---|
213 | {
|
---|
214 | RTTEST hTest;
|
---|
215 | RTEXITCODE rcExit = RTTestInitAndCreate("tstRTStrCatCopy", &hTest);
|
---|
216 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
217 | return rcExit;
|
---|
218 |
|
---|
219 | testCopy1(hTest);
|
---|
220 | testCopyEx1(hTest);
|
---|
221 | testCat1(hTest);
|
---|
222 | testCatEx1(hTest);
|
---|
223 |
|
---|
224 | return RTTestSummaryAndDestroy(hTest);
|
---|
225 | }
|
---|