79eaa0d3d0c3963524c12ba4787bf28bad0c1d11
[platform/upstream/libzypp.git] / tests / zypp / PathInfo_test.cc
1
2 #include <iostream>
3 #include <fstream>
4 #include <list>
5 #include <string>
6
7 #include <boost/test/unit_test.hpp>
8
9 #include "zypp/base/Logger.h"
10 #include "zypp/base/Exception.h"
11 #include "zypp/PathInfo.h"
12 #include "zypp/TmpPath.h"
13
14 using boost::unit_test::test_suite;
15 using boost::unit_test::test_case;
16
17 using namespace std;
18 using namespace zypp;
19 using namespace zypp::filesystem;
20
21 /**
22  * Test case for
23  * bool is_checksum( const Pathname & file, const CheckSum &checksum );
24  * std::string checksum( const Pathname & file, const std::string &algorithm );
25  */
26 BOOST_AUTO_TEST_CASE(pathinfo_checksum_test)
27 {
28   const char *buffer = "I will test the checksum of this";
29   TmpFile file;
30   ofstream str(file.path().asString().c_str(),ofstream::out);
31
32   if (!str.good())
33     ZYPP_THROW(Exception("cant open file"));
34
35   str << buffer;
36   str.flush();
37   str.close();
38
39   CheckSum file_sha1("sha1", "142df4277c326f3549520478c188cab6e3b5d042");
40   CheckSum file_md5("md5", "f139a810b84d82d1f29fc53c5e59beae");
41
42   BOOST_CHECK_EQUAL( checksum( file.path(), "sha1"), "142df4277c326f3549520478c188cab6e3b5d042" );
43   BOOST_CHECK_EQUAL( checksum( file.path(), "md5"), "f139a810b84d82d1f29fc53c5e59beae" );
44
45   BOOST_REQUIRE( is_checksum( file.path(), file_sha1 ) );
46   BOOST_REQUIRE( is_checksum( file.path(), file_md5 ) );
47 }
48
49 BOOST_AUTO_TEST_CASE(pathinfo_is_exist_test)
50 {
51   TmpDir dir;
52   Pathname subdir("text with spaces");
53   // create a fake file
54   BOOST_CHECK_EQUAL( filesystem::mkdir(dir.path() + subdir), 0 );
55   
56   Pathname filepath = (dir.path() + subdir+ "filename");
57   ofstream str(filepath.asString().c_str(),ofstream::out);
58   str << "foo bar" << endl;
59   str.flush();
60   str.close();
61   
62   BOOST_CHECK( PathInfo(filepath).isExist() );
63 }
64
65 BOOST_AUTO_TEST_CASE(pathipathinfo_misc_test)
66 {
67   TmpDir dir;
68
69   PathInfo info(dir.path());
70   BOOST_CHECK(info.isDir());
71 }
72
73 BOOST_AUTO_TEST_CASE(pathinfo_expandlink_test)
74 {
75   TmpDir dir;
76
77   // ---- not a link
78
79   // create a file
80   Pathname file(dir / "file");
81   ofstream str(file.asString().c_str(),ofstream::out);
82   str << "foo bar" << endl;
83   str.flush();
84   str.close();
85
86   // expandlink should return the original Pathname if it does not point to a link
87   BOOST_CHECK_EQUAL( file, filesystem::expandlink(file) );
88
89   // ---- valid link
90
91   // create a link to that file
92   Pathname link1(dir / "link1");
93   BOOST_CHECK_EQUAL( filesystem::symlink(file, link1), 0);
94
95   // does the link expand to the file?
96   BOOST_CHECK_EQUAL( file, filesystem::expandlink(link1) );
97
98   // ---- broken link
99
100   // create a link to a non-existent file
101   Pathname brokenlink(dir / "brokenlink");
102   Pathname non_existent(dir / "non-existent");
103   BOOST_CHECK_EQUAL( filesystem::symlink(non_existent, brokenlink), 0);
104   PathInfo info(brokenlink, PathInfo::LSTAT);
105   BOOST_CHECK(info.isLink());
106
107   // expandlink should return an empty Pathname for a broken link
108   BOOST_CHECK_EQUAL( Pathname(), filesystem::expandlink(brokenlink) );
109
110   // ---- cyclic link
111
112   // make the 'non-existent' a link to 'brokenlink' :O)
113   BOOST_CHECK_EQUAL( filesystem::symlink(brokenlink, non_existent), 0);
114   // expandlink should return an empty Pathname for such a cyclic link
115   BOOST_CHECK_EQUAL( Pathname(), filesystem::expandlink(brokenlink) );
116   BOOST_CHECK_EQUAL( Pathname(), filesystem::expandlink(non_existent) );
117
118   cout << brokenlink << " -> " << filesystem::expandlink(brokenlink) << endl;
119 }
120
121