| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/linux/certificate_watcher.h" | 5 #include "remoting/host/linux/certificate_watcher.h" |
| 6 | 6 |
| 7 #include <cstdlib> | 7 #include <cstdlib> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 namespace remoting { | 17 namespace remoting { |
| 18 | 18 |
| 19 const char kWatchFileName[] = "testfile.txt"; | 19 const char kCertFileName[] = "cert9.db"; |
| 20 const char kKeyFileName[] = "key4.db"; |
| 21 const char kPKCSFileName[] = "pkcs11.txt"; |
| 22 const char kOtherFileName[] = "testfile.txt"; |
| 20 | 23 |
| 21 const int kMessageLoopWaitMsecs = 150; | 24 const int kMessageLoopWaitMsecs = 150; |
| 22 | 25 |
| 23 class CertificateWatcherTest : public testing::Test { | 26 class CertificateWatcherTest : public testing::Test { |
| 24 public: | 27 public: |
| 25 CertificateWatcherTest() | 28 CertificateWatcherTest() : task_runner_(message_loop_.task_runner()) { |
| 26 : task_runner_(message_loop_.task_runner()), | 29 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 27 watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)) { | |
| 28 watcher_.reset(new CertificateWatcher( | 30 watcher_.reset(new CertificateWatcher( |
| 29 base::Bind(&CertificateWatcherTest::OnRestart, | 31 base::Bind(&CertificateWatcherTest::OnRestart, |
| 30 base::Unretained(this)), | 32 base::Unretained(this)), |
| 31 task_runner_)); | 33 task_runner_)); |
| 32 watcher_->SetDelayForTests(base::TimeDelta::FromSeconds(0)); | 34 watcher_->SetDelayForTests(base::TimeDelta::FromSeconds(0)); |
| 33 watcher_->SetWatchPathForTests(watch_path_); | 35 watcher_->SetWatchPathForTests(temp_dir_.path()); |
| 34 } | 36 } |
| 35 | 37 |
| 36 ~CertificateWatcherTest() override { | 38 ~CertificateWatcherTest() override { |
| 37 watcher_.reset(); | 39 watcher_.reset(); |
| 38 base::RunLoop().RunUntilIdle(); | 40 base::RunLoop().RunUntilIdle(); |
| 39 } | 41 } |
| 40 | 42 |
| 41 protected: | 43 protected: |
| 42 // Call this if you expect a restart after the loop runs. | 44 // Call this if you expect a restart after the loop runs. |
| 43 // The thread will hang if OnRestart is never called. | 45 // The thread will hang if OnRestart is never called. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 67 base::Unretained(watcher_.get()), "")); | 69 base::Unretained(watcher_.get()), "")); |
| 68 } | 70 } |
| 69 | 71 |
| 70 void Disconnect() { | 72 void Disconnect() { |
| 71 task_runner_->PostTask( | 73 task_runner_->PostTask( |
| 72 FROM_HERE, | 74 FROM_HERE, |
| 73 base::Bind(&CertificateWatcher::OnClientDisconnected, | 75 base::Bind(&CertificateWatcher::OnClientDisconnected, |
| 74 base::Unretained(watcher_.get()), "")); | 76 base::Unretained(watcher_.get()), "")); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void TouchFile() { | 79 void TouchFile(const char* filename) { |
| 78 task_runner_->PostTask( | 80 task_runner_->PostTask(FROM_HERE, |
| 79 FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask, | 81 base::Bind(&CertificateWatcherTest::TouchFileTask, |
| 80 base::Unretained(this))); | 82 base::Unretained(this), filename)); |
| 81 } | 83 } |
| 82 | 84 |
| 83 void TouchFileTask() { | 85 void TouchFileTask(const char* filename) { |
| 84 std::string testWriteString = std::to_string(rand()); | 86 std::string testWriteString = std::to_string(rand()); |
| 87 base::FilePath path = temp_dir_.path().AppendASCII(filename); |
| 85 | 88 |
| 86 base::WriteFile(watch_path_, testWriteString.c_str(), | 89 if (base::PathExists(path)) { |
| 87 testWriteString.length()); | 90 EXPECT_TRUE(base::AppendToFile(path, testWriteString.c_str(), |
| 91 testWriteString.length())); |
| 92 } else { |
| 93 EXPECT_TRUE(base::WriteFile(path, testWriteString.c_str(), |
| 94 testWriteString.length())); |
| 95 } |
| 88 } | 96 } |
| 89 | 97 |
| 90 base::MessageLoopForIO message_loop_; | 98 base::MessageLoopForIO message_loop_; |
| 91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 99 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 92 base::ScopedTempDir temp_dir_; | 100 base::ScopedTempDir temp_dir_; |
| 93 base::FilePath watch_path_; | |
| 94 std::unique_ptr<CertificateWatcher> watcher_; | 101 std::unique_ptr<CertificateWatcher> watcher_; |
| 95 int restart_count_ = 0; | 102 int restart_count_ = 0; |
| 96 base::TimeDelta loop_wait_ = | 103 base::TimeDelta loop_wait_ = |
| 97 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs); | 104 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs); |
| 98 base::Closure quit_loop_closure_; | 105 base::Closure quit_loop_closure_; |
| 99 | 106 |
| 100 private: | 107 private: |
| 101 const base::FilePath& CreateAndGetUniqueTempDir() { | |
| 102 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
| 103 return temp_dir_.path(); | |
| 104 } | |
| 105 | |
| 106 void OnRestart() { | 108 void OnRestart() { |
| 107 restart_count_++; | 109 restart_count_++; |
| 108 quit_loop_closure_.Run(); | 110 quit_loop_closure_.Run(); |
| 109 } | 111 } |
| 110 }; | 112 }; |
| 111 | 113 |
| 112 TEST_F(CertificateWatcherTest, OneTouch) { | 114 TEST_F(CertificateWatcherTest, OneTouch) { |
| 113 EXPECT_EQ(0, restart_count_); | 115 EXPECT_EQ(0, restart_count_); |
| 114 Start(); | 116 Start(); |
| 115 EXPECT_EQ(0, restart_count_); | 117 EXPECT_EQ(0, restart_count_); |
| 116 TouchFile(); | 118 TouchFile(kCertFileName); |
| 117 RunLoop(); | 119 RunLoop(); |
| 118 EXPECT_EQ(1, restart_count_); | 120 EXPECT_EQ(1, restart_count_); |
| 119 } | 121 } |
| 122 |
| 123 TEST_F(CertificateWatcherTest, OneTouchAppend) { |
| 124 EXPECT_EQ(0, restart_count_); |
| 125 TouchFileTask(kKeyFileName); |
| 126 Start(); |
| 127 EXPECT_EQ(0, restart_count_); |
| 128 TouchFile(kKeyFileName); // Appends to existing file. |
| 129 RunLoop(); |
| 130 EXPECT_EQ(1, restart_count_); |
| 131 } |
| 120 | 132 |
| 121 TEST_F(CertificateWatcherTest, InhibitDeferRestart) { | 133 TEST_F(CertificateWatcherTest, InhibitDeferRestart) { |
| 122 Start(); | 134 Start(); |
| 123 EXPECT_EQ(0, restart_count_); | 135 EXPECT_EQ(0, restart_count_); |
| 124 Connect(); | 136 Connect(); |
| 125 EXPECT_EQ(0, restart_count_); | 137 EXPECT_EQ(0, restart_count_); |
| 126 TouchFile(); | 138 TouchFile(kPKCSFileName); |
| 127 RunAndWait(); | 139 RunAndWait(); |
| 128 EXPECT_EQ(0, restart_count_); | 140 EXPECT_EQ(0, restart_count_); |
| 129 Disconnect(); | 141 Disconnect(); |
| 130 RunLoop(); | 142 RunLoop(); |
| 131 EXPECT_EQ(1, restart_count_); | 143 EXPECT_EQ(1, restart_count_); |
| 132 } | 144 } |
| 133 | 145 |
| 134 TEST_F(CertificateWatcherTest, UninhibitAndRestart) { | 146 TEST_F(CertificateWatcherTest, UninhibitAndRestart) { |
| 135 Start(); | 147 Start(); |
| 136 EXPECT_EQ(0, restart_count_); | 148 EXPECT_EQ(0, restart_count_); |
| 137 Connect(); | 149 Connect(); |
| 138 EXPECT_EQ(0, restart_count_); | 150 EXPECT_EQ(0, restart_count_); |
| 139 Disconnect(); | 151 Disconnect(); |
| 140 RunAndWait(); | 152 RunAndWait(); |
| 141 EXPECT_EQ(0, restart_count_); | 153 EXPECT_EQ(0, restart_count_); |
| 142 TouchFile(); | 154 TouchFile(kCertFileName); |
| 143 RunLoop(); | 155 RunLoop(); |
| 144 EXPECT_EQ(1, restart_count_); | 156 EXPECT_EQ(1, restart_count_); |
| 145 } | 157 } |
| 146 | 158 |
| 159 TEST_F(CertificateWatcherTest, TouchOtherFile) { |
| 160 // The watcher should not trigger if changes are made that don't affect the |
| 161 // NSS DB contents. |
| 162 EXPECT_EQ(0, restart_count_); |
| 163 Start(); |
| 164 EXPECT_EQ(0, restart_count_); |
| 165 TouchFile(kOtherFileName); |
| 166 RunAndWait(); |
| 167 EXPECT_EQ(0, restart_count_); |
| 168 } |
| 169 |
| 147 } // namespace remoting | 170 } // namespace remoting |
| OLD | NEW |