1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
This file is part of libkdepim.
Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "testkldap.h"
#include "testkldap.moc"
#include "ldif.h"
#include "ldapdn.h"
#include "ldapurl.h"
#include "ldapserver.h"
#include "ldapconnection.h"
#include "ldapmodel.h"
#include "ldapoperation.h"
#include "ldapsearch.h"
#include "ber.h"
#include <kdebug.h>
#include <qtest_kde.h>
QTEST_KDEMAIN( KLdapTest, NoGUI )
void KLdapTest::initTestCase()
{
/*
Read in the connection details of an LDAP server to use for testing.
You should copy the file testurl.txt.tmpl to testurl.txt and specify a url in this file.
The specified server should not be a production server in case we break anything here.
You have been warned!
*/
m_search=0;
m_model=0;
QString filename( "testurl.txt" );
QFile file( filename );
if ( file.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
QTextStream stream( &file );
stream >> m_url;
file.close();
}
// else
// QCOMPARE( 0, 1 );
m_search = new LdapSearch;
/* Let's also create an LdapModel object */
m_model = new LdapModel( this );
}
void KLdapTest::testBer()
{
Ber ber1, ber2, ber3, ber4, ber5, ber6, ber7;
Ber bber;
QByteArray flat;
int ainteger;
QByteArray aoctetString1, aoctetString2, aoctetString3;
QList<QByteArray> alist1, alist2;
int binteger;
QByteArray boctetString1, boctetString2, boctetString3;
QList<QByteArray> blist1, blist2;
aoctetString1 = "KDE";
aoctetString2 = "the";
aoctetString3 = "next generation";
alist1.append( aoctetString1 );
alist1.append( aoctetString2 );
alist2.append( aoctetString2 );
alist2.append( aoctetString3 );
alist2.append( aoctetString1 );
ainteger = 23543;
ber1.printf( "i", ainteger );
ber2.printf( "o", &aoctetString1 );
ber3.printf( "O", &aoctetString2 );
ber4.printf( "s", &aoctetString3 );
ber5.printf( "{v}", &alist1 );
ber6.printf( "{V}", &alist2 );
ber7.printf( "oi{v}O", &aoctetString1, ainteger, &alist2, &aoctetString2 );
//test integer:
bber = ber1;
bber.scanf( "i", &binteger );
QCOMPARE( ainteger, binteger );
//test octet strings:
bber = ber2;
bber.scanf( "o", &boctetString1 );
QCOMPARE( aoctetString1, boctetString1 );
bber = ber3;
bber.scanf( "o", &boctetString2 );
QCOMPARE( aoctetString2, boctetString2 );
bber = ber4;
bber.scanf( "o", &boctetString3 );
QCOMPARE( aoctetString3, boctetString3 );
//test sequence of octet strings:
bber = ber5;
bber.scanf( "v", &blist1 );
QCOMPARE( alist1, blist1 );
bber = ber6;
bber.scanf( "v", &blist2 );
QCOMPARE( alist2, blist2 );
//complex tests
boctetString1 = boctetString2 = boctetString3 = QByteArray();
binteger = 0;
blist1.clear();
blist2.clear();
bber = ber7;
bber.scanf( "oivO", &boctetString1, &binteger, &blist2, &boctetString2 );
QCOMPARE( aoctetString1, boctetString1 );
QCOMPARE( aoctetString2, boctetString2 );
QCOMPARE( alist2, blist2 );
QCOMPARE( ainteger, binteger );
}
void KLdapTest::cleanupTestCase()
{
if ( m_search ) {
delete m_search;
}
if ( m_model ) {
delete m_model;
}
}
void KLdapTest::testLdapUrl()
{
// Test LdapUrl using some hardwired values so that we know what to compare to
LdapUrl url;
bool critical;
url.setUrl( "ldap://cn=manager,dc=kde,dc=org:password@localhost:3999/dc=kde,dc=org?cn,mail?sub?(objectClass=*)?x-dir=base" );
url.parseQuery();
QCOMPARE( url.user(), QString::fromLatin1( "cn=manager,dc=kde,dc=org" ) );
QCOMPARE( url.password(), QString::fromLatin1( "password" ) );
QCOMPARE( url.dn(), LdapDN( "dc=kde,dc=org" ) );
QCOMPARE( url.scope(), LdapUrl::Sub );
QCOMPARE( url.attributes().at( 0 ), QString::fromLatin1( "cn" ) );
QCOMPARE( url.attributes().at( 1 ), QString::fromLatin1( "mail" ) );
QCOMPARE( url.filter(), QString::fromLatin1( "(objectClass=*)" ) );
QCOMPARE( url.extension( QString::fromLatin1( "x-dir" ), critical ),
QString::fromLatin1( "base" ) );
}
void KLdapTest::testLdapConnection()
{
// Try to connect using an LdapUrl (read in from testurl.txt).
LdapUrl url;
url.setUrl( m_url );
LdapConnection conn;
conn.setUrl( url );
int ret;
if ( ( ret = conn.connect() ) ) {
kDebug() << "Could not connect to LDAP server. Error was:" << conn.connectionError();
}
QCOMPARE( ret, 0 );
LdapOperation op( conn );
// Now attempt to bind
if ( ( ret = op.bind_s() ) ) {
kDebug() << "Could not bind to server. Error was:" << conn.ldapErrorString();
}
QCOMPARE( ret, 0 );
}
void KLdapTest::testLdapSearch()
{
// Lets try a search using the specified url
LdapUrl url;
url.setUrl( m_url );
url.parseQuery();
connect( m_search, SIGNAL( result( KLDAP::LdapSearch* ) ),
this, SLOT( searchResult( KLDAP::LdapSearch* ) ) );
connect( m_search, SIGNAL( data( KLDAP::LdapSearch*, const KLDAP::LdapObject& ) ),
this, SLOT( searchData( KLDAP::LdapSearch*, const KLDAP::LdapObject& ) ) );
bool success = m_search->search( url );
while ( QCoreApplication::hasPendingEvents() ) {
qApp->processEvents();
}
QCOMPARE( success, true );
kDebug() << "Search found" << m_objects.size() << "matching entries";
}
void KLdapTest::searchResult( KLDAP::LdapSearch *search )
{
kDebug();
int err = search->error();
if ( err ) {
kDebug() << "Search returned the following error:" << search->errorString();
}
QCOMPARE( err, 0 );
}
void KLdapTest::searchData( KLDAP::LdapSearch *search, const KLDAP::LdapObject &obj )
{
Q_UNUSED( search );
//kDebug();
//kDebug() << "Object:";
//kDebug() << obj.toString();
m_objects.append( obj );
}
void KLdapTest::testLdapDN()
{
QString strDN( "uid=Test\\+Person+ou=accounts\\,outgoing,dc=kde,dc=org" );
LdapDN dn( strDN );
QCOMPARE( dn.isValid(), true );
QCOMPARE( dn.rdnString(), QString( "uid=Test\\+Person+ou=accounts\\,outgoing" ) );
}
void KLdapTest::testLdapModel()
{
// Use the user-supplied testing url
LdapUrl url;
url.setUrl( m_url );
// Create a connection to use and bind with it
LdapConnection conn;
conn.setUrl( url );
int ret;
if ( ( ret = conn.connect() ) ) {
kDebug() << "Could not connect to LDAP server. Error was:" << conn.connectionError();
}
QCOMPARE( ret, 0 );
LdapOperation op( conn );
if ( ( ret = op.bind_s() ) ) {
kDebug() << "Could not bind to server. Error was:" << conn.ldapErrorString();
}
QCOMPARE( ret, 0 );
// Let's use this connection with the model
m_model->setConnection( conn );
while ( QCoreApplication::hasPendingEvents() ) {
qApp->processEvents();
}
QModelIndex rootIndex = QModelIndex();
QVariant data = m_model->data( rootIndex, Qt::DisplayRole );
kDebug() << "Root Item Distinguished Name =" << data.toString();
QVERIFY( m_model->hasChildren( rootIndex ) == true );
QVERIFY( m_model->canFetchMore( rootIndex ) == false );
}
/*
void KLdapTest::testKLdap()
{
LdapUrl url;
bool critical;
url.setUrl("ldap://cn=manager,dc=kde,dc=org:password@localhost:3999/dc=kde,dc=org?cn,mail?sub?(objectClass=*)?x-dir=base");
url.parseQuery();
QCOMPARE( url.user(), QString::fromLatin1("cn=manager,dc=kde,dc=org") );
QCOMPARE( url.password(), QString::fromLatin1("password") );
QCOMPARE( url.dn(), QString::fromLatin1("dc=kde,dc=org") );
QCOMPARE( url.scope(), LdapUrl::Sub );
QCOMPARE( url.attributes().at(0), QString::fromLatin1("cn") );
QCOMPARE( url.attributes().at(1), QString::fromLatin1("mail") );
QCOMPARE( url.filter(), QString::fromLatin1("(objectClass=*)") );
QCOMPARE( url.extension(QString::fromLatin1("x-dir"), critical), QString::fromLatin1("base") );
url.setDn("ou=People,dc=kde,dc=org");
QCOMPARE( url.dn(), QString::fromLatin1("ou=People,dc=kde,dc=org") );
url.setDn("/ou=People,dc=kde,dc=org");
QCOMPARE( url.dn(), QString::fromLatin1("ou=People,dc=kde,dc=org") );
LdapServer server;
// url.setUrl("ldaps://cn=manager,dc=kde,dc=org:passwor@localhost:3999/dc=kde,dc=org????x-timelimt=5,x-sizelimit=6,x=pagesize=7,binddn=cn=apple,ou=berry");
url.setUrl("ldaps://cn=manager,dc=kde,dc=org:password@localhost:3999/dc=kde,dc=org??base??x-timelimit=5");
url.parseQuery();
server.setUrl( url );
QCOMPARE( url.query(), QString::fromLatin1("??base??x-timelimit=5") );
QCOMPARE( url.url(), server.url().url() );
LdapControl c1;
c1.setControl( QString::fromLatin1("1.2.3.4.5.6"), QByteArray("abcdefg"), true );
//test copy constructor
LdapControl c2(c1);
QCOMPARE( c2.oid(), QString::fromLatin1("1.2.3.4.5.6") );
QCOMPARE( c2.value(), QByteArray("abcdefg") );
QCOMPARE( c2.critical(), true );
//test assignment operator
LdapControl c3;
c3 = c1;
QCOMPARE( c3.oid(), QString::fromLatin1("1.2.3.4.5.6") );
QCOMPARE( c3.value(), QByteArray("abcdefg") );
QCOMPARE( c3.critical(), true );
*/
//test Ber functions
/*
QByteArray left1("bertest"), right1;
int left2 = 0, right2;
int left3 = 1, right3;
int left4 = 2, right4;
int left5 = 3, right5;
int left6 = 1, right6;
QList<QByteArray> left7, right7;
left7.append( "abcdefghij" );
left7.append( "123456789" );
left7.append( "1234\0\0\056789" );
Ber ber;
ber.printf("{seeiib}", &left1, left2, left3, left4, left5, left6 );
// ber.printf("{ioOi{i}}", left3, &left1, &left2, left4, left4 );
Ber ber2 = ber;
unsigned int a;
int b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
a = ber2.skipTag( b );
kDebug() << "next tag:" << a << "size:" << b;
BerElement *_ber, *_ber2;
_ber = ber_alloc_t( LBER_USE_DER );
ber_len_t bl;
ber_printf( _ber, "{i}", 5 );
kDebug() << "native";
_ber2 = ber_dup( _ber );
a = ber_skip_tag( _ber2, &bl );
kDebug() << "next tag:" << a << "size:" << bl;
// ber_dump( _ber, 0 );
// ber2.scanf("{v}", &right5 );
// ber2.scanf("{inoOi{v}}", &right3, &right1, &right2, &right4, &right5 );
// QCOMPARE( left1, right1 );
// QCOMPARE( left2, right2 );
// QCOMPARE( left3, right3 );
// QCOMPARE( left4, right4 );
// QCOMPARE( left5, right5 );
*/
/*
url.setUrl("ldap://localhost/dc=gyurco,dc=localdomain");
url.parseQuery();
server.setUrl( url );
LdapConnection conn( server );
int result = conn.connect();
kDebug() << "connect result" << result << conn.errorString();
LdapOperation op( conn );
int msgid = op.search( "ou=People,dc=gyurco,dc=localdomain", LdapUrl::One, "", QStringList() );
kDebug() << "search msgid" << msgid;
result = op.result( msgid );
kDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString();
while ( result == LdapOperation::RES_SEARCH_ENTRY ) {
kDebug() << op.object().toString();
result = op.result( msgid );
}
kDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString();
msgid = op.del( "ou=People,dc=gyurco,dc=localdomain" );
kDebug() << "search msgid" << msgid;
result = op.result( msgid );
kDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString();
msgid = op.compare( "ou=People,dc=gyurco,dc=localdomain", "objectClass", QByteArray("top") );
kDebug() << "search msgid" << msgid;
result = op.result( msgid );
kDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString();
msgid = op.compare( "ou=People,dc=gyurco,dc=localdomain", "objectClass", QByteArray("inetOrgPerson") );
kDebug() << "search msgid" << msgid;
result = op.result( msgid );
kDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString();
msgid = op.exop( "1.2.3.4.5.6.7.8", QByteArray("inetOrgPerson") );
kDebug() << "search msgid" << msgid;
result = op.result( msgid );
kDebug() << "error code" << conn.ldapErrorCode() << "str:" << conn.ldapErrorString();
*/
/*
}
*/
|