Chromium Code Reviews| Index: remoting/host/linux/certificate_watcher_unittest.cc |
| diff --git a/remoting/host/linux/certificate_watcher_unittest.cc b/remoting/host/linux/certificate_watcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9fc438645de15ad421e3abf206b9795a2113e918 |
| --- /dev/null |
| +++ b/remoting/host/linux/certificate_watcher_unittest.cc |
| @@ -0,0 +1,122 @@ |
| +// 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 "remoting/host/linux/certificate_watcher.h" |
| + |
| +#include <cstring> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "remoting/base/auto_thread.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace remoting { |
| +const char kWatchFileName[] = "testfile.txt"; |
|
Sergey Ulanov
2016/03/31 22:36:16
nit: add empty line.
Yuwei
2016/04/01 23:41:03
Done.
|
| +const char kTestWriteString[] = "TEST STRING"; |
| + |
| +class CertificateWatcherTest : public testing::Test { |
| + public: |
| + CertificateWatcherTest() |
| + : task_runner_(message_loop_.task_runner()), |
| + watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)), |
| + watcher_(base::WeakPtr<HostStatusMonitor>(), |
| + base::Bind(&CertificateWatcherTest::OnRestart, |
| + base::Unretained(this))) { |
| + watcher_.SetDelay(base::TimeDelta::FromSeconds(0)); |
| + watcher_.SetRestartDeferredAction(base::Bind( |
| + &CertificateWatcherTest::OnDeferred, |
| + base::Unretained(this))); |
| + watcher_.SetWatchPath(watch_path_); |
| + } |
| + |
| + protected: |
| + void Start() { |
| + if (!task_runner_->BelongsToCurrentThread()) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&CertificateWatcherTest::Start, |
| + base::Unretained(this))); |
| + return; |
| + } |
| + |
| + watcher_.Start(); |
| + } |
| + |
| + void RunLoop() { message_loop_.Run(); } |
| + |
| + void TouchFile() { |
| + if (!task_runner_->BelongsToCurrentThread()) { |
| + task_runner_->PostTask(FROM_HERE, |
| + base::Bind(&CertificateWatcherTest::TouchFile, |
| + base::Unretained(this))); |
| + return; |
| + } |
| + |
| + base::WriteFile(watch_path_, |
| + kTestWriteString, |
| + strlen(kTestWriteString)); |
| + } |
| + |
| + base::MessageLoopForIO message_loop_; |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + base::ScopedTempDir temp_dir_; |
| + base::FilePath watch_path_; |
| + CertificateWatcher watcher_; |
| + int restart_count_ = 0; |
| + |
| + private: |
| + const base::FilePath& CreateAndGetUniqueTempDir() { |
| + EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + return temp_dir_.path(); |
| + } |
| + |
| + void OnRestart() { |
| + EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| + restart_count_++; |
| + message_loop_.QuitNow(); |
| + } |
| + |
| + void OnDeferred() { |
| + // Simply quit the loop to so that the test can continue |
| + EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| + message_loop_.QuitNow(); |
| + } |
| +}; |
| + |
| +TEST_F(CertificateWatcherTest, OneTouch) { |
| + EXPECT_EQ(0, restart_count_); |
| + Start(); |
| + TouchFile(); |
| + RunLoop(); |
| + EXPECT_EQ(1, restart_count_); |
| +} |
| + |
| +TEST_F(CertificateWatcherTest, InhibitDeferRestart) { |
| + Start(); |
| + EXPECT_EQ(0, restart_count_); |
| + watcher_.SetInhibit(true); |
|
Sergey Ulanov
2016/03/31 22:36:16
Call OnClientConnected() and OnClientDisconnected(
Yuwei
2016/04/01 23:41:03
Done.
|
| + EXPECT_EQ(0, restart_count_); |
| + TouchFile(); |
| + RunLoop(); |
| + EXPECT_EQ(0, restart_count_); |
| + watcher_.SetInhibit(false); |
| + RunLoop(); |
| + EXPECT_EQ(1, restart_count_); |
| +} |
| + |
| +TEST_F(CertificateWatcherTest, UninhibitAndRestart) { |
| + Start(); |
| + EXPECT_EQ(0, restart_count_); |
| + watcher_.SetInhibit(true); |
| + EXPECT_EQ(0, restart_count_); |
| + watcher_.SetInhibit(false); |
| + EXPECT_EQ(0, restart_count_); |
| + TouchFile(); |
| + RunLoop(); |
| + EXPECT_EQ(1, restart_count_); |
| +} |
| + |
| +} // namespace remoting |