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