VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTGetOptArgv.cpp@ 27968

Last change on this file since 27968 was 27968, checked in by vboxsync, 15 years ago

RTGetOptArgvToString: Implemented bourne shell style quoting.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.1 KB
Line 
1/* $Id: tstRTGetOptArgv.cpp 27968 2010-04-02 20:36:35Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTGetOptArgv*.
4 */
5
6/*
7 * Copyright (C) 2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/path.h>
35
36#include <iprt/err.h>
37#include <iprt/param.h>
38#include <iprt/getopt.h>
39#include <iprt/string.h>
40#include <iprt/test.h>
41
42
43static void tst2(void)
44{
45 RTTestISub("RTGetOptArgvToString / MS_CRT");
46
47 static const struct
48 {
49 const char * const apszArgs[5];
50 const char *pszCmdLine;
51 } s_aMscCrtTests[] =
52 {
53 {
54 { "abcd", "a ", " b", " c ", NULL },
55 "abcd \"a \" \" b\" \" c \""
56 },
57 {
58 { "a\\\\\\b", "de fg", "h", NULL, NULL },
59 "a\\\\\\b \"de fg\" h"
60 },
61 {
62 { "a\\\"b", "c", "d", "\"", NULL },
63 "\"a\\\\\\\"b\" c d \"\\\"\""
64 },
65 {
66 { "a\\\\b c", "d", "e", " \\", NULL },
67 "\"a\\\\b c\" d e \" \\\\\""
68 },
69 };
70
71 for (size_t i = 0; i < RT_ELEMENTS(s_aMscCrtTests); i++)
72 {
73 char *pszCmdLine = NULL;
74 int rc = RTGetOptArgvToString(&pszCmdLine, s_aMscCrtTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
75 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
76 if (strcmp(s_aMscCrtTests[i].pszCmdLine, pszCmdLine))
77 RTTestIFailed("g_aTest[%i] failed:\n"
78 " got '%s'\n"
79 " expected '%s'\n",
80 i, pszCmdLine, s_aMscCrtTests[i].pszCmdLine);
81 RTStrFree(pszCmdLine);
82 }
83
84
85 RTTestISub("RTGetOptArgvToString / BOURNE_SH");
86
87 static const struct
88 {
89 const char * const apszArgs[5];
90 const char *pszCmdLine;
91 } s_aBournShTests[] =
92 {
93 {
94 { "abcd", "a ", " b", " c ", NULL },
95 "abcd 'a ' ' b' ' c '"
96 },
97 {
98 { "a\n\\b", "de'fg", "h", "'", NULL },
99 "'a\n\\b' 'de'\"'\"'fg' h ''\"'\"''"
100 }
101 };
102
103 for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
104 {
105 char *pszCmdLine = NULL;
106 int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
107 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
108 if (strcmp(s_aBournShTests[i].pszCmdLine, pszCmdLine))
109 RTTestIFailed("g_aTest[%i] failed:\n"
110 " got |%s|\n"
111 " expected |%s|\n",
112 i, pszCmdLine, s_aBournShTests[i].pszCmdLine);
113 RTStrFree(pszCmdLine);
114 }
115
116
117 RTTestISub("RTGetOptArgvToString <-> RTGetOptArgvFromString");
118
119 for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
120 {
121 char *pszCmdLine = NULL;
122 int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
123 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
124
125 char **papszArgs;
126 int cArgs;
127 rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszCmdLine, NULL);
128 RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
129
130 size_t j = 0;
131 while (papszArgs[j] && s_aBournShTests[i].apszArgs[j])
132 {
133 if (strcmp(papszArgs[j], s_aBournShTests[i].apszArgs[j]))
134 RTTestIFailed("Test #%u, argument #%u mismatch:\n"
135 " FromString: |%s| (got)\n"
136 " ToString: |%s| (expected)\n",
137 i, j, papszArgs[j], s_aBournShTests[i].apszArgs[j]);
138
139 /* next */
140 j++;
141 }
142 RTTESTI_CHECK(papszArgs[j] == NULL);
143 RTTESTI_CHECK(s_aBournShTests[i].apszArgs[j] == NULL);
144
145 RTGetOptArgvFree(papszArgs);
146 RTStrFree(pszCmdLine);
147 }
148}
149
150static void tst1(void)
151{
152 RTTestISub("RTGetOptArgvFromString");
153 char **papszArgs = NULL;
154 int cArgs = -1;
155 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "", NULL), VINF_SUCCESS);
156 RTTESTI_CHECK_RETV(cArgs == 0);
157 RTTESTI_CHECK_RETV(papszArgs);
158 RTTESTI_CHECK_RETV(!papszArgs[0]);
159 RTGetOptArgvFree(papszArgs);
160
161 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0 1 \"\"2'' '3' 4 5 '''''6' 7 8 9 10 11", NULL), VINF_SUCCESS);
162 RTTESTI_CHECK_RETV(cArgs == 12);
163 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0"));
164 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "1"));
165 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "2"));
166 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "3"));
167 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "4"));
168 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "5"));
169 RTTESTI_CHECK_RETV(!strcmp(papszArgs[6], "6"));
170 RTTESTI_CHECK_RETV(!strcmp(papszArgs[7], "7"));
171 RTTESTI_CHECK_RETV(!strcmp(papszArgs[8], "8"));
172 RTTESTI_CHECK_RETV(!strcmp(papszArgs[9], "9"));
173 RTTESTI_CHECK_RETV(!strcmp(papszArgs[10], "10"));
174 RTTESTI_CHECK_RETV(!strcmp(papszArgs[11], "11"));
175 RTTESTI_CHECK_RETV(!papszArgs[12]);
176 RTGetOptArgvFree(papszArgs);
177
178 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "\t\" asdf \" '\"'xyz \"\t\" '\n' '\"' \"'\"\n\r ", NULL), VINF_SUCCESS);
179 RTTESTI_CHECK_RETV(cArgs == 6);
180 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], " asdf "));
181 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "\"xyz"));
182 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "\t"));
183 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "\n"));
184 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "\""));
185 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "\'"));
186 RTTESTI_CHECK_RETV(!papszArgs[6]);
187 RTGetOptArgvFree(papszArgs);
188
189 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, ":0::1::::2:3:4:5:", ":"), VINF_SUCCESS);
190 RTTESTI_CHECK_RETV(cArgs == 6);
191 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0"));
192 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "1"));
193 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "2"));
194 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "3"));
195 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "4"));
196 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "5"));
197 RTTESTI_CHECK_RETV(!papszArgs[6]);
198 RTGetOptArgvFree(papszArgs);
199
200 RTTESTI_CHECK_RC_RETV(RTGetOptArgvFromString(&papszArgs, &cArgs, "0:1;2:3;4:5", ";;;;;;;;;;;;;;;;;;;;;;:"), VINF_SUCCESS);
201 RTTESTI_CHECK_RETV(cArgs == 6);
202 RTTESTI_CHECK_RETV(!strcmp(papszArgs[0], "0"));
203 RTTESTI_CHECK_RETV(!strcmp(papszArgs[1], "1"));
204 RTTESTI_CHECK_RETV(!strcmp(papszArgs[2], "2"));
205 RTTESTI_CHECK_RETV(!strcmp(papszArgs[3], "3"));
206 RTTESTI_CHECK_RETV(!strcmp(papszArgs[4], "4"));
207 RTTESTI_CHECK_RETV(!strcmp(papszArgs[5], "5"));
208 RTTESTI_CHECK_RETV(!papszArgs[6]);
209 RTGetOptArgvFree(papszArgs);
210}
211
212int main()
213{
214 /*
215 * Init RT+Test.
216 */
217 RTTEST hTest;
218 int rc = RTTestInitAndCreate("tstRTGetOptArgv", &hTest);
219 if (rc)
220 return rc;
221 RTTestBanner(hTest);
222
223 /*
224 * The test.
225 */
226 tst1();
227 tst2();
228
229 /*
230 * Summary.
231 */
232 return RTTestSummaryAndDestroy(hTest);
233}
234
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