1 // Copyright (c) 2012 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.
5 #include "remoting/host/register_support_host_request.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/observer_list.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "remoting/base/constants.h"
14 #include "remoting/base/rsa_key_pair.h"
15 #include "remoting/base/test_rsa_key_pair.h"
16 #include "remoting/signaling/iq_sender.h"
17 #include "remoting/signaling/mock_signal_strategy.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "third_party/webrtc/libjingle/xmllite/xmlelement.h"
21 #include "third_party/webrtc/libjingle/xmpp/constants.h"
24 using buzz::XmlElement
;
27 using testing::Invoke
;
28 using testing::NotNull
;
29 using testing::Return
;
30 using testing::SaveArg
;
35 const char kTestBotJid
[] = "remotingunittest@bot.talk.google.com";
36 const char kTestJid
[] = "user@gmail.com/chromoting123";
37 const char kSupportId
[] = "AB4RF3";
38 const char kSupportIdLifetime
[] = "300";
39 const char kStanzaId
[] = "123";
41 ACTION_P(AddListener
, list
) {
42 list
->AddObserver(arg0
);
44 ACTION_P(RemoveListener
, list
) {
45 list
->RemoveObserver(arg0
);
50 MOCK_METHOD3(OnResponse
, void(bool result
, const std::string
& support_id
,
51 const base::TimeDelta
& lifetime
));
56 class RegisterSupportHostRequestTest
: public testing::Test
{
59 void SetUp() override
{
60 key_pair_
= RsaKeyPair::FromString(kTestRsaKeyPair
);
61 ASSERT_TRUE(key_pair_
.get());
63 EXPECT_CALL(signal_strategy_
, AddListener(NotNull()))
64 .WillRepeatedly(AddListener(&signal_strategy_listeners_
));
65 EXPECT_CALL(signal_strategy_
, RemoveListener(NotNull()))
66 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_
));
67 EXPECT_CALL(signal_strategy_
, GetLocalJid())
68 .WillRepeatedly(Return(kTestJid
));
71 base::MessageLoop message_loop_
;
72 MockSignalStrategy signal_strategy_
;
73 ObserverList
<SignalStrategy::Listener
, true> signal_strategy_listeners_
;
74 scoped_refptr
<RsaKeyPair
> key_pair_
;
75 MockCallback callback_
;
78 TEST_F(RegisterSupportHostRequestTest
, Send
) {
79 // |iq_request| is freed by RegisterSupportHostRequest.
80 int64 start_time
= static_cast<int64
>(base::Time::Now().ToDoubleT());
82 scoped_ptr
<RegisterSupportHostRequest
> request(
83 new RegisterSupportHostRequest(&signal_strategy_
, key_pair_
,
85 base::Bind(&MockCallback::OnResponse
,
86 base::Unretained(&callback_
))));
88 XmlElement
* sent_iq
= nullptr;
89 EXPECT_CALL(signal_strategy_
, GetNextId())
90 .WillOnce(Return(kStanzaId
));
91 EXPECT_CALL(signal_strategy_
, SendStanzaPtr(NotNull()))
92 .WillOnce(DoAll(SaveArg
<0>(&sent_iq
), Return(true)));
94 request
->OnSignalStrategyStateChange(SignalStrategy::CONNECTED
);
95 base::RunLoop().RunUntilIdle();
97 // Verify format of the query.
98 scoped_ptr
<XmlElement
> stanza(sent_iq
);
99 ASSERT_TRUE(stanza
!= nullptr);
101 EXPECT_EQ(stanza
->Attr(buzz::QName(std::string(), "to")),
102 std::string(kTestBotJid
));
103 EXPECT_EQ(stanza
->Attr(buzz::QName(std::string(), "type")), "set");
105 EXPECT_EQ(QName(kChromotingXmlNamespace
, "register-support-host"),
106 stanza
->FirstElement()->Name());
108 QName
signature_tag(kChromotingXmlNamespace
, "signature");
109 XmlElement
* signature
= stanza
->FirstElement()->FirstNamed(signature_tag
);
110 ASSERT_TRUE(signature
!= nullptr);
111 EXPECT_TRUE(stanza
->NextNamed(signature_tag
) == nullptr);
113 std::string time_str
=
114 signature
->Attr(QName(kChromotingXmlNamespace
, "time"));
116 EXPECT_TRUE(base::StringToInt64(time_str
, &time
));
117 int64 now
= static_cast<int64
>(base::Time::Now().ToDoubleT());
118 EXPECT_LE(start_time
, time
);
119 EXPECT_GE(now
, time
);
121 scoped_refptr
<RsaKeyPair
> key_pair
= RsaKeyPair::FromString(kTestRsaKeyPair
);
122 ASSERT_TRUE(key_pair
.get());
124 std::string expected_signature
=
125 key_pair
->SignMessage(std::string(kTestJid
) + ' ' + time_str
);
126 EXPECT_EQ(expected_signature
, signature
->BodyText());
128 // Generate response and verify that callback is called.
129 EXPECT_CALL(callback_
, OnResponse(true, kSupportId
,
130 base::TimeDelta::FromSeconds(300)));
132 scoped_ptr
<XmlElement
> response(new XmlElement(buzz::QN_IQ
));
133 response
->AddAttr(QName(std::string(), "from"), kTestBotJid
);
134 response
->AddAttr(QName(std::string(), "type"), "result");
135 response
->AddAttr(QName(std::string(), "id"), kStanzaId
);
137 XmlElement
* result
= new XmlElement(
138 QName(kChromotingXmlNamespace
, "register-support-host-result"));
139 response
->AddElement(result
);
141 XmlElement
* support_id
= new XmlElement(
142 QName(kChromotingXmlNamespace
, "support-id"));
143 support_id
->AddText(kSupportId
);
144 result
->AddElement(support_id
);
146 XmlElement
* support_id_lifetime
= new XmlElement(
147 QName(kChromotingXmlNamespace
, "support-id-lifetime"));
148 support_id_lifetime
->AddText(kSupportIdLifetime
);
149 result
->AddElement(support_id_lifetime
);
152 ObserverListBase
<SignalStrategy::Listener
>::Iterator
it(
153 &signal_strategy_listeners_
);
154 SignalStrategy::Listener
* listener
;
155 while ((listener
= it
.GetNext()) != nullptr) {
156 if (listener
->OnSignalStrategyIncomingStanza(response
.get()))
159 EXPECT_EQ(1, consumed
);
161 base::RunLoop().RunUntilIdle();
164 } // namespace remoting