| 1 |
/* |
| 2 |
* Copyright (C) 2014 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include "cockpittemplate.h" |
| 9 |
#include "cockpitjson.h" |
| 10 |
|
| 11 |
#include "testlib/cockpittest.h" |
| 12 |
|
| 13 |
#include <string.h> |
| 14 |
|
| 15 |
typedef struct { |
| 16 |
GHashTable *variables; |
| 17 |
} TestCase; |
| 18 |
|
| 19 |
static void |
| 20 |
setup (TestCase *tc, |
| 21 |
gconstpointer data) |
| 22 |
{ |
| 23 |
tc->variables = g_hash_table_new (g_str_hash, g_str_equal); |
| 24 |
g_hash_table_insert (tc->variables, "Scruffy", "janitor"); |
| 25 |
g_hash_table_insert (tc->variables, "oh", "marmalade"); |
| 26 |
g_hash_table_insert (tc->variables, "oh_dash", "dash-marmalade"); |
| 27 |
g_hash_table_insert (tc->variables, "empty", ""); |
| 28 |
g_hash_table_insert (tc->variables, "💩", "error"); |
| 29 |
} |
| 30 |
|
| 31 |
static void |
| 32 |
teardown (TestCase *tc, |
| 33 |
gconstpointer data) |
| 34 |
{ |
| 35 |
g_hash_table_destroy (tc->variables); |
| 36 |
} |
| 37 |
|
| 38 |
static GBytes * |
| 39 |
lookup_table (const char *name, |
| 40 |
gpointer user_data) |
| 41 |
{ |
| 42 |
GHashTable *variables = user_data; |
| 43 |
const gchar *value; |
| 44 |
|
| 45 |
value = g_hash_table_lookup (variables, name); |
| 46 |
if (!value) |
| 47 |
return NULL; |
| 48 |
return g_bytes_new (value, strlen (value)); |
| 49 |
} |
| 50 |
|
| 51 |
typedef struct { |
| 52 |
const char *name; |
| 53 |
const char *input; |
| 54 |
const char *output[8]; |
| 55 |
} Fixture; |
| 56 |
|
| 57 |
static const Fixture expand_fixtures[] = { |
| 58 |
{ "empty-string", "", { NULL } }, |
| 59 |
{ "no-vars", "Test no vars", { "Test no vars", NULL } }, |
| 60 |
{ "only-var", "${oh}", { "marmalade", NULL } }, |
| 61 |
{ "only-vars", "${oh}${oh}", { "marmalade", "marmalade", NULL } }, |
| 62 |
{ "simple", "Test ${oh} suffix", { "Test ", "marmalade", " suffix", NULL } }, |
| 63 |
{ "extra-dollar", "Te$st ${oh} suffix", { "Te$st ", "marmalade", " suffix", NULL } }, |
| 64 |
{ "extra-brace", "Te{st ${oh} suffix", { "Te{st ", "marmalade", " suffix", NULL } }, |
| 65 |
{ "no-ending", "Test ${oh} su${ffix", { "Test ", "marmalade", " su${ffix", NULL } }, |
| 66 |
{ "extra-dollar-after", "Test ${oh} su${ff$ix", { "Test ", "marmalade", " su${ff$ix", NULL } }, |
| 67 |
{ "unknown", "Test ${unknown} suffix", { "Test ", "${unknown}", " suffix", NULL } }, |
| 68 |
{ "escaped", "Test \\${oh} ${oh} suffix", { "Test ", "${oh}", " ", "marmalade", " suffix", NULL } }, |
| 69 |
{ "dash", "Test ${oh_dash} suffix", { "Test ", "dash-marmalade", " suffix", NULL } }, |
| 70 |
{ "lots", "Oh ${oh} says Scruffy ${empty} the ${Scruffy}", |
| 71 |
{ "Oh ", "marmalade", " says Scruffy ", " the ", "janitor", NULL } |
| 72 |
}, |
| 73 |
{ "unsupported-var", "${ig.nore} ${💩} ${Kōhī}", { "${ig.nore} ${💩} ${Kōhī}" } }, |
| 74 |
{ "var-in-string", "te${oh}st", { "te", "marmalade", "st" } }, |
| 75 |
{ "var-in-var-string", "te${test${oh}st", { "te${test", "marmalade", "st" } }, |
| 76 |
}; |
| 77 |
|
| 78 |
static void |
| 79 |
test_expand (TestCase *tc, |
| 80 |
gconstpointer data) |
| 81 |
{ |
| 82 |
const Fixture *fixture = data; |
| 83 |
GBytes *input; |
| 84 |
GList *output; |
| 85 |
GList *l; |
| 86 |
int i; |
| 87 |
|
| 88 |
input = g_bytes_new_static (fixture->input, strlen (fixture->input)); |
| 89 |
|
| 90 |
output = cockpit_template_expand (input, lookup_table, tc->variables); |
| 91 |
g_bytes_unref (input); |
| 92 |
|
| 93 |
for (i = 0, l = output; l && fixture->output[i] != NULL; i++, l = g_list_next (l)) |
| 94 |
cockpit_assert_bytes_eq (l->data, fixture->output[i], -1); |
| 95 |
g_assert_cmpint (g_list_length (output), ==, i); |
| 96 |
|
| 97 |
g_list_free_full (output, (GDestroyNotify)g_bytes_unref); |
| 98 |
} |
| 99 |
|
| 100 |
int |
| 101 |
main (int argc, |
| 102 |
char *argv[]) |
| 103 |
{ |
| 104 |
gchar *name; |
| 105 |
int i; |
| 106 |
|
| 107 |
cockpit_test_init (&argc, &argv); |
| 108 |
|
| 109 |
for (i = 0; i < G_N_ELEMENTS (expand_fixtures); i++) |
| 110 |
{ |
| 111 |
name = g_strdup_printf ("/template/expand/%s", expand_fixtures[i].name); |
| 112 |
g_test_add (name, TestCase, expand_fixtures + i, setup, test_expand, teardown); |
| 113 |
g_free (name); |
| 114 |
} |
| 115 |
|
| 116 |
return g_test_run (); |
| 117 |
} |