| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-2010 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 // This unit test is hardcoded to run against production servers only. | |
| 17 | |
| 18 #include <iostream> | |
| 19 #include <vector> | |
| 20 #include "base/scoped_ptr.h" | |
| 21 #include "omaha/base/constants.h" | |
| 22 #include "omaha/base/vistautil.h" | |
| 23 #include "omaha/net/cup_request.h" | |
| 24 #include "omaha/net/network_config.h" | |
| 25 #include "omaha/net/simple_request.h" | |
| 26 #include "omaha/net/urlmon_request.h" | |
| 27 #include "omaha/testing/unit_test.h" | |
| 28 | |
| 29 namespace omaha { | |
| 30 | |
| 31 const TCHAR* const kGetUrl = | |
| 32 _T("http://tools.google.com/service/update2/id"); | |
| 33 const TCHAR* const kGetUrlNoResponseBody = | |
| 34 _T("http://tools.google.com/service/update2/nil"); | |
| 35 const TCHAR* const kPostUrl = | |
| 36 _T("http://tools.google.com/service/update2"); | |
| 37 const uint8 kRequestBuffer[] = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><o:gup
date xmlns:o=\"http://www.google.com/update2/request\" protocol=\"2.0\" version=
\"1.2.1.0\" ismachine=\"1\" testsource=\"dev\"><o:os platform=\"win\" version=\"
5.1\" sp=\"Service Pack 2\"/><o:app appid=\"{52820187-5605-4C18-AA51-8BD0A1209C8
C}\" version=\"1.1.1.3\" lang=\"abc\" client=\"{0AF52D61-9958-4fea-9B29-CDD9DCDB
B145}\" iid=\"{F723495F-8ACF-4746-8240-643741C797B5}\"><o:event eventtype=\"1\"
eventresult=\"1\" errorcode=\"0\" previousversion=\"1.0.0.0\"/></o:app></o:gupda
te>"; // NOLINT | |
| 38 | |
| 39 class CupRequestTest : public testing::Test { | |
| 40 protected: | |
| 41 CupRequestTest() {} | |
| 42 | |
| 43 static void SetUpTestCase() { | |
| 44 NetworkConfig* network_config = NULL; | |
| 45 EXPECT_HRESULT_SUCCEEDED( | |
| 46 NetworkConfigManager::Instance().GetUserNetworkConfig(&network_config)); | |
| 47 network_config->SetCupCredentials(NULL); | |
| 48 | |
| 49 // For debugging purposes, try FF configuration first. | |
| 50 network_config->Clear(); | |
| 51 network_config->Add(new FirefoxProxyDetector()); | |
| 52 EXPECT_HRESULT_SUCCEEDED(network_config->Detect()); | |
| 53 } | |
| 54 | |
| 55 static void TearDownTestCase() { | |
| 56 NetworkConfig* network_config = NULL; | |
| 57 EXPECT_HRESULT_SUCCEEDED( | |
| 58 NetworkConfigManager::Instance().GetUserNetworkConfig(&network_config)); | |
| 59 network_config->SetCupCredentials(NULL); | |
| 60 network_config->Clear(); | |
| 61 } | |
| 62 | |
| 63 void DoRequest(HttpRequestInterface* contained_request, | |
| 64 const CString& url, | |
| 65 const uint8* request_buffer, | |
| 66 size_t request_buffer_lenght) { | |
| 67 scoped_ptr<CupRequest> http_request(new CupRequest(contained_request)); | |
| 68 | |
| 69 NetworkConfig* network_config = NULL; | |
| 70 EXPECT_HRESULT_SUCCEEDED( | |
| 71 NetworkConfigManager::Instance().GetUserNetworkConfig(&network_config)); | |
| 72 network_config->SetCupCredentials(NULL); | |
| 73 | |
| 74 // For debugging purposes, try FF configuration first. | |
| 75 network_config->Clear(); | |
| 76 network_config->Add(new FirefoxProxyDetector()); | |
| 77 EXPECT_HRESULT_SUCCEEDED(network_config->Detect()); | |
| 78 | |
| 79 // This will use Firefox or direct connection if FF is not installed. | |
| 80 std::vector<ProxyConfig> proxy_configurations( | |
| 81 network_config->GetConfigurations()); | |
| 82 proxy_configurations.push_back(ProxyConfig()); | |
| 83 | |
| 84 // Clear CUP credentials. | |
| 85 network_config->SetCupCredentials(NULL); | |
| 86 | |
| 87 HINTERNET handle = network_config->session().session_handle; | |
| 88 http_request->set_session_handle(handle); | |
| 89 http_request->set_proxy_configuration(proxy_configurations[0]); | |
| 90 http_request->set_url(url); | |
| 91 if (request_buffer) { | |
| 92 http_request->set_request_buffer(request_buffer, request_buffer_lenght); | |
| 93 } | |
| 94 | |
| 95 // First request goes with a fresh set of client credentials. | |
| 96 EXPECT_HRESULT_SUCCEEDED(http_request->Send()); | |
| 97 int http_status = http_request->GetHttpStatusCode(); | |
| 98 EXPECT_TRUE(http_status == HTTP_STATUS_OK || | |
| 99 http_status == HTTP_STATUS_PARTIAL_CONTENT); | |
| 100 std::vector<uint8> response(http_request->GetResponse()); | |
| 101 | |
| 102 // Second request goes with cached client credentials. | |
| 103 EXPECT_HRESULT_SUCCEEDED(http_request->Send()); | |
| 104 http_status = http_request->GetHttpStatusCode(); | |
| 105 EXPECT_TRUE(http_status == HTTP_STATUS_OK || | |
| 106 http_status == HTTP_STATUS_PARTIAL_CONTENT); | |
| 107 response = http_request->GetResponse(); | |
| 108 | |
| 109 // After each test run we should have some {sk, c} persisted in the | |
| 110 // registry. The CUP credentials are written back when the CUP request | |
| 111 // that has created them is destroyed. | |
| 112 http_request.reset(); | |
| 113 CupCredentials cup_creds; | |
| 114 EXPECT_HRESULT_SUCCEEDED(network_config->GetCupCredentials(&cup_creds)); | |
| 115 EXPECT_FALSE(cup_creds.sk.empty()); | |
| 116 EXPECT_FALSE(cup_creds.c.IsEmpty()); | |
| 117 | |
| 118 network_config->SetCupCredentials(NULL); | |
| 119 EXPECT_HRESULT_FAILED(network_config->GetCupCredentials(&cup_creds)); | |
| 120 } | |
| 121 }; | |
| 122 | |
| 123 TEST_F(CupRequestTest, GetSimpleRequest) { | |
| 124 if (IsTestRunByLocalSystem()) { | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 DoRequest(new SimpleRequest, kGetUrl, NULL, 0); | |
| 129 DoRequest(new SimpleRequest, kGetUrlNoResponseBody, NULL, 0); | |
| 130 } | |
| 131 | |
| 132 TEST_F(CupRequestTest, GetUrlmonRequest) { | |
| 133 DoRequest(new UrlmonRequest, kGetUrl, NULL, 0); | |
| 134 DoRequest(new UrlmonRequest, kGetUrlNoResponseBody, NULL, 0); | |
| 135 } | |
| 136 | |
| 137 TEST_F(CupRequestTest, PostSimpleRequest) { | |
| 138 if (IsTestRunByLocalSystem()) { | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 DoRequest(new SimpleRequest, kPostUrl, | |
| 143 kRequestBuffer, arraysize(kRequestBuffer) - 1); | |
| 144 } | |
| 145 | |
| 146 TEST_F(CupRequestTest, PostUrlmonRequest) { | |
| 147 DoRequest(new UrlmonRequest, kPostUrl, | |
| 148 kRequestBuffer, arraysize(kRequestBuffer) - 1); | |
| 149 } | |
| 150 | |
| 151 } // namespace omaha | |
| 152 | |
| OLD | NEW |