| 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> |
| 10 #include <set> |
| 11 |
| 9 #include <windows.h> | 12 #include <windows.h> |
| 10 | 13 |
| 14 #include "base/bits.h" |
| 11 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 12 #include "sandbox/win/src/interception.h" | 16 #include "sandbox/win/src/interception.h" |
| 13 #include "sandbox/win/src/interceptors.h" | 17 #include "sandbox/win/src/interceptors.h" |
| 14 #include "sandbox/win/src/interception_internal.h" | 18 #include "sandbox/win/src/interception_internal.h" |
| 15 #include "sandbox/win/src/target_process.h" | 19 #include "sandbox/win/src/target_process.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 21 |
| 18 namespace sandbox { | 22 namespace sandbox { |
| 19 | 23 |
| 24 namespace internal { |
| 25 size_t GetGranularAlignedRandomOffset(size_t size); |
| 26 } |
| 27 |
| 20 // Walks the settings buffer, verifying that the values make sense and counting | 28 // Walks the settings buffer, verifying that the values make sense and counting |
| 21 // objects. | 29 // objects. |
| 22 // Arguments: | 30 // Arguments: |
| 23 // buffer (in): the buffer to walk. | 31 // buffer (in): the buffer to walk. |
| 24 // size (in): buffer size | 32 // size (in): buffer size |
| 25 // num_dlls (out): count of the dlls on the buffer. | 33 // num_dlls (out): count of the dlls on the buffer. |
| 26 // num_function (out): count of intercepted functions. | 34 // num_function (out): count of intercepted functions. |
| 27 // num_names (out): count of named interceptor functions. | 35 // num_names (out): count of named interceptor functions. |
| 28 void WalkBuffer(void* buffer, size_t size, int* num_dlls, int* num_functions, | 36 void WalkBuffer(void* buffer, size_t size, int* num_dlls, int* num_functions, |
| 29 int* num_names) { | 37 int* num_names) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 function = reinterpret_cast<FunctionInfo*>( | 76 function = reinterpret_cast<FunctionInfo*>( |
| 69 reinterpret_cast<char*>(function) + function->record_bytes); | 77 reinterpret_cast<char*>(function) + function->record_bytes); |
| 70 } | 78 } |
| 71 | 79 |
| 72 (*num_dlls)++; | 80 (*num_dlls)++; |
| 73 dll = reinterpret_cast<DllPatchInfo*>(reinterpret_cast<char*>(dll) + | 81 dll = reinterpret_cast<DllPatchInfo*>(reinterpret_cast<char*>(dll) + |
| 74 dll->record_bytes); | 82 dll->record_bytes); |
| 75 } | 83 } |
| 76 } | 84 } |
| 77 | 85 |
| 86 TEST(InterceptionManagerTest, GetGranularAlignedRandomOffset) { |
| 87 std::set<size_t> sizes; |
| 88 |
| 89 // 544 is current value of interceptions_.size() * sizeof(ThunkData) + |
| 90 // sizeof(DllInterceptionData). |
| 91 const size_t kThunkBytes = 544; |
| 92 |
| 93 // ciel(log2(544)) = 10. |
| 94 // Alignment must be 2^10 = 1024. |
| 95 const size_t kAlignmentBits = base::bits::Log2Ceiling(kThunkBytes); |
| 96 const size_t kAlignment = 1 << kAlignmentBits; |
| 97 |
| 98 const size_t kAllocGranularity = 65536; |
| 99 |
| 100 // Generate enough sample data to ensure there is at least one value in each |
| 101 // potential bucket. |
| 102 for (size_t i = 0; i < 1000000; i++) |
| 103 sizes.insert(internal::GetGranularAlignedRandomOffset(kThunkBytes)); |
| 104 |
| 105 size_t prev_val = 0; |
| 106 size_t min_val = kAllocGranularity; |
| 107 size_t min_nonzero_val = kAllocGranularity; |
| 108 size_t max_val = 0; |
| 109 |
| 110 for (size_t val : sizes) { |
| 111 ASSERT_LT(val, kAllocGranularity); |
| 112 if (prev_val) |
| 113 ASSERT_EQ(val - prev_val, kAlignment); |
| 114 if (val) |
| 115 min_nonzero_val = std::min(val, min_nonzero_val); |
| 116 min_val = std::min(val, min_val); |
| 117 prev_val = val; |
| 118 max_val = std::max(val, max_val); |
| 119 } |
| 120 ASSERT_EQ(max_val, kAllocGranularity - kAlignment); |
| 121 ASSERT_EQ(min_val, 0); |
| 122 ASSERT_EQ(min_nonzero_val, kAlignment); |
| 123 } |
| 124 |
| 78 TEST(InterceptionManagerTest, BufferLayout1) { | 125 TEST(InterceptionManagerTest, BufferLayout1) { |
| 79 wchar_t exe_name[MAX_PATH]; | 126 wchar_t exe_name[MAX_PATH]; |
| 80 ASSERT_NE(0u, GetModuleFileName(NULL, exe_name, MAX_PATH - 1)); | 127 ASSERT_NE(0u, GetModuleFileName(NULL, exe_name, MAX_PATH - 1)); |
| 81 | 128 |
| 82 TargetProcess *target = MakeTestTargetProcess(::GetCurrentProcess(), | 129 TargetProcess *target = MakeTestTargetProcess(::GetCurrentProcess(), |
| 83 ::GetModuleHandle(exe_name)); | 130 ::GetModuleHandle(exe_name)); |
| 84 | 131 |
| 85 InterceptionManager interceptions(target, true); | 132 InterceptionManager interceptions(target, true); |
| 86 | 133 |
| 87 // Any pointer will do for a function pointer. | 134 // Any pointer will do for a function pointer. |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 int num_dlls, num_functions, num_names; | 250 int num_dlls, num_functions, num_names; |
| 204 WalkBuffer(local_buffer.get(), buffer_size, &num_dlls, &num_functions, | 251 WalkBuffer(local_buffer.get(), buffer_size, &num_dlls, &num_functions, |
| 205 &num_names); | 252 &num_names); |
| 206 | 253 |
| 207 EXPECT_EQ(3, num_dlls); | 254 EXPECT_EQ(3, num_dlls); |
| 208 EXPECT_EQ(4, num_functions); | 255 EXPECT_EQ(4, num_functions); |
| 209 EXPECT_EQ(0, num_names); | 256 EXPECT_EQ(0, num_names); |
| 210 } | 257 } |
| 211 | 258 |
| 212 } // namespace sandbox | 259 } // namespace sandbox |
| OLD | NEW |