| OLD | NEW | 
|    1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2012 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 #include <windows.h> |    5 #include <windows.h> | 
|    6 #include <shellapi.h> |    6 #include <shellapi.h> | 
|    7 #include <shlobj.h> |    7 #include <shlobj.h> | 
|    8  |    8  | 
|    9 #include <fstream> |    9 #include <fstream> | 
|   10 #include <vector> |   10 #include <vector> | 
|   11  |   11  | 
|   12 #include "base/file_util.h" |   12 #include "base/file_util.h" | 
|   13 #include "base/path_service.h" |   13 #include "base/path_service.h" | 
 |   14 #include "base/md5.h" | 
|   14 #include "base/scoped_temp_dir.h" |   15 #include "base/scoped_temp_dir.h" | 
|   15 #include "base/string16.h" |   16 #include "base/string16.h" | 
 |   17 #include "base/string_util.h" | 
|   16 #include "base/win/scoped_comptr.h" |   18 #include "base/win/scoped_comptr.h" | 
|   17 #include "base/win/windows_version.h" |   19 #include "base/win/windows_version.h" | 
|   18 #include "chrome/installer/util/browser_distribution.h" |   20 #include "chrome/installer/util/browser_distribution.h" | 
|   19 #include "chrome/installer/util/master_preferences.h" |   21 #include "chrome/installer/util/master_preferences.h" | 
|   20 #include "chrome/installer/util/shell_util.h" |   22 #include "chrome/installer/util/shell_util.h" | 
|   21 #include "chrome/installer/util/util_constants.h" |   23 #include "chrome/installer/util/util_constants.h" | 
|   22 #include "testing/gtest/include/gtest/gtest.h" |   24 #include "testing/gtest/include/gtest/gtest.h" | 
|   23  |   25  | 
|   24 namespace { |   26 namespace { | 
|   25 bool VerifyChromeShortcut(const std::wstring& exe_path, |   27 bool VerifyChromeShortcut(const std::wstring& exe_path, | 
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   81             ((FilePath(file_path) != FilePath(long_path)) && |   83             ((FilePath(file_path) != FilePath(long_path)) && | 
|   82              (FilePath(file_path) != FilePath(short_path))) || |   84              (FilePath(file_path) != FilePath(short_path))) || | 
|   83             (index != icon_index)); |   85             (index != icon_index)); | 
|   84   EXPECT_FALSE(failed); |   86   EXPECT_FALSE(failed); | 
|   85   if (failed) |   87   if (failed) | 
|   86     return false; |   88     return false; | 
|   87  |   89  | 
|   88   return true; |   90   return true; | 
|   89 } |   91 } | 
|   90  |   92  | 
|   91 class ShellUtilTest : public testing::Test { |   93 class ShellUtilTestWithDirAndDist : public testing::Test { | 
|   92  protected: |   94  protected: | 
|   93   virtual void SetUp() { |   95   virtual void SetUp() { | 
|   94     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |   96     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 
|   95     dist_ = BrowserDistribution::GetDistribution(); |   97     dist_ = BrowserDistribution::GetDistribution(); | 
|   96     ASSERT_TRUE(dist_ != NULL); |   98     ASSERT_TRUE(dist_ != NULL); | 
|   97   } |   99   } | 
|   98  |  100  | 
|   99   BrowserDistribution* dist_; |  101   BrowserDistribution* dist_; | 
|  100  |  102  | 
|  101   ScopedTempDir temp_dir_; |  103   ScopedTempDir temp_dir_; | 
|  102 }; |  104 }; | 
|  103 }; |  105 }; | 
|  104  |  106  | 
|  105 // Test that we can open archives successfully. |  107 // Test that we can open archives successfully. | 
|  106 TEST_F(ShellUtilTest, UpdateChromeShortcutTest) { |  108 TEST_F(ShellUtilTestWithDirAndDist, UpdateChromeShortcutTest) { | 
|  107   // Create an executable in test path by copying ourself to it. |  109   // Create an executable in test path by copying ourself to it. | 
|  108   wchar_t exe_full_path_str[MAX_PATH]; |  110   wchar_t exe_full_path_str[MAX_PATH]; | 
|  109   EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); |  111   EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); | 
|  110   FilePath exe_full_path(exe_full_path_str); |  112   FilePath exe_full_path(exe_full_path_str); | 
|  111  |  113  | 
|  112   FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); |  114   FilePath exe_path = temp_dir_.path().AppendASCII("setup.exe"); | 
|  113   EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); |  115   EXPECT_TRUE(file_util::CopyFile(exe_full_path, exe_path)); | 
|  114  |  116  | 
|  115   FilePath shortcut_path = temp_dir_.path().AppendASCII("shortcut.lnk"); |  117   FilePath shortcut_path = temp_dir_.path().AppendASCII("shortcut.lnk"); | 
|  116   const std::wstring description(L"dummy description"); |  118   const std::wstring description(L"dummy description"); | 
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  163                                               L"", |  165                                               L"", | 
|  164                                               description2, |  166                                               description2, | 
|  165                                               exe_path.value(), |  167                                               exe_path.value(), | 
|  166                                               dist_->GetIconIndex(), |  168                                               dist_->GetIconIndex(), | 
|  167                                               ShellUtil::SHORTCUT_NO_OPTIONS)); |  169                                               ShellUtil::SHORTCUT_NO_OPTIONS)); | 
|  168   EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(), |  170   EXPECT_TRUE(VerifyChromeShortcut(exe_path.value(), | 
|  169                                    shortcut_path.value(), |  171                                    shortcut_path.value(), | 
|  170                                    description2, 1)); |  172                                    description2, 1)); | 
|  171 } |  173 } | 
|  172  |  174  | 
|  173 TEST_F(ShellUtilTest, CreateChromeDesktopShortcutTest) { |  175 TEST_F(ShellUtilTestWithDirAndDist, CreateChromeDesktopShortcutTest) { | 
|  174   // Run this test on Vista+ only if we are running elevated. |  176   // Run this test on Vista+ only if we are running elevated. | 
|  175   if (base::win::GetVersion() > base::win::VERSION_XP && !IsUserAnAdmin()) { |  177   if (base::win::GetVersion() > base::win::VERSION_XP && !IsUserAnAdmin()) { | 
|  176     LOG(ERROR) << "Must be admin to run this test on Vista+"; |  178     LOG(ERROR) << "Must be admin to run this test on Vista+"; | 
|  177     return; |  179     return; | 
|  178   } |  180   } | 
|  179  |  181  | 
|  180   // Create an executable in test path by copying ourself to it. |  182   // Create an executable in test path by copying ourself to it. | 
|  181   wchar_t exe_full_path_str[MAX_PATH]; |  183   wchar_t exe_full_path_str[MAX_PATH]; | 
|  182   EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); |  184   EXPECT_FALSE(::GetModuleFileName(NULL, exe_full_path_str, MAX_PATH) == 0); | 
|  183   FilePath exe_full_path(exe_full_path_str); |  185   FilePath exe_full_path(exe_full_path_str); | 
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  356                                    second_profile_shortcut_path.value(), |  358                                    second_profile_shortcut_path.value(), | 
|  357                                    description, |  359                                    description, | 
|  358                                    0)); |  360                                    0)); | 
|  359   std::vector<string16> profile_names; |  361   std::vector<string16> profile_names; | 
|  360   profile_names.push_back(default_profile_shortcut_name); |  362   profile_names.push_back(default_profile_shortcut_name); | 
|  361   profile_names.push_back(second_profile_shortcut_name); |  363   profile_names.push_back(second_profile_shortcut_name); | 
|  362   EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames( |  364   EXPECT_TRUE(ShellUtil::RemoveChromeDesktopShortcutsWithAppendedNames( | 
|  363       profile_names)); |  365       profile_names)); | 
|  364 } |  366 } | 
|  365  |  367  | 
|  366 TEST_F(ShellUtilTest, BuildAppModelIdBasic) { |  368 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdBasic) { | 
|  367   std::vector<string16> components; |  369   std::vector<string16> components; | 
|  368   const string16 base_app_id(dist_->GetBaseAppId()); |  370   const string16 base_app_id(dist_->GetBaseAppId()); | 
|  369   components.push_back(base_app_id); |  371   components.push_back(base_app_id); | 
|  370   ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); |  372   ASSERT_EQ(base_app_id, ShellUtil::BuildAppModelId(components)); | 
|  371 } |  373 } | 
|  372  |  374  | 
|  373 TEST_F(ShellUtilTest, BuildAppModelIdManySmall) { |  375 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdManySmall) { | 
|  374   std::vector<string16> components; |  376   std::vector<string16> components; | 
|  375   const string16 suffixed_app_id(dist_->GetBaseAppId().append(L".gab")); |  377   const string16 suffixed_app_id(dist_->GetBaseAppId().append(L".gab")); | 
|  376   components.push_back(suffixed_app_id); |  378   components.push_back(suffixed_app_id); | 
|  377   components.push_back(L"Default"); |  379   components.push_back(L"Default"); | 
|  378   components.push_back(L"Test"); |  380   components.push_back(L"Test"); | 
|  379   ASSERT_EQ(suffixed_app_id + L".Default.Test", |  381   ASSERT_EQ(suffixed_app_id + L".Default.Test", | 
|  380             ShellUtil::BuildAppModelId(components)); |  382             ShellUtil::BuildAppModelId(components)); | 
|  381 } |  383 } | 
|  382  |  384  | 
|  383 TEST_F(ShellUtilTest, BuildAppModelIdLongUsernameNormalProfile) { |  385 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdLongUsernameNormalProfile) { | 
|  384   std::vector<string16> components; |  386   std::vector<string16> components; | 
|  385   const string16 long_appname( |  387   const string16 long_appname( | 
|  386       L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" |  388       L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" | 
|  387       L"that_goes_over_64_characters"); |  389       L"that_goes_over_64_characters"); | 
|  388   components.push_back(long_appname); |  390   components.push_back(long_appname); | 
|  389   components.push_back(L"Default"); |  391   components.push_back(L"Default"); | 
|  390   ASSERT_EQ(L"Chrome.a_user_wer_64_characters.Default", |  392   ASSERT_EQ(L"Chrome.a_user_wer_64_characters.Default", | 
|  391             ShellUtil::BuildAppModelId(components)); |  393             ShellUtil::BuildAppModelId(components)); | 
|  392 } |  394 } | 
|  393  |  395  | 
|  394 TEST_F(ShellUtilTest, BuildAppModelIdLongEverything) { |  396 TEST_F(ShellUtilTestWithDirAndDist, BuildAppModelIdLongEverything) { | 
|  395   std::vector<string16> components; |  397   std::vector<string16> components; | 
|  396   const string16 long_appname( |  398   const string16 long_appname( | 
|  397       L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" |  399       L"Chrome.a_user_who_has_a_crazy_long_name_with_some_weird@symbols_in_it_" | 
|  398       L"that_goes_over_64_characters"); |  400       L"that_goes_over_64_characters"); | 
|  399   components.push_back(long_appname); |  401   components.push_back(long_appname); | 
|  400   components.push_back( |  402   components.push_back( | 
|  401       L"A_crazy_profile_name_not_even_sure_whether_that_is_possible"); |  403       L"A_crazy_profile_name_not_even_sure_whether_that_is_possible"); | 
|  402   const string16 constructed_app_id(ShellUtil::BuildAppModelId(components)); |  404   const string16 constructed_app_id(ShellUtil::BuildAppModelId(components)); | 
|  403   ASSERT_LE(constructed_app_id.length(), installer::kMaxAppModelIdLength); |  405   ASSERT_LE(constructed_app_id.length(), installer::kMaxAppModelIdLength); | 
|  404   ASSERT_EQ(L"Chrome.a_user_wer_64_characters.A_crazy_profilethat_is_possible", |  406   ASSERT_EQ(L"Chrome.a_user_wer_64_characters.A_crazy_profilethat_is_possible", | 
|  405             constructed_app_id); |  407             constructed_app_id); | 
|  406 } |  408 } | 
 |  409  | 
 |  410 TEST(ShellUtilTest, GetUserSpecificRegistrySuffix) { | 
 |  411   string16 suffix; | 
 |  412   ASSERT_TRUE(ShellUtil::GetUserSpecificRegistrySuffix(&suffix)); | 
 |  413   ASSERT_TRUE(StartsWith(suffix, L".", true)); | 
 |  414   ASSERT_EQ(27, suffix.length()); | 
 |  415   ASSERT_TRUE(ContainsOnlyChars(suffix.substr(1), | 
 |  416                                 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567")); | 
 |  417 } | 
 |  418  | 
 |  419 TEST(ShellUtilTest, GetOldUserSpecificRegistrySuffix) { | 
 |  420   string16 suffix; | 
 |  421   ASSERT_TRUE(ShellUtil::GetOldUserSpecificRegistrySuffix(&suffix)); | 
 |  422   ASSERT_TRUE(StartsWith(suffix, L".", true)); | 
 |  423  | 
 |  424   wchar_t user_name[256]; | 
 |  425   DWORD size = arraysize(user_name); | 
 |  426   ASSERT_NE(0, ::GetUserName(user_name, &size)); | 
 |  427   ASSERT_GE(size, 1U); | 
 |  428   ASSERT_STREQ(user_name, suffix.substr(1).c_str()); | 
 |  429 } | 
 |  430  | 
 |  431 TEST(ShellUtilTest, ByteArrayToBase32) { | 
 |  432   // Tests from http://tools.ietf.org/html/rfc4648#section-10. | 
 |  433   const unsigned char test_array[] = { 'f', 'o', 'o', 'b', 'a', 'r' }; | 
 |  434  | 
 |  435   const string16 expected[] = { L"", L"MY", L"MZXQ", L"MZXW6", L"MZXW6YQ", | 
 |  436                                 L"MZXW6YTB", L"MZXW6YTBOI"}; | 
 |  437  | 
 |  438   // Run the tests, with one more letter in the input every pass. | 
 |  439   for (int i = 0; i < arraysize(expected); ++i) { | 
 |  440     ASSERT_EQ(expected[i], | 
 |  441               ShellUtil::ByteArrayToBase32(test_array, i)); | 
 |  442   } | 
 |  443 } | 
| OLD | NEW |