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

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/values.h"
11 #include "build/build_config.h"
12 #include "chrome/browser/net/chrome_net_log.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using content::BrowserThread;
17
18 class CustomNetLogTempFile : public NetLogTempFile {
mmenke 2013/01/23 19:01:27 nit: Think "TestNetLogTempFile" or "NetLogTempFil
ramant (doing other things) 2013/01/24 03:23:55 Done.
19 public:
20 void set_lie_about_file_existence(bool lie_about_file_existence) {
21 lie_about_file_existence_ = lie_about_file_existence;
22 }
23
24 protected:
25 explicit CustomNetLogTempFile(ChromeNetLog* chrome_net_log)
26 : NetLogTempFile(chrome_net_log),
27 lie_about_file_existence_(false) {
28 }
29
30 bool GetNetExportLog(FilePath* path) {
31 bool file_exists = NetLogTempFile::GetNetExportLog(path);
32 if (lie_about_file_existence_)
33 return false;
34 return file_exists;
35 }
36
37 private:
38 friend class NetLogTempFileTest;
mmenke 2013/01/23 19:01:27 Don't think we need to friend the test, can just m
ramant (doing other things) 2013/01/24 03:23:55 Made constructor and NetExportLogExists public. D
39
40 bool lie_about_file_existence_;
41 };
42
43 class NetLogTempFileTest : public ::testing::Test {
44 protected:
45 NetLogTempFileTest() {
46 net_log_.reset(new ChromeNetLog);
47
48 file_user_blocking_thread_.reset(
49 new content::TestBrowserThread(BrowserThread::FILE_USER_BLOCKING));
50 file_user_blocking_thread_->Start();
51
52 net_log_temp_file_ = new CustomNetLogTempFile(net_log_.get());
53 }
54
55 virtual void SetUp() OVERRIDE {
56 // Get a temporary file name for unit tests.
57 ASSERT_TRUE(file_util::CreateTemporaryFile(&net_export_log_));
58
59 FilePath net_export_log_filename = net_export_log_.BaseName();
60 net_log_temp_file_->log_filename_ = net_export_log_filename.value();
mmenke 2013/01/23 19:01:27 nit: Could just move this to the constructor, and
ramant (doing other things) 2013/01/24 03:23:55 Would like to call ASSERT_TRUE to check the return
mmenke 2013/01/24 03:55:34 Didn't realize that, fine as-is, then.
61 }
62
63 virtual void TearDown() OVERRIDE {
64 // Delete the temporary file we have created.
65 ASSERT_TRUE(file_util::Delete(net_log_temp_file_->log_path(), false));
mmenke 2013/01/23 19:01:27 Suggest we use |net_export_log_| instead of net_lo
ramant (doing other things) 2013/01/24 03:23:55 Done.
66 }
67
68 CustomNetLogTempFile* net_log_temp_file_;
69 FilePath net_export_log_;
70
71 private:
72 scoped_ptr<ChromeNetLog> net_log_;
73 scoped_ptr<content::TestBrowserThread> file_user_blocking_thread_;
74 };
75
76 TEST_F(NetLogTempFileTest, InitAllowStart) {
77 net_log_temp_file_->set_lie_about_file_existence(true);
78 net_log_temp_file_->Init();
79 base::DictionaryValue* dict = net_log_temp_file_->GetState();
80 std::string state;
81 dict->GetString("state", &state);
82 EXPECT_EQ("ALLOW_START", state);
83 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START, net_log_temp_file_->state());
84 FilePath log_path = net_log_temp_file_->log_path();
85 EXPECT_EQ(net_export_log_, log_path);
86 }
87
88 TEST_F(NetLogTempFileTest, InitAllowStartOrSend) {
89 net_log_temp_file_->Init();
90 base::DictionaryValue* dict = net_log_temp_file_->GetState();
91 std::string state;
92 dict->GetString("state", &state);
93 EXPECT_EQ("ALLOW_START_SEND", state);
94 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
95 net_log_temp_file_->state());
96 FilePath log_path = net_log_temp_file_->log_path();
97 EXPECT_EQ(net_export_log_, log_path);
98 EXPECT_TRUE(file_util::PathExists(log_path));
99 }
100
101 TEST_F(NetLogTempFileTest, ProcessCommandDoStart) {
mmenke 2013/01/23 19:01:27 Not sure this test really gets us anything that Do
ramant (doing other things) 2013/01/24 03:23:55 Merged the tests into ProcessCommandDoStartAndStop
102 // Execute DO_START command via ProcessCommand and verfiy that we have
103 // transitioned to STATE_ALLOW_STOP state.
104 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
105 base::DictionaryValue* dict = net_log_temp_file_->GetState();
106 std::string state;
107 dict->GetString("state", &state);
108 EXPECT_EQ("ALLOW_STOP", state);
109 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
110 FilePath log_path = net_log_temp_file_->log_path();
111 EXPECT_EQ(net_export_log_, log_path);
112 EXPECT_TRUE(file_util::PathExists(log_path));
113 int64 file_size;
114 EXPECT_TRUE(file_util::GetFileSize(log_path, &file_size));
115 EXPECT_GT(file_size, 0);
116
117 // Execute DO_STOP so that temporary file could be deleted.
118 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
119 // TODO(rtenneti): Add check for the contents of the file, by adding a random
120 // NetLog event and verify it is there.
121 }
122
123 TEST_F(NetLogTempFileTest, ProcessCommandDoStop) {
124 // Execute DO_START command via ProcessCommand and verfiy that we transition
125 // to ALLOW_STOP state.
126 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
127 base::DictionaryValue* dict = net_log_temp_file_->GetState();
128 std::string state;
129 dict->GetString("state", &state);
mmenke 2013/01/23 19:01:27 Should use EXPECT_TRUE on calls to dict->GetString
ramant (doing other things) 2013/01/24 03:23:55 Done.
130 EXPECT_EQ("ALLOW_STOP", state);
131 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
132 FilePath log_path = net_log_temp_file_->log_path();
133 EXPECT_EQ(net_export_log_, log_path);
134 EXPECT_TRUE(file_util::PathExists(log_path));
135
136 // Execute DO_STOP command via ProcessCommand and verfiy that we have
137 // transitioned to STATE_ALLOW_START_SEND state.
138 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
139 dict = net_log_temp_file_->GetState();
140 dict->GetString("state", &state);
141 EXPECT_EQ("ALLOW_START_SEND", state);
142 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
143 net_log_temp_file_->state());
144 FilePath log_path1 = net_log_temp_file_->log_path();
145 EXPECT_EQ(net_export_log_, log_path1);
146 EXPECT_TRUE(file_util::PathExists(log_path1));
147 // TODO(rtenneti): Add check for the contents of the file, by adding a random
148 // NetLog event and verify it is there.
149 }
150
151 TEST_F(NetLogTempFileTest, DoStartClearsFile) {
152 // Execute DO_START command via ProcessCommand and verfiy that we transition
153 // to ALLOW_STOP state.
154 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
155 base::DictionaryValue* dict = net_log_temp_file_->GetState();
156 std::string state;
157 dict->GetString("state", &state);
158 EXPECT_EQ("ALLOW_STOP", state);
159 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
160 FilePath log_path = net_log_temp_file_->log_path();
161 EXPECT_EQ(net_export_log_, log_path);
162 EXPECT_TRUE(file_util::PathExists(log_path));
163 int64 start_file_size;
164 EXPECT_TRUE(file_util::GetFileSize(log_path, &start_file_size));
mmenke 2013/01/23 19:01:27 To keep the length of this test down, sugget delet
ramant (doing other things) 2013/01/24 03:23:55 Done.
165
166 // Execute DO_STOP command via ProcessCommand and verfiy that we have
167 // transitioned to STATE_ALLOW_START_SEND state.
168 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
169 dict = net_log_temp_file_->GetState();
170 dict->GetString("state", &state);
171 EXPECT_EQ("ALLOW_START_SEND", state);
172 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
173 net_log_temp_file_->state());
174 FilePath log_path1 = net_log_temp_file_->log_path();
175 EXPECT_EQ(net_export_log_, log_path1);
176 EXPECT_EQ(log_path, log_path1);
177 EXPECT_TRUE(file_util::PathExists(log_path1));
178 int64 stop_file_size;
179 EXPECT_TRUE(file_util::GetFileSize(log_path, &stop_file_size));
180 EXPECT_GE(stop_file_size, start_file_size);
181
182 // Add some junk at the end of the file.
183 std::string junk_data("Hello");
184 EXPECT_GT(file_util::AppendToFile(
185 log_path1, junk_data.c_str(), junk_data.size()), 0);
mmenke 2013/01/23 19:01:27 Ahh, you weren't following me here...What I was su
ramant (doing other things) 2013/01/24 03:23:55 Done.
186 int64 junk_file_size;
187 EXPECT_TRUE(file_util::GetFileSize(log_path1, &junk_file_size));
188 EXPECT_GT(junk_file_size, start_file_size);
189
190 // Execute DO_START command and make sure the file is back to the size before
191 // addition of junk data.
192 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
193 dict = net_log_temp_file_->GetState();
194 dict->GetString("state", &state);
195 EXPECT_EQ("ALLOW_STOP", state);
196 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_STOP, net_log_temp_file_->state());
197 FilePath log_path2 = net_log_temp_file_->log_path();
198 EXPECT_EQ(net_export_log_, log_path2);
199 EXPECT_TRUE(file_util::PathExists(log_path2));
200 int64 new_file_size;
201 EXPECT_TRUE(file_util::GetFileSize(log_path, &new_file_size));
202 EXPECT_EQ(new_file_size, start_file_size);
203
204 // Execute DO_STOP so that temporary file could be deleted.
205 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
206 }
207
208 TEST_F(NetLogTempFileTest, GetFilePath) {
mmenke 2013/01/23 19:01:27 Suggest merging this with InitAllowStart / InitAll
ramant (doing other things) 2013/01/24 03:23:55 Done.
209 // Execute DO_START and DO_STOP commands via ProcessCommand and verfiy that we
210 // have transitioned to STATE_ALLOW_START_SEND state.
211 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_START);
212 net_log_temp_file_->ProcessCommand(NetLogTempFile::DO_STOP);
213 EXPECT_EQ(NetLogTempFile::STATE_ALLOW_START_SEND,
214 net_log_temp_file_->state());
215 FilePath log_path = net_log_temp_file_->log_path();
216 EXPECT_EQ(net_export_log_, log_path);
217
218 FilePath net_export_file_path;
219 EXPECT_TRUE(net_log_temp_file_->GetFilePath(&net_export_file_path));
220 EXPECT_TRUE(file_util::PathExists(net_export_file_path));
221 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698