summaryrefslogtreecommitdiffstats
path: root/contrib/atf/atf-report/reader.cpp
blob: 8f5fde791c8ab9a91b9e39835c5a7353a2f1e850 (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
//
// Automated Testing Framework (atf)
//
// Copyright (c) 2007 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.
//

extern "C" {
#include <sys/time.h>
}

#include <map>
#include <sstream>
#include <utility>

#include "atf-c/defs.h"

#include "atf-c++/detail/parser.hpp"
#include "atf-c++/detail/sanity.hpp"
#include "atf-c++/detail/text.hpp"

#include "reader.hpp"

namespace impl = atf::atf_report;
#define IMPL_NAME "atf::atf_report"

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

template< typename Type >
Type
string_to_int(const std::string& str)
{
    std::istringstream ss(str);
    Type s;
    ss >> s;

    return s;
}

// ------------------------------------------------------------------------
// The "atf_tps" auxiliary parser.
// ------------------------------------------------------------------------

namespace atf_tps {

static const atf::parser::token_type eof_type = 0;
static const atf::parser::token_type nl_type = 1;
static const atf::parser::token_type text_type = 2;
static const atf::parser::token_type colon_type = 3;
static const atf::parser::token_type comma_type = 4;
static const atf::parser::token_type tps_count_type = 5;
static const atf::parser::token_type tp_start_type = 6;
static const atf::parser::token_type tp_end_type = 7;
static const atf::parser::token_type tc_start_type = 8;
static const atf::parser::token_type tc_so_type = 9;
static const atf::parser::token_type tc_se_type = 10;
static const atf::parser::token_type tc_end_type = 11;
static const atf::parser::token_type passed_type = 12;
static const atf::parser::token_type failed_type = 13;
static const atf::parser::token_type skipped_type = 14;
static const atf::parser::token_type info_type = 16;
static const atf::parser::token_type expected_death_type = 17;
static const atf::parser::token_type expected_exit_type = 18;
static const atf::parser::token_type expected_failure_type = 19;
static const atf::parser::token_type expected_signal_type = 20;
static const atf::parser::token_type expected_timeout_type = 21;

class tokenizer : public atf::parser::tokenizer< std::istream > {
public:
    tokenizer(std::istream& is, size_t curline) :
        atf::parser::tokenizer< std::istream >
            (is, true, eof_type, nl_type, text_type, curline)
    {
        add_delim(':', colon_type);
        add_delim(',', comma_type);
        add_keyword("tps-count", tps_count_type);
        add_keyword("tp-start", tp_start_type);
        add_keyword("tp-end", tp_end_type);
        add_keyword("tc-start", tc_start_type);
        add_keyword("tc-so", tc_so_type);
        add_keyword("tc-se", tc_se_type);
        add_keyword("tc-end", tc_end_type);
        add_keyword("passed", passed_type);
        add_keyword("failed", failed_type);
        add_keyword("skipped", skipped_type);
        add_keyword("info", info_type);
        add_keyword("expected_death", expected_death_type);
        add_keyword("expected_exit", expected_exit_type);
        add_keyword("expected_failure", expected_failure_type);
        add_keyword("expected_signal", expected_signal_type);
        add_keyword("expected_timeout", expected_timeout_type);
    }
};

} // namespace atf_tps

struct timeval
read_timeval(atf::parser::parser< atf_tps::tokenizer >& parser)
{
    using namespace atf_tps;

    atf::parser::token t = parser.expect(text_type, "timestamp");
    const std::string::size_type divider = t.text().find('.');
    if (divider == std::string::npos || divider == 0 ||
        divider == t.text().length() - 1)
        throw atf::parser::parse_error(t.lineno(),
                                       "Malformed timestamp value " + t.text());

    struct timeval tv;
    tv.tv_sec = string_to_int< long >(t.text().substr(0, divider));
    tv.tv_usec = string_to_int< long >(t.text().substr(divider + 1));
    return tv;
}

// ------------------------------------------------------------------------
// The "atf_tps_reader" class.
// ------------------------------------------------------------------------

impl::atf_tps_reader::atf_tps_reader(std::istream& is) :
    m_is(is)
{
}

impl::atf_tps_reader::~atf_tps_reader(void)
{
}

void
impl::atf_tps_reader::got_info(
    const std::string& what ATF_DEFS_ATTRIBUTE_UNUSED,
    const std::string& val ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_ntps(size_t ntps ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_tp_start(
    const std::string& tp ATF_DEFS_ATTRIBUTE_UNUSED,
    size_t ntcs ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_tp_end(
    struct timeval* tv ATF_DEFS_ATTRIBUTE_UNUSED,
    const std::string& reason ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_tc_start(
    const std::string& tcname ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_tc_stdout_line(
    const std::string& line ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_tc_stderr_line(
    const std::string& line ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_tc_end(
    const std::string& state ATF_DEFS_ATTRIBUTE_UNUSED,
    struct timeval* tv ATF_DEFS_ATTRIBUTE_UNUSED,
    const std::string& reason ATF_DEFS_ATTRIBUTE_UNUSED)
{
}

void
impl::atf_tps_reader::got_eof(void)
{
}

void
impl::atf_tps_reader::read_info(void* pptr)
{
    using atf::parser::parse_error;
    using namespace atf_tps;

    atf::parser::parser< tokenizer >& p =
        *reinterpret_cast< atf::parser::parser< tokenizer >* >
        (pptr);

    (void)p.expect(colon_type, "`:'");

    atf::parser::token t = p.expect(text_type, "info property name");
    (void)p.expect(comma_type, "`,'");
    got_info(t.text(), atf::text::trim(p.rest_of_line()));

    (void)p.expect(nl_type, "new line");
}

void
impl::atf_tps_reader::read_tp(void* pptr)
{
    using atf::parser::parse_error;
    using namespace atf_tps;

    atf::parser::parser< tokenizer >& p =
        *reinterpret_cast< atf::parser::parser< tokenizer >* >
        (pptr);

    atf::parser::token t = p.expect(tp_start_type,
                                    "start of test program");

    t = p.expect(colon_type, "`:'");

    struct timeval s1 = read_timeval(p);

    t = p.expect(comma_type, "`,'");

    t = p.expect(text_type, "test program name");
    std::string tpname = t.text();

    t = p.expect(comma_type, "`,'");

    t = p.expect(text_type, "number of test programs");
    size_t ntcs = string_to_int< std::size_t >(t.text());

    t = p.expect(nl_type, "new line");

    ATF_PARSER_CALLBACK(p, got_tp_start(tpname, ntcs));

    size_t i = 0;
    while (p.good() && i < ntcs) {
        try {
            read_tc(&p);
            i++;
        } catch (const parse_error& pe) {
            p.add_error(pe);
            p.reset(nl_type);
        }
    }
    t = p.expect(tp_end_type, "end of test program");

    t = p.expect(colon_type, "`:'");

    struct timeval s2 = read_timeval(p);

    struct timeval s3;
    timersub(&s2, &s1, &s3);

    t = p.expect(comma_type, "`,'");

    t = p.expect(text_type, "test program name");
    if (t.text() != tpname)
        throw parse_error(t.lineno(), "Test program name used in "
                                      "terminator does not match "
                                      "opening");

    t = p.expect(nl_type, comma_type,
                 "new line or comma_type");
    std::string reason;
    if (t.type() == comma_type) {
        reason = text::trim(p.rest_of_line());
        if (reason.empty())
            throw parse_error(t.lineno(),
                              "Empty reason for failed test program");
        t = p.next();
    }

    ATF_PARSER_CALLBACK(p, got_tp_end(&s3, reason));
}

void
impl::atf_tps_reader::read_tc(void* pptr)
{
    using atf::parser::parse_error;
    using namespace atf_tps;

    atf::parser::parser< tokenizer >& p =
        *reinterpret_cast< atf::parser::parser< tokenizer >* >
        (pptr);

    atf::parser::token t = p.expect(tc_start_type, "start of test case");

    t = p.expect(colon_type, "`:'");

    struct timeval s1 = read_timeval(p);

    t = p.expect(comma_type, "`,'");

    t = p.expect(text_type, "test case name");
    std::string tcname = t.text();

    ATF_PARSER_CALLBACK(p, got_tc_start(tcname));

    t = p.expect(nl_type, "new line");

    t = p.expect(tc_end_type, tc_so_type, tc_se_type,
                 "end of test case or test case's stdout/stderr line");
    while (t.type() != tc_end_type &&
           (t.type() == tc_so_type || t.type() == tc_se_type)) {
        atf::parser::token t2 = t;

        t = p.expect(colon_type, "`:'");

        std::string line = p.rest_of_line();

        if (t2.type() == tc_so_type) {
            ATF_PARSER_CALLBACK(p, got_tc_stdout_line(line));
        } else {
            INV(t2.type() == tc_se_type);
            ATF_PARSER_CALLBACK(p, got_tc_stderr_line(line));
        }

        t = p.expect(nl_type, "new line");

        t = p.expect(tc_end_type, tc_so_type, tc_se_type,
                     "end of test case or test case's stdout/stderr line");
    }

    t = p.expect(colon_type, "`:'");

    struct timeval s2 = read_timeval(p);

    struct timeval s3;
    timersub(&s2, &s1, &s3);

    t = p.expect(comma_type, "`,'");

    t = p.expect(text_type, "test case name");
    if (t.text() != tcname)
        throw parse_error(t.lineno(),
                          "Test case name used in terminator does not "
                          "match opening");

    t = p.expect(comma_type, "`,'");

    t = p.expect(expected_death_type, expected_exit_type, expected_failure_type,
        expected_signal_type, expected_timeout_type, passed_type, failed_type,
        skipped_type, "expected_{death,exit,failure,signal,timeout}, failed, "
        "passed or skipped");
    if (t.type() == passed_type) {
        ATF_PARSER_CALLBACK(p, got_tc_end("passed", &s3, ""));
    } else {
        std::string state;
        if (t.type() == expected_death_type) state = "expected_death";
        else if (t.type() == expected_exit_type) state = "expected_exit";
        else if (t.type() == expected_failure_type) state = "expected_failure";
        else if (t.type() == expected_signal_type) state = "expected_signal";
        else if (t.type() == expected_timeout_type) state = "expected_timeout";
        else if (t.type() == failed_type) state = "failed";
        else if (t.type() == skipped_type) state = "skipped";
        else UNREACHABLE;

        t = p.expect(comma_type, "`,'");
        std::string reason = text::trim(p.rest_of_line());
        if (reason.empty())
            throw parse_error(t.lineno(), "Empty reason for " + state +
                " test case result");
        ATF_PARSER_CALLBACK(p, got_tc_end(state, &s3, reason));
    }

    t = p.expect(nl_type, "new line");
}

void
impl::atf_tps_reader::read(void)
{
    using atf::parser::parse_error;
    using namespace atf_tps;

    std::pair< size_t, atf::parser::headers_map > hml =
        atf::parser::read_headers(m_is, 1);
    atf::parser::validate_content_type(hml.second, "application/X-atf-tps", 3);

    tokenizer tkz(m_is, hml.first);
    atf::parser::parser< tokenizer > p(tkz);

    try {
        atf::parser::token t;

        while ((t = p.expect(tps_count_type, info_type, "tps-count or info "
                             "field")).type() == info_type)
            read_info(&p);

        t = p.expect(colon_type, "`:'");

        t = p.expect(text_type, "number of test programs");
        size_t ntps = string_to_int< std::size_t >(t.text());
        ATF_PARSER_CALLBACK(p, got_ntps(ntps));

        t = p.expect(nl_type, "new line");

        size_t i = 0;
        while (p.good() && i < ntps) {
            try {
                read_tp(&p);
                i++;
            } catch (const parse_error& pe) {
                p.add_error(pe);
                p.reset(nl_type);
            }
        }

        while ((t = p.expect(eof_type, info_type, "end of stream or info "
                             "field")).type() == info_type)
            read_info(&p);
        ATF_PARSER_CALLBACK(p, got_eof());
    } catch (const parse_error& pe) {
        p.add_error(pe);
        p.reset(nl_type);
    }
}
OpenPOWER on IntegriCloud