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/keygen_handler.h" | 5 #include "net/base/keygen_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/location.h" | 11 #include "base/location.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/threading/worker_pool.h" | 13 #include "base/threading/worker_pool.h" |
14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
15 #include "base/synchronization/waitable_event.h" | 15 #include "base/synchronization/waitable_event.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 #if defined(USE_NSS) | 19 #if defined(USE_NSS_CERTS) |
20 #include <private/pprthred.h> // PR_DetachThread | 20 #include <private/pprthred.h> // PR_DetachThread |
21 #include "crypto/nss_crypto_module_delegate.h" | 21 #include "crypto/nss_crypto_module_delegate.h" |
22 #include "crypto/scoped_test_nss_db.h" | 22 #include "crypto/scoped_test_nss_db.h" |
23 #endif | 23 #endif |
24 | 24 |
25 namespace net { | 25 namespace net { |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 #if defined(USE_NSS) | 29 #if defined(USE_NSS_CERTS) |
30 class StubCryptoModuleDelegate : public crypto::NSSCryptoModuleDelegate { | 30 class StubCryptoModuleDelegate : public crypto::NSSCryptoModuleDelegate { |
31 public: | 31 public: |
32 explicit StubCryptoModuleDelegate(crypto::ScopedPK11Slot slot) | 32 explicit StubCryptoModuleDelegate(crypto::ScopedPK11Slot slot) |
33 : slot_(slot.Pass()) {} | 33 : slot_(slot.Pass()) {} |
34 | 34 |
35 std::string RequestPassword(const std::string& slot_name, | 35 std::string RequestPassword(const std::string& slot_name, |
36 bool retry, | 36 bool retry, |
37 bool* cancelled) override { | 37 bool* cancelled) override { |
38 return std::string(); | 38 return std::string(); |
39 } | 39 } |
40 | 40 |
41 crypto::ScopedPK11Slot RequestSlot() override { | 41 crypto::ScopedPK11Slot RequestSlot() override { |
42 return crypto::ScopedPK11Slot(PK11_ReferenceSlot(slot_.get())); | 42 return crypto::ScopedPK11Slot(PK11_ReferenceSlot(slot_.get())); |
43 } | 43 } |
44 | 44 |
45 private: | 45 private: |
46 crypto::ScopedPK11Slot slot_; | 46 crypto::ScopedPK11Slot slot_; |
47 }; | 47 }; |
48 #endif | 48 #endif |
49 | 49 |
50 class KeygenHandlerTest : public ::testing::Test { | 50 class KeygenHandlerTest : public ::testing::Test { |
51 public: | 51 public: |
52 KeygenHandlerTest() {} | 52 KeygenHandlerTest() {} |
53 ~KeygenHandlerTest() override {} | 53 ~KeygenHandlerTest() override {} |
54 | 54 |
55 scoped_ptr<KeygenHandler> CreateKeygenHandler() { | 55 scoped_ptr<KeygenHandler> CreateKeygenHandler() { |
56 scoped_ptr<KeygenHandler> handler(new KeygenHandler( | 56 scoped_ptr<KeygenHandler> handler(new KeygenHandler( |
57 768, "some challenge", GURL("http://www.example.com"))); | 57 768, "some challenge", GURL("http://www.example.com"))); |
58 #if defined(USE_NSS) | 58 #if defined(USE_NSS_CERTS) |
59 handler->set_crypto_module_delegate( | 59 handler->set_crypto_module_delegate( |
60 scoped_ptr<crypto::NSSCryptoModuleDelegate>( | 60 scoped_ptr<crypto::NSSCryptoModuleDelegate>( |
61 new StubCryptoModuleDelegate(crypto::ScopedPK11Slot( | 61 new StubCryptoModuleDelegate(crypto::ScopedPK11Slot( |
62 PK11_ReferenceSlot(test_nss_db_.slot()))))); | 62 PK11_ReferenceSlot(test_nss_db_.slot()))))); |
63 #endif | 63 #endif |
64 return handler.Pass(); | 64 return handler.Pass(); |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
68 #if defined(USE_NSS) | 68 #if defined(USE_NSS_CERTS) |
69 crypto::ScopedTestNSSDB test_nss_db_; | 69 crypto::ScopedTestNSSDB test_nss_db_; |
70 #endif | 70 #endif |
71 }; | 71 }; |
72 | 72 |
73 // Assert that |result| is a valid output for KeygenHandler given challenge | 73 // Assert that |result| is a valid output for KeygenHandler given challenge |
74 // string of |challenge|. | 74 // string of |challenge|. |
75 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, | 75 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, |
76 const std::string& challenge) { | 76 const std::string& challenge) { |
77 ASSERT_GT(result.length(), 0U); | 77 ASSERT_GT(result.length(), 0U); |
78 | 78 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 void ConcurrencyTestCallback(const std::string& challenge, | 117 void ConcurrencyTestCallback(const std::string& challenge, |
118 base::WaitableEvent* event, | 118 base::WaitableEvent* event, |
119 scoped_ptr<KeygenHandler> handler, | 119 scoped_ptr<KeygenHandler> handler, |
120 std::string* result) { | 120 std::string* result) { |
121 // We allow Singleton use on the worker thread here since we use a | 121 // We allow Singleton use on the worker thread here since we use a |
122 // WaitableEvent to synchronize, so it's safe. | 122 // WaitableEvent to synchronize, so it's safe. |
123 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; | 123 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; |
124 handler->set_stores_key(false); // Don't leave the key-pair behind. | 124 handler->set_stores_key(false); // Don't leave the key-pair behind. |
125 *result = handler->GenKeyAndSignChallenge(); | 125 *result = handler->GenKeyAndSignChallenge(); |
126 event->Signal(); | 126 event->Signal(); |
127 #if defined(USE_NSS) | 127 #if defined(USE_NSS_CERTS) |
128 // Detach the thread from NSPR. | 128 // Detach the thread from NSPR. |
129 // Calling NSS functions attaches the thread to NSPR, which stores | 129 // Calling NSS functions attaches the thread to NSPR, which stores |
130 // the NSPR thread ID in thread-specific data. | 130 // the NSPR thread ID in thread-specific data. |
131 // The threads in our thread pool terminate after we have called | 131 // The threads in our thread pool terminate after we have called |
132 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 132 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
133 // segfaults on shutdown when the threads' thread-specific data | 133 // segfaults on shutdown when the threads' thread-specific data |
134 // destructors run. | 134 // destructors run. |
135 PR_DetachThread(); | 135 PR_DetachThread(); |
136 #endif | 136 #endif |
137 } | 137 } |
(...skipping 23 matching lines...) Expand all Loading... |
161 events[i] = NULL; | 161 events[i] = NULL; |
162 | 162 |
163 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; | 163 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; |
164 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); | 164 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); |
165 } | 165 } |
166 } | 166 } |
167 | 167 |
168 } // namespace | 168 } // namespace |
169 | 169 |
170 } // namespace net | 170 } // namespace net |
OLD | NEW |