tizen 2.3.1 release
[framework/web/wearable/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/scoped_free.h>
28 #include <dpl/utils/path.h>
29 #include <dpl/foreach.h>
30 #include <dpl/log/log.h>
31 #include <dpl/binary_queue.h>
32 #include <dpl/file_input.h>
33 #include <dpl/file_output.h>
34
35 #include <sys/stat.h>
36 #include <unistd.h>
37
38 using namespace DPL::Utils;
39
40 namespace {
41 //do not used path here we test it
42 std::string rootTest = "/tmp/wrttest/";
43 }
44
45 #define ROOTGUARD_TESTMETHOD(FUNC)                                                         \
46 {                                                                                          \
47     bool catched = false;                                                                  \
48     Try {                                                                                  \
49         FUNC;                                                                              \
50     } Catch(Path::RootDirectoryError) {                                                    \
51         catched = true;                                                                    \
52     }                                                                                      \
53     RUNNER_ASSERT_MSG(catched, "Use of method should be protected against root diretory"); \
54 }                                                                                          \
55
56 RUNNER_TEST_GROUP_INIT(DPL_Path)
57
58 /*
59 Name: path_touch
60 Description: constructs paths in many ways
61 Expected: success full constrution
62 */
63 RUNNER_TEST(path_mkfile)
64 {
65     DPL::ScopedDir sd(rootTest);
66
67     struct stat st;
68     memset(&st, 0, sizeof(struct stat));
69     Path path = Path(rootTest) / "touch.txt";
70     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
71     RUNNER_ASSERT(!path.Exists());
72     MakeEmptyFile(path);
73     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
74     RUNNER_ASSERT(path.Exists());
75     RUNNER_ASSERT(path.IsFile());
76     RUNNER_ASSERT(!path.IsDir());
77     RUNNER_ASSERT(!path.IsSymlink());
78 }
79
80 /*
81 Name: path_touch
82 Description: tries to craete file when it exists
83 Expected: failure
84 */
85 RUNNER_TEST(path_mkfile_exists)
86 {
87     DPL::ScopedDir sd(rootTest);
88
89     Path path = Path(rootTest) / "touch.txt";
90     MakeEmptyFile(path);
91     RUNNER_ASSERT(path.Exists());
92     bool cannotCreate2ndTime = false;
93     Try
94     {
95         MakeEmptyFile(path);
96     }
97     Catch(Path::AlreadyExists)
98     {
99         cannotCreate2ndTime = true;
100     }
101     RUNNER_ASSERT_MSG(cannotCreate2ndTime, "File created should not be able to be created second time");
102 }
103
104 /*
105 Name: path_exists_and_is_file_or_dir
106 Description: test for checking for existence of directory or file
107 Expected: success
108 */
109 RUNNER_TEST(path_exists_and_is_file_or_dir)
110 {
111     DPL::ScopedDir sd(rootTest);
112
113     Path file = Path(rootTest) / "testfile.txt";
114     MakeEmptyFile(file);
115     RUNNER_ASSERT_MSG(file.ExistsAndIsFile(), "File should exist");
116     RUNNER_ASSERT_MSG(!file.ExistsAndIsDir(), "It should not be a directory");
117
118     Path dir = Path(rootTest) / "testdir";
119     MakeDir(dir);
120     RUNNER_ASSERT_MSG(dir.ExistsAndIsDir(), "Directory should exist");
121     RUNNER_ASSERT_MSG(!dir.ExistsAndIsFile(), "Is should not be a file");
122 }
123
124 /*
125 Name: path_mkfile_invalid_path
126 Description: tries to create file in not exisitng directory
127 Expected: failure at creation
128 */
129 RUNNER_TEST(path_mkfile_invalid_path)
130 {
131     DPL::ScopedDir sd(rootTest);
132
133     Path path = Path(rootTest) / "not_existing" / "touch.txt";
134     bool cannotCreate = false;
135     Try
136     {
137         MakeEmptyFile(path);
138     }
139     Catch(Path::OperationFailed)
140     {
141         cannotCreate = true;
142     }
143     RUNNER_ASSERT_MSG(cannotCreate, "File created should not be able to be created");
144 }
145
146 /*
147 Name: path_mkdir
148 Description: creates valid directory
149 Expected: success direcotry creation
150 */
151 RUNNER_TEST(path_mkdir)
152 {
153     DPL::ScopedDir sd(rootTest);
154
155     struct stat st;
156     memset(&st, 0, sizeof(struct stat));
157     Path path = Path(rootTest) / "touchDir";
158     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
159     RUNNER_ASSERT(!path.Exists());
160     MakeDir(path);
161     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
162     RUNNER_ASSERT(path.Exists());
163     RUNNER_ASSERT(!path.IsFile());
164     RUNNER_ASSERT(path.IsDir());
165     RUNNER_ASSERT(!path.IsSymlink());
166 }
167
168 /*
169 Name: path_symlink
170 Description: chekc if symlink is correctly recognized
171 Expected: method isSymlink returns true
172 */
173 RUNNER_TEST(path_symlink)
174 {
175     DPL::ScopedDir sd(rootTest);
176
177     struct stat st;
178     memset(&st, 0, sizeof(struct stat));
179     Path path = Path(rootTest) / "symlink";
180     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Symlink should not be created");
181     RUNNER_ASSERT(!path.Exists());
182     (void)symlink("/nonexistisfile/file/file/file ", path.Fullpath().c_str());
183     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Symlink should be created");
184     RUNNER_ASSERT(path.Exists());
185     RUNNER_ASSERT(!path.IsFile());
186     RUNNER_ASSERT(!path.IsDir());
187     RUNNER_ASSERT(path.IsSymlink());
188 }
189
190 /*
191 Name: path_construction_empty
192 Description: tries to construct empty path
193 Expected: failure
194 */
195 RUNNER_TEST(path_construction_empty)
196 {
197     bool passed = false;
198     Try
199     {
200         Path path1(std::string(""));
201     }
202     Catch(Path::EmptyPath)
203     {
204         passed = true;
205     }
206     RUNNER_ASSERT_MSG(passed, "Construction with empty path do not fails");
207 }
208
209 /*
210 Name: path_construction_root
211 Description: tries to construct root path
212 Expected: success
213 */
214 RUNNER_TEST(path_construction_root)
215 {
216     Path path1(std::string("/"));
217     RUNNER_ASSERT(path1.Fullpath() == "/");
218     RUNNER_ASSERT(path1.Filename() == "");
219     bool passed = false;
220     Try
221     {
222         RUNNER_ASSERT(path1.DirectoryName() == "/");
223     }
224     Catch(Path::InternalError)
225     {
226         passed = true;
227     }
228     RUNNER_ASSERT_MSG(passed, "Directory name for root should be an error");
229 }
230
231 /*
232 Name: path_construction_1
233 Description: constructs paths in many ways
234 Expected: success full constrution
235 */
236 RUNNER_TEST(path_construction_1)
237 {
238     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
239
240     Path path1(std::string("/test/bin/file"));
241     RUNNER_ASSERT(path1.Fullpath() == "/test/bin/file");
242     RUNNER_ASSERT(path1.Filename() == "file");
243     RUNNER_ASSERT(path1.DirectoryName() == "/test/bin");
244 }
245
246 /*
247 Name: path_construction_2
248 Description: constructs paths in many ways
249 Expected: success full constrution
250 */
251 RUNNER_TEST(path_construction_2)
252 {
253     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
254     std::string cwd(sf.Get());
255
256     if("/" == cwd)
257         cwd = "";
258
259     Path path2(std::string("test/bin/file.eas"));
260     RUNNER_ASSERT(path2.Fullpath() == cwd + "/test/bin/file.eas");
261     RUNNER_ASSERT(path2.Filename() == "file.eas");
262     RUNNER_ASSERT(path2.DirectoryName() == cwd + "/test/bin");
263 }
264
265 /*
266 Name: path_construction_3
267 Description: constructs paths in many ways
268 Expected: success full constrution
269 */
270 RUNNER_TEST(path_construction_3)
271 {
272     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
273     std::string cwd(sf.Get());
274
275     if("/" == cwd)
276         cwd = "";
277
278     Path path3("test/23/abc");
279     RUNNER_ASSERT(path3.Fullpath() == cwd + "/test/23/abc");
280     RUNNER_ASSERT(path3.Filename() == "abc");
281     RUNNER_ASSERT(path3.DirectoryName() == cwd + "/test/23");
282 }
283
284 /*
285 Name: path_construction_4
286 Description: constructs paths in many ways
287 Expected: success full constrution
288 */
289 RUNNER_TEST(path_construction_4)
290 {
291     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
292
293     Path path4("/test/bin/abc");
294     RUNNER_ASSERT(path4.Fullpath() == "/test/bin/abc");
295     RUNNER_ASSERT(path4.Filename() == "abc");
296     RUNNER_ASSERT(path4.DirectoryName() == "/test/bin");
297 }
298
299 /*
300 Name: path_construction_5
301 Description: constructs paths in many ways
302 Expected: success full constrution
303 */
304 RUNNER_TEST(path_construction_5)
305 {
306     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
307     std::string cwd(sf.Get());
308
309     if("/" == cwd)
310         cwd = "";
311
312     Path path5(DPL::String(L"test/bin/file.st.exe"));
313     RUNNER_ASSERT(path5.Fullpath() == cwd + "/test/bin/file.st.exe");
314     RUNNER_ASSERT(path5.Filename() == "file.st.exe");
315     RUNNER_ASSERT(path5.DirectoryName() == cwd + "/test/bin");
316 }
317
318 /*
319 Name: path_construction_6
320 Description: constructs paths in many ways
321 Expected: success full constrution
322 */
323 RUNNER_TEST(path_construction_6)
324 {
325     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
326
327     Path path6(DPL::String(L"/test/bin/file"));
328     RUNNER_ASSERT(path6.Fullpath() == "/test/bin/file");
329     RUNNER_ASSERT(path6.Filename() == "file");
330     RUNNER_ASSERT(path6.DirectoryName() == "/test/bin");
331 }
332
333 /*
334 Name: path_construction_7
335 Description: constructs paths in many ways
336 Expected: success full constrution
337 */
338 RUNNER_TEST(path_construction_7)
339 {
340     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
341     std::string cwd(sf.Get());
342
343     if("/" == cwd)
344         cwd = "";
345
346     Path path7 = Path("test") / "a///23/lol";
347     RUNNER_ASSERT(path7.Fullpath() == cwd + "/test/a/23/lol");
348     RUNNER_ASSERT(path7.Filename() == "lol");
349     RUNNER_ASSERT(path7.DirectoryName() == cwd + "/test/a/23");
350 }
351
352 /*
353 Name: path_construction_8
354 Description: constructs paths in many ways
355 Expected: success full constrution
356 */
357 RUNNER_TEST(path_construction_8)
358 {
359     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
360
361     Path path8 = Path("/test/bin/") / "123" / "dir1.dll";
362     RUNNER_ASSERT(path8.Fullpath() == "/test/bin/123/dir1.dll");
363     RUNNER_ASSERT(path8.Filename() == "dir1.dll");
364     RUNNER_ASSERT(path8.DirectoryName() ==  "/test/bin/123");
365 }
366
367 /*
368 Name: path_construction_9
369 Description: constructs paths in many ways
370 Expected: success full constrution
371 */
372 RUNNER_TEST(path_construction_9)
373 {
374     DPL::ScopedFree<char> sf(getcwd(NULL, 0));
375
376     Path path9 = Path("/test/bin/file.txt//");
377     RUNNER_ASSERT(path9.Fullpath() == "/test/bin/file.txt");
378     RUNNER_ASSERT(path9.Filename() == "file.txt");
379     RUNNER_ASSERT(path9.DirectoryName() ==  "/test/bin");
380 }
381
382 /*
383 Name: path_construction_10
384 Description: constructs paths by appending
385 Expected: success full constrution
386 */
387 RUNNER_TEST(path_construction_10)
388 {
389     Path path10 = Path("/test/");
390     path10 /= "one";
391     path10 /= std::string("two");
392     path10 /= DPL::String(L"three");
393     RUNNER_ASSERT(path10.Fullpath() == "/test/one/two/three");
394     RUNNER_ASSERT(path10.Filename() == "three");
395     RUNNER_ASSERT(path10.DirectoryName() ==  "/test/one/two");
396 }
397
398 /*
399 Name: path_remove
400 Description: tests removing paths
401 Expected: successfull path remove
402 */
403 RUNNER_TEST(path_remove_valid)
404 {
405     DPL::ScopedDir sd(rootTest);
406
407     struct stat st;
408     memset(&st, 0, sizeof(struct stat));
409     Path path = Path(rootTest) / "touchDir";
410     RUNNER_ASSERT(!path.Exists());
411
412     MakeDir(path);
413     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
414     RUNNER_ASSERT(path.Exists());
415
416     Remove(path);
417     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
418     RUNNER_ASSERT(!path.Exists());
419
420     MakeEmptyFile(path);
421     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
422     RUNNER_ASSERT(path.Exists());
423
424     Remove(path);
425     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
426     RUNNER_ASSERT(!path.Exists());
427 }
428
429 /*
430 Name: path_try_remove
431 Description: tests removing paths
432 Expected: successfull path remove once
433 */
434 RUNNER_TEST(path_try_remove_valid)
435 {
436     DPL::ScopedDir sd(rootTest);
437
438     struct stat st;
439     memset(&st, 0, sizeof(struct stat));
440     Path path = Path(rootTest) / "touchDir";
441     RUNNER_ASSERT(!path.Exists());
442
443     MakeDir(path);
444     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "Directory should be created");
445     RUNNER_ASSERT(path.Exists());
446
447     RUNNER_ASSERT(TryRemove(path));
448     RUNNER_ASSERT(!TryRemove(path));
449     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "Directory should not be created");
450     RUNNER_ASSERT(!path.Exists());
451
452     MakeEmptyFile(path);
453     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
454     RUNNER_ASSERT(path.Exists());
455
456     RUNNER_ASSERT(TryRemove(path));
457     RUNNER_ASSERT(!TryRemove(path));
458     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) != 0, "File should not be created");
459     RUNNER_ASSERT(!path.Exists());
460 }
461
462 /*
463 Name: path_remove_invalid
464 Description: tests removing invalid paths
465 Expected: failure at path remove
466 */
467 RUNNER_TEST(path_remove_invalid)
468 {
469     DPL::ScopedDir sd(rootTest);
470
471     Path path = Path(rootTest) / "touchDir";
472
473     bool removedNotExisting = true;
474     Try
475     {
476         Remove(path);
477     }
478     Catch(Path::OperationFailed)
479     {
480         removedNotExisting = false;
481     }
482     RUNNER_ASSERT_MSG(!removedNotExisting, "Removing not existing file");
483 }
484
485 /*
486 Name: path_rename
487 Description: tests path renaming
488 Expected: path is successfully renamed
489 */
490 RUNNER_TEST(path_rename)
491 {
492     DPL::ScopedDir sd(rootTest);
493
494     struct stat st;
495     memset(&st, 0, sizeof(struct stat));
496     Path path = Path(rootTest) / "touchDir";
497     Path dirpath = Path(rootTest) / "directory";
498     Path path2 = dirpath / "touchDir2";
499
500     MakeDir(dirpath);
501
502     MakeEmptyFile(path);
503     RUNNER_ASSERT_MSG(lstat(path.Fullpath().c_str(), &st) == 0, "File should be created");
504     RUNNER_ASSERT(path.Exists());
505     RUNNER_ASSERT(!path2.Exists());
506
507     Rename(path, path2);
508     RUNNER_ASSERT(!path.Exists());
509     RUNNER_ASSERT(path2.Exists());
510
511     Rename(path2, path);
512     RUNNER_ASSERT(path.Exists());
513     RUNNER_ASSERT(!path2.Exists());
514 }
515
516 /*
517 Name: path_rename_xdev
518 Description: tests path renaming between devices
519 Expected: path is successfully renamed
520 */
521 RUNNER_TEST(path_rename_xdev)
522 {
523     //assuming /opt/usr and /usr is normally on other partitions on device
524     //TODO: better
525     Path path = Path("/opt/usr") / "touchDir";
526     Path dirpath = path / "directory";
527     Path filepath = path / "file.txt";
528
529     Path path2 = Path("/usr") / "touchDir2";
530     Path dirpath2 = path2 / "directory";
531     Path filepath2 = path2 / "file.txt";
532
533     TryRemove(path);
534
535     MakeDir(path);
536     MakeDir(dirpath);
537     MakeEmptyFile(filepath);
538
539     RUNNER_ASSERT(path.Exists());
540     RUNNER_ASSERT(dirpath.Exists());
541     RUNNER_ASSERT(filepath.Exists());
542     RUNNER_ASSERT(!path2.Exists());
543     RUNNER_ASSERT(!dirpath2.Exists());
544     RUNNER_ASSERT(!filepath2.Exists());
545
546     Rename(path, path2);
547     RUNNER_ASSERT(!path.Exists());
548     RUNNER_ASSERT(!dirpath.Exists());
549     RUNNER_ASSERT(!filepath.Exists());
550     RUNNER_ASSERT(path2.Exists());
551     RUNNER_ASSERT(dirpath2.Exists());
552     RUNNER_ASSERT(filepath2.Exists());
553
554     Rename(path2, path);
555     RUNNER_ASSERT(path.Exists());
556     RUNNER_ASSERT(dirpath.Exists());
557     RUNNER_ASSERT(filepath.Exists());
558     RUNNER_ASSERT(!path2.Exists());
559     RUNNER_ASSERT(!dirpath2.Exists());
560     RUNNER_ASSERT(!filepath2.Exists());
561
562     Remove(path);
563 }
564
565 /**
566  * Name: path_basename
567  * Description: check basename equivalents
568  * Expected: failure
569  */
570 RUNNER_TEST(path_basename)
571 {
572     DPL::ScopedDir sd(rootTest);
573     Path path = Path(rootTest) / "directory" / "touch.txt";
574     RUNNER_ASSERT(path.DirectoryName() == path.DirectoryPath().Fullpath());
575     RUNNER_ASSERT(path.DirectoryPath().DirectoryName() == path.DirectoryPath().DirectoryPath().Fullpath());
576 }
577
578 /**
579  * Name: path_safe
580  * Description: check if operations cannot be executed on root directory
581  *
582  * This is check because of default path is root and it should not be used usually
583  * Default constructor is unfortnatelly easier to use
584  *
585  * Expected: failure
586  */
587 RUNNER_TEST(path_safe)
588 {
589     DPL::ScopedDir sd(rootTest);
590     Path normal = Path(rootTest) / "directory" / "touch.txt";
591     Path root("/");
592     ROOTGUARD_TESTMETHOD( Rename(normal, root) );
593     ROOTGUARD_TESTMETHOD( Rename(root, root) );
594     ROOTGUARD_TESTMETHOD( Rename(root, normal) );
595     ROOTGUARD_TESTMETHOD( CopyDir(normal, root) );
596     ROOTGUARD_TESTMETHOD( CopyDir(root, root) );
597     ROOTGUARD_TESTMETHOD( CopyDir(root, normal) );
598     ROOTGUARD_TESTMETHOD( CopyFile(normal, root) );
599     ROOTGUARD_TESTMETHOD( CopyFile(root, root) );
600     ROOTGUARD_TESTMETHOD( CopyFile(root, normal) );
601     ROOTGUARD_TESTMETHOD( Remove(root) );
602     ROOTGUARD_TESTMETHOD( MakeEmptyFile(root) );
603     ROOTGUARD_TESTMETHOD( MakeDir(root) );
604 }
605
606 /**
607  * Name: path_size
608  * Description: check testing size of file
609  * Expected: correct size
610  */
611 RUNNER_TEST(path_size)
612 {
613     DPL::ScopedDir sd(rootTest);
614     Path path = Path(rootTest) / "touch.txt";
615     DPL::Utils::MakeEmptyFile(path);
616     RUNNER_ASSERT(path.Size() == 0);
617
618     {
619         DPL::FileOutput out(path.Fullpath());
620         DPL::BinaryQueue bq;
621         bq.AppendCopy("123456789", 9);
622         out.Write(bq, bq.Size());
623         out.Close();
624         RUNNER_ASSERT(path.Size() == 9);
625     }
626
627     {
628         DPL::FileOutput out(path.Fullpath());
629         DPL::BinaryQueue bq;
630         bq.AppendCopy("123456789", 4);
631         out.Write(bq, bq.Size());
632         out.Close();
633         RUNNER_ASSERT(path.Size() == 4);
634     }
635 }
636
637 /**
638 Name: path_copy
639 Description: tests path coping directory andfiles
640 Expected: coping should be done
641 */
642 RUNNER_TEST(path_copy_directory)
643 {
644     DPL::ScopedDir sd(rootTest);
645
646     Path path = Path(rootTest) / "sourceDir";
647     Path innerPath = Path(rootTest) / "sourceDir" / "level1" ;
648     Path innerPath2 = Path(rootTest) / "sourceDir" / "level1" / "level2";
649     Path file1 = Path(rootTest) / "sourceDir" / "level1" / "level2" / "file1.txt";
650     Path file2 = Path(rootTest) / "sourceDir" / "level1" / "level2" / "file2.txt";
651     Path file3 = Path(rootTest) / "sourceDir" / "level1" / "file3.txt";
652     Path file4 = Path(rootTest) / "sourceDir" /  "file4.txt";
653
654     Path tfile1 = Path(rootTest) / "targetDir" / "level1" / "level2" / "file1.txt";
655     Path tfile2 = Path(rootTest) / "targetDir" / "level1" / "level2" / "file2.txt";
656     Path tfile3 = Path(rootTest) / "targetDir" / "level1" / "file3.txt";
657     Path tfile4 = Path(rootTest) / "targetDir" /  "file4.txt";
658
659     Path target = Path(rootTest) / "targetDir";
660
661     DPL::Utils::MakeDir(path);
662     DPL::Utils::MakeDir(innerPath);
663     DPL::Utils::MakeDir(innerPath2);
664     DPL::Utils::MakeEmptyFile(file1);
665     DPL::Utils::MakeEmptyFile(file2);
666     DPL::Utils::MakeEmptyFile(file3);
667     DPL::Utils::MakeEmptyFile(file4);
668
669     DPL::Utils::CopyDir(path, target);
670
671     RUNNER_ASSERT_MSG(tfile1.Exists(), tfile1.Fullpath() + " not exists");
672     RUNNER_ASSERT_MSG(tfile1.IsFile(), tfile1.Fullpath() + " is not file");
673     RUNNER_ASSERT_MSG(tfile2.Exists(), tfile2.Fullpath() + " not exists");
674     RUNNER_ASSERT_MSG(tfile2.IsFile(), tfile2.Fullpath() + " is not file");
675     RUNNER_ASSERT_MSG(tfile3.Exists(), tfile3.Fullpath() + " not exists");
676     RUNNER_ASSERT_MSG(tfile3.IsFile(), tfile3.Fullpath() + " is not file");
677     RUNNER_ASSERT_MSG(tfile4.Exists(), tfile4.Fullpath() + " not exists");
678     RUNNER_ASSERT_MSG(tfile4.IsFile(), tfile4.Fullpath() + " is not file");
679 }
680
681 /*
682 Name: path_copy_inner
683 Description: tests path coping to subdirectory
684 Expected: coping shoudl fail
685 */
686 RUNNER_TEST(path_copy_inner)
687 {
688     DPL::ScopedDir sd(rootTest);
689
690     Path path = Path(rootTest) / "touchDir";
691     Path inner = Path(rootTest) / "touchDir" / "innerDirectory";
692
693     bool exceptionCatched = false;
694     Try
695     {
696         DPL::Utils::CopyDir(path, inner);
697     }
698     Catch(DPL::Utils::Path::CannotCopy)
699     {
700         exceptionCatched = true;
701     }
702     RUNNER_ASSERT_MSG(exceptionCatched, "Copy should fail");
703 }
704
705 /*
706 Name: issubpath
707 Description: tests method compare if one path is subpath of another
708 Expected: correct results
709 */
710 RUNNER_TEST(path_issubpath)
711 {
712     Path path1 = Path(rootTest) / "touchDir/asd/sdf";
713     Path path2 = Path(rootTest) / "touchDir/asd/sdf/123";
714     Path path3 = Path(rootTest) / "touchDir/asd/sdno";
715     Path path4 = Path("/");
716
717     RUNNER_ASSERT(path1.isSubPath(path2));
718     RUNNER_ASSERT(!path1.isSubPath(path3));
719     RUNNER_ASSERT(!path1.isSubPath(path4));
720
721     RUNNER_ASSERT(!path2.isSubPath(path1));
722     RUNNER_ASSERT(!path2.isSubPath(path3));
723     RUNNER_ASSERT(!path2.isSubPath(path4));
724
725     RUNNER_ASSERT(path4.isSubPath(path1));
726     RUNNER_ASSERT(path4.isSubPath(path2));
727     RUNNER_ASSERT(path4.isSubPath(path3));
728 }
729
730 /*
731 Name: path_rename_same
732 Description: tests if renam does not brokens file if target location is equal to source location
733 Expected: path is avaliable
734 */
735 RUNNER_TEST(path_rename_same)
736 {
737     DPL::ScopedDir sd(rootTest);
738
739     struct stat st;
740     memset(&st, 0, sizeof(struct stat));
741     Path path = Path(rootTest) / "touchDir";
742
743     MakeDir(path);
744     Rename(path, path);
745     RUNNER_ASSERT(path.Exists());
746 }
747
748 /*
749 Name: path_iterate_not_directory
750 Description: iterates not a directory
751 Expected: success full constrution
752 */
753 RUNNER_TEST(path_iterate_not_directory)
754 {
755     DPL::ScopedDir sd(rootTest);
756
757     Path fileTest = Path(rootTest) / "file.txt";
758     MakeEmptyFile(fileTest);
759
760     bool passed = false;
761     Try
762     {
763         FOREACH(file, fileTest)
764         {
765
766         }
767     }
768     Catch(Path::NotDirectory)
769     {
770         passed = true;
771     }
772     RUNNER_ASSERT(passed);
773 }
774
775 /*
776 Name: path_construction
777 Description: constructs paths in many ways
778 Expected: success full constrution
779 */
780 RUNNER_TEST(path_iterate_empty_directory)
781 {
782     DPL::ScopedDir sd(rootTest);
783
784     Path dirTest = Path(rootTest) / "directory";
785     MakeDir(dirTest);
786
787     bool passed = true;
788     Try
789     {
790         FOREACH(file, dirTest)
791         {
792             passed = false;
793             LogError("Directory should be empty");
794         }
795     }
796     Catch(Path::NotDirectory)
797     {
798         passed = false;
799         LogError("Directory should exists");
800     }
801     RUNNER_ASSERT(passed);
802 }
803
804 /*
805 Name: path_construction
806 Description: constructs paths in many ways
807 Expected: success full constrution
808 */
809 RUNNER_TEST(path_iterate_notempty_directory)
810 {
811     DPL::ScopedDir sd(rootTest);
812
813     Path dirTest = Path(rootTest) / "directory";
814
815     Path path1 = Path(rootTest) / "directory" / "file1";
816     Path path2 = Path(rootTest) / "directory" / "file2";
817     Path path3 = Path(rootTest) / "directory" / "file3";
818
819     std::set<std::string> resultSet;
820     std::set<std::string> testSet;
821     testSet.insert(path1.Fullpath());
822     testSet.insert(path2.Fullpath());
823     testSet.insert(path3.Fullpath());
824
825     MakeDir(dirTest);
826     MakeEmptyFile(path1);
827     MakeEmptyFile(path2);
828     MakeEmptyFile(path3);
829
830     FOREACH(file, dirTest)
831     {
832         resultSet.insert(file->Fullpath());
833     }
834
835     RUNNER_ASSERT_MSG(testSet == resultSet, "Testing");
836 }
837
838 /*
839 Name: path_construction
840 Description: constructs paths in many ways
841 Expected: success full constrution
842 */
843 RUNNER_TEST(path_iterator_copy_constructor)
844 {
845     DPL::ScopedDir sd(rootTest);
846
847     Path dirTest = Path(rootTest) / "directory";
848
849     Path path1 = Path(rootTest) / "directory" / "file1";
850     Path path2 = Path(rootTest) / "directory" / "file2";
851     Path path3 = Path(rootTest) / "directory" / "file3";
852
853     MakeDir(dirTest);
854     MakeEmptyFile(path1);
855     MakeEmptyFile(path2);
856     MakeEmptyFile(path3);
857
858     std::shared_ptr<Path::Iterator> iter1(new Path::Iterator((Path(rootTest) / "directory").Fullpath().c_str()));
859
860     //as it's input iterator it's guaranteed for one element to be iterate only once
861     (*iter1)++;
862     std::shared_ptr<Path::Iterator> iter2(new Path::Iterator( (*iter1)));
863     iter1.reset();
864     (*iter2)++;
865     ++(*iter2);
866     RUNNER_ASSERT_MSG(*iter2 == dirTest.end(), "Iterator is in broken state");
867     iter2.reset();
868 }
869
870 /*
871 Name: path_extension_test
872 Description: Tests if file extension is correct
873 Expected: Proper recognition of extensions
874 */
875 RUNNER_TEST(path_extension_test)
876 {
877     Path path1("/path/to/file.dot");
878     Path path2("/path/to/file..dot");
879     Path path3("/path/to/file..dot.");
880     Path path4("/path/to/file..dot.dot");
881     Path path5("/path/to.dot/file");
882     Path path6("./path/to/file");
883     Path path7("./path/to/file");
884     Path path8("/path/../file.xml");
885     Path path9("/path/../file.XML");
886     Path path10("/path/../file.myfileextension");
887
888     RUNNER_ASSERT(path1.Extension() == "dot");
889     RUNNER_ASSERT(path2.Extension() == "dot");
890     RUNNER_ASSERT(path3.Extension() == "");
891     RUNNER_ASSERT(path4.Extension() == "dot");
892     RUNNER_ASSERT(path5.Extension() == "");
893     RUNNER_ASSERT(path6.Extension() == "");
894     RUNNER_ASSERT(path7.Extension() == "");
895     RUNNER_ASSERT(path8.Extension() == "xml");
896     RUNNER_ASSERT(path9.Extension() != "xml");
897     RUNNER_ASSERT(path10.Extension() == "myfileextension");
898 }
899
900 /*
901 Name: path_has_extension_test
902 Description: Tests if file extension is correct
903 Expected: Proper recognition of extensions
904 */
905 RUNNER_TEST(path_has_extension_test)
906 {
907
908     Path dirTest = Path("extension");
909
910     Path path1 = dirTest / "file1.XML";
911     Path path2 = dirTest / "file2.JPG";
912     Path path3 = dirTest / "file3.";
913     Path path4 = dirTest / "file4";
914     Path path5 = dirTest / "file5.HTML";
915     Path path6 = dirTest / "file6.JS";
916     Path path7 = dirTest / "file7.VERY_VERY_LONG_EXTENSION";
917     Path path8 = dirTest / "file8.VERY.VERY.LONG.EXTENSION.WITH.DOTS";
918
919     RUNNER_ASSERT_MSG(path1.hasExtension("XML"), "Problem with comparison");
920     RUNNER_ASSERT_MSG(path2.hasExtension("JPG"), "Problem with comparison");
921     RUNNER_ASSERT_MSG(path5.hasExtension("HTML"), "Problem with comparison");
922     RUNNER_ASSERT_MSG(path6.hasExtension("JS"), "Problem with comparison");
923     RUNNER_ASSERT_MSG(path7.hasExtension("VERY_VERY_LONG_EXTENSION"),
924             "Problem with comparison");
925     RUNNER_ASSERT_MSG(path8.hasExtension("DOTS"), "Problem with comparison");
926
927     RUNNER_ASSERT_MSG(!path1.hasExtension(".XML"),
928             "Wrong argument in hasExtension() function");
929     RUNNER_ASSERT_MSG(!path1.hasExtension("MXL"),
930             "Wrong argument in hasExtension() function");
931     RUNNER_ASSERT_MSG(!path2.hasExtension(".JPG"),
932             "Wrong argument in hasExtension() function");
933     RUNNER_ASSERT_MSG(!path5.hasExtension(".HTML"),
934             "Wrong argument in hasExtension() function");
935     RUNNER_ASSERT_MSG(!path6.hasExtension(".JS"),
936             "Wrong argument in hasExtension() function");
937
938     RUNNER_ASSERT_MSG(path3.hasExtension(""), "Extension length is 0");
939
940     RUNNER_ASSERT_MSG(path4.hasExtension(""), "Extension length is 0");
941 }
942
943 /*
944 Name: path_create_temp_dir
945 Description: tests if temp dir was created
946 Expected: temp dir exists
947 */
948 RUNNER_TEST(path_create_temp_dir)
949 {
950     Path p1 = CreateTempPath(Path("/usr/tmp/"));
951     Path p2 = CreateTempPath(Path("/opt/usr/apps/tmp/"));
952     Path p3 = CreateTempPath(Path("/opt/usr/apps/tmp/"));
953
954     RUNNER_ASSERT_MSG(p1.Exists(), "Temp dir doesn't exists");
955     RUNNER_ASSERT_MSG(p2.Exists(), "Temp dir doesn't exists");
956     RUNNER_ASSERT_MSG(p3.Exists(), "Temp dir doesn't exists");
957     RUNNER_ASSERT_MSG(p2.Fullpath() != p3.Fullpath(), "Each temp path should be unique due to having tv_usec in dir name.");
958 }