VirtualBox

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

最後變更 在這個檔案從5999是 5999,由 vboxsync 提交於 17 年 前

The Giant CDDL Dual-License Header Change.

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

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