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

« back to all changes in this revision

Viewing changes to src/tests/jinglesessionmanager/jinglesessionmanager_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
#define CLIENTBASE_H__
 
2
#define DISCO_H__
 
3
#define GLOOX_TESTS
 
4
#define JINGLE_TEST
 
5
#define IQ_TEST
 
6
#include "../../iq.h"
 
7
#include "../../iqhandler.h"
 
8
#include "../../jid.h"
 
9
#include "../../stanzaextension.h"
 
10
#include "../../stanzaextensionfactory.h"
 
11
 
 
12
#include <stdio.h>
 
13
#include <locale.h>
 
14
#include <string>
 
15
#include <cstdio> // [s]print[f]
 
16
 
 
17
namespace gloox
 
18
{
 
19
  class Disco
 
20
  {
 
21
  public:
 
22
    Disco() {}
 
23
    void addFeature( const std::string& ) {}
 
24
  };
 
25
 
 
26
  class ClientBase
 
27
  {
 
28
    public:
 
29
      ClientBase() : m_jid( "self" ) {}
 
30
      virtual ~ClientBase() {}
 
31
      const JID& jid() const { return m_jid; }
 
32
      const std::string getID();
 
33
      virtual void send( const IQ& ) = 0;
 
34
      virtual void send( IQ&, IqHandler*, int ) = 0;
 
35
      void removeIqHandler( IqHandler* ih, int exttype );
 
36
      void removeIDHandler( IqHandler* ih );
 
37
      void registerIqHandler( IqHandler* ih, int exttype );
 
38
      void registerStanzaExtension( StanzaExtension* ext );
 
39
      void removeStanzaExtension( int ext );
 
40
      Disco* disco() { return &m_disco; }
 
41
    protected:
 
42
      JID m_jid;
 
43
      Disco m_disco;
 
44
      StanzaExtensionFactory m_sef;
 
45
  };
 
46
  void ClientBase::removeIqHandler( IqHandler*, int ) {}
 
47
  void ClientBase::removeIDHandler( IqHandler* ) {}
 
48
  void ClientBase::registerIqHandler( IqHandler*, int ) {}
 
49
  void ClientBase::registerStanzaExtension( StanzaExtension* se ) { delete se; }
 
50
  void ClientBase::removeStanzaExtension( int ) {}
 
51
  const std::string ClientBase::getID() { return "id"; }
 
52
}
 
53
using namespace gloox;
 
54
 
 
55
#define JINGLESESSION_TEST
 
56
#include "../../jinglesession.h"
 
57
#include "../../jinglesession.cpp"
 
58
#include "../../jinglesessionhandler.h"
 
59
#include "../../jinglesessionmanager.h"
 
60
#include "../../jinglesessionmanager.cpp"
 
61
#include "../../jinglecontent.h"
 
62
#include "../../jingleiceudp.h"
 
63
#include "../../jinglefiletransfer.h"
 
64
class TestInitiator : public ClientBase, public Jingle::SessionHandler
 
65
{
 
66
  public:
 
67
    TestInitiator() : m_sm( this, this ), m_result( false ), m_result2( false )
 
68
    {
 
69
      m_sef.registerExtension( new Jingle::Session::Jingle() );
 
70
      m_sm.registerPlugin( new Jingle::Content() );
 
71
      m_sm.registerPlugin( new Jingle::ICEUDP() );
 
72
      m_sm.registerPlugin( new Jingle::FileTransfer() );
 
73
    }
 
74
    virtual ~TestInitiator() {}
 
75
    void setTest( int test ) { m_test = test; }
 
76
    virtual void send( const IQ&  ) {}
 
77
    virtual void send( IQ& , IqHandler*, int ) {}
 
78
    virtual void send( Tag* tag );
 
79
    virtual void trackID( IqHandler *ih, const std::string& id, int context ) {}
 
80
    bool checkResult() { bool t = m_result; m_result = false; return t; }
 
81
    bool checkResult2() { bool t = m_result2; m_result2 = false; return t; }
 
82
    virtual void handleSessionAction( Jingle::Action action, const Jingle::Session* session, const Jingle::Session::Jingle* jingle );
 
83
    virtual void handleSessionActionError( Jingle::Action action, const Jingle::Session* /*session*/, const Error* /*e*/ ) {}
 
84
    virtual void handleIncomingSession( Jingle::Session* session ) {}
 
85
private:
 
86
    Jingle::SessionManager m_sm;
 
87
    int m_test;
 
88
    bool m_result;
 
89
    bool m_result2;
 
90
};
 
91
 
 
92
 
 
93
 
 
94
 
 
95
void TestInitiator::handleSessionAction( Jingle::Action action, const Jingle::Session* session, const Jingle::Session::Jingle* jingle )
 
96
{
 
97
  m_result = false;
 
98
  switch( m_test )
 
99
  {
 
100
    case 1:
 
101
      if( action != Jingle::InvalidAction || jingle->plugins().size() != 0 )
 
102
      {
 
103
        printf( "action: %d, plugins: %d\n", action, jingle->plugins().size() );
 
104
      }
 
105
      else
 
106
        m_result = true;
 
107
      break;
 
108
    case 2:
 
109
      if( action != Jingle::SessionInitiate || jingle->plugins().size() != 1 || static_cast<const Jingle::Content*>( jingle->plugins().front() )->name() != "stub" )
 
110
      {
 
111
        printf( "action: %d, plugins: %d\n", action, jingle->plugins().size() );
 
112
      }
 
113
      else
 
114
        m_result = true;
 
115
      break;
 
116
    case 3:
 
117
      if( action != Jingle::SessionInitiate || jingle->plugins().size() != 1 || static_cast<const Jingle::Content*>( jingle->plugins().front() )->name() != "iceudp"
 
118
        || static_cast<const Jingle::Content*>( jingle->plugins().front() )->plugins().size() != 1
 
119
        || static_cast<const Jingle::ICEUDP*>( static_cast<const Jingle::Content*>( jingle->plugins().front() )->plugins().front() )->pwd() != "pwd" )
 
120
      {
 
121
        printf( "action: %d, plugins: %d\n", action, jingle->plugins().size() );
 
122
      }
 
123
      else
 
124
        m_result = true;
 
125
      break;
 
126
    case 4:
 
127
      if( action != Jingle::SessionInitiate || jingle->plugins().size() != 1 || static_cast<const Jingle::Content*>( jingle->plugins().front() )->name() != "ft"
 
128
        || static_cast<const Jingle::Content*>( jingle->plugins().front() )->plugins().size() != 2
 
129
        || static_cast<const Jingle::ICEUDP*>( static_cast<const Jingle::Content*>( jingle->plugins().front() )->plugins().front() )->pwd() != "pwd"
 
130
        || static_cast<const Jingle::FileTransfer*>( static_cast<const Jingle::Content*>( jingle->plugins().front() )->plugins().back() )->type() != Jingle::FileTransfer::Offer )
 
131
      {
 
132
        printf( "action: %d, plugins: %d\n", action, jingle->plugins().size() );
 
133
      }
 
134
      else
 
135
        m_result = true;
 
136
      break;
 
137
  }
 
138
 
 
139
}
 
140
 
 
141
void TestInitiator::send( Tag* tag )
 
142
{
 
143
  IQ iq( tag );
 
144
  m_sef.addExtensions( iq, tag );
 
145
  m_sm.handleIq( iq );
 
146
}
 
147
 
 
148
 
 
149
// ------------------------------------------------------------------------------------------------------------
 
150
 
 
151
 
 
152
 
 
153
// ------------------------------------------------------------------------------------------------------------
 
154
 
 
155
 
 
156
int main( int /*argc*/, char** /*argv*/ )
 
157
{
 
158
  int fail = 0;
 
159
  std::string name;
 
160
  TestInitiator ini;
 
161
  Tag* i = new Tag( "iq" ); i->addAttribute( "from", "me@there" ); i->addAttribute( "to", "you@here" ); i->addAttribute( "type", "set" ); i->addAttribute( "id", "someid" );
 
162
  Tag* j = new Tag( i, "jingle", XMLNS, XMLNS_JINGLE );
 
163
  Tag* c = 0;
 
164
 
 
165
  // -------
 
166
  name = "invalid jingle";
 
167
  ini.setTest( 1 );
 
168
  ini.send( i );
 
169
  if( !ini.checkResult() )
 
170
  {
 
171
    ++fail;
 
172
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
 
173
  }
 
174
 
 
175
  // -------
 
176
  name = "jingle with empty content";
 
177
  ini.setTest( 2 );
 
178
  j->addAttribute( "action", "session-initiate" );
 
179
  c = new Tag( j, "content" ); c->addAttribute( "creator", "initor" ); c->addAttribute( "name", "stub" );
 
180
  ini.send( i );
 
181
  if( !ini.checkResult() )
 
182
  {
 
183
    ++fail;
 
184
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
 
185
  }
 
186
 
 
187
  // -------
 
188
  name = "jingle with content and transport";
 
189
  ini.setTest( 3 );
 
190
  c->addAttribute( "name", "iceudp" );
 
191
  Tag* t = new Tag( c, "transport", XMLNS, XMLNS_JINGLE_ICE_UDP ); t->addAttribute( "pwd", "pwd" ); t->addAttribute( "ufrag", "ufrag" );
 
192
  ini.send( i );
 
193
  if( !ini.checkResult() )
 
194
  {
 
195
    ++fail;
 
196
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
 
197
  }
 
198
 
 
199
  // -------
 
200
  name = "jingle with content and transport + description";
 
201
  ini.setTest( 4 );
 
202
  c->addAttribute( "name", "ft" );
 
203
  Tag* d = new Tag( c, "description", XMLNS, XMLNS_JINGLE_FILE_TRANSFER ); new Tag( new Tag( d, "offer" ), "file" );
 
204
  ini.send( i );
 
205
  if( !ini.checkResult() )
 
206
  {
 
207
    ++fail;
 
208
    fprintf( stderr, "test '%s' failed\n", name.c_str() );
 
209
  }
 
210
 
 
211
 
 
212
 
 
213
  delete i;
 
214
 
 
215
 
 
216
  if( fail == 0 )
 
217
  {
 
218
    printf( "Jingle::SessionManager: OK\n" );
 
219
    return 0;
 
220
  }
 
221
  else
 
222
  {
 
223
    fprintf( stderr, "Jingle::SessionManager: %d test(s) failed\n", fail );
 
224
    return 1;
 
225
  }
 
226
 
 
227
}