1 /*
2  * This file is part of KMyMoney, A Personal Finance Manager by KDE
3  * Copyright (C) 2014 Christian Dávid <christian-david@web.de>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "internationalaccountidentifier-test.h"
20 #include "payeeidentifier/ibanbic/ibanbic.h"
21 #include "plugins/ibanbicdata/ibanbicdata.h"
22 
23 #include <QtTest>
24 
25 QTEST_GUILESS_MAIN(internationalAccountIdentifierTest);
26 
dataInstalled(const QString & countryCode)27 bool internationalAccountIdentifierTest::dataInstalled(const QString& countryCode)
28 {
29   // Not really implemented yet
30   Q_UNUSED(countryCode);
31   return false;
32 }
33 
initTestCase()34 void internationalAccountIdentifierTest::initTestCase()
35 {
36   // Called before the first testfunction is executed
37 }
38 
cleanupTestCase()39 void internationalAccountIdentifierTest::cleanupTestCase()
40 {
41   // Called after the last testfunction was executed
42 }
43 
init()44 void internationalAccountIdentifierTest::init()
45 {
46   // Called before each testfunction is executed
47 }
48 
cleanup()49 void internationalAccountIdentifierTest::cleanup()
50 {
51   // Called after every testfunction
52 }
53 
comparison()54 void internationalAccountIdentifierTest::comparison()
55 {
56 
57 }
58 
ibanChecksum_data()59 void internationalAccountIdentifierTest::ibanChecksum_data()
60 {
61   QTest::addColumn<QString>("iban");
62   QTest::addColumn<bool>("testResult");
63 
64   QTest::newRow("KDE e.V.") << "DE82200700240066644600" << true;
65   QTest::newRow("Invalid iban") << "DE82200700240066644601" << false;
66   QTest::newRow("IBAN with letters") << "BH82AEHI21601643513576" << true;
67 }
68 
ibanChecksum()69 void internationalAccountIdentifierTest::ibanChecksum()
70 {
71   QFETCH(QString, iban);
72   QFETCH(bool, testResult);
73 
74   QCOMPARE(payeeIdentifiers::ibanBic::validateIbanChecksum(iban), testResult);
75 }
76 
paperformatIban_data()77 void internationalAccountIdentifierTest::paperformatIban_data()
78 {
79   QTest::addColumn<QString>("iban");
80   QTest::addColumn<QString>("paperformat");
81 
82   /** Random ibans generated using https://www.mobilefish.com/services/random_iban_generator/random_iban_generator.php */
83   QTest::newRow("AL55359338525014419438694535") << "AL55359338525014419438694535" << "AL55 3593 3852 5014 4194 3869 4535";
84   QTest::newRow("AD6507599863323512292387")     << "AD6507599863323512292387"     << "AD65 0759 9863 3235 1229 2387";
85   QTest::newRow("AT550611200130969602")         << "AT550611200130969602"         << "AT55 0611 2001 3096 9602";
86   QTest::newRow("AZ45YKNN50322618666505570288") << "AZ45YKNN50322618666505570288" << "AZ45 YKNN 5032 2618 6665 0557 0288";
87   QTest::newRow("BH82AEHI21601643513576")       << "BH82AEHI21601643513576"       << "BH82 AEHI 2160 1643 5135 76";
88   QTest::newRow("FR1767089178626632115068411")  << "FR1767089178626632115068411"  << "FR17 6708 9178 6266 3211 5068 411";
89   QTest::newRow("GE87UH3052380574220575")       << "GE87UH3052380574220575"       << "GE87 UH30 5238 0574 2205 75";
90   QTest::newRow("DE88476823289460743695")       << "DE88476823289460743695"       << "DE88 4768 2328 9460 7436 95";
91   QTest::newRow("it81K2055156417927233643224")  << "it81K2055156417927233643224"  << "IT81 K205 5156 4179 2723 3643 224";
92 
93   // Unfinished ibans
94   QTest::newRow("PK")                           << "PK"                           << "PK";
95   QTest::newRow("NO5194556")                    << "NO5194556"                    << "NO51 9455 6";
96 
97   // Non canonical ibans
98   QTest::newRow("VG33 NRCC 0371 8957 2076 3593") << "VG33 NRCC 0371 8957 2076 3593" << "VG33 NRCC 0371 8957 2076 3593";
99   QTest::newRow(" SI 4523 946 27 23 327 14 9  ") << " SI 4523 946 27 23 327 14 9  " << "SI45 2394 6272 3327 149";
100   QTest::newRow("MK 3-27/3287/612--98207//26")  << "MK 3-27/3287/612--98207//26"  << "MK32 7328 7612 9820 726";
101 }
102 
paperformatIban()103 void internationalAccountIdentifierTest::paperformatIban()
104 {
105   QFETCH(QString, iban);
106   QFETCH(QString, paperformat);
107 
108   QCOMPARE(payeeIdentifiers::ibanBic::ibanToPaperformat(iban), paperformat);
109 }
110 
electronicformatIban_data()111 void internationalAccountIdentifierTest::electronicformatIban_data()
112 {
113   QTest::addColumn<QString>("iban");
114   QTest::addColumn<QString>("electronic");
115 
116   QTest::newRow("AL89 5112 2491 7164 1236 8777 7047") << "AL89 5112 2491 7164 1236 8777 7047" << "AL89511224917164123687777047";
117   QTest::newRow("AZ11 BIAH 1276 1568 9842 3064 6155") << "AZ11 BIAH 1276 1568 9842 3064 6155" << "AZ11BIAH12761568984230646155";
118   QTest::newRow("AZ73  WMRx 62 73 6   823 2803 9705") << "AZ73  WMRx 62 73 6   823 2803 9705" << "AZ73WMRX6273682328039705";
119   QTest::newRow("AZ55/MKDW-9866$8070(4022)5306 7865") << "AZ55/MKDW-9866$8070(4022)5306 7865" << "AZ55MKDW98668070402253067865";
120   QTest::newRow("AZ 57                             ") << "AZ 57                             " << "AZ57";
121   QTest::newRow("dk3958515811555611")                 << "dk3958515811555611"                 << "DK3958515811555611";
122 }
123 
electronicformatIban()124 void internationalAccountIdentifierTest::electronicformatIban()
125 {
126   QFETCH(QString, iban);
127   QFETCH(QString, electronic);
128   QCOMPARE(payeeIdentifiers::ibanBic::ibanToElectronic(iban), electronic);
129 }
130 
setIban_data()131 void internationalAccountIdentifierTest::setIban_data()
132 {
133   QTest::addColumn<QString>("iban");
134   QTest::addColumn<QString>("electronic");
135   QTest::addColumn<QString>("paperformat");
136 
137   QTest::newRow("AL89 5112 2491 7164 1236 8777 7047") << "AL89 5112 2491 7164 1236 8777 7047" << "AL89511224917164123687777047" << "AL89 5112 2491 7164 1236 8777 7047" ;
138   QTest::newRow("AZ73  WMRX 62 73 6   823 2803 9705") << "AZ73  WMRX 62 73 6   823 2803 9705" << "AZ73WMRX6273682328039705"     << "AZ73 WMRX 6273 6823 2803 9705";
139   QTest::newRow("AZ55/MKDW-9866$8070(4022)5306 7865") << "AZ55/MKDW-9866$8070(4022)5306 7865" << "AZ55MKDW98668070402253067865" << "AZ55 MKDW 9866 8070 4022 5306 7865";
140   QTest::newRow("AZ 57                             ") << "AZ 57                             " << "AZ57"                         << "AZ57";
141   QTest::newRow("DK3958515811555611")                 << "DK3958515811555611"                 << "DK3958515811555611"           << "DK39 5851 5811 5556 11";
142 }
143 
setIban()144 void internationalAccountIdentifierTest::setIban()
145 {
146   QFETCH(QString, iban);
147   QFETCH(QString, electronic);
148   QFETCH(QString, paperformat);
149 
150   payeeIdentifiers::ibanBic ident;
151   ident.setIban(iban);
152   QCOMPARE(ident.electronicIban(), electronic);
153   QCOMPARE(ident.paperformatIban(), paperformat);
154 }
155 
setBic_data()156 void internationalAccountIdentifierTest::setBic_data()
157 {
158   QTest::addColumn<QString>("input");
159   QTest::addColumn<QString>("normalized");
160   QTest::addColumn<QString>("full");
161 
162   QTest::newRow("Lower case")     << "chasgb2lxXx" << "CHASGB2L"    << "CHASGB2LXXX";
163   QTest::newRow("Arbitrary case") << "RZtIaT22263" << "RZTIAT22263" << "RZTIAT22263";
164   QTest::newRow("Without XXX")    << "MARKDEFF"    << "MARKDEFF"    << "MARKDEFFXXX";
165   QTest::newRow("With XXX")       << "MARKDEFFXXX" << "MARKDEFF"    << "MARKDEFFXXX";
166   QTest::newRow("Arbitray bic")   << "GENODEF1JEV" << "GENODEF1JEV" << "GENODEF1JEV";
167 }
168 
setBic()169 void internationalAccountIdentifierTest::setBic()
170 {
171   QFETCH(QString, input);
172   QFETCH(QString, normalized);
173   QFETCH(QString, full);
174 
175   payeeIdentifiers::ibanBic ident;
176   ident.setBic(input);
177   QCOMPARE(ident.bic(), normalized);
178   QCOMPARE(ident.fullBic(), full);
179 }
180 
equalOperator_data()181 void internationalAccountIdentifierTest::equalOperator_data()
182 {
183   QTest::addColumn<QString>("bic1");
184   QTest::addColumn<QString>("iban1");
185   QTest::addColumn<QString>("bic2");
186   QTest::addColumn<QString>("iban2");
187   QTest::addColumn<bool>("equals");
188 
189   QTest::newRow("equal") << "MARKDEFFXXX" << "DE88476823289460743695" << "MARKDEFF" << "        DE88 4768 2328 9460 7436 95" << true;
190   QTest::newRow("BIC unequal") << "MARKDEFF001" << "DE884768  23289460743695" << "MARKDEFF" << "DE88 4768 2328 9460 7436 95" << false;
191   QTest::newRow("IBAN unequal") << "MARKDEFFXXX" << "DE88476823289460743695" << "MARKDEFF" << "GE87UH3052380574220575" << false;
192 }
193 
equalOperator()194 void internationalAccountIdentifierTest::equalOperator()
195 {
196   QFETCH(QString, bic1);
197   QFETCH(QString, iban1);
198   QFETCH(QString, bic2);
199   QFETCH(QString, iban2);
200   QFETCH(bool, equals);
201 
202   payeeIdentifiers::ibanBic ident1;
203   ident1.setBic(bic1);
204   ident1.setIban(iban1);
205 
206   payeeIdentifiers::ibanBic ident2;
207   ident2.setBic(bic2);
208   ident2.setIban(iban2);
209 
210   if (equals)
211     QCOMPARE(ident1, ident2);
212   else
213     QVERIFY(!(ident1 == ident2));
214 }
215 
uneqalOperator_data()216 void internationalAccountIdentifierTest::uneqalOperator_data()
217 {
218   equalOperator_data();
219 }
220 
uneqalOperator()221 void internationalAccountIdentifierTest::uneqalOperator()
222 {
223   QFETCH(QString, bic1);
224   QFETCH(QString, iban1);
225   QFETCH(QString, bic2);
226   QFETCH(QString, iban2);
227   QFETCH(bool, equals);
228 
229   payeeIdentifiers::ibanBic ident1;
230   ident1.setBic(bic1);
231   ident1.setIban(iban1);
232 
233   payeeIdentifiers::ibanBic ident2;
234   ident2.setBic(bic2);
235   ident2.setIban(iban2);
236 
237   if (equals)
238     QVERIFY(!(ident1 != ident2));
239   else
240     QVERIFY(ident1 != ident2);
241 }
242 
getProperties_data()243 void internationalAccountIdentifierTest::getProperties_data()
244 {
245   QTest::addColumn<QString>("countryCode");
246   QTest::addColumn<int>("bbanLength");
247   QTest::addColumn<int>("bankIdentifierLength");
248 
249   QTest::newRow("Germany")     << "DE" << 18 << 8;
250   QTest::newRow("France")      << "FR" << 23 << 10;
251   QTest::newRow("Switzerland") << "CH" << 17 << 5;
252 }
253 
getProperties()254 void internationalAccountIdentifierTest::getProperties()
255 {
256   QFETCH(QString, countryCode);
257   QFETCH(int, bbanLength);
258 
259   if (!dataInstalled(countryCode))
260     QSKIP(qPrintable(QString("Could not find ibanBicData service for this country (was looking for \"%1\"). Did you install the services?").arg(countryCode)), SkipSingle);
261 
262   QCOMPARE(payeeIdentifiers::ibanBic::ibanLengthByCountry(countryCode), bbanLength + 4);
263 }
264 
iban2bic_data()265 void internationalAccountIdentifierTest::iban2bic_data()
266 {
267   QTest::addColumn<QString>("iban");
268   QTest::addColumn<QString>("bic");
269 
270   QTest::newRow("Germany (Unicef Germany)")   << "DE57370205000000300000" << "BFSWDE33XXX";
271   QTest::newRow("Switzerland (Unicef Swiss)") << "CH8809000000800072119"  << "POFICHBEXXX";
272 }
273 
iban2bic()274 void internationalAccountIdentifierTest::iban2bic()
275 {
276   QFETCH(QString, iban);
277   QFETCH(QString, bic);
278 
279   if (!dataInstalled(iban.left(2)))
280     QSKIP(qPrintable(QString("Could not find ibanBicData service for this country (was looking for \"%1\"). Did you install the services?").arg(iban)), SkipSingle);
281 
282   QCOMPARE(payeeIdentifiers::ibanBic::bicByIban(iban), bic);
283 }
284 
nameByBic_data()285 void internationalAccountIdentifierTest::nameByBic_data()
286 {
287   QTest::addColumn<QString>("bic");
288   QTest::addColumn<QString>("name");
289 
290   QTest::newRow("Germany (Bundesbank)") << "MARKDEF1100" << "Bundesbank";
291 }
292 
nameByBic()293 void internationalAccountIdentifierTest::nameByBic()
294 {
295   QFETCH(QString, bic);
296   QFETCH(QString, name);
297 
298   if (!dataInstalled(bic.mid(4, 2)))
299     QSKIP(qPrintable(QString("Could not find ibanBicData service for this country (was looking for \"%1\"). Did you install the services?").arg(bic)), SkipSingle);
300 
301   QCOMPARE(payeeIdentifiers::ibanBic::institutionNameByBic(bic), name);
302 }
303 
bicAndNameByIban_data()304 void internationalAccountIdentifierTest::bicAndNameByIban_data()
305 {
306   QTest::addColumn<QString>("iban");
307   QTest::addColumn<QString>("bic");
308   QTest::addColumn<QString>("name");
309 
310   QTest::newRow("Germany (Unicef Germany)")   << "DE57370205000000300000" << "BFSWDE33XXX" << "";
311 }
312 
bicAndNameByIban()313 void internationalAccountIdentifierTest::bicAndNameByIban()
314 {
315   QTest::addColumn<QString>("iban");
316   QTest::addColumn<QString>("bic");
317   QTest::addColumn<QString>("name");
318 
319   QSKIP("Test not implemented", SkipAll);
320   //QPair<QString, QString> pair = internationalAccountIdentifier::;
321 }
322 
qStringNullAndEmpty()323 void internationalAccountIdentifierTest::qStringNullAndEmpty()
324 {
325   const QString nullStr1;
326   QVERIFY(nullStr1.isNull());
327   QVERIFY(nullStr1.isEmpty());
328 
329   const QString nullStr2 = QString();
330   QVERIFY(nullStr2.isEmpty());
331   QVERIFY(nullStr2.isNull());
332 
333   const QString empty = QString("");
334   QVERIFY(empty.isEmpty());
335   QVERIFY(!empty.isNull());
336 }
337 
338 Q_DECLARE_METATYPE(payeeIdentifiers::ibanBic::bicAllocationStatus);
339 
bicAllocated_data()340 void internationalAccountIdentifierTest::bicAllocated_data()
341 {
342   QTest::addColumn<QString>("bic");
343   QTest::addColumn<payeeIdentifiers::ibanBic::bicAllocationStatus>("allocated");
344 
345   QTest::newRow("Bundesbank")   << "MARKDEFFXXX" << payeeIdentifiers::ibanBic::bicAllocated;
346   QTest::newRow("Not existing") << "DOOFDE12NOT" << payeeIdentifiers::ibanBic::bicNotAllocated;
347   QTest::newRow("Unknown")      << "NODAFRTAFOR" << payeeIdentifiers::ibanBic::bicAllocationUncertain;
348 }
349 
bicAllocated()350 void internationalAccountIdentifierTest::bicAllocated()
351 {
352   QFETCH(QString, bic);
353   QFETCH(payeeIdentifiers::ibanBic::bicAllocationStatus, allocated);
354 
355   if (!dataInstalled(bic.mid(4, 2)))
356     QSKIP(qPrintable(QString("Could not find ibanBicData service for this country (was looking for \"%1\"). Did you install the services?").arg(bic)), SkipSingle);
357 
358   QCOMPARE(payeeIdentifiers::ibanBic::isBicAllocated(bic), allocated);
359 }
360