Move utils_expand_path and utils_expand_path_keep_symlink to libpath.la
[lttng-tools.git] / tests / unit / ini_config / ini_config.cpp
1 /*
2 * Copyright (C) 2013 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <tap/tap.h>
9 #include <common/ini-config/ini-config.h>
10 #include <common/utils.h>
11 #include <common/path.h>
12 #include <string.h>
13 #include <lttng/constant.h>
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;
26 int lttng_opt_mi;
27
28 static int entry_handler(const struct config_entry *entry, struct state *state)
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
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 }
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 }
This page took 0.031785 seconds and 4 git commands to generate.