Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. |
|
grt (UTC plus 2)
2015/12/21 20:19:06
nit: this file should be called chrome_watcher_com
chrisha
2015/12/21 23:25:30
Doh! Done.
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/app/chrome_watcher_command_line_win.h" | 5 #include "chrome/app/chrome_watcher_command_line_win.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/process/process_handle.h" | 11 #include "base/process/process_handle.h" |
| 12 #include "base/win/scoped_handle.h" | 12 #include "base/win/scoped_handle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 class TestChromeWatcherCommandLineGenerator | |
| 16 : public ChromeWatcherCommandLineGenerator { | |
| 17 public: | |
| 18 explicit TestChromeWatcherCommandLineGenerator( | |
| 19 const base::FilePath& chrome_exe) | |
| 20 : ChromeWatcherCommandLineGenerator(chrome_exe) { | |
| 21 } | |
| 22 | |
| 23 void ReleaseHandlesWithoutClosing() { | |
| 24 on_initialized_event_handle_.Take(); | |
| 25 parent_process_handle_.Take(); | |
| 26 } | |
| 27 }; | |
| 28 | |
| 29 TEST(ChromeWatcherCommandLineTest, EndToEnd) { | |
| 30 base::win::ScopedHandle process(::OpenProcess( | |
| 31 PROCESS_QUERY_INFORMATION | SYNCHRONIZE, | |
| 32 FALSE, // Not inheritable. | |
| 33 ::GetCurrentProcessId())); | |
| 34 ASSERT_TRUE(process.Get()); | |
| 35 | |
| 36 base::win::ScopedHandle event(::CreateEvent(nullptr, FALSE, FALSE, nullptr)); | |
| 37 ASSERT_TRUE(event.Get()); | |
| 38 | |
| 39 // The above handles are duplicated by the generator. | |
| 40 TestChromeWatcherCommandLineGenerator generator( | |
| 41 base::FilePath(L"example.exe")); | |
| 42 generator.SetOnInitializedEventHandle(event.Get()); | |
| 43 generator.SetParentProcessHandle(process.Get()); | |
| 44 | |
| 45 // Expect there to be two inherited handles created by the generator. | |
| 46 std::vector<HANDLE> handles; | |
| 47 generator.GetInheritedHandles(&handles); | |
| 48 EXPECT_EQ(2u, handles.size()); | |
| 49 | |
| 50 base::CommandLine cmd_line = generator.GenerateCommandLine(); | |
| 51 | |
| 52 // Release the handles from the generator. Ownership will be picked up by the | |
| 53 // interpreter, and this prevents the handle tracking from complaining about | |
| 54 // there being two owners of the same handle. | |
| 55 generator.ReleaseHandlesWithoutClosing(); | |
| 56 | |
| 57 // Parse the command line. This creates scoped vectors around the same handles | |
| 58 // that are currently owned by the generator. | |
| 59 ChromeWatcherCommandLineInterpreter interpreter; | |
| 60 EXPECT_TRUE(interpreter.InterpretCommandLine(cmd_line)); | |
| 61 | |
| 62 EXPECT_EQ(::GetCurrentThreadId(), interpreter.main_thread_id()); | |
| 63 | |
| 64 // Explicitly take the handles from the interpreter. | |
| 65 base::win::ScopedHandle on_init, proc; | |
| 66 EXPECT_TRUE(interpreter.TakeOnInitializedEventHandle(&on_init)); | |
| 67 EXPECT_TRUE(interpreter.TakeParentProcessHandle(&proc)); | |
| 68 } | |
| 69 | |
| 15 TEST(ChromeWatcherCommandLineTest, BasicTest) { | 70 TEST(ChromeWatcherCommandLineTest, BasicTest) { |
| 16 // Ownership of these handles is passed to the ScopedHandles below via | 71 // Ownership of these handles is passed to the ScopedHandles below via |
| 17 // InterpretChromeWatcherCommandLine(). | 72 // InterpretChromeWatcherCommandLine(). |
| 18 base::ProcessHandle current = | 73 base::ProcessHandle current = |
| 19 ::OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, | 74 ::OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, |
| 20 TRUE, // Inheritable | 75 TRUE, // Inheritable |
| 21 ::GetCurrentProcessId()); | 76 ::GetCurrentProcessId()); |
| 22 ASSERT_NE(nullptr, current); | 77 ASSERT_NE(nullptr, current); |
| 23 | 78 |
| 24 HANDLE event = ::CreateEvent(nullptr, FALSE, FALSE, nullptr); | 79 HANDLE event = ::CreateEvent(nullptr, FALSE, FALSE, nullptr); |
| 25 ASSERT_NE(nullptr, event); | 80 ASSERT_NE(nullptr, event); |
| 26 DWORD current_thread_id = ::GetCurrentThreadId(); | 81 DWORD current_thread_id = ::GetCurrentThreadId(); |
| 27 base::CommandLine cmd_line = GenerateChromeWatcherCommandLine( | 82 base::CommandLine cmd_line = GenerateChromeWatcherCommandLine( |
| 28 base::FilePath(L"example.exe"), current, current_thread_id, event); | 83 base::FilePath(L"example.exe"), current, current_thread_id, event); |
| 29 | 84 |
| 30 base::win::ScopedHandle current_result; | 85 base::win::ScopedHandle current_result; |
| 31 DWORD current_thread_id_result = 0; | 86 DWORD current_thread_id_result = 0; |
| 32 base::win::ScopedHandle event_result; | 87 base::win::ScopedHandle event_result; |
| 33 ASSERT_TRUE(InterpretChromeWatcherCommandLine( | 88 ASSERT_TRUE(InterpretChromeWatcherCommandLine( |
| 34 cmd_line, ¤t_result, ¤t_thread_id_result, &event_result)); | 89 cmd_line, ¤t_result, ¤t_thread_id_result, &event_result)); |
| 35 ASSERT_EQ(current, current_result.Get()); | 90 ASSERT_EQ(current, current_result.Get()); |
| 36 ASSERT_EQ(current_thread_id, current_thread_id_result); | 91 ASSERT_EQ(current_thread_id, current_thread_id_result); |
| 37 ASSERT_EQ(event, event_result.Get()); | 92 ASSERT_EQ(event, event_result.Get()); |
| 38 } | 93 } |
| OLD | NEW |