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..571862799d1b26eb8552345b1ea6c908efa6475e |
| --- /dev/null |
| +++ b/remoting/host/linux/certificate_watcher_unittest.cc |
| @@ -0,0 +1,146 @@ |
| +// 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 <cstdlib> |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/files/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "remoting/base/auto_thread.h" |
|
Sergey Ulanov
2016/04/06 21:04:06
Don't need this.
Yuwei
2016/04/07 00:35:42
removed.
|
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace remoting { |
| + |
| +const char kWatchFileName[] = "testfile.txt"; |
| + |
| +const int kMessageLoopWaitMsecs = 150; |
| + |
| +class CertificateWatcherTest : public testing::Test { |
| + public: |
| + CertificateWatcherTest() |
| + : task_runner_(message_loop_.task_runner()), |
| + watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)) { |
| + watcher_ = new CertificateWatcher( |
| + base::Bind(&CertificateWatcherTest::OnRestart, |
| + base::Unretained(this)), |
| + task_runner_); |
| + watcher_->SetDelayForTests(base::TimeDelta::FromSeconds(0)); |
| + watcher_->SetWatchPathForTests(watch_path_); |
| + } |
| + |
| + ~CertificateWatcherTest() override { |
| + delete watcher_; |
| + RunAndWait(); |
|
Sergey Ulanov
2016/04/06 21:04:06
You can just call RunUntilIdle() here - that will
Yuwei
2016/04/07 00:35:42
Done.
|
| + } |
| + |
| + protected: |
| + // Call this if you expect a restart after the loop runs. |
| + // The thread will hang if OnRestart is never called. |
| + void RunLoop() { |
| + message_loop_.Run(); |
|
Sergey Ulanov
2016/04/06 21:04:06
MessageLoop::Run() is deprecated. New code is supp
Yuwei
2016/04/07 00:35:41
Done.
|
| + } |
| + |
| + // Call this if you expect no restart after the loop runs. |
| + // Will quit the loop after kMessageLoopWaitMsecs. |
| + void RunAndWait() { |
| + task_runner_->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&base::MessageLoopForIO::QuitNow, |
| + base::Unretained(&message_loop_)), |
| + loop_wait_); |
| + message_loop_.Run(); |
|
Sergey Ulanov
2016/04/06 21:04:06
Use RunLoop here as well. This function will be si
Yuwei
2016/04/07 00:35:42
Done.
|
| + } |
| + |
| + void Start() { |
| + watcher_->Start(); |
| + } |
| + |
| + void Connect() { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CertificateWatcher::OnClientConnected, |
| + base::Unretained(watcher_), "")); |
| + } |
| + |
| + void Disconnect() { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&CertificateWatcher::OnClientDisconnected, |
| + base::Unretained(watcher_), "")); |
| + } |
| + |
| + void TouchFile() { |
| + task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask, |
| + base::Unretained(this))); |
| + } |
| + |
| + void TouchFileTask() { |
| + std::string testWriteString = std::to_string(rand()); |
| + |
| + base::WriteFile(watch_path_, testWriteString.c_str(), |
| + testWriteString.length()); |
| + } |
| + |
| + base::MessageLoopForIO message_loop_; |
| + scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| + base::ScopedTempDir temp_dir_; |
| + base::FilePath watch_path_; |
| + CertificateWatcher* watcher_; |
|
Sergey Ulanov
2016/04/06 21:04:06
use scoped_ptr<> here to indicate ownership
Yuwei
2016/04/07 00:35:41
Done.
|
| + int restart_count_ = 0; |
| + base::TimeDelta loop_wait_ = |
| + base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs); |
| + |
| + private: |
| + const base::FilePath& CreateAndGetUniqueTempDir() { |
| + EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| + return temp_dir_.path(); |
| + } |
| + |
| + void OnRestart() { |
| + restart_count_++; |
| + message_loop_.QuitNow(); |
| + } |
| +}; |
| + |
| +TEST_F(CertificateWatcherTest, OneTouch) { |
| + EXPECT_EQ(0, restart_count_); |
| + Start(); |
| + EXPECT_EQ(0, restart_count_); |
| + TouchFile(); |
| + RunLoop(); |
| + EXPECT_EQ(1, restart_count_); |
| +} |
| + |
| +TEST_F(CertificateWatcherTest, InhibitDeferRestart) { |
| + Start(); |
| + EXPECT_EQ(0, restart_count_); |
| + Connect(); |
| + EXPECT_EQ(0, restart_count_); |
| + TouchFile(); |
| + RunAndWait(); |
| + EXPECT_EQ(0, restart_count_); |
| + Disconnect(); |
| + RunLoop(); |
| + EXPECT_EQ(1, restart_count_); |
| +} |
| + |
| +TEST_F(CertificateWatcherTest, UninhibitAndRestart) { |
| + Start(); |
| + EXPECT_EQ(0, restart_count_); |
| + Connect(); |
| + EXPECT_EQ(0, restart_count_); |
| + Disconnect(); |
| + RunAndWait(); |
| + EXPECT_EQ(0, restart_count_); |
| + TouchFile(); |
| + RunLoop(); |
| + EXPECT_EQ(1, restart_count_); |
| +} |
| + |
| +} // namespace remoting |