| 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/run_loop.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace remoting { |
| 17 |
| 18 const char kWatchFileName[] = "testfile.txt"; |
| 19 |
| 20 const int kMessageLoopWaitMsecs = 150; |
| 21 |
| 22 class CertificateWatcherTest : public testing::Test { |
| 23 public: |
| 24 CertificateWatcherTest() |
| 25 : task_runner_(message_loop_.task_runner()), |
| 26 watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)) { |
| 27 watcher_.reset(new CertificateWatcher( |
| 28 base::Bind(&CertificateWatcherTest::OnRestart, |
| 29 base::Unretained(this)), |
| 30 task_runner_)); |
| 31 watcher_->SetDelayForTests(base::TimeDelta::FromSeconds(0)); |
| 32 watcher_->SetWatchPathForTests(watch_path_); |
| 33 } |
| 34 |
| 35 ~CertificateWatcherTest() override { |
| 36 watcher_.reset(); |
| 37 base::RunLoop().RunUntilIdle(); |
| 38 } |
| 39 |
| 40 protected: |
| 41 // Call this if you expect a restart after the loop runs. |
| 42 // The thread will hang if OnRestart is never called. |
| 43 void RunLoop() { |
| 44 base::RunLoop loop; |
| 45 quit_loop_closure_ = loop.QuitClosure(); |
| 46 loop.Run(); |
| 47 } |
| 48 |
| 49 // Call this if you expect no restart after the loop runs. |
| 50 // Will quit the loop after kMessageLoopWaitMsecs. |
| 51 void RunAndWait() { |
| 52 base::RunLoop loop; |
| 53 task_runner_->PostDelayedTask(FROM_HERE,loop.QuitClosure(), |
| 54 loop_wait_); |
| 55 loop.Run(); |
| 56 } |
| 57 |
| 58 void Start() { |
| 59 watcher_->Start(); |
| 60 } |
| 61 |
| 62 void Connect() { |
| 63 task_runner_->PostTask( |
| 64 FROM_HERE, |
| 65 base::Bind(&CertificateWatcher::OnClientConnected, |
| 66 base::Unretained(watcher_.get()), "")); |
| 67 } |
| 68 |
| 69 void Disconnect() { |
| 70 task_runner_->PostTask( |
| 71 FROM_HERE, |
| 72 base::Bind(&CertificateWatcher::OnClientDisconnected, |
| 73 base::Unretained(watcher_.get()), "")); |
| 74 } |
| 75 |
| 76 void TouchFile() { |
| 77 task_runner_->PostTask( |
| 78 FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask, |
| 79 base::Unretained(this))); |
| 80 } |
| 81 |
| 82 void TouchFileTask() { |
| 83 std::string testWriteString = std::to_string(rand()); |
| 84 |
| 85 base::WriteFile(watch_path_, testWriteString.c_str(), |
| 86 testWriteString.length()); |
| 87 } |
| 88 |
| 89 base::MessageLoopForIO message_loop_; |
| 90 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 91 base::ScopedTempDir temp_dir_; |
| 92 base::FilePath watch_path_; |
| 93 scoped_ptr<CertificateWatcher> watcher_; |
| 94 int restart_count_ = 0; |
| 95 base::TimeDelta loop_wait_ = |
| 96 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs); |
| 97 base::Closure quit_loop_closure_; |
| 98 |
| 99 private: |
| 100 const base::FilePath& CreateAndGetUniqueTempDir() { |
| 101 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 102 return temp_dir_.path(); |
| 103 } |
| 104 |
| 105 void OnRestart() { |
| 106 restart_count_++; |
| 107 quit_loop_closure_.Run(); |
| 108 } |
| 109 }; |
| 110 |
| 111 TEST_F(CertificateWatcherTest, OneTouch) { |
| 112 EXPECT_EQ(0, restart_count_); |
| 113 Start(); |
| 114 EXPECT_EQ(0, restart_count_); |
| 115 TouchFile(); |
| 116 RunLoop(); |
| 117 EXPECT_EQ(1, restart_count_); |
| 118 } |
| 119 |
| 120 TEST_F(CertificateWatcherTest, InhibitDeferRestart) { |
| 121 Start(); |
| 122 EXPECT_EQ(0, restart_count_); |
| 123 Connect(); |
| 124 EXPECT_EQ(0, restart_count_); |
| 125 TouchFile(); |
| 126 RunAndWait(); |
| 127 EXPECT_EQ(0, restart_count_); |
| 128 Disconnect(); |
| 129 RunLoop(); |
| 130 EXPECT_EQ(1, restart_count_); |
| 131 } |
| 132 |
| 133 TEST_F(CertificateWatcherTest, UninhibitAndRestart) { |
| 134 Start(); |
| 135 EXPECT_EQ(0, restart_count_); |
| 136 Connect(); |
| 137 EXPECT_EQ(0, restart_count_); |
| 138 Disconnect(); |
| 139 RunAndWait(); |
| 140 EXPECT_EQ(0, restart_count_); |
| 141 TouchFile(); |
| 142 RunLoop(); |
| 143 EXPECT_EQ(1, restart_count_); |
| 144 } |
| 145 |
| 146 } // namespace remoting |
| OLD | NEW |