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