| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/rand_util.h" | 8 #include "base/rand_util.h" |
| 9 #include "chromecast/crash/linux/dummy_minidump_generator.h" | 9 #include "chromecast/crash/linux/dummy_minidump_generator.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace chromecast { | 12 namespace chromecast { |
| 13 | 13 |
| 14 namespace { |
| 15 // This value should stay in sync with the internal buffer size used in |
| 16 // CopyAndDelete(). |
| 17 const int kInternalBufferSize = 32768; |
| 18 } // namespace |
| 19 |
| 14 TEST(DummyMinidumpGeneratorTest, GenerateFailsWithInvalidPath) { | 20 TEST(DummyMinidumpGeneratorTest, GenerateFailsWithInvalidPath) { |
| 15 // Create directory in which to put minidump. | 21 // Create directory in which to put minidump. |
| 16 base::FilePath minidump_dir; | 22 base::FilePath minidump_dir; |
| 17 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | 23 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 18 | 24 |
| 19 // Attempt to generate a minidump from an invalid path. | 25 // Attempt to generate a minidump from an invalid path. |
| 20 DummyMinidumpGenerator generator("/path/does/not/exist/minidump.dmp"); | 26 DummyMinidumpGenerator generator("/path/does/not/exist/minidump.dmp"); |
| 21 ASSERT_FALSE(generator.Generate(minidump_dir.Append("minidump.dmp").value())); | 27 ASSERT_FALSE(generator.Generate(minidump_dir.Append("minidump.dmp").value())); |
| 22 } | 28 } |
| 23 | 29 |
| 24 TEST(DummyMinidumpGeneratorTest, GenerateSucceedsWithSmallSource) { | 30 TEST(DummyMinidumpGeneratorTest, GenerateSucceedsWithValidPath) { |
| 25 // Create directory in which to put minidump. | 31 // Create directory in which to put minidump. |
| 26 base::FilePath minidump_dir; | 32 base::FilePath minidump_dir; |
| 27 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | 33 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 28 | 34 |
| 29 // Create a fake minidump file. | 35 // Create a fake minidump file. |
| 30 base::FilePath fake_minidump; | 36 base::FilePath fake_minidump; |
| 31 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); | 37 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); |
| 32 const std::string data("Test contents of the minidump file.\n"); | 38 const std::string data("Test contents of the minidump file.\n"); |
| 33 ASSERT_EQ(static_cast<int>(data.size()), | 39 ASSERT_EQ(static_cast<int>(data.size()), |
| 34 base::WriteFile(fake_minidump, data.c_str(), data.size())); | 40 base::WriteFile(fake_minidump, data.c_str(), data.size())); |
| 35 | 41 |
| 36 DummyMinidumpGenerator generator(fake_minidump.value()); | 42 DummyMinidumpGenerator generator(fake_minidump.value()); |
| 37 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); | 43 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); |
| 38 EXPECT_TRUE(generator.Generate(new_minidump.value())); | 44 EXPECT_TRUE(generator.Generate(new_minidump.value())); |
| 39 | 45 |
| 40 // Original file should not exist, and new file should contain original | 46 // Original file should not exist, and new file should contain original |
| 41 // contents. | 47 // contents. |
| 42 std::string copied_data; | 48 std::string copied_data; |
| 43 EXPECT_FALSE(base::PathExists(fake_minidump)); | 49 EXPECT_FALSE(base::PathExists(fake_minidump)); |
| 44 ASSERT_TRUE(base::PathExists(new_minidump)); | 50 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 45 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); | 51 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); |
| 46 EXPECT_EQ(data, copied_data); | 52 EXPECT_EQ(data, copied_data); |
| 47 } | 53 } |
| 48 | 54 |
| 49 TEST(DummyMinidumpGeneratorTest, GenerateSucceedsWithLargeSource) { | 55 TEST(DummyMinidumpGeneratorTest, CopyAndDeleteFailsWithInvalidSource) { |
| 56 // Create directory in which to put minidump. |
| 57 base::FilePath minidump_dir; |
| 58 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 59 |
| 60 // Attempt to copy from an invalid path. |
| 61 DummyMinidumpGenerator generator("/path/does/not/exist/minidump.dmp"); |
| 62 ASSERT_FALSE(generator.CopyAndDeleteForTest( |
| 63 minidump_dir.Append("minidump.dmp").value())); |
| 64 } |
| 65 |
| 66 TEST(DummyMinidumpGeneratorTest, CopyAndDeleteSucceedsWithSmallSource) { |
| 67 // Create directory in which to put minidump. |
| 68 base::FilePath minidump_dir; |
| 69 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 70 |
| 71 // Create a fake minidump file. |
| 72 base::FilePath fake_minidump; |
| 73 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); |
| 74 const std::string data("Test contents of the minidump file.\n"); |
| 75 ASSERT_EQ(static_cast<int>(data.size()), |
| 76 base::WriteFile(fake_minidump, data.c_str(), data.size())); |
| 77 |
| 78 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); |
| 79 DummyMinidumpGenerator generator(fake_minidump.value()); |
| 80 ASSERT_TRUE(generator.CopyAndDeleteForTest(new_minidump.value())); |
| 81 |
| 82 // Original file should not exist, and new file should contain original |
| 83 // contents. |
| 84 std::string copied_data; |
| 85 EXPECT_FALSE(base::PathExists(fake_minidump)); |
| 86 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 87 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); |
| 88 EXPECT_EQ(data, copied_data); |
| 89 } |
| 90 |
| 91 TEST(DummyMinidumpGeneratorTest, CopyAndDeleteSucceedsWithLargeSource) { |
| 50 // Create directory in which to put minidump. | 92 // Create directory in which to put minidump. |
| 51 base::FilePath minidump_dir; | 93 base::FilePath minidump_dir; |
| 52 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | 94 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 53 | 95 |
| 54 // Create a large fake minidump file. | 96 // Create a large fake minidump file. |
| 97 // Note: The file must be greater than the size of the buffer used to copy the |
| 98 // file in CopyAndDelete(). Create a big string in memory. |
| 55 base::FilePath fake_minidump; | 99 base::FilePath fake_minidump; |
| 56 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); | 100 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); |
| 57 size_t str_len = 32768 * 10 + 1; | 101 size_t str_len = kInternalBufferSize * 10 + 1; |
| 58 const std::string data = base::RandBytesAsString(str_len); | 102 const std::string data = base::RandBytesAsString(str_len); |
| 59 | 103 |
| 60 // Write the string to the file. | 104 // Write the string to the file and verify that the file is big enough. |
| 61 ASSERT_EQ(static_cast<int>(data.size()), | 105 ASSERT_EQ(static_cast<int>(data.size()), |
| 62 base::WriteFile(fake_minidump, data.c_str(), data.size())); | 106 base::WriteFile(fake_minidump, data.c_str(), data.size())); |
| 107 int64_t filesize = 0; |
| 108 ASSERT_TRUE(base::GetFileSize(fake_minidump, &filesize)); |
| 109 ASSERT_GT(filesize, kInternalBufferSize); |
| 63 | 110 |
| 64 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); | 111 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); |
| 65 DummyMinidumpGenerator generator(fake_minidump.value()); | 112 DummyMinidumpGenerator generator(fake_minidump.value()); |
| 66 ASSERT_TRUE(generator.Generate(new_minidump.value())); | 113 ASSERT_TRUE(generator.CopyAndDeleteForTest(new_minidump.value())); |
| 67 | 114 |
| 68 // Original file should not exist, and new file should contain original | 115 // Original file should not exist, and new file should contain original |
| 69 // contents. | 116 // contents. |
| 117 std::string copied_data; |
| 118 EXPECT_FALSE(base::PathExists(fake_minidump)); |
| 119 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 120 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); |
| 121 EXPECT_EQ(data, copied_data); |
| 122 } |
| 123 |
| 124 TEST(DummyMinidumpGeneratorTest, CopyAndDeleteSucceedsWhenEOFAlignsWithBuffer) { |
| 125 // Create directory in which to put minidump. |
| 126 base::FilePath minidump_dir; |
| 127 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 128 |
| 129 // Create a large fake minidump file. |
| 130 // Note: The file must be greater than the size of the buffer used to copy the |
| 131 // file in CopyAndDelete(). Create a big string in memory. |
| 132 base::FilePath fake_minidump; |
| 133 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); |
| 134 size_t str_len = kInternalBufferSize; |
| 135 const std::string data = base::RandBytesAsString(str_len); |
| 136 |
| 137 // Write the string to the file and verify that the file is big enough. |
| 138 ASSERT_EQ(static_cast<int>(data.size()), |
| 139 base::WriteFile(fake_minidump, data.c_str(), data.size())); |
| 140 int64_t filesize = 0; |
| 141 ASSERT_TRUE(base::GetFileSize(fake_minidump, &filesize)); |
| 142 ASSERT_EQ(kInternalBufferSize, filesize); |
| 143 |
| 144 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); |
| 145 DummyMinidumpGenerator generator(fake_minidump.value()); |
| 146 ASSERT_TRUE(generator.CopyAndDeleteForTest(new_minidump.value())); |
| 147 |
| 148 // Original file should not exist, and new file should contain original |
| 149 // contents. |
| 70 std::string copied_data; | 150 std::string copied_data; |
| 71 EXPECT_FALSE(base::PathExists(fake_minidump)); | 151 EXPECT_FALSE(base::PathExists(fake_minidump)); |
| 72 ASSERT_TRUE(base::PathExists(new_minidump)); | 152 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 73 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); | 153 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); |
| 74 EXPECT_EQ(data, copied_data); | 154 EXPECT_EQ(data, copied_data); |
| 75 } | 155 } |
| 76 | 156 |
| 77 } // namespace chromecast | 157 } // namespace chromecast |
| OLD | NEW |