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