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 #include "base/scoped_ptr.h" | |
17 #include "omaha/base/const_addresses.h" | |
18 #include "omaha/base/scoped_any.h" | |
19 #include "omaha/base/scoped_ptr_address.h" | |
20 #include "omaha/base/string.h" | |
21 #include "omaha/base/vista_utils.h" | |
22 #include "omaha/common/config_manager.h" | |
23 #include "omaha/common/update_request.h" | |
24 #include "omaha/common/update_response.h" | |
25 #include "omaha/common/web_services_client.h" | |
26 #include "omaha/net/network_request.h" | |
27 #include "omaha/testing/unit_test.h" | |
28 | |
29 using ::testing::_; | |
30 | |
31 namespace omaha { | |
32 | |
33 // TODO(omaha): test the machine case. | |
34 | |
35 class WebServicesClientTest : public testing::Test { | |
36 protected: | |
37 virtual void SetUp() { | |
38 EXPECT_HRESULT_SUCCEEDED( | |
39 ConfigManager::Instance()->GetUpdateCheckUrl(&update_check_url_)); | |
40 | |
41 web_service_client_.reset(new WebServicesClient(false)); | |
42 | |
43 update_request_.reset(xml::UpdateRequest::Create(false, | |
44 _T("unittest_sessionid"), | |
45 _T("unittest_instsource"), | |
46 CString())); | |
47 update_response_.reset(xml::UpdateResponse::Create()); | |
48 } | |
49 | |
50 virtual void TearDown() { | |
51 web_service_client_.reset(); | |
52 } | |
53 | |
54 NetworkRequest* network_request() const { | |
55 return web_service_client_->network_request(); | |
56 } | |
57 | |
58 CString update_check_url_; | |
59 | |
60 scoped_ptr<WebServicesClient> web_service_client_; | |
61 scoped_ptr<xml::UpdateRequest> update_request_; | |
62 scoped_ptr<xml::UpdateResponse> update_response_; | |
63 }; | |
64 | |
65 TEST_F(WebServicesClientTest, Send) { | |
66 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Initialize(update_check_url_, | |
67 HeadersVector(), | |
68 false)); | |
69 | |
70 // Test sending a user update check request. | |
71 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Send(update_request_.get(), | |
72 update_response_.get())); | |
73 EXPECT_TRUE(web_service_client_->is_http_success()); | |
74 | |
75 xml::response::Response response(update_response_->response()); | |
76 EXPECT_STREQ(_T("3.0"), response.protocol); | |
77 | |
78 NetworkRequest* network_request(network_request()); | |
79 | |
80 CString cookie; | |
81 EXPECT_HRESULT_FAILED(network_request->QueryHeadersString( | |
82 WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_COOKIE, | |
83 WINHTTP_HEADER_NAME_BY_INDEX, | |
84 &cookie)); | |
85 EXPECT_TRUE(cookie.IsEmpty()); | |
86 | |
87 CString etag; | |
88 EXPECT_HRESULT_FAILED(network_request->QueryHeadersString( | |
89 WINHTTP_QUERY_ETAG, WINHTTP_HEADER_NAME_BY_INDEX, &etag)); | |
90 EXPECT_TRUE(etag.IsEmpty()); | |
91 } | |
92 | |
93 TEST_F(WebServicesClientTest, SendUsingCup) { | |
94 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Initialize(update_check_url_, | |
95 HeadersVector(), | |
96 true)); | |
97 | |
98 // Test sending a user update check request. | |
99 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Send(update_request_.get(), | |
100 update_response_.get())); | |
101 EXPECT_TRUE(web_service_client_->is_http_success()); | |
102 | |
103 xml::response::Response response(update_response_->response()); | |
104 EXPECT_STREQ(_T("3.0"), response.protocol); | |
105 | |
106 NetworkRequest* network_request(network_request()); | |
107 | |
108 CString no_request_age_header; | |
109 network_request->QueryHeadersString( | |
110 WINHTTP_QUERY_CUSTOM | WINHTTP_QUERY_FLAG_REQUEST_HEADERS, | |
111 _T("X-RequestAge"), | |
112 &no_request_age_header); | |
113 | |
114 EXPECT_STREQ(_T(""), no_request_age_header); | |
115 | |
116 // A CUP transaction has either a request or a response CUP cookie and | |
117 // the ETag response header. | |
118 CString request_cookie; | |
119 network_request->QueryHeadersString( | |
120 WINHTTP_QUERY_COOKIE | WINHTTP_QUERY_FLAG_REQUEST_HEADERS, | |
121 WINHTTP_HEADER_NAME_BY_INDEX, | |
122 &request_cookie); | |
123 const bool has_cup_request_cookie = request_cookie.Find(_T("c=")) != -1; | |
124 | |
125 CString response_cookie; | |
126 network_request->QueryHeadersString(WINHTTP_QUERY_SET_COOKIE, | |
127 WINHTTP_HEADER_NAME_BY_INDEX, | |
128 &response_cookie); | |
129 const bool has_cup_response_cookie = response_cookie.Find(_T("c=")) != -1; | |
130 | |
131 EXPECT_TRUE(has_cup_request_cookie || has_cup_response_cookie); | |
132 | |
133 CString etag; | |
134 EXPECT_HRESULT_SUCCEEDED(network_request->QueryHeadersString( | |
135 WINHTTP_QUERY_ETAG, WINHTTP_HEADER_NAME_BY_INDEX, &etag)); | |
136 EXPECT_FALSE(etag.IsEmpty()); | |
137 } | |
138 | |
139 TEST_F(WebServicesClientTest, SendForcingHttps) { | |
140 // Skips the test if the update check URL is not https. | |
141 if (!String_StartsWith(update_check_url_, kHttpsProtoScheme, true)) { | |
142 return; | |
143 } | |
144 | |
145 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Initialize(update_check_url_, | |
146 HeadersVector(), | |
147 true)); | |
148 | |
149 EXPECT_TRUE(update_request_->IsEmpty()); | |
150 | |
151 // Adds an application with non-empty tt_token to the update request. | |
152 // This should prevent the network stack from replacing https with | |
153 // CUP protocol. | |
154 xml::request::App app; | |
155 app.app_id = _T("{21CD0965-0B0E-47cf-B421-2D191C16C0E2}"); | |
156 app.iid = _T("{00000000-0000-0000-0000-000000000000}"); | |
157 app.update_check.is_valid = true; | |
158 app.update_check.tt_token = _T("Test TT token"); | |
159 update_request_->AddApp(app); | |
160 | |
161 EXPECT_FALSE(update_request_->IsEmpty()); | |
162 EXPECT_TRUE(update_request_->has_tt_token()); | |
163 | |
164 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Send(update_request_.get(), | |
165 update_response_.get())); | |
166 EXPECT_TRUE(web_service_client_->is_http_success()); | |
167 | |
168 // Do a couple of sanity checks on the parsing of the response. | |
169 xml::response::Response response(update_response_->response()); | |
170 EXPECT_STREQ(_T("3.0"), response.protocol); | |
171 ASSERT_EQ(1, response.apps.size()); | |
172 EXPECT_STREQ(_T("error-unknownApplication"), response.apps[0].status); | |
173 } | |
174 | |
175 TEST_F(WebServicesClientTest, SendWithCustomHeader) { | |
176 HeadersVector headers; | |
177 headers.push_back(std::make_pair(_T("X-RequestAge"), _T("200"))); | |
178 | |
179 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Initialize(update_check_url_, | |
180 headers, | |
181 true)); | |
182 | |
183 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Send(update_request_.get(), | |
184 update_response_.get())); | |
185 EXPECT_TRUE(web_service_client_->is_http_success()); | |
186 | |
187 xml::response::Response response(update_response_->response()); | |
188 EXPECT_STREQ(_T("3.0"), response.protocol); | |
189 | |
190 NetworkRequest* network_request(network_request()); | |
191 | |
192 CString request_age_header; | |
193 network_request->QueryHeadersString( | |
194 WINHTTP_QUERY_CUSTOM | WINHTTP_QUERY_FLAG_REQUEST_HEADERS, | |
195 _T("X-RequestAge"), | |
196 &request_age_header); | |
197 | |
198 EXPECT_STREQ(_T("200"), request_age_header); | |
199 } | |
200 | |
201 TEST_F(WebServicesClientTest, SendString) { | |
202 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Initialize(update_check_url_, | |
203 HeadersVector(), | |
204 false)); | |
205 | |
206 // Test sending a user update check request. | |
207 CString request_string = | |
208 _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") | |
209 _T("<request protocol=\"3.0\" testsource=\"dev\"></request>"); | |
210 scoped_ptr<xml::UpdateResponse> response(xml::UpdateResponse::Create()); | |
211 EXPECT_HRESULT_SUCCEEDED(web_service_client_->SendString(&request_string, | |
212 response.get())); | |
213 EXPECT_TRUE(web_service_client_->is_http_success()); | |
214 } | |
215 | |
216 TEST_F(WebServicesClientTest, SendStringWithCustomHeader) { | |
217 HeadersVector headers; | |
218 headers.push_back(std::make_pair(_T("X-FooBar"), _T("424"))); | |
219 | |
220 EXPECT_HRESULT_SUCCEEDED(web_service_client_->Initialize(update_check_url_, | |
221 headers, | |
222 false)); | |
223 | |
224 // Test sending a user update check request. | |
225 CString request_string = | |
226 _T("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") | |
227 _T("<request protocol=\"3.0\" testsource=\"dev\"></request>"); | |
228 scoped_ptr<xml::UpdateResponse> response(xml::UpdateResponse::Create()); | |
229 EXPECT_HRESULT_SUCCEEDED(web_service_client_->SendString(&request_string, | |
230 response.get())); | |
231 EXPECT_TRUE(web_service_client_->is_http_success()); | |
232 | |
233 NetworkRequest* network_request(network_request()); | |
234 | |
235 CString foobar_header; | |
236 network_request->QueryHeadersString( | |
237 WINHTTP_QUERY_CUSTOM | WINHTTP_QUERY_FLAG_REQUEST_HEADERS, | |
238 _T("X-FooBar"), | |
239 &foobar_header); | |
240 | |
241 EXPECT_STREQ(_T("424"), foobar_header); | |
242 } | |
243 | |
244 } // namespace omaha | |
245 | |
OLD | NEW |