| 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 <common/ini-config/ini-config.hpp> |
| 9 | #include <common/path.hpp> |
| 10 | #include <common/utils.hpp> |
| 11 | |
| 12 | #include <lttng/constant.h> |
| 13 | |
| 14 | #include <string.h> |
| 15 | #include <tap/tap.h> |
| 16 | |
| 17 | namespace { |
| 18 | struct state { |
| 19 | int section_1; |
| 20 | int section_2; |
| 21 | int section_3; |
| 22 | int section_global; |
| 23 | int text_entry; |
| 24 | int int_entry; |
| 25 | }; |
| 26 | } /* namespace */ |
| 27 | |
| 28 | int lttng_opt_quiet = 1; |
| 29 | int lttng_opt_verbose = 0; |
| 30 | int lttng_opt_mi; |
| 31 | |
| 32 | static int entry_handler(const struct config_entry *entry, struct state *state) |
| 33 | { |
| 34 | int ret = 0; |
| 35 | |
| 36 | if (!entry || !state) { |
| 37 | ret = -1; |
| 38 | goto end; |
| 39 | } |
| 40 | |
| 41 | if (!strcmp(entry->section, "section1")) { |
| 42 | state->section_1 = 1; |
| 43 | if (!strcmp(entry->name, "section1_entry") && !strcmp(entry->value, "42")) { |
| 44 | state->int_entry = 1; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | if (!strcmp(entry->section, "section2")) { |
| 49 | state->section_2 = 1; |
| 50 | } |
| 51 | |
| 52 | if (!strcmp(entry->section, "section 3")) { |
| 53 | state->section_3 = 1; |
| 54 | if (!strcmp(entry->name, "name with a space") && |
| 55 | !strcmp(entry->value, "another value")) { |
| 56 | state->text_entry = 1; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!strcmp(entry->section, "")) { |
| 61 | state->section_global = 1; |
| 62 | } |
| 63 | end: |
| 64 | return ret; |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char **argv) |
| 68 | { |
| 69 | char *path = nullptr; |
| 70 | int ret; |
| 71 | struct state state = {}; |
| 72 | |
| 73 | if (argc < 2) { |
| 74 | diag("Usage: path_to_sample_INI_file"); |
| 75 | goto end; |
| 76 | } |
| 77 | |
| 78 | if (strlen(argv[1]) >= LTTNG_PATH_MAX) { |
| 79 | diag("The provided path exceeds the maximal permitted length of %i bytes", |
| 80 | LTTNG_PATH_MAX); |
| 81 | goto end; |
| 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( |
| 90 | path, nullptr, (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 && state.section_global, |
| 93 | "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( |
| 98 | path, "section1", (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 && !state.section_global, |
| 101 | "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, "", (config_entry_handler_cb) entry_handler, &state); |
| 106 | ok(ret == 0, "Successfully opened a config file, registered to the global section"); |
| 107 | ok(!state.section_1 && !state.section_2 && !state.section_3 && state.section_global, |
| 108 | "Processed an entry from the global section only"); |
| 109 | end: |
| 110 | free(path); |
| 111 | return exit_status(); |
| 112 | } |