OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
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 // This file contains unit tests for InterceptionManager. | 5 // This file contains unit tests for InterceptionManager. |
6 // The tests require private information so the whole interception.cc file is | 6 // The tests require private information so the whole interception.cc file is |
7 // included from this file. | 7 // included from this file. |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 ASSERT_LT(val, kAllocGranularity); | 111 ASSERT_LT(val, kAllocGranularity); |
112 if (prev_val) | 112 if (prev_val) |
113 ASSERT_EQ(val - prev_val, kAlignment); | 113 ASSERT_EQ(val - prev_val, kAlignment); |
114 if (val) | 114 if (val) |
115 min_nonzero_val = std::min(val, min_nonzero_val); | 115 min_nonzero_val = std::min(val, min_nonzero_val); |
116 min_val = std::min(val, min_val); | 116 min_val = std::min(val, min_val); |
117 prev_val = val; | 117 prev_val = val; |
118 max_val = std::max(val, max_val); | 118 max_val = std::max(val, max_val); |
119 } | 119 } |
120 ASSERT_EQ(max_val, kAllocGranularity - kAlignment); | 120 ASSERT_EQ(max_val, kAllocGranularity - kAlignment); |
121 ASSERT_EQ(min_val, 0); | 121 ASSERT_EQ(0u, min_val); |
122 ASSERT_EQ(min_nonzero_val, kAlignment); | 122 ASSERT_EQ(min_nonzero_val, kAlignment); |
123 } | 123 } |
124 | 124 |
125 TEST(InterceptionManagerTest, BufferLayout1) { | 125 TEST(InterceptionManagerTest, BufferLayout1) { |
126 wchar_t exe_name[MAX_PATH]; | 126 wchar_t exe_name[MAX_PATH]; |
127 ASSERT_NE(0u, GetModuleFileName(NULL, exe_name, MAX_PATH - 1)); | 127 ASSERT_NE(0u, GetModuleFileName(NULL, exe_name, MAX_PATH - 1)); |
128 | 128 |
129 TargetProcess *target = MakeTestTargetProcess(::GetCurrentProcess(), | 129 TargetProcess *target = MakeTestTargetProcess(::GetCurrentProcess(), |
130 ::GetModuleHandle(exe_name)); | 130 ::GetModuleHandle(exe_name)); |
131 | 131 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 INTERCEPTION_EAT, function, OPEN_KEY_ID); | 172 INTERCEPTION_EAT, function, OPEN_KEY_ID); |
173 interceptions.AddToPatchedFunctions(L"b.dll", | 173 interceptions.AddToPatchedFunctions(L"b.dll", |
174 "TheIncredibleCallToSaveTheWorld", | 174 "TheIncredibleCallToSaveTheWorld", |
175 INTERCEPTION_EAT, function, OPEN_KEY_ID); | 175 INTERCEPTION_EAT, function, OPEN_KEY_ID); |
176 interceptions.AddToPatchedFunctions(L"a.dll", "BIsLame", | 176 interceptions.AddToPatchedFunctions(L"a.dll", "BIsLame", |
177 INTERCEPTION_EAT, function, OPEN_KEY_ID); | 177 INTERCEPTION_EAT, function, OPEN_KEY_ID); |
178 interceptions.AddToPatchedFunctions(L"a.dll", "ARules", | 178 interceptions.AddToPatchedFunctions(L"a.dll", "ARules", |
179 INTERCEPTION_EAT, function, OPEN_KEY_ID); | 179 INTERCEPTION_EAT, function, OPEN_KEY_ID); |
180 | 180 |
181 // Verify that all interceptions were added | 181 // Verify that all interceptions were added |
182 ASSERT_EQ(18, interceptions.interceptions_.size()); | 182 ASSERT_EQ(18u, interceptions.interceptions_.size()); |
183 | 183 |
184 size_t buffer_size = interceptions.GetBufferSize(); | 184 size_t buffer_size = interceptions.GetBufferSize(); |
185 scoped_ptr<BYTE[]> local_buffer(new BYTE[buffer_size]); | 185 scoped_ptr<BYTE[]> local_buffer(new BYTE[buffer_size]); |
186 | 186 |
187 ASSERT_TRUE(interceptions.SetupConfigBuffer(local_buffer.get(), | 187 ASSERT_TRUE(interceptions.SetupConfigBuffer(local_buffer.get(), |
188 buffer_size)); | 188 buffer_size)); |
189 | 189 |
190 // At this point, the interceptions should have been separated into two | 190 // At this point, the interceptions should have been separated into two |
191 // groups: one group with the local ("cold") interceptions, consisting of | 191 // groups: one group with the local ("cold") interceptions, consisting of |
192 // everything from ntdll and stuff set as INTRECEPTION_SERVICE_CALL, and | 192 // everything from ntdll and stuff set as INTRECEPTION_SERVICE_CALL, and |
193 // another group with the interceptions belonging to dlls that will be "hot" | 193 // another group with the interceptions belonging to dlls that will be "hot" |
194 // patched on the client. The second group lives on local_buffer, and the | 194 // patched on the client. The second group lives on local_buffer, and the |
195 // first group remains on the list of interceptions (inside the object | 195 // first group remains on the list of interceptions (inside the object |
196 // "interceptions"). There are 3 local interceptions (of ntdll); the | 196 // "interceptions"). There are 3 local interceptions (of ntdll); the |
197 // other 15 have to be sent to the child to be performed "hot". | 197 // other 15 have to be sent to the child to be performed "hot". |
198 EXPECT_EQ(3, interceptions.interceptions_.size()); | 198 EXPECT_EQ(3u, interceptions.interceptions_.size()); |
199 | 199 |
200 int num_dlls, num_functions, num_names; | 200 int num_dlls, num_functions, num_names; |
201 WalkBuffer(local_buffer.get(), buffer_size, &num_dlls, &num_functions, | 201 WalkBuffer(local_buffer.get(), buffer_size, &num_dlls, &num_functions, |
202 &num_names); | 202 &num_names); |
203 | 203 |
204 // The 15 interceptions on the buffer (to the child) should be grouped on 6 | 204 // The 15 interceptions on the buffer (to the child) should be grouped on 6 |
205 // dlls. Only four interceptions are using an explicit name for the | 205 // dlls. Only four interceptions are using an explicit name for the |
206 // interceptor function. | 206 // interceptor function. |
207 EXPECT_EQ(6, num_dlls); | 207 EXPECT_EQ(6, num_dlls); |
208 EXPECT_EQ(15, num_functions); | 208 EXPECT_EQ(15, num_functions); |
(...skipping 16 matching lines...) Expand all Loading... |
225 interceptions.AddToPatchedFunctions(L"ntdll.dll", "NtCreateFile", | 225 interceptions.AddToPatchedFunctions(L"ntdll.dll", "NtCreateFile", |
226 INTERCEPTION_SERVICE_CALL, function, | 226 INTERCEPTION_SERVICE_CALL, function, |
227 OPEN_FILE_ID); | 227 OPEN_FILE_ID); |
228 interceptions.AddToPatchedFunctions(L"kernel32.dll", "CreateFileEx", | 228 interceptions.AddToPatchedFunctions(L"kernel32.dll", "CreateFileEx", |
229 INTERCEPTION_EAT, function, OPEN_FILE_ID); | 229 INTERCEPTION_EAT, function, OPEN_FILE_ID); |
230 interceptions.AddToUnloadModules(L"some02.dll"); | 230 interceptions.AddToUnloadModules(L"some02.dll"); |
231 interceptions.AddToPatchedFunctions(L"kernel32.dll", "SomeFileEx", | 231 interceptions.AddToPatchedFunctions(L"kernel32.dll", "SomeFileEx", |
232 INTERCEPTION_SMART_SIDESTEP, function, | 232 INTERCEPTION_SMART_SIDESTEP, function, |
233 OPEN_FILE_ID); | 233 OPEN_FILE_ID); |
234 // Verify that all interceptions were added | 234 // Verify that all interceptions were added |
235 ASSERT_EQ(5, interceptions.interceptions_.size()); | 235 ASSERT_EQ(5u, interceptions.interceptions_.size()); |
236 | 236 |
237 size_t buffer_size = interceptions.GetBufferSize(); | 237 size_t buffer_size = interceptions.GetBufferSize(); |
238 scoped_ptr<BYTE[]> local_buffer(new BYTE[buffer_size]); | 238 scoped_ptr<BYTE[]> local_buffer(new BYTE[buffer_size]); |
239 | 239 |
240 ASSERT_TRUE(interceptions.SetupConfigBuffer(local_buffer.get(), | 240 ASSERT_TRUE(interceptions.SetupConfigBuffer(local_buffer.get(), |
241 buffer_size)); | 241 buffer_size)); |
242 | 242 |
243 // At this point, the interceptions should have been separated into two | 243 // At this point, the interceptions should have been separated into two |
244 // groups: one group with the local ("cold") interceptions, and another | 244 // groups: one group with the local ("cold") interceptions, and another |
245 // group with the interceptions belonging to dlls that will be "hot" | 245 // group with the interceptions belonging to dlls that will be "hot" |
246 // patched on the client. The second group lives on local_buffer, and the | 246 // patched on the client. The second group lives on local_buffer, and the |
247 // first group remains on the list of interceptions, in this case just one. | 247 // first group remains on the list of interceptions, in this case just one. |
248 EXPECT_EQ(1, interceptions.interceptions_.size()); | 248 EXPECT_EQ(1u, interceptions.interceptions_.size()); |
249 | 249 |
250 int num_dlls, num_functions, num_names; | 250 int num_dlls, num_functions, num_names; |
251 WalkBuffer(local_buffer.get(), buffer_size, &num_dlls, &num_functions, | 251 WalkBuffer(local_buffer.get(), buffer_size, &num_dlls, &num_functions, |
252 &num_names); | 252 &num_names); |
253 | 253 |
254 EXPECT_EQ(3, num_dlls); | 254 EXPECT_EQ(3, num_dlls); |
255 EXPECT_EQ(4, num_functions); | 255 EXPECT_EQ(4, num_functions); |
256 EXPECT_EQ(0, num_names); | 256 EXPECT_EQ(0, num_names); |
257 } | 257 } |
258 | 258 |
259 } // namespace sandbox | 259 } // namespace sandbox |
OLD | NEW |