1 /*
2 * Copyright 2011 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include <string>
12 #include <vector>
13
14 #include "webrtc/libjingle/xmllite/xmlelement.h"
15 #include "webrtc/libjingle/xmpp/constants.h"
16 #include "webrtc/libjingle/xmpp/fakexmppclient.h"
17 #include "webrtc/libjingle/xmpp/pingtask.h"
18 #include "webrtc/base/faketaskrunner.h"
19 #include "webrtc/base/gunit.h"
20 #include "webrtc/base/sigslot.h"
21
22 class PingTaskTest;
23
24 class PingXmppClient : public buzz::FakeXmppClient {
25 public:
PingXmppClient(rtc::TaskParent * parent,PingTaskTest * tst)26 PingXmppClient(rtc::TaskParent* parent, PingTaskTest* tst) :
27 FakeXmppClient(parent), test(tst) {
28 }
29
30 buzz::XmppReturnStatus SendStanza(const buzz::XmlElement* stanza);
31
32 private:
33 PingTaskTest* test;
34 };
35
36 class PingTaskTest : public testing::Test, public sigslot::has_slots<> {
37 public:
PingTaskTest()38 PingTaskTest() : respond_to_pings(true), timed_out(false) {
39 }
40
SetUp()41 virtual void SetUp() {
42 runner = new rtc::FakeTaskRunner();
43 xmpp_client = new PingXmppClient(runner, this);
44 }
45
TearDown()46 virtual void TearDown() {
47 // delete xmpp_client; Deleted by deleting runner.
48 delete runner;
49 }
50
ConnectTimeoutSignal(buzz::PingTask * task)51 void ConnectTimeoutSignal(buzz::PingTask* task) {
52 task->SignalTimeout.connect(this, &PingTaskTest::OnPingTimeout);
53 }
54
OnPingTimeout()55 void OnPingTimeout() {
56 timed_out = true;
57 }
58
59 rtc::FakeTaskRunner* runner;
60 PingXmppClient* xmpp_client;
61 bool respond_to_pings;
62 bool timed_out;
63 };
64
SendStanza(const buzz::XmlElement * stanza)65 buzz::XmppReturnStatus PingXmppClient::SendStanza(
66 const buzz::XmlElement* stanza) {
67 buzz::XmppReturnStatus result = FakeXmppClient::SendStanza(stanza);
68 if (test->respond_to_pings && (stanza->FirstNamed(buzz::QN_PING) != NULL)) {
69 std::string ping_response =
70 "<iq xmlns=\'jabber:client\' id='0' type='result'/>";
71 HandleStanza(buzz::XmlElement::ForStr(ping_response));
72 }
73 return result;
74 }
75
TEST_F(PingTaskTest,TestSuccess)76 TEST_F(PingTaskTest, TestSuccess) {
77 uint32_t ping_period_millis = 100;
78 buzz::PingTask* task = new buzz::PingTask(xmpp_client,
79 rtc::Thread::Current(),
80 ping_period_millis, ping_period_millis / 10);
81 ConnectTimeoutSignal(task);
82 task->Start();
83 unsigned int expected_ping_count = 5U;
84 EXPECT_EQ_WAIT(xmpp_client->sent_stanzas().size(), expected_ping_count,
85 ping_period_millis * (expected_ping_count + 1));
86 EXPECT_FALSE(task->IsDone());
87 EXPECT_FALSE(timed_out);
88 }
89
TEST_F(PingTaskTest,TestTimeout)90 TEST_F(PingTaskTest, TestTimeout) {
91 respond_to_pings = false;
92 uint32_t ping_timeout_millis = 200;
93 buzz::PingTask* task = new buzz::PingTask(xmpp_client,
94 rtc::Thread::Current(),
95 ping_timeout_millis * 10, ping_timeout_millis);
96 ConnectTimeoutSignal(task);
97 task->Start();
98 WAIT(false, ping_timeout_millis / 2);
99 EXPECT_FALSE(timed_out);
100 EXPECT_TRUE_WAIT(timed_out, ping_timeout_millis * 2);
101 }
102