Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include "TemplateDLL.h"
- #include <iomanip>
- #include <vector>
- #include "User.h"
- std::vector<User> readFile(std::wstring fileName); //read file
- std::wifstream & operator >> (std::wifstream & wifs, User & user); //read each user
- void tryToOpenFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath);
- void tryToCreateFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath);
- void tryToReadFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath);
- int foo(int a) {
- return a + a;
- }
- extern "C"
- {
- LPCWSTR GetName()
- {
- return m_sName;
- }
- LPCWSTR GetDescription()
- {
- return m_sDescription;
- }
- bool RunTest(std::function<void(LPCWSTR)> pFnLog)
- {
- std::wstring logText = L"log text:";
- pFnLog(logText.c_str());
- // test code
- std::wstring confFileName = L"conf.ini"; //configuration file
- std::vector<User> data; //data: users
- data = readFile(confFileName); //read conf file
- //output
- std::wstring outputfileName = L"TestResult.txt"; //prepare output file
- std::wofstream testOutputStream;
- testOutputStream.open(outputfileName);
- //impersonation
- HANDLE hToken; //receives a handle to a token that represents the specified user
- for (int i = 0; i < data.size(); i++) {
- std::wstring name = data[i].getUserName(); //temp variables
- std::wstring pass = data[i].getPassword();
- LogonUserW( //log on user
- (LPCWSTR)name.c_str(),
- L".", //using only the local account database
- (LPCWSTR)pass.c_str(),
- LOGON32_LOGON_INTERACTIVE,
- LOGON32_PROVIDER_DEFAULT,
- &hToken
- );
- testOutputStream << name << ":" << std::endl;
- ImpersonateLoggedOnUser(hToken); //impersonate user
- //try to read file FileToRead.txt
- std::wstring folderFullPath = data[i].getFullPath();
- std::wstring fileToReadFullName = folderFullPath + L"FileToRead.txt";
- tryToOpenFile(testOutputStream, data, fileToReadFullName);
- //try to create a file FileToCreate.txt
- std::wstring fileToCreateFullName = folderFullPath + L"FileToCreate.txt";
- tryToCreateFile(testOutputStream, data, fileToCreateFullName);
- //try to read FileToCreate.txt
- tryToReadFile(testOutputStream, data, fileToCreateFullName);
- RevertToSelf(); //session revertion
- }
- testOutputStream.close(); //close output
- return true;
- }
- }
- std::vector<User> readFile(std::wstring fileName) {
- std::vector<User> data; //vectror of users
- std::wifstream fin(fileName);
- while (!fin.eof())
- {
- User tmp_user;
- fin >> tmp_user;
- data.push_back(tmp_user);
- }
- return data;
- }
- std::wifstream & operator >> (std::wifstream & wifs, User & user) {
- wifs >> user.userName >> user.domainName >> user.password >> user.fullPath;
- return wifs;
- }
- void tryToOpenFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath) {
- std::wifstream fin;
- fin.open(filePath.c_str());
- if (fin.is_open()) {
- stream << "File is opened " << filePath << std::endl;
- }
- else {
- stream << "Cannot open the file " << filePath << std::endl;
- }
- fin.close();
- }
- void tryToCreateFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath) {
- std::wofstream fout;
- fout.open(filePath);
- if (fout.is_open()) {
- stream << "File is created " << filePath << std::endl;
- //try to write to file FileToCreate.txt
- fout << "Try to write text";
- }
- else {
- stream << "Cannot create the file " << filePath << std::endl;
- }
- fout.close();
- }
- void tryToReadFile(std::wofstream& stream, std::vector<User> data, std::wstring filePath) {
- std::wifstream fin;
- fin.open(filePath);
- if (fin.is_open()) {
- stream << "Reading the file " << filePath << std::endl << "File contains:" << std::endl;
- while (!fin.eof())
- {
- std::wstring temp;
- fin >> temp;
- stream << temp << " ";
- }
- }
- else {
- stream << "Cannot open " << filePath << std::endl;
- }
- fin.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement