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