Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9832)

Unified Diff: chrome/installer/setup/setup_singleton_unittest.cc

Issue 2292293002: Add installer::SetupSingleton. (Closed)
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/installer/setup/setup_singleton_unittest.cc
diff --git a/chrome/installer/setup/setup_singleton_unittest.cc b/chrome/installer/setup/setup_singleton_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..24abaa86de97d033f93c4db429c82b0216f77399
--- /dev/null
+++ b/chrome/installer/setup/setup_singleton_unittest.cc
@@ -0,0 +1,204 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/installer/setup/setup_singleton.h"
+
+#include <Windows.h>
grt (UTC plus 2) 2016/08/31 12:03:48 nit: 'w'
fdoray 2016/08/31 19:14:21 Done.
grt (UTC plus 2) 2016/09/01 12:05:39 try harder. :-)
+
+#include <string>
+#include <vector>
+
+#include "base/base_switches.h"
+#include "base/command_line.h"
+#include "base/files/file.h"
+#include "base/files/file_path.h"
+#include "base/files/file_util.h"
+#include "base/files/scoped_temp_dir.h"
+#include "base/process/launch.h"
+#include "base/strings/string16.h"
+#include "base/threading/platform_thread.h"
+#include "base/time/time.h"
+#include "chrome/installer/util/installer_state.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/multiprocess_func_list.h"
+
+namespace installer {
+
+namespace {
+
+constexpr char kInstallDirSwitch[] = "install-dir";
+constexpr char kSystemLevelSwitch[] = "system-level";
+constexpr base::char16 kSentinelFileName[] = L"sentinel.txt";
+constexpr base::char16 kTestProcessReadyEventName[] =
+ L"Local\\ChromeSetupSingletonTestProcessReady";
+constexpr int kWaitDurationMs = 50;
grt (UTC plus 2) 2016/08/31 12:03:48 can you get rid of this and use something in base/
fdoray 2016/08/31 19:14:22 Done.
+
+enum ErrorCodes {
+ SUCCESS,
+ SENTINEL_FILE_CREATE_ERROR,
+ SENTINEL_FILE_DELETE_ERROR,
+ WAIT_RETURNED_FALSE,
+};
+
+base::string16 ReplaceBackslashes(const base::string16& path) {
+ base::string16 path_without_backslashes(path);
+ std::replace(path_without_backslashes.begin(), path_without_backslashes.end(),
grt (UTC plus 2) 2016/08/31 12:03:48 #include <algorithm>
fdoray 2016/08/31 19:14:21 N/A
+ L'\\', L'/');
+ return path_without_backslashes;
+}
+
+int CreateAndDeleteSentinelFile(const base::FilePath& install_dir) {
+ const base::FilePath sentinel_file_path =
+ install_dir.Append(kSentinelFileName);
+
+ base::File file(sentinel_file_path,
grt (UTC plus 2) 2016/08/31 12:03:48 nit: no need to keep this in a local var. you can
fdoray 2016/08/31 19:14:21 Done.
+ base::File::FLAG_CREATE | base::File::FLAG_WRITE);
+ if (!file.IsValid())
+ return SENTINEL_FILE_CREATE_ERROR;
+ file.Close();
+
+ base::PlatformThread::Sleep(
+ base::TimeDelta::FromMilliseconds(kWaitDurationMs));
+
+ if (!base::DeleteFile(sentinel_file_path, false))
+ return SENTINEL_FILE_DELETE_ERROR;
+
+ return SUCCESS;
+}
+
+MULTIPROCESS_TEST_MAIN(SetupSingletonTestExclusiveAccessProcessMain) {
+ base::CommandLine* const command_line =
+ base::CommandLine::ForCurrentProcess();
+
+ // Acquire the exclusive right to modify the Chrome installation.
+ const base::FilePath install_dir =
+ command_line->GetSwitchValuePath(kInstallDirSwitch);
+ SetupSingleton setup_singleton(install_dir,
+ command_line->HasSwitch(kSystemLevelSwitch));
+
+ // Create a sentinel file and delete it after a few milliseconds. This will
+ // fail it the sentinel file already exists (which shouldn't be the case since
grt (UTC plus 2) 2016/08/31 12:03:48 it -> if
fdoray 2016/08/31 19:14:21 Done.
+ // we are in the scope of a SetupSingleton).
+ return CreateAndDeleteSentinelFile(install_dir);
+}
+
+MULTIPROCESS_TEST_MAIN(SetupSingletonTestWaitOtherSetupSingletonProcessMain) {
+ base::CommandLine* const command_line =
+ base::CommandLine::ForCurrentProcess();
+
+ // Acquire the exclusive right to modify the Chrome installation.
+ const base::FilePath install_dir =
+ command_line->GetSwitchValuePath(kInstallDirSwitch);
+ SetupSingleton setup_singleton(install_dir,
+ command_line->HasSwitch(kSystemLevelSwitch));
+
+ // Signal an event to indicate that this process has acquired the
+ // SetupSingleton.
+ base::WaitableEvent ready_event(base::win::ScopedHandle(::CreateEvent(
+ nullptr, FALSE, FALSE,
+ (kTestProcessReadyEventName + ReplaceBackslashes(install_dir.value()))
+ .c_str())));
+ ready_event.Signal();
+
+ // Wait indefinitely. This should only return when another SetupSingleton is
+ // instantiated for |install_dir|.
+ if (!setup_singleton.Wait(base::TimeDelta::Max()))
+ return WAIT_RETURNED_FALSE;
+
+ // Create a sentinel file and delete it after a few milliseconds. This will
+ // fail it the sentinel file already exists (which shouldn't be the case since
+ // we are in the scope of a SetupSingleton).
+ return CreateAndDeleteSentinelFile(install_dir);
+}
+
+class SetupSingletonTest
+ : public testing::TestWithParam<InstallerState::Level> {
+ public:
+ SetupSingletonTest() = default;
+
+ void SetUp() override { ASSERT_TRUE(install_dir_.CreateUniqueTempDir()); }
+
+ base::Process SpawnChildProcess(const std::string& process_name) {
+ base::CommandLine command_line = *base::CommandLine::ForCurrentProcess();
+ command_line.SetProgram(
+ base::MakeAbsoluteFilePath(command_line.GetProgram()));
+ command_line.AppendSwitchASCII(::switches::kTestChildProcess, process_name);
+ command_line.AppendSwitchPath(kInstallDirSwitch, install_dir_.path());
+ if (GetParam() == InstallerState::SYSTEM_LEVEL)
+ command_line.AppendSwitch(kSystemLevelSwitch);
+ base::LaunchOptions options;
+ options.start_hidden = true;
+ return LaunchProcess(command_line, options);
+ }
+
+ const base::FilePath& install_dir_path() const { return install_dir_.path(); }
+
+ private:
+ base::ScopedTempDir install_dir_;
+
+ DISALLOW_COPY_AND_ASSIGN(SetupSingletonTest);
+};
+
+} // namespace
+
+INSTANTIATE_TEST_CASE_P(InstallLevel,
+ SetupSingletonTest,
+ ::testing::Values(InstallerState::SYSTEM_LEVEL,
grt (UTC plus 2) 2016/08/31 12:03:48 i find that the logs (especially failures) are eas
fdoray 2016/08/31 19:14:21 N/A
+ InstallerState::USER_LEVEL));
+
+// Verify that a single SetupSingleton can be active at a time for a given
+// Chrome installation.
+TEST_P(SetupSingletonTest, ExclusiveAccess) {
+ constexpr size_t kNumProcesses = 10;
grt (UTC plus 2) 2016/08/31 12:03:48 nit: use int for this
fdoray 2016/08/31 19:14:21 Done.
+
+ std::vector<base::Process> processes;
+ for (size_t i = 0; i < kNumProcesses; ++i) {
+ processes.push_back(
+ SpawnChildProcess("SetupSingletonTestExclusiveAccessProcessMain"));
+ }
+
+ for (base::Process& process : processes) {
+ int exit_code = 0;
+ EXPECT_TRUE(process.WaitForExit(&exit_code));
+ EXPECT_EQ(SUCCESS, exit_code);
+ }
+}
+
+// Verify that Wait() returns false when its delay expires.
+TEST_P(SetupSingletonTest, WaitNoOtherSetupSingleton) {
+ SetupSingleton setup_singleton(install_dir_path(),
+ GetParam() == InstallerState::SYSTEM_LEVEL);
+ EXPECT_FALSE(
+ setup_singleton.Wait(base::TimeDelta::FromMilliseconds(kWaitDurationMs)));
+}
+
+// Verify that Wait() returns true immediately when another SetupSingleton is
+// instantiated.
+TEST_P(SetupSingletonTest, WaitOtherSetupSingleton) {
+ base::Process wait_process =
+ SpawnChildProcess("SetupSingletonTestWaitOtherSetupSingletonProcessMain");
+
+ // Wait until the other process acquires the SetupSingleton.
+ base::WaitableEvent ready_event(base::win::ScopedHandle(::CreateEvent(
+ nullptr, FALSE, FALSE, (kTestProcessReadyEventName +
+ ReplaceBackslashes(install_dir_path().value()))
+ .c_str())));
+ ready_event.Wait();
+
+ // Acquire the SetupSingleton.
+ SetupSingleton setup_singleton(install_dir_path(),
+ GetParam() == InstallerState::SYSTEM_LEVEL);
+
+ // Create a sentinel file and delete it after a few milliseconds. This will
+ // fail it the sentinel file already exists (which shouldn't be the case since
+ // we are in the scope of a SetupSingleton).
+ EXPECT_EQ(SUCCESS, CreateAndDeleteSentinelFile(install_dir_path()));
+
+ // Join |wait_process|.
+ int exit_code = 0;
+ EXPECT_TRUE(wait_process.WaitForExit(&exit_code));
+ EXPECT_EQ(SUCCESS, exit_code);
+}
+
+} // namespace installer
« chrome/installer/setup/setup_singleton.cc ('K') | « chrome/installer/setup/setup_singleton.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698