| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "build/build_config.h" // Needs to be imported early for USE_NSS | |
| 8 | |
| 9 #if defined(USE_NSS) | |
| 10 #include <private/pprthred.h> // PR_DetachThread | |
| 11 #endif | |
| 12 | |
| 13 #include <string> | 7 #include <string> |
| 14 | 8 |
| 9 #include "build/build_config.h" |
| 15 #include "base/base64.h" | 10 #include "base/base64.h" |
| 16 #include "base/logging.h" | 11 #include "base/logging.h" |
| 17 #include "base/nss_util.h" | 12 #include "base/nss_util.h" |
| 18 #include "base/task.h" | 13 #include "base/task.h" |
| 19 #include "base/thread_restrictions.h" | 14 #include "base/thread_restrictions.h" |
| 20 #include "base/waitable_event.h" | 15 #include "base/waitable_event.h" |
| 21 #include "base/worker_pool.h" | 16 #include "base/worker_pool.h" |
| 22 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 23 | 18 |
| 19 #if defined(USE_NSS) |
| 20 #include <private/pprthred.h> // PR_DetachThread |
| 21 #endif |
| 22 |
| 24 namespace net { | 23 namespace net { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 class KeygenHandlerTest : public ::testing::Test { | 27 class KeygenHandlerTest : public ::testing::Test { |
| 29 public: | 28 public: |
| 30 KeygenHandlerTest() {} | 29 KeygenHandlerTest() {} |
| 31 virtual ~KeygenHandlerTest() {} | 30 virtual ~KeygenHandlerTest() {} |
| 32 | 31 |
| 33 virtual void SetUp() { | 32 virtual void SetUp() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 // Exponent: 65537 (0x10001) | 65 // Exponent: 65537 (0x10001) |
| 67 // Challenge String: some challenge | 66 // Challenge String: some challenge |
| 68 // Signature Algorithm: md5WithRSAEncryption | 67 // Signature Algorithm: md5WithRSAEncryption |
| 69 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... | 68 // 92:f3:cc:ff:0b:d3:d0:4a:3a:4c:ba:ff:d6:38:7f:a5:4b:b5: ..... |
| 70 // Signature OK | 69 // Signature OK |
| 71 // | 70 // |
| 72 // The value of |spkac| can be ASN.1-parsed with: | 71 // The value of |spkac| can be ASN.1-parsed with: |
| 73 // openssl asn1parse -inform DER | 72 // openssl asn1parse -inform DER |
| 74 } | 73 } |
| 75 | 74 |
| 76 // Keygen not yet implemented for OpenSSL: http://crbug.com/64917 | 75 TEST_F(KeygenHandlerTest, SmokeTest) { |
| 77 #if defined(USE_OPENSSL) | |
| 78 #define MAYBE_SmokeTest FAILS_SmokeTest | |
| 79 #else | |
| 80 #define MAYBE_SmokeTest SmokeTest | |
| 81 #endif | |
| 82 TEST_F(KeygenHandlerTest, MAYBE_SmokeTest) { | |
| 83 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); | 76 KeygenHandler handler(768, "some challenge", GURL("http://www.example.com")); |
| 84 handler.set_stores_key(false); // Don't leave the key-pair behind | 77 handler.set_stores_key(false); // Don't leave the key-pair behind |
| 85 std::string result = handler.GenKeyAndSignChallenge(); | 78 std::string result = handler.GenKeyAndSignChallenge(); |
| 86 VLOG(1) << "KeygenHandler produced: " << result; | 79 VLOG(1) << "KeygenHandler produced: " << result; |
| 87 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); | 80 AssertValidSignedPublicKeyAndChallenge(result, "some challenge"); |
| 88 } | 81 } |
| 89 | 82 |
| 90 class ConcurrencyTestTask : public Task { | 83 class ConcurrencyTestTask : public Task { |
| 91 public: | 84 public: |
| 92 ConcurrencyTestTask(base::WaitableEvent* event, | 85 ConcurrencyTestTask(base::WaitableEvent* event, |
| 93 const std::string& challenge, std::string* result) | 86 const std::string& challenge, std::string* result) |
| 94 : event_(event), | 87 : event_(event), |
| 95 challenge_(challenge), | 88 challenge_(challenge), |
| 96 result_(result) { | 89 result_(result) { |
| 97 } | 90 } |
| 98 | 91 |
| 99 virtual void Run() { | 92 virtual void Run() { |
| 100 // We allow Singleton use on the worker thread here since we use a | 93 // We allow Singleton use on the worker thread here since we use a |
| 101 // WaitableEvent to synchronize, so it's safe. | 94 // WaitableEvent to synchronize, so it's safe. |
| 102 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; | 95 base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton; |
| 103 KeygenHandler handler(768, "some challenge", | 96 KeygenHandler handler(768, "some challenge", |
| 104 GURL("http://www.example.com")); | 97 GURL("http://www.example.com")); |
| 105 handler.set_stores_key(false); // Don't leave the key-pair behind. | 98 handler.set_stores_key(false); // Don't leave the key-pair behind. |
| 106 *result_ = handler.GenKeyAndSignChallenge(); | 99 *result_ = handler.GenKeyAndSignChallenge(); |
| 107 event_->Signal(); | 100 event_->Signal(); |
| 108 #if defined(USE_NSS) | 101 #if defined(USE_NSS) |
| 109 // Detach the thread from NSPR. | 102 // Detach the thread from NSPR. |
| 110 // Calling NSS functions attaches the thread to NSPR, which stores | 103 // Calling NSS functions attaches the thread to NSPR, which stores |
| 111 // the NSPR thread ID in thread-specific data. | 104 // the NSPR thread ID in thread-specific data. |
| 112 // The threads in our thread pool terminate after we have called | 105 // The threads in our thread pool terminate after we have called |
| 113 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets | 106 // PR_Cleanup. Unless we detach them from NSPR, net_unittests gets |
| 114 // segfaults on shutdown when the threads' thread-specific data | 107 // segfaults on shutdown when the threads' thread-specific data |
| 115 // destructors run. | 108 // destructors run. |
| 116 PR_DetachThread(); | 109 PR_DetachThread(); |
| 117 #endif | 110 #endif |
| 118 } | 111 } |
| 119 | 112 |
| 120 private: | 113 private: |
| 121 base::WaitableEvent* event_; | 114 base::WaitableEvent* event_; |
| 122 std::string challenge_; | 115 std::string challenge_; |
| 123 std::string* result_; | 116 std::string* result_; |
| 124 }; | 117 }; |
| 125 | 118 |
| 126 // Keygen not yet implemented for OpenSSL: http://crbug.com/64917 | |
| 127 #if defined(USE_OPENSSL) | |
| 128 #define MAYBE_ConcurrencyTest FAILS_ConcurrencyTest | |
| 129 #else | |
| 130 #define MAYBE_ConcurrencyTest ConcurrencyTest | |
| 131 #endif | |
| 132 // We asynchronously generate the keys so as not to hang up the IO thread. This | 119 // We asynchronously generate the keys so as not to hang up the IO thread. This |
| 133 // test tries to catch concurrency problems in the keygen implementation. | 120 // test tries to catch concurrency problems in the keygen implementation. |
| 134 TEST_F(KeygenHandlerTest, MAYBE_ConcurrencyTest) { | 121 TEST_F(KeygenHandlerTest, ConcurrencyTest) { |
| 135 const int NUM_HANDLERS = 5; | 122 const int NUM_HANDLERS = 5; |
| 136 base::WaitableEvent* events[NUM_HANDLERS] = { NULL }; | 123 base::WaitableEvent* events[NUM_HANDLERS] = { NULL }; |
| 137 std::string results[NUM_HANDLERS]; | 124 std::string results[NUM_HANDLERS]; |
| 138 for (int i = 0; i < NUM_HANDLERS; i++) { | 125 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 139 events[i] = new base::WaitableEvent(false, false); | 126 events[i] = new base::WaitableEvent(false, false); |
| 140 WorkerPool::PostTask(FROM_HERE, | 127 WorkerPool::PostTask(FROM_HERE, |
| 141 new ConcurrencyTestTask(events[i], "some challenge", | 128 new ConcurrencyTestTask(events[i], "some challenge", |
| 142 &results[i]), | 129 &results[i]), |
| 143 true); | 130 true); |
| 144 } | 131 } |
| 145 | 132 |
| 146 for (int i = 0; i < NUM_HANDLERS; i++) { | 133 for (int i = 0; i < NUM_HANDLERS; i++) { |
| 147 // Make sure the job completed | 134 // Make sure the job completed |
| 148 bool signaled = events[i]->Wait(); | 135 bool signaled = events[i]->Wait(); |
| 149 EXPECT_TRUE(signaled); | 136 EXPECT_TRUE(signaled); |
| 150 delete events[i]; | 137 delete events[i]; |
| 151 events[i] = NULL; | 138 events[i] = NULL; |
| 152 | 139 |
| 153 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; | 140 VLOG(1) << "KeygenHandler " << i << " produced: " << results[i]; |
| 154 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); | 141 AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge"); |
| 155 } | 142 } |
| 156 } | 143 } |
| 157 | 144 |
| 158 } // namespace | 145 } // namespace |
| 159 | 146 |
| 160 } // namespace net | 147 } // namespace net |
| OLD | NEW |