|
| 1 | +/* |
| 2 | + * This file is part of libmodulemd |
| 3 | + * Copyright (C) 2023 Red Hat, Inc. |
| 4 | + * |
| 5 | + * Fedora-License-Identifier: MIT |
| 6 | + * SPDX-2.0-License-Identifier: MIT |
| 7 | + * SPDX-3.0-License-Identifier: MIT |
| 8 | + * |
| 9 | + * This program is free software. |
| 10 | + * For more information on the license, see COPYING. |
| 11 | + * For more information on free software, see <https://www.gnu.org/philosophy/free-sw.en.html>. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <glib.h> |
| 15 | +#include <locale.h> |
| 16 | +#include "modulemd-profile.h" |
| 17 | +#include "private/modulemd-yaml.h" |
| 18 | +#include "private/modulemd-profile-private.h" |
| 19 | + |
| 20 | +/*#include <glib/gstdio.h> |
| 21 | +
|
| 22 | +
|
| 23 | +#include "private/glib-extensions.h" |
| 24 | +#include "private/test-utils.h"*/ |
| 25 | + |
| 26 | +struct item |
| 27 | +{ |
| 28 | + const char *input; |
| 29 | + int quoted; /* true for quoting expected, otherwise unquoted expected */ |
| 30 | +}; |
| 31 | + |
| 32 | +/* |
| 33 | + * Test that strings only consisting of a number are quoted to prevent |
| 34 | + * consumers from interpretting them as a number and thus mangling the |
| 35 | + * string value by normalizing the number. |
| 36 | + * Ability to quote numerical strings at each part of a YAML document is |
| 37 | + * tested in tests for the particular document type, i.e. not in this file. |
| 38 | + * This code uses an RPM package list of a stream profile for the purpose |
| 39 | + * of testing. It's the most brief usage of quoting. |
| 40 | + */ |
| 41 | +static void |
| 42 | +test_quoting (gconstpointer data) |
| 43 | +{ |
| 44 | + const struct item *test_case = (const struct item *)data; |
| 45 | + g_autoptr (ModulemdProfile) p = NULL; |
| 46 | + g_autoptr (GError) error = NULL; |
| 47 | + MMD_INIT_YAML_EMITTER (emitter); |
| 48 | + MMD_INIT_YAML_STRING (&emitter, yaml_string); |
| 49 | + g_autoptr (GString) expected = g_string_new (NULL); |
| 50 | + |
| 51 | + if (test_case->quoted) |
| 52 | + g_string_printf (expected, |
| 53 | + "---\n" |
| 54 | + "\"0\":\n" |
| 55 | + " rpms:\n" |
| 56 | + " - \"%s\"\n" |
| 57 | + "...\n", |
| 58 | + test_case->input); |
| 59 | + else |
| 60 | + g_string_printf (expected, |
| 61 | + "---\n" |
| 62 | + "\"0\":\n" |
| 63 | + " rpms:\n" |
| 64 | + " - %s\n" |
| 65 | + "...\n", |
| 66 | + test_case->input); |
| 67 | + |
| 68 | + p = modulemd_profile_new ("0"); |
| 69 | + |
| 70 | + modulemd_profile_add_rpm (p, test_case->input); |
| 71 | + |
| 72 | + g_assert_true (mmd_emitter_start_stream (&emitter, &error)); |
| 73 | + g_assert_true (mmd_emitter_start_document (&emitter, &error)); |
| 74 | + g_assert_true ( |
| 75 | + mmd_emitter_start_mapping (&emitter, YAML_BLOCK_MAPPING_STYLE, &error)); |
| 76 | + g_assert_true (modulemd_profile_emit_yaml (p, &emitter, &error)); |
| 77 | + g_assert_true (mmd_emitter_end_mapping (&emitter, &error)); |
| 78 | + g_assert_true (mmd_emitter_end_document (&emitter, &error)); |
| 79 | + g_assert_true (mmd_emitter_end_stream (&emitter, &error)); |
| 80 | + if (g_strcmp0 (yaml_string->str, expected->str)) |
| 81 | + { |
| 82 | + g_test_message ( |
| 83 | + "Expected=\"%s\"\nGot=\"%s\"", expected->str, yaml_string->str); |
| 84 | + g_test_fail (); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +int |
| 90 | +main (int argc, char *argv[]) |
| 91 | +{ |
| 92 | + /* clang-format off */ |
| 93 | + struct item test_cases[] = { |
| 94 | + {"0", 1}, /* YAML/JSON floats */ |
| 95 | + {"0.", 1}, |
| 96 | + {"0.0", 1}, |
| 97 | + {".0", 1}, |
| 98 | + {"-1", 1}, |
| 99 | + {"-1.", 1}, |
| 100 | + {"-1.0", 1}, |
| 101 | + {"-.0", 1}, |
| 102 | + {"+1", 1}, /* Handle "+" for sure */ |
| 103 | + {"+1.", 1}, |
| 104 | + {"+1.0", 1}, |
| 105 | + {"+.0", 1}, |
| 106 | + {"1.0e1", 1}, |
| 107 | + {"-1.0e1", 1}, |
| 108 | + {"+1.0e1", 1}, |
| 109 | + {"1.0e-1", 1}, |
| 110 | + {"-1.0e-1", 1}, |
| 111 | + {"+1.0e-1", 1}, |
| 112 | + {"1.0e+1", 1}, |
| 113 | + {"-1.0e+1", 1}, |
| 114 | + {"+1.0e+1", 1}, |
| 115 | + {".inf", 1}, |
| 116 | + {"-.inf", 1}, |
| 117 | + {"+.inf", 1}, |
| 118 | + {".nan", 1}, |
| 119 | + |
| 120 | + {"0x", 0}, /* Incomplete hexadecimal */ |
| 121 | + {"0x0", 1}, /* YAML hexadecicmal notation */ |
| 122 | + {"0xa", 1}, |
| 123 | + {"0xA", 1}, |
| 124 | + {"0xg", 0}, /* Invalid hexadecimal */ |
| 125 | + |
| 126 | + {"0o", 0}, /* Incomplete octal */ |
| 127 | + {"0o0", 1}, /* YAML octal notation */ |
| 128 | + {"0o8", 0}, /* Invalid octal */ |
| 129 | + |
| 130 | + {"0a", 0}, /* This does not need quoting. Common in refs. */ |
| 131 | + {NULL, 0} |
| 132 | + }; |
| 133 | + /* clang-format on */ |
| 134 | + struct item *test_case; |
| 135 | + g_autoptr (GString) testpath = g_string_new (NULL); |
| 136 | + |
| 137 | + setlocale (LC_ALL, ""); |
| 138 | + g_test_init (&argc, &argv, NULL); |
| 139 | + g_test_set_nonfatal_assertions (); |
| 140 | + |
| 141 | + for (test_case = test_cases; test_case->input != NULL; test_case++) |
| 142 | + { |
| 143 | + g_string_printf ( |
| 144 | + testpath, "/modulemd/yaml/quoting/%s", test_case->input); |
| 145 | + g_test_add_data_func ( |
| 146 | + testpath->str, (gconstpointer)test_case, test_quoting); |
| 147 | + } |
| 148 | + |
| 149 | + return g_test_run (); |
| 150 | +} |
0 commit comments