Merge pull request #3370 from nfedera/fix-winpr-test-thread-createprocess

winpr/thread: fix TestThreadCreateProcess
This commit is contained in:
Bernhard Miklautz 2016-05-25 16:39:22 +02:00
commit 8aec19d116
2 changed files with 59 additions and 28 deletions

View File

@ -84,6 +84,20 @@ typedef STARTUPINFOA STARTUPINFO;
typedef LPSTARTUPINFOA LPSTARTUPINFO; typedef LPSTARTUPINFOA LPSTARTUPINFO;
#endif #endif
#define STARTF_USESHOWWINDOW 0x00000001
#define STARTF_USESIZE 0x00000002
#define STARTF_USEPOSITION 0x00000004
#define STARTF_USECOUNTCHARS 0x00000008
#define STARTF_USEFILLATTRIBUTE 0x00000010
#define STARTF_RUNFULLSCREEN 0x00000020
#define STARTF_FORCEONFEEDBACK 0x00000040
#define STARTF_FORCEOFFFEEDBACK 0x00000080
#define STARTF_USESTDHANDLES 0x00000100
#define STARTF_USEHOTKEY 0x00000200
#define STARTF_TITLEISLINKNAME 0x00000800
#define STARTF_TITLEISAPPID 0x00001000
#define STARTF_PREVENTPINNING 0x00002000
/* Process */ /* Process */
#define LOGON_WITH_PROFILE 0x00000001 #define LOGON_WITH_PROFILE 0x00000001

View File

@ -7,7 +7,8 @@
#include <winpr/environment.h> #include <winpr/environment.h>
#include <winpr/pipe.h> #include <winpr/pipe.h>
#define TESTENV "TEST_PROCESS=oyeah" #define TESTENV_A "HELLO=WORLD"
#define TESTENV_T _T(TESTENV_A)
int TestThreadCreateProcess(int argc, char* argv[]) int TestThreadCreateProcess(int argc, char* argv[])
{ {
@ -26,17 +27,17 @@ int TestThreadCreateProcess(int argc, char* argv[])
LPTCH lpszEnvironmentBlock; LPTCH lpszEnvironmentBlock;
HANDLE pipe_read = NULL; HANDLE pipe_read = NULL;
HANDLE pipe_write = NULL; HANDLE pipe_write = NULL;
char buf[255]; char buf[1024];
DWORD read_bytes; DWORD read_bytes;
int ret = 0; int ret = 0;
SECURITY_ATTRIBUTES saAttr;
lpszEnvironmentBlock = GetEnvironmentStrings(); lpszEnvironmentBlock = GetEnvironmentStrings();
lpApplicationName = NULL; lpApplicationName = NULL;
//lpCommandLine = _T("ls -l /");
#ifdef _WIN32 #ifdef _WIN32
lpCommandLine = _T("env"); lpCommandLine = _T("cmd /C set");
#else #else
lpCommandLine = _T("printenv"); lpCommandLine = _T("printenv");
#endif #endif
@ -45,7 +46,9 @@ int TestThreadCreateProcess(int argc, char* argv[])
lpThreadAttributes = NULL; lpThreadAttributes = NULL;
bInheritHandles = FALSE; bInheritHandles = FALSE;
dwCreationFlags = 0; dwCreationFlags = 0;
lpEnvironment = NULL; #ifdef _UNICODE
dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;
#endif
lpEnvironment = lpszEnvironmentBlock; lpEnvironment = lpszEnvironmentBlock;
lpCurrentDirectory = NULL; lpCurrentDirectory = NULL;
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO)); ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
@ -69,8 +72,11 @@ int TestThreadCreateProcess(int argc, char* argv[])
return 1; return 1;
} }
if (WaitForSingleObject(ProcessInformation.hProcess, 5000) != WAIT_OBJECT_0)
WaitForSingleObject(ProcessInformation.hProcess, INFINITE); {
printf("Failed to wait for first process. error=%d\n", GetLastError());
return 1;
}
exitCode = 0; exitCode = 0;
status = GetExitCodeProcess(ProcessInformation.hProcess, &exitCode); status = GetExitCodeProcess(ProcessInformation.hProcess, &exitCode);
@ -82,22 +88,36 @@ int TestThreadCreateProcess(int argc, char* argv[])
CloseHandle(ProcessInformation.hThread); CloseHandle(ProcessInformation.hThread);
FreeEnvironmentStrings(lpszEnvironmentBlock); FreeEnvironmentStrings(lpszEnvironmentBlock);
/* Test stdin,stdout,stderr redirection */
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
StartupInfo.cb = sizeof(STARTUPINFO);
ZeroMemory(&ProcessInformation, sizeof(PROCESS_INFORMATION));
if (!CreatePipe(&pipe_read, &pipe_write, NULL, 0)) /* Test stdin,stdout,stderr redirection */
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
saAttr.lpSecurityDescriptor = NULL;
if (!CreatePipe(&pipe_read, &pipe_write, &saAttr, 0))
{ {
printf("Pipe creation failed. error=%d\n", GetLastError()); printf("Pipe creation failed. error=%d\n", GetLastError());
return 1; return 1;
} }
bInheritHandles = TRUE;
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.hStdOutput = pipe_write; StartupInfo.hStdOutput = pipe_write;
StartupInfo.hStdError = pipe_write; StartupInfo.hStdError = pipe_write;
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
ZeroMemory(&ProcessInformation, sizeof(PROCESS_INFORMATION));
if (!(lpEnvironment = calloc(1, sizeof(TESTENV_T) + sizeof(TCHAR))))
{
printf("Failed to allocate environment buffer. error=%d\n", GetLastError());
return 1;
}
memcpy(lpEnvironment, (void*)TESTENV_T, sizeof(TESTENV_T));
lpEnvironment = calloc(1, sizeof(TESTENV) + 1);
strncpy(lpEnvironment, TESTENV, strlen(TESTENV));
lpCommandLine = _T("printenv");
status = CreateProcess(lpApplicationName, status = CreateProcess(lpApplicationName,
lpCommandLine, lpCommandLine,
@ -120,22 +140,19 @@ int TestThreadCreateProcess(int argc, char* argv[])
return 1; return 1;
} }
if (WaitForSingleObject(pipe_read, 200) != WAIT_OBJECT_0) if (WaitForSingleObject(ProcessInformation.hProcess, 5000) != WAIT_OBJECT_0)
{ {
printf("pipe wait failed.\n"); printf("Failed to wait for second process. error=%d\n", GetLastError());
ret = 1; return 1;
}
else
{
ReadFile(pipe_read, buf, 255, &read_bytes, NULL);
if (read_bytes < strlen(TESTENV))
{
printf("pipe read problem?!\n");
ret = 1;
}
} }
WaitForSingleObject(ProcessInformation.hProcess, INFINITE); ZeroMemory(buf, sizeof(buf));
ReadFile(pipe_read, buf, sizeof(buf)-1, &read_bytes, NULL);
if (!strstr((const char*)buf, TESTENV_A))
{
printf("No or unexpected data read from pipe\n");
ret = 1;
}
CloseHandle(pipe_read); CloseHandle(pipe_read);
CloseHandle(pipe_write); CloseHandle(pipe_write);