This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
/
tester.d
103 lines (92 loc) · 2.86 KB
/
tester.d
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
import std.exception;
import std.bitmanip;
import std.process;
import std.string;
import std.stdio;
import std.conv;
import std.json;
import std.uni;
import core.thread;
static import std.file;
bool isNumeric(in string s)
{
if (s.length == 0)
return false;
foreach (c; s)
if (!c.isNumber)
return false;
return true;
}
string readAll(File file)
{
ubyte[1024] buf;
string data;
while (!file.eof)
data ~= cast(string) file.rawRead(buf);
return data;
}
void main(string[] args)
{
import etc.linux.memoryerror;
static if (is(typeof(registerMemoryErrorHandler)))
registerMemoryErrorHandler();
//dfmt off
string[] preprogrammed = [
/* 0 */`{"cmd":"load","components":["dub","dcd"],"dir":"` ~ std.file.getcwd() ~ `","port":9621}`,
/* 1 */`{"cmd":"dub","subcmd":"list:dep"}`,
/* 2 */`{"cmd":"dub","subcmd":"list:import"}`,
/* 3 */`{"cmd":"dub","subcmd":"list:string-import"}`,
/* 4 */`{"cmd":"dub","subcmd":"update"}`,
/* 5 */`{"cmd":"dcd","subcmd":"setup-server"}`,
/* 6 */`{"cmd":"dcd","subcmd":"add-imports","imports":["/usr/include/dmd/druntime/import","/usr/include/dmd/phobos"]}`,
/* 7 */`{"cmd":"dcd","subcmd":"search-symbol","query":"toImpl"}`,
/* 8 */`{"cmd":"dcd","subcmd":"find-declaration","pos":14,"code":"void main() {foo();} void foo() {}"}`,
/* 9 */`{"cmd":"dcd","subcmd":"list-completion","pos":21,"code":"int integer; integer."}`,
/* 10 */`{"cmd":"dcd","subcmd":"get-socketfile"}`,
/* 11 */`{"cmd":"dcd","subcmd":"get-port"}`,
];
//dfmt on
auto pipes = pipeProcess(["./workspace-d"] ~ args,
Redirect.stdin | Redirect.stdout | Redirect.stderr);
int requestID = 0;
ubyte[4] intBuffer;
ubyte[] dataBuffer;
new Thread({
while (!pipes.stderr.eof)
write("Error: ", pipes.stderr.readln());
}).start();
while (true)
{
write("Enter JSON: ");
string instr = readln().strip();
if (instr.isNumeric)
instr = preprogrammed[instr.to!int];
ubyte[] input = cast(ubyte[]) instr;
if (input.length == 0)
continue;
requestID++;
input = nativeToBigEndian(requestID) ~ input;
pipes.stdin.rawWrite(nativeToBigEndian(cast(int) input.length) ~ input);
pipes.stdin.flush();
dataBuffer = pipes.stdout.rawRead(intBuffer);
assert(dataBuffer.length == 4, "Invalid buffer data");
int length = bigEndianToNative!int(dataBuffer[0 .. 4]);
assert(length >= 4, "Invalid request");
dataBuffer = pipes.stdout.rawRead(intBuffer);
assert(dataBuffer.length == 4, "Invalid buffer data");
int receivedID = bigEndianToNative!int(dataBuffer[0 .. 4]);
enforce(requestID == receivedID,
"Processed invalid id! Got those bytes instead: " ~ cast(
string) dataBuffer ~ pipes.stdout.readAll);
dataBuffer.length = length - 4;
dataBuffer = pipes.stdout.rawRead(dataBuffer);
try
{
writeln(parseJSON(cast(string) dataBuffer).toPrettyString());
}
catch (Exception e)
{
writeln("[INVALID]: ", cast(string) dataBuffer);
}
}
}