| 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 | |
| 20 TEST(DummyMinidumpGeneratorTest, GenerateFailsWithInvalidPath) { | 14 TEST(DummyMinidumpGeneratorTest, GenerateFailsWithInvalidPath) { |
| 21 // Create directory in which to put minidump. | 15 // Create directory in which to put minidump. |
| 22 base::FilePath minidump_dir; | 16 base::FilePath minidump_dir; |
| 23 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | 17 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 24 | 18 |
| 25 // Attempt to generate a minidump from an invalid path. | 19 // Attempt to generate a minidump from an invalid path. |
| 26 DummyMinidumpGenerator generator("/path/does/not/exist/minidump.dmp"); | 20 DummyMinidumpGenerator generator("/path/does/not/exist/minidump.dmp"); |
| 27 ASSERT_FALSE(generator.Generate(minidump_dir.Append("minidump.dmp").value())); | 21 ASSERT_FALSE(generator.Generate(minidump_dir.Append("minidump.dmp").value())); |
| 28 } | 22 } |
| 29 | 23 |
| 30 TEST(DummyMinidumpGeneratorTest, GenerateSucceedsWithValidPath) { | 24 TEST(DummyMinidumpGeneratorTest, GenerateSucceedsWithSmallSource) { |
| 31 // Create directory in which to put minidump. | 25 // Create directory in which to put minidump. |
| 32 base::FilePath minidump_dir; | 26 base::FilePath minidump_dir; |
| 33 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | 27 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 34 | 28 |
| 35 // Create a fake minidump file. | 29 // Create a fake minidump file. |
| 36 base::FilePath fake_minidump; | 30 base::FilePath fake_minidump; |
| 37 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); | 31 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); |
| 38 const std::string data("Test contents of the minidump file.\n"); | 32 const std::string data("Test contents of the minidump file.\n"); |
| 39 ASSERT_EQ(static_cast<int>(data.size()), | 33 ASSERT_EQ(static_cast<int>(data.size()), |
| 40 base::WriteFile(fake_minidump, data.c_str(), data.size())); | 34 base::WriteFile(fake_minidump, data.c_str(), data.size())); |
| 41 | 35 |
| 42 DummyMinidumpGenerator generator(fake_minidump.value()); | 36 DummyMinidumpGenerator generator(fake_minidump.value()); |
| 43 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); | 37 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); |
| 44 EXPECT_TRUE(generator.Generate(new_minidump.value())); | 38 EXPECT_TRUE(generator.Generate(new_minidump.value())); |
| 45 | 39 |
| 46 // Original file should not exist, and new file should contain original | 40 // Original file should not exist, and new file should contain original |
| 47 // contents. | 41 // contents. |
| 48 std::string copied_data; | 42 std::string copied_data; |
| 49 EXPECT_FALSE(base::PathExists(fake_minidump)); | 43 EXPECT_FALSE(base::PathExists(fake_minidump)); |
| 50 ASSERT_TRUE(base::PathExists(new_minidump)); | 44 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 51 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); | 45 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); |
| 52 EXPECT_EQ(data, copied_data); | 46 EXPECT_EQ(data, copied_data); |
| 53 } | 47 } |
| 54 | 48 |
| 55 TEST(DummyMinidumpGeneratorTest, CopyAndDeleteFailsWithInvalidSource) { | 49 TEST(DummyMinidumpGeneratorTest, GenerateSucceedsWithLargeSource) { |
| 56 // Create directory in which to put minidump. | 50 // Create directory in which to put minidump. |
| 57 base::FilePath minidump_dir; | 51 base::FilePath minidump_dir; |
| 58 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | 52 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); |
| 59 | 53 |
| 60 // Attempt to copy from an invalid path. | 54 // Create a large fake minidump file. |
| 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; | 55 base::FilePath fake_minidump; |
| 73 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); | 56 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); |
| 74 const std::string data("Test contents of the minidump file.\n"); | 57 size_t str_len = 32768 * 10 + 1; |
| 58 const std::string data = base::RandBytesAsString(str_len); |
| 59 |
| 60 // Write the string to the file. |
| 75 ASSERT_EQ(static_cast<int>(data.size()), | 61 ASSERT_EQ(static_cast<int>(data.size()), |
| 76 base::WriteFile(fake_minidump, data.c_str(), data.size())); | 62 base::WriteFile(fake_minidump, data.c_str(), data.size())); |
| 77 | 63 |
| 78 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); | 64 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); |
| 79 DummyMinidumpGenerator generator(fake_minidump.value()); | 65 DummyMinidumpGenerator generator(fake_minidump.value()); |
| 80 ASSERT_TRUE(generator.CopyAndDeleteForTest(new_minidump.value())); | 66 ASSERT_TRUE(generator.Generate(new_minidump.value())); |
| 81 | 67 |
| 82 // Original file should not exist, and new file should contain original | 68 // Original file should not exist, and new file should contain original |
| 83 // contents. | 69 // 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) { | |
| 92 // Create directory in which to put minidump. | |
| 93 base::FilePath minidump_dir; | |
| 94 ASSERT_TRUE(base::CreateNewTempDirectory("", &minidump_dir)); | |
| 95 | |
| 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. | |
| 99 base::FilePath fake_minidump; | |
| 100 ASSERT_TRUE(base::CreateTemporaryFile(&fake_minidump)); | |
| 101 size_t str_len = kInternalBufferSize * 10 + 1; | |
| 102 const std::string data = base::RandBytesAsString(str_len); | |
| 103 | |
| 104 // Write the string to the file and verify that the file is big enough. | |
| 105 ASSERT_EQ(static_cast<int>(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); | |
| 110 | |
| 111 base::FilePath new_minidump = minidump_dir.Append("minidump.dmp"); | |
| 112 DummyMinidumpGenerator generator(fake_minidump.value()); | |
| 113 ASSERT_TRUE(generator.CopyAndDeleteForTest(new_minidump.value())); | |
| 114 | |
| 115 // Original file should not exist, and new file should contain original | |
| 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. | |
| 150 std::string copied_data; | 70 std::string copied_data; |
| 151 EXPECT_FALSE(base::PathExists(fake_minidump)); | 71 EXPECT_FALSE(base::PathExists(fake_minidump)); |
| 152 ASSERT_TRUE(base::PathExists(new_minidump)); | 72 ASSERT_TRUE(base::PathExists(new_minidump)); |
| 153 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); | 73 EXPECT_TRUE(base::ReadFileToString(new_minidump, &copied_data)); |
| 154 EXPECT_EQ(data, copied_data); | 74 EXPECT_EQ(data, copied_data); |
| 155 } | 75 } |
| 156 | 76 |
| 157 } // namespace chromecast | 77 } // namespace chromecast |
| OLD | NEW |