summaryrefslogtreecommitdiff
path: root/xmpp/rostermodule_unittest.cc
blob: 0e2b26821123127fc296b892aa8168a6ad183d00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
 * libjingle
 * Copyright 2004, Google Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  1. Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include <sstream>
#include <string>

#include "talk/xmllite/xmlelement.h"
#include "talk/xmpp/constants.h"
#include "talk/xmpp/rostermodule.h"
#include "talk/xmpp/util_unittest.h"
#include "talk/xmpp/xmppengine.h"
#include "webrtc/base/gunit.h"
#include "webrtc/base/scoped_ptr.h"

#define TEST_OK(x) EXPECT_EQ((x),XMPP_RETURN_OK)
#define TEST_BADARGUMENT(x) EXPECT_EQ((x),XMPP_RETURN_BADARGUMENT)


namespace buzz {

class RosterModuleTest;

static void
WriteString(std::ostream& os, const std::string& str) {
  os<<str;
}

static void
WriteSubscriptionState(std::ostream& os, XmppSubscriptionState state)
{
  switch (state) {
    case XMPP_SUBSCRIPTION_NONE:
      os<<"none";
      break;
    case XMPP_SUBSCRIPTION_NONE_ASKED:
      os<<"none_asked";
      break;
    case XMPP_SUBSCRIPTION_TO:
      os<<"to";
      break;
    case XMPP_SUBSCRIPTION_FROM:
      os<<"from";
      break;
    case XMPP_SUBSCRIPTION_FROM_ASKED:
      os<<"from_asked";
      break;
    case XMPP_SUBSCRIPTION_BOTH:
      os<<"both";
      break;
    default:
      os<<"unknown";
      break;
  }
}

static void
WriteSubscriptionRequestType(std::ostream& os,
                             XmppSubscriptionRequestType type) {
  switch(type) {
    case XMPP_REQUEST_SUBSCRIBE:
      os<<"subscribe";
      break;
    case XMPP_REQUEST_UNSUBSCRIBE:
      os<<"unsubscribe";
      break;
    case XMPP_REQUEST_SUBSCRIBED:
      os<<"subscribed";
      break;
    case XMPP_REQUEST_UNSUBSCRIBED:
      os<<"unsubscribe";
      break;
    default:
      os<<"unknown";
      break;
  }
}

static void
WritePresenceShow(std::ostream& os, XmppPresenceShow show) {
  switch(show) {
    case XMPP_PRESENCE_AWAY:
      os<<"away";
      break;
    case XMPP_PRESENCE_CHAT:
      os<<"chat";
      break;
    case XMPP_PRESENCE_DND:
      os<<"dnd";
      break;
    case XMPP_PRESENCE_XA:
      os<<"xa";
      break;
    case XMPP_PRESENCE_DEFAULT:
      os<<"[default]";
      break;
    default:
      os<<"[unknown]";
      break;
  }
}

static void
WritePresence(std::ostream& os, const XmppPresence* presence) {
  if (presence == NULL) {
    os<<"NULL";
    return;
  }

  os<<"[Presence jid:";
  WriteString(os, presence->jid().Str());
  os<<" available:"<<presence->available();
  os<<" presence_show:";
  WritePresenceShow(os, presence->presence_show());
  os<<" priority:"<<presence->priority();
  os<<" status:";
  WriteString(os, presence->status());
  os<<"]"<<presence->raw_xml()->Str();
}

static void
WriteContact(std::ostream& os, const XmppRosterContact* contact) {
  if (contact == NULL) {
    os<<"NULL";
    return;
  }

  os<<"[Contact jid:";
  WriteString(os, contact->jid().Str());
  os<<" name:";
  WriteString(os, contact->name());
  os<<" subscription_state:";
  WriteSubscriptionState(os, contact->subscription_state());
  os<<" groups:[";
  for(size_t i=0; i < contact->GetGroupCount(); ++i) {
    os<<(i==0?"":", ");
    WriteString(os, contact->GetGroup(i));
  }
  os<<"]]"<<contact->raw_xml()->Str();
}

//! This session handler saves all calls to a string.  These are events and
//! data delivered form the engine to application code.
class XmppTestRosterHandler : public XmppRosterHandler {
public:
  XmppTestRosterHandler() {}
  virtual ~XmppTestRosterHandler() {}

  virtual void SubscriptionRequest(XmppRosterModule*,
                                   const Jid& requesting_jid,
                                   XmppSubscriptionRequestType type,
                                   const XmlElement* raw_xml) {
    ss_<<"[SubscriptionRequest Jid:" << requesting_jid.Str()<<" type:";
    WriteSubscriptionRequestType(ss_, type);
    ss_<<"]"<<raw_xml->Str();
  }

  //! Some type of presence error has occured
  virtual void SubscriptionError(XmppRosterModule*,
                                 const Jid& from,
                                 const XmlElement* raw_xml) {
    ss_<<"[SubscriptionError from:"<<from.Str()<<"]"<<raw_xml->Str();
  }

  virtual void RosterError(XmppRosterModule*,
                           const XmlElement* raw_xml) {
    ss_<<"[RosterError]"<<raw_xml->Str();
  }

  //! New presence information has come in
  //! The user is notified with the presence object directly.  This info is also
  //! added to the store accessable from the engine.
  virtual void IncomingPresenceChanged(XmppRosterModule*,
                                       const XmppPresence* presence) {
    ss_<<"[IncomingPresenceChanged presence:";
    WritePresence(ss_, presence);
    ss_<<"]";
  }

  //! A contact has changed
  //! This indicates that the data for a contact may have changed.  No
  //! contacts have been added or removed.
  virtual void ContactChanged(XmppRosterModule* roster,
                              const XmppRosterContact* old_contact,
                              size_t index) {
    ss_<<"[ContactChanged old_contact:";
    WriteContact(ss_, old_contact);
    ss_<<" index:"<<index<<" new_contact:";
    WriteContact(ss_, roster->GetRosterContact(index));
    ss_<<"]";
  }

  //! A set of contacts have been added
  //! These contacts may have been added in response to the original roster
  //! request or due to a "roster push" from the server.
  virtual void ContactsAdded(XmppRosterModule* roster,
                             size_t index, size_t number) {
    ss_<<"[ContactsAdded index:"<<index<<" number:"<<number;
    for (size_t i = 0; i < number; ++i) {
      ss_<<" "<<(index+i)<<":";
      WriteContact(ss_, roster->GetRosterContact(index+i));
    }
    ss_<<"]";
  }

  //! A contact has been removed
  //! This contact has been removed form the list.
  virtual void ContactRemoved(XmppRosterModule*,
                              const XmppRosterContact* removed_contact,
                              size_t index) {
    ss_<<"[ContactRemoved old_contact:";
    WriteContact(ss_, removed_contact);
    ss_<<" index:"<<index<<"]";
  }

  std::string Str() {
    return ss_.str();
  }

  std::string StrClear() {
    std::string result = ss_.str();
    ss_.str("");
    return result;
  }

private:
  std::stringstream ss_;
};

//! This is the class that holds all of the unit test code for the
//! roster module
class RosterModuleTest : public testing::Test {
public:
  RosterModuleTest() {}
  static void RunLogin(RosterModuleTest* obj, XmppEngine* engine,
                       XmppTestHandler* handler) {
    // Needs to be similar to XmppEngineTest::RunLogin
  }
};

TEST_F(RosterModuleTest, TestPresence) {
  XmlElement* status = new XmlElement(QN_GOOGLE_PSTN_CONFERENCE_STATUS);
  status->AddAttr(QN_STATUS, STR_PSTN_CONFERENCE_STATUS_CONNECTING);
  XmlElement presence_xml(QN_PRESENCE);
  presence_xml.AddElement(status);
  rtc::scoped_ptr<XmppPresence> presence(XmppPresence::Create());
  presence->set_raw_xml(&presence_xml);
  EXPECT_EQ(presence->connection_status(), XMPP_CONNECTION_STATUS_CONNECTING);
}

TEST_F(RosterModuleTest, TestOutgoingPresence) {
  std::stringstream dump;

  rtc::scoped_ptr<XmppEngine> engine(XmppEngine::Create());
  XmppTestHandler handler(engine.get());
  XmppTestRosterHandler roster_handler;

  rtc::scoped_ptr<XmppRosterModule> roster(XmppRosterModule::Create());
  roster->set_roster_handler(&roster_handler);

  // Configure the roster module
  roster->RegisterEngine(engine.get());

  // Set up callbacks
  engine->SetOutputHandler(&handler);
  engine->AddStanzaHandler(&handler);
  engine->SetSessionHandler(&handler);

  // Set up minimal login info
  engine->SetUser(Jid("david@my-server"));
  // engine->SetPassword("david");

  // Do the whole login handshake
  RunLogin(this, engine.get(), &handler);
  EXPECT_EQ("", handler.OutputActivity());

  // Set some presence and broadcast it
  TEST_OK(roster->outgoing_presence()->
    set_available(XMPP_PRESENCE_AVAILABLE));
  TEST_OK(roster->outgoing_presence()->set_priority(-37));
  TEST_OK(roster->outgoing_presence()->set_presence_show(XMPP_PRESENCE_DND));
  TEST_OK(roster->outgoing_presence()->
    set_status("I'm off to the races!<>&"));
  TEST_OK(roster->BroadcastPresence());

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<presence>"
      "<priority>-37</priority>"
      "<show>dnd</show>"
      "<status>I'm off to the races!&lt;&gt;&amp;</status>"
    "</presence>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Try some more
  TEST_OK(roster->outgoing_presence()->
    set_available(XMPP_PRESENCE_UNAVAILABLE));
  TEST_OK(roster->outgoing_presence()->set_priority(0));
  TEST_OK(roster->outgoing_presence()->set_presence_show(XMPP_PRESENCE_XA));
  TEST_OK(roster->outgoing_presence()->set_status("Gone fishin'"));
  TEST_OK(roster->BroadcastPresence());

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<presence type=\"unavailable\">"
      "<show>xa</show>"
      "<status>Gone fishin'</status>"
    "</presence>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Okay -- we are back on
  TEST_OK(roster->outgoing_presence()->
    set_available(XMPP_PRESENCE_AVAILABLE));
  TEST_BADARGUMENT(roster->outgoing_presence()->set_priority(128));
  TEST_OK(roster->outgoing_presence()->
      set_presence_show(XMPP_PRESENCE_DEFAULT));
  TEST_OK(roster->outgoing_presence()->set_status("Cookin' wit gas"));
  TEST_OK(roster->BroadcastPresence());

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<presence>"
      "<status>Cookin' wit gas</status>"
    "</presence>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Set it via XML
  XmlElement presence_input(QN_PRESENCE);
  presence_input.AddAttr(QN_TYPE, "unavailable");
  presence_input.AddElement(new XmlElement(QN_PRIORITY));
  presence_input.AddText("42", 1);
  presence_input.AddElement(new XmlElement(QN_STATUS));
  presence_input.AddAttr(QN_XML_LANG, "es", 1);
  presence_input.AddText("Hola Amigos!", 1);
  presence_input.AddElement(new XmlElement(QN_STATUS));
  presence_input.AddText("Hey there, friend!", 1);
  TEST_OK(roster->outgoing_presence()->set_raw_xml(&presence_input));
  TEST_OK(roster->BroadcastPresence());

  WritePresence(dump, roster->outgoing_presence());
  EXPECT_EQ(dump.str(),
    "[Presence jid: available:0 presence_show:[default] "
              "priority:42 status:Hey there, friend!]"
    "<cli:presence type=\"unavailable\" xmlns:cli=\"jabber:client\">"
      "<cli:priority>42</cli:priority>"
      "<cli:status xml:lang=\"es\">Hola Amigos!</cli:status>"
      "<cli:status>Hey there, friend!</cli:status>"
    "</cli:presence>");
  dump.str("");
  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<presence type=\"unavailable\">"
      "<priority>42</priority>"
      "<status xml:lang=\"es\">Hola Amigos!</status>"
      "<status>Hey there, friend!</status>"
    "</presence>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Construct a directed presence
  rtc::scoped_ptr<XmppPresence> directed_presence(XmppPresence::Create());
  TEST_OK(directed_presence->set_available(XMPP_PRESENCE_AVAILABLE));
  TEST_OK(directed_presence->set_priority(120));
  TEST_OK(directed_presence->set_status("*very* available"));
  TEST_OK(roster->SendDirectedPresence(directed_presence.get(),
                                       Jid("myhoney@honey.net")));

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<presence to=\"myhoney@honey.net\">"
      "<priority>120</priority>"
      "<status>*very* available</status>"
    "</presence>");
  EXPECT_EQ(handler.SessionActivity(), "");
}

TEST_F(RosterModuleTest, TestIncomingPresence) {
  rtc::scoped_ptr<XmppEngine> engine(XmppEngine::Create());
  XmppTestHandler handler(engine.get());
  XmppTestRosterHandler roster_handler;

  rtc::scoped_ptr<XmppRosterModule> roster(XmppRosterModule::Create());
  roster->set_roster_handler(&roster_handler);

  // Configure the roster module
  roster->RegisterEngine(engine.get());

  // Set up callbacks
  engine->SetOutputHandler(&handler);
  engine->AddStanzaHandler(&handler);
  engine->SetSessionHandler(&handler);

  // Set up minimal login info
  engine->SetUser(Jid("david@my-server"));
  // engine->SetPassword("david");

  // Do the whole login handshake
  RunLogin(this, engine.get(), &handler);
  EXPECT_EQ("", handler.OutputActivity());

  // Load up with a bunch of data
  std::string input;
  input = "<presence from='maude@example.net/studio' "
                    "to='david@my-server/test'/>"
          "<presence from='walter@example.net/home' "
                    "to='david@my-server/test'>"
            "<priority>-10</priority>"
            "<show>xa</show>"
            "<status>Off bowling</status>"
          "</presence>"
          "<presence from='walter@example.net/alley' "
                    "to='david@my-server/test'>"
            "<priority>20</priority>"
            "<status>Looking for toes...</status>"
          "</presence>"
          "<presence from='donny@example.net/alley' "
                    "to='david@my-server/test'>"
            "<priority>10</priority>"
            "<status>Throwing rocks</status>"
          "</presence>";
  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[IncomingPresenceChanged "
      "presence:[Presence jid:maude@example.net/studio available:1 "
                 "presence_show:[default] priority:0 status:]"
        "<cli:presence from=\"maude@example.net/studio\" "
                      "to=\"david@my-server/test\" "
                      "xmlns:cli=\"jabber:client\"/>]"
    "[IncomingPresenceChanged "
      "presence:[Presence jid:walter@example.net/home available:1 "
                 "presence_show:xa priority:-10 status:Off bowling]"
        "<cli:presence from=\"walter@example.net/home\" "
                      "to=\"david@my-server/test\" "
                      "xmlns:cli=\"jabber:client\">"
          "<cli:priority>-10</cli:priority>"
          "<cli:show>xa</cli:show>"
          "<cli:status>Off bowling</cli:status>"
        "</cli:presence>]"
    "[IncomingPresenceChanged "
      "presence:[Presence jid:walter@example.net/alley available:1 "
                 "presence_show:[default] "
                 "priority:20 status:Looking for toes...]"
        "<cli:presence from=\"walter@example.net/alley\" "
                       "to=\"david@my-server/test\" "
                       "xmlns:cli=\"jabber:client\">"
          "<cli:priority>20</cli:priority>"
          "<cli:status>Looking for toes...</cli:status>"
        "</cli:presence>]"
    "[IncomingPresenceChanged "
      "presence:[Presence jid:donny@example.net/alley available:1 "
                 "presence_show:[default] priority:10 status:Throwing rocks]"
        "<cli:presence from=\"donny@example.net/alley\" "
                      "to=\"david@my-server/test\" "
                      "xmlns:cli=\"jabber:client\">"
          "<cli:priority>10</cli:priority>"
          "<cli:status>Throwing rocks</cli:status>"
        "</cli:presence>]");
  EXPECT_EQ(handler.OutputActivity(), "");
  handler.SessionActivity(); // Ignore the session output

  // Now look at the data structure we've built
  EXPECT_EQ(roster->GetIncomingPresenceCount(), static_cast<size_t>(4));
  EXPECT_EQ(roster->GetIncomingPresenceForJidCount(Jid("maude@example.net")),
            static_cast<size_t>(1));
  EXPECT_EQ(roster->GetIncomingPresenceForJidCount(Jid("walter@example.net")),
            static_cast<size_t>(2));

  const XmppPresence * presence;
  presence = roster->GetIncomingPresenceForJid(Jid("walter@example.net"), 1);

  std::stringstream dump;
  WritePresence(dump, presence);
  EXPECT_EQ(dump.str(),
          "[Presence jid:walter@example.net/alley available:1 "
            "presence_show:[default] priority:20 status:Looking for toes...]"
              "<cli:presence from=\"walter@example.net/alley\" "
                            "to=\"david@my-server/test\" "
                            "xmlns:cli=\"jabber:client\">"
                "<cli:priority>20</cli:priority>"
                "<cli:status>Looking for toes...</cli:status>"
              "</cli:presence>");
  dump.str("");

  // Maude took off...
  input = "<presence from='maude@example.net/studio' "
                    "to='david@my-server/test' "
                    "type='unavailable'>"
            "<status>Stealing my rug back</status>"
            "<priority>-10</priority>"
          "</presence>";
  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[IncomingPresenceChanged "
      "presence:[Presence jid:maude@example.net/studio available:0 "
                 "presence_show:[default] priority:-10 "
                 "status:Stealing my rug back]"
        "<cli:presence from=\"maude@example.net/studio\" "
                      "to=\"david@my-server/test\" type=\"unavailable\" "
                      "xmlns:cli=\"jabber:client\">"
          "<cli:status>Stealing my rug back</cli:status>"
          "<cli:priority>-10</cli:priority>"
        "</cli:presence>]");
  EXPECT_EQ(handler.OutputActivity(), "");
  handler.SessionActivity(); // Ignore the session output
}

TEST_F(RosterModuleTest, TestPresenceSubscription) {
  rtc::scoped_ptr<XmppEngine> engine(XmppEngine::Create());
  XmppTestHandler handler(engine.get());
  XmppTestRosterHandler roster_handler;

  rtc::scoped_ptr<XmppRosterModule> roster(XmppRosterModule::Create());
  roster->set_roster_handler(&roster_handler);

  // Configure the roster module
  roster->RegisterEngine(engine.get());

  // Set up callbacks
  engine->SetOutputHandler(&handler);
  engine->AddStanzaHandler(&handler);
  engine->SetSessionHandler(&handler);

  // Set up minimal login info
  engine->SetUser(Jid("david@my-server"));
  // engine->SetPassword("david");

  // Do the whole login handshake
  RunLogin(this, engine.get(), &handler);
  EXPECT_EQ("", handler.OutputActivity());

  // Test incoming requests
  std::string input;
  input =
    "<presence from='maude@example.net' type='subscribe'/>"
    "<presence from='maude@example.net' type='unsubscribe'/>"
    "<presence from='maude@example.net' type='subscribed'/>"
    "<presence from='maude@example.net' type='unsubscribed'/>";
  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[SubscriptionRequest Jid:maude@example.net type:subscribe]"
      "<cli:presence from=\"maude@example.net\" type=\"subscribe\" "
                    "xmlns:cli=\"jabber:client\"/>"
    "[SubscriptionRequest Jid:maude@example.net type:unsubscribe]"
      "<cli:presence from=\"maude@example.net\" type=\"unsubscribe\" "
                    "xmlns:cli=\"jabber:client\"/>"
    "[SubscriptionRequest Jid:maude@example.net type:subscribed]"
      "<cli:presence from=\"maude@example.net\" type=\"subscribed\" "
                    "xmlns:cli=\"jabber:client\"/>"
    "[SubscriptionRequest Jid:maude@example.net type:unsubscribe]"
      "<cli:presence from=\"maude@example.net\" type=\"unsubscribed\" "
                    "xmlns:cli=\"jabber:client\"/>");
  EXPECT_EQ(handler.OutputActivity(), "");
  handler.SessionActivity(); // Ignore the session output

  TEST_OK(roster->RequestSubscription(Jid("maude@example.net")));
  TEST_OK(roster->CancelSubscription(Jid("maude@example.net")));
  TEST_OK(roster->ApproveSubscriber(Jid("maude@example.net")));
  TEST_OK(roster->CancelSubscriber(Jid("maude@example.net")));

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<presence to=\"maude@example.net\" type=\"subscribe\"/>"
    "<presence to=\"maude@example.net\" type=\"unsubscribe\"/>"
    "<presence to=\"maude@example.net\" type=\"subscribed\"/>"
    "<presence to=\"maude@example.net\" type=\"unsubscribed\"/>");
  EXPECT_EQ(handler.SessionActivity(), "");
}

TEST_F(RosterModuleTest, TestRosterReceive) {
  rtc::scoped_ptr<XmppEngine> engine(XmppEngine::Create());
  XmppTestHandler handler(engine.get());
  XmppTestRosterHandler roster_handler;

  rtc::scoped_ptr<XmppRosterModule> roster(XmppRosterModule::Create());
  roster->set_roster_handler(&roster_handler);

  // Configure the roster module
  roster->RegisterEngine(engine.get());

  // Set up callbacks
  engine->SetOutputHandler(&handler);
  engine->AddStanzaHandler(&handler);
  engine->SetSessionHandler(&handler);

  // Set up minimal login info
  engine->SetUser(Jid("david@my-server"));
  // engine->SetPassword("david");

  // Do the whole login handshake
  RunLogin(this, engine.get(), &handler);
  EXPECT_EQ("", handler.OutputActivity());

  // Request a roster update
  TEST_OK(roster->RequestRosterUpdate());

  EXPECT_EQ(roster_handler.StrClear(),"");
  EXPECT_EQ(handler.OutputActivity(),
    "<iq type=\"get\" id=\"2\">"
      "<query xmlns=\"jabber:iq:roster\"/>"
    "</iq>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Prime the roster with a starting set
  std::string input =
    "<iq to='david@myserver/test' type='result' id='2'>"
      "<query xmlns='jabber:iq:roster'>"
        "<item jid='maude@example.net' "
              "name='Maude Lebowski' "
              "subscription='none' "
              "ask='subscribe'>"
          "<group>Business Partners</group>"
        "</item>"
        "<item jid='walter@example.net' "
              "name='Walter Sobchak' "
              "subscription='both'>"
          "<group>Friends</group>"
          "<group>Bowling Team</group>"
          "<group>Bowling League</group>"
        "</item>"
        "<item jid='donny@example.net' "
              "name='Donny' "
              "subscription='both'>"
          "<group>Friends</group>"
          "<group>Bowling Team</group>"
          "<group>Bowling League</group>"
        "</item>"
        "<item jid='jeffrey@example.net' "
              "name='The Big Lebowski' "
              "subscription='to'>"
          "<group>Business Partners</group>"
        "</item>"
        "<item jid='jesus@example.net' "
              "name='Jesus Quintana' "
              "subscription='from'>"
          "<group>Bowling League</group>"
        "</item>"
      "</query>"
    "</iq>";

  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[ContactsAdded index:0 number:5 "
      "0:[Contact jid:maude@example.net name:Maude Lebowski "
                 "subscription_state:none_asked "
                 "groups:[Business Partners]]"
        "<ros:item jid=\"maude@example.net\" name=\"Maude Lebowski\" "
                  "subscription=\"none\" ask=\"subscribe\" "
                  "xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Business Partners</ros:group>"
        "</ros:item> "
      "1:[Contact jid:walter@example.net name:Walter Sobchak "
                 "subscription_state:both "
                 "groups:[Friends, Bowling Team, Bowling League]]"
        "<ros:item jid=\"walter@example.net\" name=\"Walter Sobchak\" "
                  "subscription=\"both\" "
                  "xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Friends</ros:group>"
          "<ros:group>Bowling Team</ros:group>"
          "<ros:group>Bowling League</ros:group>"
        "</ros:item> "
      "2:[Contact jid:donny@example.net name:Donny "
                 "subscription_state:both "
                 "groups:[Friends, Bowling Team, Bowling League]]"
        "<ros:item jid=\"donny@example.net\" name=\"Donny\" "
                  "subscription=\"both\" "
                  "xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Friends</ros:group>"
          "<ros:group>Bowling Team</ros:group>"
          "<ros:group>Bowling League</ros:group>"
        "</ros:item> "
      "3:[Contact jid:jeffrey@example.net name:The Big Lebowski "
                 "subscription_state:to "
                 "groups:[Business Partners]]"
        "<ros:item jid=\"jeffrey@example.net\" name=\"The Big Lebowski\" "
                  "subscription=\"to\" "
                  "xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Business Partners</ros:group>"
        "</ros:item> "
      "4:[Contact jid:jesus@example.net name:Jesus Quintana "
                 "subscription_state:from groups:[Bowling League]]"
        "<ros:item jid=\"jesus@example.net\" name=\"Jesus Quintana\" "
                  "subscription=\"from\" xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Bowling League</ros:group>"
        "</ros:item>]");
  EXPECT_EQ(handler.OutputActivity(), "");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Request that someone be added
  rtc::scoped_ptr<XmppRosterContact> contact(XmppRosterContact::Create());
  TEST_OK(contact->set_jid(Jid("brandt@example.net")));
  TEST_OK(contact->set_name("Brandt"));
  TEST_OK(contact->AddGroup("Business Partners"));
  TEST_OK(contact->AddGroup("Watchers"));
  TEST_OK(contact->AddGroup("Friends"));
  TEST_OK(contact->RemoveGroup("Friends")); // Maybe not...
  TEST_OK(roster->RequestRosterChange(contact.get()));

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<iq type=\"set\" id=\"3\">"
      "<query xmlns=\"jabber:iq:roster\">"
        "<item jid=\"brandt@example.net\" "
              "name=\"Brandt\">"
          "<group>Business Partners</group>"
          "<group>Watchers</group>"
        "</item>"
      "</query>"
    "</iq>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Get the push from the server
  input =
    "<iq type='result' to='david@my-server/test' id='3'/>"
    "<iq type='set' id='server_1'>"
      "<query xmlns='jabber:iq:roster'>"
        "<item jid='brandt@example.net' "
              "name='Brandt' "
              "subscription='none'>"
          "<group>Business Partners</group>"
          "<group>Watchers</group>"
        "</item>"
      "</query>"
    "</iq>";
  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[ContactsAdded index:5 number:1 "
      "5:[Contact jid:brandt@example.net name:Brandt "
                 "subscription_state:none "
                 "groups:[Business Partners, Watchers]]"
        "<ros:item jid=\"brandt@example.net\" name=\"Brandt\" "
                  "subscription=\"none\" "
                  "xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Business Partners</ros:group>"
          "<ros:group>Watchers</ros:group>"
        "</ros:item>]");
  EXPECT_EQ(handler.OutputActivity(),
    "<iq type=\"result\" id=\"server_1\"/>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Get a contact update
  input =
    "<iq type='set' id='server_2'>"
      "<query xmlns='jabber:iq:roster'>"
        "<item jid='walter@example.net' "
              "name='Walter Sobchak' "
              "subscription='both'>"
          "<group>Friends</group>"
          "<group>Bowling Team</group>"
          "<group>Bowling League</group>"
          "<group>Not wrong, just an...</group>"
        "</item>"
      "</query>"
    "</iq>";

  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[ContactChanged "
      "old_contact:[Contact jid:walter@example.net name:Walter Sobchak "
                           "subscription_state:both "
                           "groups:[Friends, Bowling Team, Bowling League]]"
        "<ros:item jid=\"walter@example.net\" name=\"Walter Sobchak\" "
                  "subscription=\"both\" xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Friends</ros:group>"
          "<ros:group>Bowling Team</ros:group>"
          "<ros:group>Bowling League</ros:group>"
          "</ros:item> "
      "index:1 "
      "new_contact:[Contact jid:walter@example.net name:Walter Sobchak "
                           "subscription_state:both "
                           "groups:[Friends, Bowling Team, Bowling League, "
                                   "Not wrong, just an...]]"
        "<ros:item jid=\"walter@example.net\" name=\"Walter Sobchak\" "
                  "subscription=\"both\" xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Friends</ros:group>"
          "<ros:group>Bowling Team</ros:group>"
          "<ros:group>Bowling League</ros:group>"
          "<ros:group>Not wrong, just an...</ros:group>"
        "</ros:item>]");
  EXPECT_EQ(handler.OutputActivity(),
    "<iq type=\"result\" id=\"server_2\"/>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Remove a contact
  TEST_OK(roster->RequestRosterRemove(Jid("jesus@example.net")));

  EXPECT_EQ(roster_handler.StrClear(), "");
  EXPECT_EQ(handler.OutputActivity(),
    "<iq type=\"set\" id=\"4\">"
      "<query xmlns=\"jabber:iq:roster\" jid=\"jesus@example.net\" "
             "subscription=\"remove\"/>"
    "</iq>");
  EXPECT_EQ(handler.SessionActivity(), "");

  // Response from the server
  input =
    "<iq type='result' to='david@my-server/test' id='4'/>"
    "<iq type='set' id='server_3'>"
      "<query xmlns='jabber:iq:roster'>"
        "<item jid='jesus@example.net' "
              "subscription='remove'>"
        "</item>"
      "</query>"
    "</iq>";
  TEST_OK(engine->HandleInput(input.c_str(), input.length()));

  EXPECT_EQ(roster_handler.StrClear(),
    "[ContactRemoved "
      "old_contact:[Contact jid:jesus@example.net name:Jesus Quintana "
                           "subscription_state:from groups:[Bowling League]]"
        "<ros:item jid=\"jesus@example.net\" name=\"Jesus Quintana\" "
                  "subscription=\"from\" "
                  "xmlns:ros=\"jabber:iq:roster\">"
          "<ros:group>Bowling League</ros:group>"
        "</ros:item> index:4]");
  EXPECT_EQ(handler.OutputActivity(),
    "<iq type=\"result\" id=\"server_3\"/>");
  EXPECT_EQ(handler.SessionActivity(), "");
}

}