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