blob: defb2c3fd0c991aa6103c96d57e36fae6f2f04cd [file] [log] [blame]
Francis Laniel261d29e2023-12-22 22:02:24 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * (C) Copyright 2021
4 * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
5 */
6
7#include <common.h>
8#include <command.h>
9#include <env_attr.h>
10#include <test/hush.h>
11#include <test/ut.h>
12
13static int hush_test_simple_dollar(struct unit_test_state *uts)
14{
15 console_record_reset_enable();
16 ut_assertok(run_command("echo $dollar_foo", 0));
17 ut_assert_nextline_empty();
18 ut_assert_console_end();
19
20 ut_assertok(run_command("echo ${dollar_foo}", 0));
21 ut_assert_nextline_empty();
22 ut_assert_console_end();
23
24 ut_assertok(run_command("dollar_foo=bar", 0));
25
26 ut_assertok(run_command("echo $dollar_foo", 0));
27 ut_assert_nextline("bar");
28 ut_assert_console_end();
29
30 ut_assertok(run_command("echo ${dollar_foo}", 0));
31 ut_assert_nextline("bar");
32 ut_assert_console_end();
33
34 ut_assertok(run_command("dollar_foo=\\$bar", 0));
35
36 ut_assertok(run_command("echo $dollar_foo", 0));
37 ut_assert_nextline("$bar");
38 ut_assert_console_end();
39
40 ut_assertok(run_command("dollar_foo='$bar'", 0));
41
42 ut_assertok(run_command("echo $dollar_foo", 0));
43 ut_assert_nextline("$bar");
44 ut_assert_console_end();
45
46 ut_asserteq(1, run_command("dollar_foo=bar quux", 0));
47 /* Next line contains error message */
48 ut_assert_skipline();
49 ut_assert_console_end();
50
51 ut_asserteq(1, run_command("dollar_foo='bar quux", 0));
52 /* Next line contains error message */
53 ut_assert_skipline();
54 ut_assert_console_end();
55
56 ut_asserteq(1, run_command("dollar_foo=bar quux\"", 0));
57 /* Two next lines contain error message */
58 ut_assert_skipline();
59 ut_assert_skipline();
60 ut_assert_console_end();
61
62 ut_assertok(run_command("dollar_foo='bar \"quux'", 0));
63
64 ut_assertok(run_command("echo $dollar_foo", 0));
65 /*
66 * This one is buggy.
67 * ut_assert_nextline("bar \"quux");
68 * ut_assert_console_end();
69 *
70 * So, let's reset output:
71 */
72 console_record_reset_enable();
73
74 ut_asserteq(1, run_command("dollar_foo=\"bar 'quux\"", 0));
75 /* Next line contains error message */
76 ut_assert_skipline();
77 ut_assert_console_end();
78
79 ut_assertok(run_command("dollar_foo='bar quux'", 0));
80 ut_assertok(run_command("echo $dollar_foo", 0));
81 ut_assert_nextline("bar quux");
82 ut_assert_console_end();
83
84 puts("Beware: this test set local variable dollar_foo and it cannot be unset!");
85
86 return 0;
87}
88HUSH_TEST(hush_test_simple_dollar, 0);
89
90static int hush_test_env_dollar(struct unit_test_state *uts)
91{
92 env_set("env_foo", "bar");
93 console_record_reset_enable();
94
95 ut_assertok(run_command("echo $env_foo", 0));
96 ut_assert_nextline("bar");
97 ut_assert_console_end();
98
99 ut_assertok(run_command("echo ${env_foo}", 0));
100 ut_assert_nextline("bar");
101 ut_assert_console_end();
102
103 /* Environment variables have priority over local variable */
104 ut_assertok(run_command("env_foo=quux", 0));
105 ut_assertok(run_command("echo ${env_foo}", 0));
106 ut_assert_nextline("bar");
107 ut_assert_console_end();
108
109 /* Clean up setting the variable */
110 env_set("env_foo", NULL);
111
112 puts("Beware: this test set local variable env_foo and it cannot be unset!");
113
114 return 0;
115}
116HUSH_TEST(hush_test_env_dollar, 0);
117
118static int hush_test_command_dollar(struct unit_test_state *uts)
119{
120 console_record_reset_enable();
121
122 ut_assertok(run_command("dollar_bar=\"echo bar\"", 0));
123
124 ut_assertok(run_command("$dollar_bar", 0));
125 ut_assert_nextline("bar");
126 ut_assert_console_end();
127
128 ut_assertok(run_command("${dollar_bar}", 0));
129 ut_assert_nextline("bar");
130 ut_assert_console_end();
131
132 ut_assertok(run_command("dollar_bar=\"echo\nbar\"", 0));
133
134 ut_assertok(run_command("$dollar_bar", 0));
135 ut_assert_nextline("bar");
136 ut_assert_console_end();
137
138 ut_assertok(run_command("dollar_bar='echo bar\n'", 0));
139
140 ut_assertok(run_command("$dollar_bar", 0));
141 ut_assert_nextline("bar");
142 ut_assert_console_end();
143
144 ut_assertok(run_command("dollar_bar='echo bar\\n'", 0));
145
146 ut_assertok(run_command("$dollar_bar", 0));
147 ut_assert_nextline("barn");
148 ut_assert_console_end();
149
150 ut_assertok(run_command("dollar_bar='echo $bar'", 0));
151
152 ut_assertok(run_command("$dollar_bar", 0));
153 ut_assert_nextline("$bar");
154 ut_assert_console_end();
155
156 ut_assertok(run_command("dollar_quux=quux", 0));
157 ut_assertok(run_command("dollar_bar=\"echo $dollar_quux\"", 0));
158
159 ut_assertok(run_command("$dollar_bar", 0));
160 ut_assert_nextline("quux");
161 ut_assert_console_end();
162
163 puts("Beware: this test sets local variable dollar_bar and dollar_quux and they cannot be unset!");
164
165 return 0;
166}
167HUSH_TEST(hush_test_command_dollar, 0);