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/bind_helpers.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/scoped_ptr.h" |
13 #include "base/threading/worker_pool.h" | 15 #include "base/threading/worker_pool.h" |
14 #include "base/threading/thread_restrictions.h" | 16 #include "base/threading/thread_restrictions.h" |
15 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
16 #include "build/build_config.h" | 18 #include "build/build_config.h" |
17 #include "crypto/nss_util.h" | 19 #include "crypto/nss_util.h" |
| 20 #include "crypto/nss_util_internal.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 | 22 |
20 #if defined(USE_NSS) | 23 #if defined(USE_NSS) |
21 #include <private/pprthred.h> // PR_DetachThread | 24 #include <private/pprthred.h> // PR_DetachThread |
22 #endif | 25 #endif |
23 | 26 |
24 namespace net { | 27 namespace net { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
28 class KeygenHandlerTest : public ::testing::Test { | 31 class KeygenHandlerTest : public ::testing::Test { |
29 public: | 32 public: |
30 KeygenHandlerTest() {} | 33 KeygenHandlerTest() {} |
31 virtual ~KeygenHandlerTest() {} | 34 virtual ~KeygenHandlerTest() {} |
32 | 35 |
33 private: | 36 protected: |
34 #if defined(OS_CHROMEOS) && defined(USE_NSS) | 37 void SetUpKeyHandler(KeygenHandler* handler) { |
| 38 #if defined(USE_NSS) |
| 39 crypto::ScopedPK11Slot slot(crypto::GetPersistentNSSKeySlot()); |
| 40 handler->set_key_slot(slot.get()); |
| 41 #endif |
| 42 } |
| 43 |
| 44 #if defined(USE_NSS) |
35 crypto::ScopedTestNSSDB test_nss_db_; | 45 crypto::ScopedTestNSSDB test_nss_db_; |
36 #endif | 46 #endif |
37 }; | 47 }; |
38 | 48 |
39 // Assert that |result| is a valid output for KeygenHandler given challenge | 49 // Assert that |result| is a valid output for KeygenHandler given challenge |
40 // string of |challenge|. | 50 // string of |challenge|. |
41 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, | 51 void AssertValidSignedPublicKeyAndChallenge(const std::string& result, |
42 const std::string& challenge) { | 52 const std::string& challenge) { |
43 ASSERT_GT(result.length(), 0U); | 53 ASSERT_GT(result.length(), 0U); |
44 | 54 |
(...skipping 22 matching lines...) Expand all Loading... |
67 // Signature Algorithm: md5WithRSAEncryption | 77 // Signature Algorithm: md5WithRSAEncryption |
68 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... | 78 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... |
69 // Signature OK | 79 // Signature OK |
70 // | 80 // |
71 // The value of |spkac| can be ASN.1-parsed with: | 81 // The value of |spkac| can be ASN.1-parsed with: |
72 // openssl asn1parse -inform DER | 82 // openssl asn1parse -inform DER |
73 } | 83 } |
74 | 84 |
75 TEST_F(KeygenHandlerTest, SmokeTest) { | 85 TEST_F(KeygenHandlerTest, SmokeTest) { |
76 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); | 86 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); |
| 87 SetUpKeyHandler(&handler); |
77 handler.set_stores_key(false); // Don't leave the key-pair behind | 88 handler.set_stores_key(false); // Don't leave the key-pair behind |
78 std::string result = handler.GenKeyAndSignChallenge(); | 89 std::string result = handler.GenKeyAndSignChallenge(); |
79 VLOG(1) << "KeygenHandler produced: " << result; | 90 VLOG(1) << "KeygenHandler produced: " << result; |
80 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); | 91 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); |
81 } | 92 } |
82 | 93 |
83 void ConcurrencyTestCallback(base::WaitableEvent* event, | 94 void ConcurrencyTestCallback(base::WaitableEvent* event, |
84 const std::string& challenge, | 95 scoped_ptr<KeygenHandler> handler, |
85 std::string* result) { | 96 std::string* result) { |
86 // We allow Singleton use on the worker thread here since we use a | 97 // We allow Singleton use on the worker thread here since we use a |
87 // WaitableEvent to synchronize, so it's safe. | 98 // WaitableEvent to synchronize, so it's safe. |
88 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; | 99 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; |
89 KeygenHandler handler(768, challenge, GURL("http://www.example.com")); | 100 handler->set_stores_key(false); // Don't leave the key-pair behind. |
90 handler.set_stores_key(false); // Don't leave the key-pair behind. | 101 *result = handler->GenKeyAndSignChallenge(); |
91 *result = handler.GenKeyAndSignChallenge(); | |
92 event->Signal(); | 102 event->Signal(); |
93 #if defined(USE_NSS) | 103 #if defined(USE_NSS) |
94 // Detach the thread from NSPR. | 104 // Detach the thread from NSPR. |
95 // Calling NSS functions attaches the thread to NSPR, which stores | 105 // Calling NSS functions attaches the thread to NSPR, which stores |
96 // the NSPR thread ID in thread-specific data. | 106 // the NSPR thread ID in thread-specific data. |
97 // The threads in our thread pool terminate after we have called | 107 // The threads in our thread pool terminate after we have called |
98 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 108 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
99 // segfaults on shutdown when the threads' thread-specific data | 109 // segfaults on shutdown when the threads' thread-specific data |
100 // destructors run. | 110 // destructors run. |
101 PR_DetachThread(); | 111 PR_DetachThread(); |
102 #endif | 112 #endif |
103 } | 113 } |
104 | 114 |
105 // We asynchronously generate the keys so as not to hang up the IO thread. This | 115 // We asynchronously generate the keys so as not to hang up the IO thread. This |
106 // test tries to catch concurrency problems in the keygen implementation. | 116 // test tries to catch concurrency problems in the keygen implementation. |
107 TEST_F(KeygenHandlerTest, ConcurrencyTest) { | 117 TEST_F(KeygenHandlerTest, ConcurrencyTest) { |
108 const int NUM_HANDLERS = 5; | 118 const int NUM_HANDLERS = 5; |
109 base::WaitableEvent* events[NUM_HANDLERS] = { NULL }; | 119 base::WaitableEvent* events[NUM_HANDLERS] = {NULL}; |
110 std::string results[NUM_HANDLERS]; | 120 std::string results[NUM_HANDLERS]; |
111 for (int i = 0; i < NUM_HANDLERS; i++) { | 121 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 122 scoped_ptr<KeygenHandler> handler(new KeygenHandler( |
| 123 768, "some challenge", GURL("http://www.example.com"))); |
| 124 SetUpKeyHandler(handler.get()); |
112 events[i] = new base::WaitableEvent(false, false); | 125 events[i] = new base::WaitableEvent(false, false); |
113 base::WorkerPool::PostTask( | 126 base::WorkerPool::PostTask(FROM_HERE, |
114 FROM_HERE, | 127 base::Bind(ConcurrencyTestCallback, |
115 base::Bind(ConcurrencyTestCallback, events[i], "some challenge", | 128 events[i], |
116 &results[i]), | 129 base::Passed(&handler), |
117 true); | 130 &results[i]), |
| 131 true); |
118 } | 132 } |
119 | 133 |
120 for (int i = 0; i < NUM_HANDLERS; i++) { | 134 for (int i = 0; i < NUM_HANDLERS; i++) { |
121 // Make sure the job completed | 135 // Make sure the job completed |
122 events[i]->Wait(); | 136 events[i]->Wait(); |
123 delete events[i]; | 137 delete events[i]; |
124 events[i] = NULL; | 138 events[i] = NULL; |
125 | 139 |
126 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; | 140 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; |
127 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); | 141 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); |
128 } | 142 } |
129 } | 143 } |
130 | 144 |
131 } // namespace | 145 } // namespace |
132 | 146 |
133 } // namespace net | 147 } // namespace net |
OLD | NEW |