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

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

Issue 1838313002: Restart the host when the third party auth certificate changes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Reviewed Created 4 years, 8 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
(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_) {
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 bool Start() {
55 return 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 std::string testWriteString = std::to_string(rand());
80
81 base::WriteFile(watch_path_, testWriteString.c_str(),
82 testWriteString.length());
83 }
84
85 base::MessageLoopForIO message_loop_;
86 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
87 base::ScopedTempDir temp_dir_;
88 base::FilePath watch_path_;
89 CertificateWatcher watcher_;
90 int restart_count_ = 0;
91 base::TimeDelta loop_wait_ =
92 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs);
93
94 private:
95 const base::FilePath& CreateAndGetUniqueTempDir() {
96 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
97 return temp_dir_.path();
98 }
99
100 void OnRestart() {
101 restart_count_++;
102 message_loop_.QuitNow();
103 }
104 };
105
106 TEST_F(CertificateWatcherTest, OneTouch) {
107 EXPECT_EQ(0, restart_count_);
108 EXPECT_TRUE(Start());
109 EXPECT_EQ(0, restart_count_);
110 TouchFile();
111 RunLoop();
112 EXPECT_EQ(1, restart_count_);
113 }
114
115 TEST_F(CertificateWatcherTest, InhibitDeferRestart) {
116 EXPECT_TRUE(Start());
117 EXPECT_EQ(0, restart_count_);
118 Connect();
119 EXPECT_EQ(0, restart_count_);
120 TouchFile();
121 RunAndWait();
122 EXPECT_EQ(0, restart_count_);
123 Disconnect();
124 RunLoop();
125 EXPECT_EQ(1, restart_count_);
126 }
127
128 TEST_F(CertificateWatcherTest, UninhibitAndRestart) {
129 EXPECT_TRUE(Start());
130 EXPECT_EQ(0, restart_count_);
131 Connect();
132 EXPECT_EQ(0, restart_count_);
133 Disconnect();
134 RunAndWait();
135 EXPECT_EQ(0, restart_count_);
136 TouchFile();
137 RunLoop();
138 EXPECT_EQ(1, restart_count_);
139 }
140
141 TEST_F(CertificateWatcherTest, EmptyPathFailToStart) {
142 // Covers the case when base::PathService::Get(base::DIR_HOME, ...) fails
143 watcher_.SetWatchPathForTests(base::FilePath());
144 EXPECT_FALSE(Start());
145 }
146
147 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698