tizen 2.4 release
[framework/web/wrt-commons.git] / tests / utils / path_tests.cpp
1 /*
2  * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file    path.h
18  * @author  Tomasz Iwanek (t.iwanek@samsung.com)
19  * @version 1.0
20  */
21
22 #include <set>
23 #include <memory>
24
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>
33
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 using namespace DPL::Utils;
38
39 namespace {
40 //do not used path here we test it
41 std::string rootTest = "/tmp/wrttest/";
42 }
43
44 #define ROOTGUARD_TESTMETHOD(FUNC)                                                         \
45 {                                                                                          \
46     bool catched = false;                                                                  \
47     Try {                                                                                  \
48         FUNC;                                                                              \
49     } Catch(Path::RootDirectoryError) {                                                    \
50         catched = true;                                                                    \
51     }                                                                                      \
52     RUNNER_ASSERT_MSG(catched, "Use of method should be protected against root diretory"); \
53 }                                                                                          \
54
55 RUNNER_TEST_GROUP_INIT(DPL_Path)
56
57 /*
58 Name: path_touch
59 Description: constructs paths in many ways
60 Expected: success full constrution
61 */
62 RUNNER_TEST(path_mkfile)
63 {
64     DPL::ScopedDir sd(rootTest);
65
66     struct stat st;
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());
71     MakeEmptyFile(path);
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());
77 }
78
79 /*
80 Name: path_touch
81 Description: tries to craete file when it exists
82 Expected: failure
83 */
84 RUNNER_TEST(path_mkfile_exists)
85 {
86     DPL::ScopedDir sd(rootTest);
87
88     Path path = Path(rootTest) / "touch.txt";
89     MakeEmptyFile(path);
90     RUNNER_ASSERT(path.Exists());
91     bool cannotCreate2ndTime = false;
92     Try
93     {
94         MakeEmptyFile(path);
95     }
96     Catch(Path::AlreadyExists)
97     {
98         cannotCreate2ndTime = true;
99     }
100     RUNNER_ASSERT_MSG(cannotCreate2ndTime, "File created should not be able to be created second time");
101 }
102
103 /*
104 Name: path_exists_and_is_file_or_dir
105 Description: test for checking for existence of directory or file
106 Expected: success
107 */
108 RUNNER_TEST(path_exists_and_is_file_or_dir)
109 {
110     DPL::ScopedDir sd(rootTest);
111
112     Path file = Path(rootTest) / "testfile.txt";
113     MakeEmptyFile(file);
114     RUNNER_ASSERT_MSG(file.ExistsAndIsFile(), "File should exist");
115     RUNNER_ASSERT_MSG(!file.ExistsAndIsDir(), "It should not be a directory");
116
117     Path dir = Path(rootTest) / "testdir";
118     MakeDir(dir);
119     RUNNER_ASSERT_MSG(dir.ExistsAndIsDir(), "Directory should exist");
120     RUNNER_ASSERT_MSG(!dir.ExistsAndIsFile(), "Is should not be a file");
121 }
122
123 /*
124 Name: path_mkfile_invalid_path
125 Description: tries to create file in not exisitng directory
126 Expected: failure at creation
127 */
128 RUNNER_TEST(path_mkfile_invalid_path)
129 {
130     DPL::ScopedDir sd(rootTest);
131
132     Path path = Path(rootTest) / "not_existing" / "touch.txt";
133     bool cannotCreate = false;
134     Try
135     {
136         MakeEmptyFile(path);
137     }
138     Catch(Path::OperationFailed)
139     {
140         cannotCreate = true;
141     }
142     RUNNER_ASSERT_MSG(cannotCreate, "File created should not be able to be created");
143 }
144
145 /*
146 Name: path_mkdir
147 Description: creates valid directory
148 Expected: success direcotry creation
149 */
150 RUNNER_TEST(path_mkdir)
151 {
152     DPL::ScopedDir sd(rootTest);
153
154     struct stat st;
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());
159     MakeDir(path);
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());
165 }
166
167 /*
168 Name: path_symlink
169 Description: chekc if symlink is correctly recognized
170 Expected: method isSymlink returns true
171 */
172 RUNNER_TEST(path_symlink)
173 {
174     DPL::ScopedDir sd(rootTest);
175
176     struct stat st;
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());
187 }
188
189 /*
190 Name: path_construction_empty
191 Description: tries to construct empty path
192 Expected: failure
193 */
194 RUNNER_TEST(path_construction_empty)
195 {
196     bool passed = false;
197     Try
198     {
199         Path path1(std::string(""));
200     }
201     Catch(Path::EmptyPath)
202     {
203         passed = true;
204     }
205     RUNNER_ASSERT_MSG(passed, "Construction with empty path do not fails");
206 }
207
208 /*
209 Name: path_construction_root
210 Description: tries to construct root path
211 Expected: success
212 */
213 RUNNER_TEST(path_construction_root)
214 {
215     Path path1(std::string("/"));
216     RUNNER_ASSERT(path1.Fullpath() == "/");
217     RUNNER_ASSERT(path1.Filename() == "");
218     bool passed = false;
219     Try
220     {
221         RUNNER_ASSERT(path1.DirectoryName() == "/");
222     }
223     Catch(Path::InternalError)
224     {
225         passed = true;
226     }
227     RUNNER_ASSERT_MSG(passed, "Directory name for root should be an error");
228 }
229
230 /*
231 Name: path_construction_1
232 Description: constructs paths in many ways
233 Expected: success full constrution
234 */
235 RUNNER_TEST(path_construction_1)
236 {
237    std::unique_ptr<char> sf(getcwd(NULL, 0));
238
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");
243 }
244
245 /*
246 Name: path_construction_2
247 Description: constructs paths in many ways
248 Expected: success full constrution
249 */
250 RUNNER_TEST(path_construction_2)
251 {
252    std::unique_ptr<char> sf(getcwd(NULL, 0));
253     std::string cwd(sf.get());
254
255     if("/" == cwd)
256         cwd = "";
257
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");
262 }
263
264 /*
265 Name: path_construction_3
266 Description: constructs paths in many ways
267 Expected: success full constrution
268 */
269 RUNNER_TEST(path_construction_3)
270 {
271    std::unique_ptr<char> sf(getcwd(NULL, 0));
272     std::string cwd(sf.get());
273
274     if("/" == cwd)
275         cwd = "";
276
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");
281 }
282
283 /*
284 Name: path_construction_4
285 Description: constructs paths in many ways
286 Expected: success full constrution
287 */
288 RUNNER_TEST(path_construction_4)
289 {
290    std::unique_ptr<char> sf(getcwd(NULL, 0));
291
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");
296 }
297
298 /*
299 Name: path_construction_5
300 Description: constructs paths in many ways
301 Expected: success full constrution
302 */
303 RUNNER_TEST(path_construction_5)
304 {
305    std::unique_ptr<char> sf(getcwd(NULL, 0));
306     std::string cwd(sf.get());
307
308     if("/" == cwd)
309         cwd = "";
310
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");
315 }
316
317 /*
318 Name: path_construction_6
319 Description: constructs paths in many ways
320 Expected: success full constrution
321 */
322 RUNNER_TEST(path_construction_6)
323 {
324    std::unique_ptr<char> sf(getcwd(NULL, 0));
325
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");
330 }
331
332 /*
333 Name: path_construction_7
334 Description: constructs paths in many ways
335 Expected: success full constrution
336 */
337 RUNNER_TEST(path_construction_7)
338 {
339    std::unique_ptr<char> sf(getcwd(NULL, 0));
340     std::string cwd(sf.get());
341
342     if("/" == cwd)
343         cwd = "";
344
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");
349 }
350
351 /*
352 Name: path_construction_8
353 Description: constructs paths in many ways
354 Expected: success full constrution
355 */
356 RUNNER_TEST(path_construction_8)
357 {
358    std::unique_ptr<char> sf(getcwd(NULL, 0));
359
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");
364 }
365
366 /*
367 Name: path_construction_9
368 Description: constructs paths in many ways
369 Expected: success full constrution
370 */
371 RUNNER_TEST(path_construction_9)
372 {
373    std::unique_ptr<char> sf(getcwd(NULL, 0));
374
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");
379 }
380
381 /*
382 Name: path_construction_10
383 Description: constructs paths by appending
384 Expected: success full constrution
385 */
386 RUNNER_TEST(path_construction_10)
387 {
388     Path path10 = Path("/test/");
389     path10 /= "one";
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");
395 }
396
397 /*
398 Name: path_remove
399 Description: tests removing paths
400 Expected: successfull path remove
401 */
402 RUNNER_TEST(path_remove_valid)
403 {
404     DPL::ScopedDir sd(rootTest);
405
406     struct stat st;
407     memset(&st, 0, sizeof(struct stat));
408     Path path = Path(rootTest) / "touchDir";
409     RUNNER_ASSERT(!path.Exists());
410
411     MakeDir(path);
412     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
413     RUNNER_ASSERT(path.Exists());
414
415     Remove(path);
416     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
417     RUNNER_ASSERT(!path.Exists());
418
419     MakeEmptyFile(path);
420     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
421     RUNNER_ASSERT(path.Exists());
422
423     Remove(path);
424     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
425     RUNNER_ASSERT(!path.Exists());
426 }
427
428 /*
429 Name: path_try_remove
430 Description: tests removing paths
431 Expected: successfull path remove once
432 */
433 RUNNER_TEST(path_try_remove_valid)
434 {
435     DPL::ScopedDir sd(rootTest);
436
437     struct stat st;
438     memset(&st, 0, sizeof(struct stat));
439     Path path = Path(rootTest) / "touchDir";
440     RUNNER_ASSERT(!path.Exists());
441
442     MakeDir(path);
443     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
444     RUNNER_ASSERT(path.Exists());
445
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());
450
451     MakeEmptyFile(path);
452     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
453     RUNNER_ASSERT(path.Exists());
454
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());
459 }
460
461 /*
462 Name: path_remove_invalid
463 Description: tests removing invalid paths
464 Expected: failure at path remove
465 */
466 RUNNER_TEST(path_remove_invalid)
467 {
468     DPL::ScopedDir sd(rootTest);
469
470     Path path = Path(rootTest) / "touchDir";
471
472     bool removedNotExisting = true;
473     Try
474     {
475         Remove(path);
476     }
477     Catch(Path::OperationFailed)
478     {
479         removedNotExisting = false;
480     }
481     RUNNER_ASSERT_MSG(!removedNotExisting, "Removing not existing file");
482 }
483
484 /*
485 Name: path_rename
486 Description: tests path renaming
487 Expected: path is successfully renamed
488 */
489 RUNNER_TEST(path_rename)
490 {
491     DPL::ScopedDir sd(rootTest);
492
493     struct stat st;
494     memset(&st, 0, sizeof(struct stat));
495     Path path = Path(rootTest) / "touchDir";
496     Path dirpath = Path(rootTest) / "directory";
497     Path path2 = dirpath / "touchDir2";
498
499     MakeDir(dirpath);
500
501     MakeEmptyFile(path);
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());
505
506     Rename(path, path2);
507     RUNNER_ASSERT(!path.Exists());
508     RUNNER_ASSERT(path2.Exists());
509
510     Rename(path2, path);
511     RUNNER_ASSERT(path.Exists());
512     RUNNER_ASSERT(!path2.Exists());
513 }
514
515 /*
516 Name: path_rename_xdev
517 Description: tests path renaming between devices
518 Expected: path is successfully renamed
519 */
520 RUNNER_TEST(path_rename_xdev)
521 {
522     //assuming /opt/usr and /usr is normally on other partitions on device
523     //TODO: better
524     Path path = Path("/opt/usr") / "touchDir";
525     Path dirpath = path / "directory";
526     Path filepath = path / "file.txt";
527
528     Path path2 = Path("/usr") / "touchDir2";
529     Path dirpath2 = path2 / "directory";
530     Path filepath2 = path2 / "file.txt";
531
532     TryRemove(path);
533
534     MakeDir(path);
535     MakeDir(dirpath);
536     MakeEmptyFile(filepath);
537
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());
544
545     Rename(path, path2);
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());
552
553     Rename(path2, path);
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());
560
561     Remove(path);
562 }
563
564 /**
565  * Name: path_basename
566  * Description: check basename equivalents
567  * Expected: failure
568  */
569 RUNNER_TEST(path_basename)
570 {
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());
575 }
576
577 /**
578  * Name: path_safe
579  * Description: check if operations cannot be executed on root directory
580  *
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
583  *
584  * Expected: failure
585  */
586 RUNNER_TEST(path_safe)
587 {
588     DPL::ScopedDir sd(rootTest);
589     Path normal = Path(rootTest) / "directory" / "touch.txt";
590     Path root("/");
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) );
603 }
604
605 /**
606  * Name: path_size
607  * Description: check testing size of file
608  * Expected: correct size
609  */
610 RUNNER_TEST(path_size)
611 {
612     DPL::ScopedDir sd(rootTest);
613     Path path = Path(rootTest) / "touch.txt";
614     DPL::Utils::MakeEmptyFile(path);
615     RUNNER_ASSERT(path.Size() == 0);
616
617     {
618         DPL::FileOutput out(path.Fullpath());
619         DPL::BinaryQueue bq;
620         bq.AppendCopy("123456789", 9);
621         out.Write(bq, bq.Size());
622         out.Close();
623         RUNNER_ASSERT(path.Size() == 9);
624     }
625
626     {
627         DPL::FileOutput out(path.Fullpath());
628         DPL::BinaryQueue bq;
629         bq.AppendCopy("123456789", 4);
630         out.Write(bq, bq.Size());
631         out.Close();
632         RUNNER_ASSERT(path.Size() == 4);
633     }
634 }
635
636 /**
637 Name: path_copy
638 Description: tests path coping directory andfiles
639 Expected: coping should be done
640 */
641 RUNNER_TEST(path_copy_directory)
642 {
643     DPL::ScopedDir sd(rootTest);
644
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";
652
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";
657
658     Path target = Path(rootTest) / "targetDir";
659
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);
667
668     DPL::Utils::CopyDir(path, target);
669
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");
678 }
679
680 /*
681 Name: path_copy_inner
682 Description: tests path coping to subdirectory
683 Expected: coping shoudl fail
684 */
685 RUNNER_TEST(path_copy_inner)
686 {
687     DPL::ScopedDir sd(rootTest);
688
689     Path path = Path(rootTest) / "touchDir";
690     Path inner = Path(rootTest) / "touchDir" / "innerDirectory";
691
692     bool exceptionCatched = false;
693     Try
694     {
695         DPL::Utils::CopyDir(path, inner);
696     }
697     Catch(DPL::Utils::Path::CannotCopy)
698     {
699         exceptionCatched = true;
700     }
701     RUNNER_ASSERT_MSG(exceptionCatched, "Copy should fail");
702 }
703
704 /*
705 Name: issubpath
706 Description: tests method compare if one path is subpath of another
707 Expected: correct results
708 */
709 RUNNER_TEST(path_issubpath)
710 {
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("/");
715
716     RUNNER_ASSERT(path1.isSubPath(path2));
717     RUNNER_ASSERT(!path1.isSubPath(path3));
718     RUNNER_ASSERT(!path1.isSubPath(path4));
719
720     RUNNER_ASSERT(!path2.isSubPath(path1));
721     RUNNER_ASSERT(!path2.isSubPath(path3));
722     RUNNER_ASSERT(!path2.isSubPath(path4));
723
724     RUNNER_ASSERT(path4.isSubPath(path1));
725     RUNNER_ASSERT(path4.isSubPath(path2));
726     RUNNER_ASSERT(path4.isSubPath(path3));
727 }
728
729 /*
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
733 */
734 RUNNER_TEST(path_rename_same)
735 {
736     DPL::ScopedDir sd(rootTest);
737
738     struct stat st;
739     memset(&st, 0, sizeof(struct stat));
740     Path path = Path(rootTest) / "touchDir";
741
742     MakeDir(path);
743     Rename(path, path);
744     RUNNER_ASSERT(path.Exists());
745 }
746
747 /*
748 Name: path_iterate_not_directory
749 Description: iterates not a directory
750 Expected: success full constrution
751 */
752 RUNNER_TEST(path_iterate_not_directory)
753 {
754     DPL::ScopedDir sd(rootTest);
755
756     Path fileTest = Path(rootTest) / "file.txt";
757     MakeEmptyFile(fileTest);
758
759     bool passed = false;
760     Try
761     {
762         FOREACH(file, fileTest)
763         {
764
765         }
766     }
767     Catch(Path::NotDirectory)
768     {
769         passed = true;
770     }
771     RUNNER_ASSERT(passed);
772 }
773
774 /*
775 Name: path_construction
776 Description: constructs paths in many ways
777 Expected: success full constrution
778 */
779 RUNNER_TEST(path_iterate_empty_directory)
780 {
781     DPL::ScopedDir sd(rootTest);
782
783     Path dirTest = Path(rootTest) / "directory";
784     MakeDir(dirTest);
785
786     bool passed = true;
787     Try
788     {
789         FOREACH(file, dirTest)
790         {
791             passed = false;
792             WrtLogE("Directory should be empty");
793         }
794     }
795     Catch(Path::NotDirectory)
796     {
797         passed = false;
798         WrtLogE("Directory should exists");
799     }
800     RUNNER_ASSERT(passed);
801 }
802
803 /*
804 Name: path_construction
805 Description: constructs paths in many ways
806 Expected: success full constrution
807 */
808 RUNNER_TEST(path_iterate_notempty_directory)
809 {
810     DPL::ScopedDir sd(rootTest);
811
812     Path dirTest = Path(rootTest) / "directory";
813
814     Path path1 = Path(rootTest) / "directory" / "file1";
815     Path path2 = Path(rootTest) / "directory" / "file2";
816     Path path3 = Path(rootTest) / "directory" / "file3";
817
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());
823
824     MakeDir(dirTest);
825     MakeEmptyFile(path1);
826     MakeEmptyFile(path2);
827     MakeEmptyFile(path3);
828
829     FOREACH(file, dirTest)
830     {
831         resultSet.insert(file->Fullpath());
832     }
833
834     RUNNER_ASSERT_MSG(testSet == resultSet, "Testing");
835 }
836
837 /*
838 Name: path_construction
839 Description: constructs paths in many ways
840 Expected: success full constrution
841 */
842 RUNNER_TEST(path_iterator_copy_constructor)
843 {
844     DPL::ScopedDir sd(rootTest);
845
846     Path dirTest = Path(rootTest) / "directory";
847
848     Path path1 = Path(rootTest) / "directory" / "file1";
849     Path path2 = Path(rootTest) / "directory" / "file2";
850     Path path3 = Path(rootTest) / "directory" / "file3";
851
852     MakeDir(dirTest);
853     MakeEmptyFile(path1);
854     MakeEmptyFile(path2);
855     MakeEmptyFile(path3);
856
857     std::shared_ptr<Path::Iterator> iter1(new Path::Iterator((Path(rootTest) / "directory").Fullpath().c_str()));
858
859     //as it's input iterator it's guaranteed for one element to be iterate only once
860     (*iter1)++;
861     std::shared_ptr<Path::Iterator> iter2(new Path::Iterator( (*iter1)));
862     iter1.reset();
863     (*iter2)++;
864     ++(*iter2);
865     RUNNER_ASSERT_MSG(*iter2 == dirTest.end(), "Iterator is in broken state");
866     iter2.reset();
867 }
868
869 /*
870 Name: path_extension_test
871 Description: Tests if file extension is correct
872 Expected: Proper recognition of extensions
873 */
874 RUNNER_TEST(path_extension_test)
875 {
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");
886
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");
897 }
898
899 /*
900 Name: path_has_extension_test
901 Description: Tests if file extension is correct
902 Expected: Proper recognition of extensions
903 */
904 RUNNER_TEST(path_has_extension_test)
905 {
906
907     Path dirTest = Path("extension");
908
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";
917
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");
925
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");
936
937     RUNNER_ASSERT_MSG(path3.hasExtension(""), "Extension length is 0");
938
939     RUNNER_ASSERT_MSG(path4.hasExtension(""), "Extension length is 0");
940 }
941
942 /*
943 Name: path_create_temp_dir
944 Description: tests if temp dir was created
945 Expected: temp dir exists
946 */
947 RUNNER_TEST(path_create_temp_dir)
948 {
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/"));
952
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.");
957 }