OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/http/disk_based_cert_cache.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "net/base/completion_callback.h" | |
9 #include "net/base/net_errors.h" | |
10 #include "net/base/test_completion_callback.h" | |
11 #include "net/base/test_data_directory.h" | |
12 #include "net/disk_cache/memory/mem_backend_impl.h" | |
13 #include "net/test/cert_test_util.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace { | |
wtc
2014/06/12 03:13:17
You can put this file in the net namespace, so tha
| |
17 | |
18 // This class is essentially a workaround for not being able to use | |
19 // net::CompletionCallback with non-int parameters. Using this class as a sort | |
20 // of wrapper, it is possible to use the DiskBasedCertCache and also | |
21 // net::TestCompletionCallback::WaitForResult. | |
22 | |
23 class MockCertCacheUser { | |
24 public: | |
25 explicit MockCertCacheUser(net::DiskBasedCertCache* cert_cache) | |
26 : weak_factory_(this), cert_cache_(cert_cache) {} | |
27 | |
28 void Set(const net::X509Certificate::OSCertHandle cert_handle, | |
29 net::CompletionCallback cb) { | |
30 user_callback_ = cb; | |
31 cert_cache_->Set( | |
32 cert_handle, | |
33 base::Bind(&MockCertCacheUser::FinishSet, weak_factory_.GetWeakPtr())); | |
34 } | |
35 | |
36 // todo: take in a key. This is slightly cumbersome ( ? since the string must | |
wtc
2014/06/12 03:13:17
Use all caps TODO, and include your user name.
| |
37 // stay alive for the cert cache and is passed by const ref) | |
38 void Get(net::CompletionCallback cb) { | |
39 user_callback_ = cb; | |
40 cert_cache_->Get( | |
41 last_key_, | |
42 base::Bind(&MockCertCacheUser::FinishGet, weak_factory_.GetWeakPtr())); | |
43 } | |
44 | |
45 void FinishSet(const std::string& key) { | |
46 ASSERT_TRUE(!key.empty()); | |
47 last_key_ = key; | |
48 net::CompletionCallback callback = user_callback_; | |
49 user_callback_.Reset(); | |
50 callback.Run(net::OK); | |
51 } | |
52 | |
53 void FinishGet(net::X509Certificate::OSCertHandle cert_handle) { | |
54 ASSERT_TRUE(cert_handle); | |
55 | |
56 last_cert_handle_ = cert_handle; | |
57 net::CompletionCallback callback = user_callback_; | |
58 user_callback_.Reset(); | |
59 callback.Run(net::OK); | |
60 } | |
61 | |
62 net::X509Certificate::OSCertHandle GetLastCertHandle() { | |
63 return last_cert_handle_; | |
64 } | |
65 | |
66 private: | |
67 base::WeakPtrFactory<MockCertCacheUser> weak_factory_; | |
68 net::DiskBasedCertCache* cert_cache_; | |
69 net::CompletionCallback user_callback_; | |
70 std::string last_key_; | |
71 net::X509Certificate::OSCertHandle last_cert_handle_; | |
72 }; | |
73 | |
74 } // namespace | |
75 | |
76 // Stores a cert cache in DiskBasedCertCache, then retrieves it | |
77 // and makes sure it was retrieved successfully. | |
78 TEST(DiskBasedCertCache, SimpleSetAndGet) { | |
79 scoped_ptr<disk_cache::Backend> backend( | |
80 disk_cache::MemBackendImpl::CreateBackend(0, NULL)); | |
81 | |
82 ASSERT_TRUE(backend.get()); | |
83 | |
84 net::DiskBasedCertCache cert_cache(backend.get()); | |
85 | |
86 scoped_refptr<net::X509Certificate> cert(net::ImportCertFromFile( | |
87 net::GetTestCertsDirectory(), "root_ca_cert.pem")); | |
88 | |
89 ASSERT_TRUE(cert.get()); | |
90 | |
91 MockCertCacheUser user(&cert_cache); | |
92 | |
93 net::TestCompletionCallback callbackSet; | |
94 net::TestCompletionCallback callbackGet; | |
95 | |
96 user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); | |
97 callbackSet.WaitForResult(); | |
98 | |
99 user.Get(callbackGet.callback()); | |
100 callbackGet.WaitForResult(); | |
101 | |
102 net::X509Certificate::OSCertHandle retrieved_cert_handle = | |
103 user.GetLastCertHandle(); | |
104 | |
105 ASSERT_TRUE(net::X509Certificate::IsSameOSCert(retrieved_cert_handle, | |
106 cert.get()->os_cert_handle())); | |
107 } | |
108 | |
109 // Test the behavior when a matching entry already exists when set is called. | |
110 TEST(DiskBasedCertCache, OverwriteSet) { | |
111 scoped_ptr<disk_cache::Backend> backend( | |
112 disk_cache::MemBackendImpl::CreateBackend(0, NULL)); | |
113 | |
114 net::DiskBasedCertCache cert_cache(backend.get()); | |
115 | |
116 scoped_refptr<net::X509Certificate> cert(net::ImportCertFromFile( | |
117 net::GetTestCertsDirectory(), "root_ca_cert.pem")); | |
118 | |
119 MockCertCacheUser user(&cert_cache); | |
120 | |
121 net::TestCompletionCallback callbackSet; | |
122 net::TestCompletionCallback callbackGet; | |
123 | |
124 user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); | |
125 callbackSet.WaitForResult(); | |
126 | |
127 user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); | |
128 callbackSet.WaitForResult(); | |
129 } | |
OLD | NEW |