Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: remoting/host/linux/certificate_watcher_unittest.cc

Issue 1977963002: [remoting host] Only watch for material changes to NSS DB files (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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()
26 : task_runner_(message_loop_.task_runner()), 29 : task_runner_(message_loop_.task_runner()) {
27 watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)) { 30 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
28 watcher_.reset(new CertificateWatcher( 31 watcher_.reset(new CertificateWatcher(
29 base::Bind(&CertificateWatcherTest::OnRestart, 32 base::Bind(&CertificateWatcherTest::OnRestart,
30 base::Unretained(this)), 33 base::Unretained(this)),
31 task_runner_)); 34 task_runner_));
32 watcher_->SetDelayForTests(base::TimeDelta::FromSeconds(0)); 35 watcher_->SetDelayForTests(base::TimeDelta::FromSeconds(0));
33 watcher_->SetWatchPathForTests(watch_path_); 36 watcher_->SetWatchPathForTests(temp_dir_.path());
34 } 37 }
35 38
36 ~CertificateWatcherTest() override { 39 ~CertificateWatcherTest() override {
37 watcher_.reset(); 40 watcher_.reset();
38 base::RunLoop().RunUntilIdle(); 41 base::RunLoop().RunUntilIdle();
39 } 42 }
40 43
41 protected: 44 protected:
42 // Call this if you expect a restart after the loop runs. 45 // Call this if you expect a restart after the loop runs.
43 // The thread will hang if OnRestart is never called. 46 // The thread will hang if OnRestart is never called.
(...skipping 23 matching lines...) Expand all
67 base::Unretained(watcher_.get()), "")); 70 base::Unretained(watcher_.get()), ""));
68 } 71 }
69 72
70 void Disconnect() { 73 void Disconnect() {
71 task_runner_->PostTask( 74 task_runner_->PostTask(
72 FROM_HERE, 75 FROM_HERE,
73 base::Bind(&CertificateWatcher::OnClientDisconnected, 76 base::Bind(&CertificateWatcher::OnClientDisconnected,
74 base::Unretained(watcher_.get()), "")); 77 base::Unretained(watcher_.get()), ""));
75 } 78 }
76 79
77 void TouchFile() { 80 void TouchFile(const char* filename) {
78 task_runner_->PostTask( 81 task_runner_->PostTask(
79 FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask, 82 FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask,
80 base::Unretained(this))); 83 base::Unretained(this),
84 filename));
81 } 85 }
82 86
83 void TouchFileTask() { 87 void TouchFileTask(const char* filename) {
84 std::string testWriteString = std::to_string(rand()); 88 std::string testWriteString = std::to_string(rand());
89 base::FilePath path = temp_dir_.path().AppendASCII(filename);
85 90
86 base::WriteFile(watch_path_, testWriteString.c_str(), 91 if (base::PathExists(path)) {
87 testWriteString.length()); 92 EXPECT_TRUE(base::AppendToFile(path,
93 testWriteString.c_str(),
94 testWriteString.length()));
95 } else {
96 EXPECT_TRUE(base::WriteFile(path,
97 testWriteString.c_str(),
98 testWriteString.length()));
99 }
88 } 100 }
89 101
90 base::MessageLoopForIO message_loop_; 102 base::MessageLoopForIO message_loop_;
91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 103 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
92 base::ScopedTempDir temp_dir_; 104 base::ScopedTempDir temp_dir_;
93 base::FilePath watch_path_;
94 std::unique_ptr<CertificateWatcher> watcher_; 105 std::unique_ptr<CertificateWatcher> watcher_;
95 int restart_count_ = 0; 106 int restart_count_ = 0;
96 base::TimeDelta loop_wait_ = 107 base::TimeDelta loop_wait_ =
97 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs); 108 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs);
98 base::Closure quit_loop_closure_; 109 base::Closure quit_loop_closure_;
99 110
100 private: 111 private:
101 const base::FilePath& CreateAndGetUniqueTempDir() {
102 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
103 return temp_dir_.path();
104 }
105
106 void OnRestart() { 112 void OnRestart() {
107 restart_count_++; 113 restart_count_++;
108 quit_loop_closure_.Run(); 114 quit_loop_closure_.Run();
109 } 115 }
110 }; 116 };
111 117
112 TEST_F(CertificateWatcherTest, OneTouch) { 118 TEST_F(CertificateWatcherTest, OneTouch) {
113 EXPECT_EQ(0, restart_count_); 119 EXPECT_EQ(0, restart_count_);
114 Start(); 120 Start();
115 EXPECT_EQ(0, restart_count_); 121 EXPECT_EQ(0, restart_count_);
116 TouchFile(); 122 TouchFile(kCertFileName);
117 RunLoop(); 123 RunLoop();
118 EXPECT_EQ(1, restart_count_); 124 EXPECT_EQ(1, restart_count_);
119 } 125 }
126
127 TEST_F(CertificateWatcherTest, OneTouchAppend) {
128 EXPECT_EQ(0, restart_count_);
129 TouchFileTask(kKeyFileName);
130 Start();
131 EXPECT_EQ(0, restart_count_);
132 TouchFile(kKeyFileName); // Appends to existing file.
133 RunLoop();
134 EXPECT_EQ(1, restart_count_);
135 }
120 136
121 TEST_F(CertificateWatcherTest, InhibitDeferRestart) { 137 TEST_F(CertificateWatcherTest, InhibitDeferRestart) {
122 Start(); 138 Start();
123 EXPECT_EQ(0, restart_count_); 139 EXPECT_EQ(0, restart_count_);
124 Connect(); 140 Connect();
125 EXPECT_EQ(0, restart_count_); 141 EXPECT_EQ(0, restart_count_);
126 TouchFile(); 142 TouchFile(kPKCSFileName);
127 RunAndWait(); 143 RunAndWait();
128 EXPECT_EQ(0, restart_count_); 144 EXPECT_EQ(0, restart_count_);
129 Disconnect(); 145 Disconnect();
130 RunLoop(); 146 RunLoop();
131 EXPECT_EQ(1, restart_count_); 147 EXPECT_EQ(1, restart_count_);
132 } 148 }
133 149
134 TEST_F(CertificateWatcherTest, UninhibitAndRestart) { 150 TEST_F(CertificateWatcherTest, UninhibitAndRestart) {
135 Start(); 151 Start();
136 EXPECT_EQ(0, restart_count_); 152 EXPECT_EQ(0, restart_count_);
137 Connect(); 153 Connect();
138 EXPECT_EQ(0, restart_count_); 154 EXPECT_EQ(0, restart_count_);
139 Disconnect(); 155 Disconnect();
140 RunAndWait(); 156 RunAndWait();
141 EXPECT_EQ(0, restart_count_); 157 EXPECT_EQ(0, restart_count_);
142 TouchFile(); 158 TouchFile(kCertFileName);
143 RunLoop(); 159 RunLoop();
144 EXPECT_EQ(1, restart_count_); 160 EXPECT_EQ(1, restart_count_);
145 } 161 }
146 162
163 TEST_F(CertificateWatcherTest, TouchOtherFile) {
164 // The watcher should not trigger if changes are made that don't affect the
165 // NSS DB contents.
166 EXPECT_EQ(0, restart_count_);
167 Start();
168 EXPECT_EQ(0, restart_count_);
169 TouchFile(kOtherFileName);
170 RunAndWait();
171 EXPECT_EQ(0, restart_count_);
172 }
173
147 } // namespace remoting 174 } // namespace remoting
OLDNEW
« remoting/host/linux/certificate_watcher.cc ('K') | « remoting/host/linux/certificate_watcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698