OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 "chrome/browser/net/net_log_temp_file.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/values.h" | |
12 #include "build/build_config.h" | |
13 #include "chrome/browser/net/chrome_net_log.h" | |
14 #include "content/public/test/test_browser_thread.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 using content::BrowserThread; | |
18 | |
19 class TestNetLogTempFile : public NetLogTempFile { | |
20 public: | |
21 explicit TestNetLogTempFile(ChromeNetLog* chrome_net_log) | |
22 : NetLogTempFile(chrome_net_log), | |
23 lie_about_net_export_log_directory_(false), | |
24 lie_about_file_existence_(false) { | |
25 } | |
26 | |
27 // NetLogTempFile implementation: | |
28 virtual bool GetNetExportLogDirectory(FilePath* path) OVERRIDE { | |
29 if (lie_about_net_export_log_directory_) | |
30 return false; | |
31 return NetLogTempFile::GetNetExportLogDirectory(path); | |
32 } | |
33 | |
34 virtual bool NetExportLogExists() OVERRIDE { | |
35 if (lie_about_file_existence_) | |
36 return false; | |
37 return NetLogTempFile::NetExportLogExists(); | |
38 } | |
39 | |
40 void set_lie_about_net_export_log_directory( | |
41 bool lie_about_net_export_log_directory) { | |
42 lie_about_net_export_log_directory_ = lie_about_net_export_log_directory; | |
43 } | |
44 | |
45 void set_lie_about_file_existence(bool lie_about_file_existence) { | |
46 lie_about_file_existence_ = lie_about_file_existence; | |
47 } | |
48 | |
49 private: | |
50 bool lie_about_net_export_log_directory_; | |
51 bool lie_about_file_existence_; | |
52 }; | |
53 | |
54 class NetLogTempFileTest : public ::testing::Test { | |
55 protected: | |
mmenke
2013/01/28 21:51:41
nit: While this works, it's more common just to m
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
56 NetLogTempFileTest() | |
57 : net_log_(new ChromeNetLog), | |
58 net_log_temp_file_(new TestNetLogTempFile(net_log_.get())), | |
59 file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING, | |
60 &message_loop_) { | |
61 } | |
62 | |
63 // ::testing::Test implementation: | |
64 virtual void SetUp() OVERRIDE { | |
65 // Get a temporary file name for unit tests. | |
66 FilePath net_log_dir; | |
67 ASSERT_TRUE(net_log_temp_file_->GetNetExportLogDirectory(&net_log_dir)); | |
68 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(net_log_dir, | |
69 &net_export_log_)); | |
70 | |
71 net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value(); | |
72 | |
73 // CreateTemporaryFileInDir may return a legacy 8.3 file name on windows. | |
74 // Need to use the original directory name for string comparisons. | |
75 net_log_temp_file_->GetNetExportLog(); | |
mmenke
2013/01/28 21:51:41
ASSERT_TRUE?
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
76 net_export_log_ = net_log_temp_file_->log_path_; | |
mmenke
2013/01/28 21:51:41
ASSERT not empty?
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
77 } | |
78 | |
79 virtual void TearDown() OVERRIDE { | |
80 // Delete the temporary file we have created. | |
81 ASSERT_TRUE(file_util::Delete(net_export_log_, false)); | |
82 } | |
83 | |
84 std::string GetStateString() const { | |
85 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState()); | |
86 std::string state; | |
87 EXPECT_TRUE(dict->GetString("state", &state)); | |
88 return state; | |
89 } | |
90 | |
91 // Make sure the export file has been created and is non-empty, as net | |
92 // constants will always be written to it on creation. | |
93 void VerifyNetExportLog() { | |
94 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_); | |
95 EXPECT_TRUE(file_util::PathExists(net_export_log_)); | |
96 | |
97 int64 file_size; | |
98 // file_util::GetFileSize returns proper file size on open handles. | |
99 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &file_size)); | |
100 EXPECT_GT(file_size, 0); | |
101 } | |
102 | |
103 // Verify state and GetFilePath return correct values if EnsureInit() fails. | |
104 void VerifyFilePathAndStateAfterEnsureInitFailure() { | |
105 EXPECT_EQ("UNINITIALIZED", GetStateString()); | |
106 EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED, | |
107 net_log_temp_file_->state()); | |
108 | |
109 FilePath net_export_file_path; | |
110 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
111 } | |
112 | |
113 // When we lie in NetExportLogExists, make sure state and GetFilePath return | |
114 // correct values. | |
115 void VerifyFilePathAndStateAfterEnsureInit() { | |
116 EXPECT_EQ("ALLOW_START", GetStateString()); | |
117 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state()); | |
118 | |
119 FilePath net_export_file_path; | |
120 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
121 EXPECT_FALSE(net_log_temp_file_->NetExportLogExists()); | |
122 } | |
123 | |
124 // Make sure the export file has been succeffully initialized. | |
mmenke
2013/01/28 21:51:41
nit: successfully
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
125 void VerifyFileAndStateAfterDoStart() { | |
126 EXPECT_EQ("ALLOW_STOP", GetStateString()); | |
127 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state()); | |
128 | |
129 // Check GetFilePath returns false, if we are still writing to file. | |
130 FilePath net_export_file_path; | |
131 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
132 | |
133 VerifyNetExportLog(); | |
134 } | |
135 | |
136 // Make sure the export file has been succeffully initialized. | |
mmenke
2013/01/28 21:51:41
nit: successfully
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
137 void VerifyFileAndStateAfterDoStop() { | |
138 EXPECT_EQ("ALLOW_START_SEND", GetStateString()); | |
139 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, | |
140 net_log_temp_file_->state()); | |
141 | |
142 FilePath net_export_file_path; | |
143 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
144 EXPECT_TRUE(file_util::PathExists(net_export_file_path)); | |
mmenke
2013/01/28 21:51:41
EXPECT_EQ(net_export_log_, net_export_file_path);
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
145 | |
146 VerifyNetExportLog(); | |
147 } | |
148 | |
149 scoped_ptr<ChromeNetLog> net_log_; | |
150 // |net_log_temp_file_| is initialized after |net_log_| so that it can stop | |
151 // obvserving on destruction. | |
152 scoped_ptr<TestNetLogTempFile> net_log_temp_file_; | |
153 FilePath net_export_log_; | |
154 | |
155 private: | |
156 MessageLoop message_loop_; | |
157 content::TestBrowserThread file_user_blocking_thread_; | |
158 }; | |
159 | |
160 TEST_F(NetLogTempFileTest, EnsureInitFailure) { | |
161 net_log_temp_file_->set_lie_about_net_export_log_directory(true); | |
162 | |
163 EXPECT_FALSE(net_log_temp_file_->EnsureInit()); | |
164 VerifyFilePathAndStateAfterEnsureInitFailure(); | |
165 | |
166 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
167 VerifyFilePathAndStateAfterEnsureInitFailure(); | |
168 } | |
169 | |
170 TEST_F(NetLogTempFileTest, EnsureInitAllowStart) { | |
171 net_log_temp_file_->set_lie_about_file_existence(true); | |
172 | |
173 EXPECT_TRUE(net_log_temp_file_->EnsureInit()); | |
174 VerifyFilePathAndStateAfterEnsureInit(); | |
175 | |
176 // Calling EnsureInit() second time should be a no-op. | |
177 EXPECT_TRUE(net_log_temp_file_->EnsureInit()); | |
178 VerifyFilePathAndStateAfterEnsureInit(); | |
179 } | |
180 | |
181 TEST_F(NetLogTempFileTest, EnsureInitAllowStartOrSend) { | |
182 EXPECT_TRUE(net_log_temp_file_->EnsureInit()); | |
183 | |
184 EXPECT_EQ("ALLOW_START_SEND", GetStateString()); | |
185 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND, | |
186 net_log_temp_file_->state()); | |
187 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_); | |
188 EXPECT_TRUE(file_util::PathExists(net_export_log_)); | |
189 | |
190 FilePath net_export_file_path; | |
191 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
192 EXPECT_TRUE(file_util::PathExists(net_export_file_path)); | |
mmenke
2013/01/28 21:51:41
EXPECT_EQ(net_export_log_, net_export_file_path);
ramant (doing other things)
2013/01/29 04:48:37
Done.
| |
193 | |
194 // GetFilePath should return false if NetExportLogExists() fails. | |
195 net_log_temp_file_->set_lie_about_file_existence(true); | |
196 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path)); | |
197 } | |
198 | |
199 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) { | |
200 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
201 VerifyFileAndStateAfterDoStart(); | |
202 | |
203 // Calling DO_START second time should be a no-op. | |
204 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
205 VerifyFileAndStateAfterDoStart(); | |
206 | |
207 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
208 VerifyFileAndStateAfterDoStop(); | |
209 | |
210 // Calling DO_STOP second time should be a no-op. | |
211 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
212 VerifyFileAndStateAfterDoStop(); | |
213 } | |
214 | |
215 TEST_F(NetLogTempFileTest, DoStartClearsFile) { | |
216 // Verify file sizes after two consecutives start/stop are the same (even if | |
217 // we add some junk data in between). | |
218 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
219 VerifyFileAndStateAfterDoStart(); | |
220 | |
221 int64 start_file_size; | |
222 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &start_file_size)); | |
223 | |
224 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
225 VerifyFileAndStateAfterDoStop(); | |
226 | |
227 int64 stop_file_size; | |
228 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size)); | |
229 EXPECT_GE(stop_file_size, start_file_size); | |
230 | |
231 // Add some junk at the end of the file. | |
232 std::string junk_data("Hello"); | |
233 EXPECT_GT(file_util::AppendToFile( | |
234 net_export_log_, junk_data.c_str(), junk_data.size()), 0); | |
235 | |
236 int64 junk_file_size; | |
237 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &junk_file_size)); | |
238 EXPECT_GT(junk_file_size, stop_file_size); | |
239 | |
240 // Execute DO_START/DO_STOP commands and make sure the file is back to the | |
241 // size before addition of junk data. | |
242 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
243 VerifyFileAndStateAfterDoStart(); | |
244 | |
245 int64 new_start_file_size; | |
246 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_start_file_size)); | |
247 EXPECT_EQ(new_start_file_size, start_file_size); | |
248 | |
249 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
250 VerifyFileAndStateAfterDoStop(); | |
251 | |
252 int64 new_stop_file_size; | |
253 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_stop_file_size)); | |
254 EXPECT_EQ(new_stop_file_size, stop_file_size); | |
255 } | |
256 | |
257 TEST_F(NetLogTempFileTest, CheckAddEvent) { | |
258 // Add an event to |net_log_| and then test to make sure that, after we stop | |
259 // logging, the file is larger than the file created without that event. | |
260 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
261 VerifyFileAndStateAfterDoStart(); | |
262 | |
263 // Get file size without the event. | |
264 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
265 VerifyFileAndStateAfterDoStop(); | |
266 | |
267 int64 stop_file_size; | |
268 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size)); | |
269 | |
270 // Perform DO_START and add an Event and then DO_STOP and then compare | |
271 // file sizes. | |
272 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START); | |
273 VerifyFileAndStateAfterDoStart(); | |
274 | |
275 // Log an event. | |
276 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED); | |
277 | |
278 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP); | |
279 VerifyFileAndStateAfterDoStop(); | |
280 | |
281 int64 new_stop_file_size; | |
282 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_stop_file_size)); | |
283 EXPECT_GE(new_stop_file_size, stop_file_size); | |
284 } | |
OLD | NEW |