VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTStrCatCopy.cpp@ 83745

Last change on this file since 83745 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette