OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/app/chrome_watcher_command_line_win.h" | |
6 | |
7 #include <windows.h> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/process/process.h" | |
12 #include "base/process/process_handle.h" | |
13 #include "base/win/scoped_handle.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace { | |
17 | |
18 const wchar_t kExampleExe[] = FILE_PATH_LITERAL("example.exe"); | |
19 | |
20 base::FilePath ExampleExe() { | |
21 base::FilePath example_exe(kExampleExe); | |
22 return example_exe; | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 class TestChromeWatcherCommandLineGenerator | |
28 : public ChromeWatcherCommandLineGenerator { | |
29 public: | |
30 TestChromeWatcherCommandLineGenerator() | |
31 : ChromeWatcherCommandLineGenerator(base::FilePath(kExampleExe)) { | |
32 } | |
33 | |
34 void ReleaseHandlesWithoutClosing() { | |
35 on_initialized_event_handle_.Take(); | |
36 parent_process_handle_.Take(); | |
37 } | |
38 }; | |
39 | |
40 TEST(ChromeWatcherCommandLineTest, UseOfBadHandlesFails) { | |
41 // Handles are always machine word aligned so there is no way these are valid. | |
42 HANDLE bad_handle_1 = reinterpret_cast<HANDLE>(0x01FC00B1); | |
43 HANDLE bad_handle_2 = reinterpret_cast<HANDLE>(0x01FC00B3); | |
44 | |
45 // The above handles are duplicated by the generator. | |
46 TestChromeWatcherCommandLineGenerator generator; | |
Sigurður Ásgeirsson
2016/01/08 16:55:19
do you need to use the TEst class here?
chrisha
2016/01/08 22:29:00
Not specifically in this test, but it just makes t
| |
47 EXPECT_FALSE(generator.SetOnInitializedEventHandle(bad_handle_1)); | |
48 EXPECT_FALSE(generator.SetParentProcessHandle(bad_handle_2)); | |
49 | |
50 // Expect there to be no inherited handles created by the generator. | |
51 std::vector<HANDLE> handles; | |
52 generator.GetInheritedHandles(&handles); | |
53 EXPECT_TRUE(handles.empty()); | |
54 } | |
55 | |
56 TEST(ChromeWatcherCommandLineTest, BadCommandLineFailsInterpretation) { | |
57 // Create an invalid command-line that is missing several fields. | |
58 base::CommandLine cmd_line(ExampleExe()); | |
59 | |
60 // Parse the command line. | |
61 auto interpreted = ChromeWatcherCommandLine::InterpretCommandLine(cmd_line); | |
62 EXPECT_FALSE(interpreted); | |
63 } | |
64 | |
65 TEST(ChromeWatcherCommandLineDeathTest, HandlesLeftUntakenCausesDeath) { | |
66 base::Process process(base::Process::OpenWithAccess( | |
67 base::GetCurrentProcId(), PROCESS_QUERY_INFORMATION | SYNCHRONIZE)); | |
68 ASSERT_TRUE(process.Handle()); | |
69 | |
70 base::win::ScopedHandle event(::CreateEvent(nullptr, FALSE, FALSE, nullptr)); | |
71 ASSERT_TRUE(event.IsValid()); | |
72 | |
73 // The above handles are duplicated by the generator. | |
74 TestChromeWatcherCommandLineGenerator generator; | |
75 generator.SetOnInitializedEventHandle(event.Get()); | |
76 generator.SetParentProcessHandle(process.Handle()); | |
77 | |
78 // Expect there to be two inherited handles created by the generator. | |
79 std::vector<HANDLE> handles; | |
80 generator.GetInheritedHandles(&handles); | |
81 EXPECT_EQ(2U, handles.size()); | |
82 | |
83 base::CommandLine cmd_line = generator.GenerateCommandLine(); | |
84 | |
85 // Release the handles from the generator. Ownership will be picked up by the | |
Sigurður Ásgeirsson
2016/01/08 16:55:19
Nit: explain why this is - e.g. in the normal case
chrisha
2016/01/08 22:29:00
Done.
| |
86 // interpreter, and this prevents the handle tracking from complaining about | |
87 // there being two owners of the same handle. | |
88 generator.ReleaseHandlesWithoutClosing(); | |
89 | |
90 // Parse the command line. This creates scoped handles around the same handles | |
91 // that are currently owned by the generator. | |
92 auto interpreted = ChromeWatcherCommandLine::InterpretCommandLine(cmd_line); | |
93 EXPECT_TRUE(interpreted); | |
94 | |
95 // Leave the handles in the interpreter and expect it to explode upon | |
96 // destruction. | |
97 EXPECT_DEATH(interpreted.reset(), "Handles left untaken."); | |
98 | |
99 // The above call to the destructor only runs in the context of the death test | |
100 // child process. To prevent the parent process from exploding in a similar | |
101 // fashion, release the handles so the destructor is happy. | |
102 interpreted->TakeOnInitializedEventHandle().Close(); | |
103 interpreted->TakeParentProcessHandle().Close(); | |
104 } | |
105 | |
106 TEST(ChromeWatcherCommandLineTest, SuccessfulParse) { | |
107 base::Process process(base::Process::OpenWithAccess( | |
Sigurður Ásgeirsson
2016/01/08 16:55:19
This code repeats a fair bit - move to a fixture?
chrisha
2016/01/08 22:29:00
Done.
| |
108 base::GetCurrentProcId(), PROCESS_QUERY_INFORMATION | SYNCHRONIZE)); | |
109 ASSERT_TRUE(process.Handle()); | |
110 | |
111 base::win::ScopedHandle event(::CreateEvent(nullptr, FALSE, FALSE, nullptr)); | |
112 ASSERT_TRUE(event.IsValid()); | |
113 | |
114 // The above handles are duplicated by the generator. | |
115 TestChromeWatcherCommandLineGenerator generator; | |
116 generator.SetOnInitializedEventHandle(event.Get()); | |
117 generator.SetParentProcessHandle(process.Handle()); | |
118 | |
119 // Expect there to be two inherited handles created by the generator. | |
120 std::vector<HANDLE> handles; | |
121 generator.GetInheritedHandles(&handles); | |
122 EXPECT_EQ(2U, handles.size()); | |
123 | |
124 base::CommandLine cmd_line = generator.GenerateCommandLine(); | |
125 | |
126 // Release the handles from the generator. Ownership will be picked up by the | |
127 // interpreter, and this prevents the handle tracking from complaining about | |
128 // there being two owners of the same handle. | |
129 generator.ReleaseHandlesWithoutClosing(); | |
130 | |
131 // Parse the command line. This creates scoped handles around the same handles | |
132 // that are currently owned by the generator. | |
133 auto interpreted = ChromeWatcherCommandLine::InterpretCommandLine(cmd_line); | |
134 EXPECT_TRUE(interpreted); | |
135 | |
136 EXPECT_EQ(::GetCurrentThreadId(), interpreted->main_thread_id()); | |
137 | |
138 // Explicitly take the handles from the interpreter so it doesn't explode. | |
139 base::win::ScopedHandle on_init = interpreted->TakeOnInitializedEventHandle(); | |
140 base::win::ScopedHandle proc = interpreted->TakeParentProcessHandle(); | |
141 EXPECT_TRUE(on_init.IsValid()); | |
142 EXPECT_TRUE(proc.IsValid()); | |
143 } | |
144 | |
145 TEST(ChromeWatcherCommandLineTest, BasicTest) { | |
146 // Ownership of these handles is passed to the ScopedHandles below via | |
Sigurður Ásgeirsson
2016/01/08 16:55:19
This is a legacy test?
chrisha
2016/01/08 22:29:00
Yup... commented that fact. It will be removed onc
| |
147 // InterpretChromeWatcherCommandLine(). | |
148 base::ProcessHandle current = | |
149 ::OpenProcess(PROCESS_QUERY_INFORMATION | SYNCHRONIZE, | |
150 TRUE, // Inheritable. | |
151 ::GetCurrentProcessId()); | |
152 ASSERT_NE(nullptr, current); | |
153 | |
154 HANDLE event = ::CreateEvent(nullptr, FALSE, FALSE, nullptr); | |
155 ASSERT_NE(nullptr, event); | |
156 DWORD current_thread_id = ::GetCurrentThreadId(); | |
157 base::CommandLine cmd_line = GenerateChromeWatcherCommandLine( | |
158 ExampleExe(), current, current_thread_id, event); | |
159 | |
160 base::win::ScopedHandle current_result; | |
161 DWORD current_thread_id_result = 0; | |
162 base::win::ScopedHandle event_result; | |
163 ASSERT_TRUE(InterpretChromeWatcherCommandLine( | |
164 cmd_line, ¤t_result, ¤t_thread_id_result, &event_result)); | |
165 ASSERT_EQ(current, current_result.Get()); | |
166 ASSERT_EQ(current_thread_id, current_thread_id_result); | |
167 ASSERT_EQ(event, event_result.Get()); | |
168 } | |
OLD | NEW |