Debian Codegraph @

src/plugin/folderlistmodel/smb/qsambaclient/test/testqsambasuite.cpp

raw symbols SCIP
1 /**************************************************************************
2 *
3 * Copyright 2014 Canonical Ltd.
4 * Copyright 2014 Carlos J Mazieri <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; version 3.
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 Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * File: testqsambasuite.cpp
19 * Date: 03/12/2014
20 */
21
22 #include "testqsambasuite.h"
23 #include "smbusershare.h"
24 #include "smbiteminfo.h"
25 #include "smbplaces.h"
26 #include "locationurl.h"
27 #include "smbutil.h"
28 #include "smblocationdiriterator.h"
29 #include "smblocationitemfile.h"
30 #include "smblocationitemdir.h"
31 #include <sys/stat.h>
32 #include <errno.h>
33
34 #include <QTest>
35 #include <QFile>
36 #include <QTemporaryDir>
37 #include <QFileInfo>
38 #include <QDebug>
39
40
41 #define RETURN_SHARE_STATUS_WHEN_FALSE(x,y) if (! (x == y) ) { \
42 qDebug() << "ERROR:" << Q_FUNC_INFO << "line:" << __LINE__ << "compare failed"; \
43 return ret; \
44 }
45
46 ShareCreationStatus::ShareCreationStatus(const QString& dirNameMask) :
47 status(false),
48 tempDir(new QTemporaryDir(QDir::tempPath() + QDir::separator() + dirNameMask))
49 {
50 sharedDirPath = tempDir->path();
51 }
52
53 ShareCreationStatus::ShareCreationStatus(const ShareCreationStatus & o)
54 {
55 *this = o;
56 }
57
58
59 ShareCreationStatus::~ShareCreationStatus()
60 {
61 if (tempDir)
62 {
63 delete tempDir;
64 }
65 }
66
67 ShareCreationStatus &
68 ShareCreationStatus::operator=(const ShareCreationStatus & o)
69 {
70 ShareCreationStatus *other = const_cast<ShareCreationStatus*> (&o);
71 *this = *other;
72 return *this;
73 }
74
75 ShareCreationStatus &
76 ShareCreationStatus::operator=(ShareCreationStatus & o)
77 {
78
79 sharedDirPath = o.sharedDirPath;
80 shareName = o.shareName;
81 fileContent.diskPathname = o.fileContent.diskPathname;
82 status = o.status;
83 tempDir = o.tempDir;
84 url = o.url;
85 if (tempDir)
86 {
87 tempDir->setAutoRemove(true);
88 }
89 o.tempDir = 0;
90 return *this;
91 }
92
93 SmbSharedPathAccess
94 ShareCreationStatus::createPathForItem(const QString &item)
95 {
96 SmbSharedPathAccess pathItem;
97 pathItem.diskPathname = sharedDirPath + QLatin1Char('/') + item;
98 pathItem.smbUrl = url + QLatin1Char('/') + item;
99 return pathItem;
100 }
101
102 SmbSharedPathAccess
103 ShareCreationStatus::createPathForItems(const QStringList &items)
104 {
105 SmbSharedPathAccess pathItem;
106 pathItem.diskPathname = sharedDirPath;
107 pathItem.smbUrl = url;
108 for (int counter=0; counter < items.count(); ++counter)
109 {
110 pathItem.diskPathname += QLatin1Char('/') + items.at(counter);
111 pathItem.smbUrl += QLatin1Char('/') + items.at(counter);
112 }
113 return pathItem;
114 }
115
116
117 TestQSambaSuite::TestQSambaSuite(QObject *parent) :
118 QObject(parent)
119 ,m_smbShares( new SmbPlaces() )
120 ,m_curUmask(0)
121 {
122 }
123
124
125 TestQSambaSuite::~TestQSambaSuite()
126 {
127 delete m_smbShares;
128 if (!m_curShareName.isEmpty())
129 {
130 SmbUserShare::removeShare(m_curShareName);
131 }
132 }
133
134
135 void TestQSambaSuite::initTestCase()
136 {
137 QCOMPARE(SmbUserShare::canCreateShares(), true);
138 m_curUmask = umask(0);
139 }
140
141
142 void TestQSambaSuite::cleanupTestCase()
143 {
144 umask(m_curUmask);
145 }
146
147
148 void TestQSambaSuite::init()
149 {
150 m_curShareName.clear();
151 }
152
153 /*!
154 * \brief TestQSambaSuite::cleanup()
155 *
156 * It is called after every test case, removes the shared indicated by m_curShareName
157 * and checks if it was removed
158 *
159 */
160 void TestQSambaSuite::cleanup()
161 {
162 if (!m_curShareName.isEmpty())
163 {
164 SmbUserShare::removeShare(m_curShareName);
165 SmbUserShare::UserShareFile share = SmbUserShare::search(m_curShareName);
166 m_curShareName.clear();
167 QCOMPARE(share.exists() , false);
168 }
169 }
170
171 // ============== utils
172
173 /*!
174 * \brief TestQSambaSuite::createTempFile() creates a file \a name inside \a path
175 * \param path
176 * \param name
177 * \param content some content or nothing
178 *
179 * \return fullpath of the file if it was able to create
180 */
181 QString TestQSambaSuite::createTempFile(const QString &path,
182 const QString &name,
183 const QByteArray &content)
184
185 {
186 QString ret;
187 QFile f( path + QDir::separator() + name );
188 if (f.open(QFile::WriteOnly))
189 {
190 if (f.write(content) == content.size())
191 {
192 ret = f.fileName();
193 }
194 f.close();
195 }
196 return ret;
197 }
198
199
200 /*!
201 * \brief TestQSambaSuite::existShare(0 checks is share name exist in a such list of shares
202 * \param sharesFullPathList someting like ("smb://localhost/samba-devel", "smb://localhost/mac-devel")
203 * \param shareName e.g. "samba-devel"
204 * \return true if exists
205 */
206 bool TestQSambaSuite::existsShare(const QStringList& sharesFullPathList, const QString &shareName)
207 {
208 bool ret = false;
209 int counter = sharesFullPathList.count();
210 while (counter--)
211 {
212 if (sharesFullPathList.at(counter).contains(shareName))
213 {
214 ret = true;
215 break;
216 }
217 }
218 return ret;
219 }
220
221
222 ShareCreationStatus TestQSambaSuite::createTempShare(const QString &maskName,
223 bool fullAccess,
224 bool allowGuests)
225 {
226
227 ShareCreationStatus ret(maskName);
228 QTemporaryDir * shareDir = ret.tempDir;
229 RETURN_SHARE_STATUS_WHEN_FALSE(shareDir->isValid(), true);
230
231 m_curShareName = QFileInfo(shareDir->path()).fileName();
232
233 //put some content in it
234 QString fileContentName("somecontent.txt");
235 QString filename = createTempFile(shareDir->path(), fileContentName, "hello Samba");
236 RETURN_SHARE_STATUS_WHEN_FALSE(filename.isEmpty(), false);
237
238 //save current samba shares list
239 QStringList currentShares = m_smbShares->listPlacesSync();
240
241 //get the supposed share name that is going to be created
242 QString shareDirName = SmbUserShare::proposedName(shareDir->path());
243 ret.shareName = shareDirName;
244
245 //make sure shareDirName does not exist in the current samba share list
246 RETURN_SHARE_STATUS_WHEN_FALSE(shareDirName.isEmpty(), false);
247 RETURN_SHARE_STATUS_WHEN_FALSE(shareDirName, m_curShareName);
248 bool exists = existsShare(currentShares,shareDirName);
249 RETURN_SHARE_STATUS_WHEN_FALSE(exists, false);
250
251 //first remove the share if it already exists, perhaps due to a failure in a previous test
252 SmbUserShare::removeShare(shareDir->path());
253 //create the share
254 bool created = SmbUserShare::createShareForFolder(shareDir->path(),
255 fullAccess ? SmbUserShare::ReadWrite : SmbUserShare::Readonly,
256 allowGuests);
257 RETURN_SHARE_STATUS_WHEN_FALSE(created, true);
258
259 // now make sure the new share is created
260 QStringList moreOneShare = m_smbShares->listPlacesSync();
261 exists = existsShare(moreOneShare, shareDirName);
262 RETURN_SHARE_STATUS_WHEN_FALSE(exists, true);
263
264 //everything is OK
265 ret.status = true;
266
267 //let the share be removed by its path instead of the name
268 m_curShareName = shareDir->path();
269 ret.url = LocationUrl::SmbURL + "localhost/" + ret.shareName;
270 ret.fileContent = ret.createPathForItem(fileContentName);
271
272 return ret;
273 }
274
275
276 //================ test cases==================
277 void TestQSambaSuite::positive_createReadOnlyUserShare()
278 {
279 //create a directory to share its content
280 QTemporaryDir shareDir (QDir::tempPath() + QDir::separator() + "positive_createReadOnlyUserShare");
281 m_curShareName = QFileInfo(shareDir.path()).fileName();
282 QCOMPARE(shareDir.isValid(), true);
283
284 //put some content in it
285 QString filename = createTempFile(shareDir.path(), "sometext.xt", "hello Samba");
286 QCOMPARE(filename.isEmpty(), false);
287
288 //save current samba shares list
289 QStringList currentShares = m_smbShares->listPlacesSync();
290 qDebug() << "currentShares:" << currentShares;
291
292 //get the supposed share name that is going to be created
293 QString shareDirName = SmbUserShare::proposedName(shareDir.path());
294
295 //make sure shareDirName does not exist in the current samba share list
296 QCOMPARE(shareDirName.isEmpty(), false);
297 QCOMPARE(shareDirName, m_curShareName);
298 bool exists = existsShare(currentShares,shareDirName);
299 QCOMPARE(exists, false);
300
301 //create the share
302 bool created = SmbUserShare::createShareForFolder(shareDir.path());
303 QCOMPARE(created, true);
304
305 // now make sure the new share is created
306 QStringList moreOneShare = m_smbShares->listPlacesSync();
307 exists = existsShare(moreOneShare, shareDirName);
308 qDebug() << "moreOneShare:" << moreOneShare;
309 QCOMPARE(exists, true);
310
311 //using share name
312 SmbUserShare::Access readAccess = SmbUserShare::getEveryoneAccess(m_curShareName);
313 bool isReadOnlyAccess = readAccess == SmbUserShare::Readonly;
314 QCOMPARE(isReadOnlyAccess, true);
315 }
316
317
318
319 void TestQSambaSuite::positive_createFullAccessUserShare()
320 {
321 //create a directory to share its content
322 QTemporaryDir shareDir (QDir::tempPath() + QDir::separator() + "positive_createFullAccessUserShare");
323 m_curShareName = QFileInfo(shareDir.path()).fileName();
324 QCOMPARE(shareDir.isValid(), true);
325
326 //put some content in it
327 QString filename = createTempFile(shareDir.path(), "sometext.xt", "hello Samba");
328 QCOMPARE(filename.isEmpty(), false);
329
330 //save current samba shares list
331 QStringList currentShares = m_smbShares->listPlacesSync();
332 qDebug() << "currentShares:" << currentShares;
333
334 //get the supposed share name that is going to be created
335 QString shareDirName = SmbUserShare::proposedName(shareDir.path());
336
337 //make sure shareDirName does not exist in the current samba share list
338 QCOMPARE(shareDirName.isEmpty(), false);
339 QCOMPARE(shareDirName, m_curShareName);
340 bool exists = existsShare(currentShares,shareDirName);
341 QCOMPARE(exists, false);
342
343 //create the share
344 bool created = SmbUserShare::createShareForFolder(shareDir.path(), SmbUserShare::ReadWrite);
345 QCOMPARE(created, true);
346
347 // now make sure the new share is created
348 QStringList moreOneShare = m_smbShares->listPlacesSync();
349 exists = existsShare(moreOneShare, shareDirName);
350 qDebug() << "moreOneShare:" << moreOneShare;
351 QCOMPARE(exists, true);
352
353 //using dir full path
354 SmbUserShare::Access readWrite = SmbUserShare::getEveryoneAccess(shareDir.path());
355 bool isReadOnlyAccess = readWrite == SmbUserShare::ReadWrite;
356 QCOMPARE(isReadOnlyAccess, true);
357
358 //let the share be removed by its path instead of the name
359 m_curShareName = shareDir.path();
360 }
361
362
363 void TestQSambaSuite::positive_itemInfoRoot()
364 {
365 SmbItemInfo root(LocationUrl::SmbURL);
366 QCOMPARE(root.isLocal() , false);
367 QCOMPARE(root.isHost(), false);
368 QCOMPARE(root.exists(), true);
369 QCOMPARE(root.isRelative(),false);
370 QCOMPARE(root.isAbsolute(), true);
371 QCOMPARE(root.isRoot(), true);
372 QCOMPARE(root.isSharedDir(), false);
373 QCOMPARE(root.isHost(), false);
374 QCOMPARE(root.isWorkGroup(), false);
375 QCOMPARE(root.isShare(), false);
376 QCOMPARE(root.absoluteFilePath(), LocationUrl::SmbURL);
377 QCOMPARE(root.fileName().isEmpty(), true);
378 QCOMPARE(root.absolutePath(), LocationUrl::SmbURL);
379 QCOMPARE(root.sharePath().isEmpty(), true);
380 }
381
382
383 void TestQSambaSuite::positive_itemInfoLocalHost()
384 {
385 SmbItemInfo localhost(LocationUrl::SmbURL + "localhost");
386 QCOMPARE(localhost.isLocal() , false);
387 QCOMPARE(localhost.isHost(), true);
388 QCOMPARE(localhost.exists(), true);
389 QCOMPARE(localhost.isRelative(),false);
390 QCOMPARE(localhost.isAbsolute(), true);
391 QCOMPARE(localhost.isRoot(), false);
392 QCOMPARE(localhost.isSharedDir(), false);
393 QCOMPARE(localhost.isWorkGroup(), false);
394 QCOMPARE(localhost.isShare(), false);
395 QCOMPARE(localhost.path(), LocationUrl::SmbURL);
396 QCOMPARE(localhost.filePath(), LocationUrl::SmbURL + "localhost");
397 QCOMPARE(localhost.absoluteFilePath(), LocationUrl::SmbURL + "localhost");
398 //for hosts sharePath() are the hostitself
399 QCOMPARE(localhost.sharePath(), localhost.absoluteFilePath());
400 }
401
402
403 void TestQSambaSuite::positive_itemInfoShare()
404 {
405 ShareCreationStatus share(createTempShare("positive_itemInfoShare"));
406 if (share.tempDir)
407 {
408 share.tempDir->setAutoRemove(true);
409 }
410 QCOMPARE(share.status, true);
411 QFileInfo file(share.fileContent.diskPathname);
412 QCOMPARE(file.exists(), true);
413
414 QString urlPath("smb://localhost/" + share.shareName);
415 //basic share/dir information
416 SmbItemInfo dirinfo(urlPath);
417 QCOMPARE(dirinfo.isLocal() , false);
418 QCOMPARE(dirinfo.isHost(), false);
419 QCOMPARE(dirinfo.exists(), true);
420 QCOMPARE(dirinfo.isRelative(),false);
421 QCOMPARE(dirinfo.isAbsolute(), true);
422 QCOMPARE(dirinfo.isDir(), true); // shares also are directories
423 QCOMPARE(dirinfo.isFile(), false);
424 QCOMPARE(dirinfo.isReadable(), true);
425 QCOMPARE(dirinfo.isWritable(), true);
426 QCOMPARE(dirinfo.isExecutable(), true);
427 QCOMPARE(dirinfo.isSelected(), false);
428 QCOMPARE(dirinfo.isSharedDir(), false);
429 QCOMPARE(dirinfo.isRoot(), false);
430 QCOMPARE(dirinfo.isHost(), false);
431 QCOMPARE(dirinfo.isWorkGroup(), false);
432 QCOMPARE(dirinfo.isShare(), true);
433 QCOMPARE(dirinfo.sharePath(), dirinfo.absoluteFilePath());
434 }
435
436 void TestQSambaSuite::positive_itemInfoCommonPermissions()
437 {
438 ShareCreationStatus share(createTempShare("iteminfoCommonPermission"));
439 if (share.tempDir)
440 {
441 share.tempDir->setAutoRemove(true);
442 }
443 QCOMPARE(share.status, true);
444 QFileInfo file(share.fileContent.diskPathname);
445 QCOMPARE(file.exists(), true);
446
447 //set a common permission to the file
448 QFile::Permissions myPermissions = QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser |
449 QFile::ReadGroup | QFile::ReadOther;
450 QCOMPARE(QFile::setPermissions(share.fileContent.diskPathname, myPermissions), true);
451
452 QString urlPath("smb://localhost/" + share.shareName);
453 QString url(urlPath + QDir::separator() + file.fileName());
454 //basic file information from share
455 SmbItemInfo fileinfo(url);
456 QCOMPARE(fileinfo.isLocal() , false);
457 QCOMPARE(fileinfo.isHost(), false);
458 QCOMPARE(fileinfo.exists(), true);
459 QCOMPARE(fileinfo.isRelative(),false);
460 QCOMPARE(fileinfo.isAbsolute(), true);
461 QCOMPARE(fileinfo.isDir(), false);
462 QCOMPARE(fileinfo.isFile(), true);
463 QCOMPARE(fileinfo.isReadable(), true);
464 QCOMPARE(fileinfo.isWritable(), true);
465 QCOMPARE(fileinfo.isExecutable(), false);
466 QCOMPARE(fileinfo.isSelected(), false);
467 QCOMPARE(fileinfo.isSharedDir(), false);
468 QCOMPARE(fileinfo.isRoot(), false);
469 QCOMPARE(fileinfo.isRemote(), true);
470 QCOMPARE(fileinfo.isSymLink(), false);
471 QCOMPARE(fileinfo.absolutePath(), urlPath);
472 QCOMPARE(fileinfo.filePath(), url);
473 QCOMPARE(fileinfo.fileName(), file.fileName());
474 QCOMPARE(fileinfo.absoluteFilePath(), url);
475 QCOMPARE(fileinfo.permissions(), myPermissions);
476 QCOMPARE(fileinfo.sharePath(), urlPath);
477 }
478
479
480 void TestQSambaSuite::negative_emptyItemInfo()
481 {
482 SmbItemInfo notSmb;
483 QCOMPARE(notSmb.isValid(), false);
484 QCOMPARE(notSmb.exists(), false);
485 QCOMPARE(notSmb.isHost(), false);
486 }
487
488
489 void TestQSambaSuite::negative_itemInfo()
490 {
491 SmbItemInfo notSmb("test://localhost");
492 QCOMPARE(notSmb.isValid(), false);
493 QCOMPARE(notSmb.exists(), false);
494
495 SmbItemInfo notHost("smb://__this_host_must_not_exist_");
496 QCOMPARE(notHost.isValid(), true);
497 QCOMPARE(notHost.exists(), false);
498
499 SmbItemInfo notShare("smb://localhost/__this_share_must_not_exist_");
500 QCOMPARE(notShare.isValid(), true);
501 QCOMPARE(notShare.exists(), false);
502
503 SmbPlaces shares;
504 QStringList existentShares = shares.listPlacesSync();
505 if (existentShares.count() > 0)
506 {
507 SmbItemInfo parent(existentShares.at(0));
508 QCOMPARE(parent.isValid(), true);
509 QCOMPARE(parent.exists(), true);
510 // it may not have permission to access
511 if (parent.isReadable())
512 {
513 QCOMPARE(parent.isShare(), true);
514 SmbItemInfo dir(existentShares.at(0) +
515 QDir::separator() +
516 "__this_dir_must_not_exist_");
517 QCOMPARE(dir.isValid(), true);
518 QCOMPARE(dir.exists(), false);
519 }
520 }
521 }
522
523 void TestQSambaSuite::negative_createShareDirDoesNotExist()
524 {
525 QDir d(QDir::homePath() + "___2323_hope_this_does_not_exist_");
526 while (d.exists())
527 {
528 d.setPath(d.path() + QChar('1'));
529 }
530
531 bool ret = SmbUserShare::createShareForFolder(d.path());
532
533 QCOMPARE(ret, false);
534 }
535
536
537 void TestQSambaSuite::negative_dirIterator()
538 {
539 SmbLocationDirIterator iterator1("Nome");
540 QCOMPARE(iterator1.hasNext() , false);
541
542 SmbLocationDirIterator iterator2("smb://localhost/_IT_MUST_NOT_EXIST_I_HOPE_");
543 QCOMPARE(iterator2.hasNext() , false);
544 }
545
546
547 void TestQSambaSuite::positive_dirIterator()
548 {
549 ShareCreationStatus share(createTempShare("positive_dirIterator"));
550 if (share.tempDir)
551 {
552 share.tempDir->setAutoRemove(true);
553 }
554 QCOMPARE(share.status, true);
555 QFileInfo file(share.fileContent.diskPathname);
556 QCOMPARE(file.exists(), true);
557
558 //create a second directory inside the temporary share
559 QString secondDirStr("secondDir");
560 QString secondElementPath = share.sharedDirPath + QDir::separator() + secondDirStr;
561 QCOMPARE(QDir().mkpath(secondElementPath), true);
562
563 //create a file inside secondElementPath
564 QString secondElementContent(secondElementPath + QDir::separator() + "test.readme");
565 QFile f(secondElementContent);
566 QCOMPARE(f.open(QFile::WriteOnly), true);
567 f.close();
568
569 //recursive 3 items
570 int counter;
571 SmbLocationDirIterator tree(share.url, QDir::AllEntries, QDirIterator::Subdirectories);
572 for(counter=0; tree.hasNext() ; ++counter)
573 {
574 tree.next();
575 }
576 QCOMPARE(counter, 3);
577
578 //using a mask
579 QStringList nameFilter = QStringList() << "*.readme";
580 SmbLocationDirIterator onlyReadme(share.url, nameFilter,QDir::AllEntries, QDirIterator::Subdirectories);
581 for(counter=0; onlyReadme.hasNext() ; ++counter)
582 {
583 onlyReadme.next();
584 }
585 QCOMPARE(counter, 1);
586
587 //2 items
588 SmbLocationDirIterator lowLevelIterator(share.url);
589 for(counter=0; lowLevelIterator.hasNext() ; ++counter)
590 {
591 lowLevelIterator.next();
592 }
593 QCOMPARE(counter, 2);
594
595 //2 more "." and "."
596 SmbLocationDirIterator lowLevelIterator2(share.url,QDir::AllEntries | QDir::Hidden);
597 for(counter=0; lowLevelIterator2.hasNext() ; ++counter)
598 {
599 lowLevelIterator2.next();
600 }
601 QCOMPARE(counter, 4);
602
603 //2 more ".."
604 bool dotdot = false;
605 SmbLocationDirIterator lowLevelIterator3(share.url,QDir::AllEntries | QDir::Hidden | QDir::NoDot);
606 for(counter=0; lowLevelIterator3.hasNext() ; ++counter)
607 {
608 lowLevelIterator3.next();
609 if (lowLevelIterator3.fileName() == "..")
610 {
611 dotdot = true;
612 }
613 }
614 QCOMPARE(counter, 3);
615 QCOMPARE(dotdot, true);
616
617 //2 more "."
618 bool dot = false;
619 SmbLocationDirIterator lowLevelIterator4(share.url,QDir::AllEntries | QDir::Hidden | QDir::NoDotDot);
620 for(counter=0; lowLevelIterator4.hasNext() ; ++counter)
621 {
622 lowLevelIterator4.next();
623 if (lowLevelIterator4.fileName() == ".")
624 {
625 dot = true;
626 }
627 }
628 QCOMPARE(counter, 3);
629 QCOMPARE(dot, true);
630
631 bool secondDir = false;
632 SmbLocationDirIterator dirOnly(share.url,QDir::Dirs | QDir::NoDotAndDotDot);
633 for(counter=0; dirOnly.hasNext() ; ++counter)
634 {
635 dirOnly.next();
636 if (dirOnly.fileName() == secondDirStr)
637 {
638 secondDir = true;
639 }
640 }
641 QCOMPARE(counter, 1);
642 QCOMPARE(secondDir, true);
643
644 bool fileContent = false;
645 SmbLocationDirIterator fileOnly(share.url,QDir::Files);
646 for(counter=0; fileOnly.hasNext() ; ++counter)
647 {
648 fileOnly.next();
649 if (fileOnly.fileName() == QFileInfo(share.fileContent.diskPathname).fileName())
650 {
651 fileContent = true;
652 }
653 }
654 QCOMPARE(counter, 1);
655 QCOMPARE(fileContent, true);
656 }
657
658
659 /*!
660 * This is not a test case
661 *
662 * It intends to create a real user share in the local host
663 */
664 bool TestQSambaSuite::createPermanentShare(const QString &path, bool fullAccess)
665 {
666 SmbUserShare::Access access = fullAccess ? SmbUserShare::ReadWrite
667 : SmbUserShare::Readonly;
668 SmbUserShare::createShareForFolder(path, access);
669 SmbUserShare::UserShareFile ret = SmbUserShare::search(path);
670 return ret.exists();
671 }
672
673
674 /*!
675 * \brief TestQSambaSuite::openPermanenteShare()
676 *
677 * It is not a test case, just shows some information about an URL
678 *
679 * \param smb_path
680 * \return
681 */
682 bool TestQSambaSuite::openPermanenteShare(const QString &smb_path)
683 {
684 SmbUtil smb;
685 struct stat st;
686 bool ok = smb.getStatInfo(smb_path, &st) != -1;
687 qDebug() << st.st_mode
688 << st.st_size
689 << st.st_rdev
690 << st.st_mtime
691 << st.st_uid
692 << st.st_blocks
693 << st.st_ino
694 ;
695 return ok;
696 }
697
698 /*!
699 * \brief TestQSambaSuite::listLocalhost()
700 *
701 * It is not a test case
702 */
703 bool TestQSambaSuite::listLocalhost()
704 {
705 QString smb_path("smb://localhost");
706 SmbLocationDirIterator listIterator(smb_path);
707 QStringList shares;
708 while (listIterator.hasNext())
709 {
710 shares.append(listIterator.next());
711 }
712 qDebug() << shares;
713 return shares.count() > 0 ? true : false;
714 }
715
716
717 bool TestQSambaSuite::listRecursive()
718 {
719 SmbUtil smb;
720 QStringList all = smb.listContent(LocationUrl::SmbURL, true);
721 if (all.count() == 0)
722 {
723 return false;
724 }
725 for (int counter=0 ; counter < all.count(); ++ counter)
726 {
727 qDebug() << all.at(counter);
728 }
729 return true;
730 }
731
732
733 void TestQSambaSuite::unit_QFile_rename()
734 {
735 ShareCreationStatus share(createTempShare("unit_QFile_rename"));
736 if (share.tempDir)
737 {
738 share.tempDir->setAutoRemove(true);
739 }
740 QCOMPARE(share.status, true);
741
742 //negative tests
743 //empty object
744 SmbLocationItemFile fileNameEmptyDoesNotExist;
745 QString newName("this_was_renamed.txt");
746 QCOMPARE(fileNameEmptyDoesNotExist.rename(newName), false);
747 QCOMPARE(fileNameEmptyDoesNotExist.rename(QLatin1String(0), newName), false);
748 //orig file does not exist
749 QString nameDoesNotExist("_it_must_not_exist");
750 SmbSharedPathAccess item = share.createPathForItem(nameDoesNotExist);
751 QFileInfo diskFileContent(item.diskPathname);
752 QCOMPARE(diskFileContent.exists(), false);
753 SmbLocationItemFile fileNameDoesNotExist(item.smbUrl);
754 QCOMPARE(fileNameDoesNotExist.rename(newName), false);
755 QCOMPARE(fileNameDoesNotExist.rename(nameDoesNotExist, newName), false);
756
757 //positive tests
758 SmbSharedPathAccess newItem = share.createPathForItem(newName);
759 QFileInfo newFileInfo(newItem.diskPathname);
760 //make sure target does not exist
761 QCOMPARE(newFileInfo.exists(), false);
762 //now rename
763 SmbLocationItemFile fileNameExists(share.fileContent.smbUrl);
764 QCOMPARE(fileNameExists.rename(newItem.smbUrl), true);
765 //now target must exist in local file system
766 QCOMPARE(newFileInfo.exists(), true);
767 diskFileContent.setFile(share.fileContent.diskPathname);
768 //make sure orignal file no longer exist
769 QCOMPARE(diskFileContent.exists(), false);
770 //now back to original name using an empty object
771 SmbLocationItemFile empty;
772 QCOMPARE(empty.rename(newItem.smbUrl, share.fileContent.smbUrl), true);
773 //make sure orignal exists again
774 QCOMPARE(diskFileContent.exists(), true);
775 }
776
777
778 void TestQSambaSuite::unit_QFile_remove()
779 {
780 ShareCreationStatus share(createTempShare("unit_QFile_remove"));
781 if (share.tempDir)
782 {
783 share.tempDir->setAutoRemove(true);
784 }
785 QCOMPARE(share.status, true);
786
787 //negative tests
788 //empty object
789 SmbLocationItemFile fileNameEmptyDoesNotExist;
790 QCOMPARE(fileNameEmptyDoesNotExist.remove(), false);
791 //file name does not exist
792 SmbSharedPathAccess item = share.createPathForItem("_it_must_not_exist");
793 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
794 SmbLocationItemFile fileNameDoesNotExist(item.smbUrl);
795 QCOMPARE(fileNameDoesNotExist.remove(), false);
796 QFileInfo diskFileInfo(share.fileContent.diskPathname);
797 // //now try to remove a file which does not have write permission
798 // QCOMPARE(diskFileInfo.exists(), true);
799 // QFile::Permissions originalPermissions = diskFileInfo.permissions();
800 // QCOMPARE(QFile::setPermissions(share.fileContent.diskPathname, QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther), true);
801 // SmbLocationItemFile existentFileButNoWritePermission(share.fileContent.smbUrl);
802 // //remove must fail
803 // QCOMPARE(existentFileButNoWritePermission.remove(), false);
804 // QCOMPARE(errno, EPERM);
805 // QCOMPARE(QFile::setPermissions(share.fileContent.diskPathname, originalPermissions), true);
806
807 //positive test, remove fileContent
808 SmbLocationItemFile existentFile(share.fileContent.smbUrl);
809 SmbItemInfo smbExistentItem(share.fileContent.smbUrl);
810 QCOMPARE(smbExistentItem.exists(), true);
811 QCOMPARE(existentFile.remove(), true);
812 SmbItemInfo smbItem(share.fileContent.smbUrl);
813 //samba url for this file must not exist
814 QCOMPARE(smbItem.exists(), false);
815 //now origin file in file system must no longer exist
816 QCOMPARE(diskFileInfo.exists(), false);
817 }
818
819
820 void TestQSambaSuite::unit_QFile_open()
821 {
822 ShareCreationStatus share(createTempShare("unit_QFile_open"));
823 if (share.tempDir)
824 {
825 share.tempDir->setAutoRemove(true);
826 }
827 QCOMPARE(share.status, true);
828
829 //negative tests
830 SmbLocationItemFile fileNameEmptyDoesNotExist;
831 QCOMPARE(fileNameEmptyDoesNotExist.open(QFile::WriteOnly), false);
832 SmbSharedPathAccess item = share.createPathForItem("_it_must_not_exist");
833 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
834 SmbLocationItemFile fileNameDoesNotExist(item.smbUrl);
835 QCOMPARE(fileNameDoesNotExist.open(QFile::ReadOnly), false);
836
837 // ---- positive tests
838 //create a smb file using open
839 item = share.createPathForItem("now_it_must_be_created");
840 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
841 SmbLocationItemFile fileCreated(item.smbUrl);
842 QCOMPARE(fileCreated.open(QFile::WriteOnly), true);
843 if (QFileInfo(item.diskPathname).exists() == false)
844 {
845 fileCreated.close(); // force to close if necessary
846 }
847 QCOMPARE(fileCreated.isOpen(), true);
848 //now check in the local disk to see if it was really created
849 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
850 //as QFile it closes itself in the destructor
851 }
852
853
854 void TestQSambaSuite::unit_QFile_read()
855 {
856 ShareCreationStatus share(createTempShare("unit_QFile_read"));
857 if (share.tempDir)
858 {
859 share.tempDir->setAutoRemove(true);
860 }
861 QCOMPARE(share.status, true);
862
863 //negative tests
864 SmbLocationItemFile fileNameEmptyDoesNotExist;
865 char buffer[100];
866
867 //get Qt read return when there is no file
868 qint64 qt_read_no_file = QFile().read(buffer, sizeof(buffer));
869
870 QCOMPARE(fileNameEmptyDoesNotExist.read(buffer, sizeof(buffer)), qt_read_no_file);
871 SmbSharedPathAccess item = share.createPathForItem("_it_must_not_exist");
872 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
873 SmbLocationItemFile fileMustNotExist(item.smbUrl);
874 QCOMPARE(fileMustNotExist.read(buffer, sizeof(buffer)), qt_read_no_file);
875
876 //positive test
877 //create a file in the local folder which is shared by Samba
878 item = share.createPathForItem("created_in_the_disk.txt");
879 QFile inDisk(item.diskPathname);
880 QCOMPARE(inDisk.open(QFile::WriteOnly), true);
881 QByteArray diskContent("This a simple content used to test reading files from samba: ");
882 QByteArray initialDiskContent(diskContent);
883 QByteArray moreDiskContent(100, 'a');
884 diskContent += moreDiskContent;
885 QCOMPARE(inDisk.write(diskContent), (qint64)diskContent.size());
886 inDisk.close();
887 //now read from Samba
888 SmbLocationItemFile existentFile(item.smbUrl);
889 QCOMPARE(existentFile.open(QFile::ReadOnly), true);
890 char smbBuffer [diskContent.size() + 100];
891 //before first read
892 QCOMPARE(existentFile.atEnd(), false);
893 qint64 bytesRead = existentFile.read(smbBuffer, (qint64)initialDiskContent.size());
894 qint64 expectedRead = (qint64)initialDiskContent.size();
895 QCOMPARE(bytesRead, expectedRead);
896 QByteArray smbContent(smbBuffer,initialDiskContent.size());
897 //compare the intial content
898 QCOMPARE(smbContent, initialDiskContent);
899 //before second read
900 QCOMPARE(existentFile.atEnd(), false);
901 expectedRead = (qint64) (diskContent.size() - initialDiskContent.size());
902 //read remaining data in the file
903 bytesRead = existentFile.read(smbBuffer, expectedRead);
904 QCOMPARE(bytesRead, expectedRead);
905 QByteArray moreSmbContent(smbBuffer, expectedRead);
906 smbContent += moreSmbContent;
907 //now compare the whole content
908 QCOMPARE(smbContent, diskContent);
909 //now atEnd() must be true
910 QCOMPARE(existentFile.atEnd(), true);
911 }
912
913
914 void TestQSambaSuite::unit_QFile_write()
915 {
916 ShareCreationStatus share(createTempShare("unit_QFile_write"));
917 if (share.tempDir)
918 {
919 share.tempDir->setAutoRemove(true);
920 }
921 QCOMPARE(share.status, true);
922
923 //negative tests
924 SmbLocationItemFile fileNameEmptyDoesNotExist;
925 QByteArray someContent("This is a simple content\n");
926 //get Qt write return when there is no file
927 qint64 qt_write_no_file = QFile().write(someContent.constData(), (qint64)someContent.size());
928 QCOMPARE(fileNameEmptyDoesNotExist.write(someContent.constData(), (qint64)someContent.size()), qt_write_no_file);
929 SmbSharedPathAccess item = share.createPathForItem("_it_must_not_exist");
930 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
931 SmbLocationItemFile fileMustNotExist(item.smbUrl);
932 QCOMPARE(fileMustNotExist.write(someContent.constData(), (qint64)someContent.size()), qt_write_no_file);
933
934 //positive tests
935 //work with 2 items saving in Samba and saving in local file system
936 SmbSharedPathAccess items[2];
937 items[0] = share.createPathForItem("first_item.txt");
938 items[1] = share.createPathForItem("second_item.txt");
939 QByteArray content; // starts empty
940 QFile::OpenMode openMode = QFile::WriteOnly; //after first write it receives Append mode
941 const int diskIndex = 0;
942 const int smbindex = 1;
943 QByteArray savedContent;
944 for(int counter = 0; counter < 4; ++counter, content += someContent)
945 {
946 QFile diskFile(items[diskIndex].diskPathname);
947 SmbLocationItemFile smbFile(items[smbindex].smbUrl);
948 //open both files
949 QCOMPARE(diskFile.open(openMode), true);
950 QCOMPARE(smbFile.open(openMode), true);
951 //first write should return 0 as content is empty
952 qint64 toSave = content.size();
953 qint64 wrote = diskFile.write(content.constData(), toSave);
954 QCOMPARE(wrote, toSave);
955 wrote = smbFile.write(content.constData(), toSave);
956 QCOMPARE(wrote, toSave);
957 savedContent += content;
958 diskFile.close();
959 smbFile.close();
960 if (counter > 0)
961 {
962 openMode = QFile::Append;
963 }
964 }
965 //now check size and content
966 QFile diskFile(items[diskIndex].diskPathname);
967 SmbLocationItemFile smbFile(items[smbindex].smbUrl);
968 QCOMPARE(smbFile.size(), diskFile.size());
969 QCOMPARE(diskFile.open(QFile::ReadOnly), true);
970 QCOMPARE(smbFile.open(QFile::ReadOnly), true);
971 //read file from disk, check the content
972 char buffer [1024];
973 qint64 gotBytes = diskFile.read(buffer, sizeof(buffer));
974 QCOMPARE((qint64)savedContent.size(), gotBytes);
975 QByteArray otherContent(buffer, (int)gotBytes);
976 QCOMPARE(otherContent, savedContent);
977 //read SMB file from disk, check the content
978 gotBytes = smbFile.read(buffer, sizeof(buffer));
979 QCOMPARE((qint64)savedContent.size(), gotBytes);
980 otherContent.setRawData(buffer, (int)gotBytes);
981 QCOMPARE(otherContent, savedContent);
982 }
983
984
985 void TestQSambaSuite::unit_QFile_atEnd()
986 {
987 ShareCreationStatus share(createTempShare("unit_QFile_atEnd"));
988 if (share.tempDir)
989 {
990 share.tempDir->setAutoRemove(true);
991 }
992 QCOMPARE(share.status, true);
993
994 //negative tests
995 SmbLocationItemFile fileNameEmptyDoesNotExist;
996 QCOMPARE(fileNameEmptyDoesNotExist.atEnd(), true);
997 SmbSharedPathAccess item = share.createPathForItem("_it_must_not_exist");
998 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
999 SmbLocationItemFile fileMustNotExist(item.smbUrl);
1000 QCOMPARE(fileMustNotExist.atEnd(), true);
1001 QFileInfo fileContentInfo(share.fileContent.diskPathname);
1002 QCOMPARE(fileContentInfo.exists(), true);
1003 SmbLocationItemFile existentFile(share.fileContent.smbUrl);
1004 //first at end when file is still closed
1005 QCOMPARE(existentFile.atEnd(), true);
1006 QCOMPARE(existentFile.open(QFile::ReadOnly), true);
1007 char buffer [fileContentInfo.size()];
1008 QCOMPARE(existentFile.read(buffer, (qint64)10), (qint64)10);
1009 //file is opened and not at end
1010 QCOMPARE(existentFile.atEnd(), false);
1011 QVERIFY(existentFile.read(buffer, fileContentInfo.size()) > 0);
1012 QCOMPARE(existentFile.atEnd(), true);
1013 }
1014
1015
1016 void TestQSambaSuite::unit_QFile_size()
1017 {
1018 ShareCreationStatus share(createTempShare("unit_QFile_size"));
1019 if (share.tempDir)
1020 {
1021 share.tempDir->setAutoRemove(true);
1022 }
1023 QCOMPARE(share.status, true);
1024
1025 //negative tests
1026 //empty objects
1027 qint64 qFile_size_for_disk = QFileInfo().size();
1028 SmbLocationItemFile fileNameEmptyDoesNotExist;
1029 QCOMPARE(fileNameEmptyDoesNotExist.size(), qFile_size_for_disk);
1030 //file does not exist
1031 SmbSharedPathAccess item = share.createPathForItem("it_must_not_exist.txt");
1032 QFileInfo diskFileInfo(item.diskPathname);
1033 QCOMPARE(diskFileInfo.exists(), false);
1034 qFile_size_for_disk = diskFileInfo.size();
1035 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1036 SmbLocationItemFile fileMustNotExist(item.smbUrl);
1037 QCOMPARE(fileMustNotExist.size(), qFile_size_for_disk);
1038
1039 //positive tests
1040 diskFileInfo.setFile(share.fileContent.diskPathname);
1041 QCOMPARE(diskFileInfo.exists(), true);
1042 qFile_size_for_disk = diskFileInfo.size();
1043 QVERIFY(qFile_size_for_disk > 0);
1044 SmbLocationItemFile existentFile(share.fileContent.smbUrl);
1045 //first time the file is closed
1046 QCOMPARE(existentFile.size(), qFile_size_for_disk);
1047 QCOMPARE(existentFile.open(QFile::ReadOnly), true);
1048 //second time the file is opened
1049 QCOMPARE(existentFile.size(), qFile_size_for_disk);
1050 //now append data using local file system
1051 QFile moreDataFile(share.fileContent.diskPathname);
1052 QCOMPARE(moreDataFile.open(QFile::Append), true);
1053 QByteArray moreData("just a more bytes");
1054 QCOMPARE(moreDataFile.write(moreData), (qint64)moreData.size());
1055 moreDataFile.close();
1056 //other QFileInfo object to get new information
1057 QFileInfo newDiskFileInfo(share.fileContent.diskPathname);
1058 QVERIFY(newDiskFileInfo.size() > qFile_size_for_disk); // has more data now
1059 QCOMPARE(existentFile.size(), newDiskFileInfo.size());
1060 }
1061
1062
1063 void TestQSambaSuite::unit_QFile_isOpen()
1064 {
1065 ShareCreationStatus share(createTempShare("unit_QFile_isOpen"));
1066 if (share.tempDir)
1067 {
1068 share.tempDir->setAutoRemove(true);
1069 }
1070 QCOMPARE(share.status, true);
1071
1072 //negative tests
1073 SmbLocationItemFile fileNameEmptyDoesNotExist;
1074 QCOMPARE(fileNameEmptyDoesNotExist.isOpen(), false);
1075 SmbSharedPathAccess item = share.createPathForItem("_it_must_not_exist");
1076 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1077 SmbLocationItemFile fileMustNotExist(item.smbUrl);
1078 QCOMPARE(fileMustNotExist.isOpen(), false);
1079
1080 //positive tests
1081 SmbLocationDirIterator fileContentIterator(share.url, QDir::Files);
1082 QCOMPARE(fileContentIterator.hasNext(), true);
1083 fileContentIterator.next();
1084 DirItemInfo iteminfo = fileContentIterator.fileInfo();
1085 QCOMPARE(iteminfo.exists(), true);
1086 SmbLocationItemFile smbFile(iteminfo.urlPath());
1087 QCOMPARE(smbFile.isOpen(), false);
1088 QCOMPARE(smbFile.open(QFile::ReadOnly), true);
1089 QCOMPARE(smbFile.isOpen(), true);
1090 }
1091
1092
1093 void TestQSambaSuite::unit_QFile_permissions()
1094 {
1095 ShareCreationStatus share(createTempShare("unit_QFile_permissions"));
1096 if (share.tempDir)
1097 {
1098 share.tempDir->setAutoRemove(true);
1099 }
1100 QCOMPARE(share.status, true);
1101
1102 //negative tests
1103 //empty objects
1104 QFile::Permissions qt_empty_permissions = QFileInfo().permissions();
1105 SmbLocationItemFile fileNameEmptyDoesNotExist;
1106 QCOMPARE(fileNameEmptyDoesNotExist.permissions(), qt_empty_permissions);
1107 //file does not exist
1108 SmbSharedPathAccess item = share.createPathForItem("it_must_not_exist.txt");
1109 QFileInfo diskFileInfo(item.diskPathname);
1110 QCOMPARE(diskFileInfo.exists() ,false);
1111 SmbLocationItemFile fileNameDoesNotExist(item.smbUrl);
1112 QCOMPARE(fileNameDoesNotExist.permissions(), qt_empty_permissions);
1113
1114 //positive test, change in the local disk , check for permission in Samba
1115 QFile::Permissions perm_readOnly = QFile::ReadOwner | QFile::ReadGroup | QFile::ReadOther | QFile::ReadUser;
1116
1117 QFileInfo diskExistentFile(share.fileContent.diskPathname);
1118 QCOMPARE(diskExistentFile.exists(), true);
1119 //first make sure permissions are different
1120 QVERIFY(diskExistentFile.permissions() != perm_readOnly);
1121 //set permission using file in the local file system
1122 QCOMPARE(QFile::setPermissions(share.fileContent.diskPathname, perm_readOnly), true);
1123 //check the permission in the local file system
1124 QTest::qWait(100);
1125 QFile::Permissions curr_permissions = QFile(share.fileContent.diskPathname).permissions();
1126 QCOMPARE(curr_permissions, perm_readOnly);
1127 //now the same permission must come using Samba
1128 SmbLocationItemFile smbExistentFile(share.fileContent.smbUrl);
1129 QFile::Permissions curr_smb_ermissions = smbExistentFile.permissions();
1130 QCOMPARE(curr_smb_ermissions, perm_readOnly);
1131 }
1132
1133
1134 void TestQSambaSuite::unit_QFile_setPermissions()
1135 {
1136 ShareCreationStatus share(createTempShare("unit_QFile_setPermissions"));
1137 if (share.tempDir)
1138 {
1139 share.tempDir->setAutoRemove(true);
1140 }
1141 QCOMPARE(share.status, true);
1142
1143 //negative tests
1144 //empty objects
1145 QFile::Permissions qt_permissions = QFile::ReadOwner | QFile::ReadUser | QFile::WriteOwner | QFile::WriteUser;
1146 SmbLocationItemFile fileNameEmptyDoesNotExist;
1147 QCOMPARE(fileNameEmptyDoesNotExist.setPermissions(qt_permissions), false);
1148 //file does not exist
1149 SmbSharedPathAccess item = share.createPathForItem("it_must_not_exist.txt");
1150 QFileInfo diskFileInfo(item.diskPathname);
1151 QCOMPARE(diskFileInfo.exists() ,false);
1152 SmbLocationItemFile fileNameDoesNotExist(item.smbUrl);
1153 QCOMPARE(fileNameDoesNotExist.setPermissions(qt_permissions), false);
1154
1155 QCOMPARE(fileNameDoesNotExist.setPermissions(share.fileContent.smbUrl, qt_permissions), true);
1156 SmbLocationItemFile smbFile(share.fileContent.smbUrl);
1157 QCOMPARE(smbFile.setPermissions(qt_permissions), true);
1158 }
1159
1160
1161 void TestQSambaSuite::unit_QDir_exists()
1162 {
1163 ShareCreationStatus share(createTempShare("unit_QDir_mkdir"));
1164 if (share.tempDir)
1165 {
1166 share.tempDir->setAutoRemove(true);
1167 }
1168 QCOMPARE(share.status, true);
1169
1170 //negative tests
1171 SmbLocationItemDir directoryEmptyName;
1172 QCOMPARE(directoryEmptyName.exists(), false);
1173 SmbSharedPathAccess item = share.createPathForItem("dirName");
1174 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1175 SmbLocationItemDir directoryDoesNotExist(item.smbUrl);
1176 QCOMPARE(directoryDoesNotExist.exists(), false);
1177
1178 //positve test
1179 //item.smbUrl does not exist, check above
1180 //create it in the disk
1181 QCOMPARE(QDir().mkpath(item.diskPathname), true);
1182 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1183 SmbLocationItemDir directoyExists(item.smbUrl);
1184 QCOMPARE(directoyExists.exists(), true);
1185 }
1186
1187
1188
1189
1190 void TestQSambaSuite::unit_QDir_mkdir()
1191 {
1192 ShareCreationStatus share(createTempShare("unit_QDir_mkdir"));
1193 if (share.tempDir)
1194 {
1195 share.tempDir->setAutoRemove(true);
1196 }
1197 QCOMPARE(share.status, true);
1198
1199 //negative tests
1200 SmbLocationItemDir directoryDoesNotExist;
1201 QCOMPARE(directoryDoesNotExist.mkdir("relativeDir"), false);
1202 QCOMPARE(directoryDoesNotExist.mkdir("smb://localhost/share_does_not_exist"), false);
1203 QCOMPARE(directoryDoesNotExist.mkdir("smb://localhost/share_does_not_exist/invalid_share"), false);
1204
1205 //positive tests
1206
1207 //create relative directory
1208 QString relativeName("relativeDir");
1209 SmbSharedPathAccess item = share.createPathForItem(relativeName);
1210 SmbLocationItemDir testRelative(share.url);
1211 SmbItemInfo smbDirBeforeCreation(item.smbUrl);
1212 //make sure directory does not exist
1213 QCOMPARE(smbDirBeforeCreation.exists(), false);
1214 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1215 //create directory using Samba
1216 QCOMPARE(testRelative.mkdir(relativeName), true);
1217 //make sure it exists now
1218 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1219 SmbItemInfo smbDirAfterCreation(item.smbUrl);
1220 QCOMPARE(smbDirAfterCreation.exists(), true);
1221
1222 //directory already exists, should return true
1223 QCOMPARE(testRelative.mkdir(relativeName), true);
1224
1225 //mkdir using absolute path
1226 item = share.createPathForItem("using_absolute_path");
1227 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1228 SmbLocationItemDir testAbsoluteDir;
1229 QCOMPARE(testAbsoluteDir.mkdir(item.smbUrl), true);
1230 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1231 }
1232
1233
1234
1235 void TestQSambaSuite::unit_QDir_mkpath()
1236 {
1237 ShareCreationStatus share(createTempShare("unit_QDir_mkpath"));
1238 if (share.tempDir)
1239 {
1240 share.tempDir->setAutoRemove(true);
1241 }
1242 QCOMPARE(share.status, true);
1243
1244 //create multiple paths
1245 QStringList paths("path1");
1246 paths.append("path2");
1247 paths.append("path3");
1248 //item will have smbUrl=smb://localhost/shareName/path1/path2/path3 and diskPathname=/tmp/tmpdir/path1/path2/path3
1249 //using absolute path
1250 SmbSharedPathAccess item = share.createPathForItems(paths);
1251 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1252 SmbLocationItemDir testAbsoluteDir;
1253 QCOMPARE(testAbsoluteDir.mkpath(item.smbUrl), true);
1254 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1255 //test when it already exists
1256 QCOMPARE(testAbsoluteDir.mkpath(item.smbUrl), true);
1257
1258 //relative paths
1259 paths.clear();
1260 paths.append("relative1");
1261 paths.append("relative2");
1262 item = share.createPathForItems(paths);
1263 SmbLocationItemDir relativeDir(share.url);
1264 QString multiplePaths = paths.join(QDir::separator());
1265 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1266 QCOMPARE(relativeDir.mkpath(multiplePaths), true);
1267 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1268 }
1269
1270
1271
1272 void TestQSambaSuite::unit_QDir_rmdir()
1273 {
1274 ShareCreationStatus share(createTempShare("unit_QDir_rmdir"));
1275 if (share.tempDir)
1276 {
1277 share.tempDir->setAutoRemove(true);
1278 }
1279 QCOMPARE(share.status, true);
1280
1281 //negative tests
1282 SmbLocationItemDir directoryEmptyName;
1283 QCOMPARE(directoryEmptyName.rmdir("none"), false);
1284 QString dirName("dirName");
1285 SmbSharedPathAccess item = share.createPathForItem(dirName);
1286 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1287 SmbLocationItemDir directoryDoesNotExist(share.url);
1288 QCOMPARE(directoryDoesNotExist.rmdir(dirName), false);
1289
1290 //create a directory
1291 SmbLocationItemDir directory(share.url);
1292 //create using relative
1293 QCOMPARE(directory.mkdir(dirName), true);
1294 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1295 //remove the relative directory
1296 QCOMPARE(directory.rmdir(dirName), true);
1297 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1298 //create it again and now remove using absolute url
1299 QCOMPARE(directory.mkdir(dirName), true);
1300 QCOMPARE(QFileInfo(item.diskPathname).exists(), true);
1301 SmbLocationItemDir emptyObject;
1302 QCOMPARE(emptyObject.rmdir(item.smbUrl), true);
1303 QCOMPARE(QFileInfo(item.diskPathname).exists(), false);
1304 }
1305
1306
1307 void TestQSambaSuite::unit_QDirIterator_path()
1308 {
1309 ShareCreationStatus share(createTempShare("unit_QDirIterator_path"));
1310 if (share.tempDir)
1311 {
1312 share.tempDir->setAutoRemove(true);
1313 }
1314 QCOMPARE(share.status, true);
1315 QStringList paths = QStringList() << "path1" << "anotherPath" << "testdir" << "passThru" << "haha";
1316 QStringList files = QStringList() << "text.txt" << "hiper.html" << "document.odf" << "spread.odx" << "tx.list";
1317
1318 QStringList curPath;
1319 for (int counter=0; counter < paths.count() && counter < files.count(); ++counter)
1320 {
1321 curPath.append(paths.at(counter));
1322 SmbSharedPathAccess item = share.createPathForItems(curPath);
1323 QCOMPARE(QDir().mkpath(item.diskPathname), true);
1324 QString fileFullPath = item.diskPathname + QDir::separator() + files.at(counter);
1325 QFile file(fileFullPath);
1326 QCOMPARE(file.open(QFile::WriteOnly), true);
1327 file.close();
1328 }
1329
1330 QDir::Filters dirFilter = QDir::NoDotAndDotDot;
1331 QDir::Filters moreFilter[] = { QDir::AllEntries, QDir::Hidden, QDir::System };
1332
1333 for (uint counter=0; counter < sizeof(moreFilter)/sizeof(moreFilter[0]); ++counter)
1334 {
1335 dirFilter |= moreFilter[counter];
1336 QDirIterator dirIterator(share.sharedDirPath, dirFilter, QDirIterator::Subdirectories);
1337 SmbLocationDirIterator smbIterator(share.url, dirFilter, QDirIterator::Subdirectories);
1338 QStringList listDir;
1339 QStringList listSmb;
1340 qDebug() << "\n=================";
1341 while (dirIterator.hasNext() && smbIterator.hasNext())
1342 {
1343 qDebug() << dirIterator.next() << smbIterator.next();
1344 listDir.append(dirIterator.fileName());
1345 listSmb.append(smbIterator.fileName());
1346 }
1347 QCOMPARE(listDir, listSmb);
1348 }
1349 }
1350
1351
1352 void TestQSambaSuite::positive_statvfs()
1353 {
1354 ShareCreationStatus share(createTempShare("positive_statvfs"));
1355 if (share.tempDir)
1356 {
1357 share.tempDir->setAutoRemove(true);
1358 }
1359 QCOMPARE(share.status, true);
1360
1361 struct statvfs diskVfs;
1362 struct statvfs smbVfs;
1363 ::memset(&diskVfs, 0, sizeof(struct statvfs));
1364 ::memset(&smbVfs, 0, sizeof(struct statvfs));
1365
1366 //using a file that exists
1367 SmbUtil smb;
1368 QCOMPARE((int)smb.getStatvfsInfo(share.fileContent.smbUrl, &smbVfs), 0);
1369 QCOMPARE(::statvfs(share.fileContent.diskPathname.toLocal8Bit().constData(), &diskVfs), 0);
1370 QCOMPARE(smbVfs.f_blocks, diskVfs.f_blocks);
1371 QCOMPARE(smbVfs.f_ffree, diskVfs.f_ffree);
1372 QCOMPARE(smbVfs.f_files, diskVfs.f_files);
1373 QCOMPARE(smbVfs.f_bsize, diskVfs.f_bsize);
1374 QVERIFY( qAbs(smbVfs.f_bfree - diskVfs.f_bfree) < 10 );
1375
1376 //using a directory
1377 ::memset(&diskVfs, 0, sizeof(struct statvfs));
1378 ::memset(&smbVfs, 0, sizeof(struct statvfs));
1379 QCOMPARE((int)smb.getStatvfsInfo(share.url, &smbVfs), 0);
1380 QCOMPARE(::statvfs(share.sharedDirPath.toLocal8Bit().constData(), &diskVfs), 0);
1381 QCOMPARE(smbVfs.f_blocks, diskVfs.f_blocks);
1382 QCOMPARE(smbVfs.f_ffree, diskVfs.f_ffree);
1383 QCOMPARE(smbVfs.f_files, diskVfs.f_files);
1384 QCOMPARE(smbVfs.f_bsize, diskVfs.f_bsize);
1385 QVERIFY( qAbs(smbVfs.f_bfree - diskVfs.f_bfree) < 10 );
1386 }