/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
/** detailed wrong message.
*/ static OString errorToString(const osl::FileBase::RC _nError)
{
OString sResult; switch (_nError) { case osl::FileBase::E_None:
sResult = "Success"_ostr; break; case osl::FileBase::E_PERM:
sResult = "Operation not permitted"_ostr; break; case osl::FileBase::E_NOENT:
sResult = "No such file or directory"_ostr; break; case osl::FileBase::E_EXIST:
sResult = "Already Exist"_ostr; break; case osl::FileBase::E_ACCES:
sResult = "Permission denied"_ostr; break; case osl::FileBase::E_INVAL:
sResult = "The format of the parameters was not valid"_ostr; break; case osl::FileBase::E_NOTDIR:
sResult = "Not a directory"_ostr; break; case osl::FileBase::E_ISDIR:
sResult = "Is a directory"_ostr; break; case osl::FileBase::E_BADF:
sResult = "Bad file"_ostr; break; case osl::FileBase::E_NOTEMPTY:
sResult = "The directory is not empty"_ostr; break; default:
sResult = "Unknown Error"_ostr; break;
} return sResult;
}
static OString errorToStr(osl::FileBase::RC const& nError)
{
OString suBuf = "The returned error is: " +
errorToString(nError) + "!\n"; return suBuf;
}
/** compare two TimeValue, unit is "ms", since Windows time precision is better than UNX.
*/ /* FIXME: the above assertion is bogus */
#if (defined UNX) // precision of time in Windows is better than UNX # define delta 2000 // time precision, 2000ms #else # define delta 1800 // time precision, 1.8s #endif
/** compare two OUString file name.
*/ staticbool compareFileName(const OUString & ustr1, const OUString & ustr2)
{ bool bOk; // on Windows, the separator is '\', so here change to '/', then compare #ifdefined(_WIN32)
OUString ustr1new,ustr2new;
sal_Unicode reverseSlash = '\\';
if (ustr1.lastIndexOf(reverseSlash) != -1)
ustr1new = ustr1.replace(reverseSlash,'/'); else
ustr1new = ustr1; if (ustr2.lastIndexOf(reverseSlash) != -1)
ustr2new = ustr2.replace(reverseSlash,'/'); else
ustr2new = ustr2;
bOk = ustr1new.equalsIgnoreAsciiCase(ustr2new); #else
bOk = ustr1.equalsIgnoreAsciiCase(ustr2); #endif return bOk;
}
/** simple version to judge if a file name or directory name is a URL or a system path, just to see if it is start with "file:///";.
*/ staticbool isURL(const OUString& pathname)
{ return pathname.startsWith(aPreURL);
}
/** concat two part to form a URL or system path, add PATH_SEPARATOR between them if necessary, add "file:///" to beginning if necessary.
*/ staticvoid concatURL(OUString & pathname1, const OUString & pathname2)
{ // check if pathname1 is full qualified URL; if (!isURL(pathname1))
{
OUString aPathName = pathname1.copy(0);
osl::FileBase::getFileURLFromSystemPath(pathname1, aPathName); // convert if not full qualified URL
pathname1 = aPathName.copy(0);
}
// check if '/' is in the end of pathname1 or at the begin of pathname2; if (!pathname1.endsWith(aSlashURL) && !pathname2.startsWith(aSlashURL))
pathname1 += aSlashURL;
pathname1 += pathname2;
}
/** create a temp test file using OUString name of full qualified URL or system path.
*/ staticvoid createTestFile(const OUString& filename)
{
OUString aPathURL = filename.copy(0);
osl::FileBase::RC nError;
if (!isURL(filename))
osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
/** create a temp test file using OUString name of full qualified URL or system path in a base directory.
*/ staticvoid createTestFile(const OUString& basename, const OUString& filename)
{
OUString aBaseURL = basename.copy(0);
/** delete a temp test file using OUString name of full qualified URL or system path in a base directory.
*/ staticvoid deleteTestFile(const OUString& basename, const OUString& filename)
{
OUString aBaseURL = basename.copy(0);
/** create a temp test directory using OUString name of full qualified URL or system path.
*/ staticvoid createTestDirectory(const OUString& dirname)
{
OUString aPathURL = dirname.copy(0);
osl::FileBase::RC nError;
if (!isURL(dirname))
osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
nError = Directory::create(aPathURL); if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST))
printf("createTestDirectory failed: %d!\n", int(nError));
}
/** create a temp test directory using OUString name of full qualified URL or system path in a base directory.
*/ staticvoid createTestDirectory(const OUString& basename, const OUString& dirname)
{
OUString aBaseURL = basename.copy(0);
/** delete a temp test directory using OUString name of full qualified URL or system path.
*/ staticvoid deleteTestDirectory(const OUString& dirname)
{
OUString aPathURL = dirname.copy(0); if (!isURL(dirname))
osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
Directory testDir(aPathURL); if (testDir.isOpen())
testDir.close(); // close if still open.
/** delete a temp test directory using OUString name of full qualified URL or system path in a base directory.
*/ staticvoid deleteTestDirectory(const OUString& basename, const OUString& dirname)
{
OUString aBaseURL = basename.copy(0);
/** Check for the file and directory access right.
*/ enumclass oslCheckMode {
Exist,
OpenAccess,
ReadAccess,
WriteAccess
};
}
/** check if the file exist
*/ staticbool ifFileExist(const OUString & str)
{
File testFile(str); return (testFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None);
}
/** check if the file can be written
*/ staticbool ifFileCanWrite(const OUString & str)
{ // on Windows, the file has no write right, but can be written #ifdef _WIN32 bool bCheckResult = false;
OUString aUStr = str.copy(0); if (isURL(str))
osl::FileBase::getSystemPathFromFileURL(str, aUStr);
OString aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US); constchar *path = aString.getStr(); if ((_access(path, 2)) != -1)
bCheckResult = true; // on UNX, just test if open success with osl_File_OpenFlag_Write #else
File testFile(str); bool bCheckResult = (testFile.open(osl_File_OpenFlag_Write) == osl::FileBase::E_None); #endif return bCheckResult;
}
OString aString = msg +
OString::Concat(": the returned value is '") +
returnVal + "', but the value should be '" +
rightVal + "'."; return aString;
}
#if (defined UNX) /* chmod() method is different in Windows */ /** Change file mode, two version in UNIX and Windows;.
*/ staticvoid changeFileMode(OUString & filepath, sal_Int32 mode)
{
OString aString;
OUString aUStr = filepath.copy(0);
if (isURL(filepath))
osl::FileBase::getSystemPathFromFileURL(filepath, aUStr);
aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US); int ret = chmod(aString.getStr(), mode);
CPPUNIT_ASSERT_EQUAL(0, ret);
} #else staticvoid hideFile(const OUString& filepath)
{
OUString aSysPath(filepath);
if (isURL(filepath))
osl::FileBase::getSystemPathFromFileURL(filepath, aSysPath);
bool ret = SetFileAttributesW(o3tl::toW(aSysPath.getStr()), FILE_ATTRIBUTE_HIDDEN);
CPPUNIT_ASSERT(ret);
} #endif
if (nError == osl::FileBase::E_None)
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: ResultURL is not equal to expected URL ", _suAssumeResultStr, suResultURL);
}
}
// if the given string is gt length 0, // we check also this string
OString sStr = OUStringToOString(suStr, RTL_TEXTENCODING_UTF8);
OString sError = errorToString(nError);
/** Test for getSystemPathFromFileURL() this test is split into 2 different OS tests, the first function checkUNXBehaviour... runs only on Unix based Systems, the second only on windows based systems the first parameter are a file URL where we want to get the system path of, the second parameter is the assumed error of the osl_getSystemPathFromFileURL() function, the third parameter is the assumed result string, the string will only test, if its length is greater than 0
*/
void SystemPath_FileURL::getSystemPathFromFileURL_001_21()
{ /* From RFC3986, "2.2. Reserved Characters":
"The purpose of reserved characters is to provide a set of delimiting characters that are distinguishable from other data within a URI. URIs that differ in the replacement of a reserved character with its corresponding percent-encoded octet are not equivalent. Percent- encoding a reserved character, or decoding a percent-encoded octet that corresponds to a reserved character, will change how the URI is interpreted by most applications. Thus, characters in the reserved set are protected from normalization and are therefore safe to be used by scheme-specific and producer-specific algorithms for delimiting data subcomponents within a URI."
In other words, %2F ("/") is NOT the same as /.
*/
OString sURL("%2F"_ostr);
checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
}
class searchFileURL : public CppUnit::TestFixture
{ private:
OUString aUStr;
public: void searchFileURL_001()
{ /* search file is passed by system filename */ auto nError1 = osl::FileBase::searchFileURL(aTmpName1, aUserDirectorySys, aUStr); /* search file is passed by full qualified file URL */ auto nError2 = osl::FileBase::searchFileURL(aCanURL1, aUserDirectorySys, aUStr); /* search file is passed by relative file path */ auto nError3 = osl::FileBase::searchFileURL(aRelURL4, aUserDirectorySys, aUStr);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
osl::FileBase::E_NOENT, nError1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
osl::FileBase::E_NOENT, nError2);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
osl::FileBase::E_NOENT, nError3);
}
void searchFileURL_002()
{ #ifndef UNX /* search file is passed by system filename */
OUString strRootSys = INetURLObject(aTempDirectoryURL).GetLastName(); auto nError1 = osl::FileBase::searchFileURL(aTempDirectorySys, strRootSys, aUStr); bool bOk1 = compareFileName(aUStr, aTempDirectoryURL); /* search file is passed by full qualified file URL */ auto nError2 = osl::FileBase::searchFileURL(aTempDirectoryURL, strRootSys, aUStr); bool bOk2 = compareFileName(aUStr, aTempDirectoryURL); #ifndef _WIN32 /* search file is passed by relative file path */ auto nError3 = osl::FileBase::searchFileURL(aRelURL5, strRootSys, aUStr); bool bOk3 = compareFileName(aUStr, aTempDirectoryURL); #endif /* search file is passed by an exist file */
createTestFile(aCanURL1); auto nError4 = osl::FileBase::searchFileURL(aCanURL4, aUserDirectorySys, aUStr); bool bOk4 = compareFileName(aUStr, aCanURL1);
deleteTestFile(aCanURL1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
osl::FileBase::E_None, nError2); #ifndef _WIN32
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
osl::FileBase::E_None, nError3); #endif
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
osl::FileBase::E_None, nError4);
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
bOk1);
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
bOk2); #ifndef _WIN32
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
bOk3); #endif
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
bOk4); #endif
}
void searchFileURL_003()
{
OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path"); auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr); bool bOk = compareFileName(aUStr, aUserDirectoryURL);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
bOk);
}
void searchFileURL_004()
{
OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path/../name"); auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr); bool bOk = compareFileName(aUStr, aUserDirectoryURL);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
bOk);
}
void searchFileURL_005()
{ auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aNullURL, aUStr); bool bOk = compareFileName(aUStr, aUserDirectoryURL);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is NULL",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is NULL",
bOk);
}
void getTempDirURL_002()
{
CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
checkDirectory(aUStr, oslCheckMode::OpenAccess));
CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
checkDirectory(aUStr, oslCheckMode::ReadAccess));
CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
checkDirectory(aUStr, oslCheckMode::WriteAccess));
}
void createTempFile_001()
{ auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
File testFile(*pUStr_FileURL); auto nError2 = testFile.open(osl_File_OpenFlag_Create);
if (nError2 == osl::FileBase::E_EXIST)
{
osl_closeFile(*pHandle);
deleteTestFile(*pUStr_FileURL);
}
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: create temp file and test the existence",
(pHandle != nullptr));
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
osl::FileBase::E_EXIST, nError2);
}
void createTempFile_002()
{ bool bOK = false; auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
File testFile(*pUStr_FileURL); auto nError2 = testFile.open(osl_File_OpenFlag_Create);
CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
(pHandle != nullptr));
CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
osl::FileBase::E_EXIST, nError2);
// check file if have the write permission if (nError2 == osl::FileBase::E_EXIST)
{
bOK = ifFileCanWrite(*pUStr_FileURL);
osl_closeFile(*pHandle);
deleteTestFile(*pUStr_FileURL);
}
CPPUNIT_ASSERT_MESSAGE("test for open and write access rights, in (W32), it did not have write access right, but it should be writable.",
bOK);
}
void createTempFile_003()
{ auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), nullptr); // the temp file will be removed when return from createTempFile bool bOK = (pHandle != nullptr && nError1 == osl::FileBase::E_None); if (bOK)
osl_closeFile(*pHandle);
CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
bOK);
}
void createTempFile_004()
{ auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), nullptr, pUStr_FileURL.get()); bool bOK = (pUStr_FileURL != nullptr);
CPPUNIT_ASSERT(bOK);
File testFile(*pUStr_FileURL); auto nError2 = testFile.open(osl_File_OpenFlag_Create);
deleteTestFile(*pUStr_FileURL);
CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
osl::FileBase::E_None, nError1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
osl::FileBase::E_EXIST, nError2);
CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
bOK);
Link is not defined in Windows, and on Linux, we can not get the directory item of the linked file. We have to defer to filesystems, normal filesystems support links (EXT2, ...), castrated filesystems don't have links (FAT, FAT32) and Windows NT NTFS support links, but the Windows API doesn't :-(
*/ void isValid_003()
{ #if 0 #ifdefined (UNX)
sal_Int32 fd;
// create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
// testDirectory is "/tmp/PID/tmpdir/"
Directory testDirectory(aTmpName3);
testDirectory.open();
OUString aFileName ("link.file"); bool bOk = false; while (true)
{
osl::FileBase::RC nError1 = testDirectory.getNextItem(rItem_link, 4);
// testing the method // inline Type getFileType() const
class getFileType : public CppUnit::TestFixture
{ private:
DirectoryItem m_aItem_1, m_aItem_2, m_aVolumeItem, m_aFifoItem;
DirectoryItem m_aLinkItem, m_aSocketItem, m_aSpecialItem;
public: void setUp() override
{ // create a tempfile: $TEMP/tmpdir/tmpname. // a tempdirectory: $TEMP/tmpdir/tmpdir. // use $ROOT/staroffice as volume ---> use dev/fd as volume. // and get their directory item.
createTestDirectory(aTmpName3);
createTestFile(aTmpName3, aTmpName2);
createTestDirectory(aTmpName3, aTmpName1);
std::unique_ptr<Directory> xDir(new Directory(aTmpName3)); auto nError1 = xDir->open();
CPPUNIT_ASSERT_EQUAL_MESSAGE("open aTmpName3 failed!", osl::FileBase::E_None, nError1); // getNextItem can not assure which item retrieved
nError1 = xDir->getNextItem(m_aItem_1, 1);
CPPUNIT_ASSERT_EQUAL_MESSAGE("get first item failed!", osl::FileBase::E_None, nError1);
nError1 = xDir->getNextItem(m_aItem_2);
CPPUNIT_ASSERT_EQUAL_MESSAGE("get second item failed!", osl::FileBase::E_None, nError1);
xDir->close(); // FIXME mindy: failed on my RH9, so removed temporarily // nError1 = DirectoryItem::get(aVolURL2, m_aVolumeItem); // CPPUNIT_ASSERT_MESSAGE("get volume item failed!", osl::FileBase::E_None == nError1);
}
void tearDown() override
{ // remove all in $TEMP/tmpdir.
deleteTestDirectory(aTmpName3, aTmpName1);
deleteTestFile(aTmpName3, aTmpName2);
deleteTestDirectory(aTmpName3);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.