Commit e6a4f66e authored by Joon Ahn's avatar Joon Ahn Committed by Commit Bot

logs: Fix ReadEndOfFileMedium test flakes

If the first char of a full chunk happens to be '\0' it assumes the file is less than max_size and just returns chunk which is incomplete.

Bug: chromium:1099000
Change-Id: Iaebbc1d908c9c6386cbacf151f3bb8c19000fb51
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2271245Reviewed-by: default avatarAhmed Fakhry <afakhry@chromium.org>
Reviewed-by: default avatarIan Barkley-Yeung <iby@chromium.org>
Commit-Queue: Joon Ahn <joonbug@chromium.org>
Cr-Commit-Position: refs/heads/master@{#783812}
parent 79ee812b
...@@ -83,16 +83,19 @@ bool ReadEndOfFile(const base::FilePath& path, ...@@ -83,16 +83,19 @@ bool ReadEndOfFile(const base::FilePath& path,
chunk[0] = '\0'; chunk[0] = '\0';
last_chunk[0] = '\0'; last_chunk[0] = '\0';
size_t total_bytes_read = 0;
size_t bytes_read = 0; size_t bytes_read = 0;
// Since most logs are not seekable, read until the end keeping tracking of // Since most logs are not seekable, read until the end keeping tracking of
// last two chunks. // last two chunks.
while ((bytes_read = fread(chunk.get(), 1, max_size, fp.get())) == max_size) { while ((bytes_read = fread(chunk.get(), 1, max_size, fp.get())) == max_size) {
total_bytes_read += bytes_read;
last_chunk.swap(chunk); last_chunk.swap(chunk);
chunk[0] = '\0'; chunk[0] = '\0';
} }
total_bytes_read += bytes_read;
if (last_chunk[0] == '\0') { if (total_bytes_read < max_size) {
// File is smaller than max_size // File is smaller than max_size
contents->assign(chunk.get(), bytes_read); contents->assign(chunk.get(), bytes_read);
} else if (bytes_read == 0) { } else if (bytes_read == 0) {
......
...@@ -36,7 +36,7 @@ TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileSmall) { ...@@ -36,7 +36,7 @@ TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileSmall) {
const char kTestData[] = "0123456789"; // Length of 10 const char kTestData[] = "0123456789"; // Length of 10
std::string read_data; std::string read_data;
base::FilePath file_path = temp_dir_.GetPath().Append("test.txt"); base::FilePath file_path = temp_dir_.GetPath().Append("test_small.txt");
WriteFile(file_path, kTestData, strlen(kTestData)); WriteFile(file_path, kTestData, strlen(kTestData));
...@@ -61,6 +61,36 @@ TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileSmall) { ...@@ -61,6 +61,36 @@ TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileSmall) {
EXPECT_EQ("56789", read_data); EXPECT_EQ("56789", read_data);
} }
TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileWithZeros) {
const size_t test_size = 10;
std::string test_data("abcd\0\0\0\0hi", test_size);
std::string read_data;
base::FilePath file_path = temp_dir_.GetPath().Append("test_zero.txt");
WriteFile(file_path, test_data.data(), test_size);
read_data.clear();
EXPECT_TRUE(ReadEndOfFile(file_path, &read_data, 15));
EXPECT_EQ(test_data, read_data);
read_data.clear();
EXPECT_TRUE(ReadEndOfFile(file_path, &read_data, 10));
EXPECT_EQ(test_data, read_data);
read_data.clear();
EXPECT_TRUE(ReadEndOfFile(file_path, &read_data, 2));
EXPECT_EQ(test_data.substr(test_size - 2, 2), read_data);
read_data.clear();
EXPECT_TRUE(ReadEndOfFile(file_path, &read_data, 3));
EXPECT_EQ(test_data.substr(test_size - 3, 3), read_data);
read_data.clear();
EXPECT_TRUE(ReadEndOfFile(file_path, &read_data, 5));
EXPECT_EQ(test_data.substr(test_size - 5, 5), read_data);
}
TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileMedium) { TEST_F(DebugDaemonLogSourceTest, ReadEndOfFileMedium) {
std::string test_data = base::RandBytesAsString(10000); // 10KB data std::string test_data = base::RandBytesAsString(10000); // 10KB data
std::string read_data; std::string read_data;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment