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