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