| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string> | 5 #include <string> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "googleurl/src/gurl.h" | 11 #include "googleurl/src/gurl.h" |
| 12 #include "net/base/cookie_policy.h" | 12 #include "net/base/cookie_policy.h" |
| 13 #include "net/base/cookie_store.h" | 13 #include "net/base/cookie_store.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "net/base/sys_addrinfo.h" | 15 #include "net/base/sys_addrinfo.h" |
| 16 #include "net/base/transport_security_state.h" |
| 16 #include "net/socket_stream/socket_stream.h" | 17 #include "net/socket_stream/socket_stream.h" |
| 17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
| 18 #include "net/websockets/websocket_job.h" | 19 #include "net/websockets/websocket_job.h" |
| 19 #include "net/websockets/websocket_throttle.h" | 20 #include "net/websockets/websocket_throttle.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 22 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 private: | 150 private: |
| 150 bool allow_all_cookies_; | 151 bool allow_all_cookies_; |
| 151 }; | 152 }; |
| 152 | 153 |
| 153 class MockURLRequestContext : public URLRequestContext { | 154 class MockURLRequestContext : public URLRequestContext { |
| 154 public: | 155 public: |
| 155 MockURLRequestContext(CookieStore* cookie_store, | 156 MockURLRequestContext(CookieStore* cookie_store, |
| 156 CookiePolicy* cookie_policy) { | 157 CookiePolicy* cookie_policy) { |
| 157 set_cookie_store(cookie_store); | 158 set_cookie_store(cookie_store); |
| 158 set_cookie_policy(cookie_policy); | 159 set_cookie_policy(cookie_policy); |
| 160 transport_security_state_ = new TransportSecurityState(); |
| 161 set_transport_security_state(transport_security_state_.get()); |
| 162 TransportSecurityState::DomainState state; |
| 163 state.expiry = base::Time::Now() + base::TimeDelta::FromSeconds(1000); |
| 164 transport_security_state_->EnableHost("upgrademe.com", state); |
| 159 } | 165 } |
| 160 | 166 |
| 161 private: | 167 private: |
| 162 friend class base::RefCountedThreadSafe<MockURLRequestContext>; | 168 friend class base::RefCountedThreadSafe<MockURLRequestContext>; |
| 163 virtual ~MockURLRequestContext() {} | 169 virtual ~MockURLRequestContext() {} |
| 170 |
| 171 scoped_refptr<TransportSecurityState> transport_security_state_; |
| 164 }; | 172 }; |
| 165 | 173 |
| 166 class WebSocketJobTest : public PlatformTest { | 174 class WebSocketJobTest : public PlatformTest { |
| 167 public: | 175 public: |
| 168 virtual void SetUp() { | 176 virtual void SetUp() { |
| 169 cookie_store_ = new MockCookieStore; | 177 cookie_store_ = new MockCookieStore; |
| 170 cookie_policy_.reset(new MockCookiePolicy); | 178 cookie_policy_.reset(new MockCookiePolicy); |
| 171 context_ = new MockURLRequestContext( | 179 context_ = new MockURLRequestContext( |
| 172 cookie_store_.get(), cookie_policy_.get()); | 180 cookie_store_.get(), cookie_policy_.get()); |
| 173 } | 181 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 202 } | 210 } |
| 203 void CloseWebSocketJob() { | 211 void CloseWebSocketJob() { |
| 204 if (websocket_->socket_) { | 212 if (websocket_->socket_) { |
| 205 websocket_->socket_->DetachDelegate(); | 213 websocket_->socket_->DetachDelegate(); |
| 206 WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_); | 214 WebSocketThrottle::GetInstance()->RemoveFromQueue(websocket_); |
| 207 } | 215 } |
| 208 websocket_->state_ = WebSocketJob::CLOSED; | 216 websocket_->state_ = WebSocketJob::CLOSED; |
| 209 websocket_->delegate_ = NULL; | 217 websocket_->delegate_ = NULL; |
| 210 websocket_->socket_ = NULL; | 218 websocket_->socket_ = NULL; |
| 211 } | 219 } |
| 220 SocketStream* GetSocket(SocketStreamJob* job) { |
| 221 return job->socket_.get(); |
| 222 } |
| 212 | 223 |
| 213 scoped_refptr<MockCookieStore> cookie_store_; | 224 scoped_refptr<MockCookieStore> cookie_store_; |
| 214 scoped_ptr<MockCookiePolicy> cookie_policy_; | 225 scoped_ptr<MockCookiePolicy> cookie_policy_; |
| 215 scoped_refptr<MockURLRequestContext> context_; | 226 scoped_refptr<MockURLRequestContext> context_; |
| 216 scoped_refptr<WebSocketJob> websocket_; | 227 scoped_refptr<WebSocketJob> websocket_; |
| 217 scoped_refptr<MockSocketStream> socket_; | 228 scoped_refptr<MockSocketStream> socket_; |
| 218 }; | 229 }; |
| 219 | 230 |
| 220 TEST_F(WebSocketJobTest, SimpleHandshake) { | 231 TEST_F(WebSocketJobTest, SimpleHandshake) { |
| 221 GURL url("ws://example.com/demo"); | 232 GURL url("ws://example.com/demo"); |
| (...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 | 498 |
| 488 EXPECT_EQ(2U, cookie_store_->entries().size()); | 499 EXPECT_EQ(2U, cookie_store_->entries().size()); |
| 489 EXPECT_EQ(cookieUrl, cookie_store_->entries()[0].url); | 500 EXPECT_EQ(cookieUrl, cookie_store_->entries()[0].url); |
| 490 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); | 501 EXPECT_EQ("CR-test=1", cookie_store_->entries()[0].cookie_line); |
| 491 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); | 502 EXPECT_EQ(cookieUrl, cookie_store_->entries()[1].url); |
| 492 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); | 503 EXPECT_EQ("CR-test-httponly=1", cookie_store_->entries()[1].cookie_line); |
| 493 | 504 |
| 494 CloseWebSocketJob(); | 505 CloseWebSocketJob(); |
| 495 } | 506 } |
| 496 | 507 |
| 508 TEST_F(WebSocketJobTest, HSTSUpgrade) { |
| 509 GURL url("ws://upgrademe.com/"); |
| 510 MockSocketStreamDelegate delegate; |
| 511 scoped_refptr<SocketStreamJob> job = SocketStreamJob::CreateSocketStreamJob( |
| 512 url, &delegate, *context_.get()); |
| 513 EXPECT_TRUE(GetSocket(job.get())->is_secure()); |
| 514 job->DetachDelegate(); |
| 515 |
| 516 url = GURL("ws://donotupgrademe.com/"); |
| 517 job = SocketStreamJob::CreateSocketStreamJob( |
| 518 url, &delegate, *context_.get()); |
| 519 EXPECT_FALSE(GetSocket(job.get())->is_secure()); |
| 520 job->DetachDelegate(); |
| 521 } |
| 522 |
| 497 } // namespace net | 523 } // namespace net |
| OLD | NEW |