This source file includes following definitions.
- SetUp
- AssertValidSignedPublicKeyAndChallenge
- TEST_F
- ConcurrencyTestCallback
- TEST_F
#include "net/base/keygen_handler.h"
#include <string>
#include "base/base64.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/threading/worker_pool.h"
#include "base/threading/thread_restrictions.h"
#include "base/synchronization/waitable_event.h"
#include "build/build_config.h"
#include "crypto/nss_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(USE_NSS)
#include <private/pprthred.h>
#endif
namespace net {
namespace {
class KeygenHandlerTest : public ::testing::Test {
public:
KeygenHandlerTest() {}
virtual ~KeygenHandlerTest() {}
virtual void SetUp() {
#if defined(OS_CHROMEOS)
crypto::OpenPersistentNSSDB();
#endif
}
};
void AssertValidSignedPublicKeyAndChallenge(const std::string& result,
const std::string& challenge) {
ASSERT_GT(result.length(), 0U);
std::string spkac;
ASSERT_TRUE(base::Base64Decode(result, &spkac));
ASSERT_GE(spkac.length(), 200U);
ASSERT_LE(spkac.length(), 300U);
}
TEST_F(KeygenHandlerTest, SmokeTest) {
KeygenHandler handler(768, "some challenge", GURL("http://www.example.com"));
handler.set_stores_key(false);
std::string result = handler.GenKeyAndSignChallenge();
VLOG(1) << "KeygenHandler produced: " << result;
AssertValidSignedPublicKeyAndChallenge(result, "some challenge");
}
void ConcurrencyTestCallback(base::WaitableEvent* event,
const std::string& challenge,
std::string* result) {
base::ThreadRestrictions::ScopedAllowSingleton scoped_allow_singleton;
KeygenHandler handler(768, challenge, GURL("http://www.example.com"));
handler.set_stores_key(false);
*result = handler.GenKeyAndSignChallenge();
event->Signal();
#if defined(USE_NSS)
PR_DetachThread();
#endif
}
TEST_F(KeygenHandlerTest, ConcurrencyTest) {
const int NUM_HANDLERS = 5;
base::WaitableEvent* events[NUM_HANDLERS] = { NULL };
std::string results[NUM_HANDLERS];
for (int i = 0; i < NUM_HANDLERS; i++) {
events[i] = new base::WaitableEvent(false, false);
base::WorkerPool::PostTask(
FROM_HERE,
base::Bind(ConcurrencyTestCallback, events[i], "some challenge",
&results[i]),
true);
}
for (int i = 0; i < NUM_HANDLERS; i++) {
events[i]->Wait();
delete events[i];
events[i] = NULL;
VLOG(1) << "KeygenHandler " << i << " produced: " << results[i];
AssertValidSignedPublicKeyAndChallenge(results[i], "some challenge");
}
}
}
}