-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mi_integration.d
More file actions
478 lines (415 loc) · 11.6 KB
/
Copy pathtest_mi_integration.d
File metadata and controls
478 lines (415 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/// Automated GDB/MI integration tests.
///
/// Spawns aliceserver with -i mi, sends MI commands, and asserts on responses.
/// Run with: rdmd test_mi_integration.d
///
/// Authors: dd86k <dd@dax.moe>
/// Copyright: dd86k <dd@dax.moe>
/// License: BSD-3-Clause-Clear
module test_mi_integration;
import std;
import core.thread;
version (Windows)
{
immutable string defaultServer = "aliceserver.exe";
}
else
{
immutable string defaultServer = "./aliceserver";
}
enum Op : char
{
trace = '@',
info = '~',
important = '*',
warn = '?',
error = '!',
sending = '>',
receiving = '<',
}
__gshared
{
ProcessPipes server;
bool verbose;
bool isGdb; // true when testing against real GDB
}
void log(A...)(char op, string fmt, A args)
{
if (verbose == false)
switch (op) {
case Op.trace, Op.receiving, Op.sending: return;
default:
}
stderr.write("TEST[", op, "]: ");
stderr.writefln(fmt, args);
}
struct MIResponse
{
string[] lines; // all raw lines (including ~, &, = prefixed)
string resultLine; // the ^... line (with token prefix if any)
string resultClass; // "done", "error", "exit", "running"
string resultBody; // everything after ^class, or ^class,
}
/// Read lines from server stdout until "(gdb)" prompt.
string[] readUntilPrompt()
{
string[] lines;
while (true)
{
string line = stripRight(server.stdout.readln());
log(Op.receiving, "%s", line);
if (line is null)
break; // EOF
if (line == "(gdb)")
break;
if (line.length > 0)
lines ~= line;
}
return lines;
}
/// Send a command and collect the response.
MIResponse send(string command)
{
log(Op.sending, "%s", command);
server.stdin.write(command, '\n');
server.stdin.flush();
string[] lines = readUntilPrompt();
MIResponse resp;
resp.lines = lines;
// Find the result record (line starting with optional digits then ^)
foreach (line; lines)
{
// Skip stream/async records
if (line.length == 0)
continue;
// Find ^ in the line - it could be preceded by token digits
auto caretIdx = line.indexOf('^');
if (caretIdx < 0)
continue;
// Verify everything before ^ is digits (token)
bool isResult = true;
foreach (ch; line[0 .. caretIdx])
{
if (!ch.isDigit)
{
isResult = false;
break;
}
}
if (!isResult)
continue;
resp.resultLine = line;
// Parse result class and body from after ^
string afterCaret = line[caretIdx + 1 .. $];
auto commaIdx = afterCaret.indexOf(',');
if (commaIdx >= 0)
{
resp.resultClass = afterCaret[0 .. commaIdx];
resp.resultBody = afterCaret[commaIdx + 1 .. $];
}
else
{
resp.resultClass = afterCaret;
resp.resultBody = "";
}
break;
}
return resp;
}
// --- Test infrastructure ---
struct TestResult
{
string name;
bool passed;
string failMsg;
}
TestResult[] results;
int testsPassed;
int testsFailed;
alias TestFunc = void function();
struct TestEntry
{
string name;
TestFunc func;
}
TestEntry[] allTests;
void registerTest(string name, TestFunc func)
{
allTests ~= TestEntry(name, func);
}
void runTest(TestEntry test)
{
try
{
test.func();
writefln("[PASS] %s", test.name);
results ~= TestResult(test.name, true, "");
testsPassed++;
}
catch (Exception e)
{
writefln("[FAIL] %s: %s", test.name, e.msg);
results ~= TestResult(test.name, false, e.msg);
testsFailed++;
}
}
void assertDone(MIResponse resp, string context = "")
{
if (resp.resultClass != "done")
throw new Exception(
format(`expected "^done" but got "^%s"%s`,
resp.resultClass,
context.length ? " (" ~ context ~ ")" : ""));
}
void assertError(MIResponse resp, string msgSubstring = "")
{
if (resp.resultClass != "error")
throw new Exception(
format(`expected "^error" but got "^%s"`, resp.resultClass));
if (msgSubstring.length > 0 && !resp.resultBody.canFind(msgSubstring))
throw new Exception(
format(`error body missing "%s", got: %s`, msgSubstring, resp.resultBody));
}
void assertResult(MIResponse resp, string expectedClass)
{
if (resp.resultClass != expectedClass)
throw new Exception(
format(`expected "^%s" but got "^%s"`, expectedClass, resp.resultClass));
}
void assertLinesContain(MIResponse resp, string substring)
{
foreach (line; resp.lines)
if (line.canFind(substring))
return;
throw new Exception(
format(`no line contains "%s"`, substring));
}
// --- Test definitions ---
static this()
{
registerTest("show_version", &test_show_version);
registerTest("environment_pwd", &test_environment_pwd);
registerTest("info_cmd_exists", &test_info_cmd_exists);
registerTest("info_cmd_nonexistent", &test_info_cmd_nonexistent);
registerTest("list_features", &test_list_features);
registerTest("file_exec_and_symbols", &test_file_exec_and_symbols);
registerTest("file_exec_and_symbols_error", &test_file_exec_and_symbols_error);
registerTest("exec_arguments", &test_exec_arguments);
registerTest("gdb_set", &test_gdb_set);
registerTest("unknown_command", &test_unknown_command);
registerTest("empty_command", &test_empty_command);
registerTest("dash_only", &test_dash_only);
registerTest("token_handling", &test_token_handling);
registerTest("gdb_exit", &test_gdb_exit); // must be last
}
void test_show_version()
{
auto resp = send("show version");
assertDone(resp);
// aliceserver: ~"GNU gdb compatible Aliceserver ...", GDB: ~"GNU gdb ..."
assertLinesContain(resp, `~"GNU gdb`);
}
void test_environment_pwd()
{
auto resp = send("-environment-pwd");
assertDone(resp);
if (!resp.resultBody.canFind("cwd="))
throw new Exception(
format(`body missing "cwd=", got: %s`, resp.resultBody));
}
void test_info_cmd_exists()
{
// Use list-features: a real MI command recognized by both GDB and aliceserver
auto resp = send("-info-gdb-mi-command list-features");
assertDone(resp);
if (!resp.resultBody.canFind(`exists="true"`))
throw new Exception(
format(`body missing exists="true", got: %s`, resp.resultBody));
}
void test_info_cmd_nonexistent()
{
auto resp = send("-info-gdb-mi-command nonexistent");
assertDone(resp);
if (!resp.resultBody.canFind(`exists="false"`))
throw new Exception(
format(`body missing exists="false", got: %s`, resp.resultBody));
}
void test_list_features()
{
auto resp = send("-list-features");
assertDone(resp);
if (!resp.resultBody.canFind("features="))
throw new Exception(
format(`body missing "features=", got: %s`, resp.resultBody));
}
void test_file_exec_and_symbols()
{
auto resp = send("-file-exec-and-symbols /bin/true");
assertDone(resp);
}
void test_file_exec_and_symbols_error()
{
auto resp = send("-file-exec-and-symbols a b");
// GDB: only processes first arg, errors "No such file"
// aliceserver: rejects multiple args
if (isGdb)
assertError(resp, "No such file");
else
assertError(resp, "Unrecognized argument");
}
void test_exec_arguments()
{
auto resp = send("-exec-arguments arg1 arg2");
assertDone(resp);
}
void test_gdb_set()
{
// GDB errors on unknown set variables; aliceserver accepts silently
auto resp = send("-gdb-set something");
if (isGdb)
assertError(resp);
else
assertDone(resp);
}
void test_unknown_command()
{
auto resp = send("-blah-nonexistent");
// GDB: "Undefined MI command", aliceserver: "Unknown request"
if (isGdb)
assertError(resp, "Undefined MI command");
else
assertError(resp, "Unknown request");
}
void test_empty_command()
{
auto resp = send("");
assertDone(resp);
}
void test_dash_only()
{
auto resp = send("-");
// GDB: ^error (undefined MI command ""), aliceserver: ^done
if (isGdb)
assertError(resp);
else
assertDone(resp);
}
void test_token_handling()
{
auto resp = send("123-list-features");
assertDone(resp);
if (!resp.resultLine.startsWith("123^done"))
throw new Exception(
format(`result line should start with "123^done", got: %s`, resp.resultLine));
}
void test_gdb_exit()
{
auto resp = send("-gdb-exit");
assertResult(resp, "exit");
}
// --- Main ---
int main(string[] args)
{
string oserver;
string otestFilter;
bool olist;
GetoptResult ores;
try
{
ores = getopt(args,
"s|server", "Server path (default=./aliceserver)", &oserver,
"t|test", "Run specific test by name", &otestFilter,
"l|list", "List test names", &olist,
"v|verbose", "Show protocol traffic", &verbose,
);
}
catch (Exception ex)
{
stderr.writefln("Error: %s", ex.msg);
return 1;
}
if (ores.helpWanted)
{
defaultGetoptPrinter(
`MI integration tests for aliceserver
OPTIONS`, ores.options);
return 0;
}
if (olist)
{
foreach (test; allTests)
writeln(test.name);
return 0;
}
// Resolve server and build launch args
string[] svropts;
switch (oserver) {
case "gdb":
svropts = ["gdb", "-i", "mi", "--quiet"];
isGdb = true;
break;
case "":
// Auto-build if needed
if (!exists(defaultServer))
{
log(Op.important, "Server not found locally, building...");
int code = wait(spawnProcess(["dub", "build"]));
if (code)
{
log(Op.error, "Compilation ended in error, aborting");
return code;
}
}
svropts = [defaultServer, "-i", "mi"];
break;
default:
svropts = [oserver];
}
// Spawn server
log(Op.info, "Starting server: %s", svropts.join(" "));
server = pipeProcess(svropts, Redirect.stdin | Redirect.stdout);
Thread.sleep(250.msecs);
if (tryWait(server.pid).terminated)
{
log(Op.error, "Could not launch server");
return 2;
}
// Read initial (gdb) prompt
// This is because GDB without -q tend to send a wall of text
readUntilPrompt();
// Filter tests if requested
TestEntry[] testsToRun;
if (otestFilter.length)
{
foreach (test; allTests)
if (test.name == otestFilter)
testsToRun ~= test;
if (testsToRun.length == 0)
{
stderr.writefln("Unknown test: %s", otestFilter);
// Still need to clean up server
server.stdin.write("-gdb-exit\n");
server.stdin.flush();
wait(server.pid);
return 1;
}
}
else
{
testsToRun = allTests;
}
// Run tests
foreach (test; testsToRun)
runTest(test);
// If gdb_exit wasn't run as part of tests, shut down the server
if (!testsToRun.canFind!(t => t.name == "gdb_exit"))
{
send("-gdb-exit");
}
// Wait for server to terminate
wait(server.pid);
// Summary
int total = testsPassed + testsFailed;
writefln("\nResults: %d passed, %d failed, %d total", testsPassed, testsFailed, total);
return testsFailed > 0 ? 1 : 0;
}