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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <QtTest/QtTest>
#include <q3urloperator.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qfile.h>
#include <qfileinfo.h>
#include <qurlinfo.h>
#include <q3network.h>
#include <q3networkprotocol.h>
#include <qtimer.h>
#include <q3ptrlist.h>
#include <q3valuelist.h>
#include <qlist.h>
#include "../network-settings.h"
//TESTED_CLASS=
//TESTED_FILES=
QT_FORWARD_DECLARE_CLASS(Q3NetworkOperation)
class tst_Q3UrlOperator : public QObject
{
Q_OBJECT
public:
tst_Q3UrlOperator();
virtual ~tst_Q3UrlOperator();
public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
private slots:
void copy_data();
void copy();
void put_data();
void put();
void rename_data();
void rename();
void stop_data();
void stop();
void listChildren_data();
void listChildren();
protected slots:
void slotFinished_init( Q3NetworkOperation* );
void slotFinished_cleanup( Q3NetworkOperation* );
void slotFinished_copy( Q3NetworkOperation* );
void slotFinished_put( Q3NetworkOperation* );
void slotData_put( const QByteArray&, Q3NetworkOperation* );
void slotFinished_rename( Q3NetworkOperation* );
void slotItemChanged_rename( Q3NetworkOperation* );
void slotFinished_stop( Q3NetworkOperation* );
void slotDataTransferProgress_stop( int, int, Q3NetworkOperation* );
void slotFinished_listChildren( Q3NetworkOperation* );
void slotNewChildren_listChildren( const Q3ValueList<QUrlInfo> &, Q3NetworkOperation * );
void stopOperation();
private:
Q3UrlOperator *urlOp;
QString ftpQtestUpload;
bool doStop;
bool finished;
Q3PtrList<Q3NetworkOperation> pendingOperations;
Q3NetworkProtocol::State finishedState;
int finishedErrorCode;
QString finishedProtocolDetail;
Q3ValueList<QUrlInfo> listChildrenInfo;
QByteArray baData;
uint timeout_count;
};
//#define DUMP_SIGNALS
tst_Q3UrlOperator::tst_Q3UrlOperator()
{
q3InitNetworkProtocols();
timeout_count = 0;
}
tst_Q3UrlOperator::~tst_Q3UrlOperator()
{
}
void tst_Q3UrlOperator::initTestCase()
{
// create files for checking permissions
QFile textFile("listData/readOnly");
textFile.open(QIODevice::WriteOnly);
textFile.close();
textFile.setPermissions(QFile::ReadOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther);
QFile exe("listData/executable.exe");
exe.open(QIODevice::WriteOnly);
exe.close();
exe.setPermissions(QFile::ReadOwner | QFile::ReadUser | QFile::ReadGroup | QFile::ReadOther |
QFile::ExeOwner | QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther);
// prepare: make sure that there is a unique directory for FTP upload
// testing (to avoid parallel executed tests interfere with each other)
ftpQtestUpload = QString("ftp://%1/").arg(QtNetworkSettings::serverLocalName());
QString dir = QString( "qtest/upload/%1" ).arg( (ulong)this );
Q3UrlOperator opMkdir( ftpQtestUpload );
connect( &opMkdir, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_init(Q3NetworkOperation*)) );
if ( opMkdir.mkdir( dir ) != 0 ) {
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
qWarning( "Prepare: Network operation timed out for create directory" );
}
ftpQtestUpload += dir;
}
void tst_Q3UrlOperator::slotFinished_init( Q3NetworkOperation *op )
{
if ( op->state() == Q3NetworkProtocol::StFailed )
qWarning( QString( "Prepare: Can't create directory: network operation failed ('%1'). " ).arg(op->protocolDetail()) );
QTestEventLoop::instance().exitLoop();
}
void tst_Q3UrlOperator::cleanupTestCase()
{
// remove the files created before
QFile textFile("listData/readOnly");
textFile.setPermissions(textFile.permissions() | QFile::WriteOwner);
if (! textFile.remove())
qWarning() << "could not remove file:" << textFile.error();
QFile exe("listData/executable.exe");
exe.setPermissions(textFile.permissions() | QFile::WriteOwner);
if (! exe.remove())
qWarning() << "could not remove file:" << exe.error();
// cleanup: delete the unique directory for FTP upload testing
QStringList tmp = QStringList::split( '/', ftpQtestUpload, TRUE );
QString dir = tmp.last();
tmp.pop_back();
QString url = tmp.join( "/" );
Q3UrlOperator opRemove( url );
connect( &opRemove, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_cleanup(Q3NetworkOperation*)) );
if ( opRemove.remove( dir ) != 0 ) {
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
qWarning( "Cleanup: Network operation timed out for removing directory" );
}
}
void tst_Q3UrlOperator::slotFinished_cleanup( Q3NetworkOperation *op )
{
if ( op->state() == Q3NetworkProtocol::StFailed )
qWarning( QString( "Cleanup: Can't remove directory: network operation failed ('%1'). " ).arg(op->protocolDetail()) );
QTestEventLoop::instance().exitLoop();
}
void tst_Q3UrlOperator::init()
{
doStop = FALSE;
finished = FALSE;
pendingOperations.clear();
}
void tst_Q3UrlOperator::cleanup()
{
}
void tst_Q3UrlOperator::copy_data()
{
const QString ftpQtest( QString("ftp://%1/qtest").arg(QtNetworkSettings::serverLocalName()) );
const QString httpQtest( QString("http://%1/qtest").arg(QtNetworkSettings::serverLocalName()) );
// argument for the constructor
QTest::addColumn<QString>("url");
// arguments for the copy()
QTest::addColumn<QString>("from");
QTest::addColumn<QString>("to");
QTest::addColumn<bool>("move");
QTest::addColumn<bool>("toPath");
// FTP data
// QTest::newRow( "ftp00" ) << QString() << QString("%1/rfc3252.txt").arg(ftpQtest) << QString(".") << (bool)FALSE << (bool)TRUE;
QTest::newRow( "ftp01" ) << QString() << QString("%1/rfc3252").arg(ftpQtest) << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "ftp02" ) << QString("%1/").arg(ftpQtest) << QString("rfc3252.txt") << QString(".") << (bool)FALSE << (bool)TRUE;
QTest::newRow( "ftp03" ) << QString("%1/").arg(ftpQtest) << QString("rfc3252") << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "ftp04" ) << QString("%1").arg(ftpQtest) << QString("rfc3252.txt") << QString(".") << (bool)FALSE << (bool)TRUE;
QTest::newRow( "ftp05" ) << QString("%1").arg(ftpQtest) << QString("rfc3252") << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
// the ftp.faqs.org host is down that often, that it does not make sense to
// use it for automated tests
// QTest::newRow( "ftp06" ) << QString() << QString("ftp://ftp.faqs.org/rfc/rfc3252.txt") << QString(".") << (bool)FALSE << (bool)TRUE;
// HTTP data
QTest::newRow( "http00" ) << QString() << QString("%1/rfc3252.txt").arg(httpQtest) << QString(".") << (bool)FALSE << (bool)TRUE;
QTest::newRow( "http01" ) << QString() << QString("%1/rfc3252").arg(httpQtest) << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http02" ) << QString() << QString("%1/").arg(httpQtest) << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http03" ) << QString("%1/").arg(httpQtest) << QString("rfc3252.txt") << QString(".") << (bool)FALSE << (bool)TRUE;
QTest::newRow( "http04" ) << QString("%1/").arg(httpQtest) << QString("rfc3252") << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http05" ) << QString("%1/").arg(httpQtest) << QString("") << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http06" ) << QString("%1/").arg(httpQtest) << QString() << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http07" ) << QString("%1").arg(httpQtest) << QString("rfc3252.txt") << QString(".") << (bool)FALSE << (bool)TRUE;
QTest::newRow( "http08" ) << QString("%1").arg(httpQtest) << QString("rfc3252") << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http09" ) << QString("%1").arg(httpQtest) << QString("") << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
QTest::newRow( "http10" ) << QString("%1").arg(httpQtest) << QString() << QString("rfc3252.txt") << (bool)FALSE << (bool)FALSE;
// Unstable host:
// QTest::newRow( "http11" ) << QString() << QString("http://www.ietf.org/rfc/rfc3252.txt") << QString(".") << (bool)FALSE << (bool)TRUE;
}
void tst_Q3UrlOperator::copy()
{
if (timeout_count >= 3) {
QFAIL( "Too many timeout's. Aborting test prematurely." );
}
QFETCH( QString, url );
QFETCH( QString, from );
QFETCH( QString, to );
QFETCH( bool, move );
QFETCH( bool, toPath );
if ( url.isNull() ) {
urlOp = new Q3UrlOperator();
} else {
urlOp = new Q3UrlOperator( url );
}
pendingOperations = urlOp->copy( from, to, move, toPath );
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_copy(Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 60 );
delete urlOp;
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
// test the results of the operation
if ( finished ) {
QVERIFY( finishedState==Q3NetworkProtocol::StDone || finishedState==Q3NetworkProtocol::StFailed );
if ( finishedState == Q3NetworkProtocol::StDone ) {
QFile file( "rfc3252.txt" );
if ( !file.open( QIODevice::ReadOnly ) )
QFAIL( "Could not open downloaded file" );
// ### this should move in the tesdata
QFile fileRes( "copy.res/rfc3252.txt" );
if ( !fileRes.open( QIODevice::ReadOnly ) )
QFAIL( "Could not open result file" );
QCOMPARE(file.readAll(), fileRes.readAll());
// delete the downloaded file
if ( !file.remove() )
QFAIL( QString("Cleanup failed: could not remove the downloaded file '%1'").arg(file.name()) );
timeout_count = 0;
} else {
QFAIL( QString( "Network operation failed ('%1'). "
"This could be due to a temporary network failure. "
"If this test continues to fail, take a closer look").arg(finishedProtocolDetail) );
timeout_count++;
}
}
}
void tst_Q3UrlOperator::slotFinished_copy( Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "finished( %p )", op );
#endif
QVERIFY( op != 0 );
pendingOperations.remove( op );
// We are finished if either of this happens:
// - both, the get and the put of the copy are finished
// - we are in an error case (for the case that the get fails, no
// finished() signal for the put is emitted since it is never executed)
if ( pendingOperations.isEmpty() || op->state()==Q3NetworkProtocol::StFailed ) {
finished = TRUE;
finishedState = op->state();
finishedErrorCode = op->errorCode();
finishedProtocolDetail = op->protocolDetail();
QTestEventLoop::instance().exitLoop();
}
}
void tst_Q3UrlOperator::put_data()
{
const QString httpQtest( QString("http://%1/qtest/cgi-bin").arg(QtNetworkSettings::serverLocalName()) );
const QString httpQtestGet( QString("http://%1/qtest/cgi-bin/retrieve_testfile.cgi").arg(QtNetworkSettings::serverLocalName()) );
QByteArray putData_1( 5 );
putData_1[0] = 'a';
putData_1[1] = 'b';
putData_1[2] = '\n';
putData_1[3] = 'c';
putData_1[4] = 'd';
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("to");
QTest::addColumn<QByteArray>("putData");
QTest::addColumn<QString>("getUrl");
QTest::addColumn<QString>("rmUrl");
QTest::addColumn<QString>("rmFile");
// FTP data
QTest::newRow( "ftp00" )
<< ftpQtestUpload << "put_ftp00" << putData_1
<< QString("%1/put_ftp00").arg(ftpQtestUpload)
<< ftpQtestUpload << "put_ftp00";
QTest::newRow( "ftp01" )
<< QString() << QString("%1/put_ftp01").arg(ftpQtestUpload) << putData_1
<< QString("%1/put_ftp01").arg(ftpQtestUpload)
<< ftpQtestUpload << "put_ftp01";
QTest::newRow( "ftp02" )
<< ftpQtestUpload << "put_ftp02" << QByteArray(0)
<< QString("%1/put_ftp02").arg(ftpQtestUpload)
<< ftpQtestUpload << "put_ftp02";
QTest::newRow( "ftp03" )
<< QString() << QString("%1/put_ftp03").arg(ftpQtestUpload) << QByteArray(0)
<< QString("%1/put_ftp03").arg(ftpQtestUpload)
<< ftpQtestUpload << "put_ftp03";
// HTTP data
QTest::newRow( "http00" )
<< httpQtest << "store_testfile.cgi" << putData_1
<< httpQtestGet
<< httpQtest << QString();
QTest::newRow( "http01" )
<< QString() << QString("%1/store_testfile.cgi").arg(httpQtest) << putData_1
<< httpQtestGet
<< httpQtest << QString();
QTest::newRow( "http02" )
<< httpQtest << "store_testfile.cgi" << QByteArray(0)
<< httpQtestGet
<< httpQtest << QString();
QTest::newRow( "http03" ) << QString() << QString("%1/store_testfile.cgi").arg(httpQtest) << QByteArray(0)
<< httpQtestGet
<< httpQtest << QString();
}
void tst_Q3UrlOperator::put()
{
{
// do the put
QFETCH( QString, url );
QFETCH( QString, to );
QFETCH( QByteArray, putData );
if ( url.isNull() ) {
urlOp = new Q3UrlOperator();
} else {
urlOp = new Q3UrlOperator( url );
}
pendingOperations.append( urlOp->put( putData, to) );
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_put(Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 30 );
delete urlOp;
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
}
{
// In order to test that the put was really successful, we have to get the
// file from the server.
QFETCH( QString, getUrl );
baData.resize( 0 );
Q3UrlOperator opGet( getUrl );
opGet.get();
connect( &opGet, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_put(Q3NetworkOperation*)) );
connect( &opGet, SIGNAL(data(const QByteArray&,Q3NetworkOperation*)), SLOT(slotData_put(const QByteArray&,Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QTEST( baData, "putData" );
}
{
// cleanup: delete file (if possible)
QFETCH( QString, rmUrl );
QFETCH( QString, rmFile );
Q3UrlOperator opRemove( rmUrl );
connect( &opRemove, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_put(Q3NetworkOperation*)) );
if ( opRemove.remove( rmFile ) != 0 ) {
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QVERIFY( finishedState==Q3NetworkProtocol::StDone || finishedState==Q3NetworkProtocol::StFailed );
if ( finishedState != Q3NetworkProtocol::StDone ) {
QFAIL( QString( "Can't remove file: network operation failed ('%1'). " ).arg(finishedProtocolDetail) );
}
}
}
}
void tst_Q3UrlOperator::slotFinished_put( Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "finished( %p )", op );
#endif
QVERIFY( op != 0 );
pendingOperations.remove( op );
QVERIFY( pendingOperations.isEmpty() );
finished = TRUE;
finishedState = op->state();
finishedErrorCode = op->errorCode();
finishedProtocolDetail = op->protocolDetail();
if (op->state() != Q3NetworkProtocol::StFailed)
QTestEventLoop::instance().exitLoop();
}
void tst_Q3UrlOperator::slotData_put( const QByteArray& ba, Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "data( %d, %p )", ba.size(), op );
#endif
QVERIFY( op != 0 );
int oldSize = baData.size();
baData.resize( ba.size() + oldSize );
memcpy( baData.data()+oldSize, ba.data(), ba.size() );
}
void tst_Q3UrlOperator::rename_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("oldname");
QTest::addColumn<QString>("newname");
QTest::newRow( "local00" ) << QString(".") << QString("foo") << QString("bar");
}
void tst_Q3UrlOperator::rename()
{
{
// create direcotry first
QFETCH( QString, url );
QFETCH( QString, oldname );
if ( url.isNull() ) {
urlOp = new Q3UrlOperator();
} else {
urlOp = new Q3UrlOperator( url );
}
pendingOperations.append( urlOp->mkdir( oldname ) );
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_rename(Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 30 );
delete urlOp;
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QVERIFY( finishedState == Q3NetworkProtocol::StDone );
}
{
// rename directory
QFETCH( QString, url );
QFETCH( QString, oldname );
QFETCH( QString, newname );
if ( url.isNull() ) {
urlOp = new Q3UrlOperator();
} else {
urlOp = new Q3UrlOperator( url );
}
pendingOperations.append( urlOp->rename( oldname, newname ) );
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_rename(Q3NetworkOperation*)) );
connect( urlOp, SIGNAL(itemChanged(Q3NetworkOperation*)), SLOT(slotItemChanged_rename(Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 30 );
delete urlOp;
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QVERIFY( finishedState == Q3NetworkProtocol::StDone );
}
{
// delete direcotry
QFETCH( QString, url );
QFETCH( QString, newname );
if ( url.isNull() ) {
urlOp = new Q3UrlOperator();
} else {
urlOp = new Q3UrlOperator( url );
}
pendingOperations.append( urlOp->remove( newname ) );
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_rename(Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 30 );
delete urlOp;
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QVERIFY( finishedState == Q3NetworkProtocol::StDone );
}
}
void tst_Q3UrlOperator::slotFinished_rename( Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "finished( %p )", op );
#endif
QVERIFY( op != 0 );
pendingOperations.remove( op );
QVERIFY( pendingOperations.isEmpty() );
finished = TRUE;
finishedState = op->state();
finishedErrorCode = op->errorCode();
finishedProtocolDetail = op->protocolDetail();
QTestEventLoop::instance().exitLoop();
}
void tst_Q3UrlOperator::slotItemChanged_rename( Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "itemChanged( %p )", op );
#endif
QVERIFY( op != 0 );
// ### what to do here?
}
void tst_Q3UrlOperator::stop_data()
{
QTest::addColumn<QString>("from");
QTest::addColumn<QString>("to");
QTest::addColumn<QString>("rmUrl");
QTest::addColumn<QString>("rmFile");
// FTP data
QTest::newRow( "ftp01" )
<< "stop/bigfile" << ftpQtestUpload
<< ftpQtestUpload << "bigfile";
}
void tst_Q3UrlOperator::stop()
{
doStop = TRUE;
urlOp = new Q3UrlOperator();
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)),
SLOT(slotFinished_stop(Q3NetworkOperation*)) );
connect( urlOp, SIGNAL(dataTransferProgress(int,int,Q3NetworkOperation*)),
SLOT(slotDataTransferProgress_stop(int,int,Q3NetworkOperation*)) );
{
// do the upload
QFETCH( QString, from );
QFETCH( QString, to );
pendingOperations = urlOp->copy( from, to );
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
}
doStop = FALSE;
{
// do the upload again (this time without stop) -- this used to fail
// until change 71380
QFETCH( QString, from );
QFETCH( QString, to );
pendingOperations = urlOp->copy( from, to );
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
}
delete urlOp;
{
// cleanup: delete file (if possible)
QFETCH( QString, rmUrl );
QFETCH( QString, rmFile );
Q3UrlOperator opRemove( rmUrl );
connect( &opRemove, SIGNAL(finished(Q3NetworkOperation*)), SLOT(slotFinished_stop(Q3NetworkOperation*)) );
if ( opRemove.remove( rmFile ) != 0 ) {
QTestEventLoop::instance().enterLoop( 30 );
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QVERIFY( finishedState==Q3NetworkProtocol::StDone || finishedState==Q3NetworkProtocol::StFailed );
if ( finishedState != Q3NetworkProtocol::StDone )
QFAIL( QString( "Can't remove file: network operation failed ('%1'). " ).arg(finishedProtocolDetail) );
}
}
}
void tst_Q3UrlOperator::slotFinished_stop( Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "finished( %p )", op );
#endif
QVERIFY( op != 0 );
pendingOperations.remove( op );
// We are finished if either of this happens:
// - both, the get and the put of the copy are finished
// - we are in an error case (for the case that the get fails, no
// finished() signal for the put is emitted since it is never executed)
if ( pendingOperations.isEmpty() || op->state()==Q3NetworkProtocol::StFailed ) {
finished = TRUE;
finishedState = op->state();
finishedErrorCode = op->errorCode();
finishedProtocolDetail = op->protocolDetail();
QTestEventLoop::instance().exitLoop();
}
}
void tst_Q3UrlOperator::slotDataTransferProgress_stop( int done, int total, Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "dataTransferProgress( %d, %d, %p )", done, total, op );
#endif
QVERIFY( op != 0 );
QVERIFY( done <= total );
if ( QTest::currentTestFunction() == QLatin1String("stop") ) {
// ### it would be nice if we could specify in our testdata when to do
// the stop
if ( doStop && pendingOperations.count()==1 ) {
if ( done > 0 && done >= total/100000 ) {
// it is not safe to call stop() in a slot connected to the
// dataTransferProgress() signal (and it is not trivial to make
// it work)
QTimer::singleShot( 0, this, SLOT(stopOperation()) );
doStop = FALSE;
}
}
}
}
void tst_Q3UrlOperator::stopOperation()
{
if ( urlOp )
urlOp->stop();
}
void tst_Q3UrlOperator::listChildren_data()
{
QTest::addColumn<QString>("url");
QTest::addColumn<QString>("nameFilter");
QTest::addColumn<bool>("readable");
QTest::addColumn<bool>("writable");
QTest::addColumn<bool>("executable");
QTest::addColumn<int>("permissions");
QTest::newRow( "localfs00" )
<< QString("listData") << QString("readOnly")
<< (bool)TRUE << (bool)FALSE << (bool)FALSE << 0444;
QTest::newRow( "localfs01" )
<< QString("listData") << QString("executable.exe")
<< (bool)TRUE << (bool)FALSE << (bool)TRUE << 0555;
int permissions = 0755;
#ifdef Q_OS_WIN
permissions = 0777;
#endif
QTest::newRow( "localfs02" )
<< QString("listData") << QString("readWriteExec.exe")
<< (bool)TRUE << (bool)TRUE << (bool)TRUE << permissions;
}
void tst_Q3UrlOperator::listChildren()
{
QFETCH( QString, url );
QFETCH( QString, nameFilter );
urlOp = new Q3UrlOperator( "listData" );
urlOp->setNameFilter( nameFilter );
pendingOperations.append( urlOp->listChildren() );
connect( urlOp, SIGNAL(finished(Q3NetworkOperation*)),
SLOT(slotFinished_listChildren(Q3NetworkOperation*)) );
connect( urlOp, SIGNAL(newChildren(const Q3ValueList<QUrlInfo>&, Q3NetworkOperation*)),
SLOT(slotNewChildren_listChildren(const Q3ValueList<QUrlInfo>&, Q3NetworkOperation*)) );
QTestEventLoop::instance().enterLoop( 30 );
delete urlOp;
if ( QTestEventLoop::instance().timeout() )
QFAIL( "Network operation timed out" );
QVERIFY( finishedState == Q3NetworkProtocol::StDone );
for ( int i=0; i<(int)listChildrenInfo.count(); i++ ) {
if ( listChildrenInfo[i].name() == "." || listChildrenInfo[i].name() == ".." )
continue;
QFETCH( bool, readable );
QFETCH( bool, writable );
QFETCH( bool, executable );
QFETCH( int, permissions );
QCOMPARE( listChildrenInfo[i].isReadable(), readable );
QCOMPARE( listChildrenInfo[i].isWritable(), writable );
QCOMPARE( listChildrenInfo[i].isExecutable(), executable);
QCOMPARE( listChildrenInfo[i].permissions(), permissions );
}
}
void tst_Q3UrlOperator::slotFinished_listChildren( Q3NetworkOperation *op )
{
#if defined( DUMP_SIGNALS )
qDebug( "finished( %p )", op );
#endif
QVERIFY( op != 0 );
pendingOperations.remove( op );
QVERIFY( pendingOperations.isEmpty() );
finished = TRUE;
finishedState = op->state();
finishedErrorCode = op->errorCode();
finishedProtocolDetail = op->protocolDetail();
QTestEventLoop::instance().exitLoop();
}
void tst_Q3UrlOperator::slotNewChildren_listChildren( const Q3ValueList<QUrlInfo> &i, Q3NetworkOperation * )
{
listChildrenInfo = i;
}
QTEST_MAIN(tst_Q3UrlOperator)
#include "tst_q3urloperator.moc"
|