iq_sender_unittest.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright 2014 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. #include "remoting/signaling/iq_sender.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/run_loop.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "base/test/mock_callback.h"
  12. #include "base/test/task_environment.h"
  13. #include "remoting/signaling/mock_signal_strategy.h"
  14. #include "remoting/signaling/xmpp_constants.h"
  15. #include "testing/gmock/include/gmock/gmock.h"
  16. #include "testing/gtest/include/gtest/gtest.h"
  17. #include "third_party/libjingle_xmpp/xmllite/xmlelement.h"
  18. using ::testing::_;
  19. using ::testing::DeleteArg;
  20. using ::testing::DoAll;
  21. using ::testing::InvokeWithoutArgs;
  22. using ::testing::NotNull;
  23. using ::testing::Return;
  24. using ::testing::SaveArg;
  25. using ::jingle_xmpp::QName;
  26. using ::jingle_xmpp::XmlElement;
  27. namespace remoting {
  28. namespace {
  29. const char kStanzaId[] = "123";
  30. const char kNamespace[] = "chromium:testns";
  31. const char kNamespacePrefix[] = "tes";
  32. const char kBodyTag[] = "test";
  33. const char kType[] = "get";
  34. const char kTo[] = "user@domain.com";
  35. MATCHER_P(XmlEq, expected, "") {
  36. return arg->Str() == expected->Str();
  37. }
  38. } // namespace
  39. class IqSenderTest : public testing::Test {
  40. public:
  41. IqSenderTest() : signal_strategy_(SignalingAddress("local_jid@domain.com")) {
  42. EXPECT_CALL(signal_strategy_, AddListener(NotNull()));
  43. sender_ = std::make_unique<IqSender>(&signal_strategy_);
  44. EXPECT_CALL(signal_strategy_, RemoveListener(
  45. static_cast<SignalStrategy::Listener*>(sender_.get())));
  46. }
  47. protected:
  48. void SendTestMessage() {
  49. std::unique_ptr<XmlElement> iq_body(
  50. new XmlElement(QName(kNamespace, kBodyTag)));
  51. XmlElement* sent_stanza;
  52. EXPECT_CALL(signal_strategy_, GetNextId())
  53. .WillOnce(Return(kStanzaId));
  54. EXPECT_CALL(signal_strategy_, SendStanzaPtr(_))
  55. .WillOnce(DoAll(SaveArg<0>(&sent_stanza), Return(true)));
  56. request_ = sender_->SendIq(kType, kTo, std::move(iq_body), callback_.Get());
  57. std::string expected_xml_string =
  58. base::StringPrintf(
  59. "<cli:iq type=\"%s\" to=\"%s\" id=\"%s\" "
  60. "xmlns:cli=\"jabber:client\">"
  61. "<%s:%s xmlns:%s=\"%s\"/>"
  62. "</cli:iq>",
  63. kType, kTo, kStanzaId, kNamespacePrefix, kBodyTag,
  64. kNamespacePrefix, kNamespace);
  65. EXPECT_EQ(expected_xml_string, sent_stanza->Str());
  66. delete sent_stanza;
  67. }
  68. bool FormatAndDeliverResponse(const std::string& from,
  69. std::unique_ptr<XmlElement>* response_out) {
  70. std::unique_ptr<XmlElement> response(new XmlElement(kQNameIq));
  71. response->AddAttr(QName(std::string(), "type"), "result");
  72. response->AddAttr(QName(std::string(), "id"), kStanzaId);
  73. response->AddAttr(QName(std::string(), "from"), from);
  74. XmlElement* response_body = new XmlElement(
  75. QName("test:namespace", "response-body"));
  76. response->AddElement(response_body);
  77. bool result = sender_->OnSignalStrategyIncomingStanza(response.get());
  78. if (response_out)
  79. *response_out = std::move(response);
  80. return result;
  81. }
  82. base::test::SingleThreadTaskEnvironment task_environment_;
  83. MockSignalStrategy signal_strategy_;
  84. std::unique_ptr<IqSender> sender_;
  85. base::MockCallback<IqSender::ReplyCallback> callback_;
  86. std::unique_ptr<IqRequest> request_;
  87. };
  88. TEST_F(IqSenderTest, SendIq) {
  89. ASSERT_NO_FATAL_FAILURE({
  90. SendTestMessage();
  91. });
  92. std::unique_ptr<XmlElement> response;
  93. EXPECT_TRUE(FormatAndDeliverResponse(kTo, &response));
  94. EXPECT_CALL(callback_, Run(request_.get(), XmlEq(response.get())));
  95. base::RunLoop().RunUntilIdle();
  96. }
  97. TEST_F(IqSenderTest, Timeout) {
  98. ASSERT_NO_FATAL_FAILURE({
  99. SendTestMessage();
  100. });
  101. request_->SetTimeout(base::Milliseconds(2));
  102. base::RunLoop run_loop;
  103. EXPECT_CALL(callback_, Run(request_.get(), nullptr))
  104. .WillOnce(InvokeWithoutArgs(&run_loop, &base::RunLoop::QuitWhenIdle));
  105. run_loop.Run();
  106. }
  107. TEST_F(IqSenderTest, NotNormalizedJid) {
  108. ASSERT_NO_FATAL_FAILURE({
  109. SendTestMessage();
  110. });
  111. // Set upper-case from value, which is equivalent to kTo in the original
  112. // message.
  113. std::unique_ptr<XmlElement> response;
  114. EXPECT_TRUE(FormatAndDeliverResponse("USER@domain.com", &response));
  115. EXPECT_CALL(callback_, Run(request_.get(), XmlEq(response.get())));
  116. base::RunLoop().RunUntilIdle();
  117. }
  118. TEST_F(IqSenderTest, InvalidFrom) {
  119. ASSERT_NO_FATAL_FAILURE({
  120. SendTestMessage();
  121. });
  122. EXPECT_FALSE(FormatAndDeliverResponse("different_user@domain.com", nullptr));
  123. EXPECT_CALL(callback_, Run(_, _)).Times(0);
  124. base::RunLoop().RunUntilIdle();
  125. }
  126. } // namespace remoting