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