Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / net / net_log_temp_file_unittest.cc
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/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/message_loop/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   bool GetNetExportLogDirectory(base::FilePath* path) override {
29     if (lie_about_net_export_log_directory_)
30       return false;
31     return NetLogTempFile::GetNetExportLogDirectory(path);
32   }
33
34   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  public:
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   void SetUp() override {
65     // Get a temporary file name for unit tests.
66     base::FilePath net_log_dir;
67     ASSERT_TRUE(net_log_temp_file_->GetNetExportLogDirectory(&net_log_dir));
68     ASSERT_TRUE(base::CreateTemporaryFileInDir(net_log_dir, &net_export_log_));
69
70     net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value();
71
72     // CreateTemporaryFileInDir may return a legacy 8.3 file name on windows.
73     // Need to use the original directory name for string comparisons.
74     ASSERT_TRUE(net_log_temp_file_->GetNetExportLog());
75     net_export_log_ = net_log_temp_file_->log_path_;
76     ASSERT_FALSE(net_export_log_.empty());
77   }
78
79   void TearDown() override {
80     // Delete the temporary file we have created.
81     ASSERT_TRUE(base::DeleteFile(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   std::string GetLogTypeString() const {
92     scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
93     std::string log_type;
94     EXPECT_TRUE(dict->GetString("logType", &log_type));
95     return log_type;
96   }
97
98   // Make sure the export file has been created and is non-empty, as net
99   // constants will always be written to it on creation.
100   void VerifyNetExportLog() {
101     EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
102     EXPECT_TRUE(base::PathExists(net_export_log_));
103
104     int64 file_size;
105     // base::GetFileSize returns proper file size on open handles.
106     EXPECT_TRUE(base::GetFileSize(net_export_log_, &file_size));
107     EXPECT_GT(file_size, 0);
108   }
109
110   // Verify state and GetFilePath return correct values if EnsureInit() fails.
111   void VerifyFilePathAndStateAfterEnsureInitFailure() {
112     EXPECT_EQ("UNINITIALIZED", GetStateString());
113     EXPECT_EQ(NetLogTempFile::STATE_UNINITIALIZED,
114               net_log_temp_file_->state());
115
116     base::FilePath net_export_file_path;
117     EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
118   }
119
120   // When we lie in NetExportLogExists, make sure state and GetFilePath return
121   // correct values.
122   void VerifyFilePathAndStateAfterEnsureInit() {
123     EXPECT_EQ("NOT_LOGGING", GetStateString());
124     EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
125     EXPECT_EQ("NONE", GetLogTypeString());
126     EXPECT_EQ(NetLogTempFile::LOG_TYPE_NONE, net_log_temp_file_->log_type());
127
128     base::FilePath net_export_file_path;
129     EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
130     EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
131   }
132
133   // Make sure the export file has been successfully initialized.
134   void VerifyFileAndStateAfterDoStart() {
135     EXPECT_EQ("LOGGING", GetStateString());
136     EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state());
137     EXPECT_EQ("NORMAL", GetLogTypeString());
138     EXPECT_EQ(NetLogTempFile::LOG_TYPE_NORMAL, net_log_temp_file_->log_type());
139
140     // Check GetFilePath returns false, if we are still writing to file.
141     base::FilePath net_export_file_path;
142     EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
143
144     VerifyNetExportLog();
145     EXPECT_EQ(net::NetLog::LOG_ALL_BUT_BYTES, net_log_->GetLogLevel());
146   }
147
148   // Make sure the export file has been successfully initialized.
149   void VerifyFileAndStateAfterDoStartStripPrivateData() {
150     EXPECT_EQ("LOGGING", GetStateString());
151     EXPECT_EQ(NetLogTempFile::STATE_LOGGING, net_log_temp_file_->state());
152     EXPECT_EQ("STRIP_PRIVATE_DATA", GetLogTypeString());
153     EXPECT_EQ(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
154               net_log_temp_file_->log_type());
155
156     // Check GetFilePath returns false, if we are still writing to file.
157     base::FilePath net_export_file_path;
158     EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
159
160     VerifyNetExportLog();
161     EXPECT_EQ(net::NetLog::LOG_STRIP_PRIVATE_DATA, net_log_->GetLogLevel());
162   }
163
164   // Make sure the export file has been successfully initialized.
165   void VerifyFileAndStateAfterDoStop() {
166     EXPECT_EQ("NOT_LOGGING", GetStateString());
167     EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
168     EXPECT_EQ("NORMAL", GetLogTypeString());
169     EXPECT_EQ(NetLogTempFile::LOG_TYPE_NORMAL, net_log_temp_file_->log_type());
170
171     base::FilePath net_export_file_path;
172     EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
173     EXPECT_TRUE(base::PathExists(net_export_file_path));
174     EXPECT_EQ(net_export_log_, net_export_file_path);
175
176     VerifyNetExportLog();
177   }
178
179   // Make sure the export file has been successfully initialized.
180   void VerifyFileAndStateAfterDoStopWithStripPrivateData() {
181     EXPECT_EQ("NOT_LOGGING", GetStateString());
182     EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
183     EXPECT_EQ("STRIP_PRIVATE_DATA", GetLogTypeString());
184     EXPECT_EQ(NetLogTempFile::LOG_TYPE_STRIP_PRIVATE_DATA,
185               net_log_temp_file_->log_type());
186
187     base::FilePath net_export_file_path;
188     EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
189     EXPECT_TRUE(base::PathExists(net_export_file_path));
190     EXPECT_EQ(net_export_log_, net_export_file_path);
191
192     VerifyNetExportLog();
193   }
194
195   scoped_ptr<ChromeNetLog> net_log_;
196   // |net_log_temp_file_| is initialized after |net_log_| so that it can stop
197   // obvserving on destruction.
198   scoped_ptr<TestNetLogTempFile> net_log_temp_file_;
199   base::FilePath net_export_log_;
200
201  private:
202   base::MessageLoop message_loop_;
203   content::TestBrowserThread file_user_blocking_thread_;
204 };
205
206 TEST_F(NetLogTempFileTest, EnsureInitFailure) {
207   net_log_temp_file_->set_lie_about_net_export_log_directory(true);
208
209   EXPECT_FALSE(net_log_temp_file_->EnsureInit());
210   VerifyFilePathAndStateAfterEnsureInitFailure();
211
212   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
213   VerifyFilePathAndStateAfterEnsureInitFailure();
214 }
215
216 TEST_F(NetLogTempFileTest, EnsureInitAllowStart) {
217   net_log_temp_file_->set_lie_about_file_existence(true);
218
219   EXPECT_TRUE(net_log_temp_file_->EnsureInit());
220   VerifyFilePathAndStateAfterEnsureInit();
221
222   // Calling EnsureInit() second time should be a no-op.
223   EXPECT_TRUE(net_log_temp_file_->EnsureInit());
224   VerifyFilePathAndStateAfterEnsureInit();
225 }
226
227 TEST_F(NetLogTempFileTest, EnsureInitAllowStartOrSend) {
228   EXPECT_TRUE(net_log_temp_file_->EnsureInit());
229
230   EXPECT_EQ("NOT_LOGGING", GetStateString());
231   EXPECT_EQ(NetLogTempFile::STATE_NOT_LOGGING, net_log_temp_file_->state());
232   EXPECT_EQ("UNKNOWN", GetLogTypeString());
233   EXPECT_EQ(NetLogTempFile::LOG_TYPE_UNKNOWN, net_log_temp_file_->log_type());
234   EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
235   EXPECT_TRUE(base::PathExists(net_export_log_));
236
237   base::FilePath net_export_file_path;
238   EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
239   EXPECT_TRUE(base::PathExists(net_export_file_path));
240   EXPECT_EQ(net_export_log_, net_export_file_path);
241
242   // GetFilePath should return false if NetExportLogExists() fails.
243   net_log_temp_file_->set_lie_about_file_existence(true);
244   EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
245 }
246
247 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
248   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
249   VerifyFileAndStateAfterDoStart();
250
251   // Calling DO_START second time should be a no-op.
252   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
253   VerifyFileAndStateAfterDoStart();
254
255   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
256   VerifyFileAndStateAfterDoStop();
257
258   // Calling DO_STOP second time should be a no-op.
259   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
260   VerifyFileAndStateAfterDoStop();
261 }
262
263 TEST_F(NetLogTempFileTest,
264        ProcessCommandDoStartAndStopWithPrivateDataStripping) {
265   net_log_temp_file_->ProcessCommand(
266       NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
267   VerifyFileAndStateAfterDoStartStripPrivateData();
268
269   // Calling DO_START_STRIP_PRIVATE_DATA second time should be a no-op.
270   net_log_temp_file_->ProcessCommand(
271       NetLogTempFile::DO_START_STRIP_PRIVATE_DATA);
272   VerifyFileAndStateAfterDoStartStripPrivateData();
273
274   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
275   VerifyFileAndStateAfterDoStopWithStripPrivateData();
276
277   // Calling DO_STOP second time should be a no-op.
278   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
279   VerifyFileAndStateAfterDoStopWithStripPrivateData();
280 }
281
282 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
283   // Verify file sizes after two consecutives start/stop are the same (even if
284   // we add some junk data in between).
285   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
286   VerifyFileAndStateAfterDoStart();
287
288   int64 start_file_size;
289   EXPECT_TRUE(base::GetFileSize(net_export_log_, &start_file_size));
290
291   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
292   VerifyFileAndStateAfterDoStop();
293
294   int64 stop_file_size;
295   EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
296   EXPECT_GE(stop_file_size, start_file_size);
297
298   // Add some junk at the end of the file.
299   std::string junk_data("Hello");
300   EXPECT_TRUE(base::AppendToFile(net_export_log_, junk_data.c_str(),
301                                  junk_data.size()));
302
303   int64 junk_file_size;
304   EXPECT_TRUE(base::GetFileSize(net_export_log_, &junk_file_size));
305   EXPECT_GT(junk_file_size, stop_file_size);
306
307   // Execute DO_START/DO_STOP commands and make sure the file is back to the
308   // size before addition of junk data.
309   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
310   VerifyFileAndStateAfterDoStart();
311
312   int64 new_start_file_size;
313   EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_start_file_size));
314   EXPECT_EQ(new_start_file_size, start_file_size);
315
316   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
317   VerifyFileAndStateAfterDoStop();
318
319   int64 new_stop_file_size;
320   EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
321   EXPECT_EQ(new_stop_file_size, stop_file_size);
322 }
323
324 TEST_F(NetLogTempFileTest, CheckAddEvent) {
325   // Add an event to |net_log_| and then test to make sure that, after we stop
326   // logging, the file is larger than the file created without that event.
327   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
328   VerifyFileAndStateAfterDoStart();
329
330   // Get file size without the event.
331   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
332   VerifyFileAndStateAfterDoStop();
333
334   int64 stop_file_size;
335   EXPECT_TRUE(base::GetFileSize(net_export_log_, &stop_file_size));
336
337   // Perform DO_START and add an Event and then DO_STOP and then compare
338   // file sizes.
339   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
340   VerifyFileAndStateAfterDoStart();
341
342   // Log an event.
343   net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
344
345   net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
346   VerifyFileAndStateAfterDoStop();
347
348   int64 new_stop_file_size;
349   EXPECT_TRUE(base::GetFileSize(net_export_log_, &new_stop_file_size));
350   EXPECT_GE(new_stop_file_size, stop_file_size);
351 }