PageRenderTime 92ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/common/browser_utils_unittest.cc

https://github.com/Crystalnix/bitpop-omaha
C++ | 380 lines | 311 code | 47 blank | 22 comment | 35 complexity | 7858eecd0f245c84a0e32fb7f5c7dbe5 MD5 | raw file
  1. // Copyright 2008-2009 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. #include "omaha/common/browser_utils.h"
  16. #include "omaha/common/const_utils.h"
  17. #include "omaha/common/path.h"
  18. #include "omaha/common/reg_key.h"
  19. #include "omaha/testing/unit_test.h"
  20. namespace omaha {
  21. const CString kRegistryHiveOverrideClasses =
  22. CString(kRegistryHiveOverrideRoot) + _T("HKCR");
  23. // GetDefaultBrowserName() uses ::RegOpenCurrentUser, which does not appear to
  24. // be affected by registry hive overrides. Therefore, in order to test methods
  25. // that rely on it, the actual value must be replaced. This class saves the
  26. // value and restores it. If the test is interrupted before TearDown, the
  27. // default browser may not be correctly registered.
  28. class BrowserUtilsDefaultBrowserSavedTest : public testing::Test {
  29. protected:
  30. virtual void SetUp() {
  31. if (!RegKey::HasKey(kRegKeyUserDefaultBrowser)) {
  32. return;
  33. }
  34. EXPECT_SUCCEEDED(RegKey::GetValue(kRegKeyUserDefaultBrowser,
  35. NULL,
  36. &default_browser_name_));
  37. }
  38. virtual void TearDown() {
  39. if (default_browser_name_.IsEmpty()) {
  40. RegKey::DeleteKey(kRegKeyUserDefaultBrowser);
  41. return;
  42. }
  43. EXPECT_SUCCEEDED(RegKey::SetValue(kRegKeyUserDefaultBrowser,
  44. NULL,
  45. default_browser_name_));
  46. }
  47. CString default_browser_name_;
  48. };
  49. class GetLegacyDefaultBrowserInfoTest : public testing::Test {
  50. protected:
  51. virtual void SetUp() {
  52. RegKey::DeleteKey(kRegistryHiveOverrideClasses, true);
  53. RegKey classes_key;
  54. ASSERT_HRESULT_SUCCEEDED(classes_key.Create(kRegistryHiveOverrideClasses));
  55. ASSERT_HRESULT_SUCCEEDED(::RegOverridePredefKey(HKEY_CLASSES_ROOT,
  56. classes_key.Key()));
  57. }
  58. virtual void TearDown() {
  59. ASSERT_HRESULT_SUCCEEDED(::RegOverridePredefKey(HKEY_CLASSES_ROOT, NULL));
  60. ASSERT_HRESULT_SUCCEEDED(RegKey::DeleteKey(kRegistryHiveOverrideClasses,
  61. true));
  62. }
  63. };
  64. TEST_F(BrowserUtilsDefaultBrowserSavedTest, GetDefaultBrowserType_IE) {
  65. if (!ShouldRunLargeTest()) {
  66. return;
  67. }
  68. EXPECT_SUCCEEDED(
  69. RegKey::SetValue(kRegKeyUserDefaultBrowser, NULL, _T("IeXpLoRe.ExE")));
  70. BrowserType type = BROWSER_UNKNOWN;
  71. EXPECT_SUCCEEDED(GetDefaultBrowserType(&type));
  72. EXPECT_EQ(BROWSER_IE, type);
  73. }
  74. TEST_F(BrowserUtilsDefaultBrowserSavedTest, GetDefaultBrowserType_Firefox) {
  75. if (!ShouldRunLargeTest()) {
  76. return;
  77. }
  78. EXPECT_SUCCEEDED(
  79. RegKey::SetValue(kRegKeyUserDefaultBrowser, NULL, _T("FiReFoX.ExE")));
  80. BrowserType type = BROWSER_UNKNOWN;
  81. EXPECT_SUCCEEDED(GetDefaultBrowserType(&type));
  82. EXPECT_EQ(BROWSER_FIREFOX, type);
  83. }
  84. TEST_F(BrowserUtilsDefaultBrowserSavedTest, GetDefaultBrowserType_Chrome) {
  85. if (!ShouldRunLargeTest()) {
  86. return;
  87. }
  88. EXPECT_SUCCEEDED(
  89. RegKey::SetValue(kRegKeyUserDefaultBrowser, NULL, _T("ChRoMe.ExE")));
  90. BrowserType type = BROWSER_UNKNOWN;
  91. EXPECT_SUCCEEDED(GetDefaultBrowserType(&type));
  92. EXPECT_EQ(BROWSER_CHROME, type);
  93. }
  94. TEST_F(BrowserUtilsDefaultBrowserSavedTest, GetDefaultBrowserType_Unsupported) {
  95. if (!ShouldRunLargeTest()) {
  96. return;
  97. }
  98. EXPECT_SUCCEEDED(
  99. RegKey::SetValue(kRegKeyUserDefaultBrowser, NULL, _T("FoO.ExE")));
  100. BrowserType type = BROWSER_UNKNOWN;
  101. EXPECT_SUCCEEDED(GetDefaultBrowserType(&type));
  102. EXPECT_EQ(BROWSER_UNKNOWN, type);
  103. }
  104. TEST(BrowserUtilsTest, BrowserTypeToProcessName_Unknown) {
  105. CString exe_name;
  106. EXPECT_EQ(E_FAIL, BrowserTypeToProcessName(BROWSER_UNKNOWN, &exe_name));
  107. EXPECT_TRUE(exe_name.IsEmpty());
  108. }
  109. // Writes the default browser to ensure consistent results.
  110. TEST_F(BrowserUtilsDefaultBrowserSavedTest, BrowserTypeToProcessName_Default) {
  111. if (!ShouldRunLargeTest()) {
  112. return;
  113. }
  114. EXPECT_SUCCEEDED(
  115. RegKey::SetValue(kRegKeyUserDefaultBrowser, NULL, _T("IeXpLoRe.ExE")));
  116. CString default_exe_name;
  117. EXPECT_SUCCEEDED(GetDefaultBrowserName(&default_exe_name));
  118. EXPECT_STREQ(_T("IeXpLoRe.ExE"), default_exe_name);
  119. CString exe_name;
  120. EXPECT_SUCCEEDED(BrowserTypeToProcessName(BROWSER_DEFAULT, &exe_name));
  121. EXPECT_STREQ(_T("IeXpLoRe.ExE"), exe_name);
  122. }
  123. TEST(BrowserUtilsTest, BrowserTypeToProcessName_Browsers) {
  124. CString exe_name;
  125. EXPECT_SUCCEEDED(BrowserTypeToProcessName(BROWSER_IE, &exe_name));
  126. EXPECT_STREQ(_T("IEXPLORE.EXE"), exe_name);
  127. EXPECT_SUCCEEDED(BrowserTypeToProcessName(BROWSER_FIREFOX, &exe_name));
  128. EXPECT_STREQ(_T("FIREFOX.EXE"), exe_name);
  129. EXPECT_SUCCEEDED(BrowserTypeToProcessName(BROWSER_CHROME, &exe_name));
  130. EXPECT_STREQ(_T("CHROME.EXE"), exe_name);
  131. }
  132. TEST(BrowserUtilsTest, BrowserTypeToProcessName_Invalid) {
  133. CString exe_name;
  134. ExpectAsserts expect_asserts;
  135. EXPECT_EQ(E_FAIL, BrowserTypeToProcessName(BROWSER_MAX, &exe_name));
  136. EXPECT_TRUE(exe_name.IsEmpty());
  137. EXPECT_EQ(E_FAIL,
  138. BrowserTypeToProcessName(static_cast<BrowserType>(9), &exe_name));
  139. EXPECT_TRUE(exe_name.IsEmpty());
  140. EXPECT_EQ(E_FAIL,
  141. BrowserTypeToProcessName(static_cast<BrowserType>(-1), &exe_name));
  142. EXPECT_TRUE(exe_name.IsEmpty());
  143. }
  144. TEST(BrowserUtilsTest, GetBrowserImagePath_DefaultBrowser) {
  145. CString browser;
  146. ASSERT_SUCCEEDED(GetDefaultBrowserName(&browser));
  147. BrowserType default_type = BROWSER_UNKNOWN;
  148. if (browser.CompareNoCase(kIeExeName) == 0) {
  149. default_type = BROWSER_IE;
  150. } else if (browser.CompareNoCase(kFirefoxExeName) == 0) {
  151. default_type = BROWSER_FIREFOX;
  152. } else if (browser.CompareNoCase(kChromeExeName) == 0) {
  153. default_type = BROWSER_CHROME;
  154. }
  155. CString exp_browser_path;
  156. ASSERT_SUCCEEDED(GetDefaultBrowserPath(&exp_browser_path));
  157. if (default_type == BROWSER_IE) {
  158. CString path;
  159. ASSERT_SUCCEEDED(GetBrowserImagePath(BROWSER_IE, &path));
  160. CString long_name;
  161. ASSERT_SUCCEEDED(ShortPathToLongPath(path, &long_name));
  162. CString exp_long_name;
  163. ASSERT_SUCCEEDED(ShortPathToLongPath(exp_browser_path, &exp_long_name));
  164. ASSERT_STREQ(exp_long_name.MakeLower(), long_name.MakeLower());
  165. } else if (default_type == BROWSER_FIREFOX) {
  166. CString path;
  167. ASSERT_SUCCEEDED(GetBrowserImagePath(BROWSER_FIREFOX, &path));
  168. CString long_name;
  169. ASSERT_SUCCEEDED(ShortPathToLongPath(path, &long_name));
  170. CString exp_long_name;
  171. ASSERT_SUCCEEDED(ShortPathToLongPath(exp_browser_path, &exp_long_name));
  172. ASSERT_STREQ(exp_long_name.MakeLower(), long_name.MakeLower());
  173. } else if (default_type == BROWSER_CHROME) {
  174. CString path;
  175. ASSERT_SUCCEEDED(GetBrowserImagePath(BROWSER_CHROME, &path));
  176. CString long_name;
  177. ASSERT_SUCCEEDED(ShortPathToLongPath(path, &long_name));
  178. CString exp_long_name;
  179. ASSERT_SUCCEEDED(ShortPathToLongPath(exp_browser_path, &exp_long_name));
  180. ASSERT_STREQ(exp_long_name.MakeLower(), long_name.MakeLower());
  181. }
  182. CString path;
  183. ASSERT_SUCCEEDED(GetBrowserImagePath(BROWSER_DEFAULT, &path));
  184. CString long_name;
  185. ASSERT_SUCCEEDED(ShortPathToLongPath(path, &long_name));
  186. CString exp_long_name;
  187. ASSERT_SUCCEEDED(ShortPathToLongPath(exp_browser_path, &exp_long_name));
  188. ASSERT_STREQ(exp_long_name.MakeLower(), long_name.MakeLower());
  189. ASSERT_FAILED(GetBrowserImagePath(BROWSER_UNKNOWN, &path));
  190. }
  191. // Try all browsers to get more test coverage.
  192. TEST(BrowserUtilsTest, GetBrowserImagePath_AllSupportedBrowsers) {
  193. CString path;
  194. HRESULT hr = GetBrowserImagePath(BROWSER_IE, &path);
  195. if (SUCCEEDED(hr)) {
  196. EXPECT_EQ(0, path.CompareNoCase(
  197. _T("C:\\Program Files\\Internet Explorer\\iexplore.exe")))
  198. << _T("Actual path: ") << path.GetString();
  199. } else {
  200. EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), hr);
  201. }
  202. hr = GetBrowserImagePath(BROWSER_FIREFOX, &path);
  203. if (SUCCEEDED(hr)) {
  204. EXPECT_TRUE(
  205. 0 == path.CompareNoCase(
  206. _T("C:\\Program Files\\Mozilla Firefox\\firefox.exe")) ||
  207. 0 == path.CompareNoCase(
  208. _T("C:\\PROGRA~1\\MOZILL~1\\FIREFOX.EXE")) ||
  209. 0 == path.CompareNoCase(
  210. _T("C:\\Program Files\\Minefield\\FIREFOX.EXE"))) // Trunk build.
  211. << _T("Actual path: ") << path.GetString();
  212. } else {
  213. EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), hr);
  214. }
  215. hr = GetBrowserImagePath(BROWSER_CHROME, &path);
  216. if (SUCCEEDED(hr)) {
  217. EXPECT_TRUE(
  218. 0 == path.CompareNoCase(
  219. _T("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe")) ||
  220. 0 == path.CompareNoCase(
  221. GetLocalAppDataPath() +
  222. _T("Google\\Chrome\\Application\\chrome.exe")))
  223. << _T("Actual path: ") << path.GetString();
  224. } else {
  225. EXPECT_EQ(HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), hr);
  226. }
  227. }
  228. TEST(BrowserUtilsTest, GetLegacyDefaultBrowserInfo) {
  229. CString name;
  230. CString browser_path;
  231. EXPECT_SUCCEEDED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  232. EXPECT_FALSE(browser_path.IsEmpty());
  233. EXPECT_FALSE(name.IsEmpty());
  234. }
  235. TEST(BrowserUtilsTest, GetIeFontSize) {
  236. // The build server might not have IE configured.
  237. if (!IsBuildSystem()) {
  238. uint32 font_size = 0;
  239. const uint32 kMaxFontSize = 4;
  240. EXPECT_SUCCEEDED(GetIeFontSize(&font_size));
  241. EXPECT_LE(font_size, kMaxFontSize);
  242. }
  243. }
  244. TEST_F(GetLegacyDefaultBrowserInfoTest, ValidQuotedIE) {
  245. EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(
  246. kRegKeyLegacyDefaultBrowserCommand,
  247. NULL,
  248. _T("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" -nohome")));
  249. CString name;
  250. CString browser_path;
  251. EXPECT_SUCCEEDED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  252. EXPECT_STREQ(_T("C:\\Program Files\\Internet Explorer\\iexplore.exe"),
  253. browser_path);
  254. EXPECT_STREQ(_T("iexplore.exe"), name);
  255. }
  256. TEST_F(GetLegacyDefaultBrowserInfoTest, ValidUnquotedPath) {
  257. EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(
  258. kRegKeyLegacyDefaultBrowserCommand,
  259. NULL,
  260. _T("C:\\Program Files\\Internet Explorer\\iexplore.exe")));
  261. CString name;
  262. CString browser_path;
  263. EXPECT_SUCCEEDED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  264. EXPECT_STREQ(_T("C:\\Program Files\\Internet Explorer\\iexplore.exe"),
  265. browser_path);
  266. EXPECT_STREQ(_T("iexplore.exe"), name);
  267. }
  268. TEST_F(GetLegacyDefaultBrowserInfoTest, InvalidNoKey) {
  269. CString name;
  270. CString browser_path;
  271. EXPECT_FAILED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  272. EXPECT_TRUE(browser_path.IsEmpty());
  273. EXPECT_TRUE(name.IsEmpty());
  274. }
  275. TEST_F(GetLegacyDefaultBrowserInfoTest, InvalidNoValue) {
  276. EXPECT_HRESULT_SUCCEEDED(RegKey::CreateKey(
  277. kRegKeyLegacyDefaultBrowserCommand));
  278. CString name;
  279. CString browser_path;
  280. EXPECT_FAILED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  281. EXPECT_TRUE(browser_path.IsEmpty());
  282. EXPECT_TRUE(name.IsEmpty());
  283. }
  284. TEST_F(GetLegacyDefaultBrowserInfoTest, InvalidPath) {
  285. EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(
  286. kRegKeyLegacyDefaultBrowserCommand,
  287. NULL,
  288. _T("\"C:\\Program File\\iexplore.exe\" -nohome")));
  289. CString name;
  290. CString browser_path;
  291. EXPECT_FAILED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  292. EXPECT_TRUE(browser_path.IsEmpty());
  293. EXPECT_TRUE(name.IsEmpty());
  294. }
  295. TEST_F(GetLegacyDefaultBrowserInfoTest, InvalidUnquotedPath) {
  296. EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(
  297. kRegKeyLegacyDefaultBrowserCommand,
  298. NULL,
  299. _T("C:\\Program Files\\Internet Explorer\\iexplore.exe -nohome")));
  300. CString name;
  301. CString browser_path;
  302. EXPECT_FAILED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  303. EXPECT_TRUE(browser_path.IsEmpty());
  304. EXPECT_TRUE(name.IsEmpty());
  305. }
  306. TEST_F(GetLegacyDefaultBrowserInfoTest, InvalidUnquotedDirectory) {
  307. EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(
  308. kRegKeyLegacyDefaultBrowserCommand,
  309. NULL,
  310. _T("C:\\Program Files\\Internet Explorer\\")));
  311. CString name;
  312. CString browser_path;
  313. EXPECT_FAILED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  314. EXPECT_TRUE(browser_path.IsEmpty());
  315. EXPECT_TRUE(name.IsEmpty());
  316. }
  317. TEST_F(GetLegacyDefaultBrowserInfoTest, InvalidQuotedDirectory) {
  318. EXPECT_HRESULT_SUCCEEDED(RegKey::SetValue(
  319. kRegKeyLegacyDefaultBrowserCommand,
  320. NULL,
  321. _T("\"C:\\Program Files\\Internet Explorer\\\" -nohome")));
  322. CString name;
  323. CString browser_path;
  324. EXPECT_FAILED(GetLegacyDefaultBrowserInfo(&name, &browser_path));
  325. EXPECT_TRUE(browser_path.IsEmpty());
  326. EXPECT_TRUE(name.IsEmpty());
  327. }
  328. } // namespace omaha