summaryrefslogtreecommitdiffstats
path: root/unit/atf-src/tools/process_test.cpp
blob: 70da919dd0ebadcfb7b273af0f7a4c8a3cebe8da (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
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2008 The NetBSD Foundation, Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
// CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
// IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
// IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include <cstdlib>
#include <cstring>

#include <atf-c++.hpp>

#include "process.hpp"
#include "test_helpers.hpp"

// TODO: Testing the fork function is a huge task and I'm afraid of
// copy/pasting tons of stuff from the C version.  I'd rather not do that
// until some code can be shared, which cannot happen until the C++ binding
// is cleaned by a fair amount.  Instead... just rely (at the moment) on
// the system tests for the tools using this module.

// ------------------------------------------------------------------------
// Auxiliary functions.
// ------------------------------------------------------------------------

static
std::size_t
array_size(const char* const* array)
{
    std::size_t size = 0;

    for (const char* const* ptr = array; *ptr != NULL; ptr++)
        size++;

    return size;
}

static
tools::process::status
exec_process_helpers(const atf::tests::tc& tc, const char* helper_name)
{
    using tools::process::exec;

    const tools::fs::path helpers = tools::fs::path(tc.get_config_var("srcdir")) /
        "process_helpers";

    std::vector< std::string > argv;
    argv.push_back(helpers.leaf_name());
    argv.push_back(helper_name);

    return exec(helpers,
                tools::process::argv_array(argv),
                tools::process::stream_inherit(),
                tools::process::stream_inherit());
}

// ------------------------------------------------------------------------
// Tests for the "argv_array" type.
// ------------------------------------------------------------------------

ATF_TEST_CASE(argv_array_init_carray);
ATF_TEST_CASE_HEAD(argv_array_init_carray)
{
    set_md_var("descr", "Tests that argv_array is correctly constructed "
               "from a C-style array of strings");
}
ATF_TEST_CASE_BODY(argv_array_init_carray)
{
    {
        const char* const carray[] = { NULL };
        tools::process::argv_array argv(carray);

        ATF_REQUIRE_EQ(argv.size(), 0);
    }

    {
        const char* const carray[] = { "arg0", NULL };
        tools::process::argv_array argv(carray);

        ATF_REQUIRE_EQ(argv.size(), 1);
        ATF_REQUIRE(std::strcmp(argv[0], carray[0]) == 0);
    }

    {
        const char* const carray[] = { "arg0", "arg1", "arg2", NULL };
        tools::process::argv_array argv(carray);

        ATF_REQUIRE_EQ(argv.size(), 3);
        ATF_REQUIRE(std::strcmp(argv[0], carray[0]) == 0);
        ATF_REQUIRE(std::strcmp(argv[1], carray[1]) == 0);
        ATF_REQUIRE(std::strcmp(argv[2], carray[2]) == 0);
    }
}

ATF_TEST_CASE(argv_array_init_col);
ATF_TEST_CASE_HEAD(argv_array_init_col)
{
    set_md_var("descr", "Tests that argv_array is correctly constructed "
               "from a string collection");
}
ATF_TEST_CASE_BODY(argv_array_init_col)
{
    {
        std::vector< std::string > col;
        tools::process::argv_array argv(col);

        ATF_REQUIRE_EQ(argv.size(), 0);
    }

    {
        std::vector< std::string > col;
        col.push_back("arg0");
        tools::process::argv_array argv(col);

        ATF_REQUIRE_EQ(argv.size(), 1);
        ATF_REQUIRE_EQ(argv[0], col[0]);
    }

    {
        std::vector< std::string > col;
        col.push_back("arg0");
        col.push_back("arg1");
        col.push_back("arg2");
        tools::process::argv_array argv(col);

        ATF_REQUIRE_EQ(argv.size(), 3);
        ATF_REQUIRE_EQ(argv[0], col[0]);
        ATF_REQUIRE_EQ(argv[1], col[1]);
        ATF_REQUIRE_EQ(argv[2], col[2]);
    }
}

ATF_TEST_CASE(argv_array_init_empty);
ATF_TEST_CASE_HEAD(argv_array_init_empty)
{
    set_md_var("descr", "Tests that argv_array is correctly constructed "
               "by the default constructor");
}
ATF_TEST_CASE_BODY(argv_array_init_empty)
{
    tools::process::argv_array argv;

    ATF_REQUIRE_EQ(argv.size(), 0);
}

ATF_TEST_CASE(argv_array_init_varargs);
ATF_TEST_CASE_HEAD(argv_array_init_varargs)
{
    set_md_var("descr", "Tests that argv_array is correctly constructed "
               "from a variable list of arguments");
}
ATF_TEST_CASE_BODY(argv_array_init_varargs)
{
    {
        tools::process::argv_array argv("arg0", NULL);

        ATF_REQUIRE_EQ(argv.size(), 1);
        ATF_REQUIRE_EQ(argv[0], std::string("arg0"));
    }

    {
        tools::process::argv_array argv("arg0", "arg1", "arg2", NULL);

        ATF_REQUIRE_EQ(argv.size(), 3);
        ATF_REQUIRE_EQ(argv[0], std::string("arg0"));
        ATF_REQUIRE_EQ(argv[1], std::string("arg1"));
        ATF_REQUIRE_EQ(argv[2], std::string("arg2"));
    }
}

ATF_TEST_CASE(argv_array_assign);
ATF_TEST_CASE_HEAD(argv_array_assign)
{
    set_md_var("descr", "Tests that assigning an argv_array works");
}
ATF_TEST_CASE_BODY(argv_array_assign)
{
    using tools::process::argv_array;

    const char* const carray1[] = { "arg1", NULL };
    const char* const carray2[] = { "arg1", "arg2", NULL };

    std::auto_ptr< argv_array > argv1(new argv_array(carray1));
    std::auto_ptr< argv_array > argv2(new argv_array(carray2));

    *argv2 = *argv1;
    ATF_REQUIRE_EQ(argv2->size(), argv1->size());
    ATF_REQUIRE(std::strcmp((*argv2)[0], (*argv1)[0]) == 0);

    ATF_REQUIRE(argv2->exec_argv() != argv1->exec_argv());
    argv1.release();
    {
        const char* const* eargv2 = argv2->exec_argv();
        ATF_REQUIRE(std::strcmp(eargv2[0], carray1[0]) == 0);
        ATF_REQUIRE_EQ(eargv2[1], static_cast< const char* >(NULL));
    }

    argv2.release();
}

ATF_TEST_CASE(argv_array_copy);
ATF_TEST_CASE_HEAD(argv_array_copy)
{
    set_md_var("descr", "Tests that copying an argv_array constructed from "
               "a C-style array of strings works");
}
ATF_TEST_CASE_BODY(argv_array_copy)
{
    using tools::process::argv_array;

    const char* const carray[] = { "arg0", NULL };

    std::auto_ptr< argv_array > argv1(new argv_array(carray));
    std::auto_ptr< argv_array > argv2(new argv_array(*argv1));

    ATF_REQUIRE_EQ(argv2->size(), argv1->size());
    ATF_REQUIRE(std::strcmp((*argv2)[0], (*argv1)[0]) == 0);

    ATF_REQUIRE(argv2->exec_argv() != argv1->exec_argv());
    argv1.release();
    {
        const char* const* eargv2 = argv2->exec_argv();
        ATF_REQUIRE(std::strcmp(eargv2[0], carray[0]) == 0);
        ATF_REQUIRE_EQ(eargv2[1], static_cast< const char* >(NULL));
    }

    argv2.release();
}

ATF_TEST_CASE(argv_array_exec_argv);
ATF_TEST_CASE_HEAD(argv_array_exec_argv)
{
    set_md_var("descr", "Tests that the exec argv provided by an argv_array "
               "is correct");
}
ATF_TEST_CASE_BODY(argv_array_exec_argv)
{
    using tools::process::argv_array;

    {
        argv_array argv;
        const char* const* eargv = argv.exec_argv();
        ATF_REQUIRE_EQ(array_size(eargv), 0);
        ATF_REQUIRE_EQ(eargv[0], static_cast< const char* >(NULL));
    }

    {
        const char* const carray[] = { "arg0", NULL };
        argv_array argv(carray);
        const char* const* eargv = argv.exec_argv();
        ATF_REQUIRE_EQ(array_size(eargv), 1);
        ATF_REQUIRE(std::strcmp(eargv[0], "arg0") == 0);
        ATF_REQUIRE_EQ(eargv[1], static_cast< const char* >(NULL));
    }

    {
        std::vector< std::string > col;
        col.push_back("arg0");
        argv_array argv(col);
        const char* const* eargv = argv.exec_argv();
        ATF_REQUIRE_EQ(array_size(eargv), 1);
        ATF_REQUIRE(std::strcmp(eargv[0], "arg0") == 0);
        ATF_REQUIRE_EQ(eargv[1], static_cast< const char* >(NULL));
    }
}

ATF_TEST_CASE(argv_array_iter);
ATF_TEST_CASE_HEAD(argv_array_iter)
{
    set_md_var("descr", "Tests that an argv_array can be iterated");
}
ATF_TEST_CASE_BODY(argv_array_iter)
{
    using tools::process::argv_array;

    std::vector< std::string > vector;
    vector.push_back("arg0");
    vector.push_back("arg1");
    vector.push_back("arg2");

    argv_array argv(vector);
    ATF_REQUIRE_EQ(argv.size(), 3);
    std::vector< std::string >::size_type pos = 0;
    for (argv_array::const_iterator iter = argv.begin(); iter != argv.end();
         iter++) {
        ATF_REQUIRE_EQ(*iter, vector[pos]);
        pos++;
    }
}

// ------------------------------------------------------------------------
// Tests cases for the free functions.
// ------------------------------------------------------------------------

ATF_TEST_CASE(exec_failure);
ATF_TEST_CASE_HEAD(exec_failure)
{
    set_md_var("descr", "Tests execing a command that reports failure");
}
ATF_TEST_CASE_BODY(exec_failure)
{
    const tools::process::status s = exec_process_helpers(*this, "exit-failure");
    ATF_REQUIRE(s.exited());
    ATF_REQUIRE_EQ(s.exitstatus(), EXIT_FAILURE);
}

ATF_TEST_CASE(exec_success);
ATF_TEST_CASE_HEAD(exec_success)
{
    set_md_var("descr", "Tests execing a command that reports success");
}
ATF_TEST_CASE_BODY(exec_success)
{
    const tools::process::status s = exec_process_helpers(*this, "exit-success");
    ATF_REQUIRE(s.exited());
    ATF_REQUIRE_EQ(s.exitstatus(), EXIT_SUCCESS);
}

// ------------------------------------------------------------------------
// Main.
// ------------------------------------------------------------------------

ATF_INIT_TEST_CASES(tcs)
{
    // Add the test cases for the "argv_array" type.
    ATF_ADD_TEST_CASE(tcs, argv_array_assign);
    ATF_ADD_TEST_CASE(tcs, argv_array_copy);
    ATF_ADD_TEST_CASE(tcs, argv_array_exec_argv);
    ATF_ADD_TEST_CASE(tcs, argv_array_init_carray);
    ATF_ADD_TEST_CASE(tcs, argv_array_init_col);
    ATF_ADD_TEST_CASE(tcs, argv_array_init_empty);
    ATF_ADD_TEST_CASE(tcs, argv_array_init_varargs);
    ATF_ADD_TEST_CASE(tcs, argv_array_iter);

    // Add the test cases for the free functions.
    ATF_ADD_TEST_CASE(tcs, exec_failure);
    ATF_ADD_TEST_CASE(tcs, exec_success);
}