• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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 "cast/streaming/rpc_broker.h"
6 
7 #include <memory>
8 #include <vector>
9 
10 #include "cast/streaming/remoting.pb.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 using testing::_;
15 using testing::Invoke;
16 using testing::Return;
17 
18 namespace openscreen {
19 namespace cast {
20 
21 namespace {
22 
23 class FakeMessager {
24  public:
OnReceivedRpc(const RpcMessage & message)25   void OnReceivedRpc(const RpcMessage& message) {
26     received_rpc_ = message;
27     received_count_++;
28   }
29 
OnSentRpc(const std::vector<uint8_t> & message)30   void OnSentRpc(const std::vector<uint8_t>& message) {
31     EXPECT_TRUE(sent_rpc_.ParseFromArray(message.data(), message.size()));
32     sent_count_++;
33   }
34 
received_count() const35   int received_count() const { return received_count_; }
received_rpc() const36   const RpcMessage& received_rpc() const { return received_rpc_; }
37 
sent_count() const38   int sent_count() const { return sent_count_; }
sent_rpc() const39   const RpcMessage& sent_rpc() const { return sent_rpc_; }
40 
set_handle(RpcBroker::Handle handle)41   void set_handle(RpcBroker::Handle handle) { handle_ = handle; }
handle()42   RpcBroker::Handle handle() { return handle_; }
43 
44  private:
45   RpcMessage received_rpc_;
46   int received_count_ = 0;
47 
48   RpcMessage sent_rpc_;
49   int sent_count_ = 0;
50 
51   RpcBroker::Handle handle_ = -1;
52 };
53 
54 }  // namespace
55 
56 class RpcBrokerTest : public testing::Test {
57  protected:
SetUp()58   void SetUp() override {
59     fake_messager_ = std::make_unique<FakeMessager>();
60     ASSERT_FALSE(fake_messager_->received_count());
61 
62     rpc_broker_ = std::make_unique<RpcBroker>(
63         [p = fake_messager_.get()](std::vector<uint8_t> message) {
64           p->OnSentRpc(message);
65         });
66 
67     const auto handle = rpc_broker_->GetUniqueHandle();
68     fake_messager_->set_handle(handle);
69     rpc_broker_->RegisterMessageReceiverCallback(
70         handle, [p = fake_messager_.get()](const RpcMessage& message) {
71           p->OnReceivedRpc(message);
72         });
73   }
74 
75   std::unique_ptr<FakeMessager> fake_messager_;
76   std::unique_ptr<RpcBroker> rpc_broker_;
77 };
78 
TEST_F(RpcBrokerTest,TestProcessMessageFromRemoteRegistered)79 TEST_F(RpcBrokerTest, TestProcessMessageFromRemoteRegistered) {
80   RpcMessage rpc;
81   rpc.set_handle(fake_messager_->handle());
82   rpc_broker_->ProcessMessageFromRemote(rpc);
83   ASSERT_EQ(1, fake_messager_->received_count());
84 }
85 
TEST_F(RpcBrokerTest,TestProcessMessageFromRemoteUnregistered)86 TEST_F(RpcBrokerTest, TestProcessMessageFromRemoteUnregistered) {
87   RpcMessage rpc;
88   rpc_broker_->UnregisterMessageReceiverCallback(fake_messager_->handle());
89   rpc_broker_->ProcessMessageFromRemote(rpc);
90   ASSERT_EQ(0, fake_messager_->received_count());
91 }
92 
TEST_F(RpcBrokerTest,CanSendMultipleMessages)93 TEST_F(RpcBrokerTest, CanSendMultipleMessages) {
94   for (int i = 0; i < 10; ++i) {
95     rpc_broker_->SendMessageToRemote(RpcMessage{});
96   }
97   EXPECT_EQ(10, fake_messager_->sent_count());
98 }
99 
TEST_F(RpcBrokerTest,SendMessageCallback)100 TEST_F(RpcBrokerTest, SendMessageCallback) {
101   // Send message for RPC broker to process.
102   RpcMessage sent_rpc;
103   sent_rpc.set_handle(fake_messager_->handle());
104   sent_rpc.set_proc(RpcMessage::RPC_R_SETVOLUME);
105   sent_rpc.set_double_value(2.3);
106   rpc_broker_->SendMessageToRemote(sent_rpc);
107 
108   // Check if received message is identical to the one sent earlier.
109   ASSERT_EQ(1, fake_messager_->sent_count());
110   const RpcMessage& message = fake_messager_->sent_rpc();
111   ASSERT_EQ(fake_messager_->handle(), message.handle());
112   ASSERT_EQ(RpcMessage::RPC_R_SETVOLUME, message.proc());
113   ASSERT_EQ(2.3, message.double_value());
114 }
115 
TEST_F(RpcBrokerTest,ProcessMessageWithRegisteredHandle)116 TEST_F(RpcBrokerTest, ProcessMessageWithRegisteredHandle) {
117   // Send message for RPC broker to process.
118   RpcMessage sent_rpc;
119   sent_rpc.set_handle(fake_messager_->handle());
120   sent_rpc.set_proc(RpcMessage::RPC_R_SETVOLUME);
121   sent_rpc.set_double_value(3.4);
122   rpc_broker_->ProcessMessageFromRemote(sent_rpc);
123 
124   // Checks if received message is identical to the one sent earlier.
125   ASSERT_EQ(1, fake_messager_->received_count());
126   const RpcMessage& received_rpc = fake_messager_->received_rpc();
127   ASSERT_EQ(fake_messager_->handle(), received_rpc.handle());
128   ASSERT_EQ(RpcMessage::RPC_R_SETVOLUME, received_rpc.proc());
129   ASSERT_EQ(3.4, received_rpc.double_value());
130 }
131 
TEST_F(RpcBrokerTest,ProcessMessageWithUnregisteredHandle)132 TEST_F(RpcBrokerTest, ProcessMessageWithUnregisteredHandle) {
133   // Send message for RPC broker to process.
134   RpcMessage sent_rpc;
135   RpcBroker::Handle different_handle = fake_messager_->handle() + 1;
136   sent_rpc.set_handle(different_handle);
137   sent_rpc.set_proc(RpcMessage::RPC_R_SETVOLUME);
138   sent_rpc.set_double_value(4.5);
139   rpc_broker_->ProcessMessageFromRemote(sent_rpc);
140 
141   // We shouldn't have gotten the message since the handle is different.
142   ASSERT_EQ(0, fake_messager_->received_count());
143 }
144 
TEST_F(RpcBrokerTest,Registration)145 TEST_F(RpcBrokerTest, Registration) {
146   const auto handle = fake_messager_->handle();
147   ASSERT_TRUE(rpc_broker_->IsRegisteredForTesting(handle));
148 
149   rpc_broker_->UnregisterMessageReceiverCallback(handle);
150   ASSERT_FALSE(rpc_broker_->IsRegisteredForTesting(handle));
151 }
152 
153 }  // namespace cast
154 }  // namespace openscreen
155