summaryrefslogtreecommitdiffstats
path: root/contrib/atf/atf-run/config_test.cpp
blob: 5b103a78acbbc9b00f479174f4a3b6d3f7446bef (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
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2010 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 "atf-c++/detail/env.hpp"
#include "atf-c++/detail/test_helpers.hpp"
#include "atf-c++/config.hpp"
#include "atf-c++/macros.hpp"

#include "config.hpp"

namespace impl = atf::atf_run;
namespace detail = atf::atf_run::detail;

using atf::tests::vars_map;

namespace atf {
namespace config {

void __reinit(void);

}  // namespace config
}  // namespace atf

// -------------------------------------------------------------------------
// Tests for the "config" parser.
// -------------------------------------------------------------------------

class config_reader : protected detail::atf_config_reader {
    void
    got_var(const std::string& name, const std::string& val)
    {
        m_calls.push_back("got_var(" + name + ", " + val + ")");
    }

    void
    got_eof(void)
    {
        m_calls.push_back("got_eof()");
    }

public:
    config_reader(std::istream& is) :
        detail::atf_config_reader(is)
    {
    }

    void
    read(void)
    {
        atf_config_reader::read();
    }

    std::vector< std::string > m_calls;
};

ATF_TEST_CASE_WITHOUT_HEAD(config_1);
ATF_TEST_CASE_BODY(config_1)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
    ;

    const char* exp_calls[] = {
        "got_eof()",
        NULL
    };

    const char* exp_errors[] = {
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_2);
ATF_TEST_CASE_BODY(config_2)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "# This is a comment on a line of its own.\n"
        "# And this is another one.\n"
        "\n"
        "	    # Another after some whitespace.\n"
        "\n"
        "# The last one after an empty line.\n"
    ;

    const char* exp_calls[] = {
        "got_eof()",
        NULL
    };

    const char* exp_errors[] = {
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_3);
ATF_TEST_CASE_BODY(config_3)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "var1=value1\n"
        "var2 = value2\n"
        "var3	=	value3\n"
        "var4	    =	    value4\n"
        "\n"
        "var5=value5\n"
        "    var6=value6\n"
        "\n"
        "var7 = \"This is a long value.\"\n"
        "var8 = \"Single-word\"\n"
        "var9 = \"    Single-word	\"\n"
        "var10 = Single-word\n"
    ;

    const char* exp_calls[] = {
        "got_var(var1, value1)",
        "got_var(var2, value2)",
        "got_var(var3, value3)",
        "got_var(var4, value4)",
        "got_var(var5, value5)",
        "got_var(var6, value6)",
        "got_var(var7, This is a long value.)",
        "got_var(var8, Single-word)",
        "got_var(var9,     Single-word	)",
        "got_var(var10, Single-word)",
        "got_eof()",
        NULL
    };

    const char* exp_errors[] = {
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_4);
ATF_TEST_CASE_BODY(config_4)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "foo = bar # A comment.\n"
    ;

    const char* exp_calls[] = {
        "got_var(foo, bar)",
        "got_eof()",
        NULL
    };

    const char* exp_errors[] = {
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_50);
ATF_TEST_CASE_BODY(config_50)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "foo\n"
    ;

    const char* exp_calls[] = {
        NULL
    };

    const char* exp_errors[] = {
        "3: Unexpected token `<<NEWLINE>>'; expected equal sign",
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_51);
ATF_TEST_CASE_BODY(config_51)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "foo bar\n"
        "baz\n"
    ;

    const char* exp_calls[] = {
        NULL
    };

    const char* exp_errors[] = {
        "3: Unexpected token `bar'; expected equal sign",
        "4: Unexpected token `<<NEWLINE>>'; expected equal sign",
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_52);
ATF_TEST_CASE_BODY(config_52)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "foo =\n"
        "bar = # A comment.\n"
    ;

    const char* exp_calls[] = {
        NULL
    };

    const char* exp_errors[] = {
        "3: Unexpected token `<<NEWLINE>>'; expected word or quoted string",
        "4: Unexpected token `#'; expected word or quoted string",
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_53);
ATF_TEST_CASE_BODY(config_53)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "foo = \"Correct value\" # With comment.\n"
        "\n"
        "bar = # A comment.\n"
        "\n"
        "baz = \"Last variable\"\n"
        "\n"
        "# End of file.\n"
    ;

    const char* exp_calls[] = {
        "got_var(foo, Correct value)",
        NULL
    };

    const char* exp_errors[] = {
        "5: Unexpected token `#'; expected word or quoted string",
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

ATF_TEST_CASE_WITHOUT_HEAD(config_54);
ATF_TEST_CASE_BODY(config_54)
{
    const char* input =
        "Content-Type: application/X-atf-config; version=\"1\"\n"
        "\n"
        "foo = \"\n"
        "bar = \"text\n"
        "baz = \"te\\\"xt\n"
        "last = \"\\\"\n"
    ;

    const char* exp_calls[] = {
        NULL
    };

    const char* exp_errors[] = {
        "3: Missing double quotes before end of line",
        "4: Missing double quotes before end of line",
        "5: Missing double quotes before end of line",
        "6: Missing double quotes before end of line",
        NULL
    };

    do_parser_test< config_reader >(input, exp_calls, exp_errors);
}

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

ATF_TEST_CASE(merge_configs_both_empty);
ATF_TEST_CASE_HEAD(merge_configs_both_empty) {}
ATF_TEST_CASE_BODY(merge_configs_both_empty) {
    vars_map lower, upper;

    ATF_REQUIRE(impl::merge_configs(lower, upper).empty());
}

ATF_TEST_CASE(merge_configs_lower_empty);
ATF_TEST_CASE_HEAD(merge_configs_lower_empty) {}
ATF_TEST_CASE_BODY(merge_configs_lower_empty) {
    vars_map lower, upper;
    upper["var"] = "value";

    vars_map merged = impl::merge_configs(lower, upper);
    ATF_REQUIRE_EQ("value", merged["var"]);
}

ATF_TEST_CASE(merge_configs_upper_empty);
ATF_TEST_CASE_HEAD(merge_configs_upper_empty) {}
ATF_TEST_CASE_BODY(merge_configs_upper_empty) {
    vars_map lower, upper;
    lower["var"] = "value";

    vars_map merged = impl::merge_configs(lower, upper);
    ATF_REQUIRE_EQ("value", merged["var"]);
}

ATF_TEST_CASE(merge_configs_mixed);
ATF_TEST_CASE_HEAD(merge_configs_mixed) {}
ATF_TEST_CASE_BODY(merge_configs_mixed) {
    vars_map lower, upper;
    lower["var1"] = "value1";
    lower["var2"] = "value2-l";
    upper["var2"] = "value2-u";
    upper["var3"] = "value3";

    vars_map merged = impl::merge_configs(lower, upper);
    ATF_REQUIRE_EQ("value1", merged["var1"]);
    ATF_REQUIRE_EQ("value2-u", merged["var2"]);
    ATF_REQUIRE_EQ("value3", merged["var3"]);
}

ATF_TEST_CASE(read_config_files_none);
ATF_TEST_CASE_HEAD(read_config_files_none) {}
ATF_TEST_CASE_BODY(read_config_files_none) {
    atf::env::set("ATF_CONFDIR", ".");
    atf::config::__reinit();
    ATF_REQUIRE(vars_map() == impl::read_config_files("test-suite"));
}

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

ATF_INIT_TEST_CASES(tcs)
{
    ATF_ADD_TEST_CASE(tcs, config_1);
    ATF_ADD_TEST_CASE(tcs, config_2);
    ATF_ADD_TEST_CASE(tcs, config_3);
    ATF_ADD_TEST_CASE(tcs, config_4);
    ATF_ADD_TEST_CASE(tcs, config_50);
    ATF_ADD_TEST_CASE(tcs, config_51);
    ATF_ADD_TEST_CASE(tcs, config_52);
    ATF_ADD_TEST_CASE(tcs, config_53);
    ATF_ADD_TEST_CASE(tcs, config_54);

    ATF_ADD_TEST_CASE(tcs, merge_configs_both_empty);
    ATF_ADD_TEST_CASE(tcs, merge_configs_lower_empty);
    ATF_ADD_TEST_CASE(tcs, merge_configs_upper_empty);
    ATF_ADD_TEST_CASE(tcs, merge_configs_mixed);

    ATF_ADD_TEST_CASE(tcs, read_config_files_none);
}
OpenPOWER on IntegriCloud