feedback_util_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2020 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. #include "components/feedback/feedback_util.h"
  5. #include <string>
  6. #include "base/files/file_util.h"
  7. #include "base/files/scoped_file.h"
  8. #include "base/files/scoped_temp_dir.h"
  9. #include "base/rand_util.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace feedback_util {
  12. // Note: This file is excluded from win build.
  13. // See https://crbug.com/1119560.
  14. class FeedbackUtilTest : public ::testing::Test {
  15. public:
  16. void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); }
  17. base::ScopedTempDir temp_dir_;
  18. };
  19. TEST_F(FeedbackUtilTest, ReadEndOfFileEmpty) {
  20. std::string read_data("should be erased");
  21. base::FilePath file_path = temp_dir_.GetPath().Append("test_empty.txt");
  22. WriteFile(file_path, "", 0);
  23. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 10, &read_data));
  24. EXPECT_EQ(0u, read_data.length());
  25. }
  26. TEST_F(FeedbackUtilTest, ReadEndOfFileSmall) {
  27. const char kTestData[] = "0123456789"; // Length of 10
  28. std::string read_data;
  29. base::FilePath file_path = temp_dir_.GetPath().Append("test_small.txt");
  30. WriteFile(file_path, kTestData, strlen(kTestData));
  31. read_data.clear();
  32. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 15, &read_data));
  33. EXPECT_EQ(kTestData, read_data);
  34. read_data.clear();
  35. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 10, &read_data));
  36. EXPECT_EQ(kTestData, read_data);
  37. read_data.clear();
  38. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 2, &read_data));
  39. EXPECT_EQ("89", read_data);
  40. read_data.clear();
  41. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 3, &read_data));
  42. EXPECT_EQ("789", read_data);
  43. read_data.clear();
  44. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 5, &read_data));
  45. EXPECT_EQ("56789", read_data);
  46. }
  47. TEST_F(FeedbackUtilTest, ReadEndOfFileWithZeros) {
  48. const size_t test_size = 10;
  49. std::string test_data("abcd\0\0\0\0hi", test_size);
  50. std::string read_data;
  51. base::FilePath file_path = temp_dir_.GetPath().Append("test_zero.txt");
  52. WriteFile(file_path, test_data.data(), test_size);
  53. read_data.clear();
  54. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 15, &read_data));
  55. EXPECT_EQ(test_data, read_data);
  56. read_data.clear();
  57. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 10, &read_data));
  58. EXPECT_EQ(test_data, read_data);
  59. read_data.clear();
  60. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 2, &read_data));
  61. EXPECT_EQ(test_data.substr(test_size - 2, 2), read_data);
  62. read_data.clear();
  63. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 3, &read_data));
  64. EXPECT_EQ(test_data.substr(test_size - 3, 3), read_data);
  65. read_data.clear();
  66. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 5, &read_data));
  67. EXPECT_EQ(test_data.substr(test_size - 5, 5), read_data);
  68. }
  69. TEST_F(FeedbackUtilTest, ReadEndOfFileMedium) {
  70. std::string test_data = base::RandBytesAsString(10000); // 10KB data
  71. std::string read_data;
  72. const size_t test_size = test_data.length();
  73. base::FilePath file_path = temp_dir_.GetPath().Append("test_med.txt");
  74. WriteFile(file_path, test_data.data(), test_size);
  75. read_data.clear();
  76. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 15000, &read_data));
  77. EXPECT_EQ(test_data, read_data);
  78. read_data.clear();
  79. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 10000, &read_data));
  80. EXPECT_EQ(test_data, read_data);
  81. read_data.clear();
  82. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 1000, &read_data));
  83. EXPECT_EQ(test_data.substr(test_size - 1000, 1000), read_data);
  84. read_data.clear();
  85. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 300, &read_data));
  86. EXPECT_EQ(test_data.substr(test_size - 300, 300), read_data);
  87. read_data.clear();
  88. EXPECT_TRUE(feedback_util::ReadEndOfFile(file_path, 175, &read_data));
  89. EXPECT_EQ(test_data.substr(test_size - 175, 175), read_data);
  90. }
  91. } // namespace feedback_util