VirtualBox

source: vbox/trunk/src/VBox/Debugger/testcase/tstDBGCParser.cpp@ 5686

Last change on this file since 5686 was 5686, checked in by vboxsync, 17 years ago

Got the DBGC parser testcase framework going. enough fun for now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.3 KB
Line 
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*******************************************************************************/
33static DECLCALLBACK(bool) tstDBGCBackInput(PDBGCBACK pBack, uint32_t cMillies);
34static DECLCALLBACK(int) tstDBGCBackRead(PDBGCBACK pBack, void *pvBuf, size_t cbBuf, size_t *pcbRead);
35static 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. */
42static unsigned g_cErrors = 0;
43/** The DBGC backend structure for use in this testcase. */
44static DBGCBACK g_tstBack =
45{
46 tstDBGCBackInput,
47 tstDBGCBackRead,
48 tstDBGCBackWrite
49};
50/** For keeping track of output prefixing. */
51static bool g_fPendingPrefix = true;
52/** Pointer to the the current input position. */
53const 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 */
65static 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 */
85static 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 */
114static 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 */
139static 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 */
153static 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
173int 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}
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