1
/******************************************************************************
2
* Copyright (C) 2012-2013 by Savoir-Faire Linux *
3
* Author : Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com> *
5
* This library is free software; you can redistribute it and/or *
6
* modify it under the terms of the GNU Lesser General Public *
7
* License as published by the Free Software Foundation; either *
8
* version 2.1 of the License, or (at your option) any later version. *
10
* This library 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 GNU *
13
* Lesser General Public License for more details. *
15
* You should have received a copy of the Lesser GNU General Public License *
16
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
17
*****************************************************************************/
18
#include <QtCore/QString>
19
#include <QtTest/QtTest>
22
#include "../src/lib/configurationmanager_interface_singleton.h"
23
#include "../src/lib/callmanager_interface_singleton.h"
24
#include "../src/lib/instance_interface_singleton.h"
25
#include "../src/lib/typedefs.h"
26
#include "../src/lib/account.h"
27
#include "../src/lib/accountlist.h"
28
#include "../src/lib/dbus/metatypes.h"
30
class AccountTests: public QObject
34
/*void testValidity();
35
void testMonth_data();
39
void testAccountList();
41
void testIP2IPAlias();
44
void testCreateAccount();
45
void testGetNewAccount();
46
void cleanupTestCase();
49
void testAccountAlias_data();
50
void testAccountAlias ();
52
void testAccountType ();
53
void testAccountHostname_data();
54
void testAccountHostname ();
55
void testAccountHostnameInvalid_data ();
56
void testAccountHostnameInvalid ();
57
void testAccountUsername ();
58
void testAccountPassword_data ();
59
void testAccountPassword ();
60
void testAccountMailbox ();
61
void testTlsPassword ();
62
void testTlsCaListFile ();
63
void testTlsCertificateFile ();
64
void testTlsPrivateKeyFile ();
65
void testTlsCiphers ();
66
void testTlsServerName ();
67
void testAccountSipStunServer ();
68
void testPublishedAddress ();
69
void testLocalInterface ();
70
void testRingtonePath_data();
71
void testRingtonePath ();
72
void testTlsMethod ();
73
void testAccountRegistrationExpire ();
74
void testTlsNegotiationTimeoutSec ();
75
void testTlsNegotiationTimeoutMsec ();
76
void testLocalPort_data();
77
void testLocalPort ();
78
void testTlsListenerPort_data();
79
void testTlsListenerPort ();
80
void testPublishedPort_data();
81
void testPublishedPort ();
82
void testAccountEnabled ();
83
void testTlsVerifyServer ();
84
void testTlsVerifyClient ();
85
void testTlsRequireClientCertificate ();
86
void testTlsEnable ();
87
void testAccountDisplaySasOnce ();
88
void testAccountSrtpRtpFallback ();
89
void testAccountZrtpDisplaySas ();
90
void testAccountZrtpNotSuppWarning ();
91
void testAccountZrtpHelloHash ();
92
void testAccountSipStunEnabled ();
93
void testPublishedSameAsLocal ();
94
void testConfigRingtoneEnabled ();
95
void testDisableAllAccounts ();
103
//BEGIN Getting a valid account
104
void AccountTests::testAccountList()
106
AccountList* list = AccountList::getInstance();
107
QCOMPARE( list != nullptr , true);
108
QCOMPARE( list->size() >= 1, true);
111
void AccountTests::testIP2IP() {
112
Account* acc = AccountList::getInstance()->getAccountById("IP2IP");
113
QCOMPARE( acc != nullptr, true);
115
QCOMPARE( acc->getAlias(), QString("IP2IP"));
119
void AccountTests::testIP2IPAlias() {
120
Account* acc = AccountList::getInstance()->getAccountById("IP2IP");
122
QCOMPARE( acc->getAlias(), QString("IP2IP"));
123
QCOMPARE( acc->getAlias() != QString("qwerty"), true);
126
//END Getting a valid account
129
//BEGIN Creating a new account
130
void AccountTests::testCreateAccount()
132
Account* acc = Account::buildNewAccountFromAlias("unit_test_account");
134
id = acc->getAccountId();
135
QCOMPARE( acc != nullptr, true);
138
void AccountTests::testGetNewAccount()
140
Account* acc = AccountList::getInstance()->getAccountById(id);
141
QCOMPARE( acc != nullptr, true);
143
//END creating a new account
145
//BEGIN Testing every account attributes
147
void AccountTests::testAccountAlias_data()
149
QTest::addColumn<QString>("alias");
151
QTest::newRow("valid" ) << QString( "unit_alias" );
152
QTest::newRow("valid_reset") << QString( "unit_alias1");
153
QTest::newRow("numeric" ) << QString( "2314234" );
154
QTest::newRow("non-ascii" ) << QString( "ééèè>>||``" );
157
void AccountTests::testAccountAlias ()/*QString detail*/
159
Account* acc = AccountList::getInstance()->getAccountById(id);
160
//acc->setAccountAlias("unit_alias");
161
//QCOMPARE( acc->getAccountAlias(), QString("unit_alias"));
163
QFETCH(QString, alias);
164
acc->setAccountAlias(alias);
166
QCOMPARE( acc->getAccountAlias(), alias );
170
void AccountTests::testAccountType ()/*QString detail*/
172
Account* acc = AccountList::getInstance()->getAccountById(id);
173
acc->setAccountType("IAX");
175
QCOMPARE( acc->getAccountType(), QString("IAX") );
176
acc->setAccountType("SIP");
178
QCOMPARE( acc->getAccountType(), QString("SIP") );
181
acc->setAccountType("OTH");
182
QCOMPARE( acc->getAccountType() == "OTH", false );
185
void AccountTests::testAccountHostname_data()
187
QTest::addColumn<QString>("hostname");
188
QTest::newRow("valid" ) << QString( "validHostname" );
189
QTest::newRow("validPlusDigit" ) << QString( "validHostname2" );
190
QTest::newRow("ipv4" ) << QString( "192.168.77.12" );
191
QTest::newRow("ipv42" ) << QString( "10.0.250.1" );
194
//This test the various hostnames that should be allowed by the daemon
195
void AccountTests::testAccountHostname ()
197
Account* acc = AccountList::getInstance()->getAccountById(id);
198
QFETCH(QString, hostname);
199
acc->setAccountHostname(hostname);
201
QCOMPARE( acc->getAccountHostname(), hostname );
204
void AccountTests::testAccountHostnameInvalid_data()
206
QTest::addColumn<QString>("hostname");
207
QTest::newRow("invalid_ipv4" ) << QString( "192.256.12.0" );
208
QTest::newRow("invalid_ipv42" ) << QString( "192.168.12.0000" );
209
QTest::newRow("invalid_ipv43" ) << QString( "192.168.12." );
210
QTest::newRow("invalid_ipv44" ) << QString( "192.168.1" );
211
QTest::newRow("invalid_ipv45" ) << QString( ".192.168.1.1" );
212
QTest::newRow("invalid_ipv46" ) << QString( ".192.1E8.1.1" );
213
QTest::newRow("invalid_hostname" ) << QString( ".23423" );
216
//This test hostname that should be rejected by the daemon
217
void AccountTests::testAccountHostnameInvalid ()
219
QFETCH(QString, hostname);
220
Account* acc = AccountList::getInstance()->getAccountById(id);
221
acc->setAccountHostname(hostname);
223
QVERIFY(acc->getAccountHostname() != hostname);
226
void AccountTests::testAccountUsername ()/*QString detail*/
228
Account* acc = AccountList::getInstance()->getAccountById(id);
229
acc->setAccountHostname("1234567879");
231
QString username = acc->getAccountHostname();
232
QCOMPARE(username,QString("1234567879"));
236
void AccountTests::testAccountPassword_data()
238
QTest::addColumn<QString>("password");
239
QTest::newRow( "numeric" ) << QString( "1234567879" );
240
QTest::newRow( "alphanumeric" ) << QString( "asdf1234" );
241
QTest::newRow( "strong" ) << QString( "!\"'''4)(--@" );
244
void AccountTests::testAccountPassword ()/*QString detail*/
246
/*QFETCH(QString, password);
247
Account* acc = AccountList::getInstance()->getAccountById(id);
248
acc->setAccountPassword(password);
249
QString pwd = acc->getAccountPassword();
250
QCOMPARE(pwd,password);*/
253
void AccountTests::testAccountMailbox ()/*QString detail*/
255
Account* acc = AccountList::getInstance()->getAccountById(id);
256
acc->setAccountMailbox("1234567879");
258
QString mailbox = acc->getAccountMailbox();
259
QCOMPARE(mailbox,QString("1234567879"));
263
void AccountTests::testTlsPassword ()/*QString detail*/
265
Account* acc = AccountList::getInstance()->getAccountById(id);
266
acc->setTlsPassword("1234567879");
268
QString tlspass = acc->getTlsPassword();
269
QCOMPARE(tlspass,QString("1234567879"));
272
void AccountTests::testTlsCaListFile ()/*QString detail*/
274
//Account* acc = AccountList::getInstance()->getAccountById(id);
275
QSKIP("TODO",SkipAll);
278
void AccountTests::testTlsCertificateFile ()/*QString detail*/
280
//Account* acc = AccountList::getInstance()->getAccountById(id);
281
QSKIP("TODO",SkipAll);
284
void AccountTests::testTlsPrivateKeyFile ()/*QString detail*/
286
//Account* acc = AccountList::getInstance()->getAccountById(id);
287
QSKIP("TODO",SkipAll);
290
void AccountTests::testTlsCiphers ()/*QString detail*/
292
//Account* acc = AccountList::getInstance()->getAccountById(id);
293
QSKIP("TODO",SkipAll);
296
void AccountTests::testTlsServerName ()/*QString detail*/
298
Account* acc = AccountList::getInstance()->getAccountById(id);
299
acc->setTlsServerName("qwerty");
301
QString tlsserver = acc->getTlsServerName();
302
QCOMPARE(tlsserver,QString("qwerty"));
305
void AccountTests::testAccountSipStunServer ()/*QString detail*/
307
Account* acc = AccountList::getInstance()->getAccountById(id);
308
acc->setAccountSipStunServer("qwerty");
310
QString tlsserver = acc->getAccountSipStunServer();
311
QCOMPARE(tlsserver,QString("qwerty"));
314
void AccountTests::testPublishedAddress ()/*QString detail*/
316
//Account* acc = AccountList::getInstance()->getAccountById(id);
317
QSKIP("TODO",SkipAll);
320
void AccountTests::testLocalInterface ()/*QString detail*/
322
//Account* acc = AccountList::getInstance()->getAccountById(id);
323
QSKIP("TODO",SkipAll);
326
void AccountTests::testRingtonePath_data()
328
QTest::addColumn<QString>("path");
329
QMap<QString,QString> ringtonePaths = ConfigurationManagerInterfaceSingleton::getInstance().getRingtoneList();
330
QMutableMapIterator<QString, QString> iter(ringtonePaths);
331
while (iter.hasNext()) {
333
QTest::newRow( iter.value().toAscii() ) << iter.key();
335
QTest::newRow( "invalidWav" ) << QString("invalid/tmp2/fake_file.wav");
336
QTest::newRow( "invalidAu" ) << QString("invalid/tmp2/fake_file.au");
337
QTest::newRow( "invalidUl" ) << QString("invalid/tmp2/fake_file.ul");
339
QString tmpPath = QDir::tempPath();
340
QFile file (tmpPath+"/testWav.wav");
341
file.open(QIODevice::WriteOnly);
343
QTest::newRow( "wav_file" ) << tmpPath+"/testWav.wav";
345
QFile file2 (tmpPath+"/testAu.au");
346
file2.open(QIODevice::WriteOnly);
348
QTest::newRow( "au_file" ) << tmpPath+"/testAu.au";
350
QFile file3 (tmpPath+"/testUl.ul");
351
file3.open(QIODevice::WriteOnly);
353
QTest::newRow( "ul_file" ) << tmpPath+"/testUl.ul";
357
void AccountTests::testRingtonePath ()/*QString detail*/
359
Account* acc = AccountList::getInstance()->getAccountById(id);
360
QFETCH(QString, path);
361
acc->setRingtonePath(path);
363
if (path.indexOf("invalid") != -1)
364
QCOMPARE(acc->getRingtonePath() == path ,false);
366
QCOMPARE(acc->getRingtonePath(),path);
369
void AccountTests::testTlsMethod ()/*int detail*/
371
//Account* acc = AccountList::getInstance()->getAccountById(id);
372
QSKIP("TODO",SkipAll);
375
void AccountTests::testAccountRegistrationExpire ()/*int detail*/
377
Account* acc = AccountList::getInstance()->getAccountById(id);
378
acc->setAccountRegistrationExpire(10000);
380
QCOMPARE(acc->getAccountRegistrationExpire(),10000);
382
//Time machines are not on the market yet
383
acc->setAccountRegistrationExpire(-10000);
385
QCOMPARE(acc->getAccountRegistrationExpire() == -10000,false);
388
void AccountTests::testTlsNegotiationTimeoutSec ()/*int detail*/
390
Account* acc = AccountList::getInstance()->getAccountById(id);
391
acc->setTlsNegotiationTimeoutSec(10000);
394
QCOMPARE(acc->getTlsNegotiationTimeoutSec(),10000);
396
//Time machines are not on the market yet
397
acc->setTlsNegotiationTimeoutSec(-10000);
399
QCOMPARE(acc->getTlsNegotiationTimeoutSec() == -10000,false);
402
void AccountTests::testTlsNegotiationTimeoutMsec ()/*int detail*/
404
Account* acc = AccountList::getInstance()->getAccountById(id);
406
acc->setTlsNegotiationTimeoutMsec(10000);
408
QCOMPARE(acc->getTlsNegotiationTimeoutMsec(),10000);
410
//Time machines are not on the market yet
411
acc->setTlsNegotiationTimeoutMsec(-10000);
413
QCOMPARE(acc->getTlsNegotiationTimeoutMsec() == -10000,false);
416
void AccountTests::testLocalPort_data()
418
//It is really an unsigned short, but lets do real tests instead
419
QTest::addColumn<int>("port");
420
QTest::newRow( "null" ) << 0 ;
421
QTest::newRow( "2000" ) << 2000 ;
422
QTest::newRow( "high" ) << 65533 ;
423
QTest::newRow( "over" ) << 1000000;
424
QTest::newRow( "negative" ) << -1000 ;
428
void AccountTests::testLocalPort ()/*short detail*/
430
Account* acc = AccountList::getInstance()->getAccountById(id);
432
acc->setLocalPort(port);
434
if (port < 0 || port > 65533)
435
QCOMPARE(acc->getLocalPort() == port,false);
437
QCOMPARE(acc->getLocalPort(),port);
440
void AccountTests::testTlsListenerPort_data()
442
//It is really an unsigned short, but lets do real tests instead
443
QTest::addColumn<int>("port");
444
QTest::newRow( "null" ) << 0 ;
445
QTest::newRow( "2000" ) << 2000 ;
446
QTest::newRow( "high" ) << 65533 ;
447
QTest::newRow( "over" ) << 1000000;
448
QTest::newRow( "negative" ) << -1000 ;
452
void AccountTests::testTlsListenerPort ()/*short detail*/
454
Account* acc = AccountList::getInstance()->getAccountById(id);
456
acc->setTlsListenerPort(port);
458
if (port < 0 || port > 65533)
459
QCOMPARE(acc->getTlsListenerPort() == port,false);
461
QCOMPARE(acc->getTlsListenerPort(),port);
464
void AccountTests::testPublishedPort_data()
466
//It is really an unsigned short, but lets do real tests instead
467
QTest::addColumn<int>("port");
468
QTest::newRow( "null" ) << 0 ;
469
QTest::newRow( "2000" ) << 2000 ;
470
QTest::newRow( "high" ) << 65533 ;
471
QTest::newRow( "over" ) << 1000000;
472
QTest::newRow( "negative" ) << -1000 ;
476
void AccountTests::testPublishedPort ()/*short detail*/
478
Account* acc = AccountList::getInstance()->getAccountById(id);
480
acc->setPublishedPort(port);
482
if (port < 0 || port > 65533)
483
QCOMPARE(acc->getPublishedPort() == port,false);
485
QCOMPARE(acc->getPublishedPort(),port);
488
void AccountTests::testAccountEnabled ()/*bool detail*/
490
Account* acc = AccountList::getInstance()->getAccountById(id);
491
acc->setAccountEnabled(false);
492
QCOMPARE(acc->isAccountEnabled(),false);
493
acc->setAccountEnabled(true);
494
QCOMPARE(acc->isAccountEnabled(),true);
497
void AccountTests::testTlsVerifyServer ()/*bool detail*/
499
//Account* acc = AccountList::getInstance()->getAccountById(id);
500
QSKIP("TODO",SkipAll);
503
void AccountTests::testTlsVerifyClient ()/*bool detail*/
505
//Account* acc = AccountList::getInstance()->getAccountById(id);
506
QSKIP("TODO",SkipAll);
509
void AccountTests::testTlsRequireClientCertificate ()/*bool detail*/
511
Account* acc = AccountList::getInstance()->getAccountById(id);
512
acc->setTlsRequireClientCertificate(true);
514
QCOMPARE(acc->isTlsRequireClientCertificate(),true);
515
acc->setTlsRequireClientCertificate(false);
517
QCOMPARE(acc->isTlsRequireClientCertificate(),false);
520
void AccountTests::testTlsEnable ()/*bool detail*/
522
Account* acc = AccountList::getInstance()->getAccountById(id);
523
acc->setTlsEnable(true);
525
QCOMPARE(acc->isTlsEnable(),true);
526
acc->setTlsEnable(false);
528
QCOMPARE(acc->isTlsEnable(),false);
531
void AccountTests::testAccountDisplaySasOnce ()/*bool detail*/
533
Account* acc = AccountList::getInstance()->getAccountById(id);
534
acc->setAccountDisplaySasOnce(true);
536
QCOMPARE(acc->isAccountDisplaySasOnce(),true);
537
acc->setAccountDisplaySasOnce(false);
539
QCOMPARE(acc->isAccountDisplaySasOnce(),false);
542
void AccountTests::testAccountSrtpRtpFallback ()/*bool detail*/
544
Account* acc = AccountList::getInstance()->getAccountById(id);
545
acc->setAccountSrtpRtpFallback(true);
547
QCOMPARE(acc->isAccountSrtpRtpFallback(),true);
548
acc->setAccountSrtpRtpFallback(false);
550
QCOMPARE(acc->isAccountSrtpRtpFallback(),false);
553
void AccountTests::testAccountZrtpDisplaySas ()/*bool detail*/
555
Account* acc = AccountList::getInstance()->getAccountById(id);
556
acc->setAccountSrtpRtpFallback(true);
558
QCOMPARE(acc->isAccountSrtpRtpFallback(),true);
559
acc->setAccountSrtpRtpFallback(false);
561
QCOMPARE(acc->isAccountSrtpRtpFallback(),false);
564
void AccountTests::testAccountZrtpNotSuppWarning ()/*bool detail*/
566
Account* acc = AccountList::getInstance()->getAccountById(id);
567
acc->setAccountZrtpNotSuppWarning(true);
569
QCOMPARE(acc->isAccountZrtpNotSuppWarning(),true);
570
acc->setAccountZrtpNotSuppWarning(false);
572
QCOMPARE(acc->isAccountZrtpNotSuppWarning(),false);
575
void AccountTests::testAccountZrtpHelloHash ()/*bool detail*/
577
Account* acc = AccountList::getInstance()->getAccountById(id);
578
acc->setAccountZrtpHelloHash(true);
580
QCOMPARE(acc->isAccountZrtpHelloHash(),true);
581
acc->setAccountZrtpHelloHash(false);
583
QCOMPARE(acc->isAccountZrtpHelloHash(),false);
586
void AccountTests::testAccountSipStunEnabled ()/*bool detail*/
588
Account* acc = AccountList::getInstance()->getAccountById(id);
589
acc->setAccountSipStunEnabled(true);
591
QCOMPARE(acc->isAccountSipStunEnabled(),true);
592
acc->setAccountSipStunEnabled(false);
594
QCOMPARE(acc->isAccountSipStunEnabled(),false);
597
void AccountTests::testPublishedSameAsLocal ()/*bool detail*/
599
Account* acc = AccountList::getInstance()->getAccountById(id);
600
acc->setPublishedSameAsLocal(true);
602
QCOMPARE(acc->isPublishedSameAsLocal(),true);
603
acc->setPublishedSameAsLocal(false);
605
QCOMPARE(acc->isPublishedSameAsLocal(),false);
608
void AccountTests::testConfigRingtoneEnabled ()/*bool detail*/
610
Account* acc = AccountList::getInstance()->getAccountById(id);
611
acc->setRingtoneEnabled(true);
613
QCOMPARE(acc->isRingtoneEnabled(),true);
614
acc->setRingtoneEnabled(false);
616
QCOMPARE(acc->isRingtoneEnabled(),false);
619
//END Testing every account attributes
621
//BEGIN testing account list
623
void AccountTests::testDisableAllAccounts()
625
QList<bool> saveState;
626
//Disable all accounts
627
for (int i=0;i<AccountList::getInstance()->size();i++) {
628
saveState << (*AccountList::getInstance())[i]->isAccountEnabled();
629
(*AccountList::getInstance())[i]->setAccountEnabled(false);
630
(*AccountList::getInstance())[i]->save();
633
QCOMPARE(AccountList::getCurrentAccount(),(Account*)nullptr);
636
for (int i=0;i<AccountList::getInstance()->size();i++) {
637
(*AccountList::getInstance())[i]->setAccountEnabled(saveState[i]);
638
(*AccountList::getInstance())[i]->save();
642
//END testing account list
645
void AccountTests::cleanupTestCase() {
646
AccountList::getInstance()->removeAccount(AccountList::getInstance()->getAccountById(id));
647
QCOMPARE( AccountList::getInstance()->getAccountById(id) == nullptr, true);
651
QTEST_MAIN(AccountTests)
652
#include "account_test.moc"