Advertisement
YauhenMardan

Untitled

Nov 16th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 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::wofstream& stream, std::vector<User> data, std::wstring filePath);
  11. void tryToCreateFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath);
  12. void tryToReadFile(std::wofstream& stream, 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:";
  32. pFnLog(logText.c_str());
  33. // test code
  34. std::wstring confFileName = L"conf.ini"; //configuration file
  35. std::vector<User> data; //data: users
  36. data = readFile(confFileName); //read conf file
  37. //output
  38. std::wstring outputfileName = L"TestResult.txt"; //prepare output file
  39. std::wofstream testOutputStream;
  40. testOutputStream.open(outputfileName);
  41. //impersonation
  42. HANDLE hToken; //receives a handle to a token that represents the specified user
  43. for (int i = 0; i < data.size(); i++) {
  44. std::wstring name = data[i].getUserName(); //temp variables
  45. std::wstring pass = data[i].getPassword();
  46. LogonUserW( //log on user
  47. (LPCWSTR)name.c_str(),
  48. L".", //using only the local account database
  49. (LPCWSTR)pass.c_str(),
  50. LOGON32_LOGON_INTERACTIVE,
  51. LOGON32_PROVIDER_DEFAULT,
  52. &hToken
  53. );
  54. testOutputStream << name << ":" << std::endl;
  55. ImpersonateLoggedOnUser(hToken); //impersonate user
  56. //try to read file FileToRead.txt
  57. std::wstring folderFullPath = data[i].getFullPath();
  58. std::wstring fileToReadFullName = folderFullPath + L"FileToRead.txt";
  59. tryToOpenFile(testOutputStream, data, fileToReadFullName);
  60. //try to create a file FileToCreate.txt
  61. std::wstring fileToCreateFullName = folderFullPath + L"FileToCreate.txt";
  62. tryToCreateFile(testOutputStream, data, fileToCreateFullName);
  63. //try to read FileToCreate.txt
  64. tryToReadFile(testOutputStream, data, fileToCreateFullName);
  65. RevertToSelf(); //session revertion
  66. }
  67. testOutputStream.close(); //close output
  68. return true;
  69. }
  70. }
  71.  
  72. std::vector<User> readFile(std::wstring fileName) {
  73. std::vector<User> data; //vectror of users
  74. std::wifstream fin(fileName);
  75. while (!fin.eof())
  76. {
  77. User tmp_user;
  78. fin >> tmp_user;
  79. data.push_back(tmp_user);
  80. }
  81. return data;
  82. }
  83. std::wifstream & operator >> (std::wifstream & wifs, User & user) {
  84. wifs >> user.userName >> user.domainName >> user.password >> user.fullPath;
  85. return wifs;
  86. }
  87. void tryToOpenFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath) {
  88. std::wifstream fin;
  89. fin.open(filePath.c_str());
  90. if (fin.is_open()) {
  91. stream << "File is opened " << filePath << std::endl;
  92. }
  93. else {
  94. stream << "Cannot open the file " << filePath << std::endl;
  95. }
  96. fin.close();
  97. }
  98. void tryToCreateFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath) {
  99. std::wofstream fout;
  100. fout.open(filePath);
  101. if (fout.is_open()) {
  102. stream << "File is created " << filePath << std::endl;
  103. //try to write to file FileToCreate.txt
  104. fout << "Try to write text";
  105. }
  106. else {
  107. stream << "Cannot create the file " << filePath << std::endl;
  108. }
  109. fout.close();
  110. }
  111. void tryToReadFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath) {
  112. std::wifstream fin;
  113. fin.open(filePath);
  114. if (fin.is_open()) {
  115. stream << "Reading the file " << filePath << std::endl << "File contains:" << std::endl;
  116. while (!fin.eof())
  117. {
  118. std::wstring temp;
  119. fin >> temp;
  120. stream << temp << " ";
  121. }
  122. }
  123. else {
  124. stream << "Cannot open " << filePath << std::endl;
  125. }
  126. fin.close();
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement