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

Side by Side Diff: chrome/browser/net/net_log_temp_file_unittest.cc

Issue 11828036: First cut at UI for saving net_logs data into a temporary file on (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 7 years, 10 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 | Annotate | Revision Log
OLDNEW
(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_file_existence_(false) {
24 }
25
26 // NetLogTempFile implementation:
27 virtual bool NetExportLogExists() OVERRIDE {
28 if (lie_about_file_existence_)
29 return false;
30 return NetLogTempFile::NetExportLogExists();
31 }
32
33 void set_lie_about_file_existence(bool lie_about_file_existence) {
34 lie_about_file_existence_ = lie_about_file_existence;
35 }
36
37 private:
38 bool lie_about_file_existence_;
39 };
40
41 class NetLogTempFileTest : public ::testing::Test {
42 protected:
43 NetLogTempFileTest()
44 : file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
45 &message_loop_) {
46 net_log_.reset(new ChromeNetLog);
47 net_log_temp_file_ = new TestNetLogTempFile(net_log_.get());
mmenke 2013/01/25 17:39:12 Can put these in the initializer list.
ramant (doing other things) 2013/01/25 20:05:24 Done.
48 }
49
50 // ::testing::Test implementation:
51 virtual void SetUp() OVERRIDE {
52 // Get a temporary file name for unit tests.
53 FilePath net_log_dir;
54 ASSERT_TRUE(net_log_temp_file_->GetNetExportLogDirectory(&net_log_dir));
55 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(net_log_dir,
56 &net_export_log_));
57
58 net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value();
59
60 // We can't compare entire paths because GetTemporaryFile on Windows may
61 // convert the path to include legacy 8.3 filenames. Get
62 // net_log_temp_file_->log_path_ to test the path during unit tests.
63 net_log_temp_file_->GetNetExportLog();
64 net_export_log_ = net_log_temp_file_->log_path_;
65 }
66
67 // ::testing::Test implementation:
mmenke 2013/01/25 17:39:12 nit: Not needed. Generally just have one comment
ramant (doing other things) 2013/01/25 20:05:24 Done.
68 virtual void TearDown() OVERRIDE {
69 // Delete the temporary file we have created.
70 ASSERT_TRUE(file_util::Delete(net_export_log_, false));
71 }
72
73 std::string GetStateString() const {
74 scoped_ptr<base::DictionaryValue> dict(net_log_temp_file_->GetState());
75 std::string state;
76 EXPECT_TRUE(dict->GetString("state", &state));
77 return state;
78 }
79
80 void VerifyNetExportLog() {
mmenke 2013/01/25 17:39:12 Think the three "verify" functions are worth comme
ramant (doing other things) 2013/01/25 20:05:24 Done.
81 // We can't compare entire paths because GetTemporaryFile on Windows may
82 // convert the path to include legacy 8.3 filenames.
mmenke 2013/01/25 17:39:12 Looks like we are comparing entire paths here now.
ramant (doing other things) 2013/01/25 20:05:24 Deleted the comment. Done.
83 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
84 EXPECT_TRUE(file_util::PathExists(net_export_log_));
85 int64 file_size;
86 // file_util::GetFileSize returns proper file size on open handles.
87 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &file_size));
88 EXPECT_GT(file_size, 0);
89 }
90
91 void VerifyStateAfterDoStart() {
mmenke 2013/01/25 17:39:12 Suggest "VertifyFileAndStateAfterDoStart()", and t
ramant (doing other things) 2013/01/25 20:05:24 Done.
92 // Verfiy that we have transitioned to STATE_ALLOW_STOP state.
93 EXPECT_EQ("ALLOW_STOP", GetStateString());
94 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
95 VerifyNetExportLog();
96 }
97
98 void VerifyStateAfterDoStop() {
99 // Verfiy that we have transitioned to STATE_ALLOW_START_SEND state.
100 EXPECT_EQ("ALLOW_START_SEND", GetStateString());
101 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
102 net_log_temp_file_->state());
103 VerifyNetExportLog();
104 }
105
106 scoped_ptr<ChromeNetLog> net_log_;
107 TestNetLogTempFile* net_log_temp_file_;
mmenke 2013/01/25 17:39:12 Should have a note that this must go after the net
mmenke 2013/01/25 17:39:12 This should be a scoped_ptr. Currently, it leaks.
ramant (doing other things) 2013/01/25 20:05:24 Done.
ramant (doing other things) 2013/01/25 20:05:24 Done.
108 FilePath net_export_log_;
109
110 private:
111 MessageLoop message_loop_;
112 content::TestBrowserThread file_user_blocking_thread_;
113 };
114
115 TEST_F(NetLogTempFileTest, InitAllowStart) {
116 net_log_temp_file_->set_lie_about_file_existence(true);
117 EXPECT_TRUE(net_log_temp_file_->Init());
118
119 EXPECT_EQ("ALLOW_START", GetStateString());
120 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state());
121
122 FilePath net_export_file_path;
123 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
124 EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
125 }
126
127 TEST_F(NetLogTempFileTest, InitAllowStartOrSend) {
128 EXPECT_TRUE(net_log_temp_file_->Init());
129
130 EXPECT_EQ("ALLOW_START_SEND", GetStateString());
131 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
132 net_log_temp_file_->state());
133 EXPECT_EQ(net_export_log_, net_log_temp_file_->log_path_);
134 EXPECT_TRUE(file_util::PathExists(net_export_log_));
135
136 FilePath net_export_file_path;
137 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
138 EXPECT_TRUE(file_util::PathExists(net_export_file_path));
139 }
140
141 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
142 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
143 VerifyStateAfterDoStart();
144
145 // Check GetFilePath returns false, if we are still writing to file.
146 FilePath net_export_file_path;
147 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
148
149 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
150 VerifyStateAfterDoStop();
151
152 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
153 EXPECT_TRUE(file_util::PathExists(net_export_file_path));
154
155 // TODO(rtenneti): Add check for the contents of the file, by adding a random
156 // NetLog event and verify it is there.
157 }
158
159 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
160 // Verify file sizes after two consecutives start/stop are the same (even if
161 // we add some junk data in between).
162 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
163 VerifyStateAfterDoStart();
164
165 int64 start_file_size;
166 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &start_file_size));
167
168 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
169 VerifyStateAfterDoStop();
170
171 int64 stop_file_size;
172 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size));
173 EXPECT_GE(stop_file_size, start_file_size);
174
175 // Add some junk at the end of the file.
176 std::string junk_data("Hello");
177 EXPECT_GT(file_util::AppendToFile(
178 net_export_log_, junk_data.c_str(), junk_data.size()), 0);
179
180 int64 junk_file_size;
181 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &junk_file_size));
182 EXPECT_GT(junk_file_size, stop_file_size);
183
184 // Execute DO_START/DO_STOP commands and make sure the file is back to the
185 // size before addition of junk data.
186 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
187 VerifyStateAfterDoStart();
188
189 int64 new_start_file_size;
190 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_start_file_size));
191 EXPECT_EQ(new_start_file_size, start_file_size);
192
193 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
194 VerifyStateAfterDoStop();
195
196 int64 new_stop_file_size;
197 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_stop_file_size));
198 EXPECT_EQ(new_stop_file_size, stop_file_size);
199 }
200
201 TEST_F(NetLogTempFileTest, CheckAddEvent) {
202 // Add an event to |net_log_| and then test to make sure that, after we stop
203 // logging, the file is larger than the file created without that event.
204 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
205 VerifyStateAfterDoStart();
206
207 // Get file size without the event.
208 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
209 VerifyStateAfterDoStop();
210
211 int64 stop_file_size;
212 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size));
213
214 // Perform DO_START and add an Event and then DO_STOP and then compare
215 // file sizes.
216 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
217 VerifyStateAfterDoStart();
218
219 // Log an event.
220 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
221
222 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
223 VerifyStateAfterDoStop();
224
225 int64 new_stop_file_size;
226 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_stop_file_size));
227 EXPECT_GE(new_stop_file_size, stop_file_size);
228 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698