aboutsummaryrefslogtreecommitdiff
path: root/paludis/util/process_TEST.cc
blob: 518cd0ccc869144573bfe577a828db0553c66082 (plain)
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
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/util/process.hh>
#include <paludis/util/fs_path.hh>
#include <paludis/util/pipe.hh>
#include <paludis/util/safe_ofstream.hh>
#include <test/test_framework.hh>
#include <test/test_runner.hh>
#include <sstream>
#include <sys/types.h>
#include <pwd.h>

using namespace paludis;
using namespace test;

namespace
{
    std::string response_handler(const std::string & s)
    {
        if (s == "ONE")
            return "1";
        else if (s == "TWO")
            return "2";
        else if (s == "THREE")
            return "3";
        else if (s == "FOUR")
            return "4";
        else
            return "9";
    }
}

namespace test_cases
{
    struct TrueTest : TestCase
    {
        TrueTest() : TestCase("true") { }

        void run()
        {
            Process true_process(ProcessCommand({"true"}));
            TEST_CHECK_EQUAL(true_process.run().wait(), 0);
        }
    } test_true;

    struct FalseTest : TestCase
    {
        FalseTest() : TestCase("false") { }

        void run()
        {
            Process false_process(ProcessCommand({"false"}));
            TEST_CHECK_EQUAL(false_process.run().wait(), 1);
        }
    } test_false;

    struct NoWaitTest : TestCase
    {
        NoWaitTest() : TestCase("no wait") { }

        void run()
        {
            Process true_process(ProcessCommand({"true"}));
            TEST_CHECK_THROWS({ RunningProcessHandle handle(true_process.run()); }, ProcessError);
        }
    } test_no_wait;

    struct TwoWaitTest : TestCase
    {
        TwoWaitTest() : TestCase("two wait") { }

        void run()
        {
            Process true_process(ProcessCommand({"true"}));

            RunningProcessHandle handle(true_process.run());
            TEST_CHECK_EQUAL(handle.wait(), 0);
            TEST_CHECK_THROWS(int PALUDIS_ATTRIBUTE((unused)) x(handle.wait()), ProcessError);
        }
    } test_two_wait;

    struct GrabStdoutTest : TestCase
    {
        GrabStdoutTest() : TestCase("grab stdout") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process echo_process(ProcessCommand({"echo", "monkey"}));
            echo_process.capture_stdout(stdout_stream);

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "monkey\n");
        }
    } test_grab_stdout;

    struct GrabStdoutSingleCommandTest : TestCase
    {
        GrabStdoutSingleCommandTest() : TestCase("grab stdout single command") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process echo_process(ProcessCommand("echo giant space monkey"));
            echo_process.capture_stdout(stdout_stream);

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "giant space monkey\n");
        }
    } test_grab_stdout_single_command;

    struct GrabStderrTest : TestCase
    {
        GrabStderrTest() : TestCase("grab stderr") { }

        void run()
        {
            std::stringstream stderr_stream;
            Process echo_process(ProcessCommand({"bash", "-c", "echo monkey 1>&2"}));
            echo_process.capture_stderr(stderr_stream);

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stderr_stream.str(), "monkey\n");
        }
    } test_grab_stderr;

    struct GrabStdoutStderrTest : TestCase
    {
        GrabStdoutStderrTest() : TestCase("grab stdout stderr") { }

        void run()
        {
            std::stringstream stdout_stream, stderr_stream;
            Process echo_process(ProcessCommand({"bash", "-c", "echo monkey 1>&2 ; echo chimp"}));
            echo_process.capture_stdout(stdout_stream);
            echo_process.capture_stderr(stderr_stream);

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);

            TEST_CHECK_EQUAL(stdout_stream.str(), "chimp\n");
            TEST_CHECK_EQUAL(stderr_stream.str(), "monkey\n");
        }
    } test_grab_stdout_stderr;

    struct GrabStdoutLongTest : TestCase
    {
        GrabStdoutLongTest() : TestCase("grab stdout long") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process echo_process(ProcessCommand({"seq", "1", "100000"}));
            echo_process.capture_stdout(stdout_stream);

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);

            std::string s;
            for (int x(1) ; x <= 100000 ; ++x)
            {
                TEST_CHECK(std::getline(stdout_stream, s));
                TEST_CHECK_STRINGIFY_EQUAL(s, stringify(x));
            }

            TEST_CHECK(! std::getline(stdout_stream, s));
        }
    } test_grab_stdout_long;

    struct SetenvTest : TestCase
    {
        SetenvTest() : TestCase("setenv") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process printenv_process(ProcessCommand({"printenv", "monkey"}));
            printenv_process.capture_stdout(stdout_stream);
            printenv_process.setenv("monkey", "in space");

            TEST_CHECK_EQUAL(printenv_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "in space\n");
        }
    } test_setenv;

    struct ChdirTest : TestCase
    {
        ChdirTest() : TestCase("chdir") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process pwd_process(ProcessCommand({"pwd"}));
            pwd_process.capture_stdout(stdout_stream);
            pwd_process.chdir(FSPath("/"));

            TEST_CHECK_EQUAL(pwd_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "/\n");
        }
    } test_chdir;

    struct NoPtyTest : TestCase
    {
        NoPtyTest() : TestCase("no pty") { }

        void run()
        {
            std::stringstream stdout_stream, stderr_stream;
            Process test_t_process(ProcessCommand({"test", "-t", "1", "-o", "-t", "2"}));
            test_t_process.capture_stdout(stdout_stream);
            test_t_process.capture_stderr(stderr_stream);
            TEST_CHECK_EQUAL(test_t_process.run().wait(), 1);
        }
    } test_no_pty;

    struct PtyTest : TestCase
    {
        PtyTest() : TestCase("pty") { }

        void run()
        {
            std::stringstream stdout_stream, stderr_stream;
            Process test_t_process(ProcessCommand({"test", "-t", "1", "-a", "-t", "2"}));
            test_t_process.capture_stdout(stdout_stream);
            test_t_process.capture_stderr(stderr_stream);
            test_t_process.use_ptys();

            TEST_CHECK_EQUAL(test_t_process.run().wait(), 0);
        }
    } test_ptys;

    struct SetuidTest : TestCase
    {
        SetuidTest() : TestCase("setuid") { }

        void run()
        {
            if (0 != getuid())
                return;

            std::stringstream stdout_stream;
            Process whoami_process(ProcessCommand({"bash", "-c", "whoami ; groups"}));
            whoami_process.capture_stdout(stdout_stream);

            struct passwd * nobody(getpwnam("nobody"));
            if (nobody)
            {
                whoami_process.setuid_setgid(nobody->pw_uid, nobody->pw_gid);
                TEST_CHECK_EQUAL(whoami_process.run().wait(), 0);
                TEST_CHECK_EQUAL(stdout_stream.str(), "nobody\nnobody\n");
            }
        }
    } test_setuid;

    struct GrabFDTest : TestCase
    {
        GrabFDTest() : TestCase("grab fd") { }

        void run()
        {
            std::stringstream fd_stream;
            Process echo_process(ProcessCommand({"bash", "-c", "echo monkey 1>&$MAGIC_FD"}));
            echo_process.capture_output_to_fd(fd_stream, -1, "MAGIC_FD");

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(fd_stream.str(), "monkey\n");
        }
    } test_grab_fd;

    struct GrabFDFixedTest : TestCase
    {
        GrabFDFixedTest() : TestCase("grab fd fixed") { }

        void run()
        {
            std::stringstream fd_stream;
            Process echo_process(ProcessCommand({"bash", "-c", "echo monkey 1>&5"}));
            echo_process.capture_output_to_fd(fd_stream, 5, "");

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(fd_stream.str(), "monkey\n");
        }
    } test_grab_fd_fixed;

    struct StdinFDTest : TestCase
    {
        StdinFDTest() : TestCase("stdin fd") { }

        void run()
        {
            std::unique_ptr<Pipe> input_pipe(new Pipe(true));

            std::stringstream stdout_stream;
            Process cat_process(ProcessCommand({"rev"}));
            cat_process.capture_stdout(stdout_stream);
            cat_process.set_stdin_fd(input_pipe->read_fd());

            RunningProcessHandle handle(cat_process.run());

            {
                {
                    SafeOFStream s(input_pipe->write_fd(), true);
                    s << "backwards" << std::endl;
                }
                TEST_CHECK(0 == ::close(input_pipe->write_fd()));
                input_pipe->clear_write_fd();
            }

            TEST_CHECK_EQUAL(handle.wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "sdrawkcab\n");
        }
    } test_stdin_fd;

    struct PipeCommandTest : TestCase
    {
        PipeCommandTest() : TestCase("pipe command") { }

        void run()
        {
            {
                Process one_two_process(ProcessCommand({ "bash", "process_TEST_dir/pipe_test.bash", "ONE", "TWO" }));
                one_two_process.pipe_command_handler("PALUDIS_PIPE_COMMAND", &response_handler);
                TEST_CHECK_EQUAL(one_two_process.run().wait(), 12);
            }

            {
                Process three_four_process(ProcessCommand({ "bash", "process_TEST_dir/pipe_test.bash", "THREE", "FOUR" }));
                three_four_process.pipe_command_handler("PALUDIS_PIPE_COMMAND", &response_handler);
                TEST_CHECK_EQUAL(three_four_process.run().wait(), 34);
            }
        }
    } test_pipe_command;

    struct CapturedPipeCommandTest : TestCase
    {
        CapturedPipeCommandTest() : TestCase("captured pipe command") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process one_two_three_process(ProcessCommand({ "bash", "process_TEST_dir/captured_pipe_test.bash", "ONE", "TWO", "THREE" }));
            one_two_three_process.capture_stdout(stdout_stream);
            one_two_three_process.pipe_command_handler("PALUDIS_PIPE_COMMAND", &response_handler);
            TEST_CHECK_EQUAL(one_two_three_process.run().wait(), 13);

            std::string line;
            TEST_CHECK(std::getline(stdout_stream, line));
            TEST_CHECK_EQUAL(line, "2");
            TEST_CHECK(! std::getline(stdout_stream, line));
        }
    } test_captured_pipe_command;

    struct PrefixStdoutTest : TestCase
    {
        PrefixStdoutTest() : TestCase("prefix stdout") { }

        void run()
        {
            std::stringstream stdout_stream;
            Process echo_process(ProcessCommand({ "bash", "-c", "echo monkey ; echo in ; echo space"}));
            echo_process.capture_stdout(stdout_stream);
            echo_process.prefix_stdout("prefix> ");

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "prefix> monkey\nprefix> in\nprefix> space\n");
        }
    } test_prefix_stdout;

    struct PrefixStderrTest : TestCase
    {
        PrefixStderrTest() : TestCase("prefix stderr") { }

        void run()
        {
            std::stringstream stderr_stream;
            Process echo_process(ProcessCommand({ "bash", "-c", "echo monkey 1>&2 ; echo in 1>&2 ; echo space 1>&2"}));
            echo_process.capture_stderr(stderr_stream);
            echo_process.prefix_stderr("prefix> ");

            TEST_CHECK_EQUAL(echo_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stderr_stream.str(), "prefix> monkey\nprefix> in\nprefix> space\n");
        }
    } test_prefix_stderr;

    struct ClearenvTest : TestCase
    {
        ClearenvTest() : TestCase("clearenv") { }

        void run()
        {
            ::setenv("BANANAS", "IN PYJAMAS", 1);
            std::stringstream stdout_stream;
            Process printenv_process(ProcessCommand({"printenv", "BANANAS"}));
            printenv_process.capture_stdout(stdout_stream);
            printenv_process.clearenv();

            TEST_CHECK_EQUAL(printenv_process.run().wait(), 1);
            TEST_CHECK_EQUAL(stdout_stream.str(), "");
        }
    } test_clearenv;

    struct SendFDTest : TestCase
    {
        SendFDTest() : TestCase("send fd") { }

        void run()
        {
            std::stringstream stdout_stream, in_stream;
            in_stream << "monkey" << std::endl;

            Process cat_process(ProcessCommand({"bash", "-c", "cat <&$MAGIC_FD"}));
            cat_process.send_input_to_fd(in_stream, -1, "MAGIC_FD");
            cat_process.capture_stdout(stdout_stream);

            TEST_CHECK_EQUAL(cat_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "monkey\n");
        }
    } test_send_fd;

    struct SendFDFixedTest : TestCase
    {
        SendFDFixedTest() : TestCase("send fd fixed") { }

        void run()
        {
            std::stringstream stdout_stream, in_stream;
            in_stream << "monkey" << std::endl;

            Process cat_process(ProcessCommand({"bash", "-c", "cat <&5"}));
            cat_process.send_input_to_fd(in_stream, 5, "");
            cat_process.capture_stdout(stdout_stream);

            TEST_CHECK_EQUAL(cat_process.run().wait(), 0);
            TEST_CHECK_EQUAL(stdout_stream.str(), "monkey\n");
        }
    } test_send_fd_fixed;
}