Skip to content

Commit 7412ab8

Browse files
committed
Implemented custom command line arguments in unit tests.
1 parent bff3894 commit 7412ab8

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed

test/ArgumentsHandler.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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 "ArgumentsHandler.h"
26+
#include "rapidassist/process.h"
27+
#include "rapidassist/environment.h"
28+
#include "rapidassist/filesystem.h"
29+
30+
#ifndef WIN32_LEAN_AND_MEAN
31+
#define WIN32_LEAN_AND_MEAN 1
32+
#endif
33+
#include <Windows.h>
34+
#undef GetEnvironmentVariable
35+
#undef DeleteFile
36+
#undef CreateDirectory
37+
#undef CopyFile
38+
#undef CreateFile
39+
#undef GetCurrentDirectory
40+
41+
42+
namespace shellanything
43+
{
44+
45+
//https://stackoverflow.com/questions/8046097/how-to-check-if-a-process-has-the-administrative-rights
46+
bool IsProcessElevated()
47+
{
48+
BOOL fRet = FALSE;
49+
HANDLE hToken = NULL;
50+
if( OpenProcessToken( GetCurrentProcess( ),TOKEN_QUERY,&hToken ) )
51+
{
52+
TOKEN_ELEVATION Elevation;
53+
DWORD cbSize = sizeof( TOKEN_ELEVATION );
54+
if( GetTokenInformation( hToken, TokenElevation, &Elevation, sizeof( Elevation ), &cbSize ) )
55+
{
56+
fRet = Elevation.TokenIsElevated;
57+
}
58+
}
59+
if( hToken )
60+
{
61+
CloseHandle( hToken );
62+
}
63+
return (fRet == TRUE);
64+
}
65+
66+
//http://blog.aaronballman.com/2011/08/how-to-check-access-rights/
67+
bool CanAccessFolder( LPCTSTR folderName, DWORD genericAccessRights )
68+
{
69+
bool bRet = false;
70+
DWORD length = 0;
71+
if (!::GetFileSecurity( folderName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, NULL, NULL, &length ) && ERROR_INSUFFICIENT_BUFFER == ::GetLastError())
72+
{
73+
PSECURITY_DESCRIPTOR security = static_cast< PSECURITY_DESCRIPTOR >( ::malloc( length ) );
74+
if (security && ::GetFileSecurity( folderName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, security, length, &length ))
75+
{
76+
HANDLE hToken = NULL;
77+
if (::OpenProcessToken( ::GetCurrentProcess(), TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_DUPLICATE | STANDARD_RIGHTS_READ, &hToken ))
78+
{
79+
HANDLE hImpersonatedToken = NULL;
80+
if (::DuplicateToken( hToken, SecurityImpersonation, &hImpersonatedToken ))
81+
{
82+
GENERIC_MAPPING mapping = { 0xFFFFFFFF };
83+
PRIVILEGE_SET privileges = { 0 };
84+
DWORD grantedAccess = 0, privilegesLength = sizeof( privileges );
85+
BOOL result = FALSE;
86+
87+
mapping.GenericRead = FILE_GENERIC_READ;
88+
mapping.GenericWrite = FILE_GENERIC_WRITE;
89+
mapping.GenericExecute = FILE_GENERIC_EXECUTE;
90+
mapping.GenericAll = FILE_ALL_ACCESS;
91+
92+
::MapGenericMask( &genericAccessRights, &mapping );
93+
if (::AccessCheck( security, hImpersonatedToken, genericAccessRights, &mapping, &privileges, &privilegesLength, &grantedAccess, &result ))
94+
{
95+
bRet = (result == TRUE);
96+
}
97+
::CloseHandle( hImpersonatedToken );
98+
}
99+
::CloseHandle( hToken );
100+
}
101+
::free( security );
102+
}
103+
}
104+
105+
return bRet;
106+
}
107+
108+
int PrintStatistics()
109+
{
110+
std::string process_path = ra::process::GetCurrentProcessPath();
111+
ra::process::processid_t process_id = ra::process::GetCurrentProcessId();
112+
std::string process_current_directory = ra::filesystem::GetCurrentDirectory();
113+
int process_architecture = (ra::environment::IsProcess64Bit() ? 64 : 86);
114+
int process_elevated = (IsProcessElevated() ? 1 : 0);
115+
const char * build_configuration = (ra::environment::IsConfigurationDebug() ? "debug" : "release");
116+
std::string env_temp = ra::environment::GetEnvironmentVariable("TEMP");
117+
bool has_windows_write_access = CanAccessFolder("C:\\Windows", GENERIC_WRITE);
118+
119+
printf("process.path=%s\n", process_path.c_str());
120+
printf("process.pid=%d\n", process_id);
121+
printf("process.current_dir=%s\n", process_current_directory.c_str());
122+
printf("process.architecture=x%d\n", process_architecture);
123+
printf("process.elevated=%d\n", process_elevated);
124+
printf("build.configuration=%s\n", build_configuration);
125+
printf("env.temp=%s\n", env_temp.c_str());
126+
printf("has_windows_write_access=%d\n", (int)has_windows_write_access);
127+
128+
return 0;
129+
}
130+
131+
} //namespace shellanything

test/ArgumentsHandler.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#ifndef SA_ARGUMENT_HANDLER_H
26+
#define SA_ARGUMENT_HANDLER_H
27+
28+
namespace shellanything
29+
{
30+
31+
/// <summary>
32+
/// Print statistics about this program settings.
33+
/// </summary>
34+
/// <returns>Returns 0 when the function succeed. Returns a non zero values otherwise.</returns>
35+
int PrintStatistics();
36+
37+
} //namespace shellanything
38+
39+
#endif //SA_ARGUMENT_HANDLER_H

test/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ add_executable(shellanything_unittest
4848
${CONFIGURATION_TEST_FILES}
4949
${SHELLANYTHING_PRIVATE_FILES}
5050
main.cpp
51+
ArgumentsHandler.cpp
52+
ArgumentsHandler.h
5153
TestActionExecute.cpp
5254
TestActionExecute.h
5355
TestActionFile.cpp

test/main.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,19 @@
3535

3636
#include "rapidassist/testing.h"
3737
#include "rapidassist/environment.h"
38+
#include "rapidassist/cli.h"
39+
40+
#include "ArgumentsHandler.h"
3841

3942
using namespace ra;
4043

4144
int main(int argc, char **argv)
4245
{
46+
// Look for custom command line arguments
47+
bool has_printstatistics = ra::cli::ParseArgument("printstatistics", std::string(), argc, argv);
48+
if (has_printstatistics)
49+
return shellanything::PrintStatistics();
50+
4351
// Prepare Google's logging library.
4452
fLB::FLAGS_logtostderr = false; //on error, print to stdout instead of stderr
4553
fLB::FLAGS_log_prefix = 1; //prefix each message in file/console with 'E0405 19:13:07.577863 6652 shellext.cpp:847]'

0 commit comments

Comments
 (0)