| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/register_support_host_request.h" | 5 #include "remoting/host/register_support_host_request.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/stringize_macros.h" | 14 #include "base/strings/stringize_macros.h" |
| 16 #include "base/test/mock_callback.h" | 15 #include "base/test/mock_callback.h" |
| 16 #include "base/test/scoped_mock_time_message_loop_task_runner.h" |
| 17 #include "remoting/base/constants.h" | 17 #include "remoting/base/constants.h" |
| 18 #include "remoting/base/rsa_key_pair.h" | 18 #include "remoting/base/rsa_key_pair.h" |
| 19 #include "remoting/base/test_rsa_key_pair.h" | 19 #include "remoting/base/test_rsa_key_pair.h" |
| 20 #include "remoting/host/host_details.h" | 20 #include "remoting/host/host_details.h" |
| 21 #include "remoting/signaling/iq_sender.h" | 21 #include "remoting/signaling/iq_sender.h" |
| 22 #include "remoting/signaling/mock_signal_strategy.h" | 22 #include "remoting/signaling/mock_signal_strategy.h" |
| 23 #include "remoting/signaling/signaling_address.h" | 23 #include "remoting/signaling/signaling_address.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" | 26 #include "third_party/libjingle_xmpp/xmllite/xmlelement.h" |
| 27 #include "third_party/libjingle_xmpp/xmpp/constants.h" | 27 #include "third_party/libjingle_xmpp/xmpp/constants.h" |
| 28 | 28 |
| 29 using buzz::QName; | 29 using buzz::QName; |
| 30 using buzz::XmlElement; | 30 using buzz::XmlElement; |
| 31 | 31 |
| 32 using testing::_; | 32 using testing::_; |
| 33 using testing::Invoke; | 33 using testing::Invoke; |
| 34 using testing::NotNull; | 34 using testing::NotNull; |
| 35 using testing::Return; | 35 using testing::Return; |
| 36 using testing::SaveArg; | 36 using testing::SaveArg; |
| 37 using testing::DeleteArg; |
| 37 | 38 |
| 38 namespace remoting { | 39 namespace remoting { |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; | 42 const char kTestBotJid[] = "remotingunittest@bot.talk.google.com"; |
| 42 const char kTestJid[] = "User@gmail.com/chromotingABC123"; | 43 const char kTestJid[] = "User@gmail.com/chromotingABC123"; |
| 43 const char kTestJidNormalized[] = "user@gmail.com/chromotingABC123"; | 44 const char kTestJidNormalized[] = "user@gmail.com/chromotingABC123"; |
| 44 const char kSupportId[] = "AB4RF3"; | 45 const char kSupportId[] = "AB4RF3"; |
| 45 const char kSupportIdLifetime[] = "300"; | 46 const char kSupportIdLifetime[] = "300"; |
| 46 const char kStanzaId[] = "123"; | 47 const char kStanzaId[] = "123"; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 64 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); | 65 key_pair_ = RsaKeyPair::FromString(kTestRsaKeyPair); |
| 65 ASSERT_TRUE(key_pair_.get()); | 66 ASSERT_TRUE(key_pair_.get()); |
| 66 | 67 |
| 67 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) | 68 EXPECT_CALL(signal_strategy_, AddListener(NotNull())) |
| 68 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); | 69 .WillRepeatedly(AddListener(&signal_strategy_listeners_)); |
| 69 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) | 70 EXPECT_CALL(signal_strategy_, RemoveListener(NotNull())) |
| 70 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); | 71 .WillRepeatedly(RemoveListener(&signal_strategy_listeners_)); |
| 71 } | 72 } |
| 72 | 73 |
| 73 base::MessageLoop message_loop_; | 74 base::MessageLoop message_loop_; |
| 75 base::ScopedMockTimeMessageLoopTaskRunner mock_time_task_runner_; |
| 74 MockSignalStrategy signal_strategy_; | 76 MockSignalStrategy signal_strategy_; |
| 75 base::ObserverList<SignalStrategy::Listener, true> signal_strategy_listeners_; | 77 base::ObserverList<SignalStrategy::Listener, true> signal_strategy_listeners_; |
| 76 scoped_refptr<RsaKeyPair> key_pair_; | 78 scoped_refptr<RsaKeyPair> key_pair_; |
| 77 base::MockCallback<RegisterSupportHostRequest::RegisterCallback> callback_; | 79 base::MockCallback<RegisterSupportHostRequest::RegisterCallback> callback_; |
| 78 }; | 80 }; |
| 79 | 81 |
| 82 TEST_F(RegisterSupportHostRequestTest, Timeout) { |
| 83 std::unique_ptr<RegisterSupportHostRequest> request( |
| 84 new RegisterSupportHostRequest(&signal_strategy_, key_pair_, kTestBotJid, |
| 85 callback_.Get())); |
| 86 EXPECT_CALL(signal_strategy_, GetNextId()).WillOnce(Return(kStanzaId)); |
| 87 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) |
| 88 .WillOnce(DoAll(DeleteArg<0>(), Return(true))); |
| 89 |
| 90 request->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| 91 |
| 92 // Generate response and verify that callback is called. |
| 93 EXPECT_CALL(callback_, Run("", base::TimeDelta::FromSeconds(0), |
| 94 "register-support-host request timed out.")); |
| 95 |
| 96 mock_time_task_runner_->FastForwardBy(base::TimeDelta::FromSeconds(15)); |
| 97 } |
| 98 |
| 80 TEST_F(RegisterSupportHostRequestTest, Send) { | 99 TEST_F(RegisterSupportHostRequestTest, Send) { |
| 81 // |iq_request| is freed by RegisterSupportHostRequest. | 100 // |iq_request| is freed by RegisterSupportHostRequest. |
| 82 int64_t start_time = static_cast<int64_t>(base::Time::Now().ToDoubleT()); | 101 int64_t start_time = static_cast<int64_t>(base::Time::Now().ToDoubleT()); |
| 83 | 102 |
| 84 std::unique_ptr<RegisterSupportHostRequest> request( | 103 std::unique_ptr<RegisterSupportHostRequest> request( |
| 85 new RegisterSupportHostRequest(&signal_strategy_, key_pair_, kTestBotJid, | 104 new RegisterSupportHostRequest(&signal_strategy_, key_pair_, kTestBotJid, |
| 86 callback_.Get())); | 105 callback_.Get())); |
| 87 | 106 |
| 88 XmlElement* sent_iq = nullptr; | 107 XmlElement* sent_iq = nullptr; |
| 89 EXPECT_CALL(signal_strategy_, GetNextId()) | 108 EXPECT_CALL(signal_strategy_, GetNextId()) |
| 90 .WillOnce(Return(kStanzaId)); | 109 .WillOnce(Return(kStanzaId)); |
| 91 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) | 110 EXPECT_CALL(signal_strategy_, SendStanzaPtr(NotNull())) |
| 92 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); | 111 .WillOnce(DoAll(SaveArg<0>(&sent_iq), Return(true))); |
| 93 | 112 |
| 94 request->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); | 113 request->OnSignalStrategyStateChange(SignalStrategy::CONNECTED); |
| 95 base::RunLoop().RunUntilIdle(); | 114 mock_time_task_runner_->RunUntilIdle(); |
| 96 | 115 |
| 97 // Verify format of the query. | 116 // Verify format of the query. |
| 98 std::unique_ptr<XmlElement> stanza(sent_iq); | 117 std::unique_ptr<XmlElement> stanza(sent_iq); |
| 99 ASSERT_TRUE(stanza != nullptr); | 118 ASSERT_TRUE(stanza != nullptr); |
| 100 | 119 |
| 101 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "to")), | 120 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "to")), |
| 102 std::string(kTestBotJid)); | 121 std::string(kTestBotJid)); |
| 103 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "type")), "set"); | 122 EXPECT_EQ(stanza->Attr(buzz::QName(std::string(), "type")), "set"); |
| 104 | 123 |
| 105 EXPECT_EQ(QName(kChromotingXmlNamespace, "register-support-host"), | 124 EXPECT_EQ(QName(kChromotingXmlNamespace, "register-support-host"), |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 support_id_lifetime->AddText(kSupportIdLifetime); | 179 support_id_lifetime->AddText(kSupportIdLifetime); |
| 161 result->AddElement(support_id_lifetime); | 180 result->AddElement(support_id_lifetime); |
| 162 | 181 |
| 163 int consumed = 0; | 182 int consumed = 0; |
| 164 for (auto& listener : signal_strategy_listeners_) { | 183 for (auto& listener : signal_strategy_listeners_) { |
| 165 if (listener.OnSignalStrategyIncomingStanza(response.get())) | 184 if (listener.OnSignalStrategyIncomingStanza(response.get())) |
| 166 consumed++; | 185 consumed++; |
| 167 } | 186 } |
| 168 EXPECT_EQ(1, consumed); | 187 EXPECT_EQ(1, consumed); |
| 169 | 188 |
| 170 base::RunLoop().RunUntilIdle(); | 189 mock_time_task_runner_->RunUntilIdle(); |
| 171 } | 190 } |
| 172 | 191 |
| 173 } // namespace remoting | 192 } // namespace remoting |
| OLD | NEW |