Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / register_support_host_request_unittest.cc
blob29a3c4e9a2e43351fa713291d36981ac874862f3
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"
7 #include "base/bind.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"
23 using buzz::QName;
24 using buzz::XmlElement;
26 using testing::_;
27 using testing::Invoke;
28 using testing::NotNull;
29 using testing::Return;
30 using testing::SaveArg;
32 namespace remoting {
34 namespace {
35 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com";
36 const char kTestJid[] = "User@gmail.com/chromotingABC123";
37 const char kTestJidNormalized[] = "user@gmail.com/chromotingABC123";
38 const char kSupportId[] = "AB4RF3";
39 const char kSupportIdLifetime[] = "300";
40 const char kStanzaId[] = "123";
42 ACTION_P(AddListener, list) {
43 list->AddObserver(arg0);
45 ACTION_P(RemoveListener, list) {
46 list->RemoveObserver(arg0);
49 class MockCallback {
50 public:
51 MOCK_METHOD3(OnResponse, void(const std::string& support_id,
52 const base::TimeDelta& lifetime,
53 const std::string& error_message));
56 } // namespace
58 class RegisterSupportHostRequestTest : public testing::Test {
59 public:
60 protected:
61 void SetUp() override {
62 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair);
63 ASSERT_TRUE(key_pair_.get());
65 EXPECT_CALL(signal_strategy_, AddListener(NotNull()))
66 .WillRepeatedly(AddListener(&signal_strategy_listeners_));
67 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull()))
68 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_));
69 EXPECT_CALL(signal_strategy_, GetLocalJid())
70 .WillRepeatedly(Return(kTestJid));
73 base::MessageLoop message_loop_;
74 MockSignalStrategy signal_strategy_;
75 base::ObserverList<SignalStrategy::Listener, true> signal_strategy_listeners_;
76 scoped_refptr<RsaKeyPair> key_pair_;
77 MockCallback callback_;
80 TEST_F(RegisterSupportHostRequestTest, Send) {
81 // |iq_request| is freed by RegisterSupportHostRequest.
82 int64 start_time = static_cast<int64>(base::Time::Now().ToDoubleT());
84 scoped_ptr<RegisterSupportHostRequest> request(
85 new RegisterSupportHostRequest(&signal_strategy_, key_pair_,
86 kTestBotJid,
87 base::Bind(&MockCallback::OnResponse,
88 base::Unretained(&callback_))));
90 XmlElement* sent_iq = nullptr;
91 EXPECT_CALL(signal_strategy_, GetNextId())
92 .WillOnce(Return(kStanzaId));
93 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull()))
94 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true)));
96 request->OnSignalStrategyStateChange(SignalStrategy::CONNECTED);
97 base::RunLoop().RunUntilIdle();
99 // Verify format of the query.
100 scoped_ptr<XmlElement> stanza(sent_iq);
101 ASSERT_TRUE(stanza != nullptr);
103 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "to")),
104 std::string(kTestBotJid));
105 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "type")), "set");
107 EXPECT_EQ(QName(kChromotingXmlNamespace, "register-support-host"),
108 stanza->FirstElement()->Name());
110 QName signature_tag(kChromotingXmlNamespace, "signature");
111 XmlElement* signature = stanza->FirstElement()->FirstNamed(signature_tag);
112 ASSERT_TRUE(signature != nullptr);
113 EXPECT_TRUE(stanza->NextNamed(signature_tag) == nullptr);
115 std::string time_str =
116 signature->Attr(QName(kChromotingXmlNamespace, "time"));
117 int64 time;
118 EXPECT_TRUE(base::StringToInt64(time_str, &time));
119 int64 now = static_cast<int64>(base::Time::Now().ToDoubleT());
120 EXPECT_LE(start_time, time);
121 EXPECT_GE(now, time);
123 scoped_refptr<RsaKeyPair> key_pair = RsaKeyPair::FromString(kTestRsaKeyPair);
124 ASSERT_TRUE(key_pair.get());
126 std::string expected_signature =
127 key_pair->SignMessage(std::string(kTestJidNormalized) + ' ' + time_str);
128 EXPECT_EQ(expected_signature, signature->BodyText());
130 // Generate response and verify that callback is called.
131 EXPECT_CALL(callback_, OnResponse(kSupportId,
132 base::TimeDelta::FromSeconds(300),
133 ""));
135 scoped_ptr<XmlElement> response(new XmlElement(buzz::QN_IQ));
136 response->AddAttr(QName(std::string(), "from"), kTestBotJid);
137 response->AddAttr(QName(std::string(), "type"), "result");
138 response->AddAttr(QName(std::string(), "id"), kStanzaId);
140 XmlElement* result = new XmlElement(
141 QName(kChromotingXmlNamespace, "register-support-host-result"));
142 response->AddElement(result);
144 XmlElement* support_id = new XmlElement(
145 QName(kChromotingXmlNamespace, "support-id"));
146 support_id->AddText(kSupportId);
147 result->AddElement(support_id);
149 XmlElement* support_id_lifetime = new XmlElement(
150 QName(kChromotingXmlNamespace, "support-id-lifetime"));
151 support_id_lifetime->AddText(kSupportIdLifetime);
152 result->AddElement(support_id_lifetime);
154 int consumed = 0;
155 base::ObserverListBase<SignalStrategy::Listener>::Iterator it(
156 &signal_strategy_listeners_);
157 SignalStrategy::Listener* listener;
158 while ((listener = it.GetNext()) != nullptr) {
159 if (listener->OnSignalStrategyIncomingStanza(response.get()))
160 consumed++;
162 EXPECT_EQ(1, consumed);
164 base::RunLoop().RunUntilIdle();
167 } // namespace remoting