Commit | Line | Data |
---|---|---|
1501a7f3 JG |
1 | /* |
2 | * Copyright (c) - 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License as published by as | |
6 | * published by the Free Software Foundation; only version 2 of the License. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
18 | #include <tap/tap.h> | |
f40ef1d5 | 19 | #include <common/config/session-config.h> |
1501a7f3 JG |
20 | #include <common/utils.h> |
21 | #include <string.h> | |
22 | ||
23 | struct state { | |
24 | int section_1; | |
25 | int section_2; | |
26 | int section_3; | |
27 | int section_global; | |
28 | int text_entry; | |
29 | int int_entry; | |
30 | }; | |
31 | ||
32 | int lttng_opt_quiet = 1; | |
33 | int lttng_opt_verbose = 0; | |
c7e35b03 | 34 | int lttng_opt_mi; |
1501a7f3 JG |
35 | |
36 | int entry_handler(const struct config_entry *entry, | |
37 | struct state *state) | |
38 | { | |
39 | int ret = 0; | |
40 | ||
41 | if (!entry || !state) { | |
42 | ret = -1; | |
43 | goto end; | |
44 | } | |
45 | ||
46 | if (!strcmp(entry->section, "section1")) { | |
47 | state->section_1 = 1; | |
48 | if (!strcmp(entry->name, "section1_entry") && | |
49 | !strcmp(entry->value, "42")) { | |
50 | state->int_entry = 1; | |
51 | } | |
52 | } | |
53 | ||
54 | if (!strcmp(entry->section, "section2")) { | |
55 | state->section_2 = 1; | |
56 | } | |
57 | ||
58 | if (!strcmp(entry->section, "section 3")) { | |
59 | state->section_3 = 1; | |
60 | if (!strcmp(entry->name, "name with a space") && | |
61 | !strcmp(entry->value, "another value")) { | |
62 | state->text_entry = 1; | |
63 | } | |
64 | } | |
65 | ||
66 | if (!strcmp(entry->section, "")) { | |
67 | state->section_global = 1; | |
68 | } | |
69 | end: | |
70 | return ret; | |
71 | } | |
72 | ||
73 | int main(int argc, char **argv) | |
74 | { | |
75 | char *path = NULL; | |
76 | int ret; | |
77 | struct state state = {}; | |
78 | ||
79 | if (argc < 2) { | |
80 | diag("Usage: path_to_sample_INI_file"); | |
81 | goto end; | |
82 | } | |
83 | ||
84 | path = utils_expand_path(argv[1]); | |
85 | if (!path) { | |
86 | fail("Failed to resolve sample INI file path") | |
87 | } | |
88 | ||
89 | plan_no_plan(); | |
90 | ret = config_get_section_entries(path, NULL, | |
91 | (config_entry_handler_cb)entry_handler, &state); | |
92 | ok(ret == 0, "Successfully opened a config file, registered to all sections"); | |
93 | ok(state.section_1 && state.section_2 && state.section_3 && | |
94 | state.section_global, "Processed entries from each sections"); | |
95 | ok(state.text_entry, "Text value parsed correctly"); | |
96 | ||
97 | memset(&state, 0, sizeof(struct state)); | |
98 | ret = config_get_section_entries(path, "section1", | |
99 | (config_entry_handler_cb)entry_handler, &state); | |
100 | ok(ret == 0, "Successfully opened a config file, registered to one section"); | |
101 | ok(state.section_1 && !state.section_2 && !state.section_3 && | |
102 | !state.section_global, "Processed an entry from section1 only"); | |
103 | ok(state.int_entry, "Int value parsed correctly"); | |
104 | ||
105 | memset(&state, 0, sizeof(struct state)); | |
106 | ret = config_get_section_entries(path, "", | |
107 | (config_entry_handler_cb)entry_handler, &state); | |
108 | ok(ret == 0, "Successfully opened a config file, registered to the global section"); | |
109 | ok(!state.section_1 && !state.section_2 && !state.section_3 && | |
110 | state.section_global, "Processed an entry from the global section only"); | |
111 | end: | |
112 | free(path); | |
113 | return exit_status(); | |
114 | } |