OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Crashpad Authors. All rights reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 #include "util/win/scoped_process_suspend.h" | |
16 | |
17 #include <tlhelp32.h> | |
18 | |
19 #include <algorithm> | |
20 #include <vector> | |
21 | |
22 #include "gtest/gtest.h" | |
23 #include "test/win/win_child_process.h" | |
24 | |
25 namespace crashpad { | |
26 namespace test { | |
27 namespace { | |
28 | |
29 // There is no such thing as process suspension on Windows, only thread | |
30 // suspension, so we verify that all threads match the desired suspend count. | |
Mark Mentovai
2015/09/04 15:57:50
This comment is a little terse given that the impl
scottmg
2015/09/04 22:05:38
That's right.
| |
31 bool SuspendCountMatches(HANDLE process, DWORD desired_suspend_count) { | |
32 DWORD process_id = GetProcessId(process); | |
33 | |
34 ScopedKernelHANDLE snapshot(CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0)); | |
35 if (snapshot.is_valid()) { | |
36 THREADENTRY32 te; | |
37 te.dwSize = sizeof(te); | |
38 if (Thread32First(snapshot.get(), &te)) { | |
39 do { | |
40 if (te.dwSize >= offsetof(THREADENTRY32, th32OwnerProcessID) + | |
41 sizeof(te.th32OwnerProcessID)) { | |
42 if (te.th32OwnerProcessID == process_id) { | |
Mark Mentovai
2015/09/04 15:57:50
if (a) { if (b) { … }} → if (a && b) { … }
scottmg
2015/09/04 22:05:38
Done.
| |
43 ScopedKernelHANDLE thread( | |
44 OpenThread(THREAD_ALL_ACCESS, false, te.th32ThreadID)); | |
45 DWORD result = SuspendThread(thread.get()); | |
46 EXPECT_NE(result, 0xffffffff); | |
47 if (result >= 0) | |
48 ResumeThread(thread.get()); | |
49 if (result != desired_suspend_count) | |
50 return false; | |
51 } | |
52 } | |
53 te.dwSize = sizeof(te); | |
54 } while (Thread32Next(snapshot.get(), &te)); | |
55 } | |
Mark Mentovai
2015/09/04 15:57:50
else return false? Better yet: if (a) { … } else {
scottmg
2015/09/04 22:05:38
Done.
| |
56 } | |
Mark Mentovai
2015/09/04 15:57:50
Same.
scottmg
2015/09/04 22:05:38
Done.
| |
57 | |
58 return true; | |
59 } | |
60 | |
61 class ScopedProcessSuspendTest final : public WinChildProcess { | |
62 public: | |
63 ScopedProcessSuspendTest() : WinChildProcess() {} | |
64 ~ScopedProcessSuspendTest() {} | |
65 | |
66 private: | |
67 int Run() override { | |
68 char c; | |
69 // Wait for notification from parent. | |
70 EXPECT_TRUE(LoggingReadFile(ReadPipeHandle(), &c, sizeof(c))); | |
71 EXPECT_EQ(c, ' '); | |
Mark Mentovai
2015/09/04 15:57:50
For gtest _EQ macros, it’s (expected, actual).
scottmg
2015/09/04 22:05:38
Still, eh? Done.
| |
72 return EXIT_SUCCESS; | |
73 } | |
74 | |
75 DISALLOW_COPY_AND_ASSIGN(ScopedProcessSuspendTest); | |
76 }; | |
77 | |
78 TEST(ScopedProcessSuspend, ScopedProcessSuspend) { | |
79 WinChildProcess::EntryPoint<ScopedProcessSuspendTest>(); | |
80 scoped_ptr<WinChildProcess::Handles> handles = WinChildProcess::Launch(); | |
81 | |
82 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); | |
83 | |
84 { | |
85 ScopedProcessSuspend suspend(handles->process.get()); | |
86 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 1)); | |
87 | |
88 { | |
89 ScopedProcessSuspend suspend(handles->process.get()); | |
90 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 2)); | |
91 } | |
92 | |
93 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 1)); | |
94 } | |
95 | |
96 EXPECT_TRUE(SuspendCountMatches(handles->process.get(), 0)); | |
97 | |
98 // Tell the child it's OK to terminate. | |
99 char c = ' '; | |
100 EXPECT_TRUE(WriteFile(handles->write.get(), &c, sizeof(c))); | |
101 } | |
102 | |
103 } // namespace | |
104 } // namespace test | |
105 } // namespace crashpad | |
OLD | NEW |