Home | History | Annotate | Download | only in media
      1 // Copyright 2013 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <deque>
      6 
      7 #include "content/common/media/webrtc_identity_messages.h"
      8 #include "content/renderer/media/webrtc_identity_service.h"
      9 #include "ipc/ipc_message.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace content {
     13 
     14 namespace {
     15 
     16 static const char FAKE_ORIGIN[] = "http://fake.com";
     17 static const char FAKE_IDENTITY_NAME[] = "fake identity";
     18 static const char FAKE_COMMON_NAME[] = "fake common name";
     19 static const char FAKE_CERTIFICATE[] = "fake cert";
     20 static const char FAKE_PRIVATE_KEY[] = "fake private key";
     21 static const int FAKE_ERROR = 100;
     22 
     23 class WebRTCIdentityServiceForTest : public WebRTCIdentityService {
     24  public:
     25   virtual bool Send(IPC::Message* message) OVERRIDE {
     26     messages_.push_back(*message);
     27     delete message;
     28     return true;
     29   }
     30 
     31   virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE {
     32     return WebRTCIdentityService::OnControlMessageReceived(message);
     33   }
     34 
     35   IPC::Message GetLastMessage() { return messages_.back(); }
     36 
     37   int GetNumberOfMessages() { return messages_.size(); }
     38 
     39   void ClearMessages() { messages_.clear(); }
     40 
     41  private:
     42   std::deque<IPC::Message> messages_;
     43 };
     44 
     45 class WebRTCIdentityServiceTest : public ::testing::Test {
     46  public:
     47   WebRTCIdentityServiceTest()
     48       : service_(new WebRTCIdentityServiceForTest()), last_error_(0) {}
     49 
     50  protected:
     51   void OnIdentityReady(const std::string& cert, const std::string& key) {
     52     last_certificate_ = cert;
     53     last_private_key_ = key;
     54   }
     55 
     56   void OnRequestFailed(int error) { last_error_ = error; }
     57 
     58   void ResetRequestResult() {
     59     last_certificate_ = "";
     60     last_private_key_ = "";
     61     last_error_ = 0;
     62   }
     63 
     64   int RequestIdentity() {
     65     return service_->RequestIdentity(
     66         GURL(FAKE_ORIGIN),
     67         FAKE_IDENTITY_NAME,
     68         FAKE_COMMON_NAME,
     69         base::Bind(&WebRTCIdentityServiceTest::OnIdentityReady,
     70                    base::Unretained(this)),
     71         base::Bind(&WebRTCIdentityServiceTest::OnRequestFailed,
     72                    base::Unretained(this)));
     73   }
     74 
     75   scoped_ptr<WebRTCIdentityServiceForTest> service_;
     76   std::string last_certificate_;
     77   std::string last_private_key_;
     78   int last_error_;
     79 };
     80 
     81 }  // namespace
     82 
     83 TEST_F(WebRTCIdentityServiceTest, TestSendRequest) {
     84   RequestIdentity();
     85 
     86   IPC::Message ipc = service_->GetLastMessage();
     87   EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
     88 }
     89 
     90 TEST_F(WebRTCIdentityServiceTest, TestSuccessCallback) {
     91   int id = RequestIdentity();
     92 
     93   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
     94       id, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
     95   EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
     96   EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
     97 }
     98 
     99 TEST_F(WebRTCIdentityServiceTest, TestFailureCallback) {
    100   int id = RequestIdentity();
    101 
    102   service_->OnControlMessageReceived(
    103       WebRTCIdentityHostMsg_RequestFailed(id, FAKE_ERROR));
    104   EXPECT_EQ(FAKE_ERROR, last_error_);
    105 }
    106 
    107 TEST_F(WebRTCIdentityServiceTest, TestCancelRequest) {
    108   int request_id = RequestIdentity();
    109   service_->ClearMessages();
    110 
    111   service_->CancelRequest(request_id);
    112 
    113   IPC::Message ipc = service_->GetLastMessage();
    114   EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_CancelRequest::ID);
    115 }
    116 
    117 TEST_F(WebRTCIdentityServiceTest, TestQueuedRequestSentAfterSuccess) {
    118   int id = RequestIdentity();
    119   RequestIdentity();
    120   EXPECT_EQ(1, service_->GetNumberOfMessages());
    121   service_->ClearMessages();
    122 
    123   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
    124       id, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
    125 
    126   IPC::Message ipc = service_->GetLastMessage();
    127   EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
    128 }
    129 
    130 TEST_F(WebRTCIdentityServiceTest, TestQueuedRequestSentAfterFailure) {
    131   int id = RequestIdentity();
    132   RequestIdentity();
    133   EXPECT_EQ(1, service_->GetNumberOfMessages());
    134   service_->ClearMessages();
    135 
    136   service_->OnControlMessageReceived(
    137       WebRTCIdentityHostMsg_RequestFailed(id, FAKE_ERROR));
    138 
    139   IPC::Message ipc = service_->GetLastMessage();
    140   EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
    141 }
    142 
    143 TEST_F(WebRTCIdentityServiceTest, TestQueuedRequestSentAfterCancelOutstanding) {
    144   int outstand_request_id = RequestIdentity();
    145   RequestIdentity();
    146 
    147   EXPECT_EQ(1, service_->GetNumberOfMessages());
    148   service_->ClearMessages();
    149 
    150   service_->CancelRequest(outstand_request_id);
    151 
    152   // Should have two messages sent: one for cancelling the outstanding request,
    153   // one for requesting the queued request.
    154   EXPECT_EQ(2, service_->GetNumberOfMessages());
    155   IPC::Message ipc = service_->GetLastMessage();
    156   EXPECT_EQ(ipc.type(), WebRTCIdentityMsg_RequestIdentity::ID);
    157 }
    158 
    159 TEST_F(WebRTCIdentityServiceTest, TestCancelQueuedRequest) {
    160   int sent_id = RequestIdentity();
    161   int queued_request_id = RequestIdentity();
    162   EXPECT_EQ(1, service_->GetNumberOfMessages());
    163   service_->ClearMessages();
    164 
    165   service_->CancelRequest(queued_request_id);
    166 
    167   // Verifies that the queued request is not sent after the outstanding request
    168   // returns.
    169   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
    170       sent_id, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
    171 
    172   EXPECT_EQ(0, service_->GetNumberOfMessages());
    173 }
    174 
    175 TEST_F(WebRTCIdentityServiceTest, TestQueuedRequestSuccessCallback) {
    176   int id1 = RequestIdentity();
    177   int id2 = RequestIdentity();
    178 
    179   // Completes the outstanding request.
    180   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
    181       id1, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
    182   EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
    183   EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
    184 
    185   ResetRequestResult();
    186 
    187   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
    188       id2, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
    189   EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
    190   EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
    191 }
    192 
    193 TEST_F(WebRTCIdentityServiceTest, TestQueuedRequestFailureCallback) {
    194   int id1 = RequestIdentity();
    195   int id2 = RequestIdentity();
    196 
    197   // Completes the outstanding request.
    198   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
    199       id1, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
    200   EXPECT_EQ(FAKE_CERTIFICATE, last_certificate_);
    201   EXPECT_EQ(FAKE_PRIVATE_KEY, last_private_key_);
    202 
    203   ResetRequestResult();
    204 
    205   service_->OnControlMessageReceived(
    206       WebRTCIdentityHostMsg_RequestFailed(id2, FAKE_ERROR));
    207   EXPECT_EQ(FAKE_ERROR, last_error_);
    208 }
    209 
    210 // Verifies that receiving a response for a cancelled request does not incur the
    211 // callbacks.
    212 TEST_F(WebRTCIdentityServiceTest, TestRequestCompletedAfterCancelled) {
    213   int id1 = RequestIdentity();
    214   RequestIdentity();
    215   service_->CancelRequest(id1);
    216 
    217   service_->OnControlMessageReceived(WebRTCIdentityHostMsg_IdentityReady(
    218       id1, FAKE_CERTIFICATE, FAKE_PRIVATE_KEY));
    219 
    220   EXPECT_NE(FAKE_CERTIFICATE, last_certificate_);
    221   EXPECT_NE(FAKE_PRIVATE_KEY, last_private_key_);
    222 
    223   service_->OnControlMessageReceived(
    224       WebRTCIdentityHostMsg_RequestFailed(id1, FAKE_ERROR));
    225   EXPECT_NE(FAKE_ERROR, last_error_);
    226 }
    227 
    228 }  // namespace content
    229