1 | /* $Id: tstDBGCParser.cpp 8155 2008-04-18 15:16:47Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * DBGC Testcase - Command Parser.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | /*******************************************************************************
|
---|
23 | * Header Files *
|
---|
24 | *******************************************************************************/
|
---|
25 | #include <VBox/dbg.h>
|
---|
26 | #include "../DBGCInternal.h"
|
---|
27 |
|
---|
28 | #include <iprt/stream.h>
|
---|
29 | #include <iprt/string.h>
|
---|
30 | #include <iprt/initterm.h>
|
---|
31 |
|
---|
32 |
|
---|
33 | /*******************************************************************************
|
---|
34 | * Internal Functions *
|
---|
35 | *******************************************************************************/
|
---|
36 | static DECLCALLBACK(bool) tstDBGCBackInput(PDBGCBACK pBack, uint32_t cMillies);
|
---|
37 | static DECLCALLBACK(int) tstDBGCBackRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead);
|
---|
38 | static DECLCALLBACK(int) tstDBGCBackWrite(PDBGCBACK pBack, const void *pvBuf, size_t cbBuf, size_t *pcbWritten);
|
---|
39 |
|
---|
40 |
|
---|
41 | /*******************************************************************************
|
---|
42 | * Global Variables *
|
---|
43 | *******************************************************************************/
|
---|
44 | /** Global error counter. */
|
---|
45 | static unsigned g_cErrors = 0;
|
---|
46 | /** The DBGC backend structure for use in this testcase. */
|
---|
47 | static DBGCBACK g_tstBack =
|
---|
48 | {
|
---|
49 | tstDBGCBackInput,
|
---|
50 | tstDBGCBackRead,
|
---|
51 | tstDBGCBackWrite
|
---|
52 | };
|
---|
53 | /** For keeping track of output prefixing. */
|
---|
54 | static bool g_fPendingPrefix = true;
|
---|
55 | /** Pointer to the the current input position. */
|
---|
56 | const char *g_pszInput = NULL;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Checks if there is input.
|
---|
60 | *
|
---|
61 | * @returns true if there is input ready.
|
---|
62 | * @returns false if there not input ready.
|
---|
63 | * @param pBack Pointer to the backend structure supplied by
|
---|
64 | * the backend. The backend can use this to find
|
---|
65 | * it's instance data.
|
---|
66 | * @param cMillies Number of milliseconds to wait on input data.
|
---|
67 | */
|
---|
68 | static DECLCALLBACK(bool) tstDBGCBackInput(PDBGCBACK pBack, uint32_t cMillies)
|
---|
69 | {
|
---|
70 | return g_pszInput != NULL
|
---|
71 | && *g_pszInput != '\0';
|
---|
72 | }
|
---|
73 |
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Read input.
|
---|
77 | *
|
---|
78 | * @returns VBox status code.
|
---|
79 | * @param pBack Pointer to the backend structure supplied by
|
---|
80 | * the backend. The backend can use this to find
|
---|
81 | * it's instance data.
|
---|
82 | * @param pvBuf Where to put the bytes we read.
|
---|
83 | * @param cbBuf Maximum nymber of bytes to read.
|
---|
84 | * @param pcbRead Where to store the number of bytes actually read.
|
---|
85 | * If NULL the entire buffer must be filled for a
|
---|
86 | * successful return.
|
---|
87 | */
|
---|
88 | static DECLCALLBACK(int) tstDBGCBackRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead)
|
---|
89 | {
|
---|
90 | if (g_pszInput && *g_pszInput)
|
---|
91 | {
|
---|
92 | size_t cb = strlen(g_pszInput);
|
---|
93 | if (cb > cbBuf)
|
---|
94 | cb = cbBuf;
|
---|
95 | *pcbRead = cb;
|
---|
96 | memcpy(pvBuf, g_pszInput, cb);
|
---|
97 | g_pszInput += cb;
|
---|
98 | }
|
---|
99 | else
|
---|
100 | *pcbRead = 0;
|
---|
101 | return VINF_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Write (output).
|
---|
107 | *
|
---|
108 | * @returns VBox status code.
|
---|
109 | * @param pBack Pointer to the backend structure supplied by
|
---|
110 | * the backend. The backend can use this to find
|
---|
111 | * it's instance data.
|
---|
112 | * @param pvBuf What to write.
|
---|
113 | * @param cbBuf Number of bytes to write.
|
---|
114 | * @param pcbWritten Where to store the number of bytes actually written.
|
---|
115 | * If NULL the entire buffer must be successfully written.
|
---|
116 | */
|
---|
117 | static DECLCALLBACK(int) tstDBGCBackWrite(PDBGCBACK pBack, const void *pvBuf, size_t cbBuf, size_t *pcbWritten)
|
---|
118 | {
|
---|
119 | const char *pch = (const char *)pvBuf;
|
---|
120 | if (pcbWritten)
|
---|
121 | *pcbWritten = cbBuf;
|
---|
122 | while (cbBuf-- > 0)
|
---|
123 | {
|
---|
124 | if (g_fPendingPrefix)
|
---|
125 | {
|
---|
126 | RTPrintf("tstDBGCParser: OUTPUT: ");
|
---|
127 | g_fPendingPrefix = false;
|
---|
128 | }
|
---|
129 | if (*pch == '\n')
|
---|
130 | g_fPendingPrefix = true;
|
---|
131 | RTPrintf("%c", *pch);
|
---|
132 | pch++;
|
---|
133 | }
|
---|
134 | return VINF_SUCCESS;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Completes the output, making sure that we're in
|
---|
140 | * the 1 position of a new line.
|
---|
141 | */
|
---|
142 | static void tstCompleteOutput(void)
|
---|
143 | {
|
---|
144 | if (!g_fPendingPrefix)
|
---|
145 | RTPrintf("\n");
|
---|
146 | g_fPendingPrefix = true;
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Tries one command string.
|
---|
152 | * @param pDbgc Pointer to the debugger instance.
|
---|
153 | * @param pszCmds The command to test.
|
---|
154 | * @param rcCmd The expected result.
|
---|
155 | */
|
---|
156 | static void tstTry(PDBGC pDbgc, const char *pszCmds, int rcCmd)
|
---|
157 | {
|
---|
158 | g_pszInput = pszCmds;
|
---|
159 | if (strchr(pszCmds, '\0')[-1] == '\n')
|
---|
160 | RTPrintf("tstDBGCParser: RUNNING: %s", pszCmds);
|
---|
161 | else
|
---|
162 | RTPrintf("tstDBGCParser: RUNNING: %s\n", pszCmds);
|
---|
163 |
|
---|
164 | pDbgc->rcCmd = VERR_INTERNAL_ERROR;
|
---|
165 | dbgcProcessInput(pDbgc, true /* fNoExecute */);
|
---|
166 | tstCompleteOutput();
|
---|
167 |
|
---|
168 | if (pDbgc->rcCmd != rcCmd)
|
---|
169 | {
|
---|
170 | RTPrintf("tstDBGCParser: rcCmd=%Rrc expected =%Rrc\n", pDbgc->rcCmd, rcCmd);
|
---|
171 | g_cErrors++;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 | int main()
|
---|
177 | {
|
---|
178 | /*
|
---|
179 | * Init.
|
---|
180 | */
|
---|
181 | RTR3Init();
|
---|
182 | RTPrintf("tstDBGCParser: TESTING...\n");
|
---|
183 |
|
---|
184 | /*
|
---|
185 | * Create a DBGC instance.
|
---|
186 | */
|
---|
187 | PDBGC pDbgc;
|
---|
188 | int rc = dbgcCreate(&pDbgc, &g_tstBack, 0);
|
---|
189 | if (RT_SUCCESS(rc))
|
---|
190 | {
|
---|
191 | rc = dbgcProcessInput(pDbgc, true /* fNoExecute */);
|
---|
192 | tstCompleteOutput();
|
---|
193 | if (RT_SUCCESS(rc))
|
---|
194 | {
|
---|
195 | tstTry(pDbgc, "stop\n", VINF_SUCCESS);
|
---|
196 | tstTry(pDbgc, "format \n", VERR_PARSE_TOO_FEW_ARGUMENTS);
|
---|
197 | tstTry(pDbgc, "format 0 1 23 4\n", VERR_PARSE_TOO_MANY_ARGUMENTS);
|
---|
198 | tstTry(pDbgc, "sa 3 23 4 'q' \"21123123\" 'b' \n", VINF_SUCCESS);
|
---|
199 | }
|
---|
200 |
|
---|
201 | dbgcDestroy(pDbgc);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Summary
|
---|
206 | */
|
---|
207 | if (!g_cErrors)
|
---|
208 | RTPrintf("tstDBGCParser: SUCCESS\n");
|
---|
209 | else
|
---|
210 | RTPrintf("tstDBGCParser: FAILURE - %d errors\n", g_cErrors);
|
---|
211 | return g_cErrors != 0;
|
---|
212 | }
|
---|