| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/linux/certificate_watcher.h" |
| 6 |
| 7 #include <cstdlib> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/message_loop/message_loop.h" |
| 15 #include "remoting/base/auto_thread.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace remoting { |
| 19 |
| 20 const char kWatchFileName[] = "testfile.txt"; |
| 21 |
| 22 const int kMessageLoopWaitMsecs = 150; |
| 23 |
| 24 class CertificateWatcherTest : public testing::Test { |
| 25 public: |
| 26 CertificateWatcherTest() |
| 27 : task_runner_(message_loop_.task_runner()), |
| 28 watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)), |
| 29 watcher_(base::Bind(&CertificateWatcherTest::OnRestart, |
| 30 base::Unretained(this)), |
| 31 task_runner_, task_runner_) { |
| 32 watcher_.SetDelayForTests(base::TimeDelta::FromSeconds(0)); |
| 33 watcher_.SetWatchPathForTests(watch_path_); |
| 34 } |
| 35 |
| 36 protected: |
| 37 // Call this if you expect a restart after the loop runs. |
| 38 // The thread will hang if OnRestart is never called. |
| 39 void RunLoop() { |
| 40 message_loop_.Run(); |
| 41 } |
| 42 |
| 43 // Call this if you expect no restart after the loop runs. |
| 44 // Will quit the loop after kMessageLoopWaitMsecs. |
| 45 void RunAndWait() { |
| 46 task_runner_->PostDelayedTask( |
| 47 FROM_HERE, |
| 48 base::Bind(&base::MessageLoopForIO::QuitNow, |
| 49 base::Unretained(&message_loop_)), |
| 50 loop_wait_); |
| 51 message_loop_.Run(); |
| 52 } |
| 53 |
| 54 void Start() { |
| 55 watcher_.Start(); |
| 56 } |
| 57 |
| 58 void Connect() { |
| 59 task_runner_->PostTask( |
| 60 FROM_HERE, |
| 61 base::Bind(&CertificateWatcher::OnClientConnected, |
| 62 base::Unretained(&watcher_), "")); |
| 63 } |
| 64 |
| 65 void Disconnect() { |
| 66 task_runner_->PostTask( |
| 67 FROM_HERE, |
| 68 base::Bind(&CertificateWatcher::OnClientDisconnected, |
| 69 base::Unretained(&watcher_), "")); |
| 70 } |
| 71 |
| 72 void TouchFile() { |
| 73 task_runner_->PostTask( |
| 74 FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask, |
| 75 base::Unretained(this))); |
| 76 } |
| 77 |
| 78 void TouchFileTask() { |
| 79 EXPECT_TRUE(watcher_.Started()); |
| 80 std::string testWriteString = std::to_string(rand()); |
| 81 |
| 82 base::WriteFile(watch_path_, testWriteString.c_str(), |
| 83 testWriteString.length()); |
| 84 } |
| 85 |
| 86 base::MessageLoopForIO message_loop_; |
| 87 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 88 base::ScopedTempDir temp_dir_; |
| 89 base::FilePath watch_path_; |
| 90 CertificateWatcher watcher_; |
| 91 int restart_count_ = 0; |
| 92 base::TimeDelta loop_wait_ = |
| 93 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs); |
| 94 |
| 95 private: |
| 96 const base::FilePath& CreateAndGetUniqueTempDir() { |
| 97 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 98 return temp_dir_.path(); |
| 99 } |
| 100 |
| 101 void OnRestart() { |
| 102 restart_count_++; |
| 103 message_loop_.QuitNow(); |
| 104 } |
| 105 }; |
| 106 |
| 107 TEST_F(CertificateWatcherTest, OneTouch) { |
| 108 EXPECT_EQ(0, restart_count_); |
| 109 Start(); |
| 110 EXPECT_EQ(0, restart_count_); |
| 111 TouchFile(); |
| 112 RunLoop(); |
| 113 EXPECT_EQ(1, restart_count_); |
| 114 } |
| 115 |
| 116 TEST_F(CertificateWatcherTest, InhibitDeferRestart) { |
| 117 Start(); |
| 118 EXPECT_EQ(0, restart_count_); |
| 119 Connect(); |
| 120 EXPECT_EQ(0, restart_count_); |
| 121 TouchFile(); |
| 122 RunAndWait(); |
| 123 EXPECT_EQ(0, restart_count_); |
| 124 Disconnect(); |
| 125 RunLoop(); |
| 126 EXPECT_EQ(1, restart_count_); |
| 127 } |
| 128 |
| 129 TEST_F(CertificateWatcherTest, UninhibitAndRestart) { |
| 130 Start(); |
| 131 EXPECT_EQ(0, restart_count_); |
| 132 Connect(); |
| 133 EXPECT_EQ(0, restart_count_); |
| 134 Disconnect(); |
| 135 RunAndWait(); |
| 136 EXPECT_EQ(0, restart_count_); |
| 137 TouchFile(); |
| 138 RunLoop(); |
| 139 EXPECT_EQ(1, restart_count_); |
| 140 } |
| 141 |
| 142 TEST_F(CertificateWatcherTest, EmptyPathFailToStart) { |
| 143 // Covers the case when base::PathService::Get(base::DIR_HOME, ...) fails |
| 144 watcher_.SetWatchPathForTests(base::FilePath()); |
| 145 Start(); |
| 146 RunAndWait(); |
| 147 EXPECT_FALSE(watcher_.Started()); |
| 148 } |
| 149 |
| 150 } // namespace remoting |
| OLD | NEW |