Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 #include <gtest/gtest.h> | 1 #include <gtest/gtest.h> |
2 #include "../process.h" | 2 #include "../process.h" |
3 #include <functional> | 3 #include <functional> |
4 | 4 |
5 // Turn off warnings for string copies | 5 // Turn off warnings for string copies |
6 #pragma warning( disable : 4996 ) | 6 #pragma warning( disable : 4996 ) |
7 | 7 |
8 //------------------------------------------------------- | 8 //------------------------------------------------------- |
9 // Comparison objects | 9 // Comparison objects |
10 //------------------------------------------------------- | 10 //------------------------------------------------------- |
11 | 11 |
12 const wchar_t exactExeName[] = L"installer-ca-tests.exe"; | 12 const wchar_t exactExeName[] = L"installer-ca-tests.exe"; |
13 const std::wstring exactExeString(exactExeName); | 13 const std::wstring exactExeString(exactExeName); |
14 const WstringCaseInsensitive exactExeStringCI(exactExeName); | 14 const WstringCaseInsensitive exactExeStringCi(exactExeName); |
15 | 15 |
16 const wchar_t mixedcaseExeName[] = L"Installer-CA-Tests.exe"; | 16 const wchar_t mixedcaseExeName[] = L"Installer-CA-Tests.exe"; |
17 const WstringCaseInsensitive mixedcaseExeStringCI(mixedcaseExeName); | 17 const WstringCaseInsensitive mixedcaseExeStringCi(mixedcaseExeName); |
Oleksandr
2015/10/29 23:26:22
Nit: Since we already have AdbCloseIe, for example
Eric
2015/11/14 18:01:06
Done.
| |
18 | 18 |
19 const wchar_t unknownName[] = L"non-matching-name"; | 19 const wchar_t unknownName[] = L"non-matching-name"; |
20 const wchar_t* multipleExeNames[] = {mixedcaseExeName, unknownName}; | 20 const wchar_t* multipleExeNames[] = {mixedcaseExeName, unknownName}; |
21 | 21 |
22 /** | 22 /** |
23 * Compare to our own process name, case-sensitive, no length limit | 23 * Compare to our own process name, case-sensitive, no length limit |
24 */ | 24 */ |
25 struct OurProcessByName | 25 struct OurProcessByName |
26 : std::unary_function<PROCESSENTRY32W, bool> | 26 : std::unary_function<PROCESSENTRY32W, bool> |
27 { | 27 { |
28 bool operator()(const PROCESSENTRY32W& process) | 28 bool operator()(const PROCESSENTRY32W& process) |
29 { | 29 { |
30 return std::wstring(process.szExeFile) == exactExeString; | 30 return std::wstring(process.szExeFile) == exactExeString; |
31 }; | 31 }; |
32 }; | 32 }; |
33 | 33 |
34 /** | 34 /** |
35 * Compare to our own process name, case-insensitive, no length limit | 35 * Compare to our own process name, case-insensitive, no length limit |
36 */ | 36 */ |
37 struct OurProcessByNameCI | 37 struct OurProcessByNameCi |
Oleksandr
2015/10/29 23:26:22
Nit: Same comment about abbreviation as above. Als
| |
38 : std::unary_function<PROCESSENTRY32W, bool> | 38 : std::unary_function<PROCESSENTRY32W, bool> |
39 { | 39 { |
40 bool operator()(const PROCESSENTRY32W& process) | 40 bool operator()(const PROCESSENTRY32W& process) |
41 { | 41 { |
42 return WstringCaseInsensitive(process.szExeFile) == mixedcaseExeStringCI; | 42 return WstringCaseInsensitive(process.szExeFile) == mixedcaseExeStringCi; |
43 }; | 43 }; |
44 }; | 44 }; |
45 | 45 |
46 //------------------------------------------------------- | 46 //------------------------------------------------------- |
47 //------------------------------------------------------- | 47 //------------------------------------------------------- |
48 /** | 48 /** |
49 * Filter by process name. Comparison is case-insensitive. | 49 * Filter by process name. Comparison is case-insensitive. |
50 */ | 50 */ |
51 class ProcessByAnyFileNameCI | 51 class ProcessByAnyFileNameCi |
52 : public std::unary_function<PROCESSENTRY32W, bool> | 52 : public std::unary_function<PROCESSENTRY32W, bool> |
53 { | 53 { |
54 const FileNameSet& names; | 54 const FileNameSet& names; |
55 public: | 55 public: |
56 bool operator()(const PROCESSENTRY32W& process) | 56 bool operator()(const PROCESSENTRY32W& process) |
57 { | 57 { |
58 return names.find(process.szExeFile) != names.end(); | 58 return names.find(process.szExeFile) != names.end(); |
59 } | 59 } |
60 ProcessByAnyFileNameCI(const FileNameSet& names) | 60 ProcessByAnyFileNameCi(const FileNameSet& names) |
61 : names(names) | 61 : names(names) |
62 {} | 62 {} |
63 }; | 63 }; |
64 | 64 |
65 /** | 65 /** |
66 * Filter by process name. Comparison is case-insensitive. | 66 * Filter by process name. Comparison is case-insensitive. |
67 */ | 67 */ |
68 class ProcessByNameCI | 68 class ProcessByNameCi |
69 : public std::unary_function<PROCESSENTRY32W, bool> | 69 : public std::unary_function<PROCESSENTRY32W, bool> |
70 { | 70 { |
71 const WstringCaseInsensitive _name; | 71 const WstringCaseInsensitive _name; |
72 public: | 72 public: |
73 bool operator()(const PROCESSENTRY32W& process) | 73 bool operator()(const PROCESSENTRY32W& process) |
74 { | 74 { |
75 return _name == WstringCaseInsensitive(process.szExeFile); | 75 return _name == WstringCaseInsensitive(process.szExeFile); |
76 } | 76 } |
77 | 77 |
78 ProcessByNameCI(const wchar_t* name) | 78 ProcessByNameCi(const wchar_t* name) |
79 : _name(name) | 79 : _name(name) |
80 {} | 80 {} |
81 }; | 81 }; |
82 | 82 |
83 //------------------------------------------------------- | 83 //------------------------------------------------------- |
84 // TESTS, no snapshots | 84 // TESTS, no snapshots |
85 //------------------------------------------------------- | 85 //------------------------------------------------------- |
86 PROCESSENTRY32 ProcessWithName(const wchar_t* s) | 86 PROCESSENTRY32 ProcessWithName(const wchar_t* s) |
87 { | 87 { |
88 PROCESSENTRY32W p; | 88 PROCESSENTRY32W p; |
89 wcsncpy(p.szExeFile, s, MAX_PATH); | 89 wcsncpy(p.szExeFile, s, MAX_PATH); |
90 return p; | 90 return p; |
91 } | 91 } |
92 | 92 |
93 PROCESSENTRY32 processEmpty = ProcessWithName(L""); | 93 PROCESSENTRY32 processEmpty = ProcessWithName(L""); |
94 PROCESSENTRY32 processExact = ProcessWithName(exactExeName); | 94 PROCESSENTRY32 processExact = ProcessWithName(exactExeName); |
95 PROCESSENTRY32 processMixedcase = ProcessWithName(mixedcaseExeName); | 95 PROCESSENTRY32 processMixedcase = ProcessWithName(mixedcaseExeName); |
96 PROCESSENTRY32 processExplorer = ProcessWithName(L"explorer.exe"); | 96 PROCESSENTRY32 processExplorer = ProcessWithName(L"explorer.exe"); |
97 PROCESSENTRY32 processAbsent = ProcessWithName(L"no_such_name"); | 97 PROCESSENTRY32 processAbsent = ProcessWithName(L"no_such_name"); |
98 | 98 |
99 FileNameSet multipleNameSet(multipleExeNames); | 99 FileNameSet multipleNameSet(multipleExeNames); |
100 ProcessByAnyFileNameCI findInSet(multipleNameSet); | 100 ProcessByAnyFileNameCi findInSet(multipleNameSet); |
101 ProcessByAnyExeNotImmersive findInSetNotImmersive(multipleNameSet); | 101 ProcessByAnyExeNotImmersive findInSetNotImmersive(multipleNameSet); |
102 | 102 |
103 | 103 |
104 TEST(FileNameSet, ValidateSetup) | 104 TEST(FileNameSet, ValidateSetup) |
105 { | 105 { |
106 ASSERT_EQ(2u, multipleNameSet.size()); | 106 ASSERT_EQ(2u, multipleNameSet.size()); |
107 ASSERT_TRUE(multipleNameSet.find(exactExeStringCI) != multipleNameSet.end()); | 107 ASSERT_TRUE(multipleNameSet.find(exactExeStringCi) != multipleNameSet.end()); |
108 ASSERT_TRUE(multipleNameSet.find(mixedcaseExeStringCI) != multipleNameSet.end( )); | 108 ASSERT_TRUE(multipleNameSet.find(mixedcaseExeStringCi) != multipleNameSet.end( )); |
109 ASSERT_TRUE(multipleNameSet.find(L"") == multipleNameSet.end()); | 109 ASSERT_TRUE(multipleNameSet.find(L"") == multipleNameSet.end()); |
110 ASSERT_TRUE(multipleNameSet.find(L"not-in-list") == multipleNameSet.end()); | 110 ASSERT_TRUE(multipleNameSet.find(L"not-in-list") == multipleNameSet.end()); |
111 } | 111 } |
112 | 112 |
113 TEST(ProcessByAnyFileNameCI, Empty) | 113 TEST(ProcessByAnyFileNameCi, Empty) |
114 { | 114 { |
115 FileNameSet s; | 115 FileNameSet s; |
116 ProcessByAnyFileNameCI x(s); | 116 ProcessByAnyFileNameCi x(s); |
117 | 117 |
118 ASSERT_FALSE(x(processEmpty)); | 118 ASSERT_FALSE(x(processEmpty)); |
119 ASSERT_FALSE(x(processExact)); | 119 ASSERT_FALSE(x(processExact)); |
120 ASSERT_FALSE(x(processMixedcase)); | 120 ASSERT_FALSE(x(processMixedcase)); |
121 ASSERT_FALSE(x(processExplorer)); | 121 ASSERT_FALSE(x(processExplorer)); |
122 ASSERT_FALSE(x(processAbsent)); | 122 ASSERT_FALSE(x(processAbsent)); |
123 } | 123 } |
124 | 124 |
125 TEST(ProcessByAnyFileNameCI, SingleElementKnown) | 125 TEST(ProcessByAnyFileNameCi, SingleElementKnown) |
126 { | 126 { |
127 const wchar_t* elements[1] = {exactExeName}; | 127 const wchar_t* elements[1] = {exactExeName}; |
128 FileNameSet s(elements); | 128 FileNameSet s(elements); |
129 ProcessByAnyFileNameCI x(s); | 129 ProcessByAnyFileNameCi x(s); |
130 | 130 |
131 ASSERT_FALSE(x(processEmpty)); | 131 ASSERT_FALSE(x(processEmpty)); |
132 ASSERT_TRUE(x(processExact)); | 132 ASSERT_TRUE(x(processExact)); |
133 ASSERT_TRUE(x(processMixedcase)); | 133 ASSERT_TRUE(x(processMixedcase)); |
134 ASSERT_FALSE(x(processExplorer)); | 134 ASSERT_FALSE(x(processExplorer)); |
135 ASSERT_FALSE(x(processAbsent)); | 135 ASSERT_FALSE(x(processAbsent)); |
136 } | 136 } |
137 | 137 |
138 TEST(ProcessByAnyFileNameCI, SingleElementUnknown) | 138 TEST(ProcessByAnyFileNameCi, SingleElementUnknown) |
139 { | 139 { |
140 const wchar_t* elements[1] = {unknownName}; | 140 const wchar_t* elements[1] = {unknownName}; |
141 FileNameSet s(elements); | 141 FileNameSet s(elements); |
142 ProcessByAnyFileNameCI x(s); | 142 ProcessByAnyFileNameCi x(s); |
143 | 143 |
144 ASSERT_FALSE(x(processEmpty)); | 144 ASSERT_FALSE(x(processEmpty)); |
145 ASSERT_FALSE(x(processExact)); | 145 ASSERT_FALSE(x(processExact)); |
146 ASSERT_FALSE(x(processMixedcase)); | 146 ASSERT_FALSE(x(processMixedcase)); |
147 ASSERT_FALSE(x(processExplorer)); | 147 ASSERT_FALSE(x(processExplorer)); |
148 ASSERT_FALSE(x(processAbsent)); | 148 ASSERT_FALSE(x(processAbsent)); |
149 } | 149 } |
150 | 150 |
151 TEST(ProcessByAnyFileNameCI, TwoElements) | 151 TEST(ProcessByAnyFileNameCi, TwoElements) |
152 { | 152 { |
153 FileNameSet s(multipleExeNames); | 153 FileNameSet s(multipleExeNames); |
154 ProcessByAnyFileNameCI x(s); | 154 ProcessByAnyFileNameCi x(s); |
155 | 155 |
156 ASSERT_FALSE(findInSet(processEmpty)); | 156 ASSERT_FALSE(findInSet(processEmpty)); |
157 ASSERT_TRUE(findInSet(processExact)); | 157 ASSERT_TRUE(findInSet(processExact)); |
158 ASSERT_TRUE(findInSet(processMixedcase)); | 158 ASSERT_TRUE(findInSet(processMixedcase)); |
159 ASSERT_FALSE(findInSet(processExplorer)); | 159 ASSERT_FALSE(findInSet(processExplorer)); |
160 ASSERT_FALSE(findInSet(processAbsent)); | 160 ASSERT_FALSE(findInSet(processAbsent)); |
161 } | 161 } |
162 | 162 |
163 //------------------------------------------------------- | 163 //------------------------------------------------------- |
164 // Single-snapshot version of initializers | 164 // Single-snapshot version of initializers |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
203 InitializeProcessList(v, OurProcessByName(), CopyAll()); | 203 InitializeProcessList(v, OurProcessByName(), CopyAll()); |
204 size_t size(v.size()); | 204 size_t size(v.size()); |
205 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul taneously | 205 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul taneously |
206 ASSERT_GE(1u, size); | 206 ASSERT_GE(1u, size); |
207 } | 207 } |
208 | 208 |
209 /** | 209 /** |
210 * The only process we are really guaranteed to have is this test process itself . | 210 * The only process we are really guaranteed to have is this test process itself . |
211 * This test uses same one used in Process_Closer | 211 * This test uses same one used in Process_Closer |
212 */ | 212 */ |
213 TEST(ProcessListTest, FindOurProcessCIGeneric) | 213 TEST(ProcessListTest, FindOurProcessCiGeneric) |
214 { | 214 { |
215 std::vector<PROCESSENTRY32W> v; | 215 std::vector<PROCESSENTRY32W> v; |
216 InitializeProcessList(v, ProcessByNameCI(mixedcaseExeName), CopyAll()); | 216 InitializeProcessList(v, ProcessByNameCi(mixedcaseExeName), CopyAll()); |
217 size_t size(v.size()); | 217 size_t size(v.size()); |
218 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul taneously | 218 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul taneously |
219 ASSERT_GE(1u, size); | 219 ASSERT_GE(1u, size); |
220 } | 220 } |
221 | 221 |
222 /** | 222 /** |
223 * The only process we are really guaranteed to have is this test process itself . | 223 * The only process we are really guaranteed to have is this test process itself . |
224 * This test uses the generic filter function. | 224 * This test uses the generic filter function. |
225 */ | 225 */ |
226 TEST(ProcessListTest, FindOurProcessCIAsUsed) | 226 TEST(ProcessListTest, FindOurProcessCiAsUsed) |
227 { | 227 { |
228 std::vector<PROCESSENTRY32W> v; | 228 std::vector<PROCESSENTRY32W> v; |
229 InitializeProcessList(v, ProcessByAnyFileNameCI(FileNameSet(multipleExeNames)) , CopyAll()); | 229 InitializeProcessList(v, ProcessByAnyFileNameCi(FileNameSet(multipleExeNames)) , CopyAll()); |
230 size_t size(v.size()); | 230 size_t size(v.size()); |
231 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul taneously | 231 EXPECT_EQ(1u, size); // Please, don't run multiple test executables simul taneously |
232 ASSERT_GE(1u, size); | 232 ASSERT_GE(1u, size); |
233 } | 233 } |
234 | 234 |
235 /** | 235 /** |
236 * Locate the PID of our process. | 236 * Locate the PID of our process. |
237 */ | 237 */ |
238 TEST(ProcessListTest, FindOurPID) | 238 TEST(ProcessListTest, FindOurPid) |
Oleksandr
2015/10/29 23:26:22
Nit: How about FindOurPid? Same below.
Eric
2015/11/14 18:01:06
Done.
| |
239 { | 239 { |
240 std::vector<DWORD> v; | 240 std::vector<DWORD> v; |
241 InitializeProcessList(v, OurProcessByName(), CopyPID()); | 241 InitializeProcessList(v, OurProcessByName(), CopyPid()); |
242 size_t size(v.size()); | 242 size_t size(v.size()); |
243 EXPECT_EQ(size, 1u); // Please, don't run multiple test executables simul taneously | 243 EXPECT_EQ(size, 1u); // Please, don't run multiple test executables simul taneously |
244 ASSERT_GE(size, 1u); | 244 ASSERT_GE(size, 1u); |
245 } | 245 } |
246 | 246 |
247 /** | 247 /** |
248 * Locate the PID of our process using the | 248 * Locate the PID of our process using the |
249 */ | 249 */ |
250 TEST(ProcessListTest, FindOurProcessInSet) | 250 TEST(ProcessListTest, FindOurProcessInSet) |
251 { | 251 { |
252 std::vector<DWORD> v; | 252 std::vector<DWORD> v; |
253 InitializeProcessList(v, findInSet, CopyPID()); | 253 InitializeProcessList(v, findInSet, CopyPid()); |
254 size_t size(v.size()); | 254 size_t size(v.size()); |
255 EXPECT_EQ(size, 1u); // Please, don't run multiple test executables simul taneously | 255 EXPECT_EQ(size, 1u); // Please, don't run multiple test executables simul taneously |
256 ASSERT_GE(size, 1u); | 256 ASSERT_GE(size, 1u); |
257 } | 257 } |
258 | 258 |
259 //------------------------------------------------------- | 259 //------------------------------------------------------- |
260 // TESTS for process ID sets | 260 // TESTS for process ID sets |
261 //------------------------------------------------------- | 261 //------------------------------------------------------- |
262 /* | 262 /* |
263 * Can't use copy_all without a definition for "less<PROCESSENTRY32W>". | 263 * Can't use copy_all without a definition for "less<PROCESSENTRY32W>". |
264 * Thus all tests only use copy_PID | 264 * Thus all tests only use CopyPid |
265 */ | 265 */ |
266 | 266 |
267 /** | 267 /** |
268 * Construction test ensures that we don't throw and that at least one process s hows up. | 268 * Construction test ensures that we don't throw and that at least one process s hows up. |
269 */ | 269 */ |
270 TEST(PIDSet, ConstructSet) | 270 TEST(PidSet, ConstructSet) |
271 { | 271 { |
272 std::set<DWORD> s; | 272 std::set<DWORD> s; |
273 InitializeProcessSet(s, EveryProcess(), CopyPID()); | 273 InitializeProcessSet(s, EveryProcess(), CopyPid()); |
274 ASSERT_GE(s.size(), 1u); | 274 ASSERT_GE(s.size(), 1u); |
275 } | 275 } |
276 | 276 |
277 TEST(PIDSet, FindOurProcessInSet) | 277 TEST(PidSet, FindOurProcessInSet) |
278 { | 278 { |
279 std::set<DWORD> s; | 279 std::set<DWORD> s; |
280 InitializeProcessSet(s, findInSet, CopyPID()); | 280 InitializeProcessSet(s, findInSet, CopyPid()); |
281 size_t size(s.size()); | 281 size_t size(s.size()); |
282 EXPECT_EQ(size, 1u); | 282 EXPECT_EQ(size, 1u); |
283 ASSERT_GE(size, 1u); | 283 ASSERT_GE(size, 1u); |
284 } | 284 } |
285 | 285 |
286 TEST(PIDSet, FindOurProcessInSetNotImmersive) | 286 TEST(PidSet, FindOurProcessInSetNotImmersive) |
287 { | 287 { |
288 std::set<DWORD> s; | 288 std::set<DWORD> s; |
289 InitializeProcessSet(s, findInSetNotImmersive, CopyPID()); | 289 InitializeProcessSet(s, findInSetNotImmersive, CopyPid()); |
290 size_t size(s.size()); | 290 size_t size(s.size()); |
291 EXPECT_EQ(size, 1u); | 291 EXPECT_EQ(size, 1u); |
292 ASSERT_GE(size, 1u); | 292 ASSERT_GE(size, 1u); |
293 } | 293 } |
LEFT | RIGHT |