Home | History | Annotate | Download | only in pairing
      1 // Copyright 2012 Google Inc. All Rights Reserved.
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 // Tests for ServerPairingSession.
     16 
     17 #include <gtest/gtest.h>
     18 #include <polo/pairing/serverpairingsession.h>
     19 #include "polo/pairing/mocks.h"
     20 #include "polo/wire/mocks.h"
     21 
     22 using ::testing::InSequence;
     23 using ::testing::Mock;
     24 using ::testing::Return;
     25 using ::testing::StrictMock;
     26 using ::testing::_;
     27 
     28 namespace polo {
     29 namespace pairing {
     30 
     31 class TestServerPairingSession : public ServerPairingSession {
     32  public:
     33   TestServerPairingSession(wire::PoloWireAdapter* wire,
     34                           PairingContext* context,
     35                           PoloChallengeResponse* challenge)
     36       : ServerPairingSession(wire, context, challenge, "server1") {
     37   }
     38 
     39   void TestDoInitializationPhase() {
     40     DoInitializationPhase();
     41   }
     42 
     43   void TestDoConfigurationPhase() {
     44     DoConfigurationPhase();
     45   }
     46 
     47   bool TestSetConfiguration(const message::ConfigurationMessage& config) {
     48     return SetConfiguration(config);
     49   }
     50 };
     51 
     52 class ServerPairingSessionTest : public ::testing::Test {
     53  protected:
     54   ServerPairingSessionTest()
     55       : interface_(),
     56         wire_(&interface_),
     57         challenge_(),
     58         context_(NULL, NULL, true),
     59         session_(&wire_, &context_, &challenge_) {
     60   }
     61 
     62   virtual void SetUp() {
     63   }
     64 
     65   virtual void TearDown() {
     66   }
     67 
     68   void InitSession() {
     69     InSequence sequence;
     70 
     71     EXPECT_CALL(listener_, OnSessionCreated());
     72     EXPECT_CALL(wire_, GetNextMessage());
     73 
     74     session_.DoPair(&listener_);
     75   }
     76 
     77   StrictMock<wire::MockWireInterface> interface_;
     78   StrictMock<wire::MockWireAdapter> wire_;
     79   StrictMock<MockChallengeResponse> challenge_;
     80   PairingContext context_;
     81   StrictMock<MockPairingListener> listener_;
     82   StrictMock<TestServerPairingSession> session_;
     83 };
     84 
     85 TEST_F(ServerPairingSessionTest, DoInitializationPhase) {
     86   InitSession();
     87 }
     88 
     89 TEST_F(ServerPairingSessionTest, DoConfigurationPhase) {
     90   InitSession();
     91   InSequence sequence;
     92   EXPECT_CALL(wire_, GetNextMessage());
     93 
     94   session_.TestDoInitializationPhase();
     95 }
     96 
     97 TEST_F(ServerPairingSessionTest, OnPairingRequestMessage) {
     98   InitSession();
     99   InSequence sequence;
    100   EXPECT_CALL(wire_, SendPairingRequestAckMessage(_));
    101   EXPECT_CALL(wire_, GetNextMessage());
    102 
    103   message::PairingRequestMessage message("service1");
    104   session_.OnPairingRequestMessage(message);
    105 }
    106 
    107 TEST_F(ServerPairingSessionTest, OnOptionsMessage) {
    108   InitSession();
    109   InSequence sequence;
    110   EXPECT_CALL(wire_, SendOptionsMessage(_));
    111   EXPECT_CALL(wire_, GetNextMessage());
    112 
    113   message::OptionsMessage message;
    114   session_.OnOptionsMessage(message);
    115 }
    116 
    117 TEST_F(ServerPairingSessionTest, OnConfigurationMessage) {
    118   encoding::EncodingOption encoding(encoding::EncodingOption::kHexadecimal, 8);
    119   session_.AddInputEncoding(encoding);
    120   session_.AddOutputEncoding(encoding);
    121 
    122   InitSession();
    123 
    124   InSequence sequence;
    125   EXPECT_CALL(wire_, SendConfigurationAckMessage(_));
    126 
    127   EXPECT_CALL(challenge_, GetGamma(_)).WillOnce(Return(new Gamma(5, 0x5)));
    128   EXPECT_CALL(listener_, OnPerformOutputDeviceRole(Gamma(5, 0x5)));
    129   EXPECT_CALL(wire_, GetNextMessage());
    130 
    131   message::ConfigurationMessage message(encoding,
    132       message::OptionsMessage::kInputDevice);
    133   session_.OnConfigurationMessage(message);
    134 }
    135 
    136 }  // namespace pairing
    137 }  // namespace polo
    138