henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
| 3 | * Copyright 2011, Google Inc. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | * this list of conditions and the following disclaimer in the documentation |
| 12 | * and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 17 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
| 19 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 20 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <string> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include "talk/base/faketaskrunner.h" |
| 32 | #include "talk/base/gunit.h" |
| 33 | #include "talk/base/sigslot.h" |
| 34 | #include "talk/xmllite/xmlelement.h" |
| 35 | #include "talk/xmpp/constants.h" |
| 36 | #include "talk/xmpp/fakexmppclient.h" |
| 37 | #include "talk/xmpp/pingtask.h" |
| 38 | |
| 39 | class PingTaskTest; |
| 40 | |
| 41 | class PingXmppClient : public buzz::FakeXmppClient { |
| 42 | public: |
| 43 | PingXmppClient(talk_base::TaskParent* parent, PingTaskTest* tst) : |
| 44 | FakeXmppClient(parent), test(tst) { |
| 45 | } |
| 46 | |
| 47 | buzz::XmppReturnStatus SendStanza(const buzz::XmlElement* stanza); |
| 48 | |
| 49 | private: |
| 50 | PingTaskTest* test; |
| 51 | }; |
| 52 | |
| 53 | class PingTaskTest : public testing::Test, public sigslot::has_slots<> { |
| 54 | public: |
| 55 | PingTaskTest() : respond_to_pings(true), timed_out(false) { |
| 56 | } |
| 57 | |
| 58 | virtual void SetUp() { |
| 59 | runner = new talk_base::FakeTaskRunner(); |
| 60 | xmpp_client = new PingXmppClient(runner, this); |
| 61 | } |
| 62 | |
| 63 | virtual void TearDown() { |
| 64 | // delete xmpp_client; Deleted by deleting runner. |
| 65 | delete runner; |
| 66 | } |
| 67 | |
| 68 | void ConnectTimeoutSignal(buzz::PingTask* task) { |
| 69 | task->SignalTimeout.connect(this, &PingTaskTest::OnPingTimeout); |
| 70 | } |
| 71 | |
| 72 | void OnPingTimeout() { |
| 73 | timed_out = true; |
| 74 | } |
| 75 | |
| 76 | talk_base::FakeTaskRunner* runner; |
| 77 | PingXmppClient* xmpp_client; |
| 78 | bool respond_to_pings; |
| 79 | bool timed_out; |
| 80 | }; |
| 81 | |
| 82 | buzz::XmppReturnStatus PingXmppClient::SendStanza( |
| 83 | const buzz::XmlElement* stanza) { |
| 84 | buzz::XmppReturnStatus result = FakeXmppClient::SendStanza(stanza); |
| 85 | if (test->respond_to_pings && (stanza->FirstNamed(buzz::QN_PING) != NULL)) { |
| 86 | std::string ping_response = |
| 87 | "<iq xmlns=\'jabber:client\' id='0' type='result'/>"; |
| 88 | HandleStanza(buzz::XmlElement::ForStr(ping_response)); |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | TEST_F(PingTaskTest, TestSuccess) { |
| 94 | uint32 ping_period_millis = 100; |
| 95 | buzz::PingTask* task = new buzz::PingTask(xmpp_client, |
| 96 | talk_base::Thread::Current(), |
| 97 | ping_period_millis, ping_period_millis / 10); |
| 98 | ConnectTimeoutSignal(task); |
| 99 | task->Start(); |
| 100 | unsigned int expected_ping_count = 5U; |
| 101 | EXPECT_EQ_WAIT(xmpp_client->sent_stanzas().size(), expected_ping_count, |
| 102 | ping_period_millis * (expected_ping_count + 1)); |
| 103 | EXPECT_FALSE(task->IsDone()); |
| 104 | EXPECT_FALSE(timed_out); |
| 105 | } |
| 106 | |
| 107 | TEST_F(PingTaskTest, TestTimeout) { |
| 108 | respond_to_pings = false; |
| 109 | uint32 ping_timeout_millis = 200; |
| 110 | buzz::PingTask* task = new buzz::PingTask(xmpp_client, |
| 111 | talk_base::Thread::Current(), |
| 112 | ping_timeout_millis * 10, ping_timeout_millis); |
| 113 | ConnectTimeoutSignal(task); |
| 114 | task->Start(); |
| 115 | WAIT(false, ping_timeout_millis / 2); |
| 116 | EXPECT_FALSE(timed_out); |
| 117 | EXPECT_TRUE_WAIT(timed_out, ping_timeout_millis * 2); |
| 118 | } |