| 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 "build/build_config.h" | |
| 10 #include "base/base64.h" | 9 #include "base/base64.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/task.h" | 13 #include "base/task.h" |
| 14 #include "base/threading/worker_pool.h" | 14 #include "base/threading/worker_pool.h" |
| 15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
| 16 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 17 #include "build/build_config.h" |
| 17 #include "crypto/nss_util.h" | 18 #include "crypto/nss_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 #if defined(USE_NSS) | 21 #if defined(USE_NSS) |
| 21 #include <private/pprthred.h> // PR_DetachThread | 22 #include <private/pprthred.h> // PR_DetachThread |
| 22 #endif | 23 #endif |
| 23 | 24 |
| 24 namespace net { | 25 namespace net { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 } | 75 } |
| 75 | 76 |
| 76 TEST_F(KeygenHandlerTest, SmokeTest) { | 77 TEST_F(KeygenHandlerTest, SmokeTest) { |
| 77 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); | 78 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); |
| 78 handler.set_stores_key(false); // Don't leave the key-pair behind | 79 handler.set_stores_key(false); // Don't leave the key-pair behind |
| 79 std::string result = handler.GenKeyAndSignChallenge(); | 80 std::string result = handler.GenKeyAndSignChallenge(); |
| 80 VLOG(1) << "KeygenHandler produced: " << result; | 81 VLOG(1) << "KeygenHandler produced: " << result; |
| 81 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); | 82 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); |
| 82 } | 83 } |
| 83 | 84 |
| 84 class ConcurrencyTestTask : public Task { | 85 void ConcurrencyTestCallback(base::WaitableEvent* event, |
| 85 public: | 86 const std::string& challenge, |
| 86 ConcurrencyTestTask(base::WaitableEvent* event, | 87 std::string* result) { |
| 87 const std::string& challenge, std::string* result) | 88 // We allow Singleton use on the worker thread here since we use a |
| 88 : event_(event), | 89 // WaitableEvent to synchronize, so it's safe. |
| 89 challenge_(challenge), | 90 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; |
| 90 result_(result) { | 91 KeygenHandler handler(768, challenge, GURL("http://www.example.com")); |
| 91 } | 92 handler.set_stores_key(false); // Don't leave the key-pair behind. |
| 92 | 93 *result = handler.GenKeyAndSignChallenge(); |
| 93 virtual void Run() { | 94 event->Signal(); |
| 94 // We allow Singleton use on the worker thread here since we use a | |
| 95 // WaitableEvent to synchronize, so it's safe. | |
| 96 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; | |
| 97 KeygenHandler handler(768, "some challenge", | |
| 98 GURL("http://www.example.com")); | |
| 99 handler.set_stores_key(false); // Don't leave the key-pair behind. | |
| 100 *result_ = handler.GenKeyAndSignChallenge(); | |
| 101 event_->Signal(); | |
| 102 #if defined(USE_NSS) | 95 #if defined(USE_NSS) |
| 103 // Detach the thread from NSPR. | 96 // Detach the thread from NSPR. |
| 104 // Calling NSS functions attaches the thread to NSPR, which stores | 97 // Calling NSS functions attaches the thread to NSPR, which stores |
| 105 // the NSPR thread ID in thread-specific data. | 98 // the NSPR thread ID in thread-specific data. |
| 106 // The threads in our thread pool terminate after we have called | 99 // The threads in our thread pool terminate after we have called |
| 107 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 100 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 108 // segfaults on shutdown when the threads' thread-specific data | 101 // segfaults on shutdown when the threads' thread-specific data |
| 109 // destructors run. | 102 // destructors run. |
| 110 PR_DetachThread(); | 103 PR_DetachThread(); |
| 111 #endif | 104 #endif |
| 112 } | 105 } |
| 113 | |
| 114 private: | |
| 115 base::WaitableEvent* event_; | |
| 116 std::string challenge_; | |
| 117 std::string* result_; | |
| 118 }; | |
| 119 | 106 |
| 120 // We asynchronously generate the keys so as not to hang up the IO thread. This | 107 // We asynchronously generate the keys so as not to hang up the IO thread. This |
| 121 // test tries to catch concurrency problems in the keygen implementation. | 108 // test tries to catch concurrency problems in the keygen implementation. |
| 122 TEST_F(KeygenHandlerTest, ConcurrencyTest) { | 109 TEST_F(KeygenHandlerTest, ConcurrencyTest) { |
| 123 const int NUM_HANDLERS = 5; | 110 const int NUM_HANDLERS = 5; |
| 124 base::WaitableEvent* events[NUM_HANDLERS] = { NULL }; | 111 base::WaitableEvent* events[NUM_HANDLERS] = { NULL }; |
| 125 std::string results[NUM_HANDLERS]; | 112 std::string results[NUM_HANDLERS]; |
| 126 for (int i = 0; i < NUM_HANDLERS; i++) { | 113 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 127 events[i] = new base::WaitableEvent(false, false); | 114 events[i] = new base::WaitableEvent(false, false); |
| 128 base::WorkerPool::PostTask( | 115 base::WorkerPool::PostTask( |
| 129 FROM_HERE, | 116 FROM_HERE, |
| 130 new ConcurrencyTestTask(events[i], "some challenge", &results[i]), | 117 base::Bind(ConcurrencyTestCallback, events[i], "some challenge", |
| 118 &results[i]), |
| 131 true); | 119 true); |
| 132 } | 120 } |
| 133 | 121 |
| 134 for (int i = 0; i < NUM_HANDLERS; i++) { | 122 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 135 // Make sure the job completed | 123 // Make sure the job completed |
| 136 events[i]->Wait(); | 124 events[i]->Wait(); |
| 137 delete events[i]; | 125 delete events[i]; |
| 138 events[i] = NULL; | 126 events[i] = NULL; |
| 139 | 127 |
| 140 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; | 128 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; |
| 141 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); | 129 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); |
| 142 } | 130 } |
| 143 } | 131 } |
| 144 | 132 |
| 145 } // namespace | 133 } // namespace |
| 146 | 134 |
| 147 } // namespace net | 135 } // namespace net |
| OLD | NEW |