From 8d4c4bb33c199a0295d63a3ebfb7443c69d1f78c Mon Sep 17 00:00:00 2001 From: "hclam@chromium.org" Date: Thu, 23 Dec 2010 21:15:03 +0000 Subject: Use certificate and key files instead of generating them each time for unit tests. BUG= TEST= Review URL: http://codereview.chromium.org/6018008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@70097 0039d316-1c4b-4281-b951-d872f2087c98 --- remoting/protocol/jingle_session_manager.cc | 27 ++++++++++++++++++--------- remoting/protocol/jingle_session_manager.h | 13 ++++++++++++- remoting/protocol/jingle_session_unittest.cc | 28 ++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 10 deletions(-) diff --git a/remoting/protocol/jingle_session_manager.cc b/remoting/protocol/jingle_session_manager.cc index 5a99a3f..1ca26bd 100644 --- a/remoting/protocol/jingle_session_manager.cc +++ b/remoting/protocol/jingle_session_manager.cc @@ -279,16 +279,17 @@ void JingleSessionManager::OnSessionCreate( if (incoming) { // Generate private key and certificate. // TODO(hclam): Instead of generating we should restore them from the disk. - scoped_ptr private_key( - base::RSAPrivateKey::Create(1024)); - std::string subject = "CN=chromoting"; - scoped_refptr x509_certificate = - net::X509Certificate::CreateSelfSigned( - private_key.get(), subject, 1, base::TimeDelta::FromDays(1)); - CHECK(x509_certificate); + if (!certificate_) { + private_key_.reset(base::RSAPrivateKey::Create(1024)); + certificate_ = net::X509Certificate::CreateSelfSigned( + private_key_.get(), "CN=chromoting", 1, + base::TimeDelta::FromDays(1)); + CHECK(certificate_); + } JingleSession* jingle_session = - JingleSession::CreateServerSession(this, x509_certificate, - private_key.get()); + JingleSession::CreateServerSession(this, certificate_, + private_key_.release()); + certificate_ = NULL; sessions_.push_back(make_scoped_refptr(jingle_session)); jingle_session->Init(cricket_session); } @@ -530,6 +531,14 @@ bool JingleSessionManager::WriteContent( return true; } +void JingleSessionManager::SetCertificate(net::X509Certificate* certificate) { + certificate_ = certificate; +} + +void JingleSessionManager::SetPrivateKey(base::RSAPrivateKey* private_key) { + private_key_.reset(private_key); +} + cricket::SessionDescription* JingleSessionManager::CreateSessionDescription( const CandidateSessionConfig* config, const std::string& auth_token, diff --git a/remoting/protocol/jingle_session_manager.h b/remoting/protocol/jingle_session_manager.h index 8dcce03..3656e05 100644 --- a/remoting/protocol/jingle_session_manager.h +++ b/remoting/protocol/jingle_session_manager.h @@ -19,6 +19,10 @@ class MessageLoop; +namespace base { +class RSAPrivateKey; +} // namespace base + namespace cricket { class SessionManager; } // namespace cricket @@ -101,6 +105,11 @@ class JingleSessionManager buzz::XmlElement** elem, cricket::WriteError* error); + // Set the certificate and private key if they are provided externally. + // TODO(hclam): Combine these two methods. + virtual void SetCertificate(net::X509Certificate* certificate); + virtual void SetPrivateKey(base::RSAPrivateKey* private_key); + protected: virtual ~JingleSessionManager(); @@ -134,11 +143,13 @@ class JingleSessionManager cricket::SessionManager* cricket_session_manager_; scoped_ptr incoming_session_callback_; bool allow_local_ips_; - bool closed_; std::list > sessions_; + scoped_refptr certificate_; + scoped_ptr private_key_; + DISALLOW_COPY_AND_ASSIGN(JingleSessionManager); }; diff --git a/remoting/protocol/jingle_session_unittest.cc b/remoting/protocol/jingle_session_unittest.cc index cdcd069..252f113 100644 --- a/remoting/protocol/jingle_session_unittest.cc +++ b/remoting/protocol/jingle_session_unittest.cc @@ -132,6 +132,34 @@ class JingleSessionTest : public testing::Test { session_manager_pair_->client_session_manager(), NewCallback(&client_server_callback_, &MockSessionManagerCallback::OnIncomingSession)); + + FilePath certs_dir; + PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); + certs_dir = certs_dir.AppendASCII("net"); + certs_dir = certs_dir.AppendASCII("data"); + certs_dir = certs_dir.AppendASCII("ssl"); + certs_dir = certs_dir.AppendASCII("certificates"); + + FilePath cert_path = certs_dir.AppendASCII("unittest.selfsigned.der"); + std::string cert_der; + ASSERT_TRUE(file_util::ReadFileToString(cert_path, &cert_der)); + + scoped_refptr cert = + net::X509Certificate::CreateFromBytes(cert_der.data(), + cert_der.size()); + + FilePath key_path = certs_dir.AppendASCII("unittest.key.bin"); + std::string key_string; + ASSERT_TRUE(file_util::ReadFileToString(key_path, &key_string)); + std::vector key_vector( + reinterpret_cast(key_string.data()), + reinterpret_cast(key_string.data() + + key_string.length())); + + scoped_ptr private_key( + base::RSAPrivateKey::CreateFromPrivateKeyInfo(key_vector)); + host_server_->SetCertificate(cert); + host_server_->SetPrivateKey(private_key.release()); } bool InitiateConnection() { -- cgit v1.1