1 /*
2  *  Copyright (c) 2004-2019 by Jakob Schröter <js@camaya.net>
3  *  This file is part of the gloox library. http://camaya.net/gloox
4  *
5  *  This software is distributed under a license. The full license
6  *  agreement can be found in the file LICENSE in this distribution.
7  *  This software may not be copied, modified, sold or distributed
8  *  other than expressed in the named license agreement.
9  *
10  *  This software is distributed without any warranty.
11  */
12 
13 #include "../../pubsubmanager.h"
14 #include "../../pubsubresulthandler.h"
15 
16 #include <cstdio> // [s]print[f]
17 
18 using namespace gloox;
19 
20 class RH : public PubSub::ResultHandler
21 {
22   public:
handleItem(const JID &,const std::string &,const Tag *)23     void handleItem( const JID&, const std::string&, const Tag* ) {}
handleItems(const std::string &,const JID &,const std::string &,const PubSub::ItemList &,const Error *)24     void handleItems( const std::string&,
25                       const JID&, const std::string&, const PubSub::ItemList&, const Error* ) {}
handleItemPublication(const std::string &,const JID &,const std::string &,const PubSub::ItemList &,const Error *)26     void handleItemPublication( const std::string&,
27                                 const JID&, const std::string&,
28                                 const PubSub::ItemList&,
29                                 const Error* ) {}
handleItemDeletion(const std::string &,const JID &,const std::string &,const PubSub::ItemList &,const Error *)30     void handleItemDeletion( const std::string&,
31                              const JID&, const std::string&,
32                                           const PubSub::ItemList&,
33                                           const Error* ) {}
handleSubscriptionResult(const std::string &,const JID &,const std::string &,const std::string &,const JID &,const PubSub::SubscriptionType,const Error *)34     void handleSubscriptionResult( const std::string&,
35                                    const JID&, const std::string&,
36                                                const std::string&,
37                                                const JID&,
38                                                const PubSub::SubscriptionType,
39                                                const Error* ) {}
handleUnsubscriptionResult(const std::string &,const JID &,const Error *)40     void handleUnsubscriptionResult( const std::string&,
41                                      const JID&,
42                                                  const Error* ) {}
handleSubscriptionOptions(const std::string &,const JID &,const JID &,const std::string &,const DataForm *,const std::string &,const Error *)43     void handleSubscriptionOptions( const std::string&,
44                                     const JID&, const JID&,
45                                                 const std::string&,
46                                                 const DataForm*,
47                                                 const std::string&,
48                                                 const Error*) {}
handleSubscriptionOptionsResult(const std::string &,const JID &,const JID &,const std::string &,const std::string &,const Error *)49     void handleSubscriptionOptionsResult( const std::string&,
50                                           const JID&, const JID&,
51                                                       const std::string&,
52                                                       const std::string&,
53                                                       const Error* ) {}
handleSubscribers(const std::string &,const JID &,const std::string &,const PubSub::SubscriptionList &,const Error *)54     void handleSubscribers( const std::string&,
55                             const JID&, const std::string&,
56                                         const PubSub::SubscriptionList&,
57                                         const Error* ) {}
handleSubscribersResult(const std::string &,const JID &,const std::string &,const PubSub::SubscriberList *,const Error *)58     void handleSubscribersResult( const std::string&,
59                                   const JID&, const std::string&,
60                                   const PubSub::SubscriberList*,
61                                   const Error* ) {}
handleAffiliates(const std::string &,const JID &,const std::string &,const PubSub::AffiliateList *,const Error *)62     void handleAffiliates( const std::string&,
63                            const JID&, const std::string&,
64                                        const PubSub::AffiliateList*,
65                                        const Error* ) {}
handleAffiliatesResult(const std::string &,const JID &,const std::string &,const PubSub::AffiliateList *,const Error *)66     void handleAffiliatesResult( const std::string&,
67                                  const JID&, const std::string&,
68                                  const PubSub::AffiliateList*,
69                                  const Error* ) {}
handleNodeConfig(const std::string &,const JID &,const std::string &,const DataForm *,const Error *)70     void handleNodeConfig( const std::string&,
71                            const JID&, const std::string&,
72                                        const DataForm*,
73                                        const Error* ) {}
handleNodeConfigResult(const std::string &,const JID &,const std::string &,const Error *)74     void handleNodeConfigResult( const std::string&,
75                                  const JID&, const std::string&, const Error* ) {}
handleNodeCreation(const std::string &,const JID &,const std::string &,const Error *)76     void handleNodeCreation( const std::string&,
77                              const JID&, const std::string&, const Error* ) {}
handleNodeDeletion(const std::string &,const JID &,const std::string &,const Error *)78     void handleNodeDeletion( const std::string&,
79                              const JID&, const std::string&, const Error* ) {}
80 
handleNodePurge(const std::string &,const JID &,const std::string &,const Error *)81     void handleNodePurge( const std::string&,
82                           const JID&, const std::string&, const Error* ) {}
handleSubscriptions(const std::string &,const JID &,const PubSub::SubscriptionMap &,const Error *)83     void handleSubscriptions( const std::string&,
84                               const JID&, const PubSub::SubscriptionMap&, const Error* ) {}
handleAffiliations(const std::string &,const JID &,const PubSub::AffiliationMap &,const Error *)85     void handleAffiliations( const std::string&,
86                              const JID&, const PubSub::AffiliationMap&, const Error* ) {}
handleDefaultNodeConfig(const std::string &,const JID &,const DataForm *,const Error *)87     void handleDefaultNodeConfig( const std::string&,
88                                   const JID&, const DataForm*, const Error*) {}
89 
90 };
91 
92 enum
93 {
94   SubscriptionDefault,
95   SubscriptionJID,
96   SubscriptionSubType,
97   SubscriptionDepth,
98 
99   UnsubscriptionDefault,
100   UnsubscriptionJID,
101 
102   GetSubscriptionOptions,
103   SetSubscriptionOptions,
104 
105   GetSubscriptionList,
106   GetSubscriberList,
107   SetSubscriberList,
108 
109   GetAffiliationList,
110   GetAffiliateList,
111   SetAffiliateList,
112 
113   GetNodeConfig,
114   SetNodeConfig,
115   DefaultNodeConfig,
116 
117   GetItemList,
118   PublishItem,
119   DeleteItem,
120 
121   CreateNodeLeaf,
122   DeleteNodeCollection,
123   PurgeNode,
124 
125   NodeAssociation,
126   NodeDisassociation
127 
128 };
129 
130 static const JID jid( "aaa@bbb.ccc" );
131 static const std::string node( "node" );
132 static const std::string subid( "subid" );
133 
134 static const Tag* tag;
135 
136 namespace gloox
137 {
138 
139 class ClientBase
140 {
141   public:
ClientBase()142     ClientBase() : failed( 0 ) {}
setTest(int test)143     void setTest( int test ) { m_context = test; }
getID()144     const std::string getID()
145     {
146       static const std::string id( "id" );
147       return id;
148     }
jid()149     const JID& jid() { return ::jid; }
150 
151     void send( const IQ& iq, IqHandler* = 0, int context = 0 );
152 
registerStanzaExtension(StanzaExtension * se)153     void registerStanzaExtension( StanzaExtension* se )
154       { delete se; }
155 
156     int failed;
157 
158   protected:
159     int m_context;
160 };
161 
162 static const std::string setheader =
163     "<iq to='aaa@bbb.ccc' id='id' type='set'>"
164       "<pubsub xmlns='http://jabber.org/protocol/pubsub'>";
165 
166 static const std::string getheader =
167     "<iq to='aaa@bbb.ccc' id='id' type='get'>"
168       "<pubsub xmlns='http://jabber.org/protocol/pubsub'>";
169 
170 static const std::string setheaderOwner =
171     "<iq to='aaa@bbb.ccc' id='id' type='set'>"
172     "<pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>";
173 
174 static const std::string getheaderOwner =
175     "<iq to='aaa@bbb.ccc' id='id' type='get'>"
176     "<pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>";
177 static const std::string testValues [][2] =
178 {
179   { "subscription basic",
180     setheader + "<subscribe node='node' jid='aaa@bbb.ccc'/></pubsub></iq>" },
181 
182   { "subscription other",
183     setheader + "<subscribe node='node' jid='some@jid.com'/></pubsub></iq>" },
184 
185   { "subscription items",
186     setheader + "<subscribe node='node' jid='some@jid.com'/>"
187     "<options>"
188         "<x xmlns='jabber:x:data' type='submit'>"
189         "<field type='hidden' var='FORM_TYPE'>"
190                 "<value>http://jabber.org/protocol/pubsub#subscribe_options</value>"
191             "</field>"
192             "<field var='pubsub#subscription_type'><value>items</value></field>"
193         "</x>"
194     "</options></pubsub></iq>" },
195 
196   { "subscription depth",
197     setheader + "<subscribe node='node' jid='some@jid.com'/>"
198     "<options>"
199         "<x xmlns='jabber:x:data' type='submit'>"
200             "<field type='hidden' var='FORM_TYPE'>"
201                 "<value>http://jabber.org/protocol/pubsub#subscribe_options</value>"
202             "</field>"
203             "<field var='pubsub#subscription_type'><value>items</value></field>"
204             "<field var='pubsub#subscription_depth'><value>all</value></field>"
205         "</x>"
206     "</options></pubsub></iq>" },
207 
208   { "unsubscription default",
209     setheader + "<unsubscribe node='node' jid='aaa@bbb.ccc' subid='subid'/></pubsub></iq>" },
210 
211   { "unsubsription other",
212   setheader + "<unsubscribe node='node' jid='some@jid.com' subid='subid'/></pubsub></iq>" },
213 
214   { "subscription options get",
215     getheader + "<options node='node' jid='some@jid.com'/></pubsub></iq>" },
216 
217   { "subscription options set",
218     setheader + "<options node='node' jid='some@jid.com'>"
219          "<x xmlns='jabber:x:data' type='submit'>"
220              "<field type='hidden' var='FORM_TYPE'>"
221                  "<value>http://jabber.org/protocol/pubsub#subscribe_options</value>"
222              "</field>"
223          "</x>"
224      "</options></pubsub></iq>" },
225 
226   { "subscription list get",
227     getheader + "<subscriptions/></pubsub></iq>" },
228 
229   { "subscriber list get",
230     getheaderOwner + "<subscriptions node='node'/>"
231       "</pubsub></iq>" },
232 
233   { "subscriber list set",
234     setheaderOwner + "<subscriptions node='node'>"
235                 "<subscription jid='some@jid.com' subscription='none' subid='abc'/>"
236             "</subscriptions>"
237         "</pubsub></iq>" },
238 
239   { "affiliation list get",
240     getheader + "<affiliations/></pubsub></iq>" },
241 
242   { "affiliate list get",
243     getheaderOwner + "<affiliations node='node'/>"
244       "</pubsub></iq>" },
245 
246   { "affiliate list set",
247     setheaderOwner + "<affiliations node='node'>"
248                 "<affiliation jid='some@jid.com' affiliation='owner'/>"
249             "</affiliations>"
250         "</pubsub></iq>" },
251 
252   { "get node config",
253     getheaderOwner + "<configure node='node'/>"
254         "</pubsub></iq>" },
255 
256   { "set node config",
257     setheaderOwner + "<configure node='node'>"
258                 "<x xmlns='jabber:x:data' type='submit'>"
259                     "<field type='hidden' var='FORM_TYPE'>"
260                         "<value>http://jabber.org/protocol/pubsub#node_config</value>"
261                     "</field>"
262                     "<field var='pubsub#title'><value>Princely Musings (Atom)</value></field>"
263                 "</x>"
264             "</configure>"
265         "</pubsub></iq>" },
266 
267   { "get default node config",
268     getheaderOwner + "<default/>"
269         "</pubsub></iq>" }
270     };
271 
send(const IQ & iq,IqHandler *,int)272     void ClientBase::send( const IQ& iq, IqHandler*, int )
273     {
274       tag = iq.tag();
275       if( !tag || tag->xml() != testValues[m_context][1] )
276       {
277         fprintf( stderr, "test failed: %s\n", testValues[m_context][0].c_str() );
278         printf( "- %s\n", testValues[m_context][1].c_str() );
279         if( tag )
280           printf( "- %s\n", tag->xml().c_str() );
281         ++failed;
282       }
283       delete tag;
284     }
285 
286 
287 }
288 
289 #define CLIENTBASE_H__
290 #include "../../pubsubmanager.cpp"
291 
292 JID jid2( "some@jid.com" );
293 
main()294 int main()
295 {
296   ClientBase* cb = new ClientBase();
297   PubSub::Manager* psm = new PubSub::Manager( cb );
298   RH* rh = new RH();
299 
300   cb->setTest( SubscriptionDefault );
301   psm->subscribe( jid, node, rh );
302 
303   cb->setTest( SubscriptionJID );
304   psm->subscribe( jid, node, rh, jid2 );
305 
306   cb->setTest( SubscriptionSubType );
307   psm->subscribe( jid, node, rh, jid2, PubSub::SubscriptionItems );
308 
309   cb->setTest( SubscriptionDepth );
310   psm->subscribe( jid, node, rh, jid2, PubSub::SubscriptionItems, 0 );
311 
312   cb->setTest( UnsubscriptionDefault );
313   psm->unsubscribe( jid, node, subid, rh );
314 
315   cb->setTest( UnsubscriptionJID );
316   psm->unsubscribe( jid, node, subid, rh, jid2 );
317 
318   cb->setTest( GetSubscriptionOptions );
319   psm->getSubscriptionOptions( jid, jid2, node, rh );
320 
321   DataForm* df = new DataForm( TypeSubmit );
322   df->addField( DataFormField::TypeHidden, "FORM_TYPE",
323       "http://jabber.org/protocol/pubsub#subscribe_options" );
324   cb->setTest( SetSubscriptionOptions );
325   psm->setSubscriptionOptions( jid, jid2, node, df, rh );
326 
327   cb->setTest( GetSubscriptionList );
328   psm->getSubscriptions( jid, rh );
329 
330   cb->setTest( GetSubscriberList );
331   psm->getSubscribers( jid, node, rh );
332 
333   PubSub::SubscriberList sl;
334   sl.push_back( PubSub::Subscriber( jid2, PubSub::SubscriptionNone, "abc" ) );
335   cb->setTest( SetSubscriberList );
336   psm->setSubscribers( jid, node, sl, rh );
337 
338   cb->setTest( GetAffiliationList );
339   psm->getAffiliations( jid, rh );
340 
341   cb->setTest( GetAffiliateList );
342   psm->getAffiliates( jid, node, rh );
343 
344   PubSub::AffiliateList al;
345   al.push_back( PubSub::Affiliate( jid2, PubSub::AffiliationOwner ) );
346   cb->setTest( SetAffiliateList );
347   psm->setAffiliates( jid, node, al, rh );
348 
349   cb->setTest( GetNodeConfig );
350   psm->getNodeConfig( jid, node, rh );
351 
352   df = new DataForm( TypeSubmit );
353   df->addField( DataFormField::TypeHidden, "FORM_TYPE",
354       "http://jabber.org/protocol/pubsub#node_config" );
355   df->addField( DataFormField::TypeNone, "pubsub#title", "Princely Musings (Atom)" );
356   cb->setTest( SetNodeConfig );
357   psm->setNodeConfig( jid, node, df, rh );
358 
359   cb->setTest( DefaultNodeConfig );
360   psm->getDefaultNodeConfig( jid, PubSub::NodeLeaf, rh );
361 
362 
363   delete rh;
364   delete psm;
365 
366 
367 
368 
369   if( cb->failed )
370     fprintf( stderr, "PubSub::Manager: %d test(s) failed\n", cb->failed );
371   else
372     printf( "PubSub::Manager: OK\n" );
373 
374   delete cb;
375 
376 }
377 
378