TestTimeStampWin.cpp (2607B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/CmdLineAndEnvUtils.h" 8 #include "mozilla/TimeStamp.h" 9 10 #include "nsWindowsHelpers.h" 11 12 #include <stdio.h> 13 #include <windows.h> 14 15 static wchar_t kChildArg[] = L"--child"; 16 17 static nsReturnRef<HANDLE> CreateProcessWrapper(const wchar_t* aPath) { 18 nsAutoHandle empty; 19 20 const wchar_t* childArgv[] = {aPath, kChildArg}; 21 mozilla::UniquePtr<wchar_t[]> cmdLine( 22 mozilla::MakeCommandLine(std::size(childArgv), childArgv)); 23 24 STARTUPINFOW si = {sizeof(si)}; 25 PROCESS_INFORMATION pi; 26 BOOL ok = ::CreateProcessW(aPath, cmdLine.get(), nullptr, nullptr, FALSE, 0, 27 nullptr, nullptr, &si, &pi); 28 if (!ok) { 29 printf( 30 "TEST-FAILED | TimeStampWin | " 31 "CreateProcess failed - %08lx\n", 32 GetLastError()); 33 return empty.out(); 34 } 35 36 nsAutoHandle proc(pi.hProcess); 37 nsAutoHandle thd(pi.hThread); 38 39 return proc.out(); 40 } 41 42 int ChildMain() { 43 // Make sure a process creation timestamp is always not bigger than 44 // the current timestamp. 45 auto t0 = mozilla::TimeStamp::ProcessCreation(); 46 auto t1 = mozilla::TimeStamp::Now(); 47 if (t0 > t1) { 48 printf( 49 "TEST-FAILED | TimeStampWin | " 50 "Process creation timestamp is bigger than the current " 51 "timestamp!\n"); 52 return 1; 53 } 54 return 0; 55 } 56 57 int wmain(int argc, wchar_t* argv[]) { 58 if (argc == 2 && wcscmp(argv[1], kChildArg) == 0) { 59 return ChildMain(); 60 } 61 62 if (argc != 1) { 63 printf( 64 "TEST-FAILED | TimeStampWin | " 65 "Unexpected argc\n"); 66 return 1; 67 } 68 69 // Start a child process successively, checking any of them terminates with 70 // a non-zero value which means an error. 71 for (int i = 0; i < 20; ++i) { 72 nsAutoHandle childProc(CreateProcessWrapper(argv[0])); 73 74 if (::WaitForSingleObject(childProc, 60000) != WAIT_OBJECT_0) { 75 printf( 76 "TEST-FAILED | TimeStampWin | " 77 "Unexpected result from WaitForSingleObject\n"); 78 return 1; 79 } 80 81 DWORD childExitCode; 82 if (!::GetExitCodeProcess(childProc.get(), &childExitCode)) { 83 printf( 84 "TEST-FAILED | TimeStampWin | " 85 "GetExitCodeProcess failed - %08lx\n", 86 GetLastError()); 87 return 1; 88 } 89 90 if (childExitCode != 0) { 91 return childExitCode; 92 } 93 } 94 95 return 0; 96 }