OLD | NEW |
| (Empty) |
1 // Copyright 2004-2010 Google Inc. | |
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 | |
16 #include <atlpath.h> | |
17 #include "omaha/base/app_util.h" | |
18 #include "omaha/base/constants.h" | |
19 #include "omaha/base/file.h" | |
20 #include "omaha/base/string.h" | |
21 #include "omaha/base/utils.h" | |
22 #include "omaha/base/vistautil.h" | |
23 #include "omaha/testing/unit_test.h" | |
24 | |
25 const TCHAR* const kKernel32Name = L"kernel32.dll"; | |
26 const TCHAR* const kShell32Name = L"shell32.dll"; | |
27 const TCHAR* const kComCtl32Name = L"comctl32.dll"; | |
28 | |
29 namespace omaha { | |
30 | |
31 namespace app_util { | |
32 | |
33 TEST(AppUtilTest, AppUtil) { | |
34 HMODULE module = NULL; | |
35 CString name; | |
36 struct Local { | |
37 static void Func() {} | |
38 }; | |
39 | |
40 // First test the functionality for EXE applications. | |
41 | |
42 // Test the app name. | |
43 name = GetAppName(); | |
44 EXPECT_STREQ(kUnittestName, name); | |
45 | |
46 // Test the module name. | |
47 name = GetCurrentModuleName(); | |
48 EXPECT_STREQ(kUnittestName, name); | |
49 | |
50 // Test the app name w/o extension. | |
51 name = GetAppNameWithoutExtension() + L".exe"; | |
52 EXPECT_STREQ(kUnittestName, name); | |
53 | |
54 // Test the module name w/o extension. | |
55 name = GetCurrentModulePath(); | |
56 EXPECT_STREQ(GetCurrentModuleDirectory() + L"\\" + kUnittestName, name); | |
57 | |
58 // Test the module path and directory. | |
59 name = GetCurrentModuleName(); | |
60 EXPECT_STREQ(kUnittestName, name); | |
61 | |
62 // Test an address. | |
63 module = GetCurrentModuleHandle(); | |
64 EXPECT_TRUE(IsAddressInCurrentModule(module + 0x1)); | |
65 EXPECT_TRUE(IsAddressInCurrentModule(&Local::Func)); | |
66 EXPECT_FALSE(IsAddressInCurrentModule(reinterpret_cast<void*>(0x1))); | |
67 | |
68 // Test the functionality for DLL modules. | |
69 // Use kernel32.dll | |
70 | |
71 // Get the loading address of kernel32. | |
72 HMODULE kernel32Module = ::LoadLibrary(kKernel32Name); | |
73 EXPECT_TRUE(kernel32Module != NULL); | |
74 | |
75 // Test the dll module handle using an address. | |
76 module = GetModuleHandleFromAddress(::ReadFile); | |
77 EXPECT_EQ(kernel32Module, module); | |
78 | |
79 CString system_path; | |
80 EXPECT_SUCCEEDED(GetFolderPath(CSIDL_SYSTEMX86, &system_path)); | |
81 | |
82 // Test the dll module directory. | |
83 name = GetModuleDirectory(module); | |
84 EXPECT_EQ(0, name.CompareNoCase(system_path)); | |
85 | |
86 // Test the dll module path. | |
87 name = GetModulePath(module); | |
88 EXPECT_EQ(0, name.CompareNoCase(system_path + L"\\" + kKernel32Name)); | |
89 | |
90 // Test the dll module name. | |
91 name = GetModuleName(module); | |
92 EXPECT_EQ(0, name.CompareNoCase(kKernel32Name)); | |
93 | |
94 // Other checks. | |
95 EXPECT_FALSE(GetWindowsDir().IsEmpty()); | |
96 EXPECT_FALSE(GetHostName().IsEmpty()); | |
97 EXPECT_FALSE(GetTempDir().IsEmpty()); | |
98 EXPECT_TRUE(String_EndsWith(GetTempDir(), _T("\\"), false)); | |
99 | |
100 // DLL versioning. | |
101 // For the tests to succeed, shell32.dll must be loaded in memory. | |
102 HMODULE shell32Module = ::LoadLibrary(kShell32Name); | |
103 EXPECT_NE(0, DllGetVersion(GetSystemDir() + L"\\" + kShell32Name)); | |
104 EXPECT_NE(0, DllGetVersion(kShell32Name)); | |
105 EXPECT_NE(0, SystemDllGetVersion(kShell32Name)); | |
106 | |
107 // For the tests to succeed, comctl32.dll must be loaded in memory. | |
108 // ComCtl32 may be loaded from a side-by-side (WinSxS) directory, so it is not | |
109 // practical to do a full-path or SystemDllGetVersion test with it. | |
110 HMODULE comctl32_module = ::LoadLibrary(kComCtl32Name); | |
111 EXPECT_NE(0, DllGetVersion(kComCtl32Name)); | |
112 | |
113 // kernel32 does not export DllGetVersion. | |
114 EXPECT_EQ(0, SystemDllGetVersion(kKernel32Name)); | |
115 | |
116 // Module clean-up. | |
117 EXPECT_TRUE(::FreeLibrary(comctl32_module)); | |
118 EXPECT_TRUE(::FreeLibrary(shell32Module)); | |
119 EXPECT_TRUE(::FreeLibrary(kernel32Module)); | |
120 } | |
121 | |
122 TEST(AppUtilTest, GetVersionFromModule) { | |
123 EXPECT_EQ(OMAHA_BUILD_VERSION, GetVersionFromModule(NULL)); | |
124 } | |
125 | |
126 TEST(AppUtilTest, GetVersionFromFile) { | |
127 CPath goopdate_path(GetCurrentModuleDirectory()); | |
128 ASSERT_TRUE(goopdate_path.Append(kUnittestName)); | |
129 ASSERT_TRUE(File::Exists(goopdate_path)); | |
130 | |
131 EXPECT_EQ(OMAHA_BUILD_VERSION, GetVersionFromFile(goopdate_path)); | |
132 } | |
133 | |
134 TEST(AppUtilTest, GetTempDirForImpersonatedOrCurrentUser) { | |
135 // The behavior should be the same when the code is not running impersonated. | |
136 EXPECT_STREQ(GetTempDir(), GetTempDirForImpersonatedOrCurrentUser()); | |
137 | |
138 // The behavior should be the same when the code impersonates as self. | |
139 EXPECT_TRUE(::ImpersonateSelf(SecurityImpersonation)); | |
140 EXPECT_STREQ(GetTempDir(), GetTempDirForImpersonatedOrCurrentUser()); | |
141 | |
142 EXPECT_TRUE(::RevertToSelf()); | |
143 } | |
144 | |
145 } // namespace app_util | |
146 | |
147 } // namespace omaha | |
OLD | NEW |