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

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, 11 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
mmenke 2013/01/24 03:55:34 nit: "NetLogTempFile implementation:"
ramant (doing other things) 2013/01/24 21:56:49 Done.
26 bool NetExportLogExists() {
mmenke 2013/01/24 03:55:34 Add virtual and OVERRIDE
ramant (doing other things) 2013/01/24 21:56:49 Done.
27 if (lie_about_file_existence_)
28 return false;
29 return NetLogTempFile::NetExportLogExists();
30 }
31
32 void set_lie_about_file_existence(bool lie_about_file_existence) {
33 lie_about_file_existence_ = lie_about_file_existence;
34 }
35
36 private:
37 bool lie_about_file_existence_;
38 };
39
40 class NetLogTempFileTest : public ::testing::Test {
41 protected:
42 NetLogTempFileTest()
43 : file_user_blocking_thread_(BrowserThread::FILE_USER_BLOCKING,
44 &message_loop_) {
45 net_log_.reset(new ChromeNetLog);
46 net_log_temp_file_ = new TestNetLogTempFile(net_log_.get());
47 }
48
mmenke 2013/01/24 03:55:34 nit: ::testing::Test implementation:
ramant (doing other things) 2013/01/24 21:56:49 Done.
49 virtual void SetUp() OVERRIDE {
50 // Get a temporary file name for unit tests.
51 ASSERT_TRUE(file_util::CreateTemporaryFile(&net_export_log_));
mmenke 2013/01/24 03:55:34 Are we guaranteed this will be in the file_util::G
ramant (doing other things) 2013/01/24 21:56:49 Good point. Added GetNetExportLogDirectory (to ge
52
53 net_log_temp_file_->log_filename_ = net_export_log_.BaseName().value();
54 }
55
56 virtual void TearDown() OVERRIDE {
57 // Delete the temporary file we have created.
58 ASSERT_TRUE(file_util::Delete(net_export_log_, false));
59 }
60
61 scoped_ptr<ChromeNetLog> net_log_;
62 TestNetLogTempFile* net_log_temp_file_;
63 FilePath net_export_log_;
64
65 private:
66 MessageLoop message_loop_;
67 content::TestBrowserThread file_user_blocking_thread_;
68 };
69
70 TEST_F(NetLogTempFileTest, InitAllowStart) {
71 net_log_temp_file_->set_lie_about_file_existence(true);
72 net_log_temp_file_->Init();
73
74 base::DictionaryValue* dict = net_log_temp_file_->GetState();
75 std::string state;
76 EXPECT_TRUE(dict->GetString("state", &state));
77 EXPECT_EQ("ALLOW_START", state);
78 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state());
79
80 FilePath net_export_file_path;
81 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
82 EXPECT_FALSE(net_log_temp_file_->NetExportLogExists());
83 }
84
85 TEST_F(NetLogTempFileTest, InitAllowStartOrSend) {
86 net_log_temp_file_->Init();
87
88 base::DictionaryValue* dict = net_log_temp_file_->GetState();
89 std::string state;
90 EXPECT_TRUE(dict->GetString("state", &state));
91 EXPECT_EQ("ALLOW_START_SEND", state);
92 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
93 net_log_temp_file_->state());
94 EXPECT_TRUE(file_util::PathExists(net_export_log_));
mmenke 2013/01/24 03:55:34 So we can't rely on path equality, as a string, on
ramant (doing other things) 2013/01/24 21:56:49 Correct.
95
96 FilePath net_export_file_path;
97 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
98 EXPECT_TRUE(file_util::PathExists(net_export_file_path));
99 }
100
101 TEST_F(NetLogTempFileTest, ProcessCommandDoStartAndStop) {
102 // Execute DO_START command via ProcessCommand and verfiy that we transition
103 // to ALLOW_STOP state.
104 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
105 base::DictionaryValue* dict = net_log_temp_file_->GetState();
mmenke 2013/01/24 03:55:34 These are leaks. Should store these in scoped_ptr
ramant (doing other things) 2013/01/24 21:56:49 Many thanks for catching this. Done.
106 std::string state;
107 EXPECT_TRUE(dict->GetString("state", &state));
108 EXPECT_EQ("ALLOW_STOP", state);
109 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
110 EXPECT_TRUE(file_util::PathExists(net_export_log_));
111 int64 file_size;
112 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &file_size));
113 EXPECT_GT(file_size, 0);
114
115 FilePath net_export_file_path;
116 EXPECT_FALSE(net_log_temp_file_->GetFilePath(&net_export_file_path));
117
118 // Execute DO_STOP command via ProcessCommand and verfiy that we have
119 // transitioned to STATE_ALLOW_START_SEND state.
120 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
121 dict = net_log_temp_file_->GetState();
122 EXPECT_TRUE(dict->GetString("state", &state));
123 EXPECT_EQ("ALLOW_START_SEND", state);
124 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
125 net_log_temp_file_->state());
126 EXPECT_TRUE(file_util::PathExists(net_export_log_));
127
128 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
129 EXPECT_TRUE(file_util::PathExists(net_export_file_path));
130
131 // TODO(rtenneti): Add check for the contents of the file, by adding a random
132 // NetLog event and verify it is there.
133 }
134
135 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
136 // Execute DO_START command via ProcessCommand and verfiy that we transition
137 // to ALLOW_STOP state.
138 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
139 base::DictionaryValue* dict = net_log_temp_file_->GetState();
140 std::string state;
141 EXPECT_TRUE(dict->GetString("state", &state));
142 EXPECT_EQ("ALLOW_STOP", state);
143 int64 start_file_size;
144 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &start_file_size));
145
146 // Execute DO_STOP command via ProcessCommand and verfiy that we have
147 // transitioned to STATE_ALLOW_START_SEND state.
148 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
149 dict = net_log_temp_file_->GetState();
150 EXPECT_TRUE(dict->GetString("state", &state));
151 EXPECT_EQ("ALLOW_START_SEND", state);
152 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
153 net_log_temp_file_->state());
154 int64 stop_file_size;
155 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size));
156 EXPECT_GE(stop_file_size, start_file_size);
157
158 // Add some junk at the end of the file.
159 std::string junk_data("Hello");
160 EXPECT_GT(file_util::AppendToFile(
161 net_export_log_, junk_data.c_str(), junk_data.size()), 0);
162 int64 junk_file_size;
163 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &junk_file_size));
164 EXPECT_GT(junk_file_size, start_file_size);
165
166 // Execute DO_START command and make sure the file is back to the size before
167 // addition of junk data.
168 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
169 dict = net_log_temp_file_->GetState();
170 EXPECT_TRUE(dict->GetString("state", &state));
171 EXPECT_EQ("ALLOW_STOP", state);
172 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
173 int64 new_file_size;
174 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_file_size));
175 EXPECT_EQ(new_file_size, start_file_size);
176
177 // Execute DO_STOP so that temporary file could be deleted.
178 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
179 }
180
181 TEST_F(NetLogTempFileTest, CheckAddEvent) {
182 // Add an event to |net_log_| and then test to make sure that, after we stop
183 // logging, the file is larger than the file created without that event.
184
185 // Perform DO_START/DO_STOP and get the file size.
186 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
187 base::DictionaryValue* dict = net_log_temp_file_->GetState();
188 std::string state;
189 EXPECT_TRUE(dict->GetString("state", &state));
190 EXPECT_EQ("ALLOW_STOP", state);
191
192 // Get file size without the event.
193 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
194 int64 stop_file_size;
195 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &stop_file_size));
196
197 // Perform DO_START and add an Event and then DO_STOP and then compare
198 // file sizes.
199 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
200
201 // Log an event.
202 net_log_->AddGlobalEntry(net::NetLog::TYPE_CANCELLED);
203
204 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
205 int64 new_stop_file_size;
206 EXPECT_TRUE(file_util::GetFileSize(net_export_log_, &new_stop_file_size));
mmenke 2013/01/24 03:55:34 Are we guaranteed that file_util will see the larg
ramant (doing other things) 2013/01/24 21:56:49 We delete net_log_logger in DoStop, which closes t
207 EXPECT_GE(new_stop_file_size, stop_file_size);
208 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698