1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2007 Andrew Ziem <ahz001@gmail.com>
4 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
5 * Copyright (C) 2015 Alexander Larsson <alexl@redhat.com>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <glib.h>
27 #include <check.h>
28
29 #include "gdm-common.h"
30 #include "s-common.h"
31
32 static void
setup(void)33 setup (void)
34 {
35 }
36
37 static void
teardown(void)38 teardown (void)
39 {
40 }
41
42 static char *
expand_fn(const char * var,gpointer data)43 expand_fn (const char *var, gpointer data)
44 {
45 if (strcmp (var, "FOO") == 0)
46 return g_strdup ("BAR");
47 if (strcmp (var, "FOO9") == 0)
48 return g_strdup ("XXX");
49 if (strcmp (var, "_FOO") == 0)
50 return g_strdup ("YYY");
51 if (strcmp (var, "FOO_FOO") == 0)
52 return g_strdup ("ZZZ");
53 return NULL;
54 }
55
expands_to(const char * to_expand,const char * expanded)56 static gboolean expands_to (const char *to_expand, const char *expanded)
57 {
58 return strcmp (gdm_shell_expand (to_expand, expand_fn, NULL), expanded) == 0;
59 }
60
START_TEST(test_gdm_shell_expand)61 START_TEST (test_gdm_shell_expand)
62 {
63 fail_unless (expands_to ("foo", "foo"));
64 fail_unless (expands_to ("foo ", "foo "));
65 fail_unless (expands_to ("foo#bar", "foo#bar"));
66 fail_unless (expands_to ("foo #bar", "foo "));
67 fail_unless (expands_to ("#bar", ""));
68 fail_unless (expands_to ("foo #bar gazonk", "foo "));
69 fail_unless (expands_to ("foo #bar gazonk", "foo "));
70 fail_unless (expands_to ("foo #bar gazonk", "foo "));
71 fail_unless (expands_to ("$FOO", "BAR"));
72 fail_unless (expands_to ("$9FOO", "$9FOO"));
73 fail_unless (expands_to ("$FOO9", "XXX"));
74 fail_unless (expands_to ("${FOO}9", "BAR9"));
75 fail_unless (expands_to ("$_FOO", "YYY"));
76 fail_unless (expands_to ("$FOO_FOO", "ZZZ"));
77 fail_unless (expands_to ("${FOO}", "BAR"));
78 fail_unless (expands_to ("$FOO$FOO", "BARBAR"));
79 fail_unless (expands_to ("${FOO}${FOO}", "BARBAR"));
80 fail_unless (expands_to ("$FOO${FOO}", "BARBAR"));
81 fail_unless (expands_to ("$foo", ""));
82 fail_unless (expands_to ("$FOOBAR", ""));
83 fail_unless (expands_to ("$FOO/BAR", "BAR/BAR"));
84 fail_unless (expands_to ("${FOO}BAR", "BARBAR"));
85 fail_unless (expands_to ("$/BAR", "$/BAR"));
86 fail_unless (expands_to ("${FOO BAR}BAR", "${FOO BAR}BAR"));
87 fail_unless (expands_to ("${}BAR", "${}BAR"));
88 fail_unless (expands_to ("${$FOO}BAR", "${BAR}BAR"));
89 fail_unless (expands_to ("\\$foo", "$foo"));
90 fail_unless (expands_to ("a\\\\b", "a\\b"));
91 fail_unless (expands_to ("a\\b", "a\\b"));
92 fail_unless (expands_to ("a\\#b", "a#b"));
93 }
94 END_TEST
95
96 Suite *
suite_common(void)97 suite_common (void)
98 {
99 Suite *s;
100 TCase *tc_core;
101
102 s = suite_create ("gdm-common");
103 tc_core = tcase_create ("core");
104
105 tcase_add_checked_fixture (tc_core, setup, teardown);
106 tcase_add_test (tc_core, test_gdm_shell_expand);
107 suite_add_tcase (s, tc_core);
108
109 return s;
110 }
111