YauhenMardan

Test

Nov 18th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "TemplateDLL.h"
  3. #include <iomanip>
  4.  
  5. #include <vector>
  6. #include "User.h"
  7.  
  8. std::vector<User> readFile(std::wstring fileName); //read file
  9. std::wifstream & operator >> (std::wifstream & wifs, User & user); //read each user
  10. void tryToOpenFile(std::wstring& logText, std::vector<User> data, std::wstring filePath);
  11. void tryToCreateFile(std::wstring& logText, std::vector<User> data, std::wstring filePath);
  12. void tryToReadFile(std::wstring& logText, std::vector<User> data, std::wstring filePath);
  13.  
  14. int foo(int a) {
  15. return a + a;
  16. }
  17. extern "C"
  18. {
  19. LPCWSTR GetName()
  20. {
  21. return m_sName;
  22. }
  23.  
  24. LPCWSTR GetDescription()
  25. {
  26. return m_sDescription;
  27. }
  28.  
  29. bool RunTest(std::function<void(LPCWSTR)> pFnLog)
  30. {
  31. std::wstring logText = L"log text:\n"; //test output
  32. // test code
  33. std::wstring confFileName = L"conf.ini"; //configuration file
  34. std::vector<User> data; //data: users
  35. data = readFile(confFileName); //read conf file
  36. //impersonation
  37. HANDLE hToken; //receives a handle to a token that represents the specified user
  38. for (int i = 0; i < data.size(); i++) {
  39. std::wstring name = data[i].getUserName(); //temp variables
  40. std::wstring pass = data[i].getPassword();
  41. LogonUserW( //log on user
  42. (LPCWSTR)name.c_str(),
  43. L".", //using only the local account database
  44. (LPCWSTR)pass.c_str(),
  45. LOGON32_LOGON_INTERACTIVE,
  46. LOGON32_PROVIDER_DEFAULT,
  47. &hToken
  48. );
  49. logText += name + L"\n"; //print name of user
  50. ImpersonateLoggedOnUser(hToken); //impersonate user
  51. //try to read file FileToRead.txt
  52. std::wstring folderFullPath = data[i].getFullPath();
  53. std::wstring fileToReadFullName = folderFullPath + L"FileToRead.txt";
  54. tryToOpenFile(logText, data, fileToReadFullName);
  55. //try to create a file FileToCreate.txt
  56. std::wstring fileToCreateFullName = folderFullPath + L"FileToCreate.txt";
  57. tryToCreateFile(logText, data, fileToCreateFullName);
  58. //try to read FileToCreate.txt
  59. tryToReadFile(logText, data, fileToCreateFullName);
  60. RevertToSelf(); //session revertion
  61. }
  62. pFnLog(logText.c_str());
  63. return true;
  64. }
  65. }
  66.  
  67. std::vector<User> readFile(std::wstring fileName) {
  68. std::vector<User> data; //vectror of users
  69. std::wifstream fin(fileName);
  70. while (!fin.eof())
  71. {
  72. User tmp_user;
  73. fin >> tmp_user;
  74. data.push_back(tmp_user);
  75. }
  76. return data;
  77. }
  78. std::wifstream & operator >> (std::wifstream & wifs, User & user) {
  79. wifs >> user.userName >> user.domainName >> user.password >> user.fullPath;
  80. return wifs;
  81. }
  82. void tryToOpenFile(std::wstring& logText, std::vector<User> data, std::wstring filePath) {
  83. std::wifstream fin;
  84. fin.open(filePath.c_str());
  85. if (fin.is_open()) {
  86. logText += L"File is opened " + filePath + L"\n";
  87. }
  88. else {
  89. logText += L"Cannot open the file " + filePath + L"\n";
  90. }
  91. fin.close();
  92. }
  93. void tryToCreateFile(std::wstring& logText, std::vector<User> data, std::wstring filePath) {
  94. std::wofstream fout;
  95. fout.open(filePath);
  96. if (fout.is_open()) {
  97. logText += L"File is created " + filePath + L"\n";
  98. //try to write to file FileToCreate.txt
  99. std::wstring testText = L"Try to write text";
  100. fout << testText;
  101. logText += L"Trying to write: " + testText + L"\n";
  102. }
  103. else {
  104. logText += L"Cannot create the file " + filePath + L"\n";
  105. }
  106. fout.close();
  107. }
  108. void tryToReadFile(std::wstring& logText, std::vector<User> data, std::wstring filePath) {
  109. std::wifstream fin;
  110. fin.open(filePath);
  111. if (fin.is_open()) {
  112. logText += L"Reading the file " + filePath + L"\n" + L"File contains: ";
  113. while (!fin.eof())
  114. {
  115. std::wstring temp;
  116. fin >> temp;
  117. logText+=temp +L" ";
  118. }
  119. }
  120. else {
  121. logText+=L"Cannot open " + filePath + L"\n";
  122. }
  123. fin.close();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment