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 #define SEARCH_TEST
14 #include "../../gloox.h"
15 #include "../../jid.h"
16 #include "../../dataform.h"
17 #include "../../tag.h"
18 #include "../../iq.h"
19 #include "../../iqhandler.h"
20 #include "../../stanzaextension.h"
21 #include "../../stanzaextensionfactory.h"
22 using namespace gloox;
23 
24 #include <stdio.h>
25 #include <locale.h>
26 #include <string>
27 #include <cstdio> // [s]print[f]
28 
29 namespace gloox
30 {
31 
32   class Disco;
33   class Capabilities : public StanzaExtension
34   {
35     public:
Capabilities()36       Capabilities() : StanzaExtension( ExtUser + 1 ) {}
ver() const37       const std::string& ver() const { return EmptyString; }
node() const38       const std::string& node() const { return EmptyString; }
39   };
40 
41   class ClientBase
42   {
43     public:
ClientBase()44       ClientBase() {}
~ClientBase()45       virtual ~ClientBase() {}
getID()46       const std::string getID() { return "id"; }
47       virtual void send( IQ& iq, IqHandler*, int ) = 0;
48       virtual void trackID( IqHandler *ih, const std::string& id, int context ) = 0;
removeIDHandler(IqHandler *)49       void removeIDHandler( IqHandler* ) {}
registerStanzaExtension(StanzaExtension * ext)50       void registerStanzaExtension( StanzaExtension* ext ) { delete ext; }
removeStanzaExtension(int)51       void removeStanzaExtension( int ) {}
52   };
53 
54 }
55 
56 #define CLIENTBASE_H__
57 #include "../../search.h"
58 #include "../../search.cpp"
59 
main(int,char **)60 int main( int /*argc*/, char** /*argv*/ )
61 {
62   int fail = 0;
63   std::string name;
64 
65   // -------
66   {
67     name = "fetch fields";
68     Search::Query sq;
69     Tag* t = sq.tag();
70     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'/>" )
71     {
72       ++fail;
73       fprintf( stderr, "test '%s' failed\n", name.c_str() );
74     }
75     delete t;
76   }
77 
78   // -------
79   {
80     name = "receive search fields";
81     Tag* d = new Tag( "query" );
82     d->setXmlns( XMLNS_SEARCH );
83     new Tag( d, "instructions", "foobar" );
84     new Tag( d, "first" );
85     new Tag( d, "last" );
86     new Tag( d, "email" );
87     new Tag( d, "nick" );
88     Search::Query sq( d );
89     Tag* t = sq.tag();
90     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'>"
91          "<instructions>foobar</instructions>"
92          "<first/>"
93          "<last/>"
94          "<nick/>"
95          "<email/>"
96          "</query>"
97          || sq.instructions() != "foobar"
98          || sq.fields() != ( SearchFieldFirst | SearchFieldLast | SearchFieldNick | SearchFieldEmail ) )
99     {
100       ++fail;
101       fprintf( stderr, "test '%s' failed\n", name.c_str() );
102     }
103     delete t;
104     delete d;
105   }
106 
107   // -------
108   {
109     name = "receive search form";
110     Tag* d = new Tag( "query" );
111     d->setXmlns( XMLNS_SEARCH );
112     Tag* f = new Tag( d, "x" );
113     f->setXmlns( XMLNS_X_DATA );
114     f->addAttribute( "type", "form" );
115     Search::Query sq( d );
116     Tag* t = sq.tag();
117     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'>"
118          "<x xmlns='" + XMLNS_X_DATA + "' type='form'/>"
119          "</query>"
120          || !sq.form() )
121     {
122       ++fail;
123       fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
124     }
125     delete t;
126     delete d;
127   }
128 
129   // -------
130   {
131     name = "search by form";
132     DataForm* form = new DataForm( TypeSubmit );
133     Search::Query sq( form );
134     Tag* t = sq.tag();
135     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'>"
136        "<x xmlns='" + XMLNS_X_DATA + "' type='submit'/>"
137        "</query>" )
138     {
139       ++fail;
140       fprintf( stderr, "test '%s' failed\n", name.c_str() );
141     }
142     delete t;
143   }
144 
145   // -------
146   {
147     name = "search by fields";
148     SearchFieldStruct sfs( "first", "last", "nick", "email" );
149     Search::Query sq( SearchFieldFirst | SearchFieldLast | SearchFieldNick | SearchFieldEmail, sfs );
150     Tag* t = sq.tag();
151     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'>"
152          "<first>first</first>"
153          "<last>last</last>"
154          "<nick>nick</nick>"
155          "<email>email</email>"
156          "</query>" )
157     {
158       ++fail;
159       fprintf( stderr, "test '%s' failed\n", name.c_str() );
160     }
161     delete t;
162   }
163 
164   // -------
165   {
166     name = "receive form result";
167     Tag* d = new Tag( "query" );
168     d->setXmlns( XMLNS_SEARCH );
169     Tag* f = new Tag( d, "x" );
170     f->setXmlns( XMLNS_X_DATA );
171     f->addAttribute( "type", "result" );
172     Search::Query sq( d );
173     Tag* t = sq.tag();
174     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'>"
175          "<x xmlns='" + XMLNS_X_DATA + "' type='result'/>"
176          "</query>" )
177     {
178       ++fail;
179       fprintf( stderr, "test '%s' failed\n", name.c_str() );
180     }
181     delete t;
182     delete d;
183   }
184 
185   // -------
186   {
187     name = "receive fields result";
188     Tag* d = new Tag( "query" );
189     d->setXmlns( XMLNS_SEARCH );
190     Tag* i = new Tag( d, "item" );
191     i->addAttribute( "jid", "foo@bar" );
192     new Tag( i, "first", "first1" );
193     new Tag( i, "last", "last1" );
194     new Tag( i, "email", "email1" );
195     new Tag( i, "nick", "nick1" );
196     i = new Tag( d, "item" );
197     i->addAttribute( "jid", "foo@bar2" );
198     new Tag( i, "first", "first2" );
199     new Tag( i, "last", "last2" );
200     new Tag( i, "nick", "nick2" );
201     new Tag( i, "email", "email2" );
202     Search::Query sq( d );
203     Tag* t = sq.tag();
204     SearchResultList srl = sq.result();
205     if( !t || t->xml() != "<query xmlns='" + XMLNS_SEARCH + "'>"
206          "<item jid='foo@bar'>"
207          "<first>first1</first>"
208          "<last>last1</last>"
209          "<nick>nick1</nick>"
210          "<email>email1</email></item>"
211          "<item jid='foo@bar2'>"
212          "<first>first2</first>"
213          "<last>last2</last>"
214          "<nick>nick2</nick>"
215          "<email>email2</email></item>"
216          "</query>"
217        || srl.size() != 2 )
218     {
219       ++fail;
220       fprintf( stderr, "test '%s' failed: %s\n", name.c_str(), t->xml().c_str() );
221     }
222     delete t;
223     delete d;
224   }
225 
226 
227 
228 
229   // -------
230   name = "Search::Query/SEFactory test";
231   StanzaExtensionFactory sef;
232   sef.registerExtension( new Search::Query() );
233   Tag* f = new Tag( "iq" );
234   new Tag( f, "query", "xmlns", XMLNS_SEARCH );
235   IQ iq( IQ::Get, JID() );
236   sef.addExtensions( iq, f );
237   const Search::Query* se = iq.findExtension<Search::Query>( ExtSearch );
238   if( se == 0 )
239   {
240     ++fail;
241     fprintf( stderr, "test '%s' failed\n", name.c_str() );
242   }
243   delete f;
244 
245 
246 
247   printf( "Search::Query: " );
248   if( fail == 0 )
249   {
250     printf( "OK\n" );
251     return 0;
252   }
253   else
254   {
255     fprintf( stderr, "%d test(s) failed\n", fail );
256     return 1;
257   }
258 
259 }
260