Fix: syscall event rule: emission sites not compared in is_equal
[lttng-tools.git] / tests / unit / ini_config / ini_config.cpp
CommitLineData
1501a7f3 1/*
9d16b343 2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
1501a7f3 3 *
9d16b343 4 * SPDX-License-Identifier: GPL-2.0-only
1501a7f3 5 *
1501a7f3
JG
6 */
7
c9e313bc 8#include <common/ini-config/ini-config.hpp>
c9e313bc 9#include <common/path.hpp>
28ab034a
JG
10#include <common/utils.hpp>
11
1b0bfc2b 12#include <lttng/constant.h>
1501a7f3 13
28ab034a
JG
14#include <string.h>
15#include <tap/tap.h>
16
f1494934 17namespace {
1501a7f3
JG
18struct state {
19 int section_1;
20 int section_2;
21 int section_3;
22 int section_global;
23 int text_entry;
24 int int_entry;
25};
f1494934 26} /* namespace */
1501a7f3
JG
27
28int lttng_opt_quiet = 1;
29int lttng_opt_verbose = 0;
c7e35b03 30int lttng_opt_mi;
1501a7f3 31
d04ee1a0 32static int entry_handler(const struct config_entry *entry, struct state *state)
1501a7f3
JG
33{
34 int ret = 0;
35
36 if (!entry || !state) {
37 ret = -1;
38 goto end;
39 }
40
41 if (!strcmp(entry->section, "section1")) {
42 state->section_1 = 1;
28ab034a 43 if (!strcmp(entry->name, "section1_entry") && !strcmp(entry->value, "42")) {
1501a7f3
JG
44 state->int_entry = 1;
45 }
46 }
47
48 if (!strcmp(entry->section, "section2")) {
49 state->section_2 = 1;
50 }
51
52 if (!strcmp(entry->section, "section 3")) {
53 state->section_3 = 1;
54 if (!strcmp(entry->name, "name with a space") &&
28ab034a 55 !strcmp(entry->value, "another value")) {
1501a7f3
JG
56 state->text_entry = 1;
57 }
58 }
59
60 if (!strcmp(entry->section, "")) {
61 state->section_global = 1;
62 }
63end:
64 return ret;
65}
66
67int main(int argc, char **argv)
68{
cd9adb8b 69 char *path = nullptr;
1501a7f3
JG
70 int ret;
71 struct state state = {};
72
73 if (argc < 2) {
74 diag("Usage: path_to_sample_INI_file");
75 goto end;
76 }
77
1b0bfc2b
JG
78 if (strlen(argv[1]) >= LTTNG_PATH_MAX) {
79 diag("The provided path exceeds the maximal permitted length of %i bytes",
28ab034a 80 LTTNG_PATH_MAX);
1b0bfc2b
JG
81 goto end;
82 }
1501a7f3
JG
83 path = utils_expand_path(argv[1]);
84 if (!path) {
4f002736 85 fail("Failed to resolve sample INI file path");
1501a7f3
JG
86 }
87
88 plan_no_plan();
28ab034a 89 ret = config_get_section_entries(
cd9adb8b 90 path, nullptr, (config_entry_handler_cb) entry_handler, &state);
1501a7f3 91 ok(ret == 0, "Successfully opened a config file, registered to all sections");
28ab034a
JG
92 ok(state.section_1 && state.section_2 && state.section_3 && state.section_global,
93 "Processed entries from each sections");
1501a7f3
JG
94 ok(state.text_entry, "Text value parsed correctly");
95
96 memset(&state, 0, sizeof(struct state));
28ab034a
JG
97 ret = config_get_section_entries(
98 path, "section1", (config_entry_handler_cb) entry_handler, &state);
1501a7f3 99 ok(ret == 0, "Successfully opened a config file, registered to one section");
28ab034a
JG
100 ok(state.section_1 && !state.section_2 && !state.section_3 && !state.section_global,
101 "Processed an entry from section1 only");
1501a7f3
JG
102 ok(state.int_entry, "Int value parsed correctly");
103
104 memset(&state, 0, sizeof(struct state));
28ab034a 105 ret = config_get_section_entries(path, "", (config_entry_handler_cb) entry_handler, &state);
1501a7f3 106 ok(ret == 0, "Successfully opened a config file, registered to the global section");
28ab034a
JG
107 ok(!state.section_1 && !state.section_2 && !state.section_3 && state.section_global,
108 "Processed an entry from the global section only");
1501a7f3
JG
109end:
110 free(path);
111 return exit_status();
112}
This page took 0.061623 seconds and 5 git commands to generate.