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

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: Reviewer's fb; fixed memory leak 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 ~CertificateWatcherTest() override {
37 watcher_.Stop();
38 RunAndWait();
39 }
40
41 protected:
42 // Call this if you expect a restart after the loop runs.
43 // The thread will hang if OnRestart is never called.
44 void RunLoop() {
45 message_loop_.Run();
46 }
47
48 // Call this if you expect no restart after the loop runs.
49 // Will quit the loop after kMessageLoopWaitMsecs.
50 void RunAndWait() {
51 task_runner_->PostDelayedTask(
52 FROM_HERE,
53 base::Bind(&base::MessageLoopForIO::QuitNow,
54 base::Unretained(&message_loop_)),
55 loop_wait_);
56 message_loop_.Run();
57 }
58
59 bool Start() {
60 return watcher_.Start();
61 }
62
63 void Connect() {
64 task_runner_->PostTask(
65 FROM_HERE,
66 base::Bind(&CertificateWatcher::OnClientConnected,
67 base::Unretained(&watcher_), ""));
68 }
69
70 void Disconnect() {
71 task_runner_->PostTask(
72 FROM_HERE,
73 base::Bind(&CertificateWatcher::OnClientDisconnected,
74 base::Unretained(&watcher_), ""));
75 }
76
77 void TouchFile() {
78 task_runner_->PostTask(
79 FROM_HERE, base::Bind(&CertificateWatcherTest::TouchFileTask,
80 base::Unretained(this)));
81 }
82
83 void TouchFileTask() {
84 std::string testWriteString = std::to_string(rand());
85
86 base::WriteFile(watch_path_, testWriteString.c_str(),
87 testWriteString.length());
88 }
89
90 base::MessageLoopForIO message_loop_;
91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
92 base::ScopedTempDir temp_dir_;
93 base::FilePath watch_path_;
94 CertificateWatcher watcher_;
95 int restart_count_ = 0;
96 base::TimeDelta loop_wait_ =
97 base::TimeDelta::FromMilliseconds(kMessageLoopWaitMsecs);
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 message_loop_.QuitNow();
108 }
109 };
110
111 TEST_F(CertificateWatcherTest, OneTouch) {
112 EXPECT_EQ(0, restart_count_);
113 EXPECT_TRUE(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 EXPECT_TRUE(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 EXPECT_TRUE(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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698