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 "base/bind.h" |
| 6 #include "base/bind_helpers.h" |
| 7 #include "base/compiler_specific.h" |
5 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
6 #include "net/base/ssl_config_service.h" | 9 #include "net/base/ssl_config_service.h" |
7 #include "net/http/disk_cache_based_ssl_host_info.h" | 10 #include "net/http/disk_cache_based_ssl_host_info.h" |
8 #include "net/http/mock_http_cache.h" | 11 #include "net/http/mock_http_cache.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
10 | 13 |
11 namespace { | 14 namespace { |
12 | 15 |
13 class DeleteSSLHostInfoOldCompletionCallback : public TestOldCompletionCallback
{ | |
14 public: | |
15 explicit DeleteSSLHostInfoOldCompletionCallback(net::SSLHostInfo* ssl_host_inf
o) | |
16 : ssl_host_info_(ssl_host_info) {} | |
17 | |
18 virtual void RunWithParams(const Tuple1<int>& params) { | |
19 delete ssl_host_info_; | |
20 TestOldCompletionCallback::RunWithParams(params); | |
21 } | |
22 | |
23 private: | |
24 net::SSLHostInfo* ssl_host_info_; | |
25 }; | |
26 | |
27 // This is an empty transaction, needed to register the URL and the test mode. | 16 // This is an empty transaction, needed to register the URL and the test mode. |
28 const MockTransaction kHostInfoTransaction = { | 17 const MockTransaction kHostInfoTransaction = { |
29 "sslhostinfo:https://www.google.com", | 18 "sslhostinfo:https://www.google.com", |
30 "", | 19 "", |
31 base::Time(), | 20 base::Time(), |
32 "", | 21 "", |
33 net::LOAD_NORMAL, | 22 net::LOAD_NORMAL, |
34 "", | 23 "", |
35 "", | 24 "", |
36 base::Time(), | 25 base::Time(), |
37 "", | 26 "", |
38 TEST_MODE_NORMAL, | 27 TEST_MODE_NORMAL, |
39 NULL, | 28 NULL, |
40 0 | 29 0 |
41 }; | 30 }; |
42 | 31 |
43 } // namespace | |
44 | |
45 // Tests that we can delete a DiskCacheBasedSSLHostInfo object in a | 32 // Tests that we can delete a DiskCacheBasedSSLHostInfo object in a |
46 // completion callback for DiskCacheBasedSSLHostInfo::WaitForDataReady. | 33 // completion callback for DiskCacheBasedSSLHostInfo::WaitForDataReady. |
47 TEST(DiskCacheBasedSSLHostInfo, DeleteInCallback) { | 34 TEST(DiskCacheBasedSSLHostInfo, DeleteInCallback) { |
48 net::CertVerifier cert_verifier; | 35 net::CertVerifier cert_verifier; |
49 // Use the blocking mock backend factory to force asynchronous completion | 36 // Use the blocking mock backend factory to force asynchronous completion |
50 // of ssl_host_info->WaitForDataReady(), so that the callback will run. | 37 // of ssl_host_info->WaitForDataReady(), so that the callback will run. |
51 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 38 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
52 MockHttpCache cache(factory); | 39 MockHttpCache cache(factory); |
53 net::SSLConfig ssl_config; | 40 net::SSLConfig ssl_config; |
54 net::SSLHostInfo* ssl_host_info = | 41 scoped_ptr<net::SSLHostInfo> ssl_host_info( |
55 new net::DiskCacheBasedSSLHostInfo("https://www.verisign.com", ssl_config, | 42 new net::DiskCacheBasedSSLHostInfo("https://www.verisign.com", ssl_config, |
56 &cert_verifier, cache.http_cache()); | 43 &cert_verifier, cache.http_cache())); |
57 ssl_host_info->Start(); | 44 ssl_host_info->Start(); |
58 DeleteSSLHostInfoOldCompletionCallback callback(ssl_host_info); | 45 net::TestCompletionCallback callback; |
59 int rv = ssl_host_info->WaitForDataReady(&callback); | 46 int rv = ssl_host_info->WaitForDataReady(callback.callback()); |
60 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 47 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
61 // Now complete the backend creation and let the callback run. | 48 // Now complete the backend creation and let the callback run. |
62 factory->FinishCreation(); | 49 factory->FinishCreation(); |
63 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 50 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
64 } | 51 } |
65 | 52 |
66 // Tests the basic logic of storing, retrieving and updating data. | 53 // Tests the basic logic of storing, retrieving and updating data. |
67 TEST(DiskCacheBasedSSLHostInfo, Update) { | 54 TEST(DiskCacheBasedSSLHostInfo, Update) { |
68 MockHttpCache cache; | 55 MockHttpCache cache; |
69 AddMockTransaction(&kHostInfoTransaction); | 56 AddMockTransaction(&kHostInfoTransaction); |
70 TestOldCompletionCallback callback; | 57 net::TestCompletionCallback callback; |
71 | 58 |
72 // Store a certificate chain. | 59 // Store a certificate chain. |
73 net::CertVerifier cert_verifier; | 60 net::CertVerifier cert_verifier; |
74 net::SSLConfig ssl_config; | 61 net::SSLConfig ssl_config; |
75 scoped_ptr<net::SSLHostInfo> ssl_host_info( | 62 scoped_ptr<net::SSLHostInfo> ssl_host_info( |
76 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, | 63 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, |
77 &cert_verifier, cache.http_cache())); | 64 &cert_verifier, cache.http_cache())); |
78 ssl_host_info->Start(); | 65 ssl_host_info->Start(); |
79 int rv = ssl_host_info->WaitForDataReady(&callback); | 66 int rv = ssl_host_info->WaitForDataReady(callback.callback()); |
80 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 67 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
81 | 68 |
82 net::SSLHostInfo::State* state = ssl_host_info->mutable_state(); | 69 net::SSLHostInfo::State* state = ssl_host_info->mutable_state(); |
83 EXPECT_TRUE(state->certs.empty()); | 70 EXPECT_TRUE(state->certs.empty()); |
84 state->certs.push_back(std::string("foo")); | 71 state->certs.push_back(std::string("foo")); |
85 ssl_host_info->Persist(); | 72 ssl_host_info->Persist(); |
86 | 73 |
87 // Wait until Persist() does the work. | 74 // Wait until Persist() does the work. |
88 MessageLoop::current()->RunAllPending(); | 75 MessageLoop::current()->RunAllPending(); |
89 | 76 |
90 // Open the stored certificate chain. | 77 // Open the stored certificate chain. |
91 ssl_host_info.reset( | 78 ssl_host_info.reset( |
92 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, | 79 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, |
93 &cert_verifier, cache.http_cache())); | 80 &cert_verifier, cache.http_cache())); |
94 ssl_host_info->Start(); | 81 ssl_host_info->Start(); |
95 rv = ssl_host_info->WaitForDataReady(&callback); | 82 rv = ssl_host_info->WaitForDataReady(callback.callback()); |
96 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 83 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
97 | 84 |
98 // And now update the data. | 85 // And now update the data. |
99 state = ssl_host_info->mutable_state(); | 86 state = ssl_host_info->mutable_state(); |
100 EXPECT_EQ(1U, state->certs.size()); | 87 EXPECT_EQ(1U, state->certs.size()); |
101 EXPECT_EQ("foo", state->certs.front()); | 88 EXPECT_EQ("foo", state->certs.front()); |
102 state->certs.push_back(std::string("bar")); | 89 state->certs.push_back(std::string("bar")); |
103 | 90 |
104 // Fail instead of DCHECKing double creates. | 91 // Fail instead of DCHECKing double creates. |
105 cache.disk_cache()->set_double_create_check(false); | 92 cache.disk_cache()->set_double_create_check(false); |
106 ssl_host_info->Persist(); | 93 ssl_host_info->Persist(); |
107 MessageLoop::current()->RunAllPending(); | 94 MessageLoop::current()->RunAllPending(); |
108 | 95 |
109 // Verify that the state was updated. | 96 // Verify that the state was updated. |
110 ssl_host_info.reset( | 97 ssl_host_info.reset( |
111 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, | 98 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, |
112 &cert_verifier, cache.http_cache())); | 99 &cert_verifier, cache.http_cache())); |
113 ssl_host_info->Start(); | 100 ssl_host_info->Start(); |
114 rv = ssl_host_info->WaitForDataReady(&callback); | 101 rv = ssl_host_info->WaitForDataReady(callback.callback()); |
115 EXPECT_EQ(net::OK, callback.GetResult(rv)); | 102 EXPECT_EQ(net::OK, callback.GetResult(rv)); |
116 | 103 |
117 state = ssl_host_info->mutable_state(); | 104 state = ssl_host_info->mutable_state(); |
118 EXPECT_EQ(2U, state->certs.size()); | 105 EXPECT_EQ(2U, state->certs.size()); |
119 EXPECT_EQ("foo", state->certs[0]); | 106 EXPECT_EQ("foo", state->certs[0]); |
120 EXPECT_EQ("bar", state->certs[1]); | 107 EXPECT_EQ("bar", state->certs[1]); |
121 | 108 |
122 RemoveMockTransaction(&kHostInfoTransaction); | 109 RemoveMockTransaction(&kHostInfoTransaction); |
123 } | 110 } |
| 111 |
| 112 } // namespace |
OLD | NEW |