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
|
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <stdint.h>
#include "cm_uv.h"
#include "cmGetPipes.h"
#include "cmUVHandlePtr.h"
#include "cmUVStreambuf.h"
#define TEST_STR_LINE_1 "This string must be exactly 128 characters long so"
#define TEST_STR_LINE_2 "that we can test CMake's std::streambuf integration"
#define TEST_STR_LINE_3 "with libuv's uv_stream_t."
#define TEST_STR TEST_STR_LINE_1 "\n" TEST_STR_LINE_2 "\n" TEST_STR_LINE_3
bool writeDataToStreamPipe(uv_loop_t& loop, cm::uv_pipe_ptr& inputPipe,
char* outputData, unsigned int outputDataLength,
const char* /* unused */)
{
int err;
// Create the pipe
int pipeHandles[2];
if (cmGetPipes(pipeHandles) < 0) {
std::cout << "Could not open pipe" << std::endl;
return false;
}
cm::uv_pipe_ptr outputPipe;
inputPipe.init(loop, 0);
outputPipe.init(loop, 0);
uv_pipe_open(inputPipe, pipeHandles[0]);
uv_pipe_open(outputPipe, pipeHandles[1]);
// Write data for reading
uv_write_t writeReq;
struct WriteCallbackData
{
bool Finished = false;
int Status;
} writeData;
writeReq.data = &writeData;
uv_buf_t outputBuf;
outputBuf.base = outputData;
outputBuf.len = outputDataLength;
if ((err = uv_write(&writeReq, outputPipe, &outputBuf, 1,
[](uv_write_t* req, int status) {
auto data = static_cast<WriteCallbackData*>(req->data);
data->Finished = true;
data->Status = status;
})) < 0) {
std::cout << "Could not write to pipe: " << uv_strerror(err) << std::endl;
return false;
}
while (!writeData.Finished) {
uv_run(&loop, UV_RUN_ONCE);
}
if (writeData.Status < 0) {
std::cout << "Status is " << uv_strerror(writeData.Status)
<< ", should be 0" << std::endl;
return false;
}
return true;
}
bool writeDataToStreamProcess(uv_loop_t& loop, cm::uv_pipe_ptr& inputPipe,
char* outputData, unsigned int /* unused */,
const char* cmakeCommand)
{
int err;
inputPipe.init(loop, 0);
std::vector<std::string> arguments = { cmakeCommand, "-E", "echo_append",
outputData };
std::vector<const char*> processArgs;
for (auto const& arg : arguments) {
processArgs.push_back(arg.c_str());
}
processArgs.push_back(nullptr);
std::vector<uv_stdio_container_t> stdio(3);
stdio[0].flags = UV_IGNORE;
stdio[0].data.stream = nullptr;
stdio[1].flags =
static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
stdio[1].data.stream = inputPipe;
stdio[2].flags = UV_IGNORE;
stdio[2].data.stream = nullptr;
struct ProcessExitData
{
bool Finished = false;
int64_t ExitStatus;
int TermSignal;
} exitData;
cm::uv_process_ptr process;
auto options = uv_process_options_t();
options.file = cmakeCommand;
options.args = const_cast<char**>(processArgs.data());
options.flags = UV_PROCESS_WINDOWS_HIDE;
options.stdio = stdio.data();
options.stdio_count = static_cast<int>(stdio.size());
options.exit_cb = [](uv_process_t* handle, int64_t exitStatus,
int termSignal) {
auto data = static_cast<ProcessExitData*>(handle->data);
data->Finished = true;
data->ExitStatus = exitStatus;
data->TermSignal = termSignal;
};
if ((err = process.spawn(loop, options, &exitData)) < 0) {
std::cout << "Could not spawn process: " << uv_strerror(err) << std::endl;
return false;
}
while (!exitData.Finished) {
uv_run(&loop, UV_RUN_ONCE);
}
if (exitData.ExitStatus != 0) {
std::cout << "Process exit status is " << exitData.ExitStatus
<< ", should be 0" << std::endl;
return false;
}
if (exitData.TermSignal != 0) {
std::cout << "Process term signal is " << exitData.TermSignal
<< ", should be 0" << std::endl;
return false;
}
return true;
}
bool testUVStreambufRead(
bool (*cb)(uv_loop_t& loop, cm::uv_pipe_ptr& inputPipe, char* outputData,
unsigned int outputDataLength, const char* cmakeCommand),
const char* cmakeCommand)
{
char outputData[] = TEST_STR;
bool success = false;
cm::uv_loop_ptr loop;
loop.init();
cm::uv_pipe_ptr inputPipe;
std::vector<char> inputData(128);
std::streamsize readLen;
std::string line;
cm::uv_timer_ptr timer;
// Create the streambuf
cmUVStreambuf inputBuf(64);
std::istream inputStream(&inputBuf);
if (inputBuf.is_open()) {
std::cout << "is_open() is true, should be false" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
// Perform first read test - read all the data
if (!cb(*loop, inputPipe, outputData, 128, cmakeCommand)) {
goto end;
}
inputBuf.open(inputPipe);
if (!inputBuf.is_open()) {
std::cout << "is_open() is false, should be true" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 128) {
std::cout << "sgetn() returned " << readLen << ", should be 128"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if (std::memcmp(inputData.data(), outputData, 128)) {
std::cout << "Read data does not match write data" << std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
inputData.assign(128, char{});
inputBuf.close();
if (inputBuf.is_open()) {
std::cout << "is_open() is true, should be false" << std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
// Perform second read test - read some data and then close
if (!cb(*loop, inputPipe, outputData, 128, cmakeCommand)) {
goto end;
}
inputBuf.open(inputPipe);
if (!inputBuf.is_open()) {
std::cout << "is_open() is false, should be true" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 64)) != 64) {
std::cout << "sgetn() returned " << readLen << ", should be 64"
<< std::endl;
goto end;
}
if (std::memcmp(inputData.data(), outputData, 64)) {
std::cout << "Read data does not match write data" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 8) {
std::cout << "in_avail() returned " << readLen << ", should be 8"
<< std::endl;
goto end;
}
inputData.assign(128, char{});
inputBuf.close();
if (inputBuf.is_open()) {
std::cout << "is_open() is true, should be false" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
// Perform third read test - read line by line
if (!cb(*loop, inputPipe, outputData, 128, cmakeCommand)) {
goto end;
}
inputBuf.open(inputPipe);
if (!inputBuf.is_open()) {
std::cout << "is_open() is false, should be true" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if (!std::getline(inputStream, line)) {
std::cout << "getline returned false, should be true" << std::endl;
goto end;
}
if (line != TEST_STR_LINE_1) {
std::cout << "Line 1 is \"" << line
<< "\", should be \"" TEST_STR_LINE_1 "\"" << std::endl;
goto end;
}
if (!std::getline(inputStream, line)) {
std::cout << "getline returned false, should be true" << std::endl;
goto end;
}
if (line != TEST_STR_LINE_2) {
std::cout << "Line 2 is \"" << line
<< "\", should be \"" TEST_STR_LINE_2 "\"" << std::endl;
goto end;
}
if (!std::getline(inputStream, line)) {
std::cout << "getline returned false, should be true" << std::endl;
goto end;
}
if (line != TEST_STR_LINE_3) {
std::cout << "Line 3 is \"" << line
<< "\", should be \"" TEST_STR_LINE_3 "\"" << std::endl;
goto end;
}
if (std::getline(inputStream, line)) {
std::cout << "getline returned true, should be false" << std::endl;
goto end;
}
inputBuf.close();
if (inputBuf.is_open()) {
std::cout << "is_open() is true, should be false" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
// Perform fourth read test - run the event loop outside of underflow()
if (!cb(*loop, inputPipe, outputData, 128, cmakeCommand)) {
goto end;
}
inputBuf.open(inputPipe);
if (!inputBuf.is_open()) {
std::cout << "is_open() is false, should be true" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
uv_run(loop, UV_RUN_DEFAULT);
if ((readLen = inputBuf.in_avail()) != 72) {
std::cout << "in_avail() returned " << readLen << ", should be 72"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 128) {
std::cout << "sgetn() returned " << readLen << ", should be 128"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 128"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
inputBuf.close();
if (inputBuf.is_open()) {
std::cout << "is_open() is true, should be false" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
// Perform fifth read test - close the streambuf in the middle of a read
timer.init(*loop, &inputBuf);
if (!cb(*loop, inputPipe, outputData, 128, cmakeCommand)) {
goto end;
}
inputBuf.open(inputPipe);
if (!inputBuf.is_open()) {
std::cout << "is_open() is false, should be true" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != 0) {
std::cout << "in_avail() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
uv_timer_start(timer,
[](uv_timer_t* handle) {
auto buf = static_cast<cmUVStreambuf*>(handle->data);
buf->close();
},
0, 0);
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
if (inputBuf.is_open()) {
std::cout << "is_open() is true, should be false" << std::endl;
goto end;
}
if ((readLen = inputBuf.in_avail()) != -1) {
std::cout << "in_avail() returned " << readLen << ", should be -1"
<< std::endl;
goto end;
}
if ((readLen = inputBuf.sgetn(inputData.data(), 128)) != 0) {
std::cout << "sgetn() returned " << readLen << ", should be 0"
<< std::endl;
goto end;
}
success = true;
end:
return success;
}
int testUVStreambuf(int argc, char** const argv)
{
if (argc < 2) {
std::cout << "Invalid arguments.\n";
return -1;
}
if (!testUVStreambufRead(writeDataToStreamPipe, argv[1])) {
std::cout << "While executing testUVStreambufRead() with pipe.\n";
return -1;
}
if (!testUVStreambufRead(writeDataToStreamProcess, argv[1])) {
std::cout << "While executing testUVStreambufRead() with process.\n";
return -1;
}
return 0;
}
|