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