957874ada606579a339f1c6a3963ddf989f1e7b6
[platform/framework/web/crosswalk.git] / src / breakpad / src / common / linux / google_crashdump_uploader_test.cc
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 "common/linux/libcurl_wrapper.h"
36 #include "breakpad_googletest_includes.h"
37 #include "common/using_std_string.h"
38
39 namespace google_breakpad {
40
41 using ::testing::Return;
42 using ::testing::_;
43
44 class MockLibcurlWrapper : public LibcurlWrapper {
45  public:
46   MOCK_METHOD0(Init, bool());
47   MOCK_METHOD2(SetProxy, bool(const string& proxy_host,
48                               const string& proxy_userpwd));
49   MOCK_METHOD2(AddFile, bool(const string& upload_file_path,
50                              const string& basename));
51   MOCK_METHOD3(SendRequest,
52                bool(const string& url,
53                     const std::map<string, string>& parameters,
54                     string* server_response));
55 };
56
57 class GoogleCrashdumpUploaderTest : public ::testing::Test {
58 };
59
60 TEST_F(GoogleCrashdumpUploaderTest, InitFailsCausesUploadFailure) {
61   MockLibcurlWrapper m;
62   EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(false));
63   GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
64                                                                   "1.0",
65                                                                   "AAA-BBB",
66                                                                   "",
67                                                                   "",
68                                                                   "test@test.com",
69                                                                   "none",
70                                                                   "/tmp/foo.dmp",
71                                                                   "http://foo.com",
72                                                                   "",
73                                                                   "",
74                                                                   &m);
75   ASSERT_FALSE(uploader->Upload());
76 }
77
78 TEST_F(GoogleCrashdumpUploaderTest, TestSendRequestHappensWithValidParameters) {
79   // Create a temp file
80   char tempfn[80] = "/tmp/googletest-upload-XXXXXX";
81   int fd = mkstemp(tempfn);
82   ASSERT_NE(fd, -1);
83   close(fd);
84
85   MockLibcurlWrapper m;
86   EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(true));
87   EXPECT_CALL(m, AddFile(tempfn, _)).WillOnce(Return(true));
88   EXPECT_CALL(m,
89               SendRequest("http://foo.com",_,_)).Times(1).WillOnce(Return(true));
90   GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
91                                                                   "1.0",
92                                                                   "AAA-BBB",
93                                                                   "",
94                                                                   "",
95                                                                   "test@test.com",
96                                                                   "none",
97                                                                   tempfn,
98                                                                   "http://foo.com",
99                                                                   "",
100                                                                   "",
101                                                                   &m);
102   ASSERT_TRUE(uploader->Upload());
103 }
104
105
106 TEST_F(GoogleCrashdumpUploaderTest, InvalidPathname) {
107   MockLibcurlWrapper m;
108   EXPECT_CALL(m, Init()).Times(1).WillOnce(Return(true));
109   EXPECT_CALL(m, SendRequest(_,_,_)).Times(0);
110   GoogleCrashdumpUploader *uploader = new GoogleCrashdumpUploader("foobar",
111                                                                   "1.0",
112                                                                   "AAA-BBB",
113                                                                   "",
114                                                                   "",
115                                                                   "test@test.com",
116                                                                   "none",
117                                                                   "/tmp/foo.dmp",
118                                                                   "http://foo.com",
119                                                                   "",
120                                                                   "",
121                                                                   &m);
122   ASSERT_FALSE(uploader->Upload());
123 }
124
125 TEST_F(GoogleCrashdumpUploaderTest, TestRequiredParametersMustBePresent) {
126   // Test with empty product name.
127   GoogleCrashdumpUploader uploader("",
128                                    "1.0",
129                                    "AAA-BBB",
130                                    "",
131                                    "",
132                                    "test@test.com",
133                                    "none",
134                                    "/tmp/foo.dmp",
135                                    "http://foo.com",
136                                    "",
137                                    "");
138   ASSERT_FALSE(uploader.Upload());
139
140   // Test with empty product version.
141   GoogleCrashdumpUploader uploader1("product",
142                                     "",
143                                     "AAA-BBB",
144                                     "",
145                                     "",
146                                     "",
147                                     "",
148                                     "/tmp/foo.dmp",
149                                     "",
150                                     "",
151                                     "");
152
153   ASSERT_FALSE(uploader1.Upload());
154
155   // Test with empty client GUID.
156   GoogleCrashdumpUploader uploader2("product",
157                                     "1.0",
158                                     "",
159                                     "",
160                                     "",
161                                     "",
162                                     "",
163                                     "/tmp/foo.dmp",
164                                     "",
165                                     "",
166                                     "");
167   ASSERT_FALSE(uploader2.Upload());
168 }
169 }