This source file includes following definitions.
- SetUpTestCase
 
- TearDownTestCase
 
- SetUp
 
- TearDown
 
- MinimallyInstallProduct
 
- ApplyUninstallCommand
 
- TEST_F
 
- TEST_F
 
- TEST_F
 
- TEST_F
 
- TEST_F
 
- TEST_F
 
- TEST_F
 
#include <windows.h>
#include "base/strings/utf_string_conversions.h"
#include "base/test/test_reg_util_win.h"
#include "base/version.h"
#include "base/win/registry.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/installation_state.h"
#include "chrome/installer/util/product_unittest.h"
#include "chrome/installer/util/util_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::win::RegKey;
using installer::ProductState;
using registry_util::RegistryOverrideManager;
class ProductStateTest : public testing::Test {
 protected:
  static void SetUpTestCase();
  static void TearDownTestCase();
  virtual void SetUp();
  virtual void TearDown();
  void ApplyUninstallCommand(const wchar_t* exe_path, const wchar_t* args);
  void MinimallyInstallProduct(const wchar_t* version);
  static BrowserDistribution* dist_;
  bool system_install_;
  HKEY overridden_;
  registry_util::RegistryOverrideManager registry_override_manager_;
  RegKey clients_;
  RegKey client_state_;
};
BrowserDistribution* ProductStateTest::dist_;
void ProductStateTest::SetUpTestCase() {
  testing::Test::SetUpTestCase();
  
  dist_ = BrowserDistribution::GetSpecificDistribution(
      BrowserDistribution::CHROME_BROWSER);
}
void ProductStateTest::TearDownTestCase() {
  dist_ = NULL;
  testing::Test::TearDownTestCase();
}
void ProductStateTest::SetUp() {
  testing::Test::SetUp();
  
  system_install_ = true;
  overridden_ = (system_install_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER);
  
  
  RegKey temp_key;
  registry_override_manager_.OverrideRegistry(overridden_, L"ProductStateTest");
  EXPECT_EQ(ERROR_SUCCESS,
            clients_.Create(overridden_, dist_->GetVersionKey().c_str(),
                            KEY_ALL_ACCESS));
  EXPECT_EQ(ERROR_SUCCESS,
            client_state_.Create(overridden_, dist_->GetStateKey().c_str(),
                                 KEY_ALL_ACCESS));
}
void ProductStateTest::TearDown() {
  
  client_state_.Close();
  clients_.Close();
  overridden_ = NULL;
  system_install_ = false;
  testing::Test::TearDown();
}
void ProductStateTest::MinimallyInstallProduct(const wchar_t* version) {
  EXPECT_EQ(ERROR_SUCCESS,
            clients_.WriteValue(google_update::kRegVersionField, version));
}
void ProductStateTest::ApplyUninstallCommand(const wchar_t* exe_path,
                                             const wchar_t* args) {
  if (exe_path == NULL) {
    LONG result = client_state_.DeleteValue(installer::kUninstallStringField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
  } else {
    EXPECT_EQ(ERROR_SUCCESS,
              client_state_.WriteValue(installer::kUninstallStringField,
                                       exe_path));
  }
  if (args == NULL) {
    LONG result =
        client_state_.DeleteValue(installer::kUninstallArgumentsField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
  } else {
    EXPECT_EQ(ERROR_SUCCESS,
              client_state_.WriteValue(installer::kUninstallArgumentsField,
                                       args));
  }
}
TEST_F(ProductStateTest, InitializeInstalled) {
  
  {
    ProductState state;
    LONG result = clients_.DeleteValue(google_update::kRegVersionField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_FALSE(state.Initialize(system_install_, dist_));
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegVersionField, L"");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_FALSE(state.Initialize(system_install_, dist_));
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegVersionField,
                                      L"goofy");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_FALSE(state.Initialize(system_install_, dist_));
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegVersionField,
                                      L"10.0.47.0");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_EQ("10.0.47.0", state.version().GetString());
  }
}
TEST_F(ProductStateTest, InitializeOldVersion) {
  MinimallyInstallProduct(L"10.0.1.1");
  
  {
    ProductState state;
    LONG result = clients_.DeleteValue(google_update::kRegOldVersionField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.old_version() == NULL);
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegOldVersionField, L"");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.old_version() == NULL);
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegOldVersionField,
                                      L"coming home");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.old_version() == NULL);
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegOldVersionField,
                                      L"10.0.47.0");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.old_version() != NULL);
    EXPECT_EQ("10.0.47.0", state.old_version()->GetString());
  }
}
TEST_F(ProductStateTest, InitializeRenameCmd) {
  MinimallyInstallProduct(L"10.0.1.1");
  
  {
    ProductState state;
    LONG result = clients_.DeleteValue(google_update::kRegRenameCmdField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.rename_cmd().empty());
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegRenameCmdField, L"");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.rename_cmd().empty());
  }
  
  {
    ProductState state;
    LONG result = clients_.WriteValue(google_update::kRegRenameCmdField,
                                      L"spam.exe --spamalot");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_EQ(L"spam.exe --spamalot", state.rename_cmd());
  }
}
TEST_F(ProductStateTest, InitializeChannelInfo) {
  MinimallyInstallProduct(L"10.0.1.1");
  
  {
    ProductState state;
    LONG result = client_state_.DeleteValue(google_update::kRegApField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.channel().value().empty());
  }
  
  {
    ProductState state;
    LONG result = client_state_.WriteValue(google_update::kRegApField, L"");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.channel().value().empty());
  }
  
  {
    ProductState state;
    LONG result = client_state_.WriteValue(google_update::kRegApField, L"spam");
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_EQ(L"spam", state.channel().value());
  }
}
TEST_F(ProductStateTest, InitializeUninstallCommand) {
  MinimallyInstallProduct(L"10.0.1.1");
  
  {
    ProductState state;
    ApplyUninstallCommand(NULL, NULL);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.GetSetupPath().empty());
    EXPECT_TRUE(state.uninstall_command().GetCommandLineString().empty());
    EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(L"", L"");
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.GetSetupPath().empty());
    EXPECT_TRUE(state.uninstall_command().GetCommandLineString().empty());
    EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(NULL, L"--uninstall");
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.GetSetupPath().empty());
    EXPECT_EQ(L" --uninstall",
              state.uninstall_command().GetCommandLineString());
    EXPECT_EQ(1U, state.uninstall_command().GetSwitches().size());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(L"setup.exe", NULL);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_EQ(L"setup.exe", state.GetSetupPath().value());
    EXPECT_EQ(L"setup.exe", state.uninstall_command().GetCommandLineString());
    EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(L"set up.exe", NULL);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_EQ(L"set up.exe", state.GetSetupPath().value());
    EXPECT_EQ(L"\"set up.exe\"",
              state.uninstall_command().GetCommandLineString());
    EXPECT_TRUE(state.uninstall_command().GetSwitches().empty());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(L"setup.exe", L"--uninstall");
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_EQ(L"setup.exe", state.GetSetupPath().value());
    EXPECT_EQ(L"setup.exe --uninstall",
              state.uninstall_command().GetCommandLineString());
    EXPECT_EQ(1U, state.uninstall_command().GetSwitches().size());
  }
}
TEST_F(ProductStateTest, InitializeMsi) {
  MinimallyInstallProduct(L"10.0.1.1");
  
  {
    ProductState state;
    LONG result = client_state_.DeleteValue(google_update::kRegMSIField);
    EXPECT_TRUE(result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_FALSE(state.is_msi());
  }
  
  {
    ProductState state;
    EXPECT_EQ(ERROR_SUCCESS,
              client_state_.WriteValue(google_update::kRegMSIField,
                                       static_cast<DWORD>(0)));
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_FALSE(state.is_msi());
  }
  
  {
    ProductState state;
    EXPECT_EQ(ERROR_SUCCESS,
              client_state_.WriteValue(google_update::kRegMSIField,
                                       static_cast<DWORD>(1)));
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.is_msi());
  }
  
  {
    ProductState state;
    EXPECT_EQ(ERROR_SUCCESS,
              client_state_.WriteValue(google_update::kRegMSIField,
                                       static_cast<DWORD>(47)));
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.is_msi());
  }
  
  {
    ProductState state;
    EXPECT_EQ(ERROR_SUCCESS,
              client_state_.WriteValue(google_update::kRegMSIField,
                                       L"bogus!"));
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_FALSE(state.is_msi());
  }
}
TEST_F(ProductStateTest, InitializeMultiInstall) {
  MinimallyInstallProduct(L"10.0.1.1");
  
  {
    ProductState state;
    ApplyUninstallCommand(NULL, NULL);
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_FALSE(state.is_multi_install());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(L"setup.exe", L"--uninstall");
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_FALSE(state.is_multi_install());
  }
  
  {
    ProductState state;
    ApplyUninstallCommand(L"setup.exe",
                          L"--uninstall --chrome --multi-install");
    EXPECT_TRUE(state.Initialize(system_install_, dist_));
    EXPECT_TRUE(state.is_multi_install());
  }
}