int cnprocCheckResponse( CnpkPipeFds tFds, int nCmdID, int* pDataSize, char** pRespData ) { char response[RESPONSE_SIZE+1]; short result; short data_size; int fdRes = tFds.res_fd; short cmd; if( ReadPipe( fdRes, response, RESPONSE_SIZE ) == 0 ){ BufTool *buf_tool = buftool_new(RESPONSE_SIZE, BUFTOOL_LITTLE_ENDIAN); memcpy(buftool_data(buf_tool), response, RESPONSE_SIZE); buftool_set_pos(buf_tool, 0); buftool_read_short(buf_tool, &cmd); buftool_read_short(buf_tool, &result); buftool_read_short(buf_tool, &data_size); if( cmd != nCmdID ){ buftool_destroy(buf_tool); return -1; } if( data_size > 0 ){ *pRespData = (char*)malloc(data_size); if((char*)*pRespData != NULL ) ReadPipe( fdRes, (char*)*pRespData, data_size ); } if( pDataSize != NULL ) *pDataSize = data_size; buftool_destroy(buf_tool); return result; } else return -1; }
inline bool Read(HANDLE Pipe, string& Data) { bool Result=false; size_t DataSize = 0; if(ReadPipe(Pipe, &DataSize, sizeof(DataSize))) { if(DataSize) { wchar_t_ptr Buffer(DataSize / sizeof(wchar_t) ); if(ReadPipe(Pipe, Buffer.get(), DataSize)) { Data.assign(Buffer.get(), Buffer.size() - 1); Result=true; } } else { Result=true; } } return Result; }
int main(int argc, char **argv) { ULONG ulType = 0; ULONG ulAttr = 0; APIRET rc; int i; int iMode = MODE_NONE; rc = DosQueryHType(0, &ulType, &ulAttr); //STDIN if(!rc) { ulType &= 0xFF; if(ulType == 0 || ulType == 2) //file or pipe iMode |= MODE_IN; } rc = DosQueryHType(1, &ulType, &ulAttr); //STDOUT if(!rc) { ulType &= 0xFF; if(ulType == 0 || ulType == 2) //file or pipe iMode |= MODE_OUT; } for(i = 1; i < argc; i++) { if(argv[i][0] == '-' || argv[i][0] == '/') { switch(argv[i][1]) { case 'I': case 'i': if(argv[i][2] == '-') iMode &= ~MODE_IN; else iMode |= MODE_IN; break; case 'o': case 'O': if(argv[i][2] == '-') iMode &= ~MODE_OUT; else iMode |= MODE_OUT; break; case 'h': case 'H': case '?': usage(); return 0; } } } if(!iMode) { usage(); return 0; } rc = init_pm(); if(rc) return -1; if(iMode & MODE_IN) { char *str = ReadPipe(0); if(str) set_clip(str); delete str; } if(iMode & MODE_OUT) { char *str = get_clip(); if(str) { ULONG ulWrote = 0; rc = DosWrite(1, str, strlen(str), &ulWrote); } } done_pm(); return rc; }
static int Launch() { Log(L"Launching browser..."); DWORD processID; // The interface that allows us to activate the browser CComPtr<IApplicationActivationManager> activateMgr; if (FAILED(CoCreateInstance(CLSID_ApplicationActivationManager, nullptr, CLSCTX_LOCAL_SERVER, IID_IApplicationActivationManager, (void**)&activateMgr))) { Fail(false, L"CoCreateInstance CLSID_ApplicationActivationManager failed."); return FAILURE; } HRESULT hr; WCHAR appModelID[256]; // Activation is based on the browser's registered app model id if (!GetDefaultBrowserAppModelID(appModelID, (sizeof(appModelID)/sizeof(WCHAR)))) { Fail(false, L"GetDefaultBrowserAppModelID failed."); return FAILURE; } Log(L"App model id='%s'", appModelID); // Hand off focus rights if the terminal has focus to the out-of-process // activation server (explorer.exe). Without this the metro interface // won't launch. hr = CoAllowSetForegroundWindow(activateMgr, nullptr); if (FAILED(hr)) { // Log but don't fail. This has happened on vms with certain terminals run by // QA during mozmill testing. Log(L"Windows focus rights hand off failed (HRESULT=0x%X). Ignoring.", hr); } Log(L"Harness process id: %d", GetCurrentProcessId()); // If provided, validate the firefox path passed in. int binLen = wcslen(kFirefoxExe); if (sFirefoxPath.GetLength() && sFirefoxPath.Right(binLen) != kFirefoxExe) { Log(L"firefoxpath is missing a valid bin name! Assuming '%s'.", kFirefoxExe); if (sFirefoxPath.Right(1) != L"\\") { sFirefoxPath += L"\\"; } sFirefoxPath += kFirefoxExe; } // Because we can't pass command line args, we store params in a // tests.ini file in dist/bin which the browser picks up on launch. CStringA testFilePath; if (sFirefoxPath.GetLength()) { // Use the firefoxpath passed to us by the test harness int index = sFirefoxPath.ReverseFind('\\'); if (index == -1) { Fail(false, L"Bad firefoxpath path"); return FAILURE; } testFilePath = sFirefoxPath.Mid(0, index); testFilePath += "\\"; testFilePath += kMetroTestFile; } else { // Use the module path char path[MAX_PATH]; if (!GetModuleFileNameA(nullptr, path, MAX_PATH)) { Fail(false, L"GetModuleFileNameA errorno=%d", GetLastError()); return FAILURE; } char* slash = strrchr(path, '\\'); if (!slash) return FAILURE; *slash = '\0'; // no trailing slash testFilePath = path; testFilePath += "\\"; sFirefoxPath = testFilePath; sFirefoxPath += kFirefoxExe; testFilePath += kMetroTestFile; } // Make sure the firefox bin exists if (GetFileAttributesW(sFirefoxPath) == INVALID_FILE_ATTRIBUTES) { Fail(false, L"Invalid bin path: '%s'", sFirefoxPath); return FAILURE; } Log(L"Using bin path: '%s'", sFirefoxPath); Log(L"Writing out tests.ini to: '%s'", CStringW(testFilePath)); HANDLE hTestFile = CreateFileA(testFilePath, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (hTestFile == INVALID_HANDLE_VALUE) { Fail(false, L"CreateFileA errorno=%d", GetLastError()); return FAILURE; } DeleteTestFileHelper dtf(testFilePath); // nsAppRunner expects the first param to be the bin path, just like a // normal startup. So prepend our bin path to our param string we write. CStringA asciiParams = sFirefoxPath; asciiParams += " "; asciiParams += sAppParams; asciiParams.Trim(); Log(L"Browser command line args: '%s'", CString(asciiParams)); if (!WriteFile(hTestFile, asciiParams, asciiParams.GetLength(), nullptr, 0)) { CloseHandle(hTestFile); Fail(false, L"WriteFile errorno=%d", GetLastError()); return FAILURE; } FlushFileBuffers(hTestFile); CloseHandle(hTestFile); // Create a named stdout pipe for the browser if (!SetupTestOutputPipe()) { Fail(false, L"SetupTestOutputPipe failed (errno=%d)", GetLastError()); return FAILURE; } // Launch firefox hr = activateMgr->ActivateApplication(appModelID, L"", AO_NOERRORUI, &processID); if (FAILED(hr)) { Fail(true, L"ActivateApplication result %X", hr); return RETRY; } Log(L"Activation succeeded."); // automation.py picks up on this, don't mess with it. Log(L"METRO_BROWSER_PROCESS=%d", processID); HANDLE child = OpenProcess(SYNCHRONIZE, FALSE, processID); if (!child) { Fail(false, L"Couldn't find child process. (%d)", GetLastError()); return FAILURE; } Log(L"Waiting on child process..."); MSG msg; DWORD waitResult = WAIT_TIMEOUT; HANDLE handles[2] = { child, gTestOutputPipe }; while ((waitResult = MsgWaitForMultipleObjects(2, handles, FALSE, INFINITE, QS_ALLINPUT)) != WAIT_OBJECT_0) { if (waitResult == WAIT_FAILED) { Log(L"Wait failed (errno=%d)", GetLastError()); break; } else if (waitResult == WAIT_OBJECT_0 + 1) { ReadPipe(); } else if (waitResult == WAIT_OBJECT_0 + 2 && PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } ReadPipe(); CloseHandle(gTestOutputPipe); CloseHandle(child); Log(L"Exiting."); return SUCCESS; }
static bool Launch() { Log(L"Launching browser..."); DWORD processID; // The interface that allows us to activate the browser CComPtr<IApplicationActivationManager> activateMgr; if (FAILED(CoCreateInstance(CLSID_ApplicationActivationManager, NULL, CLSCTX_LOCAL_SERVER, IID_IApplicationActivationManager, (void**)&activateMgr))) { Fail(L"CoCreateInstance CLSID_ApplicationActivationManager failed."); return false; } HRESULT hr; WCHAR appModelID[256]; // Activation is based on the browser's registered app model id if (!GetDefaultBrowserAppModelID(appModelID, (sizeof(appModelID)/sizeof(WCHAR)))) { Fail(L"GetDefaultBrowserAppModelID failed."); return false; } Log(L"App model id='%s'", appModelID); // Hand off focus rights if the terminal has focus to the out-of-process // activation server (explorer.exe). Without this the metro interface // won't launch. if (GetForegroundWindow() == GetConsoleWindow()) { hr = CoAllowSetForegroundWindow(activateMgr, NULL); if (FAILED(hr)) { Fail(L"CoAllowSetForegroundWindow result %X", hr); return false; } } Log(L"Harness process id: %d", GetCurrentProcessId()); // Because we can't pass command line args, we store params in a // tests.ini file in dist/bin which the browser picks up on launch. CStringA testFilePath; if (sFirefoxPath.GetLength()) { // Use the firefoxpath passed to us by the test harness int index = sFirefoxPath.ReverseFind('\\'); if (index == -1) { Fail(L"Bad firefoxpath path"); return false; } testFilePath = sFirefoxPath.Mid(0, index); testFilePath += "\\"; testFilePath += kMetroTestFile; } else { // Use the module path char path[MAX_PATH]; if (!GetModuleFileNameA(NULL, path, MAX_PATH)) { Fail(L"GetModuleFileNameA errorno=%d", GetLastError()); return false; } char* slash = strrchr(path, '\\'); if (!slash) return false; *slash = '\0'; // no trailing slash testFilePath = path; testFilePath += "\\"; testFilePath += kMetroTestFile; } Log(L"Writing out tests.ini to: '%s'", CStringW(testFilePath)); HANDLE hTestFile = CreateFileA(testFilePath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hTestFile == INVALID_HANDLE_VALUE) { Fail(L"CreateFileA errorno=%d", GetLastError()); return false; } DeleteTestFileHelper dtf(testFilePath); CStringA asciiParams = sAppParams; if (!WriteFile(hTestFile, asciiParams, asciiParams.GetLength(), NULL, 0)) { CloseHandle(hTestFile); Fail(L"WriteFile errorno=%d", GetLastError()); return false; } FlushFileBuffers(hTestFile); CloseHandle(hTestFile); // Create a named stdout pipe for the browser if (!SetupTestOutputPipe()) { Fail(L"SetupTestOutputPipe failed (errno=%d)", GetLastError()); return false; } // Launch firefox hr = activateMgr->ActivateApplication(appModelID, L"", AO_NOERRORUI, &processID); if (FAILED(hr)) { Fail(L"ActivateApplication result %X", hr); return false; } Log(L"Activation succeeded. processid=%d", processID); HANDLE child = OpenProcess(SYNCHRONIZE, FALSE, processID); if (!child) { Fail(L"Couldn't find child process. (%d)", GetLastError()); return false; } Log(L"Waiting on child process..."); MSG msg; DWORD waitResult = WAIT_TIMEOUT; HANDLE handles[2] = { child, gTestOutputPipe }; while ((waitResult = MsgWaitForMultipleObjects(2, handles, FALSE, INFINITE, QS_ALLINPUT)) != WAIT_OBJECT_0) { if (waitResult == WAIT_FAILED) { Log(L"Wait failed (errno=%d)", GetLastError()); break; } else if (waitResult == WAIT_OBJECT_0 + 1) { ReadPipe(); } else if (waitResult == WAIT_OBJECT_0 + 2 && PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } ReadPipe(); CloseHandle(gTestOutputPipe); CloseHandle(child); Log(L"Exiting."); return true; }