File: test-plugin-init-fail.cpp

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (80 lines) | stat: -rw-r--r-- 2,125 bytes parent folder | download | duplicates (2)
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
/*
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Copyright (C) 2024 EfficiOS, Inc.
 */

#include <babeltrace2/babeltrace.h>

#include "common/common.h"
#include "cpp-common/bt2/exc.hpp"
#include "cpp-common/bt2/plugin-load.hpp"
#include "cpp-common/bt2c/c-string-view.hpp"
#include "cpp-common/vendor/fmt/core.h"

#include "tap/tap.h"

namespace {

void testFailOnLoadErrorTrue(const char * const pluginDir)
{
    plan_tests(1);

    try {
        bt2::findAllPluginsFromDir(pluginDir, false, true);
        bt_common_abort();
    } catch (const bt2::Error& exc) {
        fmt::print("{}\n", exc.what());

        const auto error = bt_current_thread_take_error();

        /*
         * The last error cause must be the one which the initialization
         * function of our plugin appended.
         */
        const auto cause = bt_error_borrow_cause_by_index(error, 0);
        const bt2c::CStringView msg {bt_error_cause_get_message(cause)};

        ok(msg == "This is the error message", "Message of error cause 0 is expected");
        bt_error_release(error);
    }
}

void testFailOnLoadErrorFalse(const char * const pluginDir)
{
    plan_tests(1);

    const auto plugins = bt2::findAllPluginsFromDir(pluginDir, false, false);

    ok(!plugins, "No plugin set returned");
}

} /* namespace */

int main(const int argc, const char ** const argv)
{
    if (argc != 3) {
        fmt::print(stderr,
                   "Usage: {} INIT-FAIL-PLUGIN-DIR FAIL-ON-LOAD-ERROR\n"
                   "\n"
                   "FAIL-ON-LOAD-ERROR must be `yes` or `no`\n",
                   argv[0]);
        return 1;
    }

    const auto pluginDir = argv[1];
    const bt2c::CStringView failOnLoadErrorStr {argv[2]};

    if (failOnLoadErrorStr == "yes") {
        testFailOnLoadErrorTrue(pluginDir);
    } else if (failOnLoadErrorStr == "no") {
        testFailOnLoadErrorFalse(pluginDir);
    } else {
        fmt::print(stderr,
                   "ERROR: Invalid value `{}` for FAIL-ON-LOAD-ERROR (expecting `yes` or `no`).\n",
                   failOnLoadErrorStr);
        return 1;
    }

    return exit_status();
}