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

Side by Side Diff: chrome/browser/chromeos/system_logs/single_log_source_unittest.cc

Issue 2930383002: SingleLogSource reads whole lines whenever possible (Closed)
Patch Set: Created 3 years, 6 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
« no previous file with comments | « chrome/browser/chromeos/system_logs/single_log_source.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/system_logs/single_log_source.h" 5 #include "chrome/browser/chromeos/system_logs/single_log_source.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 source_->log_file_dir_path_ = log_dir_.GetPath(); 56 source_->log_file_dir_path_ = log_dir_.GetPath();
57 log_file_path_ = source_->log_file_dir_path_.Append(source_->source_name()); 57 log_file_path_ = source_->log_file_dir_path_.Append(source_->source_name());
58 ASSERT_TRUE(base::PathExists(log_file_path_)) << log_file_path_.value(); 58 ASSERT_TRUE(base::PathExists(log_file_path_)) << log_file_path_.value();
59 } 59 }
60 60
61 // Writes/appends (respectively) a string |input| to file indicated by 61 // Writes/appends (respectively) a string |input| to file indicated by
62 // |relative_path| under |log_dir_|. 62 // |relative_path| under |log_dir_|.
63 bool WriteFile(const base::FilePath& relative_path, 63 bool WriteFile(const base::FilePath& relative_path,
64 const std::string& input) { 64 const std::string& input) {
65 return base::WriteFile(log_dir_.GetPath().Append(relative_path), 65 return base::WriteFile(log_dir_.GetPath().Append(relative_path),
66 input.data(), input.size()); 66 input.data(),
67 input.size()) == static_cast<int>(input.size());
67 } 68 }
68 bool AppendToFile(const base::FilePath& relative_path, 69 bool AppendToFile(const base::FilePath& relative_path,
69 const std::string& input) { 70 const std::string& input) {
70 return base::AppendToFile(log_dir_.GetPath().Append(relative_path), 71 return base::AppendToFile(log_dir_.GetPath().Append(relative_path),
71 input.data(), input.size()); 72 input.data(), input.size());
72 } 73 }
73 74
74 // Calls source_.Fetch() to start a logs fetch operation. Passes in 75 // Calls source_.Fetch() to start a logs fetch operation. Passes in
75 // OnFileRead() as a callback. Runs until Fetch() has completed. 76 // OnFileRead() as a callback. Runs until Fetch() has completed.
76 void FetchFromSource() { 77 void FetchFromSource() {
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 EXPECT_EQ(2, num_callback_calls()); 231 EXPECT_EQ(2, num_callback_calls());
231 EXPECT_EQ("", latest_response()); 232 EXPECT_EQ("", latest_response());
232 233
233 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "hijk\n")); 234 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "hijk\n"));
234 FetchFromSource(); 235 FetchFromSource();
235 236
236 EXPECT_EQ(3, num_callback_calls()); 237 EXPECT_EQ(3, num_callback_calls());
237 // All the previously written text should be read this time. 238 // All the previously written text should be read this time.
238 EXPECT_EQ("0123456789abcdefghijk\n", latest_response()); 239 EXPECT_EQ("0123456789abcdefghijk\n", latest_response());
239 240
240 // Partial whole-line reads are not supported. The last byte of the read must 241 // Check ability to read whole lines while leaving the remainder for later.
241 // be a new line.
242 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "Hello world\n")); 242 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "Hello world\n"));
243 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "Goodbye world")); 243 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "Goodbye world"));
244 FetchFromSource(); 244 FetchFromSource();
245 245
246 EXPECT_EQ(4, num_callback_calls()); 246 EXPECT_EQ(4, num_callback_calls());
247 EXPECT_EQ("", latest_response()); 247 EXPECT_EQ("Hello world\n", latest_response());
248 248
249 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "\n")); 249 EXPECT_TRUE(AppendToFile(base::FilePath("messages"), "\n"));
250 FetchFromSource(); 250 FetchFromSource();
251 251
252 EXPECT_EQ(5, num_callback_calls()); 252 EXPECT_EQ(5, num_callback_calls());
253 EXPECT_EQ("Hello world\nGoodbye world\n", latest_response()); 253 EXPECT_EQ("Goodbye world\n", latest_response());
254 } 254 }
255 255
256 TEST_F(SingleLogSourceTest, Anonymize) { 256 TEST_F(SingleLogSourceTest, Anonymize) {
257 InitializeSource(SingleLogSource::SupportedSource::kUiLatest); 257 InitializeSource(SingleLogSource::SupportedSource::kUiLatest);
258 258
259 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"), 259 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"),
260 "My MAC address is: 11:22:33:44:55:66\n")); 260 "My MAC address is: 11:22:33:44:55:66\n"));
261 FetchFromSource(); 261 FetchFromSource();
262 262
263 EXPECT_EQ(1, num_callback_calls()); 263 EXPECT_EQ(1, num_callback_calls());
264 EXPECT_EQ("My MAC address is: 11:22:33:00:00:01\n", latest_response()); 264 EXPECT_EQ("My MAC address is: 11:22:33:00:00:01\n", latest_response());
265 265
266 // Suppose the write operation is not atomic, and the MAC address is written 266 // Suppose the write operation is not atomic, and the MAC address is written
267 // across two separate writes. 267 // across two separate writes.
268 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"), 268 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"),
269 "Your MAC address is: AB:88:C")); 269 "Your MAC address is: AB:88:C"));
270 FetchFromSource(); 270 FetchFromSource();
271 271
272 EXPECT_EQ(2, num_callback_calls()); 272 EXPECT_EQ(2, num_callback_calls());
273 EXPECT_EQ("", latest_response()); 273 EXPECT_EQ("", latest_response());
274 274
275 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"), "D:99:EF:77\n")); 275 EXPECT_TRUE(AppendToFile(base::FilePath("ui/ui.LATEST"), "D:99:EF:77\n"));
276 FetchFromSource(); 276 FetchFromSource();
277 277
278 EXPECT_EQ(3, num_callback_calls()); 278 EXPECT_EQ(3, num_callback_calls());
279 EXPECT_EQ("Your MAC address is: ab:88:cd:00:00:02\n", latest_response()); 279 EXPECT_EQ("Your MAC address is: ab:88:cd:00:00:02\n", latest_response());
280 } 280 }
281 281
282 } // namespace system_logs 282 } // namespace system_logs
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/system_logs/single_log_source.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698