Francis Laniel | 097533a | 2023-12-22 22:02:25 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * (C) Copyright 2021 |
| 4 | * Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com |
| 5 | */ |
| 6 | |
Francis Laniel | 097533a | 2023-12-22 22:02:25 +0100 | [diff] [blame] | 7 | #include <command.h> |
| 8 | #include <env_attr.h> |
| 9 | #include <test/hush.h> |
| 10 | #include <test/ut.h> |
Francis Laniel | c42dda3 | 2023-12-22 22:02:36 +0100 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Francis Laniel | 097533a | 2023-12-22 22:02:25 +0100 | [diff] [blame] | 12 | |
| 13 | static int hush_test_semicolon(struct unit_test_state *uts) |
| 14 | { |
| 15 | /* A; B = B truth table. */ |
| 16 | ut_asserteq(1, run_command("false; false", 0)); |
| 17 | ut_assertok(run_command("false; true", 0)); |
| 18 | ut_assertok(run_command("true; true", 0)); |
| 19 | ut_asserteq(1, run_command("true; false", 0)); |
| 20 | |
| 21 | return 0; |
| 22 | } |
| 23 | HUSH_TEST(hush_test_semicolon, 0); |
| 24 | |
| 25 | static int hush_test_and(struct unit_test_state *uts) |
| 26 | { |
| 27 | /* A && B truth table. */ |
| 28 | ut_asserteq(1, run_command("false && false", 0)); |
| 29 | ut_asserteq(1, run_command("false && true", 0)); |
| 30 | ut_assertok(run_command("true && true", 0)); |
| 31 | ut_asserteq(1, run_command("true && false", 0)); |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | HUSH_TEST(hush_test_and, 0); |
| 36 | |
| 37 | static int hush_test_or(struct unit_test_state *uts) |
| 38 | { |
| 39 | /* A || B truth table. */ |
| 40 | ut_asserteq(1, run_command("false || false", 0)); |
| 41 | ut_assertok(run_command("false || true", 0)); |
| 42 | ut_assertok(run_command("true || true", 0)); |
| 43 | ut_assertok(run_command("true || false", 0)); |
| 44 | |
| 45 | return 0; |
| 46 | } |
| 47 | HUSH_TEST(hush_test_or, 0); |
| 48 | |
Francis Laniel | c42dda3 | 2023-12-22 22:02:36 +0100 | [diff] [blame] | 49 | DECLARE_GLOBAL_DATA_PTR; |
| 50 | |
Francis Laniel | 097533a | 2023-12-22 22:02:25 +0100 | [diff] [blame] | 51 | static int hush_test_and_or(struct unit_test_state *uts) |
| 52 | { |
| 53 | /* A && B || C truth table. */ |
| 54 | ut_asserteq(1, run_command("false && false || false", 0)); |
Francis Laniel | c42dda3 | 2023-12-22 22:02:36 +0100 | [diff] [blame] | 55 | |
| 56 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { |
| 57 | ut_asserteq(1, run_command("false && false || true", 0)); |
| 58 | } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { |
| 59 | /* |
| 60 | * This difference seems to come from a bug solved in Busybox |
| 61 | * hush. |
| 62 | * |
| 63 | * Indeed, the following expression can be seen like this: |
| 64 | * (false && false) || true |
| 65 | * So, (false && false) returns 1, the second false is not |
| 66 | * executed, and true is executed because of ||. |
| 67 | */ |
| 68 | ut_assertok(run_command("false && false || true", 0)); |
| 69 | } |
| 70 | |
| 71 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { |
| 72 | ut_asserteq(1, run_command("false && true || true", 0)); |
| 73 | } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { |
| 74 | /* |
| 75 | * This difference seems to come from a bug solved in Busybox |
| 76 | * hush. |
| 77 | * |
| 78 | * Indeed, the following expression can be seen like this: |
| 79 | * (false && true) || true |
| 80 | * So, (false && true) returns 1, the true is not executed, and |
| 81 | * true is executed because of ||. |
| 82 | */ |
| 83 | ut_assertok(run_command("false && true || true", 0)); |
| 84 | } |
| 85 | |
Francis Laniel | 097533a | 2023-12-22 22:02:25 +0100 | [diff] [blame] | 86 | ut_asserteq(1, run_command("false && true || false", 0)); |
| 87 | ut_assertok(run_command("true && true || false", 0)); |
| 88 | ut_asserteq(1, run_command("true && false || false", 0)); |
| 89 | ut_assertok(run_command("true && false || true", 0)); |
| 90 | ut_assertok(run_command("true && true || true", 0)); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | HUSH_TEST(hush_test_and_or, 0); |
| 95 | |
| 96 | static int hush_test_or_and(struct unit_test_state *uts) |
| 97 | { |
| 98 | /* A || B && C truth table. */ |
| 99 | ut_asserteq(1, run_command("false || false && false", 0)); |
| 100 | ut_asserteq(1, run_command("false || false && true", 0)); |
| 101 | ut_assertok(run_command("false || true && true", 0)); |
| 102 | ut_asserteq(1, run_command("false || true && false", 0)); |
Francis Laniel | c42dda3 | 2023-12-22 22:02:36 +0100 | [diff] [blame] | 103 | |
| 104 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { |
| 105 | ut_assertok(run_command("true || true && false", 0)); |
| 106 | } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { |
| 107 | /* |
| 108 | * This difference seems to come from a bug solved in Busybox |
| 109 | * hush. |
| 110 | * |
| 111 | * Indeed, the following expression can be seen like this: |
| 112 | * (true || true) && false |
| 113 | * So, (true || true) returns 0, the second true is not |
| 114 | * executed, and then false is executed because of &&. |
| 115 | */ |
| 116 | ut_asserteq(1, run_command("true || true && false", 0)); |
| 117 | } |
| 118 | |
| 119 | if (gd->flags & GD_FLG_HUSH_OLD_PARSER) { |
| 120 | ut_assertok(run_command("true || false && false", 0)); |
| 121 | } else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) { |
| 122 | /* |
| 123 | * This difference seems to come from a bug solved in Busybox |
| 124 | * hush. |
| 125 | * |
| 126 | * Indeed, the following expression can be seen like this: |
| 127 | * (true || false) && false |
| 128 | * So, (true || false) returns 0, the false is not executed, and |
| 129 | * then false is executed because of &&. |
| 130 | */ |
| 131 | ut_asserteq(1, run_command("true || false && false", 0)); |
| 132 | } |
| 133 | |
Francis Laniel | 097533a | 2023-12-22 22:02:25 +0100 | [diff] [blame] | 134 | ut_assertok(run_command("true || false && true", 0)); |
| 135 | ut_assertok(run_command("true || true && true", 0)); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | HUSH_TEST(hush_test_or_and, 0); |