Chromium Code Reviews| 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 <cstring> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/files/file_util.h" | |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "remoting/base/auto_thread.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace remoting { | |
| 18 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.
| |
| 19 const char kTestWriteString[] = "TEST STRING"; | |
| 20 | |
| 21 class CertificateWatcherTest : public testing::Test { | |
| 22 public: | |
| 23 CertificateWatcherTest() | |
| 24 : task_runner_(message_loop_.task_runner()), | |
| 25 watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)), | |
| 26 watcher_(base::WeakPtr<HostStatusMonitor>(), | |
| 27 base::Bind(&CertificateWatcherTest::OnRestart, | |
| 28 base::Unretained(this))) { | |
| 29 watcher_.SetDelay(base::TimeDelta::FromSeconds(0)); | |
| 30 watcher_.SetRestartDeferredAction(base::Bind( | |
| 31 &CertificateWatcherTest::OnDeferred, | |
| 32 base::Unretained(this))); | |
| 33 watcher_.SetWatchPath(watch_path_); | |
| 34 } | |
| 35 | |
| 36 protected: | |
| 37 void Start() { | |
| 38 if (!task_runner_->BelongsToCurrentThread()) { | |
| 39 task_runner_->PostTask(FROM_HERE, | |
| 40 base::Bind(&CertificateWatcherTest::Start, | |
| 41 base::Unretained(this))); | |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 watcher_.Start(); | |
| 46 } | |
| 47 | |
| 48 void RunLoop() { message_loop_.Run(); } | |
| 49 | |
| 50 void TouchFile() { | |
| 51 if (!task_runner_->BelongsToCurrentThread()) { | |
| 52 task_runner_->PostTask(FROM_HERE, | |
| 53 base::Bind(&CertificateWatcherTest::TouchFile, | |
| 54 base::Unretained(this))); | |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 base::WriteFile(watch_path_, | |
| 59 kTestWriteString, | |
| 60 strlen(kTestWriteString)); | |
| 61 } | |
| 62 | |
| 63 base::MessageLoopForIO message_loop_; | |
| 64 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 65 base::ScopedTempDir temp_dir_; | |
| 66 base::FilePath watch_path_; | |
| 67 CertificateWatcher watcher_; | |
| 68 int restart_count_ = 0; | |
| 69 | |
| 70 private: | |
| 71 const base::FilePath& CreateAndGetUniqueTempDir() { | |
| 72 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 73 return temp_dir_.path(); | |
| 74 } | |
| 75 | |
| 76 void OnRestart() { | |
| 77 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 78 restart_count_++; | |
| 79 message_loop_.QuitNow(); | |
| 80 } | |
| 81 | |
| 82 void OnDeferred() { | |
| 83 // Simply quit the loop to so that the test can continue | |
| 84 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | |
| 85 message_loop_.QuitNow(); | |
| 86 } | |
| 87 }; | |
| 88 | |
| 89 TEST_F(CertificateWatcherTest, OneTouch) { | |
| 90 EXPECT_EQ(0, restart_count_); | |
| 91 Start(); | |
| 92 TouchFile(); | |
| 93 RunLoop(); | |
| 94 EXPECT_EQ(1, restart_count_); | |
| 95 } | |
| 96 | |
| 97 TEST_F(CertificateWatcherTest, InhibitDeferRestart) { | |
| 98 Start(); | |
| 99 EXPECT_EQ(0, restart_count_); | |
| 100 watcher_.SetInhibit(true); | |
|
Sergey Ulanov
2016/03/31 22:36:16
Call OnClientConnected() and OnClientDisconnected(
Yuwei
2016/04/01 23:41:03
Done.
| |
| 101 EXPECT_EQ(0, restart_count_); | |
| 102 TouchFile(); | |
| 103 RunLoop(); | |
| 104 EXPECT_EQ(0, restart_count_); | |
| 105 watcher_.SetInhibit(false); | |
| 106 RunLoop(); | |
| 107 EXPECT_EQ(1, restart_count_); | |
| 108 } | |
| 109 | |
| 110 TEST_F(CertificateWatcherTest, UninhibitAndRestart) { | |
| 111 Start(); | |
| 112 EXPECT_EQ(0, restart_count_); | |
| 113 watcher_.SetInhibit(true); | |
| 114 EXPECT_EQ(0, restart_count_); | |
| 115 watcher_.SetInhibit(false); | |
| 116 EXPECT_EQ(0, restart_count_); | |
| 117 TouchFile(); | |
| 118 RunLoop(); | |
| 119 EXPECT_EQ(1, restart_count_); | |
| 120 } | |
| 121 | |
| 122 } // namespace remoting | |
| OLD | NEW |