~ubuntu-branches/ubuntu/vivid/gloox/vivid-proposed

« back to all changes in this revision

Viewing changes to src/tests/client/client_test.cpp

  • Committer: Package Import Robot
  • Author(s): Vincent Cheng
  • Date: 2014-03-16 17:34:43 UTC
  • mfrom: (12.1.2 experimental)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20140316173443-4s177dovzaz5dm8o
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#define CLIENTBASE_TEST
2
2
#include "../../client.h"
 
3
#include "../../clientbase.cpp"
3
4
#include "../../jid.h"
4
5
#include "../../connectionbase.h"
5
6
// #include "../../logsink.h"
13
14
#include <string>
14
15
#include <cstdio> // [s]print[f]
15
16
 
16
 
class ClientTest : public Client, /*LogHandler,*/ ConnectionListener
 
17
class ClientTest : public Client, LogHandler, ConnectionListener
17
18
{
18
19
  public:
19
20
    ClientTest( const JID& jid, const std::string& password, int port = -1 )
20
 
      : Client( jid, password, port ), m_idCount( 0 ), m_connected( 0 ), m_disconnected( 0 )
 
21
      : Client( jid, password, port ), m_connected( 0 ), m_disconnected( 0 ), m_log( false )
21
22
    {
22
 
//       logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
 
23
      logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
23
24
      registerConnectionListener( this );
 
25
      jidCopy = jid.full();
24
26
    }
25
27
    virtual ~ClientTest() {}
26
 
//     virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
27
 
//     {
28
 
//       printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
29
 
//     }
30
 
    virtual void onConnect() { ++m_connected; disconnect(); }
 
28
    virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
 
29
    {
 
30
      if( m_log )
 
31
        printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
 
32
    }
 
33
    virtual void onConnect()
 
34
    {
 
35
      ++m_connected;
 
36
      
 
37
      // magic jid
 
38
      if( jidCopy == "a@b/c" )
 
39
        disconnect();
 
40
    }
31
41
    virtual void onDisconnect( ConnectionError e )
32
42
    {
33
43
      ++m_disconnected;
44
54
    int disconnected() const { return m_disconnected; }
45
55
    ConnectionError disconnectReason() const { return m_disconnect; }
46
56
    StreamError streamErrorReason() const { return m_streamerror; }
47
 
 
48
 
    int m_idCount;
 
57
    void setLog( bool log ) { m_log = log; }
49
58
 
50
59
  protected:
51
60
 
54
63
    int m_disconnected;
55
64
    ConnectionError m_disconnect;
56
65
    StreamError m_streamerror;
 
66
    bool m_log;
 
67
    std::string jidCopy;
57
68
};
58
69
 
59
70
class ConnectionImpl : public ConnectionBase
103
114
    int m_test;
104
115
    int m_pos;
105
116
    bool m_run;
106
 
    static const char* m_msgs[4][9];
 
117
    static const char* m_msgs[6][12];
107
118
 
108
119
};
109
120
 
110
 
const char* ConnectionImpl::m_msgs[4][9] =
 
121
const char* ConnectionImpl::m_msgs[6][12] =
111
122
  {
112
123
    { // connection/auth goes ok.
113
124
      "<stream:stream from='jabber.cc' id='6kpid3u736sqjwd65n25wm57mzz10wz7hopvsj2w' version='1.0' "
186
197
      "</stream:features>",
187
198
      "<stream:error><xml-not-well-formed xmlns='urn:ietf:params:xml:ns:xmpp-streams'/></stream:error>",
188
199
      0,
189
 
    }
 
200
    },
 
201
    { // connection/auth goes ok. basic xep-0198 (stream management) ack'ing
 
202
      "<stream:stream from='jabber.cc' id='6kpid3u736sqjwd65n25wm57mzz10wz7hopvsj2w' version='1.0' "
 
203
      "xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"
 
204
      "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>"
 
205
      "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
 
206
      "<mechanism>PLAIN</mechanism>"
 
207
      "<mechanism>DIGEST-MD5</mechanism>"
 
208
      "</mechanisms>"
 
209
      "</stream:features>",
 
210
      "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
 
211
      "bm9uY2U9ImhvS1I2VkZDSGFibUVYY01weFhlL0QrcVZjWEdyMUdFNzQ0MVFzM2MxY2M9IixyZWFsbT0iamFiYmV"
 
212
      "yLmNjIixxb3A9ImF1dGgsYXV0aC1pbnQsYXV0aC1jb25mIixjaXBoZXI9InJjNC00MCxyYzQtNTYscmM0LGRlcyw"
 
213
      "zZGVzIixtYXhidWY9MTAyNCxjaGFyc2V0PXV0Zi04LGFsZ29yaXRobT1tZDUtc2Vzcw=="
 
214
      "</challenge>",
 
215
      "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
 
216
      "cnNwYXV0aD1mNGFhZTM0YWY0N2I1MmM0MmQ2NWQzY2NjMGNjN2YyNA=="
 
217
      "</challenge>",
 
218
      "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>",
 
219
      "<stream:stream from='jabber.cc' id='1o4p1gz2h0m1wvqutohs24d439nbv9zxx4nykm11' version='1.0' "
 
220
      "xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"
 
221
      "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>"
 
222
      "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
 
223
      "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>"
 
224
      "<sm xmlns='urn:xmpp:sm:3'/>"
 
225
      "</stream:features>",
 
226
      "<iq id='uid1' type='result' xmlns='jabber:client'>"
 
227
      "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'>"
 
228
      "<jid>hurkhurk@jabber.cc/gloox</jid></bind></iq>",
 
229
      "<enabled xmlns='urn:xmpp:sm:3' resume='true' id='some-long-id'/>",
 
230
      "<iq id='uid2' type='result' xmlns='jabber:client'/>",
 
231
      "<iq id='uid3' type='result' xmlns='jabber:client'><query xmlns='jabber:iq:private'>"
 
232
      "<roster xmlns='roster:delimiter'>::</roster></query></iq>"
 
233
      "<iq id='uid4' type='result' xmlns='jabber:client'><query xmlns='jabber:iq:roster'/></iq>",
 
234
      0
 
235
    },
 
236
    { // connection/auth goes ok.
 
237
      "<stream:stream from='jabber.cc' id='6kpid3u736sqjwd65n25wm57mzz10wz7hopvsj2w' version='1.0' "
 
238
      "xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"
 
239
      "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>"
 
240
      "<mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
 
241
      "<mechanism>PLAIN</mechanism>"
 
242
      "<mechanism>DIGEST-MD5</mechanism>"
 
243
      "</mechanisms>"
 
244
      "</stream:features>",
 
245
      "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
 
246
      "bm9uY2U9ImhvS1I2VkZDSGFibUVYY01weFhlL0QrcVZjWEdyMUdFNzQ0MVFzM2MxY2M9IixyZWFsbT0iamFiYmV"
 
247
      "yLmNjIixxb3A9ImF1dGgsYXV0aC1pbnQsYXV0aC1jb25mIixjaXBoZXI9InJjNC00MCxyYzQtNTYscmM0LGRlcyw"
 
248
      "zZGVzIixtYXhidWY9MTAyNCxjaGFyc2V0PXV0Zi04LGFsZ29yaXRobT1tZDUtc2Vzcw=="
 
249
      "</challenge>",
 
250
      "<challenge xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>"
 
251
      "cnNwYXV0aD1mNGFhZTM0YWY0N2I1MmM0MmQ2NWQzY2NjMGNjN2YyNA=="
 
252
      "</challenge>",
 
253
      "<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>",
 
254
      "<stream:stream from='jabber.cc' id='1o4p1gz2h0m1wvqutohs24d439nbv9zxx4nykm11' version='1.0' "
 
255
      "xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>"
 
256
      "<stream:features xmlns:stream='http://etherx.jabber.org/streams'>"
 
257
      "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
 
258
      "<session xmlns='urn:ietf:params:xml:ns:xmpp-session'/>"
 
259
      "<sm xmlns='urn:xmpp:sm:3'/>"
 
260
      "</stream:features>",
 
261
      "<resumed xmlns='urn:xmpp:sm:3' h='3' previd='some-long-id' />",
 
262
      "<r xmlns='urn:xmpp:sm:3'/>",
 
263
      "<message from='someone' to='someother'><body>something</body></message>",
 
264
      0
 
265
    },
190
266
  };
191
267
 
192
268
int main( int /*argc*/, char** /*argv*/ )
206
282
  c->setConnectionImpl( conn );
207
283
  c->setTls( TLSDisabled );
208
284
  c->setCompression( false );
 
285
  //   c->setLog( true );
209
286
  c->connect();
 
287
//   printf( "connected: %d, disconnected: %d, reason: %d\n", c->connected(), c->disconnected(), c->disconnectReason() );
210
288
  if( c->connected() != 1 || c->disconnected() != 1 || c->disconnectReason() != ConnUserDisconnected )
211
289
  {
212
290
    ++fail;
213
 
    printf( "test '%s' failed\n", name.c_str() );
 
291
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
214
292
  }
215
293
  delete c;
216
294
  c = 0;
223
301
  c->setConnectionImpl( conn );
224
302
  c->setTls( TLSDisabled );
225
303
  c->setCompression( false );
 
304
  //   c->setLog( true );
226
305
  c->connect();
227
306
  if( c->connected() != 0 || c->disconnected() != 1 || c->disconnectReason() != ConnAuthenticationFailed )
228
307
  {
229
308
    ++fail;
230
 
    printf( "test '%s' failed\n", name.c_str() );
 
309
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
231
310
  }
232
311
  delete c;
233
312
  c = 0;
240
319
  c->setConnectionImpl( conn );
241
320
  c->setTls( TLSDisabled );
242
321
  c->setCompression( false );
 
322
  //   c->setLog( true );
243
323
  c->connect();
244
324
  if( c->connected() != 0 || c->disconnected() != 1 || c->disconnectReason() != ConnIoError )
245
325
  {
246
326
    ++fail;
247
 
    printf( "test '%s' failed\n", name.c_str() );
 
327
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
248
328
  }
249
329
  delete c;
250
330
  c = 0;
257
337
  c->setConnectionImpl( conn );
258
338
  c->setTls( TLSDisabled );
259
339
  c->setCompression( false );
 
340
  //   c->setLog( true );
260
341
  c->connect();
261
342
  if( c->connected() != 0 || c->disconnected() != 1 || c->disconnectReason() != ConnStreamError
262
343
      || c->streamErrorReason() != StreamErrorXmlNotWellFormed )
263
344
  {
264
345
    ++fail;
265
 
    printf( "test '%s' failed: %d, %d\n", name.c_str(), c->disconnectReason(), c->streamErrorReason() );
 
346
    fprintf( stderr, "test '%s' failed: %d, %d\n", name.c_str(), c->disconnectReason(), c->streamErrorReason() );
266
347
  }
267
348
  delete c;
268
349
  c = 0;
274
355
  c->setConnectionImpl( conn );
275
356
  c->setTls( TLSDisabled );
276
357
  c->setCompression( false );
 
358
  //   c->setLog( true );
277
359
  for( int i = 1; i <= 100; ++i )
278
360
  {
279
361
    c->connect();
280
362
    if( c->connected() != 0 || c->disconnected() != i || c->disconnectReason() != ConnIoError )
281
363
    {
282
364
      ++fail;
283
 
      printf( "test '%s' failed, %d, %d, %d\n", name.c_str(),
 
365
      fprintf( stderr, "test '%s' failed, %d, %d, %d\n", name.c_str(),
284
366
              c->connected(), c->disconnected(),
285
367
              c->disconnectReason() );
286
368
      break;
296
378
  c->setConnectionImpl( conn );
297
379
  c->setTls( TLSDisabled );
298
380
  c->setCompression( false );
 
381
  //   c->setLog( true );
299
382
  for( int i = 1; i <= 100; ++i )
300
383
  {
301
384
    c->connect();
302
385
    if( c->connected() != i || c->disconnected() != i || c->disconnectReason() != ConnUserDisconnected )
303
386
    {
304
387
      ++fail;
305
 
      printf( "test '%s' failed, %d, %d, %d\n", name.c_str(),
 
388
      fprintf( stderr, "test '%s' failed, %d, %d, %d\n", name.c_str(),
306
389
              c->connected(), c->disconnected(),
307
390
              c->disconnectReason() );
308
391
      break;
309
392
    }
310
 
    c->m_idCount = 0; // FIXME re-using this variable in subsequent connection attempts
311
 
                      // causes inconsistencies with the hard-coded replies.
312
 
  }
313
 
  delete c;
314
 
  c = 0;
315
 
 
316
 
 
317
 
 
318
 
 
 
393
  }
 
394
  delete c;
 
395
  c = 0;
 
396
 
 
397
 
 
398
 
 
399
  // -------
 
400
  name = "stream management test 1: basic ack";
 
401
  c = new ClientTest( j, "b" );
 
402
  conn = new ConnectionImpl( c, 4 );
 
403
  c->setConnectionImpl( conn );
 
404
  c->setTls( TLSDisabled );
 
405
  c->setCompression( false );
 
406
  c->setStreamManagement( true, false );
 
407
//   c->setLog( true );
 
408
  c->connect();
 
409
  //   printf( "connected: %d, disconnected: %d, reason: %d\n", c->connected(), c->disconnected(), c->disconnectReason() );
 
410
  if( c->connected() != 1 || c->disconnected() != 1 || c->disconnectReason() != ConnUserDisconnected )
 
411
  {
 
412
    ++fail;
 
413
    fprintf( stderr, "test '%s' failed, %d, %d, %d\n", name.c_str(),
 
414
            c->connected(), c->disconnected(),
 
415
            c->disconnectReason() );
 
416
  }
 
417
  delete c;
 
418
  c = 0;
 
419
  
 
420
  
 
421
  j.setServer( "d" );
 
422
  // -------
 
423
  name = "stream management test 2: resume";
 
424
  c = new ClientTest( j, "b" );
 
425
  conn = new ConnectionImpl( c, 4 );
 
426
  c->setConnectionImpl( conn );
 
427
  c->setTls( TLSDisabled );
 
428
  c->setCompression( false );
 
429
  c->setStreamManagement( true, true );
 
430
//   c->setLog( true );
 
431
  c->connect();
 
432
  //   printf( "connected: %d, disconnected: %d, reason: %d\n", c->connected(), c->disconnected(), c->disconnectReason() );
 
433
  if( c->connected() != 1 || c->disconnected() != 1 || c->disconnectReason() != ConnIoError )
 
434
  {
 
435
    ++fail;
 
436
    fprintf( stderr, "test '%s' part 1 failed, %d, %d, %d\n", name.c_str(),
 
437
            c->connected(), c->disconnected(),
 
438
            c->disconnectReason() );
 
439
  }
 
440
  else
 
441
  {
 
442
    conn = new ConnectionImpl( c, 5 );
 
443
    c->setConnectionImpl( conn );
 
444
    c->connect();
 
445
    //   printf( "connected: %d, disconnected: %d, reason: %d\n", c->connected(), c->disconnected(), c->disconnectReason() );
 
446
    if( c->connected() != 2 || c->disconnected() != 2 || c->disconnectReason() != ConnIoError )
 
447
    {
 
448
      ++fail;
 
449
      fprintf( stderr, "test '%s' part 2 failed, %d, %d, %d\n", name.c_str(),
 
450
              c->connected(), c->disconnected(),
 
451
              c->disconnectReason() );
 
452
    }
 
453
  }
 
454
  delete c;
 
455
  c = 0;
 
456
  
 
457
  
319
458
 
320
459
 
321
460
 
337
476
  }
338
477
  else
339
478
  {
340
 
    printf( "Client: %d test(s) failed\n", fail );
 
479
    fprintf( stderr, "Client: %d test(s) failed\n", fail );
341
480
    return 1;
342
481
  }
343
482