~tritone-team/tritone/libvirt

« back to all changes in this revision

Viewing changes to .pc/9024-fix-broken-commandtest.patch/tests/commandtest.c

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge, Serge Hallyn, Jamie Strandboge
  • Date: 2011-03-15 11:46:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110315114634-sirhownqcyy3hbqi
Tags: 0.8.8-1ubuntu4
[ Serge Hallyn ]
* Replace 9024-skip-broken-commandtest.patch with
  9024-fix-broken-commandtest.patch from upstream.

[ Jamie Strandboge ]
* debian/patches/9026-security-avoid-memory-leak.patch: avoid memory leaks
  with the security drivers. Can be dropped in 0.8.9.
* SECURITY UPDATE: debian/patches/9027-CVE-2011-1146.patch: Add missing
  checks for read only connections. Patch from Debian. Can be dropped in
  0.8.8-3.
  - CVE-2011-1146

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * commandtest.c: Test the libCommand API
 
3
 *
 
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2.1 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 
19
 */
 
20
 
 
21
#include <config.h>
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <unistd.h>
 
27
#include <signal.h>
 
28
#include <sys/stat.h>
 
29
#include <fcntl.h>
 
30
 
 
31
#include "testutils.h"
 
32
#include "internal.h"
 
33
#include "nodeinfo.h"
 
34
#include "util.h"
 
35
#include "memory.h"
 
36
#include "command.h"
 
37
#include "files.h"
 
38
 
 
39
#ifdef WIN32
 
40
 
 
41
static int
 
42
mymain(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
 
43
{
 
44
    exit (EXIT_AM_SKIP);
 
45
}
 
46
 
 
47
#else
 
48
 
 
49
static char *progname;
 
50
static char *abs_srcdir;
 
51
 
 
52
 
 
53
static int checkoutput(const char *testname)
 
54
{
 
55
    int ret = -1;
 
56
    char cwd[1024];
 
57
    char *expectname = NULL;
 
58
    char *expectlog = NULL;
 
59
    char *actualname = NULL;
 
60
    char *actuallog = NULL;
 
61
 
 
62
    if (!getcwd(cwd, sizeof(cwd)))
 
63
        return -1;
 
64
 
 
65
    if (virAsprintf(&expectname, "%s/commanddata/%s.log", abs_srcdir,
 
66
                    testname) < 0)
 
67
        goto cleanup;
 
68
    if (virAsprintf(&actualname, "%s/commandhelper.log", abs_builddir) < 0)
 
69
        goto cleanup;
 
70
 
 
71
    if (virFileReadAll(expectname, 1024*64, &expectlog) < 0) {
 
72
        fprintf(stderr, "cannot read %s\n", expectname);
 
73
        goto cleanup;
 
74
    }
 
75
 
 
76
    if (virFileReadAll(actualname, 1024*64, &actuallog) < 0) {
 
77
        fprintf(stderr, "cannot read %s\n", actualname);
 
78
        goto cleanup;
 
79
    }
 
80
 
 
81
    if (STRNEQ(expectlog, actuallog)) {
 
82
        virtTestDifference(stderr, expectlog, actuallog);
 
83
        goto cleanup;
 
84
    }
 
85
 
 
86
    ret = 0;
 
87
 
 
88
cleanup:
 
89
    unlink(actualname);
 
90
    VIR_FREE(actuallog);
 
91
    VIR_FREE(actualname);
 
92
    VIR_FREE(expectlog);
 
93
    VIR_FREE(expectname);
 
94
    return ret;
 
95
}
 
96
 
 
97
/*
 
98
 * Run program, no args, inherit all ENV, keep CWD.
 
99
 * Only stdin/out/err open
 
100
 * No slot for return status must log error.
 
101
 */
 
102
static int test0(const void *unused ATTRIBUTE_UNUSED)
 
103
{
 
104
    virCommandPtr cmd;
 
105
    int ret = -1;
 
106
 
 
107
    cmd = virCommandNew(abs_builddir "/commandhelper-doesnotexist");
 
108
    if (virCommandRun(cmd, NULL) == 0)
 
109
        goto cleanup;
 
110
 
 
111
    if (virGetLastError() == NULL)
 
112
        goto cleanup;
 
113
 
 
114
    virResetLastError();
 
115
    ret = 0;
 
116
 
 
117
cleanup:
 
118
    virCommandFree(cmd);
 
119
    return ret;
 
120
}
 
121
 
 
122
/*
 
123
 * Run program, no args, inherit all ENV, keep CWD.
 
124
 * Only stdin/out/err open
 
125
 * Capturing return status must not log error.
 
126
 */
 
127
static int test1(const void *unused ATTRIBUTE_UNUSED)
 
128
{
 
129
    virCommandPtr cmd;
 
130
    int ret = -1;
 
131
    int status;
 
132
 
 
133
    cmd = virCommandNew(abs_builddir "/commandhelper-doesnotexist");
 
134
    if (virCommandRun(cmd, &status) < 0)
 
135
        goto cleanup;
 
136
    if (status == 0)
 
137
        goto cleanup;
 
138
    ret = 0;
 
139
 
 
140
cleanup:
 
141
    virCommandFree(cmd);
 
142
    return ret;
 
143
}
 
144
 
 
145
/*
 
146
 * Run program (twice), no args, inherit all ENV, keep CWD.
 
147
 * Only stdin/out/err open
 
148
 */
 
149
static int test2(const void *unused ATTRIBUTE_UNUSED)
 
150
{
 
151
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
152
    int ret;
 
153
 
 
154
    if (virCommandRun(cmd, NULL) < 0) {
 
155
        virErrorPtr err = virGetLastError();
 
156
        printf("Cannot run child %s\n", err->message);
 
157
        virCommandFree(cmd);
 
158
        return -1;
 
159
    }
 
160
 
 
161
    if ((ret = checkoutput("test2")) != 0) {
 
162
        virCommandFree(cmd);
 
163
        return ret;
 
164
    }
 
165
 
 
166
    if (virCommandRun(cmd, NULL) < 0) {
 
167
        virErrorPtr err = virGetLastError();
 
168
        printf("Cannot run child %s\n", err->message);
 
169
        virCommandFree(cmd);
 
170
        return -1;
 
171
    }
 
172
 
 
173
    virCommandFree(cmd);
 
174
 
 
175
    return checkoutput("test2");
 
176
}
 
177
 
 
178
/*
 
179
 * Run program, no args, inherit all ENV, keep CWD.
 
180
 * stdin/out/err + two extra FD open
 
181
 */
 
182
static int test3(const void *unused ATTRIBUTE_UNUSED)
 
183
{
 
184
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
185
    int newfd1 = dup(STDERR_FILENO);
 
186
    int newfd2 = dup(STDERR_FILENO);
 
187
    int newfd3 = dup(STDERR_FILENO);
 
188
    int ret = -1;
 
189
 
 
190
    virCommandPreserveFD(cmd, newfd1);
 
191
    virCommandTransferFD(cmd, newfd3);
 
192
 
 
193
    if (virCommandRun(cmd, NULL) < 0) {
 
194
        virErrorPtr err = virGetLastError();
 
195
        printf("Cannot run child %s\n", err->message);
 
196
        goto cleanup;
 
197
    }
 
198
 
 
199
    if (fcntl(newfd1, F_GETFL) < 0 ||
 
200
        fcntl(newfd2, F_GETFL) < 0 ||
 
201
        fcntl(newfd3, F_GETFL) >= 0) {
 
202
        puts("fds in wrong state");
 
203
        goto cleanup;
 
204
    }
 
205
 
 
206
    ret = checkoutput("test3");
 
207
 
 
208
cleanup:
 
209
    virCommandFree(cmd);
 
210
    VIR_FORCE_CLOSE(newfd1);
 
211
    VIR_FORCE_CLOSE(newfd2);
 
212
    return ret;
 
213
}
 
214
 
 
215
 
 
216
/*
 
217
 * Run program, no args, inherit all ENV, CWD is /
 
218
 * Only stdin/out/err open.
 
219
 * Daemonized
 
220
 */
 
221
static int test4(const void *unused ATTRIBUTE_UNUSED)
 
222
{
 
223
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
224
    char *pidfile = virFilePid(abs_builddir, "commandhelper");
 
225
    pid_t pid;
 
226
    int ret = -1;
 
227
 
 
228
    if (!pidfile)
 
229
        goto cleanup;
 
230
 
 
231
    virCommandSetPidFile(cmd, pidfile);
 
232
    virCommandDaemonize(cmd);
 
233
 
 
234
    if (virCommandRun(cmd, NULL) < 0) {
 
235
        virErrorPtr err = virGetLastError();
 
236
        printf("Cannot run child %s\n", err->message);
 
237
        goto cleanup;
 
238
    }
 
239
 
 
240
    if (virFileReadPid(abs_builddir, "commandhelper", &pid) != 0) {
 
241
        printf("cannot read pidfile\n");
 
242
        goto cleanup;
 
243
    }
 
244
    while (kill(pid, 0) != -1)
 
245
        usleep(100*1000);
 
246
 
 
247
    ret = checkoutput("test4");
 
248
 
 
249
cleanup:
 
250
    virCommandFree(cmd);
 
251
    unlink(pidfile);
 
252
    VIR_FREE(pidfile);
 
253
    return ret;
 
254
}
 
255
 
 
256
 
 
257
/*
 
258
 * Run program, no args, inherit filtered ENV, keep CWD.
 
259
 * Only stdin/out/err open
 
260
 */
 
261
static int test5(const void *unused ATTRIBUTE_UNUSED)
 
262
{
 
263
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
264
 
 
265
    virCommandAddEnvPassCommon(cmd);
 
266
 
 
267
    if (virCommandRun(cmd, NULL) < 0) {
 
268
        virErrorPtr err = virGetLastError();
 
269
        printf("Cannot run child %s\n", err->message);
 
270
        virCommandFree(cmd);
 
271
        return -1;
 
272
    }
 
273
 
 
274
    virCommandFree(cmd);
 
275
 
 
276
    return checkoutput("test5");
 
277
}
 
278
 
 
279
 
 
280
/*
 
281
 * Run program, no args, inherit filtered ENV, keep CWD.
 
282
 * Only stdin/out/err open
 
283
 */
 
284
static int test6(const void *unused ATTRIBUTE_UNUSED)
 
285
{
 
286
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
287
 
 
288
    virCommandAddEnvPass(cmd, "DISPLAY");
 
289
    virCommandAddEnvPass(cmd, "DOESNOTEXIST");
 
290
 
 
291
    if (virCommandRun(cmd, NULL) < 0) {
 
292
        virErrorPtr err = virGetLastError();
 
293
        printf("Cannot run child %s\n", err->message);
 
294
        virCommandFree(cmd);
 
295
        return -1;
 
296
    }
 
297
 
 
298
    virCommandFree(cmd);
 
299
 
 
300
    return checkoutput("test6");
 
301
}
 
302
 
 
303
 
 
304
/*
 
305
 * Run program, no args, inherit filtered ENV, keep CWD.
 
306
 * Only stdin/out/err open
 
307
 */
 
308
static int test7(const void *unused ATTRIBUTE_UNUSED)
 
309
{
 
310
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
311
 
 
312
    virCommandAddEnvPassCommon(cmd);
 
313
    virCommandAddEnvPass(cmd, "DISPLAY");
 
314
    virCommandAddEnvPass(cmd, "DOESNOTEXIST");
 
315
 
 
316
    if (virCommandRun(cmd, NULL) < 0) {
 
317
        virErrorPtr err = virGetLastError();
 
318
        printf("Cannot run child %s\n", err->message);
 
319
        virCommandFree(cmd);
 
320
        return -1;
 
321
    }
 
322
 
 
323
    virCommandFree(cmd);
 
324
 
 
325
    return checkoutput("test7");
 
326
}
 
327
 
 
328
/*
 
329
 * Run program, no args, inherit filtered ENV, keep CWD.
 
330
 * Only stdin/out/err open
 
331
 */
 
332
static int test8(const void *unused ATTRIBUTE_UNUSED)
 
333
{
 
334
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
335
 
 
336
    virCommandAddEnvString(cmd, "LANG=C");
 
337
    virCommandAddEnvPair(cmd, "USER", "test");
 
338
 
 
339
    if (virCommandRun(cmd, NULL) < 0) {
 
340
        virErrorPtr err = virGetLastError();
 
341
        printf("Cannot run child %s\n", err->message);
 
342
        virCommandFree(cmd);
 
343
        return -1;
 
344
    }
 
345
 
 
346
    virCommandFree(cmd);
 
347
 
 
348
    return checkoutput("test8");
 
349
}
 
350
 
 
351
 
 
352
/*
 
353
 * Run program, some args, inherit all ENV, keep CWD.
 
354
 * Only stdin/out/err open
 
355
 */
 
356
static int test9(const void *unused ATTRIBUTE_UNUSED)
 
357
{
 
358
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
359
    const char* const args[] = { "arg1", "arg2", NULL };
 
360
 
 
361
    virCommandAddArg(cmd, "-version");
 
362
    virCommandAddArgPair(cmd, "-log", "bar.log");
 
363
    virCommandAddArgSet(cmd, args);
 
364
    virCommandAddArgList(cmd, "arg3", "arg4", NULL);
 
365
 
 
366
    if (virCommandRun(cmd, NULL) < 0) {
 
367
        virErrorPtr err = virGetLastError();
 
368
        printf("Cannot run child %s\n", err->message);
 
369
        virCommandFree(cmd);
 
370
        return -1;
 
371
    }
 
372
 
 
373
    virCommandFree(cmd);
 
374
 
 
375
    return checkoutput("test9");
 
376
}
 
377
 
 
378
 
 
379
/*
 
380
 * Run program, some args, inherit all ENV, keep CWD.
 
381
 * Only stdin/out/err open
 
382
 */
 
383
static int test10(const void *unused ATTRIBUTE_UNUSED)
 
384
{
 
385
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
386
    const char *const args[] = {
 
387
        "-version", "-log=bar.log", NULL,
 
388
    };
 
389
 
 
390
    virCommandAddArgSet(cmd, args);
 
391
 
 
392
    if (virCommandRun(cmd, NULL) < 0) {
 
393
        virErrorPtr err = virGetLastError();
 
394
        printf("Cannot run child %s\n", err->message);
 
395
        virCommandFree(cmd);
 
396
        return -1;
 
397
    }
 
398
 
 
399
    virCommandFree(cmd);
 
400
 
 
401
    return checkoutput("test10");
 
402
}
 
403
 
 
404
/*
 
405
 * Run program, some args, inherit all ENV, keep CWD.
 
406
 * Only stdin/out/err open
 
407
 */
 
408
static int test11(const void *unused ATTRIBUTE_UNUSED)
 
409
{
 
410
    const char *args[] = {
 
411
        abs_builddir "/commandhelper",
 
412
        "-version", "-log=bar.log", NULL,
 
413
    };
 
414
    virCommandPtr cmd = virCommandNewArgs(args);
 
415
 
 
416
    if (virCommandRun(cmd, NULL) < 0) {
 
417
        virErrorPtr err = virGetLastError();
 
418
        printf("Cannot run child %s\n", err->message);
 
419
        virCommandFree(cmd);
 
420
        return -1;
 
421
    }
 
422
 
 
423
    virCommandFree(cmd);
 
424
 
 
425
    return checkoutput("test11");
 
426
}
 
427
 
 
428
/*
 
429
 * Run program, no args, inherit all ENV, keep CWD.
 
430
 * Only stdin/out/err open. Set stdin data
 
431
 */
 
432
static int test12(const void *unused ATTRIBUTE_UNUSED)
 
433
{
 
434
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
435
 
 
436
    virCommandSetInputBuffer(cmd, "Hello World\n");
 
437
 
 
438
    if (virCommandRun(cmd, NULL) < 0) {
 
439
        virErrorPtr err = virGetLastError();
 
440
        printf("Cannot run child %s\n", err->message);
 
441
        virCommandFree(cmd);
 
442
        return -1;
 
443
    }
 
444
 
 
445
    virCommandFree(cmd);
 
446
 
 
447
    return checkoutput("test12");
 
448
}
 
449
 
 
450
/*
 
451
 * Run program, no args, inherit all ENV, keep CWD.
 
452
 * Only stdin/out/err open. Set stdin data
 
453
 */
 
454
static int test13(const void *unused ATTRIBUTE_UNUSED)
 
455
{
 
456
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
457
    char *outactual = NULL;
 
458
    const char *outexpect = "BEGIN STDOUT\n"
 
459
        "Hello World\n"
 
460
        "END STDOUT\n";
 
461
    int ret = -1;
 
462
 
 
463
    virCommandSetInputBuffer(cmd, "Hello World\n");
 
464
    virCommandSetOutputBuffer(cmd, &outactual);
 
465
 
 
466
    if (virCommandRun(cmd, NULL) < 0) {
 
467
        virErrorPtr err = virGetLastError();
 
468
        printf("Cannot run child %s\n", err->message);
 
469
        goto cleanup;
 
470
    }
 
471
    if (!outactual)
 
472
        goto cleanup;
 
473
 
 
474
    virCommandFree(cmd);
 
475
    cmd = NULL;
 
476
 
 
477
    if (!STREQ(outactual, outexpect)) {
 
478
        virtTestDifference(stderr, outactual, outexpect);
 
479
        goto cleanup;
 
480
    }
 
481
 
 
482
    ret = checkoutput("test13");
 
483
 
 
484
cleanup:
 
485
    virCommandFree(cmd);
 
486
    VIR_FREE(outactual);
 
487
    return ret;
 
488
}
 
489
 
 
490
/*
 
491
 * Run program, no args, inherit all ENV, keep CWD.
 
492
 * Only stdin/out/err open. Set stdin data
 
493
 */
 
494
static int test14(const void *unused ATTRIBUTE_UNUSED)
 
495
{
 
496
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
497
    char *outactual = NULL;
 
498
    const char *outexpect = "BEGIN STDOUT\n"
 
499
        "Hello World\n"
 
500
        "END STDOUT\n";
 
501
    char *erractual = NULL;
 
502
    const char *errexpect = "BEGIN STDERR\n"
 
503
        "Hello World\n"
 
504
        "END STDERR\n";
 
505
    int ret = -1;
 
506
 
 
507
    virCommandSetInputBuffer(cmd, "Hello World\n");
 
508
    virCommandSetOutputBuffer(cmd, &outactual);
 
509
    virCommandSetErrorBuffer(cmd, &erractual);
 
510
 
 
511
    if (virCommandRun(cmd, NULL) < 0) {
 
512
        virErrorPtr err = virGetLastError();
 
513
        printf("Cannot run child %s\n", err->message);
 
514
        goto cleanup;
 
515
    }
 
516
    if (!outactual || !erractual)
 
517
        goto cleanup;
 
518
 
 
519
    virCommandFree(cmd);
 
520
    cmd = NULL;
 
521
 
 
522
    if (!STREQ(outactual, outexpect)) {
 
523
        virtTestDifference(stderr, outactual, outexpect);
 
524
        goto cleanup;
 
525
    }
 
526
    if (!STREQ(erractual, errexpect)) {
 
527
        virtTestDifference(stderr, erractual, errexpect);
 
528
        goto cleanup;
 
529
    }
 
530
 
 
531
    ret = checkoutput("test14");
 
532
 
 
533
cleanup:
 
534
    virCommandFree(cmd);
 
535
    VIR_FREE(outactual);
 
536
    VIR_FREE(erractual);
 
537
    return ret;
 
538
}
 
539
 
 
540
 
 
541
/*
 
542
 * Run program, no args, inherit all ENV, change CWD.
 
543
 * Only stdin/out/err open
 
544
 */
 
545
static int test15(const void *unused ATTRIBUTE_UNUSED)
 
546
{
 
547
    virCommandPtr cmd = virCommandNew(abs_builddir "/commandhelper");
 
548
    char *cwd = NULL;
 
549
    int ret = -1;
 
550
 
 
551
    if (virAsprintf(&cwd, "%s/commanddata", abs_srcdir) < 0)
 
552
        goto cleanup;
 
553
    virCommandSetWorkingDirectory(cmd, cwd);
 
554
 
 
555
    if (virCommandRun(cmd, NULL) < 0) {
 
556
        virErrorPtr err = virGetLastError();
 
557
        printf("Cannot run child %s\n", err->message);
 
558
        goto cleanup;
 
559
    }
 
560
 
 
561
    ret = checkoutput("test15");
 
562
 
 
563
cleanup:
 
564
    VIR_FREE(cwd);
 
565
    virCommandFree(cmd);
 
566
 
 
567
    return ret;
 
568
}
 
569
 
 
570
/*
 
571
 * Don't run program; rather, log what would be run.
 
572
 */
 
573
static int test16(const void *unused ATTRIBUTE_UNUSED)
 
574
{
 
575
    virCommandPtr cmd = virCommandNew("/bin/true");
 
576
    char *outactual = NULL;
 
577
    const char *outexpect = "A=B /bin/true C";
 
578
    int ret = -1;
 
579
    int fd = -1;
 
580
 
 
581
    virCommandAddEnvPair(cmd, "A", "B");
 
582
    virCommandAddArg(cmd, "C");
 
583
 
 
584
    if ((outactual = virCommandToString(cmd)) == NULL) {
 
585
        virErrorPtr err = virGetLastError();
 
586
        printf("Cannot convert to string: %s\n", err->message);
 
587
        goto cleanup;
 
588
    }
 
589
    if ((fd = open(abs_builddir "/commandhelper.log",
 
590
                   O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
 
591
        printf("Cannot open log file: %s\n", strerror (errno));
 
592
        goto cleanup;
 
593
    }
 
594
    virCommandWriteArgLog(cmd, fd);
 
595
    if (VIR_CLOSE(fd) < 0) {
 
596
        printf("Cannot close log file: %s\n", strerror (errno));
 
597
        goto cleanup;
 
598
    }
 
599
 
 
600
    if (!STREQ(outactual, outexpect)) {
 
601
        virtTestDifference(stderr, outactual, outexpect);
 
602
        goto cleanup;
 
603
    }
 
604
 
 
605
    ret = checkoutput("test16");
 
606
 
 
607
cleanup:
 
608
    virCommandFree(cmd);
 
609
    VIR_FORCE_CLOSE(fd);
 
610
    VIR_FREE(outactual);
 
611
    return ret;
 
612
}
 
613
 
 
614
/*
 
615
 * Test string handling when no output is present.
 
616
 */
 
617
static int test17(const void *unused ATTRIBUTE_UNUSED)
 
618
{
 
619
    virCommandPtr cmd = virCommandNew("/bin/true");
 
620
    int ret = -1;
 
621
    char *outbuf;
 
622
    char *errbuf;
 
623
 
 
624
    virCommandSetOutputBuffer(cmd, &outbuf);
 
625
    if (outbuf != NULL) {
 
626
        puts("buffer not sanitized at registration");
 
627
        goto cleanup;
 
628
    }
 
629
 
 
630
    if (virCommandRun(cmd, NULL) < 0) {
 
631
        virErrorPtr err = virGetLastError();
 
632
        printf("Cannot run child %s\n", err->message);
 
633
        goto cleanup;
 
634
    }
 
635
 
 
636
    if (!outbuf || *outbuf) {
 
637
        puts("output buffer is not an allocated empty string");
 
638
        goto cleanup;
 
639
    }
 
640
    VIR_FREE(outbuf);
 
641
    if ((outbuf = strdup("should not be leaked")) == NULL) {
 
642
        puts("test framework failure");
 
643
        goto cleanup;
 
644
    }
 
645
 
 
646
    virCommandSetErrorBuffer(cmd, &errbuf);
 
647
    if (errbuf != NULL) {
 
648
        puts("buffer not sanitized at registration");
 
649
        goto cleanup;
 
650
    }
 
651
 
 
652
    if (virCommandRun(cmd, NULL) < 0) {
 
653
        virErrorPtr err = virGetLastError();
 
654
        printf("Cannot run child %s\n", err->message);
 
655
        goto cleanup;
 
656
    }
 
657
 
 
658
    if (!outbuf || *outbuf || !errbuf || *errbuf) {
 
659
        puts("output buffers are not allocated empty strings");
 
660
        goto cleanup;
 
661
    }
 
662
 
 
663
    ret = 0;
 
664
cleanup:
 
665
    virCommandFree(cmd);
 
666
    VIR_FREE(outbuf);
 
667
    VIR_FREE(errbuf);
 
668
    return ret;
 
669
}
 
670
 
 
671
/*
 
672
 * Run long-running daemon, to ensure no hang.
 
673
 */
 
674
static int test18(const void *unused ATTRIBUTE_UNUSED)
 
675
{
 
676
    virCommandPtr cmd = virCommandNewArgList("sleep", "100", NULL);
 
677
    char *pidfile = virFilePid(abs_builddir, "commandhelper");
 
678
    pid_t pid;
 
679
    int ret = -1;
 
680
 
 
681
    if (!pidfile)
 
682
        goto cleanup;
 
683
 
 
684
    virCommandSetPidFile(cmd, pidfile);
 
685
    virCommandDaemonize(cmd);
 
686
 
 
687
    alarm(5);
 
688
    if (virCommandRun(cmd, NULL) < 0) {
 
689
        virErrorPtr err = virGetLastError();
 
690
        printf("Cannot run child %s\n", err->message);
 
691
        goto cleanup;
 
692
    }
 
693
    alarm(0);
 
694
 
 
695
    if (virFileReadPid(abs_builddir, "commandhelper", &pid) != 0) {
 
696
        printf("cannot read pidfile\n");
 
697
        goto cleanup;
 
698
    }
 
699
    while (kill(pid, SIGINT) != -1)
 
700
        usleep(100*1000);
 
701
 
 
702
    ret = 0;
 
703
 
 
704
cleanup:
 
705
    virCommandFree(cmd);
 
706
    unlink(pidfile);
 
707
    VIR_FREE(pidfile);
 
708
    return ret;
 
709
}
 
710
 
 
711
 
 
712
static int
 
713
mymain(int argc, char **argv)
 
714
{
 
715
    int ret = 0;
 
716
    char cwd[PATH_MAX];
 
717
    int fd;
 
718
 
 
719
    abs_srcdir = getenv("abs_srcdir");
 
720
    if (!abs_srcdir)
 
721
        abs_srcdir = getcwd(cwd, sizeof(cwd));
 
722
 
 
723
    progname = argv[0];
 
724
 
 
725
    if (argc > 1) {
 
726
        fprintf(stderr, "Usage: %s\n", progname);
 
727
        return(EXIT_FAILURE);
 
728
    }
 
729
 
 
730
    if (chdir("/tmp") < 0)
 
731
        return(EXIT_FAILURE);
 
732
 
 
733
    /* Kill off any inherited fds that might interfere with our
 
734
     * testing.  */
 
735
    fd = 3;
 
736
    VIR_FORCE_CLOSE(fd);
 
737
    fd = 4;
 
738
    VIR_FORCE_CLOSE(fd);
 
739
    fd = 5;
 
740
    VIR_FORCE_CLOSE(fd);
 
741
 
 
742
    virInitialize();
 
743
 
 
744
    const char *const newenv[] = {
 
745
        "PATH=/usr/bin:/bin",
 
746
        "HOSTNAME=test",
 
747
        "LANG=C",
 
748
        "HOME=/home/test",
 
749
        "USER=test",
 
750
        "LOGNAME=test"
 
751
        "TMPDIR=/tmp",
 
752
        "DISPLAY=:0.0",
 
753
        NULL
 
754
    };
 
755
    environ = (char **)newenv;
 
756
 
 
757
# define DO_TEST(NAME)                                                \
 
758
    if (virtTestRun("Command Exec " #NAME " test",                    \
 
759
                    1, NAME, NULL) < 0)                               \
 
760
        ret = -1
 
761
 
 
762
    DO_TEST(test0);
 
763
    DO_TEST(test1);
 
764
    DO_TEST(test2);
 
765
    DO_TEST(test3);
 
766
    DO_TEST(test4);
 
767
    DO_TEST(test5);
 
768
    DO_TEST(test6);
 
769
    DO_TEST(test7);
 
770
    DO_TEST(test8);
 
771
    DO_TEST(test9);
 
772
    DO_TEST(test10);
 
773
    DO_TEST(test11);
 
774
    DO_TEST(test12);
 
775
    DO_TEST(test13);
 
776
    DO_TEST(test14);
 
777
    DO_TEST(test15);
 
778
    DO_TEST(test16);
 
779
    DO_TEST(test17);
 
780
    DO_TEST(test18);
 
781
 
 
782
    return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE);
 
783
}
 
784
 
 
785
#endif /* !WIN32 */
 
786
 
 
787
VIRT_TEST_MAIN(mymain)