7 #include <boost/test/unit_test.hpp>
9 #include "zypp/base/Logger.h"
10 #include "zypp/base/Exception.h"
11 #include "zypp/PathInfo.h"
12 #include "zypp/TmpPath.h"
14 using boost::unit_test::test_suite;
15 using boost::unit_test::test_case;
19 using namespace zypp::filesystem;
23 * bool is_checksum( const Pathname & file, const CheckSum &checksum );
24 * std::string checksum( const Pathname & file, const std::string &algorithm );
26 BOOST_AUTO_TEST_CASE(pathinfo_checksum_test)
28 const char *buffer = "I will test the checksum of this";
30 ofstream str(file.path().asString().c_str(),ofstream::out);
33 ZYPP_THROW(Exception("cant open file"));
39 CheckSum file_sha1("sha1", "142df4277c326f3549520478c188cab6e3b5d042");
40 CheckSum file_md5("md5", "f139a810b84d82d1f29fc53c5e59beae");
42 BOOST_CHECK_EQUAL( checksum( file.path(), "sha1"), "142df4277c326f3549520478c188cab6e3b5d042" );
43 BOOST_CHECK_EQUAL( checksum( file.path(), "md5"), "f139a810b84d82d1f29fc53c5e59beae" );
45 BOOST_REQUIRE( is_checksum( file.path(), file_sha1 ) );
46 BOOST_REQUIRE( is_checksum( file.path(), file_md5 ) );
49 BOOST_AUTO_TEST_CASE(pathinfo_is_exist_test)
52 Pathname subdir("text with spaces");
54 BOOST_CHECK_EQUAL( filesystem::mkdir(dir.path() + subdir), 0 );
56 Pathname filepath = (dir.path() + subdir+ "filename");
57 ofstream str(filepath.asString().c_str(),ofstream::out);
58 str << "foo bar" << endl;
62 BOOST_CHECK( PathInfo(filepath).isExist() );
65 BOOST_AUTO_TEST_CASE(pathipathinfo_misc_test)
69 PathInfo info(dir.path());
70 BOOST_CHECK(info.isDir());
73 BOOST_AUTO_TEST_CASE(pathinfo_expandlink_test)
80 Pathname file(dir / "file");
81 ofstream str(file.asString().c_str(),ofstream::out);
82 str << "foo bar" << endl;
86 // expandlink should return the original Pathname if it does not point to a link
87 BOOST_CHECK_EQUAL( file, filesystem::expandlink(file) );
91 // create a link to that file
92 Pathname link1(dir / "link1");
93 BOOST_CHECK_EQUAL( filesystem::symlink(file, link1), 0);
95 // does the link expand to the file?
96 BOOST_CHECK_EQUAL( file, filesystem::expandlink(link1) );
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());
107 // expandlink should return an empty Pathname for a broken link
108 BOOST_CHECK_EQUAL( Pathname(), filesystem::expandlink(brokenlink) );
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) );
118 cout << brokenlink << " -> " << filesystem::expandlink(brokenlink) << endl;