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