|
| 1 | +/********************************************************************************** |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2018 Antoine Beauchamp |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + *********************************************************************************/ |
| 24 | + |
| 25 | +#include "TestActionExecute.h" |
| 26 | +#include "shellanything/Context.h" |
| 27 | +#include "shellanything/ActionExecute.h" |
| 28 | +#include "PropertyManager.h" |
| 29 | +#include "rapidassist/testing.h" |
| 30 | +#include "rapidassist/filesystem_utf8.h" |
| 31 | +#include "rapidassist/user.h" |
| 32 | +#include "rapidassist/timing.h" |
| 33 | +#include "rapidassist/environment.h" |
| 34 | + |
| 35 | +#include <Windows.h> |
| 36 | + |
| 37 | +namespace shellanything { namespace test |
| 38 | +{ |
| 39 | + |
| 40 | + //-------------------------------------------------------------------------------------------------- |
| 41 | + void TestActionExecute::SetUp() |
| 42 | + { |
| 43 | + } |
| 44 | + //-------------------------------------------------------------------------------------------------- |
| 45 | + void TestActionExecute::TearDown() |
| 46 | + { |
| 47 | + } |
| 48 | + //-------------------------------------------------------------------------------------------------- |
| 49 | + TEST_F(TestActionExecute, testBasic) |
| 50 | + { |
| 51 | + PropertyManager & pmgr = PropertyManager::GetInstance(); |
| 52 | + |
| 53 | + //Create a valid context |
| 54 | + Context c; |
| 55 | + Context::ElementList elements; |
| 56 | + elements.push_back("C:\\Windows\\System32\\calc.exe"); |
| 57 | + c.SetElements(elements); |
| 58 | + |
| 59 | + c.RegisterProperties(); |
| 60 | + |
| 61 | + //execute the action |
| 62 | + ActionExecute ae; |
| 63 | + ae.SetPath("C:\\Windows\\System32\\calc.exe"); |
| 64 | + ae.SetBaseDir(""); |
| 65 | + ae.SetArguments(""); |
| 66 | + |
| 67 | + bool executed = ae.Execute(c); |
| 68 | + ASSERT_TRUE( executed ); |
| 69 | + |
| 70 | + //cleanup |
| 71 | + ra::timing::Millisleep(500); |
| 72 | + system("cmd.exe /c taskkill /IM calc.exe >NUL 2>NUL"); |
| 73 | + } |
| 74 | + //-------------------------------------------------------------------------------------------------- |
| 75 | + TEST_F(TestActionExecute, testBaseDir) |
| 76 | + { |
| 77 | + PropertyManager & pmgr = PropertyManager::GetInstance(); |
| 78 | + |
| 79 | + //Create a valid context |
| 80 | + Context c; |
| 81 | + Context::ElementList elements; |
| 82 | + elements.push_back("C:\\Windows\\System32\\calc.exe"); |
| 83 | + c.SetElements(elements); |
| 84 | + |
| 85 | + c.RegisterProperties(); |
| 86 | + |
| 87 | + std::string home_dir = ra::user::GetHomeDirectory(); |
| 88 | + |
| 89 | + //execute the action |
| 90 | + ActionExecute ae; |
| 91 | + ae.SetPath("calc.exe"); |
| 92 | + ae.SetBaseDir("C:\\Windows\\System32"); |
| 93 | + ae.SetArguments(""); |
| 94 | + |
| 95 | + bool executed = ae.Execute(c); |
| 96 | + ASSERT_TRUE( executed ); |
| 97 | + |
| 98 | + //cleanup |
| 99 | + ra::timing::Millisleep(500); |
| 100 | + system("cmd.exe /c taskkill /IM calc.exe >NUL 2>NUL"); |
| 101 | + } |
| 102 | + //-------------------------------------------------------------------------------------------------- |
| 103 | + TEST_F(TestActionExecute, testArguments) |
| 104 | + { |
| 105 | + PropertyManager & pmgr = PropertyManager::GetInstance(); |
| 106 | + |
| 107 | + //Create a valid context |
| 108 | + Context c; |
| 109 | + Context::ElementList elements; |
| 110 | + elements.push_back("C:\\Windows\\System32\\calc.exe"); |
| 111 | + c.SetElements(elements); |
| 112 | + |
| 113 | + c.RegisterProperties(); |
| 114 | + |
| 115 | + std::string home_dir = ra::user::GetHomeDirectory(); |
| 116 | + std::string temp_dir = ra::filesystem::GetTemporaryDirectory(); |
| 117 | + std::string destination_path = temp_dir + "\\my_calc.exe"; |
| 118 | + std::string arguments = "/c copy C:\\Windows\\System32\\calc.exe " + destination_path + ">NUL 2>NUL"; |
| 119 | + |
| 120 | + //execute the action |
| 121 | + ActionExecute ae; |
| 122 | + ae.SetPath("cmd.exe"); |
| 123 | + ae.SetBaseDir(temp_dir); |
| 124 | + ae.SetArguments(arguments); |
| 125 | + |
| 126 | + bool executed = ae.Execute(c); |
| 127 | + ASSERT_TRUE( executed ); |
| 128 | + |
| 129 | + //Wait for the copy to complete, with a timeout |
| 130 | + static const double timeout_time = 5000; //ms |
| 131 | + bool file_copied = false; |
| 132 | + double timer_start = ra::timing::GetMillisecondsTimer(); |
| 133 | + double time_elapsed = ra::timing::GetMillisecondsTimer() - timer_start; |
| 134 | + while(!file_copied && time_elapsed <= timeout_time) |
| 135 | + { |
| 136 | + file_copied = ra::filesystem::FileExists(destination_path.c_str()); |
| 137 | + ra::timing::Millisleep(500); //allow process to complete |
| 138 | + time_elapsed = ra::timing::GetMillisecondsTimer() - timer_start; //evaluate elapsed time again |
| 139 | + } |
| 140 | + |
| 141 | + //Validate arguments |
| 142 | + ASSERT_TRUE(file_copied); |
| 143 | + |
| 144 | + //cleanup |
| 145 | + ra::filesystem::DeleteFileUtf8(destination_path.c_str()); |
| 146 | + } |
| 147 | + //-------------------------------------------------------------------------------------------------- |
| 148 | + TEST_F(TestActionExecute, testVerb) |
| 149 | + { |
| 150 | + //Skip this test if run on AppVeyor as it requires administrative (elevated) privileges. |
| 151 | + if (ra::testing::IsAppVeyor() || |
| 152 | + ra::testing::IsJenkins() || |
| 153 | + ra::testing::IsTravis()) |
| 154 | + { |
| 155 | + printf("Skipping tests as it requires administrative (elevated) privileges.\n"); |
| 156 | + return; |
| 157 | + } |
| 158 | + |
| 159 | + PropertyManager & pmgr = PropertyManager::GetInstance(); |
| 160 | + |
| 161 | + //Create a valid context |
| 162 | + Context c; |
| 163 | + Context::ElementList elements; |
| 164 | + elements.push_back("C:\\Windows\\System32\\calc.exe"); |
| 165 | + c.SetElements(elements); |
| 166 | + |
| 167 | + c.RegisterProperties(); |
| 168 | + |
| 169 | + std::string temp_dir = ra::filesystem::GetTemporaryDirectory(); |
| 170 | + std::string batch_file_filename = ra::testing::GetTestQualifiedName() + ".bat"; |
| 171 | + std::string result_file_filename = ra::testing::GetTestQualifiedName() + ".txt"; |
| 172 | + std::string batch_file_path = temp_dir + "\\" + batch_file_filename; |
| 173 | + std::string result_file_path = temp_dir + "\\" + result_file_filename; |
| 174 | + std::string arguments = "/c " + batch_file_filename + " >" + result_file_filename; |
| 175 | + |
| 176 | + //This is a batch file that prints ADMIN if it is run in elevated mode or prints FAIL otherwise. |
| 177 | + //Inspired from https://stackoverflow.com/questions/4051883/batch-script-how-to-check-for-admin-rights |
| 178 | + const std::string content = |
| 179 | + "@echo off\n" |
| 180 | + "net session >nul 2>&1\n" |
| 181 | + "if %errorLevel% == 0 (\n" |
| 182 | + " echo ADMIN\n" |
| 183 | + ") else (\n" |
| 184 | + " echo FAIL\n" |
| 185 | + ")\n"; |
| 186 | + ASSERT_TRUE( ra::filesystem::WriteTextFile(batch_file_path, content) ); |
| 187 | + |
| 188 | + //execute the action |
| 189 | + ActionExecute ae; |
| 190 | + ae.SetPath("cmd.exe"); |
| 191 | + ae.SetBaseDir(temp_dir); |
| 192 | + ae.SetArguments(arguments); |
| 193 | + ae.SetVerb("runas"); |
| 194 | + |
| 195 | + bool executed = ae.Execute(c); |
| 196 | + ASSERT_TRUE( executed ); |
| 197 | + |
| 198 | + //Wait for the operation to complete, with a timeout |
| 199 | + static const double timeout_time = 5000; //ms |
| 200 | + bool result_file_found = false; |
| 201 | + double timer_start = ra::timing::GetMillisecondsTimer(); |
| 202 | + double time_elapsed = ra::timing::GetMillisecondsTimer() - timer_start; |
| 203 | + while(!result_file_found && time_elapsed <= timeout_time) |
| 204 | + { |
| 205 | + result_file_found = ra::filesystem::FileExists(result_file_path.c_str()); |
| 206 | + ra::timing::Millisleep(500); //allow process to complete |
| 207 | + time_elapsed = ra::timing::GetMillisecondsTimer() - timer_start; //evaluate elapsed time again |
| 208 | + } |
| 209 | + |
| 210 | + //Validate arguments |
| 211 | + ASSERT_TRUE(result_file_found); |
| 212 | + |
| 213 | + //Read the result file |
| 214 | + std::string result; |
| 215 | + ASSERT_TRUE( ra::filesystem::ReadTextFile(result_file_path, result) ); |
| 216 | + ra::strings::Replace(result, ra::environment::GetLineSeparator(), ""); |
| 217 | + ra::strings::Replace(result, "\n", ""); |
| 218 | + static const std::string EXPECTED_RESULT = "ADMIN"; |
| 219 | + ASSERT_EQ(EXPECTED_RESULT, result); |
| 220 | + |
| 221 | + //cleanup |
| 222 | + ra::filesystem::DeleteFileUtf8(batch_file_path.c_str()); |
| 223 | + ra::filesystem::DeleteFileUtf8(result_file_path.c_str()); |
| 224 | + } |
| 225 | + //-------------------------------------------------------------------------------------------------- |
| 226 | + |
| 227 | +} //namespace test |
| 228 | +} //namespace shellanything |
0 commit comments