17c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Copyright 2012 Google Inc. All Rights Reserved.
27c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//
37c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Licensed under the Apache License, Version 2.0 (the "License");
47c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// you may not use this file except in compliance with the License.
57c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// You may obtain a copy of the License at
67c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//
77c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//     http://www.apache.org/licenses/LICENSE-2.0
87c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet//
97c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Unless required by applicable law or agreed to in writing, software
107c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// distributed under the License is distributed on an "AS IS" BASIS,
117c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
127c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// See the License for the specific language governing permissions and
137c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// limitations under the License.
147c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
157c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet// Tests for ServerPairingSession.
167c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
177c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include <gtest/gtest.h>
187c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include <polo/pairing/serverpairingsession.h>
197c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include "polo/pairing/mocks.h"
207c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet#include "polo/wire/mocks.h"
217c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
227c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetusing ::testing::InSequence;
237c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetusing ::testing::Mock;
247c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetusing ::testing::Return;
257c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetusing ::testing::StrictMock;
267c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetusing ::testing::_;
277c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
287c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetnamespace polo {
297c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetnamespace pairing {
307c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
317c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetclass TestServerPairingSession : public ServerPairingSession {
327c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet public:
337c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  TestServerPairingSession(wire::PoloWireAdapter* wire,
347c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet                          PairingContext* context,
357c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet                          PoloChallengeResponse* challenge)
367c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet      : ServerPairingSession(wire, context, challenge, "server1") {
377c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
387c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
397c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  void TestDoInitializationPhase() {
407c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    DoInitializationPhase();
417c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
427c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
437c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  void TestDoConfigurationPhase() {
447c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    DoConfigurationPhase();
457c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
467c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
477c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  bool TestSetConfiguration(const message::ConfigurationMessage& config) {
487c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    return SetConfiguration(config);
497c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
507c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet};
517c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
527c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichetclass ServerPairingSessionTest : public ::testing::Test {
537c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet protected:
547c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  ServerPairingSessionTest()
557c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet      : interface_(),
567c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        wire_(&interface_),
577c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        challenge_(),
587c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        context_(NULL, NULL, true),
597c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet        session_(&wire_, &context_, &challenge_) {
607c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
617c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
627c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  virtual void SetUp() {
637c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
647c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
657c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  virtual void TearDown() {
667c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
677c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
687c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  void InitSession() {
697c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    InSequence sequence;
707c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
717c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    EXPECT_CALL(listener_, OnSessionCreated());
727c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    EXPECT_CALL(wire_, GetNextMessage());
737c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
747c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet    session_.DoPair(&listener_);
757c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  }
767c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
777c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  StrictMock<wire::MockWireInterface> interface_;
787c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  StrictMock<wire::MockWireAdapter> wire_;
797c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  StrictMock<MockChallengeResponse> challenge_;
807c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  PairingContext context_;
817c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  StrictMock<MockPairingListener> listener_;
827c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  StrictMock<TestServerPairingSession> session_;
837c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet};
847c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
857c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(ServerPairingSessionTest, DoInitializationPhase) {
867c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InitSession();
877c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
887c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
897c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(ServerPairingSessionTest, DoConfigurationPhase) {
907c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InitSession();
917c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InSequence sequence;
927c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, GetNextMessage());
937c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
947c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  session_.TestDoInitializationPhase();
957c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
967c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
977c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(ServerPairingSessionTest, OnPairingRequestMessage) {
987c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InitSession();
997c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InSequence sequence;
1007c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, SendPairingRequestAckMessage(_));
1017c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, GetNextMessage());
1027c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1037c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  message::PairingRequestMessage message("service1");
1047c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  session_.OnPairingRequestMessage(message);
1057c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1067c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1077c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(ServerPairingSessionTest, OnOptionsMessage) {
1087c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InitSession();
1097c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InSequence sequence;
1107c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, SendOptionsMessage(_));
1117c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, GetNextMessage());
1127c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1137c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  message::OptionsMessage message;
1147c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  session_.OnOptionsMessage(message);
1157c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1167c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1177c9978567a202d6aa98beac5da5e1b3b34792862Jerome PoichetTEST_F(ServerPairingSessionTest, OnConfigurationMessage) {
1187c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  encoding::EncodingOption encoding(encoding::EncodingOption::kHexadecimal, 8);
1197c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  session_.AddInputEncoding(encoding);
1207c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  session_.AddOutputEncoding(encoding);
1217c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1227c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InitSession();
1237c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1247c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  InSequence sequence;
1257c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, SendConfigurationAckMessage(_));
1267c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1277c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(challenge_, GetGamma(_)).WillOnce(Return(new Gamma(5, 0x5)));
1287c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(listener_, OnPerformOutputDeviceRole(Gamma(5, 0x5)));
1297c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  EXPECT_CALL(wire_, GetNextMessage());
1307c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1317c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  message::ConfigurationMessage message(encoding,
1327c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet      message::OptionsMessage::kInputDevice);
1337c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet  session_.OnConfigurationMessage(message);
1347c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}
1357c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet
1367c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}  // namespace pairing
1377c9978567a202d6aa98beac5da5e1b3b34792862Jerome Poichet}  // namespace polo
138