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

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 Feedback 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/message_loop/message_loop.h"
14 #include "remoting/base/auto_thread.h"
Sergey Ulanov 2016/04/06 21:04:06 Don't need this.
Yuwei 2016/04/07 00:35:42 removed.
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace remoting {
18
19 const char kWatchFileName[] = "testfile.txt";
20
21 const int kMessageLoopWaitMsecs = 150;
22
23 class CertificateWatcherTest : public testing::Test {
24 public:
25 CertificateWatcherTest()
26 : task_runner_(message_loop_.task_runner()),
27 watch_path_(CreateAndGetUniqueTempDir().AppendASCII(kWatchFileName)) {
28 watcher_ = new CertificateWatcher(
29 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 delete watcher_;
38 RunAndWait();
Sergey Ulanov 2016/04/06 21:04:06 You can just call RunUntilIdle() here - that will
Yuwei 2016/04/07 00:35:42 Done.
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();
Sergey Ulanov 2016/04/06 21:04:06 MessageLoop::Run() is deprecated. New code is supp
Yuwei 2016/04/07 00:35:41 Done.
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();
Sergey Ulanov 2016/04/06 21:04:06 Use RunLoop here as well. This function will be si
Yuwei 2016/04/07 00:35:42 Done.
57 }
58
59 void Start() {
60 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_;
Sergey Ulanov 2016/04/06 21:04:06 use scoped_ptr<> here to indicate ownership
Yuwei 2016/04/07 00:35:41 Done.
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 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 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 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