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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2020-2021 The DOSBox Staging Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "shell.h"
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "dosbox_test_fixture.h"
const char *GetCmdName(int i);
namespace {
using namespace testing;
class DOS_Shell_CMDSTest : public DOSBoxTestFixture {};
class MockDOS_Shell : public DOS_Shell {
public:
/**
* NOTE: If we need to call the actual object, we use this. By
* default, the mocked functions return whatever we tell it to
* (if given a .WillOnce(Return(...)), or a default value
* (false).
*
* MockDOS_Shell()
* {
* // delegate call to the real object.
* ON_CALL(*this, execute_shell_cmd)
* .WillByDefault([this](char *name, char *arguments) {
* return real_.execute_shell_cmd(name, arguments);
* });
* }
*/
MOCK_METHOD(bool, execute_shell_cmd, (char *name, char *arguments), (override));
MOCK_METHOD(void, WriteOut, (const char *format, const char *arguments), (override));
MOCK_METHOD(int, WriteOut_NoParsing, (const char * format, bool dbcs), (override));
private:
DOS_Shell real_; // Keeps an instance of the real in the mock.
};
void assert_DoCommand(std::string input, std::string expected_name, std::string expected_args)
{
MockDOS_Shell shell;
char *input_c_str = const_cast<char *>(input.c_str());
EXPECT_CALL(shell, execute_shell_cmd(StrEq(expected_name), StrEq(expected_args))).Times(1).WillOnce(Return(true));
EXPECT_NO_THROW({ shell.DoCommand(input_c_str); });
}
// Tests chars that separate command name from arguments
TEST_F(DOS_Shell_CMDSTest, DoCommand_Separating_Chars)
{
// These should all cause the parser to stop
std::vector<char> end_chars{
32,
'/',
'\t',
'=',
};
for (auto end_chr : end_chars) {
MockDOS_Shell shell;
std::string name = "PATH";
std::string args = "";
std::string input = name;
input += end_chr;
input += "ARG";
args += end_chr;
args += "ARG";
assert_DoCommand(input, name, args);
}
}
TEST_F(DOS_Shell_CMDSTest, DoCommand_All_Cmds_Do_Valid_Execute)
{
MockDOS_Shell shell;
uint32_t cmd_index=0;
while (GetCmdName(cmd_index)) {
std::string input = GetCmdName(cmd_index);
assert_DoCommand(input, input, "");
cmd_index++;
}
}
TEST_F(DOS_Shell_CMDSTest, DoCommand_Trim_Space)
{
assert_DoCommand(" PATH ", "PATH", "");
}
TEST_F(DOS_Shell_CMDSTest, DoCommand_Splits_Cmd_and_Args)
{
// NOTE: It does not strip the arguments!
assert_DoCommand("DIR *.*", "DIR", " *.*");
}
TEST_F(DOS_Shell_CMDSTest, DoCommand_Doesnt_Split_Colon)
{
// ensure we don't split on colon ...
assert_DoCommand("C:", "C:", "");
// ... but it does split on slash
assert_DoCommand("C:\\", "C:", "\\");
}
TEST_F(DOS_Shell_CMDSTest, DoCommand_Nospace_Dot_Handling)
{
assert_DoCommand("DIR.EXE", "DIR", ".EXE");
assert_DoCommand("CD..", "CD", "..");
assert_DoCommand("CD....", "CD", "....");
}
TEST_F(DOS_Shell_CMDSTest, DoCommand_Nospace_Slash_Handling)
{
assert_DoCommand("CD\\DIRECTORY", "CD", "\\DIRECTORY");
assert_DoCommand("CD\\", "CD", "\\");
}
TEST_F(DOS_Shell_CMDSTest, CMD_ECHO_off_on)
{
MockDOS_Shell shell;
EXPECT_TRUE(shell.echo); // should be the default
EXPECT_CALL(shell, WriteOut_NoParsing(_, true)).Times(0);
EXPECT_NO_THROW({ shell.CMD_ECHO(const_cast<char *>("OFF")); });
EXPECT_FALSE(shell.echo);
EXPECT_NO_THROW({ shell.CMD_ECHO(const_cast<char *>("ON")); });
EXPECT_TRUE(shell.echo);
}
TEST_F(DOS_Shell_CMDSTest, CMD_ECHO_space_handling)
{
MockDOS_Shell shell;
EXPECT_TRUE(shell.echo);
EXPECT_CALL(shell, WriteOut_NoParsing(StrEq("OFF "), true)).Times(1);
// this DOES NOT trigger ECHO OFF (trailing space causes it to not)
EXPECT_NO_THROW({ shell.CMD_ECHO(const_cast<char *>(" OFF ")); });
EXPECT_TRUE(shell.echo);
EXPECT_CALL(shell, WriteOut_NoParsing(StrEq("FF "), true)).Times(1);
// this DOES NOT trigger ECHO OFF (initial 'O' gets stripped)
EXPECT_NO_THROW({ shell.CMD_ECHO(const_cast<char *>("OFF ")); });
EXPECT_TRUE(shell.echo);
// no trailing space, echo off should work
EXPECT_CALL(shell, WriteOut_NoParsing(_, true)).Times(0);
EXPECT_NO_THROW({ shell.CMD_ECHO(const_cast<char *>(" OFF")); });
// check that OFF worked properly, despite spaces
EXPECT_FALSE(shell.echo);
// NOTE: the expected string here is missing the leading char of the
// input to ECHO. the first char is stripped as it's assumed it will be
// a space, period or slash.
EXPECT_CALL(shell, WriteOut_NoParsing(StrEq(" HI "), true)).Times(1);
EXPECT_NO_THROW({ shell.CMD_ECHO(const_cast<char *>(". HI ")); });
}
} // namespace
|