Chromium Code Reviews| 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)); | |
|
wtc
2011/11/30 00:22:09
We should check that state->certs.empty() is true.
| |
| 81 | |
| 82 net::SSLHostInfo::State* state = ssl_host_info->mutable_state(); | |
| 83 state->certs.push_back(std::string("foo")); | |
| 84 ssl_host_info->Persist(); | |
| 85 | |
| 86 // Wait until Persist() does the work. | |
| 87 MessageLoop::current()->RunAllPending(); | |
| 88 | |
| 89 // Open the stored certificate chain. | |
| 90 ssl_host_info.reset( | |
| 91 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, | |
| 92 &cert_verifier, cache.http_cache())); | |
| 93 ssl_host_info->Start(); | |
| 94 rv = ssl_host_info->WaitForDataReady(&callback); | |
| 95 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
| 96 | |
| 97 // And now update the data. | |
| 98 state = ssl_host_info->mutable_state(); | |
| 99 EXPECT_EQ(1U, state->certs.size()); | |
| 100 EXPECT_EQ("foo", state->certs.front()); | |
| 101 state->certs.push_back(std::string("bar")); | |
| 102 | |
| 103 // Fail instead of DCHECKing double creates. | |
| 104 cache.disk_cache()->set_double_create_check(false); | |
|
wtc
2011/11/30 00:22:09
Is this necessary? With your found_entry_ change,
rvargas (doing something else)
2011/11/30 01:05:24
I just want to make sure that if the test fails (w
| |
| 105 ssl_host_info->Persist(); | |
| 106 MessageLoop::current()->RunAllPending(); | |
| 107 | |
| 108 // Verify that the state was updated. | |
| 109 ssl_host_info.reset( | |
| 110 new net::DiskCacheBasedSSLHostInfo("https://www.google.com", ssl_config, | |
| 111 &cert_verifier, cache.http_cache())); | |
| 112 ssl_host_info->Start(); | |
| 113 rv = ssl_host_info->WaitForDataReady(&callback); | |
| 114 EXPECT_EQ(net::OK, callback.GetResult(rv)); | |
| 115 | |
| 116 state = ssl_host_info->mutable_state(); | |
| 117 EXPECT_EQ(2U, state->certs.size()); | |
|
wtc
2011/11/30 00:22:09
Should we check that state->certs[0] is "foo" and
rvargas (doing something else)
2011/11/30 01:05:24
It felt like testing the mock cache... but I guess
| |
| 118 | |
| 119 RemoveMockTransaction(&kHostInfoTransaction); | |
| 120 } | |
| OLD | NEW |