1 // Copyright 2013 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.
5 #include "chrome/browser/install_verification/win/module_list.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback_helpers.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "chrome/browser/install_verification/win/loaded_modules_snapshot.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(ModuleListTest
, TestCase
) {
17 std::vector
<HMODULE
> snapshot
;
18 ASSERT_TRUE(GetLoadedModulesSnapshot(&snapshot
));
19 scoped_ptr
<ModuleList
> module_list(
20 ModuleList::FromLoadedModuleSnapshot(snapshot
));
22 // Lookup the number of loaded modules.
23 size_t original_list_size
= module_list
->size();
24 ASSERT_GT(original_list_size
, 0u);
27 // Load in a new module. Pick msvidc32.dll as it is present from WinXP to
28 // Win8 and yet rarely used.
29 ASSERT_EQ(NULL
, ::GetModuleHandle(L
"msvidc32.dll"));
31 HMODULE new_dll
= ::LoadLibrary(L
"msvidc32.dll");
32 ASSERT_NE(static_cast<HMODULE
>(NULL
), new_dll
);
33 base::ScopedClosureRunner
release_new_dll(
34 base::Bind(base::IgnoreResult(&::FreeLibrary
), new_dll
));
36 // Verify that there is an increase in the snapshot size.
37 ASSERT_TRUE(GetLoadedModulesSnapshot(&snapshot
));
38 module_list
= ModuleList::FromLoadedModuleSnapshot(snapshot
);
39 ASSERT_GT(module_list
->size(), original_list_size
);
42 release_new_dll
.Reset();
44 // Reset module_list here. That should typically be the last ref on
45 // msvidc32.dll, so it will be unloaded now.
47 ASSERT_EQ(NULL
, ::GetModuleHandle(L
"msvidc32.dll"));
49 // List the modules from the stale snapshot (including a dangling HMODULE to
50 // msvidc32.dll), simulating a race condition.
51 module_list
= ModuleList::FromLoadedModuleSnapshot(snapshot
);
52 ASSERT_EQ(module_list
->size(), original_list_size
);