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/cert_cache/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 { | |
17 | |
18 // This class is essentially a temporary workaround for not being able to use | |
19 // Net::CompletionCallback with non-int parameters. Using this class as a sort | |
Ryan Sleevi
2014/06/11 22:00:09
nit: Net:: -> net::
| |
20 // of wrapper, we can use the DiskBasedCertCache and also the (much needed) | |
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 | |
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 TEST(DiskBasedCertCache, SimpleSetAndGet) { | |
77 scoped_ptr<disk_cache::Backend> backend( | |
78 disk_cache::MemBackendImpl::CreateBackend(0, NULL)); | |
79 | |
80 ASSERT_TRUE(backend.get()); | |
81 | |
82 net::DiskBasedCertCache cert_cache(backend.get()); | |
83 | |
84 scoped_refptr<net::X509Certificate> cert(net::ImportCertFromFile( | |
85 net::GetTestCertsDirectory(), "root_ca_cert.pem")); | |
86 | |
87 ASSERT_TRUE(cert.get()); | |
88 | |
89 MockCertCacheUser user(&cert_cache); | |
90 | |
91 net::TestCompletionCallback callbackSet; | |
92 net::TestCompletionCallback callbackGet; | |
93 | |
94 user.Set(cert.get()->os_cert_handle(), callbackSet.callback()); | |
95 callbackSet.WaitForResult(); | |
96 | |
97 user.Get(callbackGet.callback()); | |
98 callbackGet.WaitForResult(); | |
99 | |
100 net::X509Certificate::OSCertHandle retrieved_cert_handle = | |
101 user.GetLastCertHandle(); | |
102 | |
103 ASSERT_TRUE(net::X509Certificate::IsSameOSCert(retrieved_cert_handle, | |
104 cert.get()->os_cert_handle())); | |
105 } | |
OLD | NEW |