2 * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * @author Tomasz Iwanek (t.iwanek@samsung.com)
25 #include <dpl/test/test_runner.h>
26 #include <dpl/scoped_dir.h>
27 #include <dpl/utils/path.h>
28 #include <dpl/foreach.h>
29 #include <dpl/log/wrt_log.h>
30 #include <dpl/binary_queue.h>
31 #include <dpl/file_input.h>
32 #include <dpl/file_output.h>
37 using namespace DPL::Utils;
40 //do not used path here we test it
41 std::string rootTest = "/tmp/wrttest/";
44 #define ROOTGUARD_TESTMETHOD(FUNC) \
46 bool catched = false; \
49 } Catch(Path::RootDirectoryError) { \
52 RUNNER_ASSERT_MSG(catched, "Use of method should be protected against root diretory"); \
55 RUNNER_TEST_GROUP_INIT(DPL_Path)
59 Description: constructs paths in many ways
60 Expected: success full constrution
62 RUNNER_TEST(path_mkfile)
64 DPL::ScopedDir sd(rootTest);
67 memset(&st, 0, sizeof(struct stat));
68 Path path = Path(rootTest) / "touch.txt";
69 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
70 RUNNER_ASSERT(!path.Exists());
72 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
73 RUNNER_ASSERT(path.Exists());
74 RUNNER_ASSERT(path.IsFile());
75 RUNNER_ASSERT(!path.IsDir());
76 RUNNER_ASSERT(!path.IsSymlink());
81 Description: tries to craete file when it exists
84 RUNNER_TEST(path_mkfile_exists)
86 DPL::ScopedDir sd(rootTest);
88 Path path = Path(rootTest) / "touch.txt";
90 RUNNER_ASSERT(path.Exists());
91 bool cannotCreate2ndTime = false;
96 Catch(Path::AlreadyExists)
98 cannotCreate2ndTime = true;
100 RUNNER_ASSERT_MSG(cannotCreate2ndTime, "File created should not be able to be created second time");
104 Name: path_exists_and_is_file_or_dir
105 Description: test for checking for existence of directory or file
108 RUNNER_TEST(path_exists_and_is_file_or_dir)
110 DPL::ScopedDir sd(rootTest);
112 Path file = Path(rootTest) / "testfile.txt";
114 RUNNER_ASSERT_MSG(file.ExistsAndIsFile(), "File should exist");
115 RUNNER_ASSERT_MSG(!file.ExistsAndIsDir(), "It should not be a directory");
117 Path dir = Path(rootTest) / "testdir";
119 RUNNER_ASSERT_MSG(dir.ExistsAndIsDir(), "Directory should exist");
120 RUNNER_ASSERT_MSG(!dir.ExistsAndIsFile(), "Is should not be a file");
124 Name: path_mkfile_invalid_path
125 Description: tries to create file in not exisitng directory
126 Expected: failure at creation
128 RUNNER_TEST(path_mkfile_invalid_path)
130 DPL::ScopedDir sd(rootTest);
132 Path path = Path(rootTest) / "not_existing" / "touch.txt";
133 bool cannotCreate = false;
138 Catch(Path::OperationFailed)
142 RUNNER_ASSERT_MSG(cannotCreate, "File created should not be able to be created");
147 Description: creates valid directory
148 Expected: success direcotry creation
150 RUNNER_TEST(path_mkdir)
152 DPL::ScopedDir sd(rootTest);
155 memset(&st, 0, sizeof(struct stat));
156 Path path = Path(rootTest) / "touchDir";
157 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
158 RUNNER_ASSERT(!path.Exists());
160 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
161 RUNNER_ASSERT(path.Exists());
162 RUNNER_ASSERT(!path.IsFile());
163 RUNNER_ASSERT(path.IsDir());
164 RUNNER_ASSERT(!path.IsSymlink());
169 Description: chekc if symlink is correctly recognized
170 Expected: method isSymlink returns true
172 RUNNER_TEST(path_symlink)
174 DPL::ScopedDir sd(rootTest);
177 memset(&st, 0, sizeof(struct stat));
178 Path path = Path(rootTest) / "symlink";
179 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Symlink should not be created");
180 RUNNER_ASSERT(!path.Exists());
181 (void)symlink("/nonexistisfile/file/file/file ", path.Fullpath().c_str());
182 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Symlink should be created");
183 RUNNER_ASSERT(path.Exists());
184 RUNNER_ASSERT(!path.IsFile());
185 RUNNER_ASSERT(!path.IsDir());
186 RUNNER_ASSERT(path.IsSymlink());
190 Name: path_construction_empty
191 Description: tries to construct empty path
194 RUNNER_TEST(path_construction_empty)
199 Path path1(std::string(""));
201 Catch(Path::EmptyPath)
205 RUNNER_ASSERT_MSG(passed, "Construction with empty path do not fails");
209 Name: path_construction_root
210 Description: tries to construct root path
213 RUNNER_TEST(path_construction_root)
215 Path path1(std::string("/"));
216 RUNNER_ASSERT(path1.Fullpath() == "/");
217 RUNNER_ASSERT(path1.Filename() == "");
221 RUNNER_ASSERT(path1.DirectoryName() == "/");
223 Catch(Path::InternalError)
227 RUNNER_ASSERT_MSG(passed, "Directory name for root should be an error");
231 Name: path_construction_1
232 Description: constructs paths in many ways
233 Expected: success full constrution
235 RUNNER_TEST(path_construction_1)
237 std::unique_ptr<char> sf(getcwd(NULL, 0));
239 Path path1(std::string("/test/bin/file"));
240 RUNNER_ASSERT(path1.Fullpath() == "/test/bin/file");
241 RUNNER_ASSERT(path1.Filename() == "file");
242 RUNNER_ASSERT(path1.DirectoryName() == "/test/bin");
246 Name: path_construction_2
247 Description: constructs paths in many ways
248 Expected: success full constrution
250 RUNNER_TEST(path_construction_2)
252 std::unique_ptr<char> sf(getcwd(NULL, 0));
253 std::string cwd(sf.get());
258 Path path2(std::string("test/bin/file.eas"));
259 RUNNER_ASSERT(path2.Fullpath() == cwd + "/test/bin/file.eas");
260 RUNNER_ASSERT(path2.Filename() == "file.eas");
261 RUNNER_ASSERT(path2.DirectoryName() == cwd + "/test/bin");
265 Name: path_construction_3
266 Description: constructs paths in many ways
267 Expected: success full constrution
269 RUNNER_TEST(path_construction_3)
271 std::unique_ptr<char> sf(getcwd(NULL, 0));
272 std::string cwd(sf.get());
277 Path path3("test/23/abc");
278 RUNNER_ASSERT(path3.Fullpath() == cwd + "/test/23/abc");
279 RUNNER_ASSERT(path3.Filename() == "abc");
280 RUNNER_ASSERT(path3.DirectoryName() == cwd + "/test/23");
284 Name: path_construction_4
285 Description: constructs paths in many ways
286 Expected: success full constrution
288 RUNNER_TEST(path_construction_4)
290 std::unique_ptr<char> sf(getcwd(NULL, 0));
292 Path path4("/test/bin/abc");
293 RUNNER_ASSERT(path4.Fullpath() == "/test/bin/abc");
294 RUNNER_ASSERT(path4.Filename() == "abc");
295 RUNNER_ASSERT(path4.DirectoryName() == "/test/bin");
299 Name: path_construction_5
300 Description: constructs paths in many ways
301 Expected: success full constrution
303 RUNNER_TEST(path_construction_5)
305 std::unique_ptr<char> sf(getcwd(NULL, 0));
306 std::string cwd(sf.get());
311 Path path5(DPL::String(L"test/bin/file.st.exe"));
312 RUNNER_ASSERT(path5.Fullpath() == cwd + "/test/bin/file.st.exe");
313 RUNNER_ASSERT(path5.Filename() == "file.st.exe");
314 RUNNER_ASSERT(path5.DirectoryName() == cwd + "/test/bin");
318 Name: path_construction_6
319 Description: constructs paths in many ways
320 Expected: success full constrution
322 RUNNER_TEST(path_construction_6)
324 std::unique_ptr<char> sf(getcwd(NULL, 0));
326 Path path6(DPL::String(L"/test/bin/file"));
327 RUNNER_ASSERT(path6.Fullpath() == "/test/bin/file");
328 RUNNER_ASSERT(path6.Filename() == "file");
329 RUNNER_ASSERT(path6.DirectoryName() == "/test/bin");
333 Name: path_construction_7
334 Description: constructs paths in many ways
335 Expected: success full constrution
337 RUNNER_TEST(path_construction_7)
339 std::unique_ptr<char> sf(getcwd(NULL, 0));
340 std::string cwd(sf.get());
345 Path path7 = Path("test") / "a///23/lol";
346 RUNNER_ASSERT(path7.Fullpath() == cwd + "/test/a/23/lol");
347 RUNNER_ASSERT(path7.Filename() == "lol");
348 RUNNER_ASSERT(path7.DirectoryName() == cwd + "/test/a/23");
352 Name: path_construction_8
353 Description: constructs paths in many ways
354 Expected: success full constrution
356 RUNNER_TEST(path_construction_8)
358 std::unique_ptr<char> sf(getcwd(NULL, 0));
360 Path path8 = Path("/test/bin/") / "123" / "dir1.dll";
361 RUNNER_ASSERT(path8.Fullpath() == "/test/bin/123/dir1.dll");
362 RUNNER_ASSERT(path8.Filename() == "dir1.dll");
363 RUNNER_ASSERT(path8.DirectoryName() == "/test/bin/123");
367 Name: path_construction_9
368 Description: constructs paths in many ways
369 Expected: success full constrution
371 RUNNER_TEST(path_construction_9)
373 std::unique_ptr<char> sf(getcwd(NULL, 0));
375 Path path9 = Path("/test/bin/file.txt//");
376 RUNNER_ASSERT(path9.Fullpath() == "/test/bin/file.txt");
377 RUNNER_ASSERT(path9.Filename() == "file.txt");
378 RUNNER_ASSERT(path9.DirectoryName() == "/test/bin");
382 Name: path_construction_10
383 Description: constructs paths by appending
384 Expected: success full constrution
386 RUNNER_TEST(path_construction_10)
388 Path path10 = Path("/test/");
390 path10 /= std::string("two");
391 path10 /= DPL::String(L"three");
392 RUNNER_ASSERT(path10.Fullpath() == "/test/one/two/three");
393 RUNNER_ASSERT(path10.Filename() == "three");
394 RUNNER_ASSERT(path10.DirectoryName() == "/test/one/two");
399 Description: tests removing paths
400 Expected: successfull path remove
402 RUNNER_TEST(path_remove_valid)
404 DPL::ScopedDir sd(rootTest);
407 memset(&st, 0, sizeof(struct stat));
408 Path path = Path(rootTest) / "touchDir";
409 RUNNER_ASSERT(!path.Exists());
412 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
413 RUNNER_ASSERT(path.Exists());
416 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
417 RUNNER_ASSERT(!path.Exists());
420 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
421 RUNNER_ASSERT(path.Exists());
424 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
425 RUNNER_ASSERT(!path.Exists());
429 Name: path_try_remove
430 Description: tests removing paths
431 Expected: successfull path remove once
433 RUNNER_TEST(path_try_remove_valid)
435 DPL::ScopedDir sd(rootTest);
438 memset(&st, 0, sizeof(struct stat));
439 Path path = Path(rootTest) / "touchDir";
440 RUNNER_ASSERT(!path.Exists());
443 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
444 RUNNER_ASSERT(path.Exists());
446 RUNNER_ASSERT(TryRemove(path));
447 RUNNER_ASSERT(!TryRemove(path));
448 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
449 RUNNER_ASSERT(!path.Exists());
452 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
453 RUNNER_ASSERT(path.Exists());
455 RUNNER_ASSERT(TryRemove(path));
456 RUNNER_ASSERT(!TryRemove(path));
457 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
458 RUNNER_ASSERT(!path.Exists());
462 Name: path_remove_invalid
463 Description: tests removing invalid paths
464 Expected: failure at path remove
466 RUNNER_TEST(path_remove_invalid)
468 DPL::ScopedDir sd(rootTest);
470 Path path = Path(rootTest) / "touchDir";
472 bool removedNotExisting = true;
477 Catch(Path::OperationFailed)
479 removedNotExisting = false;
481 RUNNER_ASSERT_MSG(!removedNotExisting, "Removing not existing file");
486 Description: tests path renaming
487 Expected: path is successfully renamed
489 RUNNER_TEST(path_rename)
491 DPL::ScopedDir sd(rootTest);
494 memset(&st, 0, sizeof(struct stat));
495 Path path = Path(rootTest) / "touchDir";
496 Path dirpath = Path(rootTest) / "directory";
497 Path path2 = dirpath / "touchDir2";
502 RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
503 RUNNER_ASSERT(path.Exists());
504 RUNNER_ASSERT(!path2.Exists());
507 RUNNER_ASSERT(!path.Exists());
508 RUNNER_ASSERT(path2.Exists());
511 RUNNER_ASSERT(path.Exists());
512 RUNNER_ASSERT(!path2.Exists());
516 Name: path_rename_xdev
517 Description: tests path renaming between devices
518 Expected: path is successfully renamed
520 RUNNER_TEST(path_rename_xdev)
522 //assuming /opt/usr and /usr is normally on other partitions on device
524 Path path = Path("/opt/usr") / "touchDir";
525 Path dirpath = path / "directory";
526 Path filepath = path / "file.txt";
528 Path path2 = Path("/usr") / "touchDir2";
529 Path dirpath2 = path2 / "directory";
530 Path filepath2 = path2 / "file.txt";
536 MakeEmptyFile(filepath);
538 RUNNER_ASSERT(path.Exists());
539 RUNNER_ASSERT(dirpath.Exists());
540 RUNNER_ASSERT(filepath.Exists());
541 RUNNER_ASSERT(!path2.Exists());
542 RUNNER_ASSERT(!dirpath2.Exists());
543 RUNNER_ASSERT(!filepath2.Exists());
546 RUNNER_ASSERT(!path.Exists());
547 RUNNER_ASSERT(!dirpath.Exists());
548 RUNNER_ASSERT(!filepath.Exists());
549 RUNNER_ASSERT(path2.Exists());
550 RUNNER_ASSERT(dirpath2.Exists());
551 RUNNER_ASSERT(filepath2.Exists());
554 RUNNER_ASSERT(path.Exists());
555 RUNNER_ASSERT(dirpath.Exists());
556 RUNNER_ASSERT(filepath.Exists());
557 RUNNER_ASSERT(!path2.Exists());
558 RUNNER_ASSERT(!dirpath2.Exists());
559 RUNNER_ASSERT(!filepath2.Exists());
565 * Name: path_basename
566 * Description: check basename equivalents
569 RUNNER_TEST(path_basename)
571 DPL::ScopedDir sd(rootTest);
572 Path path = Path(rootTest) / "directory" / "touch.txt";
573 RUNNER_ASSERT(path.DirectoryName() == path.DirectoryPath().Fullpath());
574 RUNNER_ASSERT(path.DirectoryPath().DirectoryName() == path.DirectoryPath().DirectoryPath().Fullpath());
579 * Description: check if operations cannot be executed on root directory
581 * This is check because of default path is root and it should not be used usually
582 * Default constructor is unfortnatelly easier to use
586 RUNNER_TEST(path_safe)
588 DPL::ScopedDir sd(rootTest);
589 Path normal = Path(rootTest) / "directory" / "touch.txt";
591 ROOTGUARD_TESTMETHOD( Rename(normal, root) );
592 ROOTGUARD_TESTMETHOD( Rename(root, root) );
593 ROOTGUARD_TESTMETHOD( Rename(root, normal) );
594 ROOTGUARD_TESTMETHOD( CopyDir(normal, root) );
595 ROOTGUARD_TESTMETHOD( CopyDir(root, root) );
596 ROOTGUARD_TESTMETHOD( CopyDir(root, normal) );
597 ROOTGUARD_TESTMETHOD( CopyFile(normal, root) );
598 ROOTGUARD_TESTMETHOD( CopyFile(root, root) );
599 ROOTGUARD_TESTMETHOD( CopyFile(root, normal) );
600 ROOTGUARD_TESTMETHOD( Remove(root) );
601 ROOTGUARD_TESTMETHOD( MakeEmptyFile(root) );
602 ROOTGUARD_TESTMETHOD( MakeDir(root) );
607 * Description: check testing size of file
608 * Expected: correct size
610 RUNNER_TEST(path_size)
612 DPL::ScopedDir sd(rootTest);
613 Path path = Path(rootTest) / "touch.txt";
614 DPL::Utils::MakeEmptyFile(path);
615 RUNNER_ASSERT(path.Size() == 0);
618 DPL::FileOutput out(path.Fullpath());
620 bq.AppendCopy("123456789", 9);
621 out.Write(bq, bq.Size());
623 RUNNER_ASSERT(path.Size() == 9);
627 DPL::FileOutput out(path.Fullpath());
629 bq.AppendCopy("123456789", 4);
630 out.Write(bq, bq.Size());
632 RUNNER_ASSERT(path.Size() == 4);
638 Description: tests path coping directory andfiles
639 Expected: coping should be done
641 RUNNER_TEST(path_copy_directory)
643 DPL::ScopedDir sd(rootTest);
645 Path path = Path(rootTest) / "sourceDir";
646 Path innerPath = Path(rootTest) / "sourceDir" / "level1" ;
647 Path innerPath2 = Path(rootTest) / "sourceDir" / "level1" / "level2";
648 Path file1 = Path(rootTest) / "sourceDir" / "level1" / "level2" / "file1.txt";
649 Path file2 = Path(rootTest) / "sourceDir" / "level1" / "level2" / "file2.txt";
650 Path file3 = Path(rootTest) / "sourceDir" / "level1" / "file3.txt";
651 Path file4 = Path(rootTest) / "sourceDir" / "file4.txt";
653 Path tfile1 = Path(rootTest) / "targetDir" / "level1" / "level2" / "file1.txt";
654 Path tfile2 = Path(rootTest) / "targetDir" / "level1" / "level2" / "file2.txt";
655 Path tfile3 = Path(rootTest) / "targetDir" / "level1" / "file3.txt";
656 Path tfile4 = Path(rootTest) / "targetDir" / "file4.txt";
658 Path target = Path(rootTest) / "targetDir";
660 DPL::Utils::MakeDir(path);
661 DPL::Utils::MakeDir(innerPath);
662 DPL::Utils::MakeDir(innerPath2);
663 DPL::Utils::MakeEmptyFile(file1);
664 DPL::Utils::MakeEmptyFile(file2);
665 DPL::Utils::MakeEmptyFile(file3);
666 DPL::Utils::MakeEmptyFile(file4);
668 DPL::Utils::CopyDir(path, target);
670 RUNNER_ASSERT_MSG(tfile1.Exists(), tfile1.Fullpath() + " not exists");
671 RUNNER_ASSERT_MSG(tfile1.IsFile(), tfile1.Fullpath() + " is not file");
672 RUNNER_ASSERT_MSG(tfile2.Exists(), tfile2.Fullpath() + " not exists");
673 RUNNER_ASSERT_MSG(tfile2.IsFile(), tfile2.Fullpath() + " is not file");
674 RUNNER_ASSERT_MSG(tfile3.Exists(), tfile3.Fullpath() + " not exists");
675 RUNNER_ASSERT_MSG(tfile3.IsFile(), tfile3.Fullpath() + " is not file");
676 RUNNER_ASSERT_MSG(tfile4.Exists(), tfile4.Fullpath() + " not exists");
677 RUNNER_ASSERT_MSG(tfile4.IsFile(), tfile4.Fullpath() + " is not file");
681 Name: path_copy_inner
682 Description: tests path coping to subdirectory
683 Expected: coping shoudl fail
685 RUNNER_TEST(path_copy_inner)
687 DPL::ScopedDir sd(rootTest);
689 Path path = Path(rootTest) / "touchDir";
690 Path inner = Path(rootTest) / "touchDir" / "innerDirectory";
692 bool exceptionCatched = false;
695 DPL::Utils::CopyDir(path, inner);
697 Catch(DPL::Utils::Path::CannotCopy)
699 exceptionCatched = true;
701 RUNNER_ASSERT_MSG(exceptionCatched, "Copy should fail");
706 Description: tests method compare if one path is subpath of another
707 Expected: correct results
709 RUNNER_TEST(path_issubpath)
711 Path path1 = Path(rootTest) / "touchDir/asd/sdf";
712 Path path2 = Path(rootTest) / "touchDir/asd/sdf/123";
713 Path path3 = Path(rootTest) / "touchDir/asd/sdno";
714 Path path4 = Path("/");
716 RUNNER_ASSERT(path1.isSubPath(path2));
717 RUNNER_ASSERT(!path1.isSubPath(path3));
718 RUNNER_ASSERT(!path1.isSubPath(path4));
720 RUNNER_ASSERT(!path2.isSubPath(path1));
721 RUNNER_ASSERT(!path2.isSubPath(path3));
722 RUNNER_ASSERT(!path2.isSubPath(path4));
724 RUNNER_ASSERT(path4.isSubPath(path1));
725 RUNNER_ASSERT(path4.isSubPath(path2));
726 RUNNER_ASSERT(path4.isSubPath(path3));
730 Name: path_rename_same
731 Description: tests if renam does not brokens file if target location is equal to source location
732 Expected: path is avaliable
734 RUNNER_TEST(path_rename_same)
736 DPL::ScopedDir sd(rootTest);
739 memset(&st, 0, sizeof(struct stat));
740 Path path = Path(rootTest) / "touchDir";
744 RUNNER_ASSERT(path.Exists());
748 Name: path_iterate_not_directory
749 Description: iterates not a directory
750 Expected: success full constrution
752 RUNNER_TEST(path_iterate_not_directory)
754 DPL::ScopedDir sd(rootTest);
756 Path fileTest = Path(rootTest) / "file.txt";
757 MakeEmptyFile(fileTest);
762 FOREACH(file, fileTest)
767 Catch(Path::NotDirectory)
771 RUNNER_ASSERT(passed);
775 Name: path_construction
776 Description: constructs paths in many ways
777 Expected: success full constrution
779 RUNNER_TEST(path_iterate_empty_directory)
781 DPL::ScopedDir sd(rootTest);
783 Path dirTest = Path(rootTest) / "directory";
789 FOREACH(file, dirTest)
792 WrtLogE("Directory should be empty");
795 Catch(Path::NotDirectory)
798 WrtLogE("Directory should exists");
800 RUNNER_ASSERT(passed);
804 Name: path_construction
805 Description: constructs paths in many ways
806 Expected: success full constrution
808 RUNNER_TEST(path_iterate_notempty_directory)
810 DPL::ScopedDir sd(rootTest);
812 Path dirTest = Path(rootTest) / "directory";
814 Path path1 = Path(rootTest) / "directory" / "file1";
815 Path path2 = Path(rootTest) / "directory" / "file2";
816 Path path3 = Path(rootTest) / "directory" / "file3";
818 std::set<std::string> resultSet;
819 std::set<std::string> testSet;
820 testSet.insert(path1.Fullpath());
821 testSet.insert(path2.Fullpath());
822 testSet.insert(path3.Fullpath());
825 MakeEmptyFile(path1);
826 MakeEmptyFile(path2);
827 MakeEmptyFile(path3);
829 FOREACH(file, dirTest)
831 resultSet.insert(file->Fullpath());
834 RUNNER_ASSERT_MSG(testSet == resultSet, "Testing");
838 Name: path_construction
839 Description: constructs paths in many ways
840 Expected: success full constrution
842 RUNNER_TEST(path_iterator_copy_constructor)
844 DPL::ScopedDir sd(rootTest);
846 Path dirTest = Path(rootTest) / "directory";
848 Path path1 = Path(rootTest) / "directory" / "file1";
849 Path path2 = Path(rootTest) / "directory" / "file2";
850 Path path3 = Path(rootTest) / "directory" / "file3";
853 MakeEmptyFile(path1);
854 MakeEmptyFile(path2);
855 MakeEmptyFile(path3);
857 std::shared_ptr<Path::Iterator> iter1(new Path::Iterator((Path(rootTest) / "directory").Fullpath().c_str()));
859 //as it's input iterator it's guaranteed for one element to be iterate only once
861 std::shared_ptr<Path::Iterator> iter2(new Path::Iterator( (*iter1)));
865 RUNNER_ASSERT_MSG(*iter2 == dirTest.end(), "Iterator is in broken state");
870 Name: path_extension_test
871 Description: Tests if file extension is correct
872 Expected: Proper recognition of extensions
874 RUNNER_TEST(path_extension_test)
876 Path path1("/path/to/file.dot");
877 Path path2("/path/to/file..dot");
878 Path path3("/path/to/file..dot.");
879 Path path4("/path/to/file..dot.dot");
880 Path path5("/path/to.dot/file");
881 Path path6("./path/to/file");
882 Path path7("./path/to/file");
883 Path path8("/path/../file.xml");
884 Path path9("/path/../file.XML");
885 Path path10("/path/../file.myfileextension");
887 RUNNER_ASSERT(path1.Extension() == "dot");
888 RUNNER_ASSERT(path2.Extension() == "dot");
889 RUNNER_ASSERT(path3.Extension() == "");
890 RUNNER_ASSERT(path4.Extension() == "dot");
891 RUNNER_ASSERT(path5.Extension() == "");
892 RUNNER_ASSERT(path6.Extension() == "");
893 RUNNER_ASSERT(path7.Extension() == "");
894 RUNNER_ASSERT(path8.Extension() == "xml");
895 RUNNER_ASSERT(path9.Extension() != "xml");
896 RUNNER_ASSERT(path10.Extension() == "myfileextension");
900 Name: path_has_extension_test
901 Description: Tests if file extension is correct
902 Expected: Proper recognition of extensions
904 RUNNER_TEST(path_has_extension_test)
907 Path dirTest = Path("extension");
909 Path path1 = dirTest / "file1.XML";
910 Path path2 = dirTest / "file2.JPG";
911 Path path3 = dirTest / "file3.";
912 Path path4 = dirTest / "file4";
913 Path path5 = dirTest / "file5.HTML";
914 Path path6 = dirTest / "file6.JS";
915 Path path7 = dirTest / "file7.VERY_VERY_LONG_EXTENSION";
916 Path path8 = dirTest / "file8.VERY.VERY.LONG.EXTENSION.WITH.DOTS";
918 RUNNER_ASSERT_MSG(path1.hasExtension("XML"), "Problem with comparison");
919 RUNNER_ASSERT_MSG(path2.hasExtension("JPG"), "Problem with comparison");
920 RUNNER_ASSERT_MSG(path5.hasExtension("HTML"), "Problem with comparison");
921 RUNNER_ASSERT_MSG(path6.hasExtension("JS"), "Problem with comparison");
922 RUNNER_ASSERT_MSG(path7.hasExtension("VERY_VERY_LONG_EXTENSION"),
923 "Problem with comparison");
924 RUNNER_ASSERT_MSG(path8.hasExtension("DOTS"), "Problem with comparison");
926 RUNNER_ASSERT_MSG(!path1.hasExtension(".XML"),
927 "Wrong argument in hasExtension() function");
928 RUNNER_ASSERT_MSG(!path1.hasExtension("MXL"),
929 "Wrong argument in hasExtension() function");
930 RUNNER_ASSERT_MSG(!path2.hasExtension(".JPG"),
931 "Wrong argument in hasExtension() function");
932 RUNNER_ASSERT_MSG(!path5.hasExtension(".HTML"),
933 "Wrong argument in hasExtension() function");
934 RUNNER_ASSERT_MSG(!path6.hasExtension(".JS"),
935 "Wrong argument in hasExtension() function");
937 RUNNER_ASSERT_MSG(path3.hasExtension(""), "Extension length is 0");
939 RUNNER_ASSERT_MSG(path4.hasExtension(""), "Extension length is 0");
943 Name: path_create_temp_dir
944 Description: tests if temp dir was created
945 Expected: temp dir exists
947 RUNNER_TEST(path_create_temp_dir)
949 Path p1 = CreateTempPath(Path("/usr/tmp/"));
950 Path p2 = CreateTempPath(Path("/opt/usr/apps/tmp/"));
951 Path p3 = CreateTempPath(Path("/opt/usr/apps/tmp/"));
953 RUNNER_ASSERT_MSG(p1.Exists(), "Temp dir doesn't exists");
954 RUNNER_ASSERT_MSG(p2.Exists(), "Temp dir doesn't exists");
955 RUNNER_ASSERT_MSG(p3.Exists(), "Temp dir doesn't exists");
956 RUNNER_ASSERT_MSG(p2.Fullpath() != p3.Fullpath(), "Each temp path should be unique due to having tv_usec in dir name.");