summaryrefslogtreecommitdiff
path: root/test/cmd/test_echo.c
blob: 091e4f823c9081605ff27cf11029d0cb9fe90a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-License-Identifier: GPL-2.0+
/*
 * Tests for echo command
 *
 * Copyright 2020, Heinrich Schuchadt <xypron.glpk@gmx.de>
 */

#include <common.h>
#include <command.h>
#include <asm/global_data.h>
#include <display_options.h>
#include <test/lib.h>
#include <test/test.h>
#include <test/ut.h>

DECLARE_GLOBAL_DATA_PTR;

struct test_data {
	char *cmd;
	char *expected;
};

static struct test_data echo_data[] = {
	{"echo 1 2 3",
	 "1 2 3"},
	/* Test new line handling */
	{"echo -n 1 2 3; echo a b c",
	 "1 2 3a b c"},
	/*
	 * Test handling of environment variables.
	 *
	 * j, q, x are among the least frequent letters in English.
	 * Hence no collision for the variable name jQx is expected.
	 */
	{"setenv jQx X; echo \"a)\" ${jQx} 'b)' '${jQx}' c) ${jQx}; setenv jQx",
	 "a) X b) ${jQx} c) X"},
	/* Test shell variable assignments without substitutions */
	{"foo=bar echo baz", "baz"},
	/* Test handling of shell variables. */
	{"setenv jQx; for jQx in 1 2 3; do echo -n \"${jQx}, \"; done; echo;",
	 "1, 2, 3, "},
};

static int lib_test_hush_echo(struct unit_test_state *uts)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(echo_data); ++i) {
		ut_silence_console(uts);
		console_record_reset_enable();
		ut_assertok(run_command(echo_data[i].cmd, 0));
		ut_unsilence_console(uts);
		console_record_readline(uts->actual_str,
					sizeof(uts->actual_str));
		ut_asserteq_str(echo_data[i].expected, uts->actual_str);
		ut_assertok(ut_check_console_end(uts));
	}
	return 0;
}

LIB_TEST(lib_test_hush_echo, 0);