1// Copyright (c) 2009, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30// Unit test for crash dump uploader.
31
32#include <string>
33
34#include "common/linux/google_crashdump_uploader.h"
35#include "breakpad_googletest_includes.h"
36#include "common/using_std_string.h"
37
38namespace google_breakpad {
39
40using ::testing::Return;
41using ::testing::_;
42
43class MockLibcurlWrapper : public LibcurlWrapper {
44 public:
45  MOCK_METHOD0(Init, bool());
46  MOCK_METHOD2(SetProxy, bool(const string& proxy_host,
47                              const string& proxy_userpwd));
48  MOCK_METHOD2(AddFile, bool(const string& upload_file_path,
49                             const string& basename));
50  MOCK_METHOD5(SendRequest,
51               bool(const string& url,
52                    const std::map<string, string>& parameters,
53                    int* http_status_code,
54                    string* http_header_data,
55                    string* http_response_data));
56};
57
58class GoogleCrashdumpUploaderTest : public ::testing::Test {
59};
60
61TEST_F(GoogleCrashdumpUploaderTest, InitFailsCausesUploadFailure) {
62  MockLibcurlWrapper m;
63  EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(false));
64  GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
65                                                                  "1.0",
66                                                                  "AAA-BBB",
67                                                                  "",
68                                                                  "",
69                                                                  "test@test.com",
70                                                                  "none",
71                                                                  "/tmp/foo.dmp",
72                                                                  "http://foo.com",
73                                                                  "",
74                                                                  "",
75                                                                  &m);
76  ASSERT_FALSE(uploader->Upload(NULL, NULL, NULL));
77}
78
79TEST_F(GoogleCrashdumpUploaderTest, TestSendRequestHappensWithValidParameters) {
80  // Create a temp file
81  char tempfn[80] = "/tmp/googletest-upload-XXXXXX";
82  int fd = mkstemp(tempfn);
83  ASSERT_NE(fd, -1);
84  close(fd);
85
86  MockLibcurlWrapper m;
87  EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(true));
88  EXPECT_CALL(m, AddFile(tempfn, _)).WillOnce(Return(true));
89  EXPECT_CALL(m,
90              SendRequest("http://foo.com",_,_,_,_)).Times(1).WillOnce(Return(true));
91  GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
92                                                                  "1.0",
93                                                                  "AAA-BBB",
94                                                                  "",
95                                                                  "",
96                                                                  "test@test.com",
97                                                                  "none",
98                                                                  tempfn,
99                                                                  "http://foo.com",
100                                                                  "",
101                                                                  "",
102                                                                  &m);
103  ASSERT_TRUE(uploader->Upload(NULL, NULL, NULL));
104}
105
106
107TEST_F(GoogleCrashdumpUploaderTest, InvalidPathname) {
108  MockLibcurlWrapper m;
109  EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(true));
110  EXPECT_CALL(m, SendRequest(_,_,_,_,_)).Times(0);
111  GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
112                                                                  "1.0",
113                                                                  "AAA-BBB",
114                                                                  "",
115                                                                  "",
116                                                                  "test@test.com",
117                                                                  "none",
118                                                                  "/tmp/foo.dmp",
119                                                                  "http://foo.com",
120                                                                  "",
121                                                                  "",
122                                                                  &m);
123  ASSERT_FALSE(uploader->Upload(NULL, NULL, NULL));
124}
125
126TEST_F(GoogleCrashdumpUploaderTest, TestRequiredParametersMustBePresent) {
127  // Test with empty product name.
128  GoogleCrashdumpUploader uploader("",
129                                   "1.0",
130                                   "AAA-BBB",
131                                   "",
132                                   "",
133                                   "test@test.com",
134                                   "none",
135                                   "/tmp/foo.dmp",
136                                   "http://foo.com",
137                                   "",
138                                   "");
139  ASSERT_FALSE(uploader.Upload(NULL, NULL, NULL));
140
141  // Test with empty product version.
142  GoogleCrashdumpUploader uploader1("product",
143                                    "",
144                                    "AAA-BBB",
145                                    "",
146                                    "",
147                                    "",
148                                    "",
149                                    "/tmp/foo.dmp",
150                                    "",
151                                    "",
152                                    "");
153
154  ASSERT_FALSE(uploader1.Upload(NULL, NULL, NULL));
155
156  // Test with empty client GUID.
157  GoogleCrashdumpUploader uploader2("product",
158                                    "1.0",
159                                    "",
160                                    "",
161                                    "",
162                                    "",
163                                    "",
164                                    "/tmp/foo.dmp",
165                                    "",
166                                    "",
167                                    "");
168  ASSERT_FALSE(uploader2.Upload(NULL, NULL, NULL));
169}
170}
171