~ubuntu-branches/ubuntu/lucid/ktorrent/lucid

« back to all changes in this revision

Viewing changes to utests/dhtmsgparsetest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Richard Birnie
  • Date: 2008-06-03 20:32:46 UTC
  • mfrom: (1.1.20 upstream)
  • Revision ID: james.westby@ubuntu.com-20080603203246-dfyemn010uhsf433
Tags: 3.1~rc1+dfsg.1-1ubuntu1
* New upstream development release      
 - Dropped 01_support_external_libbtcore.diffm,
   97_fix_target_link_libraries.diff,
   99_libbtcore_scramble_soname.diff

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2005 by Joris Guisson                                   *
3
 
 *   joris.guisson@gmail.com                                               *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.             *
19
 
 ***************************************************************************/
20
 
#include <torrent/bdecoder.h>
21
 
#include <torrent/bnode.h>
22
 
#include <kademlia/rpcmsg.h>
23
 
#include <util/log.h>
24
 
#include <torrent/globals.h>
25
 
#include "dhtmsgparsetest.h"
26
 
 
27
 
using namespace dht;
28
 
using namespace bt;
29
 
 
30
 
namespace utest
31
 
{
32
 
        
33
 
 
34
 
        DHTMsgParseTest::DHTMsgParseTest() : UnitTest("DHTMsgParseTest")
35
 
        {}
36
 
 
37
 
 
38
 
        DHTMsgParseTest::~DHTMsgParseTest()
39
 
        {}
40
 
 
41
 
        bool DHTMsgParseTest::doTest(const QString & data,int method)
42
 
        {
43
 
                QByteArray bdata(data.length());
44
 
                
45
 
                for (int i = 0;i < data.length();i++)
46
 
                {
47
 
                        bdata[i] = data[i];
48
 
                }
49
 
                
50
 
                BDecoder bdec(bdata,false);
51
 
                
52
 
                BNode* n = bdec.decode();
53
 
                if (n->getType() != BNode::DICT)
54
 
                {
55
 
                        delete n;
56
 
                        Out() << "Packet does not contain a dictionary" << endl;
57
 
                        return false;
58
 
                }
59
 
                
60
 
                MsgBase* msg = MakeRPCMsgTest((BDictNode*)n,(dht::Method)method);
61
 
                if (!msg)
62
 
                {
63
 
                        delete n;
64
 
                        Out() << "Error parsing message : " << endl;
65
 
                        return false;
66
 
                }
67
 
                delete msg;
68
 
                delete n;
69
 
                return true;
70
 
        }
71
 
        
72
 
        
73
 
 
74
 
        bool DHTMsgParseTest::doTest()
75
 
        {       
76
 
 
77
 
                QString test_str[] = {
78
 
                        "d1:rd2:id20:####################5:token20:####################6:valuesl6:######6:######6:######6:######6:######6:######6:######6:######ee1:t1:#1:y1:re",
79
 
                        
80
 
                        "d1:ad2:id20:####################9:info_hash20:####################e1:q9:get_peers1:t1:#1:y1:qe",
81
 
                        
82
 
                        "d1:rd2:id20:####################5:nodes208:################################################################################################################################################################################################################5:token20:####################e1:t1:#1:y1:re",
83
 
                        
84
 
                        QString::null
85
 
                };
86
 
                
87
 
                int types[] = {dht::GET_PEERS,dht::NONE,dht::GET_PEERS};
88
 
                
89
 
                int i = 0; 
90
 
                while (!test_str[i].isNull())
91
 
                {
92
 
                        // read and decode the packet
93
 
                        if (!doTest(test_str[i],types[i]))
94
 
                        {
95
 
                                Out() << "Testing packet " << i <<  " : Failed" << endl;
96
 
                                return false;
97
 
                        }
98
 
                        else
99
 
                        {
100
 
                                Out() << "Testing packet " << i <<  " : OK" << endl;
101
 
                        }
102
 
                        i++;
103
 
                }
104
 
                
105
 
                return true;
106
 
        }
107
 
 
108
 
}