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 "../../tag.h"
14 #define MUCROOM_TEST
15 #include "../../mucroom.h"
16 #include "../../dataform.h"
17 #include "../../iq.h"
18 #include "../../message.h"
19 #include "../../stanzaextensionfactory.h"
20 using namespace gloox;
21 
22 #include <stdio.h>
23 #include <locale.h>
24 #include <string>
25 #include <cstdio> // [s]print[f]
26 
main(int,char **)27 int main( int /*argc*/, char** /*argv*/ )
28 {
29   int fail = 0;
30   std::string name;
31   Tag *t;
32 
33   // -------
34   {
35     name = "set role 'none'";
36     MUCRoom::MUCAdmin ma( RoleNone, "foo", "fooish" );
37     t = ma.tag();
38     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
39          "<item nick='foo' role='none'>"
40          "<reason>fooish</reason>"
41          "</item></query>" )
42     {
43       ++fail;
44       fprintf( stderr, "test '%s' failed\n", name.c_str() );
45     }
46     delete t;
47   }
48 
49   // -------
50   {
51     name = "set role 'visitor'";
52     MUCRoom::MUCAdmin ma( RoleVisitor, "foo", "fooish" );
53     t = ma.tag();
54     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
55          "<item nick='foo' role='visitor'>"
56          "<reason>fooish</reason>"
57          "</item></query>" )
58     {
59       ++fail;
60       fprintf( stderr, "test '%s' failed\n", name.c_str() );
61     }
62     delete t;
63   }
64 
65   // -------
66   {
67     name = "set role 'participant'";
68     MUCRoom::MUCAdmin ma( RoleParticipant, "foo", "fooish" );
69     t = ma.tag();
70     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
71          "<item nick='foo' role='participant'>"
72          "<reason>fooish</reason>"
73          "</item></query>" )
74     {
75       ++fail;
76       fprintf( stderr, "test '%s' failed\n", name.c_str() );
77     }
78     delete t;
79   }
80 
81   // -------
82   {
83     name = "set role 'moderator'";
84     MUCRoom::MUCAdmin ma( RoleModerator, "foo", "fooish" );
85     t = ma.tag();
86     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
87          "<item nick='foo' role='moderator'>"
88          "<reason>fooish</reason>"
89          "</item></query>" )
90     {
91       ++fail;
92       fprintf( stderr, "test '%s' failed\n", name.c_str() );
93     }
94     delete t;
95   }
96 
97   // -------
98   {
99     name = "set affiliation 'none'";
100     MUCRoom::MUCAdmin ma( AffiliationNone, "foo", "fooish" );
101     t = ma.tag();
102     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
103          "<item nick='foo' affiliation='none'>"
104          "<reason>fooish</reason>"
105          "</item></query>" )
106     {
107       ++fail;
108       fprintf( stderr, "test '%s' failed\n", name.c_str() );
109     }
110     delete t;
111   }
112 
113   // -------
114   {
115     name = "set affiliation 'outcast'";
116     MUCRoom::MUCAdmin ma( AffiliationOutcast, "foo", "fooish" );
117     t = ma.tag();
118     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
119          "<item nick='foo' affiliation='outcast'>"
120          "<reason>fooish</reason>"
121          "</item></query>" )
122     {
123       ++fail;
124       fprintf( stderr, "test '%s' failed\n", name.c_str() );
125     }
126     delete t;
127   }
128 
129   // -------
130   {
131     name = "set affiliation 'member'";
132     MUCRoom::MUCAdmin ma( AffiliationMember, "foo", "fooish" );
133     t = ma.tag();
134     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
135          "<item nick='foo' affiliation='member'>"
136          "<reason>fooish</reason>"
137          "</item></query>" )
138     {
139       ++fail;
140       fprintf( stderr, "test '%s' failed\n", name.c_str() );
141     }
142     delete t;
143   }
144 
145   // -------
146   {
147     name = "set affiliation 'owner'";
148     MUCRoom::MUCAdmin ma( AffiliationOwner, "foo", "fooish" );
149     t = ma.tag();
150     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
151          "<item nick='foo' affiliation='owner'>"
152          "<reason>fooish</reason>"
153          "</item></query>" )
154     {
155       ++fail;
156       fprintf( stderr, "test '%s' failed\n", name.c_str() );
157     }
158     delete t;
159   }
160 
161   // -------
162   {
163     name = "set affiliation 'admin'";
164     MUCRoom::MUCAdmin ma( AffiliationAdmin, "foo", "fooish" );
165     t = ma.tag();
166     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
167          "<item nick='foo' affiliation='admin'>"
168          "<reason>fooish</reason>"
169          "</item></query>" )
170     {
171       ++fail;
172       fprintf( stderr, "test '%s' failed\n", name.c_str() );
173     }
174     delete t;
175   }
176 
177   // -------
178   {
179     name = "request role list: 'participant'";
180     MUCRoom::MUCAdmin ma( RequestVoiceList );
181     t = ma.tag();
182     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
183          "<item role='participant'/>"
184          "</query>" )
185     {
186       ++fail;
187       fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
188     }
189     delete t;
190   }
191 
192   // -------
193   {
194     name = "request role list: 'moderator'";
195     MUCRoom::MUCAdmin ma( RequestModeratorList );
196     t = ma.tag();
197     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
198          "<item role='moderator'/>"
199          "</query>" )
200     {
201       ++fail;
202       fprintf( stderr, "test '%s' failed\n", name.c_str() );
203     }
204     delete t;
205   }
206 
207   // -------
208   {
209     name = "request affiliation list: 'outcast'";
210     MUCRoom::MUCAdmin ma( RequestBanList );
211     t = ma.tag();
212     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
213          "<item affiliation='outcast'/>"
214          "</query>" )
215     {
216       ++fail;
217       fprintf( stderr, "test '%s' failed\n", name.c_str() );
218     }
219     delete t;
220   }
221 
222   // -------
223   {
224     name = "request affiliation list: 'member'";
225     MUCRoom::MUCAdmin ma( RequestMemberList );
226     t = ma.tag();
227     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
228          "<item affiliation='member'/>"
229          "</query>" )
230     {
231       ++fail;
232       fprintf( stderr, "test '%s' failed\n", name.c_str() );
233     }
234     delete t;
235   }
236 
237   // -------
238   {
239     name = "store affiliation list: 'member'";
240     MUCListItemList list;
241     list.push_back( MUCListItem( JID( "foo@bar" ) ) );
242     list.push_back( MUCListItem( JID( "bar@foo" ) ) );
243     MUCRoom::MUCAdmin ma( StoreMemberList, list );
244     t = ma.tag();
245     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
246          "<item jid='foo@bar' affiliation='member'/>"
247          "<item jid='bar@foo' affiliation='member'/>"
248          "</query>" )
249     {
250       ++fail;
251       fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
252     }
253     delete t;
254   }
255 
256   // -------
257   {
258     name = "store role list: 'participant'";
259     MUCListItemList list;
260     list.push_back( MUCListItem( JID( "foo@bar" ) ) );
261     list.push_back( MUCListItem( JID( "bar@foo" ) ) );
262     MUCRoom::MUCAdmin ma( StoreVoiceList, list );
263     t = ma.tag();
264     if( !t || t->xml() != "<query xmlns='" + XMLNS_MUC_ADMIN + "'>"
265          "<item jid='foo@bar' role='participant'/>"
266          "<item jid='bar@foo' role='participant'/>"
267          "</query>" )
268     {
269       ++fail;
270       fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
271     }
272     delete t;
273   }
274 
275   // -------
276   {
277     name = "parse member list from Tag";
278     Tag q( "query" );
279     q.setXmlns( XMLNS_MUC_ADMIN );
280     Tag* i = new Tag( &q, "item", "jid", "foo@bar" );
281     i->addAttribute( "affiliation", "member" );
282     i = new Tag( &q, "item", "jid", "bar@foo" );
283     i->addAttribute( "affiliation", "member" );
284     MUCRoom::MUCAdmin ma( &q );
285     Tag* t = ma.tag();
286     if( !t || q != *t )
287     {
288       ++fail;
289       fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
290     }
291     delete t;
292   }
293 
294   // -------
295   name = "MUCRoom::MUCAdmin/SEFactory test";
296   StanzaExtensionFactory sef;
297   sef.registerExtension( new MUCRoom::MUCAdmin() );
298   Tag* f = new Tag( "iq" );
299   new Tag( f, "query", "xmlns", XMLNS_MUC_ADMIN );
300   IQ iq( IQ::Set, JID(), "" );
301   sef.addExtensions( iq, f );
302   const MUCRoom::MUCAdmin* se = iq.findExtension<MUCRoom::MUCAdmin>( ExtMUCAdmin );
303   if( se == 0 )
304   {
305     ++fail;
306     fprintf( stderr, "test '%s' failed\n", name.c_str() );
307   }
308   delete f;
309 
310 
311   printf( "MUCRoom::MUCAdmin: " );
312   if( !fail )
313     printf( "OK\n" );
314   else
315     fprintf( stderr, "%d test(s) failed\n", fail );
316 
317   return fail;
318 }
319