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
3 // found in the LICENSE file.
7 #include "base/files/file_util.h"
8 #include "breakpad/src/common/linux/libcurl_wrapper.h"
9 #include "chromecast/crash/cast_crashdump_uploader.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace chromecast
{
15 class MockLibcurlWrapper
: public google_breakpad::LibcurlWrapper
{
17 MOCK_METHOD0(Init
, bool());
18 MOCK_METHOD2(SetProxy
,
19 bool(const std::string
& proxy_host
,
20 const std::string
& proxy_userpwd
));
22 bool(const std::string
& upload_file_path
,
23 const std::string
& basename
));
24 MOCK_METHOD5(SendRequest
,
25 bool(const std::string
& url
,
26 const std::map
<std::string
, std::string
>& parameters
,
27 int* http_status_code
,
28 std::string
* http_header_data
,
29 std::string
* http_response_data
));
32 // Declared for the scope of this file to increase readability.
34 using testing::Return
;
36 TEST(CastCrashdumpUploaderTest
, UploadFailsWhenInitFails
) {
37 testing::StrictMock
<MockLibcurlWrapper
> m
;
38 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(false));
40 CastCrashdumpData data
;
41 data
.product
= "foobar";
43 data
.guid
= "AAA-BBB";
44 data
.email
= "test@test.com";
45 data
.comments
= "none";
46 data
.minidump_pathname
= "/tmp/foo.dmp";
47 data
.crash_server
= "http://foo.com";
48 CastCrashdumpUploader
uploader(data
, &m
);
50 ASSERT_FALSE(uploader
.Upload(nullptr));
53 TEST(CastCrashdumpUploaderTest
, UploadSucceedsWithValidParameters
) {
54 testing::StrictMock
<MockLibcurlWrapper
> m
;
56 // Create a temporary file.
58 ASSERT_TRUE(base::CreateTemporaryFile(&temp
));
60 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
61 EXPECT_CALL(m
, AddFile(temp
.value(), _
)).WillOnce(Return(true));
62 EXPECT_CALL(m
, SendRequest("http://foo.com", _
, _
, _
, _
)).Times(1).WillOnce(
65 CastCrashdumpData data
;
66 data
.product
= "foobar";
68 data
.guid
= "AAA-BBB";
69 data
.email
= "test@test.com";
70 data
.comments
= "none";
71 data
.minidump_pathname
= temp
.value();
72 data
.crash_server
= "http://foo.com";
73 CastCrashdumpUploader
uploader(data
, &m
);
75 ASSERT_TRUE(uploader
.Upload(nullptr));
78 TEST(CastCrashdumpUploaderTest
, UploadFailsWithInvalidPathname
) {
79 testing::StrictMock
<MockLibcurlWrapper
> m
;
80 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
81 EXPECT_CALL(m
, SendRequest(_
, _
, _
, _
, _
)).Times(0);
83 CastCrashdumpData data
;
84 data
.product
= "foobar";
86 data
.guid
= "AAA-BBB";
87 data
.email
= "test@test.com";
88 data
.comments
= "none";
89 data
.minidump_pathname
= "/invalid/file/path";
90 data
.crash_server
= "http://foo.com";
91 CastCrashdumpUploader
uploader(data
, &m
);
93 ASSERT_FALSE(uploader
.Upload(nullptr));
96 TEST(CastCrashdumpUploaderTest
, UploadFailsWithoutAllRequiredParameters
) {
97 testing::StrictMock
<MockLibcurlWrapper
> m
;
99 // Create a temporary file.
101 ASSERT_TRUE(base::CreateTemporaryFile(&temp
));
103 // Has all the require fields for a crashdump.
104 CastCrashdumpData data
;
105 data
.product
= "foobar";
106 data
.version
= "1.0";
107 data
.guid
= "AAA-BBB";
108 data
.email
= "test@test.com";
109 data
.comments
= "none";
110 data
.minidump_pathname
= temp
.value();
111 data
.crash_server
= "http://foo.com";
113 // Test with empty product name.
115 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
116 CastCrashdumpUploader
uploader_no_product(data
, &m
);
117 ASSERT_FALSE(uploader_no_product
.Upload(nullptr));
118 data
.product
= "foobar";
120 // Test with empty product version.
122 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
123 CastCrashdumpUploader
uploader_no_version(data
, &m
);
124 ASSERT_FALSE(uploader_no_version
.Upload(nullptr));
125 data
.version
= "1.0";
127 // Test with empty client GUID.
129 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
130 CastCrashdumpUploader
uploader_no_guid(data
, &m
);
131 ASSERT_FALSE(uploader_no_guid
.Upload(nullptr));
134 TEST(CastCrashdumpUploaderTest
, UploadFailsWithInvalidAttachment
) {
135 testing::StrictMock
<MockLibcurlWrapper
> m
;
137 // Create a temporary file.
138 base::FilePath minidump
;
139 ASSERT_TRUE(base::CreateTemporaryFile(&minidump
));
141 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
142 EXPECT_CALL(m
, AddFile(minidump
.value(), _
)).WillOnce(Return(true));
144 CastCrashdumpData data
;
145 data
.product
= "foobar";
146 data
.version
= "1.0";
147 data
.guid
= "AAA-BBB";
148 data
.email
= "test@test.com";
149 data
.comments
= "none";
150 data
.minidump_pathname
= minidump
.value();
151 data
.crash_server
= "http://foo.com";
152 CastCrashdumpUploader
uploader(data
, &m
);
154 // Add a file that does not exist as an attachment.
155 uploader
.AddAttachment("label", "/path/does/not/exist");
156 ASSERT_FALSE(uploader
.Upload(nullptr));
159 TEST(CastCrashdumpUploaderTest
, UploadSucceedsWithValidAttachment
) {
160 testing::StrictMock
<MockLibcurlWrapper
> m
;
162 // Create a temporary file.
163 base::FilePath minidump
;
164 ASSERT_TRUE(base::CreateTemporaryFile(&minidump
));
166 // Create a valid attachment.
167 base::FilePath attachment
;
168 ASSERT_TRUE(base::CreateTemporaryFile(&attachment
));
170 EXPECT_CALL(m
, Init()).Times(1).WillOnce(Return(true));
171 EXPECT_CALL(m
, AddFile(minidump
.value(), _
)).WillOnce(Return(true));
172 EXPECT_CALL(m
, AddFile(attachment
.value(), _
)).WillOnce(Return(true));
173 EXPECT_CALL(m
, SendRequest(_
, _
, _
, _
, _
)).Times(1).WillOnce(Return(true));
175 CastCrashdumpData data
;
176 data
.product
= "foobar";
177 data
.version
= "1.0";
178 data
.guid
= "AAA-BBB";
179 data
.email
= "test@test.com";
180 data
.comments
= "none";
181 data
.minidump_pathname
= minidump
.value();
182 data
.crash_server
= "http://foo.com";
183 CastCrashdumpUploader
uploader(data
, &m
);
185 // Add a file that does not exist as an attachment.
186 uploader
.AddAttachment("label", attachment
.value());
187 ASSERT_TRUE(uploader
.Upload(nullptr));
190 } // namespace chromeceast