Fix GNU C++ version check
[LibreOffice.git] / sal / qa / osl / file / osl_File.cxx
blob159f32ad7410733937e7f4a80668b519e62b60ec
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <rtl/byteseq.hxx>
22 #include <rtl/ustring.hxx>
24 #include <osl/file.hxx>
25 #include "osl_File_Const.h"
27 #include <cppunit/TestFixture.h>
28 #include <cppunit/extensions/HelperMacros.h>
30 #include <memory>
32 #ifdef _WIN32
33 #include <prewin.h>
34 #include <postwin.h>
35 #include <o3tl/char16_t2wchar_t.hxx>
36 #include <tools/urlobj.hxx>
37 #endif
39 using namespace osl;
41 /** detailed wrong message.
43 static OString errorToString(const osl::FileBase::RC _nError)
45 OString sResult;
46 switch (_nError) {
47 case osl::FileBase::E_None:
48 sResult = "Success"_ostr;
49 break;
50 case osl::FileBase::E_PERM:
51 sResult = "Operation not permitted"_ostr;
52 break;
53 case osl::FileBase::E_NOENT:
54 sResult = "No such file or directory"_ostr;
55 break;
56 case osl::FileBase::E_EXIST:
57 sResult = "Already Exist"_ostr;
58 break;
59 case osl::FileBase::E_ACCES:
60 sResult = "Permission denied"_ostr;
61 break;
62 case osl::FileBase::E_INVAL:
63 sResult = "The format of the parameters was not valid"_ostr;
64 break;
65 case osl::FileBase::E_NOTDIR:
66 sResult = "Not a directory"_ostr;
67 break;
68 case osl::FileBase::E_ISDIR:
69 sResult = "Is a directory"_ostr;
70 break;
71 case osl::FileBase::E_BADF:
72 sResult = "Bad file"_ostr;
73 break;
74 case osl::FileBase::E_NOTEMPTY:
75 sResult = "The directory is not empty"_ostr;
76 break;
77 default:
78 sResult = "Unknown Error"_ostr;
79 break;
81 return sResult;
84 static OString errorToStr(osl::FileBase::RC const& nError)
86 OString suBuf = "The returned error is: " +
87 errorToString(nError) +
88 "!\n";
89 return suBuf;
92 /** compare two TimeValue, unit is "ms", since Windows time precision is better than UNX.
94 /* FIXME: the above assertion is bogus */
96 #if (defined UNX) // precision of time in Windows is better than UNX
97 # define delta 2000 // time precision, 2000ms
98 #else
99 # define delta 1800 // time precision, 1.8s
100 #endif
102 static bool t_compareTime(TimeValue *m_aEndTime, TimeValue *m_aStartTime, sal_Int32 nDelta)
104 sal_Int32 nDeltaSeconds = m_aEndTime->Seconds - m_aStartTime->Seconds;
105 sal_Int32 nDeltaNanoSec = sal_Int32(m_aEndTime->Nanosec) - sal_Int32(m_aStartTime->Nanosec);
106 if (nDeltaNanoSec < 0)
108 nDeltaNanoSec = 1000000000 + nDeltaNanoSec;
109 nDeltaSeconds--;
112 sal_Int32 nDeltaMilliSec = (nDeltaSeconds * 1000) + (nDeltaNanoSec / 1000000);
113 return (nDeltaMilliSec < nDelta);
116 /** compare two OUString file name.
118 static bool compareFileName(const OUString & ustr1, const OUString & ustr2)
120 bool bOk;
121 // on Windows, the separator is '\', so here change to '/', then compare
122 #if defined(_WIN32)
123 OUString ustr1new,ustr2new;
124 sal_Unicode reverseSlash = '\\';
126 if (ustr1.lastIndexOf(reverseSlash) != -1)
127 ustr1new = ustr1.replace(reverseSlash,'/');
128 else
129 ustr1new = ustr1;
130 if (ustr2.lastIndexOf(reverseSlash) != -1)
131 ustr2new = ustr2.replace(reverseSlash,'/');
132 else
133 ustr2new = ustr2;
134 bOk = ustr1new.equalsIgnoreAsciiCase(ustr2new);
135 #else
136 bOk = ustr1.equalsIgnoreAsciiCase(ustr2);
137 #endif
138 return bOk;
141 /** simple version to judge if a file name or directory name is a URL or a system path, just to see if it
142 is start with "file:///";.
144 static bool isURL(const OUString& pathname)
146 return pathname.startsWith(aPreURL);
149 /** concat two part to form a URL or system path, add PATH_SEPARATOR between them if necessary, add "file:///" to beginning if necessary.
151 static void concatURL(OUString & pathname1, const OUString & pathname2)
153 // check if pathname1 is full qualified URL;
154 if (!isURL(pathname1))
156 OUString aPathName = pathname1.copy(0);
157 osl::FileBase::getFileURLFromSystemPath(pathname1, aPathName); // convert if not full qualified URL
158 pathname1 = aPathName.copy(0);
161 // check if '/' is in the end of pathname1 or at the begin of pathname2;
162 if (!pathname1.endsWith(aSlashURL) && !pathname2.startsWith(aSlashURL))
163 pathname1 += aSlashURL;
164 pathname1 += pathname2;
167 /** create a temp test file using OUString name of full qualified URL or system path.
169 static void createTestFile(const OUString& filename)
171 OUString aPathURL = filename.copy(0);
172 osl::FileBase::RC nError;
174 if (!isURL(filename))
175 osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
177 File aFile(aPathURL);
178 nError = aFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
179 if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST))
180 printf("createTestFile failed!\n");
182 aFile.close();
186 /** create a temp test file using OUString name of full qualified URL or system path in a base directory.
188 static void createTestFile(const OUString& basename, const OUString& filename)
190 OUString aBaseURL = basename.copy(0);
192 concatURL(aBaseURL, filename);
193 createTestFile(aBaseURL);
196 /** delete a temp test file using OUString name.
198 static void deleteTestFile(const OUString& filename)
200 OUString aPathURL = filename.copy(0);
201 osl::FileBase::RC nError;
203 if (!isURL(filename))
204 osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
206 nError = File::setAttributes(aPathURL, osl_File_Attribute_GrpWrite| osl_File_Attribute_OwnWrite| osl_File_Attribute_OthWrite); // if readonly, make writable.
207 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: set writable ", (osl::FileBase::E_None == nError) || (osl::FileBase::E_NOENT == nError));
209 nError = File::remove(aPathURL);
210 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: remove ", (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT));
213 /** delete a temp test file using OUString name of full qualified URL or system path in a base directory.
215 static void deleteTestFile(const OUString& basename, const OUString& filename)
217 OUString aBaseURL = basename.copy(0);
219 concatURL(aBaseURL, filename);
220 deleteTestFile(aBaseURL);
223 /** create a temp test directory using OUString name of full qualified URL or system path.
225 static void createTestDirectory(const OUString& dirname)
227 OUString aPathURL = dirname.copy(0);
228 osl::FileBase::RC nError;
230 if (!isURL(dirname))
231 osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
232 nError = Directory::create(aPathURL);
233 if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST))
234 printf("createTestDirectory failed: %d!\n", int(nError));
237 /** create a temp test directory using OUString name of full qualified URL or system path in a base directory.
239 static void createTestDirectory(const OUString& basename, const OUString& dirname)
241 OUString aBaseURL = basename.copy(0);
243 concatURL(aBaseURL, dirname);
244 createTestDirectory(aBaseURL);
247 /** delete a temp test directory using OUString name of full qualified URL or system path.
249 static void deleteTestDirectory(const OUString& dirname)
251 OUString aPathURL = dirname.copy(0);
252 if (!isURL(dirname))
253 osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
255 Directory testDir(aPathURL);
256 if (testDir.isOpen())
257 testDir.close(); // close if still open.
259 osl::FileBase::RC nError = Directory::remove(aPathURL);
261 OString strError = "In deleteTestDirectory function: remove Directory " +
262 OUStringToOString(aPathURL, RTL_TEXTENCODING_ASCII_US) + " -> result: " + OString::number(nError);
263 CPPUNIT_ASSERT_MESSAGE(strError.getStr(), (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT));
266 /** delete a temp test directory using OUString name of full qualified URL or system path in a base directory.
268 static void deleteTestDirectory(const OUString& basename, const OUString& dirname)
270 OUString aBaseURL = basename.copy(0);
272 concatURL(aBaseURL, dirname);
273 deleteTestDirectory(aBaseURL);
276 namespace {
278 /** Check for the file and directory access right.
280 enum class oslCheckMode {
281 Exist,
282 OpenAccess,
283 ReadAccess,
284 WriteAccess
289 /** check if the file exist
291 static bool ifFileExist(const OUString & str)
293 File testFile(str);
294 return (testFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None);
297 /** check if the file can be written
299 static bool ifFileCanWrite(const OUString & str)
301 // on Windows, the file has no write right, but can be written
302 #ifdef _WIN32
303 bool bCheckResult = false;
304 OUString aUStr = str.copy(0);
305 if (isURL(str))
306 osl::FileBase::getSystemPathFromFileURL(str, aUStr);
308 OString aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
309 const char *path = aString.getStr();
310 if ((_access(path, 2)) != -1)
311 bCheckResult = true;
312 // on UNX, just test if open success with osl_File_OpenFlag_Write
313 #else
314 File testFile(str);
315 bool bCheckResult = (testFile.open(osl_File_OpenFlag_Write) == osl::FileBase::E_None);
316 #endif
317 return bCheckResult;
320 static bool checkDirectory(const OUString& str, oslCheckMode nCheckMode)
322 OUString aUString;
323 DirectoryItem rItem;
324 osl::FileBase::RC rc;
325 bool bCheckResult= false;
327 Directory aDir(str);
328 rc = aDir.open();
330 if ((rc != osl::FileBase::E_NOENT) && (rc != osl::FileBase::E_ACCES))
332 switch (nCheckMode)
334 case oslCheckMode::Exist:
335 if (rc == ::osl::FileBase::E_None)
336 bCheckResult = true;
337 break;
338 case oslCheckMode::OpenAccess:
339 if (rc == osl::FileBase::E_None)
340 bCheckResult = true;
341 break;
342 case oslCheckMode::ReadAccess:
343 rc = aDir.getNextItem(rItem);
344 bCheckResult = (rc == osl::FileBase::E_None) || (rc == osl::FileBase::E_NOENT);
345 break;
346 case oslCheckMode::WriteAccess:
347 ((aUString += str) += aSlashURL) += aTmpName2;
348 if (Directory::create(aUString) == osl::FileBase::E_None)
350 bCheckResult = true;
351 rc = Directory::remove(aUString);
352 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
354 else
356 bCheckResult = false;
358 break;
360 default:
361 bCheckResult = false;
364 rc = aDir.close();
365 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
368 return bCheckResult;
371 /** construct error message
373 static OString outputError(const OString & returnVal, const OString & rightVal, const char * msg = "")
375 if (returnVal == rightVal)
376 return OString();
378 OString aString = msg +
379 OString::Concat(": the returned value is '") +
380 returnVal +
381 "', but the value should be '" +
382 rightVal +
383 "'.";
384 return aString;
387 #if (defined UNX) /* chmod() method is different in Windows */
388 /** Change file mode, two version in UNIX and Windows;.
390 static void changeFileMode(OUString & filepath, sal_Int32 mode)
392 OString aString;
393 OUString aUStr = filepath.copy(0);
395 if (isURL(filepath))
396 osl::FileBase::getSystemPathFromFileURL(filepath, aUStr);
398 aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
399 int ret = chmod(aString.getStr(), mode);
400 CPPUNIT_ASSERT_EQUAL(0, ret);
402 #else
403 static void hideFile(const OUString& filepath)
405 OUString aSysPath(filepath);
407 if (isURL(filepath))
408 osl::FileBase::getSystemPathFromFileURL(filepath, aSysPath);
410 bool ret = SetFileAttributesW(o3tl::toW(aSysPath.getStr()), FILE_ATTRIBUTE_HIDDEN);
411 CPPUNIT_ASSERT(ret);
413 #endif
415 #if 0
416 #if defined UNX
417 static OUString getCurrentPID();
418 #endif
419 #endif
421 // Beginning of the test cases for osl::FileBase class
423 namespace osl_FileBase
425 // testing the method
426 // static inline RC getAbsoluteFileURL(const OUString& ustrBaseDirectoryURL,
427 // const OUString& ustrRelativeFileURL,
428 // OUString& ustrAbsoluteFileURL)
430 class getAbsoluteFileURL : public CppUnit::TestFixture
432 public:
433 void check_getAbsoluteFileURL(OUString const& _suBaseURL,
434 OString const& _sRelativeURL,
435 osl::FileBase::RC _nAssumeError,
436 OUString const& _suAssumeResultStr);
438 void getAbsoluteFileURL_001_1();
439 void getAbsoluteFileURL_001_2();
440 void getAbsoluteFileURL_001_3();
441 void getAbsoluteFileURL_001_4();
442 void getAbsoluteFileURL_001_5();
443 void getAbsoluteFileURL_001_6();
444 void getAbsoluteFileURL_001_7();
445 void getAbsoluteFileURL_001_8();
446 void getAbsoluteFileURL_002();
447 void getAbsoluteFileURL_003();
448 void getAbsoluteFileURL_004();
450 CPPUNIT_TEST_SUITE(getAbsoluteFileURL);
451 CPPUNIT_TEST(getAbsoluteFileURL_001_1);
452 CPPUNIT_TEST(getAbsoluteFileURL_001_2);
453 CPPUNIT_TEST(getAbsoluteFileURL_001_3);
454 CPPUNIT_TEST(getAbsoluteFileURL_001_4);
455 CPPUNIT_TEST(getAbsoluteFileURL_001_5);
456 CPPUNIT_TEST(getAbsoluteFileURL_001_6);
457 CPPUNIT_TEST(getAbsoluteFileURL_001_7);
458 CPPUNIT_TEST(getAbsoluteFileURL_001_8);
459 CPPUNIT_TEST(getAbsoluteFileURL_002);
460 CPPUNIT_TEST(getAbsoluteFileURL_003);
461 CPPUNIT_TEST(getAbsoluteFileURL_004);
462 CPPUNIT_TEST_SUITE_END();
465 void getAbsoluteFileURL::check_getAbsoluteFileURL(OUString const& _suBaseURL,
466 OString const& _sRelativeURL,
467 osl::FileBase::RC _nAssumeError,
468 OUString const& _suAssumeResultStr)
470 OUString suRelativeURL = OStringToOUString(_sRelativeURL, RTL_TEXTENCODING_UTF8);
471 OString sBaseURL = OUStringToOString(_suBaseURL, RTL_TEXTENCODING_UTF8);
472 OUString suResultURL;
473 osl::FileBase::RC nError = osl::FileBase::getAbsoluteFileURL(_suBaseURL, suRelativeURL, suResultURL);
474 OString sResultURL = OUStringToOString(suResultURL, RTL_TEXTENCODING_UTF8);
475 OString sError = errorToString(nError);
476 printf("getAbsoluteFileURL('%s','%s') deliver absolute URL: '%s', error '%s'\n",
477 sBaseURL.getStr(), _sRelativeURL.getStr(),sResultURL.getStr(), sError.getStr());
478 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: error number is wrong", _nAssumeError, nError);
480 if (nError == osl::FileBase::E_None)
482 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: ResultURL is not equal to expected URL ", _suAssumeResultStr, suResultURL);
486 void getAbsoluteFileURL::getAbsoluteFileURL_001_1()
488 OUString suAssume = aUserDirectoryURL + "/relative/file1";
489 check_getAbsoluteFileURL(aUserDirectoryURL, "relative/file1"_ostr,osl::FileBase::E_None, suAssume);
492 void getAbsoluteFileURL::getAbsoluteFileURL_001_2()
494 OUString suAssume = aUserDirectoryURL + "/relative/file2";
495 check_getAbsoluteFileURL(aUserDirectoryURL, "relative/./file2"_ostr,osl::FileBase::E_None, suAssume);
498 void getAbsoluteFileURL::getAbsoluteFileURL_001_3()
500 OUString suAssume = aUserDirectoryURL + "/file3";
501 check_getAbsoluteFileURL(aUserDirectoryURL, "relative/../file3"_ostr,osl::FileBase::E_None, suAssume);
504 void getAbsoluteFileURL::getAbsoluteFileURL_001_4()
506 OUString suAssume = aUserDirectoryURL + "/file4";
507 check_getAbsoluteFileURL(aUserDirectoryURL, "././relative/../file4"_ostr,osl::FileBase::E_None, suAssume);
510 void getAbsoluteFileURL::getAbsoluteFileURL_001_5()
512 OUString suAssume;
513 suAssume = aUserDirectoryURL + "/relative/";
514 check_getAbsoluteFileURL(aUserDirectoryURL, "././relative/."_ostr,osl::FileBase::E_None, suAssume);
517 void getAbsoluteFileURL::getAbsoluteFileURL_001_6()
519 OUString suAssume = aUserDirectoryURL + "/.relative";
520 check_getAbsoluteFileURL(aUserDirectoryURL, "./.relative"_ostr,osl::FileBase::E_None, suAssume);
523 void getAbsoluteFileURL::getAbsoluteFileURL_001_7()
525 OUString suAssume;
526 suAssume = aUserDirectoryURL + "/.a/";
527 check_getAbsoluteFileURL(aUserDirectoryURL, "./.a/mydir/.."_ostr,osl::FileBase::E_None, suAssume);
530 void getAbsoluteFileURL::getAbsoluteFileURL_001_8()
532 OUString suAssume = aUserDirectoryURL + "/tmp/ok";
533 check_getAbsoluteFileURL(aUserDirectoryURL, "tmp//ok"_ostr,osl::FileBase::E_None, suAssume);
536 void getAbsoluteFileURL::getAbsoluteFileURL_002()
538 #if 0
539 #if (defined UNX) // Link is not defined in Windows
540 OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
541 aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/link.file";
542 aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/canonical.name";
544 OString strLinkFileName, strSrcFileName;
545 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
546 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
548 createTestFile(aCanURL1);
549 sal_Int32 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
550 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
551 OString sLnkURL = OUStringToOString(aLnkURL1, RTL_TEXTENCODING_ASCII_US);
552 OUString suAssume = aUserDirectoryURL + "/canonical.name";
553 check_getAbsoluteFileURL(aUserDirectoryURL, sLnkURL, osl::FileBase::E_None, suAssume);
554 deleteTestFile(aCanURL1);
555 fd = remove(strLinkFileName.getStr());
556 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
557 #endif
558 #endif
561 // please see line# 930
562 void getAbsoluteFileURL::getAbsoluteFileURL_003()
566 void getAbsoluteFileURL::getAbsoluteFileURL_004()
568 // create two level directories under $Temp/PID/
569 OUString aUStrUpBase = aUserDirectoryURL + "/test1";
570 createTestDirectory(aUStrUpBase);
571 OUString aUStrBase = aUserDirectoryURL + "/test1/dir1";
572 createTestDirectory(aUStrBase);
574 OUString suAssume = aUserDirectoryURL + "/mytestfile";
575 check_getAbsoluteFileURL(aUStrBase, "../../mytestfile"_ostr , osl::FileBase::E_None, suAssume);
576 deleteTestDirectory(aUStrBase);
577 deleteTestDirectory(aUStrUpBase);
580 // testing two methods:
581 // static inline RC getSystemPathFromFileURL(const OUString& ustrFileURL,
582 // OUString& ustrSystemPath)
583 // static RC getFileURLFromSystemPath(const OUString & ustrSystemPath,
584 // OUString & ustrFileURL);
586 class SystemPath_FileURL : public CppUnit::TestFixture
588 public:
589 void getSystemPathFromFileURL_001_1();
590 void getSystemPathFromFileURL_001_2();
591 void getSystemPathFromFileURL_001_21();
592 void getSystemPathFromFileURL_001_22();
593 void getSystemPathFromFileURL_001_3();
594 void getSystemPathFromFileURL_001_31();
595 void getSystemPathFromFileURL_001_4();
596 void getSystemPathFromFileURL_001_41();
597 void getSystemPathFromFileURL_001_5();
598 void getSystemPathFromFileURL_001_51();
599 void getSystemPathFromFileURL_001_52();
600 void getSystemPathFromFileURL_001_53();
601 void getSystemPathFromFileURL_001_6();
602 void getSystemPathFromFileURL_001_61();
603 void getSystemPathFromFileURL_001_7();
604 void getSystemPathFromFileURL_001_71();
605 void getSystemPathFromFileURL_001_8();
606 void getSystemPathFromFileURL_001_81();
607 void getSystemPathFromFileURL_001_9();
608 void getSystemPathFromFileURL_001_91();
609 void getSystemPathFromFileURL_001_92();
610 void getSystemPathFromFileURL_004();
611 void getSystemPathFromFileURL_005();
613 // test case for getFileURLFromSystemPath
614 void getFileURLFromSystemPath_001();
615 void getFileURLFromSystemPath_002();
616 void getFileURLFromSystemPath_003();
617 void getFileURLFromSystemPath_004();
618 void getFileURLFromSystemPath_004_1();
619 void getFileURLFromSystemPath_005();
621 CPPUNIT_TEST_SUITE(SystemPath_FileURL);
622 CPPUNIT_TEST(getSystemPathFromFileURL_001_1);
623 CPPUNIT_TEST(getSystemPathFromFileURL_001_2);
624 CPPUNIT_TEST(getSystemPathFromFileURL_001_21);
625 CPPUNIT_TEST(getSystemPathFromFileURL_001_22);
626 CPPUNIT_TEST(getSystemPathFromFileURL_001_3);
627 CPPUNIT_TEST(getSystemPathFromFileURL_001_31);
628 CPPUNIT_TEST(getSystemPathFromFileURL_001_4);
629 CPPUNIT_TEST(getSystemPathFromFileURL_001_41);
630 CPPUNIT_TEST(getSystemPathFromFileURL_001_5);
631 CPPUNIT_TEST(getSystemPathFromFileURL_001_51);
632 CPPUNIT_TEST(getSystemPathFromFileURL_001_52);
633 CPPUNIT_TEST(getSystemPathFromFileURL_001_53);
634 CPPUNIT_TEST(getSystemPathFromFileURL_001_6);
635 CPPUNIT_TEST(getSystemPathFromFileURL_001_61);
636 CPPUNIT_TEST(getSystemPathFromFileURL_001_7);
637 CPPUNIT_TEST(getSystemPathFromFileURL_001_71);
638 CPPUNIT_TEST(getSystemPathFromFileURL_001_8);
639 CPPUNIT_TEST(getSystemPathFromFileURL_001_81);
640 CPPUNIT_TEST(getSystemPathFromFileURL_001_9);
641 CPPUNIT_TEST(getSystemPathFromFileURL_001_91);
642 CPPUNIT_TEST(getSystemPathFromFileURL_001_92);
643 CPPUNIT_TEST(getSystemPathFromFileURL_004);
644 CPPUNIT_TEST(getSystemPathFromFileURL_005);
645 CPPUNIT_TEST(getFileURLFromSystemPath_001);
646 CPPUNIT_TEST(getFileURLFromSystemPath_002);
647 CPPUNIT_TEST(getFileURLFromSystemPath_003);
648 CPPUNIT_TEST(getFileURLFromSystemPath_004);
649 CPPUNIT_TEST(getFileURLFromSystemPath_004_1);
650 CPPUNIT_TEST(getFileURLFromSystemPath_005);
651 CPPUNIT_TEST_SUITE_END();
653 private:
654 void check_SystemPath_FileURL(
655 OString const& _sSource,
656 osl::FileBase::RC _nAssumeError,
657 OString const& _sAssumeResultStr,
658 bool bDirection = true);
660 void checkWNTBehaviour_getSystemPathFromFileURL(
661 OString const& _sURL,
662 osl::FileBase::RC _nAssumeError,
663 OString const& _sWNTAssumeResultString);
665 void checkUNXBehaviour_getSystemPathFromFileURL(
666 OString const& _sURL,
667 osl::FileBase::RC _nAssumeError,
668 OString const& _sUnixAssumeResultString);
670 void checkWNTBehaviour_getFileURLFromSystemPath(OString const& _sSysPath,
671 osl::FileBase::RC _nAssumeError,
672 OString const& _sWNTAssumeResultString);
674 void checkUNXBehaviour_getFileURLFromSystemPath(
675 OString const& _sSysPath,
676 osl::FileBase::RC _nAssumeError,
677 OString const& _sUnixAssumeResultString);
681 // if bDirection==sal_True, check getSystemPathFromFileURL
682 // if bDirection==sal_False, check getFileURLFromSystemPath
683 void SystemPath_FileURL::check_SystemPath_FileURL(
684 OString const& _sSource,
685 osl::FileBase::RC _nAssumeError,
686 OString const& _sAssumeResultStr,
687 bool bDirection)
689 // PRE: URL as String
690 OUString suSource;
691 OUString suStr;
692 suSource = OStringToOUString(_sSource, RTL_TEXTENCODING_UTF8);
693 osl::FileBase::RC nError;
695 if (bDirection)
696 nError = osl::FileBase::getSystemPathFromFileURL(suSource, suStr);
697 else
698 nError = osl::FileBase::getFileURLFromSystemPath(suSource, suStr);
700 // if the given string is gt length 0,
701 // we check also this string
702 OString sStr = OUStringToOString(suStr, RTL_TEXTENCODING_UTF8);
703 OString sError = errorToString(nError);
705 if (bDirection)
706 printf("getSystemPathFromFileURL('%s') deliver system path: '%s', error '%s'\n",
707 _sSource.getStr(), sStr.getStr(), sError.getStr());
708 else
709 printf("getFileURLFromSystemPath('%s') deliver File URL: '%s', error '%s'\n",
710 _sSource.getStr(), sStr.getStr(), sError.getStr());
712 if (!_sAssumeResultStr.isEmpty())
714 bool bStrAreEqual = _sAssumeResultStr == sStr;
715 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong",
716 _nAssumeError, nError);
717 CPPUNIT_ASSERT_MESSAGE("Assumption is wrong",
718 bStrAreEqual);
720 else
722 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong", _nAssumeError, nError);
726 void SystemPath_FileURL::checkWNTBehaviour_getSystemPathFromFileURL(
727 OString const& _sURL,
728 osl::FileBase::RC _nAssumeError,
729 OString const& _sWNTAssumeResultString)
731 #if defined(_WIN32)
732 check_SystemPath_FileURL(_sURL, _nAssumeError, _sWNTAssumeResultString);
733 #else
734 (void)_sURL;
735 (void)_nAssumeError;
736 (void)_sWNTAssumeResultString;
737 #endif
740 void SystemPath_FileURL::checkUNXBehaviour_getSystemPathFromFileURL(
741 OString const& _sURL,
742 osl::FileBase::RC _nAssumeError,
743 OString const& _sUnixAssumeResultString)
745 #if (defined UNX)
746 check_SystemPath_FileURL(_sURL, _nAssumeError, _sUnixAssumeResultString);
747 #else
748 (void)_sURL;
749 (void)_nAssumeError;
750 (void)_sUnixAssumeResultString;
751 #endif
754 void SystemPath_FileURL::checkWNTBehaviour_getFileURLFromSystemPath(
755 OString const& _sSysPath,
756 osl::FileBase::RC _nAssumeError,
757 OString const& _sWNTAssumeResultString)
759 #if defined(_WIN32)
760 check_SystemPath_FileURL(_sSysPath, _nAssumeError, _sWNTAssumeResultString, false);
761 #else
762 (void)_sSysPath;
763 (void)_nAssumeError;
764 (void)_sWNTAssumeResultString;
765 #endif
768 void SystemPath_FileURL::checkUNXBehaviour_getFileURLFromSystemPath(
769 OString const& _sSysPath,
770 osl::FileBase::RC _nAssumeError,
771 OString const& _sUnixAssumeResultString)
773 #if (defined UNX)
774 check_SystemPath_FileURL(_sSysPath, _nAssumeError, _sUnixAssumeResultString, false);
775 #else
776 (void)_sSysPath;
777 (void)_nAssumeError;
778 (void)_sUnixAssumeResultString;
779 #endif
782 /** Test for getSystemPathFromFileURL()
783 this test is split into 2 different OS tests,
784 the first function checkUNXBehaviour... runs only on Unix based Systems,
785 the second only on windows based systems
786 the first parameter are a file URL where we want to get the system path of,
787 the second parameter is the assumed error of the osl_getSystemPathFromFileURL() function,
788 the third parameter is the assumed result string, the string will only test, if its length is greater than 0
791 void SystemPath_FileURL::getSystemPathFromFileURL_001_1()
793 OString sURL(""_ostr);
794 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
795 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
798 void SystemPath_FileURL::getSystemPathFromFileURL_001_2()
800 OString sURL("/"_ostr);
801 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
802 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "\\"_ostr);
805 void SystemPath_FileURL::getSystemPathFromFileURL_001_21()
807 /* From RFC3986, "2.2. Reserved Characters":
809 "The purpose of reserved characters is to provide a set of delimiting
810 characters that are distinguishable from other data within a URI.
811 URIs that differ in the replacement of a reserved character with its
812 corresponding percent-encoded octet are not equivalent. Percent-
813 encoding a reserved character, or decoding a percent-encoded octet
814 that corresponds to a reserved character, will change how the URI is
815 interpreted by most applications. Thus, characters in the reserved
816 set are protected from normalization and are therefore safe to be
817 used by scheme-specific and producer-specific algorithms for
818 delimiting data subcomponents within a URI."
820 In other words, %2F ("/") is NOT the same as /.
822 OString sURL("%2F"_ostr);
823 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
824 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
827 void SystemPath_FileURL::getSystemPathFromFileURL_001_22()
829 OString sURL("file:///tmp%2Fmydir"_ostr);
830 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
831 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
834 void SystemPath_FileURL::getSystemPathFromFileURL_001_3()
836 OString sURL("a"_ostr);
837 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "a"_ostr);
838 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "a"_ostr);
841 void SystemPath_FileURL::getSystemPathFromFileURL_001_31()
843 OString sURL("tmpname"_ostr);
844 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "tmpname"_ostr);
845 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "tmpname"_ostr);
848 void SystemPath_FileURL::getSystemPathFromFileURL_001_4()
850 OString sURL("file://"_ostr);
851 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, ""_ostr);
852 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
855 void SystemPath_FileURL::getSystemPathFromFileURL_001_41()
857 OString sURL("file://localhost/tmp"_ostr);
858 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, ""_ostr);
859 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
862 void SystemPath_FileURL::getSystemPathFromFileURL_001_5()
864 OString sURL("file:///tmp"_ostr);
865 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp"_ostr);
866 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
869 void SystemPath_FileURL::getSystemPathFromFileURL_001_51()
871 OString sURL("file://c:/tmp"_ostr);
872 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
873 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
876 void SystemPath_FileURL::getSystemPathFromFileURL_001_52()
878 OString sURL("file:///c:/tmp"_ostr);
879 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp"_ostr);
880 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp"_ostr);
883 void SystemPath_FileURL::getSystemPathFromFileURL_001_53()
885 OString sURL("file:///c|/tmp"_ostr);
886 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c|/tmp"_ostr);
887 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp"_ostr);
890 void SystemPath_FileURL::getSystemPathFromFileURL_001_6()
892 OString sURL("file:///tmp/first"_ostr);
893 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/first"_ostr);
894 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
897 void SystemPath_FileURL::getSystemPathFromFileURL_001_61()
899 OString sURL("file:///c:/tmp/first"_ostr);
900 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/first"_ostr);
901 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\first"_ostr);
904 void SystemPath_FileURL::getSystemPathFromFileURL_001_7()
906 OString sURL("file:///tmp/../second"_ostr);
907 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/../second"_ostr);
908 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
911 void SystemPath_FileURL::getSystemPathFromFileURL_001_71()
913 OString sURL("file:///c:/tmp/../second"_ostr);
914 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/../second"_ostr);
915 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\..\\second"_ostr);
918 void SystemPath_FileURL::getSystemPathFromFileURL_001_8()
920 OString sURL("../tmp"_ostr);
921 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "../tmp"_ostr);
922 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "..\\tmp"_ostr);
925 void SystemPath_FileURL::getSystemPathFromFileURL_001_81()
927 #if 0
928 OString sURL("file://~/tmp");
929 char* home_path;
930 home_path = getenv("HOME");
931 OString expResult(home_path ? home_path : "");
932 expResult += "/tmp";
933 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, expResult);
934 #endif
937 void SystemPath_FileURL::getSystemPathFromFileURL_001_9()
939 OString sURL("file:///tmp/first%20second"_ostr);
940 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/first second"_ostr);
941 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
944 void SystemPath_FileURL::getSystemPathFromFileURL_001_91()
946 OString sURL("file:///c:/tmp/first%20second"_ostr);
947 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/first second"_ostr);
948 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\first second"_ostr);
951 void SystemPath_FileURL::getSystemPathFromFileURL_001_92()
953 OString sURL("ca@#;+.,$///78no%01ni..name"_ostr);
954 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
955 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, ""_ostr);
958 // normal legal case
959 void SystemPath_FileURL::getSystemPathFromFileURL_004()
961 OUString aUStr;
962 OUString aUNormalURL(aTmpName6);
963 OUString aUResultURL (aSysPath4);
964 osl::FileBase::RC nError = osl::FileBase::getSystemPathFromFileURL(aUNormalURL, aUStr);
966 bool bOk = compareFileName(aUStr, aUResultURL);
968 OString sError =
969 "test for getSystemPathFromFileURL(' " +
970 OUStringToOString(aUNormalURL, RTL_TEXTENCODING_ASCII_US) +
971 " ') function:use an absolute file URL, " +
972 outputError(OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US),
973 OUStringToOString(aUResultURL, RTL_TEXTENCODING_ASCII_US));
975 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError);
976 CPPUNIT_ASSERT_MESSAGE(sError.getStr(), bOk);
980 // CJK characters case
981 void SystemPath_FileURL::getSystemPathFromFileURL_005()
983 OUString aUStr;
984 createTestDirectory(aTmpName10);
985 OUString aUNormalURL(aTmpName10);
986 OUString aUResultURL (aSysPath5);
988 osl::FileBase::RC nError = osl::FileBase::getSystemPathFromFileURL(aUNormalURL, aUStr);
990 bool bOk = compareFileName(aUStr, aUResultURL);
992 OString sError =
993 "test for getSystemPathFromFileURL(' " +
994 OUStringToOString(aUNormalURL, RTL_TEXTENCODING_ASCII_US) +
995 " ') function:use a CJK coded absolute URL, " +
996 outputError(OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US),
997 OUStringToOString(aUResultURL, RTL_TEXTENCODING_ASCII_US));
998 deleteTestDirectory(aTmpName10);
1000 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError);
1001 CPPUNIT_ASSERT_MESSAGE(sError.getStr(), bOk);
1004 void SystemPath_FileURL::getFileURLFromSystemPath_001()
1006 OString sSysPath("~/tmp"_ostr);
1007 char* home_path;
1008 home_path = getenv("HOME");
1009 OString expResult(home_path ? home_path : "");
1010 expResult = "file://"+ expResult + "/tmp";
1011 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, expResult);
1012 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "~/tmp"_ostr);
1015 void SystemPath_FileURL::getFileURLFromSystemPath_002()
1017 OString sSysPath("c:/tmp"_ostr);
1018 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "c:/tmp"_ostr);
1019 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///c:/tmp"_ostr);
1022 void SystemPath_FileURL::getFileURLFromSystemPath_003()
1024 OString sSysPath("file:///temp"_ostr);
1025 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, ""_ostr);
1026 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, ""_ostr);
1029 void SystemPath_FileURL::getFileURLFromSystemPath_004()
1031 OString sSysPath("//tmp//first start"_ostr);
1032 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///tmp/first%20start"_ostr);
1033 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, ""_ostr);
1036 void SystemPath_FileURL::getFileURLFromSystemPath_004_1()
1038 OString sSysPath("/tmp///first start"_ostr);
1039 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///tmp/first%20start"_ostr);
1040 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, ""_ostr);
1043 void SystemPath_FileURL::getFileURLFromSystemPath_005()
1045 OString sSysPath(""_ostr);
1046 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, ""_ostr);
1047 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, ""_ostr);
1050 // testing the method
1051 // static inline RC searchFileURL( const OUString& ustrFileName,
1052 // const OUString& ustrSearchPath,
1053 // OUString& ustrFileURL)
1055 class searchFileURL : public CppUnit::TestFixture
1057 private:
1058 OUString aUStr;
1060 public:
1061 void searchFileURL_001()
1063 /* search file is passed by system filename */
1064 auto nError1 = osl::FileBase::searchFileURL(aTmpName1, aUserDirectorySys, aUStr);
1065 /* search file is passed by full qualified file URL */
1066 auto nError2 = osl::FileBase::searchFileURL(aCanURL1, aUserDirectorySys, aUStr);
1067 /* search file is passed by relative file path */
1068 auto nError3 = osl::FileBase::searchFileURL(aRelURL4, aUserDirectorySys, aUStr);
1070 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) ",
1071 osl::FileBase::E_NOENT, nError1);
1072 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) ",
1073 osl::FileBase::E_NOENT, nError2);
1074 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) ",
1075 osl::FileBase::E_NOENT, nError3);
1078 void searchFileURL_002()
1080 #ifndef UNX
1081 /* search file is passed by system filename */
1082 OUString strRootSys = INetURLObject(aTempDirectoryURL).GetLastName();
1083 auto nError1 = osl::FileBase::searchFileURL(aTempDirectorySys, strRootSys, aUStr);
1084 bool bOk1 = compareFileName(aUStr, aTempDirectoryURL);
1085 /* search file is passed by full qualified file URL */
1086 auto nError2 = osl::FileBase::searchFileURL(aTempDirectoryURL, strRootSys, aUStr);
1087 bool bOk2 = compareFileName(aUStr, aTempDirectoryURL);
1088 #ifndef _WIN32
1089 /* search file is passed by relative file path */
1090 auto nError3 = osl::FileBase::searchFileURL(aRelURL5, strRootSys, aUStr);
1091 bool bOk3 = compareFileName(aUStr, aTempDirectoryURL);
1092 #endif
1093 /* search file is passed by an exist file */
1094 createTestFile(aCanURL1);
1095 auto nError4 = osl::FileBase::searchFileURL(aCanURL4, aUserDirectorySys, aUStr);
1096 bool bOk4 = compareFileName(aUStr, aCanURL1);
1097 deleteTestFile(aCanURL1);
1099 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
1100 osl::FileBase::E_None, nError1);
1101 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
1102 osl::FileBase::E_None, nError2);
1103 #ifndef _WIN32
1104 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
1105 osl::FileBase::E_None, nError3);
1106 #endif
1107 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1108 osl::FileBase::E_None, nError4);
1109 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
1110 bOk1);
1111 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
1112 bOk2);
1113 #ifndef _WIN32
1114 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
1115 bOk3);
1116 #endif
1117 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1118 bOk4);
1119 #endif
1122 void searchFileURL_003()
1124 OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path");
1125 auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr);
1126 bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1127 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1128 osl::FileBase::E_None, nError1);
1129 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1130 bOk);
1133 void searchFileURL_004()
1135 OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path/../name");
1136 auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr);
1137 bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1138 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1139 osl::FileBase::E_None, nError1);
1140 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1141 bOk);
1144 void searchFileURL_005()
1146 auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aNullURL, aUStr);
1147 bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1148 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is NULL",
1149 osl::FileBase::E_None, nError1);
1150 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is NULL",
1151 bOk);
1154 CPPUNIT_TEST_SUITE(searchFileURL);
1155 CPPUNIT_TEST(searchFileURL_001);
1156 CPPUNIT_TEST(searchFileURL_002);
1157 CPPUNIT_TEST(searchFileURL_003);
1158 CPPUNIT_TEST(searchFileURL_004);
1159 CPPUNIT_TEST(searchFileURL_005);
1160 CPPUNIT_TEST_SUITE_END();
1163 // testing the method
1164 // static inline RC getTempDirURL(OUString& ustrTempDirURL)
1166 class getTempDirURL : public CppUnit::TestFixture
1168 private:
1169 OUString aUStr;
1171 public:
1172 void setUp() override
1174 osl::FileBase::RC nError = osl::FileBase::getTempDirURL(aUStr);
1175 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getTempDirURL function: execution",
1176 osl::FileBase::E_None, nError);
1179 void getTempDirURL_002()
1181 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1182 checkDirectory(aUStr, oslCheckMode::OpenAccess));
1183 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1184 checkDirectory(aUStr, oslCheckMode::ReadAccess));
1185 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1186 checkDirectory(aUStr, oslCheckMode::WriteAccess));
1189 CPPUNIT_TEST_SUITE(getTempDirURL);
1190 CPPUNIT_TEST(getTempDirURL_002);
1191 CPPUNIT_TEST_SUITE_END();
1194 // testing the method
1195 // static inline RC createTempFile(OUString* pustrDirectoryURL,
1196 // oslFileHandle* pHandle,
1197 // OUString* pustrTempFileURL)
1199 class createTempFile : public CppUnit::TestFixture
1201 private:
1202 std::unique_ptr<oslFileHandle> pHandle;
1203 std::unique_ptr<OUString> pUStr_DirURL;
1204 std::unique_ptr<OUString> pUStr_FileURL;
1206 public:
1207 void setUp() override
1209 pHandle.reset(new oslFileHandle());
1210 pUStr_DirURL.reset(new OUString(aUserDirectoryURL));
1211 pUStr_FileURL.reset(new OUString());
1214 void tearDown() override
1216 pUStr_DirURL.reset();
1217 pUStr_FileURL.reset();
1218 pHandle.reset();
1221 void createTempFile_001()
1223 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
1224 File testFile(*pUStr_FileURL);
1225 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1227 if (nError2 == osl::FileBase::E_EXIST)
1229 osl_closeFile(*pHandle);
1230 deleteTestFile(*pUStr_FileURL);
1233 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1234 osl::FileBase::E_None, nError1);
1235 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: create temp file and test the existence",
1236 (pHandle != nullptr));
1237 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1238 osl::FileBase::E_EXIST, nError2);
1241 void createTempFile_002()
1243 bool bOK = false;
1244 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
1245 File testFile(*pUStr_FileURL);
1246 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1248 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1249 osl::FileBase::E_None, nError1);
1250 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1251 (pHandle != nullptr));
1252 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1253 osl::FileBase::E_EXIST, nError2);
1255 // check file if have the write permission
1256 if (nError2 == osl::FileBase::E_EXIST)
1258 bOK = ifFileCanWrite(*pUStr_FileURL);
1259 osl_closeFile(*pHandle);
1260 deleteTestFile(*pUStr_FileURL);
1263 CPPUNIT_ASSERT_MESSAGE("test for open and write access rights, in (W32), it did not have write access right, but it should be writable.",
1264 bOK);
1267 void createTempFile_003()
1269 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), nullptr);
1270 // the temp file will be removed when return from createTempFile
1271 bool bOK = (pHandle != nullptr && nError1 == osl::FileBase::E_None);
1272 if (bOK)
1273 osl_closeFile(*pHandle);
1275 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1276 osl::FileBase::E_None, nError1);
1277 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1278 bOK);
1281 void createTempFile_004()
1283 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), nullptr, pUStr_FileURL.get());
1284 bool bOK = (pUStr_FileURL != nullptr);
1285 CPPUNIT_ASSERT(bOK);
1286 File testFile(*pUStr_FileURL);
1287 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1288 deleteTestFile(*pUStr_FileURL);
1289 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1290 osl::FileBase::E_None, nError1);
1291 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1292 osl::FileBase::E_EXIST, nError2);
1293 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1294 bOK);
1298 CPPUNIT_TEST_SUITE(createTempFile);
1299 CPPUNIT_TEST(createTempFile_001);
1300 CPPUNIT_TEST(createTempFile_002);
1301 CPPUNIT_TEST(createTempFile_003);
1302 CPPUNIT_TEST(createTempFile_004);
1303 CPPUNIT_TEST_SUITE_END();
1306 // FIXME: remove the _disabled to enable:
1307 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getAbsoluteFileURL, "osl_osl::FileBase");
1308 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::SystemPath_FileURL, "osl_osl::FileBase");
1309 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::searchFileURL, "osl_osl::FileBase");
1310 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getTempDirURL, "osl_osl::FileBase");
1311 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::createTempFile, "osl_osl::FileBase");
1313 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_osl::FileBase");
1316 namespace osl_FileStatus
1318 // testing the method
1319 // FileStatus(sal_uInt32 nMask): _nMask(nMask)
1320 class ctors : public CppUnit::TestFixture
1322 private:
1323 OUString aUStr;
1324 DirectoryItem rItem;
1326 public:
1327 void setUp() override
1329 // create a tempfile in $TEMP/tmpdir/tmpname.
1330 createTestDirectory(aTmpName3);
1331 createTestFile(aTmpName4);
1333 Directory aDir(aTmpName3);
1334 auto nError1 = aDir.open();
1335 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1336 nError1 = aDir.getNextItem(rItem);
1337 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1338 aDir.close();
1341 void tearDown() override
1343 // remove the tempfile in $TEMP/tmpdir/tmpname.
1344 deleteTestFile(aTmpName4);
1345 deleteTestDirectory(aTmpName3);
1348 void ctors_001()
1350 FileStatus rFileStatus(osl_FileStatus_Mask_All);
1351 auto nError1 = rItem.getFileStatus(rFileStatus);
1352 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1353 aUStr = rFileStatus.getFileName();
1355 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask all and see the file name",
1356 compareFileName(aUStr, aTmpName2));
1359 void ctors_002()
1361 FileStatus rFileStatus(0);
1362 auto nError1 = rItem.getFileStatus(rFileStatus);
1363 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1364 aUStr = rFileStatus.getFileName();
1366 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask is empty",
1367 compareFileName(aUStr, aNullURL));
1370 CPPUNIT_TEST_SUITE(ctors);
1371 CPPUNIT_TEST(ctors_001);
1372 CPPUNIT_TEST(ctors_002);
1373 CPPUNIT_TEST_SUITE_END();
1376 // testing the method
1377 // inline sal_Bool isValid(sal_uInt32 nMask) const
1379 class isValid : public CppUnit::TestFixture
1381 private:
1382 std::unique_ptr<Directory> pDir;
1383 DirectoryItem rItem_file, rItem_link;
1385 public:
1386 void setUp() override
1388 // create a tempfile in $TEMP/tmpdir/tmpname.
1389 createTestDirectory(aTmpName3);
1390 createTestFile(aTmpName4);
1392 pDir.reset(new Directory(aTmpName3));
1393 osl::FileBase::RC nError1 = pDir->open();
1394 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1395 nError1 = pDir->getNextItem(rItem_file, 1);
1396 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1399 void tearDown() override
1401 osl::FileBase::RC nError1 = pDir->close();
1402 pDir.reset();
1403 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
1405 // remove the tempfile in $TEMP/tmpdir/tmpname.
1406 deleteTestFile(aTmpName4);
1407 deleteTestDirectory(aTmpName3);
1410 void isValid_001()
1412 sal_uInt32 mask = 0;
1413 FileStatus rFileStatus(mask);
1414 osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus);
1415 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1416 bool bOk = rFileStatus.isValid(mask);
1418 CPPUNIT_ASSERT_MESSAGE("test for isValid function: no fields specified", bOk);
1421 void check_FileStatus(FileStatus const& _aStatus)
1423 OString sStat;
1424 if (_aStatus.isValid(osl_FileStatus_Mask_Type))
1425 sStat += "type ";
1426 if (_aStatus.isValid(osl_FileStatus_Mask_Attributes))
1427 sStat += "attributes ";
1428 if (_aStatus.isValid(osl_FileStatus_Mask_CreationTime))
1429 sStat += "ctime ";
1430 if (_aStatus.isValid(osl_FileStatus_Mask_AccessTime))
1431 sStat += "atime ";
1432 if (_aStatus.isValid(osl_FileStatus_Mask_ModifyTime))
1433 sStat += "mtime ";
1434 if (_aStatus.isValid(osl_FileStatus_Mask_FileSize))
1435 sStat += "filesize ";
1436 if (_aStatus.isValid(osl_FileStatus_Mask_FileName))
1437 sStat += "filename ";
1438 if (_aStatus.isValid(osl_FileStatus_Mask_FileURL))
1439 sStat += "fileurl ";
1440 printf("mask: %s\n", sStat.getStr());
1443 void isValid_002()
1445 createTestFile(aTmpName6);
1446 sal_uInt32 mask_file = osl_FileStatus_Mask_Type |
1447 osl_FileStatus_Mask_Attributes |
1448 osl_FileStatus_Mask_CreationTime |
1449 osl_FileStatus_Mask_AccessTime |
1450 osl_FileStatus_Mask_ModifyTime |
1451 osl_FileStatus_Mask_FileSize |
1452 osl_FileStatus_Mask_FileName |
1453 osl_FileStatus_Mask_FileURL;
1455 FileStatus rFileStatus(mask_file);
1456 DirectoryItem::get(aTmpName6, rItem_file);
1457 osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus);
1459 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
1461 check_FileStatus(rFileStatus);
1462 deleteTestFile(aTmpName6);
1466 /** Check if is a valid linked file.
1468 Link is not defined in Windows, and on Linux, we can not get the directory item of the linked file.
1469 We have to defer to filesystems, normal filesystems support links (EXT2, ...), castrated filesystems
1470 don't have links (FAT, FAT32) and Windows NT NTFS support links, but the Windows API doesn't :-(
1472 void isValid_003()
1474 #if 0
1475 #if defined (UNX)
1476 sal_Int32 fd;
1478 OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
1479 aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/tmpdir/link.file";
1480 aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/tmpdir/tmpname";
1482 OString strLinkFileName;
1483 OString strSrcFileName;
1484 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
1485 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
1487 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
1488 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
1489 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
1491 // testDirectory is "/tmp/PID/tmpdir/"
1492 Directory testDirectory(aTmpName3);
1493 testDirectory.open();
1494 OUString aFileName ("link.file");
1495 bool bOk = false;
1496 while (true)
1498 osl::FileBase::RC nError1 = testDirectory.getNextItem(rItem_link, 4);
1500 if (nError1 == osl::FileBase::E_None)
1502 sal_uInt32 mask_link = osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_LinkTargetURL;
1503 FileStatus rFileStatus(mask_link);
1504 rItem_link.getFileStatus(rFileStatus);
1506 if (compareFileName(rFileStatus.getFileName(), aFileName))
1508 if (rFileStatus.isValid(osl_FileStatus_Mask_LinkTargetURL))
1510 bOk = true;
1511 break;
1515 else
1517 break;
1521 fd = remove(strLinkFileName.getStr());
1522 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
1524 CPPUNIT_ASSERT_MESSAGE("test for isValid function: link file, check for LinkTargetURL", bOk);
1525 #endif
1526 #endif
1529 void isValid_004()
1531 sal_uInt32 mask_file_all = osl_FileStatus_Mask_All;
1532 FileStatus rFileStatus_all(mask_file_all);
1533 osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus_all);
1534 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1536 check_FileStatus(rFileStatus_all);
1538 sal_uInt32 mask_file_val = osl_FileStatus_Mask_Validate;
1539 FileStatus rFileStatus_val(mask_file_val);
1540 nError1 = rItem_file.getFileStatus(rFileStatus_val);
1541 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1543 check_FileStatus(rFileStatus_val);
1546 CPPUNIT_TEST_SUITE(isValid);
1547 CPPUNIT_TEST(isValid_001);
1548 CPPUNIT_TEST(isValid_002);
1549 CPPUNIT_TEST(isValid_003);
1550 CPPUNIT_TEST(isValid_004);
1551 CPPUNIT_TEST_SUITE_END();
1554 // testing the method
1555 // inline Type getFileType() const
1557 class getFileType : public CppUnit::TestFixture
1559 private:
1560 DirectoryItem m_aItem_1, m_aItem_2, m_aVolumeItem, m_aFifoItem;
1561 DirectoryItem m_aLinkItem, m_aSocketItem, m_aSpecialItem;
1563 public:
1564 void setUp() override
1566 // create a tempfile: $TEMP/tmpdir/tmpname.
1567 // a tempdirectory: $TEMP/tmpdir/tmpdir.
1568 // use $ROOT/staroffice as volume ---> use dev/fd as volume.
1569 // and get their directory item.
1570 createTestDirectory(aTmpName3);
1571 createTestFile(aTmpName3, aTmpName2);
1572 createTestDirectory(aTmpName3, aTmpName1);
1574 std::unique_ptr<Directory> xDir(new Directory(aTmpName3));
1575 auto nError1 = xDir->open();
1576 CPPUNIT_ASSERT_EQUAL_MESSAGE("open aTmpName3 failed!", osl::FileBase::E_None, nError1);
1577 // getNextItem can not assure which item retrieved
1578 nError1 = xDir->getNextItem(m_aItem_1, 1);
1579 CPPUNIT_ASSERT_EQUAL_MESSAGE("get first item failed!", osl::FileBase::E_None, nError1);
1581 nError1 = xDir->getNextItem(m_aItem_2);
1582 CPPUNIT_ASSERT_EQUAL_MESSAGE("get second item failed!", osl::FileBase::E_None, nError1);
1583 xDir->close();
1584 // FIXME mindy: failed on my RH9, so removed temporarily
1585 // nError1 = DirectoryItem::get(aVolURL2, m_aVolumeItem);
1586 // CPPUNIT_ASSERT_MESSAGE("get volume item failed!", osl::FileBase::E_None == nError1);
1589 void tearDown() override
1591 // remove all in $TEMP/tmpdir.
1592 deleteTestDirectory(aTmpName3, aTmpName1);
1593 deleteTestFile(aTmpName3, aTmpName2);
1594 deleteTestDirectory(aTmpName3);
1597 void getFileType_001()
1599 FileStatus rFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName);
1600 auto nError1 = m_aItem_1.getFileStatus(rFileStatus);
1601 CPPUNIT_ASSERT_EQUAL_MESSAGE("getFileStatus failed", osl::FileBase::E_None, nError1);
1603 check_FileType(rFileStatus);
1606 void check_FileType(osl::FileStatus const& _rFileStatus)
1608 if (_rFileStatus.isValid(osl_FileStatus_Mask_FileName))
1610 OUString suFilename = _rFileStatus.getFileName();
1612 if (_rFileStatus.isValid(osl_FileStatus_Mask_Type))
1614 osl::FileStatus::Type eType = _rFileStatus.getFileType();
1615 bool bOK = false;
1617 if (compareFileName(suFilename, aTmpName2))
1618 bOK = (eType == osl::FileStatus::Regular);
1620 if (compareFileName(suFilename, aTmpName1))
1621 bOK = (eType == FileStatus::Directory);
1623 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: ", bOK);
1626 // LLA: it's not a bug, if a FileStatus not exist, so no else
1629 void getFileType_002()
1631 FileStatus rFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName);
1632 auto nError1 = m_aItem_2.getFileStatus(rFileStatus);
1634 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1635 check_FileType(rFileStatus);
1638 void getFileType_003()
1642 void getFileType_007()
1644 #if defined(__sun) // Special file is different in Windows
1645 auto nError1 = DirectoryItem::get(aTypeURL2, m_aSpecialItem);
1646 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1648 // check for File type
1649 FileStatus rFileStatus(osl_FileStatus_Mask_Type);
1650 nError1 = m_aSpecialItem.getFileStatus(rFileStatus);
1651 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1653 if (rFileStatus.isValid(osl_FileStatus_Mask_Type))
1655 osl::FileStatus::Type eType = rFileStatus.getFileType();
1657 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: Special, Solaris version ",
1658 (eType == FileStatus::Special));
1660 #endif
1663 CPPUNIT_TEST_SUITE(getFileType);
1664 CPPUNIT_TEST(getFileType_001);
1665 CPPUNIT_TEST(getFileType_002);
1666 CPPUNIT_TEST(getFileType_003);
1667 CPPUNIT_TEST(getFileType_007);
1668 CPPUNIT_TEST_SUITE_END();
1671 // testing the method
1672 // inline sal_uInt64 getAttributes() const
1674 class getAttributes : public CppUnit::TestFixture
1676 private:
1677 OUString aTypeURL, aTypeURL_Hid;
1678 DirectoryItem rItem, rItem_hidden;
1680 public:
1681 void setUp() override
1683 aTypeURL = aUserDirectoryURL.copy(0);
1684 concatURL(aTypeURL, aTmpName2);
1685 createTestFile(aTypeURL);
1686 osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1687 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1689 aTypeURL_Hid = aUserDirectoryURL.copy(0);
1690 concatURL(aTypeURL_Hid, aHidURL1);
1691 createTestFile(aTypeURL_Hid);
1692 #ifdef _WIN32
1693 hideFile(aTypeURL_Hid);
1694 #endif
1695 nError = DirectoryItem::get(aTypeURL_Hid, rItem_hidden);
1696 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1699 void tearDown() override
1701 deleteTestFile(aTypeURL);
1702 deleteTestFile(aTypeURL_Hid);
1705 #if (defined UNX)
1706 // windows only has 3 file attributes: normal, readonly and hidden
1707 void getAttributes_001()
1709 changeFileMode(aTypeURL, S_IRUSR | S_IRGRP | S_IROTH);
1711 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1712 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1713 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1715 if (geteuid() == 0) // as root, access(W_OK) may be true despite mode
1717 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: (not ReadOnly,) GrpRead, OwnRead, OthRead(UNX version) ",
1718 static_cast<sal_uInt64>(osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead),
1719 rFileStatus.getAttributes());
1721 else
1723 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(UNX version) ",
1724 static_cast<sal_uInt64>(osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead),
1725 rFileStatus.getAttributes());
1728 #else // Windows version
1729 void getAttributes_001()
1731 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(Windows version)",
1732 1, 1);
1734 #endif
1736 void getAttributes_002()
1738 #if (defined UNX)
1739 changeFileMode(aTypeURL, S_IXUSR | S_IXGRP | S_IXOTH);
1741 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1742 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1743 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1745 if (geteuid() == 0) // as root, access(W_OK) may be true despite mode
1747 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Executable, GrpExe, OwnExe, OthExe, the result is (not Readonly,) Executable, GrpExe, OwnExe, OthExe, it partly not pass(Solaris version)",
1748 static_cast<sal_uInt64>(osl_File_Attribute_Executable | osl_File_Attribute_GrpExe | osl_File_Attribute_OwnExe | osl_File_Attribute_OthExe),
1749 rFileStatus.getAttributes());
1751 else
1753 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Executable, GrpExe, OwnExe, OthExe, the result is Readonly, Executable, GrpExe, OwnExe, OthExe, it partly not pass(Solaris version)",
1754 static_cast<sal_uInt64>(osl_File_Attribute_ReadOnly | osl_File_Attribute_Executable | osl_File_Attribute_GrpExe | osl_File_Attribute_OwnExe | osl_File_Attribute_OthExe),
1755 rFileStatus.getAttributes());
1757 #endif
1760 #if (defined UNX)
1761 void getAttributes_003()
1763 changeFileMode(aTypeURL, S_IWUSR | S_IWGRP | S_IWOTH);
1765 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1766 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1767 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1769 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Solaris version)",
1770 static_cast<sal_uInt64>(osl_File_Attribute_GrpWrite | osl_File_Attribute_OwnWrite | osl_File_Attribute_OthWrite),
1771 rFileStatus.getAttributes());
1773 #else // Windows version
1774 void getAttributes_003()
1776 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Windows version)",
1777 1, 1);
1779 #endif
1781 void getAttributes_004()
1783 sal_Int32 test_Attributes = osl_File_Attribute_Hidden;
1784 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1785 osl::FileBase::RC nError = rItem_hidden.getFileStatus(rFileStatus);
1786 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1787 test_Attributes &= rFileStatus.getAttributes();
1789 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Hidden files",
1790 static_cast<sal_Int32>(osl_File_Attribute_Hidden), test_Attributes);
1793 CPPUNIT_TEST_SUITE(getAttributes);
1794 CPPUNIT_TEST(getAttributes_001);
1795 CPPUNIT_TEST(getAttributes_002);
1796 CPPUNIT_TEST(getAttributes_003);
1797 CPPUNIT_TEST(getAttributes_004);
1798 CPPUNIT_TEST_SUITE_END();
1801 // testing the method
1802 // inline TimeValue getAccessTime() const
1804 class getAccessTime : public CppUnit::TestFixture
1806 private:
1807 OUString aTypeURL;
1808 DirectoryItem rItem;
1810 public:
1811 void setUp() override
1813 aTypeURL = aUserDirectoryURL.copy(0);
1814 concatURL(aTypeURL, aTmpName2);
1815 createTestFile(aTypeURL);
1816 osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1817 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1821 void tearDown() override
1823 deleteTestFile(aTypeURL);
1826 void getAccessTime_001()
1828 TimeValue *pTV_current = nullptr;
1829 CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1830 TimeValue *pTV_access = nullptr;
1831 CPPUNIT_ASSERT((pTV_access = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1833 FileStatus rFileStatus(osl_FileStatus_Mask_AccessTime);
1834 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1835 bool bOk = osl_getSystemTime(pTV_current);
1836 CPPUNIT_ASSERT(bOk);
1837 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1839 *pTV_access = rFileStatus.getAccessTime();
1841 bool bOK = t_compareTime(pTV_access, pTV_current, delta);
1842 free(pTV_current);
1843 free(pTV_access);
1845 CPPUNIT_ASSERT_MESSAGE("test for getAccessTime function: This test turns out that UNX precision is no more than 1 sec, don't know how to test this function, in Windows test, it lost hour min sec, only have date time. ",
1846 bOK);
1849 CPPUNIT_TEST_SUITE(getAccessTime);
1850 CPPUNIT_TEST(getAccessTime_001);
1851 CPPUNIT_TEST_SUITE_END();
1854 // testing the method
1855 // inline TimeValue getModifyTime() const
1857 class getModifyTime : public CppUnit::TestFixture
1859 private:
1860 OUString aTypeURL;
1861 DirectoryItem rItem;
1863 public:
1864 void getModifyTime_001()
1866 TimeValue *pTV_current = nullptr;
1867 CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1869 // create file
1870 aTypeURL = aUserDirectoryURL.copy(0);
1871 concatURL(aTypeURL, aTmpName2);
1872 createTestFile(aTypeURL);
1874 // get current time
1875 bool bOk = osl_getSystemTime(pTV_current);
1876 CPPUNIT_ASSERT(bOk);
1878 // get instance item and filestatus
1879 osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1880 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1881 FileStatus rFileStatus(osl_FileStatus_Mask_ModifyTime);
1882 nError = rItem.getFileStatus(rFileStatus);
1883 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1885 // get modify time
1886 TimeValue* pTV_modify = nullptr;
1887 CPPUNIT_ASSERT((pTV_modify = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1888 *pTV_modify = rFileStatus.getModifyTime();
1890 bool bOK = t_compareTime(pTV_modify, pTV_current, delta);
1891 // delete file
1892 deleteTestFile(aTypeURL);
1893 free(pTV_current);
1894 free(pTV_modify);
1896 CPPUNIT_ASSERT_MESSAGE("test for getModifyTime function: This test turns out that UNX precision is no more than 1 sec, don't know how to improve this function. ",
1897 bOK);
1900 CPPUNIT_TEST_SUITE(getModifyTime);
1901 CPPUNIT_TEST(getModifyTime_001);
1902 CPPUNIT_TEST_SUITE_END();
1905 // testing the method
1906 // inline sal_uInt64 getFileSize() const
1908 class getFileSize : public CppUnit::TestFixture
1910 private:
1911 OUString aTypeURL;
1912 DirectoryItem rItem;
1914 public:
1915 void setUp() override
1917 aTypeURL = aUserDirectoryURL.copy(0);
1918 concatURL(aTypeURL, aTmpName2);
1919 createTestFile(aTypeURL);
1920 osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1921 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1924 void tearDown() override
1926 deleteTestFile(aTypeURL);
1929 void getFileSize_001()
1931 FileStatus rFileStatus(osl_FileStatus_Mask_FileSize);
1932 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1933 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1935 sal_uInt64 uFileSize = rFileStatus.getFileSize();
1937 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: empty file ",
1938 static_cast<sal_uInt64>(0), uFileSize);
1941 void getFileSize_002()
1943 File testfile(aTypeURL);
1944 osl::FileBase::RC nError = testfile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Read);
1945 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1946 nError = testfile.setSize(TEST_FILE_SIZE);
1947 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1949 nError = DirectoryItem::get(aTypeURL, rItem);
1950 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1951 FileStatus rFileStatus(osl_FileStatus_Mask_FileSize);
1952 nError = rItem.getFileStatus(rFileStatus);
1953 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1954 sal_uInt64 uFileSize = rFileStatus.getFileSize();
1956 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: file with size of TEST_FILE_SIZE, did not pass in (W32). ",
1957 static_cast<sal_uInt64>(TEST_FILE_SIZE), uFileSize);
1960 CPPUNIT_TEST_SUITE(getFileSize);
1961 CPPUNIT_TEST(getFileSize_001);
1962 CPPUNIT_TEST(getFileSize_002);
1963 CPPUNIT_TEST_SUITE_END();
1966 // testing the method
1967 // inline OUString getFileName() const
1969 class getFileName : public CppUnit::TestFixture
1971 private:
1972 OUString aTypeURL;
1973 DirectoryItem rItem;
1975 public:
1976 void setUp() override
1978 aTypeURL = aUserDirectoryURL.copy(0);
1979 concatURL(aTypeURL, aTmpName2);
1980 createTestFile(aTypeURL);
1981 osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1982 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1985 void tearDown() override
1987 deleteTestFile(aTypeURL);
1991 void getFileName_001()
1993 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
1994 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1995 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1997 OUString aFileName = rFileStatus.getFileName();
1999 CPPUNIT_ASSERT_MESSAGE("test for getFileName function: name compare with specify",
2000 compareFileName(aFileName, aTmpName2));
2003 CPPUNIT_TEST_SUITE(getFileName);
2004 CPPUNIT_TEST(getFileName_001);
2005 CPPUNIT_TEST_SUITE_END();
2008 // testing the method
2009 // inline OUString getFileURL() const
2011 class getFileURL : public CppUnit::TestFixture
2013 DirectoryItem rItem;
2015 public:
2016 void setUp() override
2018 createTestFile(aTmpName6);
2019 osl::FileBase::RC nError = DirectoryItem::get(aTmpName6, rItem);
2020 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
2023 void tearDown() override
2025 deleteTestFile(aTmpName6);
2029 void getFileURL_001()
2031 FileStatus rFileStatus(osl_FileStatus_Mask_FileURL);
2032 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
2033 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
2035 OUString aFileURL = rFileStatus.getFileURL();
2037 CPPUNIT_ASSERT_MESSAGE("test for getFileURL function: ",
2038 compareFileName(aFileURL, aTmpName6));
2041 CPPUNIT_TEST_SUITE(getFileURL);
2042 CPPUNIT_TEST(getFileURL_001);
2043 CPPUNIT_TEST_SUITE_END();
2046 // testing the method
2047 // inline OUString getLinkTargetURL() const
2049 class getLinkTargetURL : public CppUnit::TestFixture
2051 private:
2052 OUString aTypeURL;
2053 DirectoryItem rItem;
2055 public:
2056 void setUp() override
2058 aTypeURL = aUserDirectoryURL.copy(0);
2059 concatURL(aTypeURL, aTmpName2);
2060 createTestFile(aTypeURL);
2063 void tearDown() override
2065 deleteTestFile(aTypeURL);
2068 void getLinkTargetURL_001()
2070 #if 0
2071 #if (defined UNX) // Link file is not defined in Windows
2072 // create a link file;
2073 OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
2074 aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/link.file";
2075 aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/tmpname";
2077 OString strLinkFileName, strSrcFileName;
2078 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
2079 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
2081 sal_Int32 fd;
2082 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
2083 CPPUNIT_ASSERT_EQUAL_MESSAGE("in creating link file", static_cast<sal_Int32>(0), fd);
2085 // get linkTarget URL
2086 auto nError = DirectoryItem::get(aLnkURL1, rItem);
2087 CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file item", osl::FileBase::E_None, nError);
2089 FileStatus rFileStatus(osl_FileStatus_Mask_LinkTargetURL);
2090 nError = rItem.getFileStatus(rFileStatus);
2091 CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file status", osl::FileBase::E_None, nError);
2092 OUString aFileURL = rFileStatus.getLinkTargetURL();
2094 // remove link file
2095 fd = remove(strLinkFileName.getStr());
2096 CPPUNIT_ASSERT_EQUAL_MESSAGE("in deleting link file", static_cast<sal_Int32>(0), fd);
2098 CPPUNIT_ASSERT_MESSAGE("test for getLinkTargetURL function: Solaris version, create a file, and a link file link to it, get its LinkTargetURL and compare",
2099 compareFileName(aFileURL, aTypeURL));
2100 #endif
2101 #endif
2104 CPPUNIT_TEST_SUITE(getLinkTargetURL);
2105 CPPUNIT_TEST(getLinkTargetURL_001);
2106 CPPUNIT_TEST_SUITE_END();
2109 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::ctors, "osl_FileStatus");
2110 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::isValid, "osl_FileStatus");
2111 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileType, "osl_FileStatus");
2112 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAttributes, "osl_FileStatus");
2113 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAccessTime, "osl_FileStatus");
2114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getModifyTime, "osl_FileStatus");
2115 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileSize, "osl_FileStatus");
2116 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileName, "osl_FileStatus");
2117 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileURL, "osl_FileStatus");
2118 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getLinkTargetURL, "osl_FileStatus");
2120 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_FileStatus");
2123 namespace osl_File
2126 // testing the method
2127 // File(const OUString& ustrFileURL)
2129 class ctors : public CppUnit::TestFixture
2131 public:
2132 void setUp() override
2134 // create a tempfile in $TEMP/tmpdir/tmpname.
2135 createTestDirectory(aTmpName3);
2136 createTestFile(aTmpName4);
2139 void tearDown() override
2141 // remove the tempfile in $TEMP/tmpdir/tmpname.
2142 deleteTestFile(aTmpName4);
2143 deleteTestDirectory(aTmpName3);
2146 void ctors_001()
2148 File testFile(aTmpName4);
2150 osl::FileBase::RC nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2151 osl::FileBase::RC nError2 = testFile.close();
2152 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2153 osl::FileBase::E_None, nError1);
2154 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2155 osl::FileBase::E_None, nError2);
2158 void ctors_002()
2160 File testFile(aTmpName5);
2161 char buffer[30] = "Test for File constructor";
2162 sal_uInt64 nCount;
2164 osl::FileBase::RC nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2165 osl::FileBase::RC nError2 = testFile.write(buffer, 30, nCount);
2166 testFile.close();
2168 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2169 osl::FileBase::E_None, nError1);
2170 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2171 osl::FileBase::E_None, nError2);
2174 CPPUNIT_TEST_SUITE(ctors);
2175 CPPUNIT_TEST(ctors_001);
2176 CPPUNIT_TEST(ctors_002);
2177 CPPUNIT_TEST_SUITE_END();
2180 // testing the method
2181 // inline RC open(sal_uInt32 uFlags)
2183 class open : public CppUnit::TestFixture
2185 public:
2186 void setUp() override
2188 // create a tempfile in $TEMP/tmpdir/tmpname.
2189 createTestDirectory(aTmpName3);
2190 createTestFile(aTmpName4);
2193 void tearDown() override
2195 // remove the tempfile in $TEMP/tmpdir/tmpname.
2196 deleteTestFile(aTmpName4);
2197 deleteTestDirectory(aTmpName3);
2201 void open_001()
2203 File testFile(aTmpName4);
2205 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2206 auto nError2 = testFile.close();
2207 CPPUNIT_ASSERT_EQUAL_MESSAGE("close error", osl::FileBase::E_None, nError2);
2209 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a regular file",
2210 osl::FileBase::E_None, nError1);
2213 void open_002()
2215 File testFile(aTmpName3);
2217 auto nError1 = testFile.open(osl_File_OpenFlag_Read);
2219 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory",
2220 (File::E_INVAL == nError1) || (File::E_ACCES == nError1));
2223 void open_003()
2225 File testFile(aCanURL1);
2227 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2229 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a non-exist file",
2230 File::E_NOENT, nError1);
2233 void open_005()
2235 File testFile(aTmpName4);
2237 auto nError1 = testFile.open(osl_File_OpenFlag_Create);
2239 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: create an exist file",
2240 File::E_EXIST, nError1);
2243 void open_006()
2245 File testFile(aCanURL1);
2246 char buffer_write[30] = "Test for File open";
2247 char buffer_read[30];
2248 sal_uInt64 nCount_write, nCount_read;
2250 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
2251 auto nError2 = testFile.write(buffer_write, 30, nCount_write);
2252 osl::FileBase::RC nError4 = testFile.setPos(osl_Pos_Absolut, 0);
2253 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError4);
2254 auto nError3 = testFile.read(buffer_read, 10, nCount_read);
2256 osl::FileBase::RC nError5 = testFile.close();
2257 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError5);
2258 osl::FileBase::RC nError6 = osl::File::remove(aCanURL1);
2259 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError6);
2261 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2262 osl::FileBase::E_None, nError1);
2263 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2264 osl::FileBase::E_None, nError2);
2265 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2266 osl::FileBase::E_None, nError3);
2267 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2268 sal_uInt64(30), nCount_write);
2269 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2270 sal_uInt64(10), nCount_read);
2273 CPPUNIT_TEST_SUITE(open);
2274 CPPUNIT_TEST(open_001);
2275 CPPUNIT_TEST(open_002);
2276 CPPUNIT_TEST(open_003);
2277 CPPUNIT_TEST(open_005);
2278 CPPUNIT_TEST(open_006);
2279 CPPUNIT_TEST_SUITE_END();
2282 // testing the method
2283 // inline RC close()
2285 class close : public CppUnit::TestFixture
2287 public:
2288 void setUp() override
2290 // create a tempfile in $TEMP/tmpdir/tmpname.
2291 createTestDirectory(aTmpName3);
2292 createTestFile(aTmpName4);
2295 void tearDown() override
2297 // remove the tempfile in $TEMP/tmpdir/tmpname.
2298 deleteTestFile(aTmpName4);
2299 deleteTestDirectory(aTmpName3);
2303 void close_001()
2305 File testFile(aTmpName4);
2307 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2308 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2310 auto nError2 = testFile.close();
2312 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: close a regular file",
2313 osl::FileBase::E_None, nError2);
2316 void close_002()
2318 File testFile(aTmpName4);
2320 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2321 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2323 auto nError2 = testFile.close();
2325 auto nError3 = testFile.setPos(osl_Pos_Absolut, 0);
2327 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: manipulate a file after it has been closed",
2328 osl::FileBase::E_None, nError2);
2329 CPPUNIT_ASSERT_MESSAGE("test for close function: manipulate a file after it has been closed",
2330 (osl::FileBase::E_None != nError3));
2333 CPPUNIT_TEST_SUITE(close);
2334 CPPUNIT_TEST(close_001);
2335 CPPUNIT_TEST(close_002);
2336 CPPUNIT_TEST_SUITE_END();
2339 // testing the method
2340 // inline RC setPos(sal_uInt32 uHow, sal_Int64 uPos)
2342 class setPos : public CppUnit::TestFixture
2344 public:
2345 void setUp() override
2347 // create a tempfile in $TEMP/tmpdir/tmpname.
2348 createTestDirectory(aTmpName3);
2349 createTestFile(aTmpName4);
2351 // write chars into the file.
2352 File testFile(aTmpName4);
2354 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2355 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2356 sal_uInt64 nCount_write = 0;
2357 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2358 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2359 nError1 = testFile.close();
2360 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2363 void tearDown() override
2365 // remove the tempfile in $TEMP/tmpdir/tmpname.
2366 deleteTestFile(aTmpName4);
2367 deleteTestDirectory(aTmpName3);
2370 void setPos_001()
2372 File testFile(aTmpName4);
2373 char buffer_read[2];
2375 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2376 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2377 nError1 = testFile.setPos(osl_Pos_Absolut, 26);
2378 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2379 sal_uInt64 nCount_read = 0;
2380 nError1 = testFile.read(buffer_read, 1, nCount_read);
2381 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2382 nError1 = testFile.close();
2383 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2385 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setPos function: test for osl_Pos_Absolut, set the position to 26, test if the 26th char in file is correct",
2386 pBuffer_Char[26], buffer_read[0]);
2389 void setPos_002()
2391 File testFile(aTmpName4);
2392 char buffer_read[2];
2394 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2395 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2396 nError1 = testFile.setPos(osl_Pos_Absolut, sizeof(pBuffer_Char) - 2);
2397 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2398 nError1 = testFile.setPos(osl_Pos_Current, 0);
2399 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2400 sal_uInt64 nCount_read = 0;
2401 nError1 = testFile.read(buffer_read, 1, nCount_read);
2402 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2403 nError1 = testFile.close();
2404 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2406 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setPos function: test for osl_Pos_Current, set the position to end, test if the (end -1) char in file is correct",
2407 pBuffer_Char[sizeof(pBuffer_Char) - 2], buffer_read[0]);
2410 void setPos_003()
2412 File testFile(aTmpName4);
2413 char buffer_read[2];
2415 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2416 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2417 // the file size is smaller than 100
2418 nError1 = testFile.setPos(osl_Pos_End, -100);
2419 CPPUNIT_ASSERT_EQUAL_MESSAGE("should return error", osl::FileBase::E_INVAL, nError1);
2421 nError1 = testFile.setPos(osl_Pos_End, -53);
2422 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2423 sal_uInt64 nCount_read = 0;
2424 nError1 = testFile.read(buffer_read, 1, nCount_read);
2425 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2426 nError1 = testFile.close();
2427 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2429 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setPos function: test for osl_Pos_End, set the position to end, test if the first char in file is correct",
2430 pBuffer_Char[0], buffer_read[0]);
2433 CPPUNIT_TEST_SUITE(setPos);
2434 CPPUNIT_TEST(setPos_001);
2435 CPPUNIT_TEST(setPos_002);
2436 CPPUNIT_TEST(setPos_003);
2437 CPPUNIT_TEST_SUITE_END();
2440 // testing the method
2441 // inline RC getPos(sal_uInt64& uPos)
2443 class getPos : public CppUnit::TestFixture
2445 public:
2446 void setUp() override
2448 // create a tempfile in $TEMP/tmpdir/tmpname.
2449 createTestDirectory(aTmpName3);
2450 createTestFile(aTmpName4);
2452 // write chars into the file.
2453 File testFile(aTmpName4);
2455 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2456 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2457 sal_uInt64 nCount_write = 0;
2458 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2459 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2460 nError1 = testFile.close();
2461 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2464 void tearDown() override
2466 // remove the tempfile in $TEMP/tmpdir/tmpname.
2467 deleteTestFile(aTmpName4);
2468 deleteTestDirectory(aTmpName3);
2472 void getPos_001()
2474 File testFile(aTmpName4);
2475 sal_uInt64 nFilePointer;
2477 auto nError1 = testFile.getPos(nFilePointer);
2478 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, nError1);
2480 nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2481 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2483 nError1 = testFile.setPos(osl_Pos_Absolut, 26);
2484 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2485 nError1 = testFile.getPos(nFilePointer);
2486 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2488 nError1 = testFile.close();
2489 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2491 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getPos function: set the position to 26, get position and check if it is right",
2492 static_cast<sal_uInt64>(26), nFilePointer);
2495 CPPUNIT_TEST_SUITE(getPos);
2496 CPPUNIT_TEST(getPos_001);
2497 CPPUNIT_TEST_SUITE_END();
2500 // testing the method
2501 // inline RC isEndOfFile(sal_Bool *pIsEOF)
2503 class isEndOfFile : public CppUnit::TestFixture
2505 public:
2506 void setUp() override
2508 // create a tempfile in $TEMP/tmpdir/tmpname.
2509 createTestDirectory(aTmpName3);
2510 createTestFile(aTmpName4);
2512 // write chars into the file.
2513 File testFile(aTmpName4);
2515 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2516 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2517 sal_uInt64 nCount_write = 0;
2518 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2519 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2520 nError1 = testFile.close();
2521 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2524 void tearDown() override
2526 // remove the tempfile in $TEMP/tmpdir/tmpname.
2527 deleteTestFile(aTmpName4);
2528 deleteTestDirectory(aTmpName3);
2532 void isEndOfFile_001()
2534 File testFile(aTmpName4);
2535 sal_Bool bEOF = false;
2536 sal_Bool *pEOF = &bEOF;
2538 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2539 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2541 nError1 = testFile.setPos(osl_Pos_End, 0);
2542 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2543 nError1 = testFile.isEndOfFile(pEOF);
2544 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2546 nError1 = testFile.close();
2547 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2549 CPPUNIT_ASSERT_MESSAGE("test for isEndOfFile function: set the position to end, check if reach end",
2550 *pEOF);
2553 void isEndOfFile_002()
2555 File testFile(aTmpName4);
2556 sal_Bool bEOF = false;
2557 sal_Bool *pEOF = &bEOF;
2558 sal_uInt64 nFilePointer = 0;
2560 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2561 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2563 nError1 = testFile.setPos(osl_Pos_Absolut, 0);
2564 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2565 *pEOF = false;
2567 while (!(*pEOF))
2569 nError1 = testFile.isEndOfFile(pEOF);
2570 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2571 nError1 = testFile.setPos(osl_Pos_Current, 1);
2572 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2575 nError1 = testFile.getPos(nFilePointer);
2576 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2578 nError1 = testFile.close();
2579 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2581 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isEndOfFile function: use isEndOfFile to move pointer step by step",
2582 static_cast<sal_uInt64>(sizeof(pBuffer_Char) + 1), nFilePointer);
2584 CPPUNIT_TEST_SUITE(isEndOfFile);
2585 CPPUNIT_TEST(isEndOfFile_001);
2586 CPPUNIT_TEST(isEndOfFile_002);
2587 CPPUNIT_TEST_SUITE_END();
2590 // testing the method
2591 // inline RC setSize(sal_uInt64 uSize)
2593 class setSize : public CppUnit::TestFixture
2595 public:
2596 void setUp() override
2598 // create a tempfile in $TEMP/tmpdir/tmpname.
2599 createTestDirectory(aTmpName3);
2600 createTestFile(aTmpName4);
2602 // write chars into the file.
2603 File testFile(aTmpName4);
2605 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2606 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2607 sal_uInt64 nCount_write = 0;
2608 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2609 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2610 nError1 = testFile.close();
2611 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2614 void tearDown() override
2616 // remove the tempfile in $TEMP/tmpdir/tmpname.
2617 deleteTestFile(aTmpName4);
2618 deleteTestDirectory(aTmpName3);
2622 void setSize_001()
2624 File testFile(aTmpName4);
2625 sal_uInt64 nFilePointer;
2627 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2628 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2630 // enlarge the file to size of 100;
2631 nError1 = testFile.setSize(100);
2632 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2634 // get the file size;
2635 nError1 = testFile.setPos(osl_Pos_End, 0);
2636 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2637 nError1 = testFile.getPos(nFilePointer);
2638 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2640 nError1 = testFile.close();
2641 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2643 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: enlarge the file ",
2644 static_cast<sal_uInt64>(100), nFilePointer);
2647 void setSize_002()
2649 File testFile(aTmpName4);
2650 sal_uInt64 nFilePointer;
2652 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2653 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2655 // enlarge the file to size of 100;
2656 nError1 = testFile.setSize(10);
2657 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2659 // get the file size;
2660 nError1 = testFile.setPos(osl_Pos_End, 0);
2661 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2662 nError1 = testFile.getPos(nFilePointer);
2663 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2665 nError1 = testFile.close();
2666 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2668 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: truncate the file ",
2669 static_cast<sal_uInt64>(10), nFilePointer);
2672 CPPUNIT_TEST_SUITE(setSize);
2673 CPPUNIT_TEST(setSize_001);
2674 CPPUNIT_TEST(setSize_002);
2675 CPPUNIT_TEST_SUITE_END();
2678 // testing the method
2679 // inline RC read(void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead)
2681 class read : public CppUnit::TestFixture
2683 public:
2684 void setUp() override
2686 // create a tempfile in $TEMP/tmpdir/tmpname.
2687 createTestDirectory(aTmpName3);
2688 createTestFile(aTmpName4);
2690 // write chars into the file.
2691 File testFile(aTmpName4);
2693 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2694 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2695 sal_uInt64 nCount_write = 0;
2696 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2697 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2698 nError1 = testFile.close();
2699 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2702 void tearDown() override
2704 // remove the tempfile in $TEMP/tmpdir/tmpname.
2705 deleteTestFile(aTmpName4);
2706 deleteTestDirectory(aTmpName3);
2710 void read_001()
2712 File testFile(aTmpName4);
2713 sal_uInt64 nFilePointer;
2714 char buffer_read[10];
2716 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2717 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2718 sal_uInt64 nCount_read = 0;
2719 nError1 = testFile.read(buffer_read, 10, nCount_read);
2720 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2721 nError1 = testFile.getPos(nFilePointer);
2722 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2724 nError1 = testFile.close();
2725 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2727 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2728 sal_uInt64(10), nFilePointer);
2729 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2730 0, strncmp(buffer_read, pBuffer_Char, 10));
2733 void read_002()
2735 File testFile(aTmpName4);
2736 sal_uInt64 nFilePointer;
2737 char buffer_read[26];
2739 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2740 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2742 nError1 = testFile.setPos(osl_Pos_Absolut, 26);
2743 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2744 sal_uInt64 nCount_read = 0;
2745 nError1 = testFile.read(buffer_read, 26, nCount_read);
2746 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2747 nError1 = testFile.getPos(nFilePointer);
2748 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2750 nError1 = testFile.close();
2751 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2753 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2754 sal_uInt64(52), nFilePointer);
2755 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2756 sal_uInt64(26), nCount_read);
2757 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2758 0, strncmp(buffer_read, &pBuffer_Char[26], 26));
2761 CPPUNIT_TEST_SUITE(read);
2762 CPPUNIT_TEST(read_001);
2763 CPPUNIT_TEST(read_002);
2764 CPPUNIT_TEST_SUITE_END();
2767 // testing the method
2768 // inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
2770 class write : public CppUnit::TestFixture
2772 public:
2773 void setUp() override
2775 // create a tempfile in $TEMP/tmpname.
2776 createTestFile(aTmpName6);
2779 void tearDown() override
2781 // remove the tempfile in $TEMP/tmpname.
2782 deleteTestFile(aTmpName6);
2786 void write_001()
2788 File testFile(aTmpName6);
2789 sal_uInt64 nFilePointer;
2790 char buffer_read[10];
2792 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2793 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2795 sal_uInt64 nCount_write = 0, nCount_read = 0;
2796 // write chars into the file.
2797 nError1 = testFile.write(pBuffer_Char, 10, nCount_write);
2798 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2799 // get the current pointer;
2800 nError1 = testFile.getPos(nFilePointer);
2801 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2802 // reset pointer to the beginning;
2803 nError1 = testFile.setPos(osl_Pos_Absolut, 0);
2804 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2805 nError1 = testFile.read(buffer_read, 10, nCount_read);
2806 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2808 nError1 = testFile.close();
2809 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2811 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for write function: read whole content in the file to a buffer. Note, buffer size can not smaller than the read size",
2812 sal_uInt64(10), nFilePointer);
2813 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for write function: read whole content in the file to a buffer. Note, buffer size can not smaller than the read size",
2814 0, strncmp(buffer_read, pBuffer_Char, 10));
2815 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for write function: read whole content in the file to a buffer. Note, buffer size can not smaller than the read size",
2816 sal_uInt64(10), nCount_write);
2819 CPPUNIT_TEST_SUITE(write);
2820 CPPUNIT_TEST(write_001);
2821 CPPUNIT_TEST_SUITE_END();
2824 // testing the method
2825 // inline RC readLine(::ByteSequence& aSeq)
2827 class readLine : public CppUnit::TestFixture
2829 rtl::ByteSequence aSequence;
2831 public:
2832 void setUp() override
2834 // create a tempfile in $TEMP/tmpname.
2835 createTestFile(aTmpName6);
2837 // write some strings into the file.
2838 File testFile(aTmpName6);
2839 char ppStrSeq[3][27] = { "abcde\n",
2840 "1234567890\n",
2841 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2844 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2845 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2847 sal_uInt64 nCount_write = 0;
2848 for (int nCount = 0; nCount < 3; nCount++)
2850 nError1 = testFile.write(ppStrSeq[nCount], strlen(ppStrSeq[nCount]), nCount_write);
2851 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2854 nError1 = testFile.close();
2855 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2858 void tearDown() override
2860 // remove the tempfile in $TEMP/tmpname.
2861 deleteTestFile(aTmpName6);
2865 void readLine_001()
2867 File testFile(aTmpName6);
2869 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2870 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2871 nError1 = testFile.readLine(aSequence);
2872 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2873 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
2874 osl::FileBase::E_None, nError1);
2875 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
2876 0, strncmp(reinterpret_cast<char *>(aSequence.getArray()), pBuffer_Char, 5));
2879 void readLine_002()
2881 File testFile(aTmpName6);
2882 sal_Bool bEOF = false;
2883 sal_Bool *pEOF = &bEOF;
2885 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2886 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2887 for (int nCount = 0; nCount < 3; nCount++)
2889 nError1 = testFile.readLine(aSequence);
2890 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2892 nError1 = testFile.isEndOfFile(pEOF);
2893 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2895 CPPUNIT_ASSERT_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
2896 *pEOF);
2897 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
2898 0, strncmp(reinterpret_cast<char *>(aSequence.getArray()), &pBuffer_Char[26], 26));
2900 CPPUNIT_TEST_SUITE(readLine);
2901 CPPUNIT_TEST(readLine_001);
2902 CPPUNIT_TEST(readLine_002);
2903 CPPUNIT_TEST_SUITE_END();
2906 // testing the method
2907 // inline static RC copy(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
2909 class copy : public CppUnit::TestFixture
2911 public:
2912 void setUp() override
2914 // create a tempfile in $TEMP/tmpdir/tmpname.
2915 createTestDirectory(aTmpName3);
2916 createTestFile(aTmpName4);
2918 // write chars into the file.
2919 File testFile(aTmpName4);
2921 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2922 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2923 sal_uInt64 nCount_write = 0;
2924 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2925 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2926 nError1 = testFile.close();
2927 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2930 void tearDown() override
2932 // remove the tempfile in $TEMP/tmpdir/tmpname.
2933 deleteTestFile(aTmpName4);
2934 deleteTestDirectory(aTmpName3);
2937 void copy_001()
2939 File testFile(aTmpName6);
2941 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpname.
2942 auto nError1 = File::copy(aTmpName4, aTmpName6);
2943 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2944 // check
2945 nError1 = testFile.open(osl_File_OpenFlag_Create);
2946 deleteTestFile(aTmpName6);
2948 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy file to upper directory",
2949 osl::FileBase::E_EXIST, nError1);
2952 void copy_002()
2954 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
2955 auto nError1 = File::copy(aTmpName4, aTmpName3);
2957 CPPUNIT_ASSERT_MESSAGE("test for copy function: use directory as destination",
2958 (osl::FileBase::E_ISDIR == nError1) ||(osl::FileBase::E_ACCES == nError1));
2961 void copy_003()
2963 #if 0
2964 // copy $TEMP/tmpdir/tmpname to $ROOT/tmpname.
2965 auto nError1 = File::copy(aTmpName4, aTmpName7);
2966 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy to an illegal place",
2967 osl::FileBase::E_ACCES, nError1);
2968 #endif
2971 void copy_004()
2973 // copy $TEMP/tmpname to $TEMP/tmpdir/tmpname.
2974 auto nError1 = File::copy(aTmpName6, aTmpName4);
2976 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a not exist file",
2977 osl::FileBase::E_NOENT, nError1);
2980 void copy_005()
2982 // copy $TEMP/tmpname to $TEMP/system.path using system path.
2983 auto nError1 = File::copy(aTmpName6, aSysPath1);
2985 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a file using system file path",
2986 osl::FileBase::E_INVAL, nError1);
2989 void copy_006()
2991 createTestFile(aTmpName6);
2992 File tmpFile(aTmpName6);
2993 tmpFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Read);
2994 tmpFile.setSize(200);
2995 tmpFile.close();
2996 // copy to new path
2997 auto nError1 = File::copy(aTmpName6, aTmpName4);
2998 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3000 // check if is the new file
3001 File newFile(aTmpName4);
3002 newFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Read);
3003 nError1 = newFile.setPos(osl_Pos_End, 0);
3004 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3006 sal_uInt64 nFilePointer;
3007 nError1 = newFile.getPos(nFilePointer);
3008 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3009 newFile.close();
3010 deleteTestFile(aTmpName6);
3011 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: the dest file exist",
3012 static_cast<sal_uInt64>(200), nFilePointer);
3015 CPPUNIT_TEST_SUITE(copy);
3016 CPPUNIT_TEST(copy_001);
3017 CPPUNIT_TEST(copy_002);
3018 CPPUNIT_TEST(copy_003);
3019 CPPUNIT_TEST(copy_004);
3020 CPPUNIT_TEST(copy_005);
3021 CPPUNIT_TEST(copy_006);
3022 CPPUNIT_TEST_SUITE_END();
3025 // testing the method
3026 // inline static RC move(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
3028 class move : public CppUnit::TestFixture
3030 public:
3031 void setUp() override
3033 // create a tempfile in $TEMP/tmpdir/tmpname.
3034 createTestDirectory(aTmpName3);
3035 createTestFile(aTmpName4);
3037 // write chars into the file.
3038 File testFile(aTmpName4);
3040 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
3041 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3042 sal_uInt64 nCount_write = 0;
3043 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
3044 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3045 nError1 = testFile.close();
3046 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3049 void tearDown() override
3051 // remove the tempfile in $TEMP/tmpdir/tmpname.
3052 deleteTestFile(aTmpName4);
3053 deleteTestDirectory(aTmpName3);
3057 void move_001()
3059 // rename $TEMP/tmpdir/tmpname to $TEMP/canonical.name.
3060 auto nError1 = File::move(aTmpName4, aCanURL1);
3061 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3062 // check
3063 File testFile(aCanURL1);
3064 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
3065 deleteTestFile(aCanURL1);
3067 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: rename file to another directory",
3068 osl::FileBase::E_EXIST, nError2);
3071 void move_002()
3073 #ifdef _WIN32
3074 // move $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
3075 auto nError1 = File::move(aTmpName4, aTmpName3);
3076 // returned osl::FileBase::E_ACCES on WNT
3077 CPPUNIT_ASSERT_MESSAGE("test for move function: use directory as destination",
3078 (osl::FileBase::E_ACCES == nError1 || osl::FileBase::E_ISDIR == nError1) ||(osl::FileBase::E_EXIST == nError1));
3079 #endif
3082 void move_003()
3084 #if 0
3085 // move $TEMP/tmpdir/tmpname to $ROOT/tmpname.
3086 auto nError1 = File::move(aTmpName4, aTmpName7);
3087 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move to an illegal place",
3088 osl::FileBase::E_ACCES, nError1);
3089 #endif
3092 void move_004()
3094 // move $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3095 auto nError1 = File::move(aTmpName6, aTmpName4);
3097 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a not exist file",
3098 osl::FileBase::E_NOENT, nError1);
3101 void move_005()
3103 // move $TEMP/tmpname to $TEMP/system.path using system path.
3104 auto nError1 = File::move(aTmpName6, aSysPath1);
3106 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a file using system file",
3107 osl::FileBase::E_INVAL, nError1);
3110 void move_006()
3112 // move directory $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3113 createTestDirectory(aTmpName6);
3114 auto nError1 = File::move(aTmpName6, aTmpName4);
3115 // move file $TEMP/tmpdir/tmpname to $TEMP/tmpname
3116 auto nError2 = File::move(aTmpName4, aTmpName6);
3117 deleteTestDirectory(aTmpName6);
3118 #if defined(_WIN32)
3119 deleteTestDirectory(aTmpName4);// in Windows, it can be moved!!!!! this is only for not influence the following test.
3120 deleteTestFile(aTmpName6);
3121 nError1 = osl::FileBase::E_NOTDIR;
3122 nError2 = osl::FileBase::E_ISDIR;
3123 #endif
3124 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3125 osl::FileBase::E_NOTDIR, nError1);
3126 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3127 osl::FileBase::E_ISDIR, nError2);
3130 void move_007()
3132 // create directory $TEMP/tmpname.
3133 createTestDirectory(aTmpName6);
3134 // move directory $TEMP/tmpdir to $TEMP/tmpname/tmpdir
3135 auto nError1 = File::move(aTmpName3, aTmpName8);
3136 // check
3137 auto nError2 = Directory::create(aTmpName8);
3138 File::move(aTmpName8, aTmpName3);
3139 deleteTestDirectory(aTmpName6);
3141 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3142 osl::FileBase::E_None, nError1);
3143 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3144 osl::FileBase::E_EXIST, nError2);
3147 // bugid# 115420, after the bug fix, add the case
3148 CPPUNIT_TEST_SUITE(move);
3149 CPPUNIT_TEST(move_001);
3150 CPPUNIT_TEST(move_002);
3151 CPPUNIT_TEST(move_003);
3152 CPPUNIT_TEST(move_004);
3153 CPPUNIT_TEST(move_005);
3154 CPPUNIT_TEST(move_006);
3155 CPPUNIT_TEST(move_007);
3156 CPPUNIT_TEST_SUITE_END();
3159 // testing the method
3160 // inline static RC remove(const OUString& ustrFileURL)
3162 class remove : public CppUnit::TestFixture
3164 public:
3165 void setUp() override
3167 // create a tempfile in $TEMP/tmpdir/tmpname.
3168 createTestDirectory(aTmpName3);
3169 createTestFile(aTmpName4);
3171 // write chars into the file.
3172 File testFile(aTmpName4);
3174 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
3175 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3176 sal_uInt64 nCount_write = 0;
3177 nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
3178 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3179 nError1 = testFile.close();
3180 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3183 void tearDown() override
3185 // remove the tempfile in $TEMP/tmpdir/tmpname.
3186 deleteTestFile(aTmpName4);
3187 deleteTestDirectory(aTmpName3);
3191 void remove_001()
3193 // remove $TEMP/tmpdir/tmpname.
3194 auto nError1 = File::remove(aTmpName4);
3195 // check
3196 File testFile(aTmpName4);
3197 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
3199 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file",
3200 osl::FileBase::E_None, nError1);
3201 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a file",
3202 (osl::FileBase::E_EXIST != nError2));
3205 void remove_002()
3207 // remove $TEMP/tmpname.
3208 auto nError1 = File::remove(aTmpName6);
3210 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file not exist",
3211 osl::FileBase::E_NOENT, nError1);
3214 void remove_003()
3216 // remove $TEMP/system/path.
3217 auto nError1 = File::remove(aSysPath2);
3219 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: removing a file not using full qualified URL",
3220 osl::FileBase::E_INVAL, nError1);
3223 void remove_004()
3225 // remove $TEMP/tmpdir.
3226 auto nError1 = File::remove(aTmpName3);
3228 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a directory",
3229 (osl::FileBase::E_ISDIR == nError1) || (osl::FileBase::E_ACCES == nError1));
3232 CPPUNIT_TEST_SUITE(remove);
3233 CPPUNIT_TEST(remove_001);
3234 CPPUNIT_TEST(remove_002);
3235 CPPUNIT_TEST(remove_003);
3236 CPPUNIT_TEST(remove_004);
3237 CPPUNIT_TEST_SUITE_END();
3240 // testing the method
3241 // inline static RC setAttributes(const OUString& ustrFileURL, sal_uInt64 uAttributes)
3243 class setAttributes : public CppUnit::TestFixture
3245 private:
3246 DirectoryItem rItem;
3248 public:
3249 void setUp() override
3251 // create a tempfile in $TEMP/tmpdir/tmpname.
3252 createTestFile(aTmpName6);
3255 void tearDown() override
3257 // remove the tempfile in $TEMP/tmpdir/tmpname.
3258 deleteTestFile(aTmpName6);
3262 void setAttributes_001()
3264 // on windows, only can set 2 attributes: osl_File_Attribute_ReadOnly, osl_File_Attribute_Hidden
3265 #ifdef UNX
3266 // set the file to readonly
3267 auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead);
3268 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3269 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3270 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3271 // get the file attributes
3272 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
3273 nError1 = rItem.getFileStatus(rFileStatus);
3274 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3276 if (geteuid() == 0) // as root, access(W_OK) may be true despite mode
3278 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3279 static_cast<sal_uInt64>(osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead),
3280 rFileStatus.getAttributes());
3282 else
3284 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3285 static_cast<sal_uInt64>(osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead),
3286 rFileStatus.getAttributes());
3288 #else
3289 // please see GetFileAttributes
3290 auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly);
3291 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3292 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3293 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3294 // get the file attributes
3295 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
3296 nError1 = rItem.getFileStatus(rFileStatus);
3297 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3298 // here the file has 2 Attributes: FILE_ATTRIBUTE_READONLY and FILE_ATTRIBUTE_NORMAL,
3299 // but FILE_ATTRIBUTE_NORMAL is valid only if used alone, so this is maybe a bug
3300 /*OString aString = OUStringToOString(aTmpName6, RTL_TEXTENCODING_ASCII_US);
3301 DWORD dwFileAttributes = GetFileAttributes(aString.getStr());
3302 if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
3303 printf("has normal attribute");
3304 if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
3305 printf("has readonly attribute");
3307 CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes READONLY and get it to verify.",
3308 (osl_File_Attribute_ReadOnly & rFileStatus.getAttributes()) != 0);
3309 #endif
3311 void setAttributes_002()
3313 // on UNX, can not set hidden attribute to file, rename file can set the attribute
3314 #ifdef _WIN32
3315 // set the file to hidden
3316 auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_Hidden);
3318 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3319 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3320 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3321 // get the file attributes
3322 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
3323 nError1 = rItem.getFileStatus(rFileStatus);
3324 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3326 CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3327 (osl_File_Attribute_Hidden & rFileStatus.getAttributes()) != 0);
3328 #endif
3331 CPPUNIT_TEST_SUITE(setAttributes);
3332 CPPUNIT_TEST(setAttributes_001);
3333 CPPUNIT_TEST(setAttributes_002);
3334 CPPUNIT_TEST_SUITE_END();
3337 // testing the method
3338 // inline static RC setTime(
3339 // const OUString& ustrFileURL,
3340 // const TimeValue& rCreationTime,
3341 // const TimeValue& rLastAccessTime,
3342 // const TimeValue& rLastWriteTime)
3344 class setTime : public CppUnit::TestFixture
3346 private:
3347 DirectoryItem rItem;
3349 public:
3350 void setUp() override
3352 // create a tempfile in $TEMP/tmpdir/tmpname.
3353 createTestFile(aTmpName6);
3356 void tearDown() override
3358 // remove the tempfile in $TEMP/tmpdir/tmpname.
3359 deleteTestFile(aTmpName6);
3363 void setTime_001()
3365 TimeValue *pTV_current = nullptr;
3366 CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3367 TimeValue *pTV_creation = nullptr;
3368 CPPUNIT_ASSERT((pTV_creation = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3369 TimeValue *pTV_access = nullptr;
3370 CPPUNIT_ASSERT((pTV_access = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3371 TimeValue *pTV_modify = nullptr;
3372 CPPUNIT_ASSERT((pTV_modify = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3374 // get current time
3375 bool bOk = osl_getSystemTime(pTV_current);
3376 CPPUNIT_ASSERT(bOk);
3378 // set the file time
3379 auto nError2 = File::setTime(aTmpName6, *pTV_current, *pTV_current, *pTV_current);
3380 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError2).getStr(), osl::FileBase::E_None, nError2);
3382 // get the file access time, creation time, modify time
3383 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3384 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3386 FileStatus rFileStatus(osl_FileStatus_Mask_AccessTime);
3387 nError1 = rItem.getFileStatus(rFileStatus);
3388 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3389 *pTV_access = rFileStatus.getAccessTime();
3391 FileStatus rFileStatus1(osl_FileStatus_Mask_CreationTime);
3392 nError1 = rItem.getFileStatus(rFileStatus1);
3393 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3394 *pTV_creation = rFileStatus1.getCreationTime();
3396 FileStatus rFileStatus2(osl_FileStatus_Mask_ModifyTime);
3397 nError1 = rItem.getFileStatus(rFileStatus2);
3398 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3399 *pTV_modify = rFileStatus2.getModifyTime();
3401 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set access time then get it. time precision is still a problem for it cut off the nanosec.",
3402 t_compareTime(pTV_access, pTV_current, delta));
3403 #if defined(_WIN32)
3404 // Unfortunately there is no way to get the creation time of a file under Unix (it's a Windows only feature).
3405 // That means the flag osl_FileStatus_Mask_CreationTime should be deprecated under Unix.
3406 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set creation time then get it. ",
3407 t_compareTime(pTV_creation, pTV_current, delta));
3408 #endif
3409 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set modify time then get it. ",
3410 t_compareTime(pTV_modify, pTV_current, delta));
3411 free(pTV_current);
3412 free(pTV_creation);
3413 free(pTV_access);
3414 free(pTV_modify);
3417 CPPUNIT_TEST_SUITE(setTime);
3418 CPPUNIT_TEST(setTime_001);
3419 CPPUNIT_TEST_SUITE_END();
3422 // testing the method
3423 // inline static RC sync()
3425 class sync : public CppUnit::TestFixture
3427 private:
3428 DirectoryItem rItem;
3430 public:
3431 void setUp() override
3433 // create a tempfile in $TEMP/tmpdir/tmpname.
3434 createTestFile(aTmpName6);
3438 void tearDown() override
3440 // remove the tempfile in $TEMP/tmpdir/tmpname.
3441 deleteTestFile(aTmpName6);
3444 // test case: if The file is located on a read only file system.
3445 void sync_001()
3447 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3448 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3450 File tmp_file(aTmpName6);
3451 osl::FileBase::RC err = tmp_file.open(osl_File_OpenFlag_Write);
3453 CPPUNIT_ASSERT_EQUAL_MESSAGE("File open failed", osl::FileBase::E_None, err);
3455 char buffer[50000];
3456 sal_uInt64 written = 0;
3457 nError1 = tmp_file.write(static_cast<void*>(buffer), sizeof(buffer), written);
3458 CPPUNIT_ASSERT_EQUAL_MESSAGE("write failed!", osl::FileBase::E_None, nError1);
3460 // set the file to readonly
3461 auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead);
3462 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3464 nError2 = tmp_file.sync();
3466 CPPUNIT_ASSERT_EQUAL_MESSAGE("can not sync to readonly file!", osl::FileBase::E_None, nError2);
3468 tmp_file.close();
3470 // test case:no enough space, how to create such case???see test_cpy_wrt_file.cxx::test_osl_writeFile
3472 CPPUNIT_TEST_SUITE(sync);
3473 CPPUNIT_TEST(sync_001);
3474 CPPUNIT_TEST_SUITE_END();
3477 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::ctors, "osl_File");
3478 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::open, "osl_File");
3479 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::close, "osl_File");
3480 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setPos, "osl_File");
3481 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::getPos, "osl_File");
3482 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::isEndOfFile, "osl_File");
3483 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setSize, "osl_File");
3484 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::read, "osl_File");
3485 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::write, "osl_File");
3486 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::readLine, "osl_File");
3487 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::copy, "osl_File");
3488 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::move, "osl_File");
3489 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::remove, "osl_File");
3490 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setAttributes, "osl_File");
3491 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setTime, "osl_File");
3492 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::sync, "osl_File");
3494 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_File");
3497 // Beginning of the test cases for DirectoryItem class
3499 namespace osl_DirectoryItem
3501 // testing the method
3502 // DirectoryItem(): _pData(NULL)
3504 class ctors : public CppUnit::TestFixture
3506 public:
3507 void setUp() override
3509 // create a tempfile in $TEMP/tmpname.
3510 createTestFile(aTmpName6);
3513 void tearDown() override
3515 // remove the tempfile in $TEMP/tmpname.
3516 deleteTestFile(aTmpName6);
3519 void ctors_001()
3521 File testFile(aTmpName6);
3522 DirectoryItem rItem; // constructor
3524 // get the DirectoryItem.
3525 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3526 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3528 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a new instance of DirectoryItem and get an item to check.",
3529 osl::FileBase::E_None, nError1);
3532 CPPUNIT_TEST_SUITE(ctors);
3533 CPPUNIT_TEST(ctors_001);
3534 CPPUNIT_TEST_SUITE_END();
3537 // testing the method
3538 // DirectoryItem(const DirectoryItem& rItem): _pData(rItem._pData)
3540 class copy_assin_Ctors : public CppUnit::TestFixture
3542 public:
3543 void setUp() override
3545 // create a tempfile in $TEMP/tmpname.
3546 createTestFile(aTmpName6);
3549 void tearDown() override
3551 // remove the tempfile in $TEMP/tmpname.
3552 deleteTestFile(aTmpName6);
3556 void copy_assin_Ctors_001()
3558 DirectoryItem rItem; // constructor
3559 // get the DirectoryItem.
3560 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3561 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3563 DirectoryItem copyItem(rItem); // copy constructor
3564 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3565 nError1 = copyItem.getFileStatus(rFileStatus);
3566 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3568 CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: use copy constructor to get an item and check filename.",
3569 compareFileName(rFileStatus.getFileName(), aTmpName2));
3572 void copy_assin_Ctors_002()
3574 DirectoryItem rItem; // constructor
3575 // get the DirectoryItem.
3576 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3577 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3579 DirectoryItem copyItem;
3580 copyItem = rItem; // assignment operator
3581 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3582 nError1 = copyItem.getFileStatus(rFileStatus);
3583 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3585 CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: test assignment operator here since it is same as copy constructor in test way.",
3586 compareFileName(rFileStatus.getFileName(), aTmpName2));
3589 CPPUNIT_TEST_SUITE(copy_assin_Ctors);
3590 CPPUNIT_TEST(copy_assin_Ctors_001);
3591 CPPUNIT_TEST(copy_assin_Ctors_002);
3592 CPPUNIT_TEST_SUITE_END();
3595 // testing the method
3596 // inline sal_Bool is()
3598 class is : public CppUnit::TestFixture
3600 public:
3601 void setUp() override
3603 // create a tempfile in $TEMP/tmpname.
3604 createTestFile(aTmpName6);
3607 void tearDown() override
3609 // remove the tempfile in $TEMP/tmpname.
3610 deleteTestFile(aTmpName6);
3613 void is_001()
3615 DirectoryItem rItem; // constructor
3617 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3618 !rItem.is());
3621 void is_002()
3623 DirectoryItem rItem; // constructor
3624 // get the DirectoryItem.
3625 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3626 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3628 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3629 rItem.is());
3632 CPPUNIT_TEST_SUITE(is);
3633 CPPUNIT_TEST(is_001);
3634 CPPUNIT_TEST(is_002);
3635 CPPUNIT_TEST_SUITE_END();
3638 // testing the method
3639 // static inline RC get(const OUString& ustrFileURL, DirectoryItem& rItem)
3641 class get : public CppUnit::TestFixture
3643 public:
3644 void setUp() override
3646 // create a tempfile in $TEMP/tmpname.
3647 createTestFile(aTmpName6);
3650 void tearDown() override
3652 // remove the tempfile in $TEMP/tmpname.
3653 deleteTestFile(aTmpName6);
3657 void get_001()
3659 DirectoryItem rItem;
3660 auto nError2 = DirectoryItem::get(aTmpName6, rItem);
3662 // check the file name
3663 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3664 auto nError1 = rItem.getFileStatus(rFileStatus);
3665 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3667 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3668 osl::FileBase::E_None, nError2);
3669 CPPUNIT_ASSERT_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3670 compareFileName(rFileStatus.getFileName(), aTmpName2));
3673 void get_002()
3675 DirectoryItem rItem;
3676 auto nError1 = DirectoryItem::get(aSysPath1, rItem);
3678 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a system name instead of a URL.",
3679 osl::FileBase::E_INVAL, nError1);
3682 void get_003()
3684 DirectoryItem rItem;
3686 auto nError1 = DirectoryItem::get(aTmpName3, rItem);
3688 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a non existed file URL.",
3689 osl::FileBase::E_NOENT, nError1);
3692 CPPUNIT_TEST_SUITE(get);
3693 CPPUNIT_TEST(get_001);
3694 CPPUNIT_TEST(get_002);
3695 CPPUNIT_TEST(get_003);
3696 CPPUNIT_TEST_SUITE_END();
3699 // testing the method
3700 // inline RC getFileStatus(FileStatus& rStatus)
3702 class getFileStatus : public CppUnit::TestFixture
3704 public:
3705 void setUp() override
3707 // create a tempfile in $TEMP/tmpdir/tmpname.
3708 createTestDirectory(aTmpName3);
3709 createTestFile(aTmpName4);
3712 void tearDown() override
3714 // remove the tempfile in $TEMP/tmpdir/tmpname.
3715 deleteTestFile(aTmpName4);
3716 deleteTestDirectory(aTmpName3);
3720 void getFileStatus_001()
3722 DirectoryItem rItem;
3723 // get the DirectoryItem.
3724 auto nError1 = DirectoryItem::get(aTmpName4, rItem);
3725 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3727 // check the file name
3728 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3729 auto nError2 = rItem.getFileStatus(rFileStatus);
3731 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get file status and check filename",
3732 osl::FileBase::E_None, nError2);
3733 CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get file status and check filename",
3734 compareFileName(rFileStatus.getFileName(), aTmpName2));
3737 void getFileStatus_002()
3739 DirectoryItem rItem; // constructor
3740 // get the DirectoryItem.
3741 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3742 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_NOENT, nError1);
3744 // check the file name
3745 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3746 auto nError2 = rItem.getFileStatus(rFileStatus);
3748 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: file not existed",
3749 osl::FileBase::E_INVAL, nError2);
3752 void getFileStatus_003()
3754 DirectoryItem rItem; // constructor
3755 // get the DirectoryItem.
3756 auto nError1 = DirectoryItem::get(aTmpName3, rItem);
3757 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3759 // check the file name
3760 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3761 auto nError2 = rItem.getFileStatus(rFileStatus);
3763 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get directory information",
3764 osl::FileBase::E_None, nError2);
3765 CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get directory information",
3766 compareFileName(rFileStatus.getFileName(), aTmpName1));
3769 CPPUNIT_TEST_SUITE(getFileStatus);
3770 CPPUNIT_TEST(getFileStatus_001);
3771 CPPUNIT_TEST(getFileStatus_002);
3772 CPPUNIT_TEST(getFileStatus_003);
3773 CPPUNIT_TEST_SUITE_END();
3776 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::ctors, "osl_DirectoryItem");
3777 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::copy_assin_Ctors, "osl_DirectoryItem");
3778 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::is, "osl_DirectoryItem");
3779 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::get, "osl_DirectoryItem");
3780 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::getFileStatus, "osl_DirectoryItem");
3782 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_DirectoryItem");
3785 // Beginning of the test cases for Directory class
3787 namespace osl_Directory
3789 // testing the method
3790 // Directory(const OUString& strPath): _pData(0), _aPath(strPath)
3792 class ctors : public CppUnit::TestFixture
3794 public:
3795 void setUp() override
3797 // create a tempfile in $TEMP/tmpdir/tmpname.
3798 createTestDirectory(aTmpName3);
3799 createTestFile(aTmpName4);
3802 void tearDown() override
3804 // remove the tempfile in $TEMP/tmpdir/tmpname.
3805 deleteTestFile(aTmpName4);
3806 deleteTestDirectory(aTmpName3);
3807 // LLA: t_print("tearDown done.\n");
3811 void ctors_001()
3813 Directory testDirectory(aTmpName3); // constructor
3815 // open a directory
3816 auto nError1 = testDirectory.open();
3817 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3818 // close a directory
3819 auto nError2 = testDirectory.close();
3820 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3822 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
3823 osl::FileBase::E_None, nError1);
3824 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
3825 osl::FileBase::E_None, nError2);
3828 void ctors_002()
3830 Directory testDirectory(aTmpName9); // constructor
3832 // open a directory
3833 auto nError1 = testDirectory.open();
3834 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3835 // close a directory
3836 auto nError2 = testDirectory.close();
3837 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3839 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
3840 osl::FileBase::E_None, nError1);
3841 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
3842 osl::FileBase::E_None, nError2);
3845 CPPUNIT_TEST_SUITE(ctors);
3846 CPPUNIT_TEST(ctors_001);
3847 CPPUNIT_TEST(ctors_002);
3848 CPPUNIT_TEST_SUITE_END();
3851 // testing the method
3852 // inline RC open()
3854 class open : public CppUnit::TestFixture
3856 public:
3857 void setUp() override
3859 // create a tempfile in $TEMP/tmpdir/tmpname.
3860 createTestDirectory(aTmpName3);
3861 createTestFile(aTmpName4);
3864 void tearDown() override
3866 // remove the tempfile in $TEMP/tmpdir/tmpname.
3867 deleteTestFile(aTmpName4);
3868 deleteTestDirectory(aTmpName3);
3871 void open_001()
3873 Directory testDirectory(aTmpName3);
3875 // open a directory
3876 auto nError1 = testDirectory.open();
3877 // check if directory is opened.
3878 bool bOk = testDirectory.isOpen();
3879 // close a directory
3880 auto nError2 = testDirectory.close();
3882 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory and check for open",
3883 bOk);
3884 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
3885 osl::FileBase::E_None, nError1);
3886 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
3887 osl::FileBase::E_None, nError2);
3890 void open_002()
3892 Directory testDirectory(aTmpName6);
3894 auto nError1 = testDirectory.open();
3895 if (nError1 == osl::FileBase::E_None)
3897 auto nError2 = testDirectory.close();
3898 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3901 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a file that is not existed",
3902 osl::FileBase::E_NOENT, nError1);
3905 void open_003()
3907 Directory testDirectory(aUserDirectorySys);
3909 auto nError1 = testDirectory.open();
3910 if (nError1 == osl::FileBase::E_None)
3912 auto nError2 = testDirectory.close();
3913 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3916 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: using system path",
3917 osl::FileBase::E_INVAL, nError1);
3920 void open_004()
3922 Directory testDirectory(aTmpName4);
3924 auto nError1 = testDirectory.open();
3925 if (nError1 == osl::FileBase::E_None)
3927 auto nError2 = testDirectory.close();
3928 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3931 CPPUNIT_ASSERT_MESSAGE("test for open function: open a file instead of a directory",
3932 (osl::FileBase::E_NOTDIR == nError1) || (osl::FileBase::E_ACCES == nError1));
3935 CPPUNIT_TEST_SUITE(open);
3936 CPPUNIT_TEST(open_001);
3937 CPPUNIT_TEST(open_002);
3938 CPPUNIT_TEST(open_003);
3939 CPPUNIT_TEST(open_004);
3940 CPPUNIT_TEST_SUITE_END();
3943 // testing the method
3944 // inline sal_Bool isOpen() { return _pData != NULL; };
3946 class isOpen : public CppUnit::TestFixture
3948 public:
3949 void setUp() override
3951 // create a tempfile in $TEMP/tmpdir/tmpname.
3952 createTestDirectory(aTmpName3);
3953 createTestFile(aTmpName4);
3956 void tearDown() override
3958 // remove the tempfile in $TEMP/tmpdir/tmpname.
3959 deleteTestFile(aTmpName4);
3960 deleteTestDirectory(aTmpName3);
3964 void isOpen_001()
3966 Directory testDirectory(aTmpName3); // constructor
3968 // open a directory
3969 auto nError1 = testDirectory.open();
3970 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3971 // check if directory is opened.
3972 bool bOk = testDirectory.isOpen();
3973 // close a directory
3974 auto nError2 = testDirectory.close();
3975 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3976 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: open a directory and check for open",
3977 bOk);
3980 void isOpen_002()
3982 Directory testDirectory(aTmpName3); // constructor
3984 // check if directory is opened.
3985 bool bOk = testDirectory.isOpen();
3987 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: do not open a directory and check for open",
3988 !bOk);
3991 CPPUNIT_TEST_SUITE(isOpen);
3992 CPPUNIT_TEST(isOpen_001);
3993 CPPUNIT_TEST(isOpen_002);
3994 CPPUNIT_TEST_SUITE_END();
3997 // testing the method
3998 // inline RC close()
4000 class close : public CppUnit::TestFixture
4002 public:
4003 void setUp() override
4005 // create a tempdirectory : $TEMP/tmpdir.
4006 createTestDirectory(aTmpName3);
4009 void tearDown() override
4011 // remove a tempdirectory : $TEMP/tmpdir.
4012 deleteTestDirectory(aTmpName3);
4015 void close_001()
4017 Directory testDirectory(aTmpName3);
4019 // open a directory
4020 auto nError1 = testDirectory.open();
4021 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4022 // close a directory
4023 auto nError2 = testDirectory.close();
4024 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
4025 // check if directory is opened.
4026 bool bOk = testDirectory.isOpen();
4028 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: close a directory and check for open",
4029 !bOk);
4032 void close_002()
4034 Directory testDirectory(aTmpName3);
4036 // close a directory
4037 auto nError1 = testDirectory.close();
4039 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isOpen function: close a not opened directory",
4040 osl::FileBase::E_BADF, nError1);
4043 CPPUNIT_TEST_SUITE(close);
4044 CPPUNIT_TEST(close_001);
4045 CPPUNIT_TEST(close_002);
4046 CPPUNIT_TEST_SUITE_END();
4049 // testing the method
4050 // inline RC reset()
4052 class reset : public CppUnit::TestFixture
4054 private:
4055 DirectoryItem rItem;
4057 public:
4058 void setUp() override
4060 // create a tempdirectory : $TEMP/tmpdir.
4061 createTestDirectory(aTmpName3);
4062 // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4063 createTestFile(aTmpName3, aTmpName2);
4064 createTestFile(aTmpName3, aTmpName1);
4065 createTestFile(aTmpName3, aHidURL1);
4068 void tearDown() override
4070 // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4071 deleteTestFile(aTmpName3, aHidURL1);
4072 deleteTestFile(aTmpName3, aTmpName1);
4073 deleteTestFile(aTmpName3, aTmpName2);
4074 // remove a tempdirectory : $TEMP/tmpdir.
4075 deleteTestDirectory(aTmpName3);
4079 void reset_001()
4081 Directory testDirectory(aTmpName3); // constructor
4083 // open a directory
4084 auto nError1 = testDirectory.open();
4085 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4086 // get first Item
4087 nError1 = testDirectory.getNextItem(rItem, 1);
4088 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4089 // check the file name of first Item
4090 FileStatus rFileStatusFirst(osl_FileStatus_Mask_FileName);
4091 nError1 = rItem.getFileStatus(rFileStatusFirst);
4093 // get second Item
4094 // mindy: nError1 = testDirectory.getNextItem(rItem, 0);
4095 // mindy: CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4097 // reset enumeration
4098 auto nError2 = testDirectory.reset();
4099 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
4100 // get reset Item, if reset does not work, getNextItem() should return the second Item (aTmpName1)
4101 nError1 = testDirectory.getNextItem(rItem);
4102 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4104 // check the file name again
4105 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
4106 nError1 = rItem.getFileStatus(rFileStatus);
4107 // close a directory
4108 nError1 = testDirectory.close();
4109 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4111 bool bOK1,bOK2,bOK3;
4112 bOK1 = compareFileName(rFileStatus.getFileName(), aTmpName2);
4113 bOK2 = compareFileName(rFileStatus.getFileName(), aHidURL1);
4114 bOK3 = compareFileName(rFileStatus.getFileName(), rFileStatusFirst.getFileName());
4115 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4116 osl::FileBase::E_None, nError2);
4117 CPPUNIT_ASSERT_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4118 (bOK1 || bOK2 || bOK3));
4121 void reset_002()
4123 Directory testDirectory(aTmpName6); // constructor
4125 // close a directory
4126 auto nError1 = testDirectory.reset();
4128 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: reset a non existed directory",
4129 osl::FileBase::E_NOENT, nError1);
4132 void reset_003()
4134 Directory testDirectory(aTmpName4); // constructor
4136 // close a directory
4137 auto nError1 = testDirectory.reset();
4139 CPPUNIT_ASSERT_MESSAGE("test for reset function: reset a file instead of a directory",
4140 (osl::FileBase::E_NOTDIR == nError1) || (osl::FileBase::E_NOENT == nError1));
4143 void reset_004()
4145 Directory testDirectory(aUserDirectorySys); // constructor
4147 // close a directory
4148 auto nError1 = testDirectory.reset();
4150 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: use a system path",
4151 osl::FileBase::E_INVAL, nError1);
4154 CPPUNIT_TEST_SUITE(reset);
4155 CPPUNIT_TEST(reset_001);
4156 CPPUNIT_TEST(reset_002);
4157 CPPUNIT_TEST(reset_003);
4158 CPPUNIT_TEST(reset_004);
4159 CPPUNIT_TEST_SUITE_END();
4162 // testing the method
4163 // inline RC getNextItem(DirectoryItem& rItem, sal_uInt32 nHint = 0)
4165 class getNextItem : public CppUnit::TestFixture
4167 private:
4168 DirectoryItem rItem;
4170 public:
4171 void setUp() override
4173 // create a tempdirectory : $TEMP/tmpdir.
4174 createTestDirectory(aTmpName3);
4175 // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4176 createTestFile(aTmpName3, aTmpName2);
4177 createTestFile(aTmpName3, aTmpName1);
4178 createTestFile(aTmpName3, aHidURL1);
4182 void tearDown() override
4184 // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4185 deleteTestFile(aTmpName3, aHidURL1);
4186 deleteTestFile(aTmpName3, aTmpName1);
4187 deleteTestFile(aTmpName3, aTmpName2);
4188 // remove a tempdirectory : $TEMP/tmpdir.
4189 deleteTestDirectory(aTmpName3);
4193 void getNextItem_001()
4195 Directory testDirectory(aTmpName3); // constructor
4197 // open a directory
4198 auto nError1 = testDirectory.open();
4199 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4201 // check the file name
4202 bool bOk1 = false;
4203 bool bOk2 = false;
4204 bool bOk3 = false;
4205 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
4207 for (int nCount = 0; nCount < 3; nCount++)
4209 // get three Items
4210 nError1 = testDirectory.getNextItem(rItem, 2);
4211 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4212 nError1 = rItem.getFileStatus(rFileStatus);
4213 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4215 // a special order is not guaranteed. So any file may occur on any time.
4216 // But every file name should occur only once.
4217 if (!bOk1 && compareFileName(rFileStatus.getFileName(), aTmpName1))
4219 bOk1 = true;
4222 if (!bOk2 && compareFileName(rFileStatus.getFileName(), aTmpName2))
4224 bOk2 = true;
4227 if (!bOk3 && compareFileName(rFileStatus.getFileName(), aHidURL1))
4229 bOk3 = true;
4233 // close a directory
4234 nError1 = testDirectory.close();
4235 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4237 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4238 bOk1);
4239 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4240 bOk2);
4241 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4242 bOk3);
4245 void getNextItem_002()
4247 Directory testDirectory(aTmpName3); // constructor
4248 auto nError1 = testDirectory.getNextItem(rItem);
4250 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getNextItem function: retrieve an item in a directory which is not opened, also test for nHint's default value.",
4251 osl::FileBase::E_INVAL, nError1);
4254 void getNextItem_003()
4256 Directory testDirectory(aTmpName3); // constructor
4258 // open a directory
4259 auto nError1 = testDirectory.open();
4260 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4262 osl::FileBase::RC nError2 = osl::FileBase::E_None;
4263 for (int nCount = 0; nCount < 4; nCount++)
4265 nError2 = testDirectory.getNextItem(rItem, 3);
4268 // close a directory
4269 nError1 = testDirectory.close();
4270 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4272 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getNextItem function: retrieve 4 times in a directory which contain only 3 files.",
4273 osl::FileBase::E_NOENT, nError2);
4276 void getNextItem_004()
4278 // create a link file(can not on Windows), then check if getNextItem can get it.
4279 #ifdef UNX
4280 bool bLnkOK = false;
4281 bool bFoundOK = false;
4283 OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
4284 aUStr_LnkFileSys += aSlashURL + "/tmpdir/link.file";
4285 aUStr_SrcFileSys += aSlashURL + "/tmpdir/tmpname";
4287 OString strLinkFileName, strSrcFileName;
4288 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
4289 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
4291 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
4292 sal_Int32 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
4293 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
4294 Directory testDirectory(aTmpName3);
4296 // open a directory
4297 auto nError1 = testDirectory.open();
4298 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4299 OUString aFileName (u"link.file"_ustr);
4301 while (true) {
4302 nError1 = testDirectory.getNextItem(rItem, 4);
4303 if (nError1 == osl::FileBase::E_None) {
4304 FileStatus rFileStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Type);
4305 rItem.getFileStatus(rFileStatus);
4306 if (compareFileName(rFileStatus.getFileName(), aFileName))
4308 bFoundOK = true;
4309 if (rFileStatus.getFileType() == FileStatus::Link)
4311 bLnkOK = true;
4312 break;
4316 else
4317 break;
4319 fd = std::remove(strLinkFileName.getStr());
4320 CPPUNIT_ASSERT_EQUAL_MESSAGE("remove link file failed", static_cast<sal_Int32>(0), fd);
4321 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if can retrieve the link file name",
4322 bFoundOK);
4323 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if link file has file type link",
4324 bLnkOK);
4325 #endif
4328 CPPUNIT_TEST_SUITE(getNextItem);
4329 CPPUNIT_TEST(getNextItem_001);
4330 CPPUNIT_TEST(getNextItem_002);
4331 CPPUNIT_TEST(getNextItem_003);
4332 CPPUNIT_TEST(getNextItem_004);
4333 CPPUNIT_TEST_SUITE_END();
4336 // testing the method
4337 // inline static RC getVolumeInfo(const OUString& ustrDirectoryURL, VolumeInfo& rInfo)
4339 class getVolumeInfo : public CppUnit::TestFixture
4341 public:
4342 void checkValidMask(osl::VolumeInfo const& _aVolumeInfo, sal_Int32 _nMask)
4344 if (_nMask == osl_VolumeInfo_Mask_FileSystemName)
4346 // get file system name
4347 OUString aFileSysName = _aVolumeInfo.getFileSystemName();
4349 bool bRes2 = compareFileName(aFileSysName, aNullURL);
4350 CPPUNIT_ASSERT_MESSAGE("test for getVolumeInfo function: getVolumeInfo of root directory.",
4351 !bRes2);
4354 if (_nMask == osl_VolumeInfo_Mask_Attributes)
4356 bool b1 = _aVolumeInfo.getRemoteFlag();
4357 bool b2 = _aVolumeInfo.getRemoveableFlag();
4358 bool b3 = _aVolumeInfo.getCompactDiscFlag();
4359 bool b4 = _aVolumeInfo.getFloppyDiskFlag();
4360 bool b5 = _aVolumeInfo.getFixedDiskFlag();
4361 bool b6 = _aVolumeInfo.getRAMDiskFlag();
4363 OString sAttr;
4364 if (b1) sAttr = "Remote"_ostr;
4365 if (b2) sAttr += " Removeable";
4366 if (b3) sAttr += " CDROM";
4367 if (b4) sAttr += " Floppy";
4368 if (b5) sAttr += " FixedDisk";
4369 if (b6) sAttr += " RAMDisk";
4371 printf("Attributes: %s\n", sAttr.getStr());
4373 if (_nMask == osl_VolumeInfo_Mask_TotalSpace)
4375 // within Linux, df / * 1024 bytes is the result
4376 sal_uInt64 nSize = _aVolumeInfo.getTotalSpace();
4377 printf("Total space: %" SAL_PRIuUINT64 "\n", nSize);
4379 if (_nMask == osl_VolumeInfo_Mask_UsedSpace)
4381 sal_uInt64 nSize = _aVolumeInfo.getUsedSpace();
4382 printf(" Used space: %" SAL_PRIuUINT64 "\n", nSize);
4384 if (_nMask == osl_VolumeInfo_Mask_FreeSpace)
4386 sal_uInt64 nSize = _aVolumeInfo.getFreeSpace();
4387 printf(" Free space: %" SAL_PRIuUINT64 "\n", nSize);
4389 if (_nMask == osl_VolumeInfo_Mask_MaxNameLength)
4391 sal_uInt32 nLength = _aVolumeInfo.getMaxNameLength();
4392 printf("max name length: %" SAL_PRIuUINT32 "\n", nLength);
4394 if (_nMask == osl_VolumeInfo_Mask_MaxPathLength)
4396 sal_uInt32 nLength = _aVolumeInfo.getMaxPathLength();
4397 printf("max path length: %" SAL_PRIuUINT32 "\n", nLength);
4399 if (_nMask == osl_VolumeInfo_Mask_FileSystemCaseHandling)
4401 bool bIsCase = _aVolumeInfo.isCaseSensitiveFileSystem();
4402 printf("filesystem case sensitive: %s\n", bIsCase ? "yes" : "no");
4406 void checkVolumeInfo(sal_Int32 _nMask)
4408 VolumeInfo aVolumeInfo(_nMask);
4409 // call getVolumeInfo here
4410 auto nError1 = Directory::getVolumeInfo(aVolURL1, aVolumeInfo);
4411 CPPUNIT_ASSERT_EQUAL_MESSAGE(
4412 "test for getVolumeInfo function: getVolumeInfo of root directory.",
4413 osl::FileBase::E_None, nError1);
4414 // LLA: IMHO it's not a bug, if VolumeInfo is not valid, it's a feature
4415 // LLA: CPPUNIT_ASSERT_MESSAGE("mask is not valid", sal_True == aVolumeInfo.isValid(_nMask));
4416 if (aVolumeInfo.isValid(_nMask))
4417 checkValidMask(aVolumeInfo, _nMask);
4420 void getVolumeInfo_001_1()
4422 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4423 checkVolumeInfo(mask);
4426 void getVolumeInfo_001_2()
4428 sal_Int32 mask = osl_VolumeInfo_Mask_Attributes;
4429 checkVolumeInfo(mask);
4432 void getVolumeInfo_001_3()
4434 sal_Int32 mask = osl_VolumeInfo_Mask_TotalSpace;
4435 checkVolumeInfo(mask);
4438 void getVolumeInfo_001_4()
4440 sal_Int32 mask = osl_VolumeInfo_Mask_UsedSpace;
4441 checkVolumeInfo(mask);
4444 void getVolumeInfo_001_5()
4446 sal_Int32 mask = osl_VolumeInfo_Mask_FreeSpace;
4447 checkVolumeInfo(mask);
4450 void getVolumeInfo_001_6()
4452 sal_Int32 mask = osl_VolumeInfo_Mask_MaxNameLength;
4453 checkVolumeInfo(mask);
4456 void getVolumeInfo_001_7()
4458 sal_Int32 mask = osl_VolumeInfo_Mask_MaxPathLength;
4459 checkVolumeInfo(mask);
4462 void getVolumeInfo_001_8()
4464 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemCaseHandling;
4465 checkVolumeInfo(mask);
4468 void getVolumeInfo_002()
4470 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4471 VolumeInfo aVolumeInfo(mask);
4472 // call getVolumeInfo here
4474 OUString aRootSysURL;
4475 auto nError1 = osl::File::getFileURLFromSystemPath(aRootSys, aRootSysURL);
4476 CPPUNIT_ASSERT_EQUAL_MESSAGE("can't convert root path to file url", osl::FileBase::E_None, nError1);
4478 nError1 = Directory::getVolumeInfo(aRootSys, aVolumeInfo);
4480 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: use system path as parameter.",
4481 osl::FileBase::E_INVAL, nError1);
4484 void getVolumeInfo_003()
4486 // LLA: in Windows, it reply no error, it did not pass in (W32).
4487 #if defined(UNX) && !defined(IOS)
4488 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4489 VolumeInfo aVolumeInfo(mask);
4490 // call getVolumeInfo here
4491 auto nError1 = Directory::getVolumeInfo(aTmpName3, aVolumeInfo);
4493 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: non-existence test. ",
4494 osl::FileBase::E_NOENT, nError1);
4495 #endif
4498 CPPUNIT_TEST_SUITE(getVolumeInfo);
4499 CPPUNIT_TEST(getVolumeInfo_001_1);
4500 CPPUNIT_TEST(getVolumeInfo_001_2);
4501 CPPUNIT_TEST(getVolumeInfo_001_3);
4502 CPPUNIT_TEST(getVolumeInfo_001_4);
4503 CPPUNIT_TEST(getVolumeInfo_001_5);
4504 CPPUNIT_TEST(getVolumeInfo_001_6);
4505 CPPUNIT_TEST(getVolumeInfo_001_7);
4506 CPPUNIT_TEST(getVolumeInfo_001_8);
4507 CPPUNIT_TEST(getVolumeInfo_002);
4508 CPPUNIT_TEST(getVolumeInfo_003);
4509 CPPUNIT_TEST_SUITE_END();
4512 // testing the method
4513 // inline static RC create(const OUString& ustrDirectoryURL)
4515 class create : public CppUnit::TestFixture
4517 public:
4518 void create_001()
4520 // create directory in $TEMP/tmpdir
4521 auto nError1 = Directory::create(aTmpName3);
4522 // check for existence
4523 auto nError2 = Directory::create(aTmpName3);
4524 // remove it
4525 deleteTestDirectory(aTmpName3);
4527 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4528 osl::FileBase::E_None, nError1);
4529 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4530 osl::FileBase::E_EXIST, nError2);
4533 void create_002()
4535 #if !defined(_WIN32) && !defined(MACOSX) && defined(SAL_UNX)
4536 if (geteuid() == 0) // don't test if building as root
4537 return;
4539 OUString aTmpDir;
4540 auto nError1 = osl::FileBase::createTempFile(nullptr, nullptr, &aTmpDir);
4541 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File creation failed", osl::FileBase::E_None, nError1);
4543 nError1 = File::remove(aTmpDir);
4544 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File removal failed", osl::FileBase::E_None, nError1);
4546 nError1 = Directory::create(aTmpDir);
4547 OString sError = "test for create function: create a directory '" +
4548 OUStringToOString(aTmpDir, RTL_TEXTENCODING_ASCII_US) +
4549 "' and check its existence.";
4550 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError1);
4551 osl_setFileAttributes(aTmpDir.pData, 0); // no access allowed now
4553 // Shouldn't be possible now to create a dir underneath it
4554 OUString aTmpSubLevel = aTmpDir + "/notallowedhere";
4555 nError1 = Directory::create(aTmpSubLevel);
4557 // allow removal
4558 osl_setFileAttributes(aTmpDir.pData,
4559 osl_File_Attribute_OwnRead |
4560 osl_File_Attribute_OwnWrite |
4561 osl_File_Attribute_OwnExe);
4562 deleteTestDirectory(aTmpDir);
4563 sError = "test for create function: create a directory under '" +
4564 OUStringToOString(aTmpDir, RTL_TEXTENCODING_ASCII_US) +
4565 "' for access test.";
4566 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_ACCES, nError1);
4567 #endif
4570 void create_003()
4572 // create directory in /tmpname
4573 auto nError1 = Directory::create(aSysPath1);
4575 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory using system path.",
4576 osl::FileBase::E_INVAL, nError1);
4579 CPPUNIT_TEST_SUITE(create);
4580 CPPUNIT_TEST(create_001);
4581 CPPUNIT_TEST(create_002);
4582 CPPUNIT_TEST(create_003);
4583 CPPUNIT_TEST_SUITE_END();
4586 // testing the method
4587 // inline static RC remove(const OUString& ustrDirectoryURL)
4589 class remove : public CppUnit::TestFixture
4591 public:
4592 void remove_001()
4594 // create directory in $TEMP/tmpdir
4595 auto nError1 = Directory::create(aTmpName3);
4596 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4597 // remove it
4598 nError1 = Directory::remove(aTmpName3);
4599 // check for existence
4600 Directory rDirectory(aTmpName3);
4601 auto nError2 = rDirectory.open();
4603 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4604 osl::FileBase::E_None, nError1);
4605 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4606 osl::FileBase::E_NOENT, nError2);
4609 void remove_002()
4611 // create directory in $TEMP/tmpdir
4612 auto nError1 = Directory::create(aTmpName3);
4613 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4614 // try to remove it by system path
4615 nError1 = Directory::remove(aSysPath3);
4616 // check for existence
4617 Directory rDirectory(aTmpName3);
4618 auto nError2 = rDirectory.open();
4620 if (nError2 != osl::FileBase::E_NOENT)
4621 Directory::remove(aTmpName3);
4623 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory by its system path, and check its existence.",
4624 osl::FileBase::E_INVAL, nError1);
4627 void remove_003()
4629 // try to remove a non-existed directory
4630 auto nError1 = Directory::remove(aTmpName6);
4632 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: try to remove a non-existed directory.",
4633 osl::FileBase::E_NOENT, nError1);
4636 void remove_004()
4638 createTestFile(aTmpName6);
4639 bool bExist = ifFileExist(aTmpName6);
4640 // try to remove file.
4641 auto nError1 = Directory::remove(aTmpName6);
4642 deleteTestFile(aTmpName6);
4644 CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4645 bExist);
4646 CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4647 (osl::FileBase::E_NOTDIR == nError1) || (osl::FileBase::E_NOENT == nError1));
4650 void remove_005()
4652 createTestDirectory(aTmpName3);
4653 createTestFile(aTmpName4);
4654 auto nError1 = Directory::remove(aTmpName3);
4655 deleteTestFile(aTmpName4);
4656 deleteTestDirectory(aTmpName3);
4657 OString sError = "test for remove function: try to remove a directory that is not empty." +
4658 errorToStr(nError1);
4659 #if defined(__sun)
4660 // on UNX, the implementation uses rmdir(), which EEXIST is thrown on Solaris when the directory is not empty, refer to: 'man -s 2 rmdir', while on linux, ENOTEMPTY is thrown.
4661 // EEXIST The directory contains entries other than those for "." and "..".
4662 printf("#Solaris test\n");
4663 CPPUNIT_ASSERT_MESSAGE(sError.getStr(), (osl::FileBase::E_EXIST == nError1));
4664 #else
4665 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_NOTEMPTY, nError1);
4666 #endif
4669 CPPUNIT_TEST_SUITE(remove);
4670 CPPUNIT_TEST(remove_001);
4671 CPPUNIT_TEST(remove_002);
4672 CPPUNIT_TEST(remove_003);
4673 CPPUNIT_TEST(remove_004);
4674 CPPUNIT_TEST(remove_005);
4675 CPPUNIT_TEST_SUITE_END();
4678 // TEST Directory::createPath
4680 #ifdef _WIN32
4681 # define PATH_BUFFER_SIZE MAX_PATH
4682 #else
4683 # define PATH_BUFFER_SIZE PATH_MAX
4684 #endif
4686 #define TEST_PATH_POSTFIX "hello/world"
4688 static OUString const & get_test_path()
4690 static OUString test_path = []()
4692 OUString tmp;
4693 osl::FileBase::RC rc = osl::FileBase::getTempDirURL(tmp);
4695 CPPUNIT_ASSERT_EQUAL_MESSAGE
4697 "Getting the location of TMP dir failed",
4698 osl::FileBase::E_None, rc
4701 OUString system_path;
4702 rc = osl::FileBase::getSystemPathFromFileURL(tmp, system_path);
4704 CPPUNIT_ASSERT_EQUAL_MESSAGE
4706 "Cannot convert the TMP dir to system path",
4707 osl::FileBase::E_None, rc
4710 OString tmp_x(OUStringToOString(system_path, RTL_TEXTENCODING_UTF8));
4711 if (tmp_x.lastIndexOf('/') != (tmp_x.getLength() - 1))
4712 tmp_x += "/";
4714 #if !defined(_WIN32) && !defined(ANDROID)
4715 // FIXME would be nice to create unique dir even on Windows
4716 tmp_x += "XXXXXX";
4717 char *out = mkdtemp(const_cast<char*>(tmp_x.getStr()));
4719 CPPUNIT_ASSERT_MESSAGE
4721 "mkdtemp call failed",
4722 out != nullptr
4725 tmp_x += "/";
4726 #endif
4727 tmp_x += TEST_PATH_POSTFIX;
4729 OUString tmpTestPath;
4730 rc = osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x, RTL_TEXTENCODING_UTF8), tmpTestPath);
4732 CPPUNIT_ASSERT_EQUAL_MESSAGE
4734 "Cannot convert the system path back to a URL",
4735 osl::FileBase::E_None, rc
4737 return tmpTestPath;
4738 }();
4739 return test_path;
4742 static void rm_test_path(const OUString& path)
4744 sal_Unicode buffer[PATH_BUFFER_SIZE];
4745 memcpy(buffer, path.getStr(), (path.getLength() + 1) * sizeof(sal_Unicode));
4747 sal_Int32 i = rtl_ustr_lastIndexOfChar(buffer, '/');
4748 if (i == path.getLength())
4749 buffer[i] = 0;
4751 Directory::remove(OUString(buffer));
4753 i = rtl_ustr_lastIndexOfChar(buffer, '/');
4754 assert(i != -1);
4755 if (i != -1)
4757 buffer[i] = 0;
4758 Directory::remove(OUString(buffer));
4762 namespace {
4764 class DirCreatedObserver : public DirectoryCreationObserver
4766 public:
4767 DirCreatedObserver() : i(0) {}
4768 virtual void DirectoryCreated(const OUString&) override { i++; };
4770 int number_of_dirs_created() const { return i; }
4772 private:
4773 int i;
4778 class createPath : public CppUnit::TestFixture
4780 public:
4781 createPath()
4784 void with_relative_path()
4786 osl::FileBase::RC rc = Directory::createPath(u"" TEST_PATH_POSTFIX ""_ustr);
4788 CPPUNIT_ASSERT_EQUAL_MESSAGE
4790 "osl_createDirectoryPath contract broken",
4791 osl::FileBase::E_INVAL, rc
4795 void without_callback()
4797 OUString tp_url = get_test_path();
4799 rm_test_path(tp_url);
4801 osl::FileBase::RC rc = Directory::createPath(tp_url);
4803 rm_test_path(tp_url);
4805 CPPUNIT_ASSERT_EQUAL_MESSAGE
4807 "osl_createDirectoryPath failed",
4808 osl::FileBase::E_None, rc
4812 void with_callback()
4814 OUString tp_url = get_test_path();
4816 rm_test_path(tp_url);
4818 DirCreatedObserver* observer = new DirCreatedObserver;
4819 osl::FileBase::RC rc = Directory::createPath(tp_url, observer);
4820 int nDirs = observer->number_of_dirs_created();
4821 delete observer;
4823 rm_test_path(tp_url);
4825 CPPUNIT_ASSERT_EQUAL_MESSAGE
4827 "osl_createDirectoryPath failed",
4828 osl::FileBase::E_None, rc
4830 CPPUNIT_ASSERT_MESSAGE
4832 "osl_createDirectoryPath failed",
4833 nDirs > 0
4838 #ifdef _WIN32
4840 const char* get_unused_drive_letter()
4842 static const char m_aBuff[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4844 DWORD ld = GetLogicalDrives();
4845 DWORD i = 4;
4846 DWORD j = 2;
4848 while ((ld & i) && (i > 1))
4849 { i = i << 1; j++; }
4851 if (i > 2)
4852 return m_aBuff + j;
4854 return nullptr;
4857 void at_invalid_logical_drive()
4859 const char* drv = get_unused_drive_letter();
4860 char buff[PATH_BUFFER_SIZE];
4861 memset(buff, 0, sizeof(buff));
4863 strncpy(buff, drv, 1);
4864 strcat(buff, ":\\");
4865 strcat(buff, TEST_PATH_POSTFIX);
4867 OUString path = OUString::createFromAscii(buff);
4868 OUString tp_url;
4869 osl::FileBase::getFileURLFromSystemPath(path, tp_url);
4871 osl::FileBase::RC rc = Directory::createPath(tp_url);
4873 CPPUNIT_ASSERT_MESSAGE
4875 "osl_createDirectoryPath doesn't fail on unused logical drive letters",
4876 rc != osl::FileBase::E_None
4879 #endif /* _WIN32 */
4881 CPPUNIT_TEST_SUITE(createPath);
4882 CPPUNIT_TEST(with_relative_path);
4883 CPPUNIT_TEST(without_callback);
4884 CPPUNIT_TEST(with_callback);
4885 #ifdef _WIN32
4886 CPPUNIT_TEST(at_invalid_logical_drive);
4887 #endif
4888 CPPUNIT_TEST_SUITE_END();
4892 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::ctors);
4893 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::open);
4894 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::isOpen);
4895 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::close);
4896 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::reset);
4897 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getNextItem);
4898 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getVolumeInfo);
4899 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::create);
4900 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::remove);
4901 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::createPath);
4904 #if 0
4905 #if defined UNX
4906 /** get Current PID.
4908 OUString getCurrentPID()
4910 //~ Get current PID and turn it into OUString;
4911 int nPID = 0;
4912 #ifdef _WIN32
4913 nPID = GetCurrentProcessId();
4914 #else
4915 nPID = getpid();
4916 #endif
4917 return OUString::number(nPID);
4919 #endif
4920 #endif
4922 namespace {
4924 //~ do some clean up work after all test completed.
4925 class GlobalObject
4927 public:
4928 ~GlobalObject()
4932 //~ special clean up task in Windows and Unix separately;
4933 #if (defined UNX)
4934 //~ some clean up task for UNIX OS
4936 #else
4937 //~ some clean up task for Windows OS
4938 //~ check if some files are in the way, remove them if necessary.
4939 if (ifFileExist(aTmpName6))
4940 deleteTestFile(aTmpName6);
4941 if (ifFileExist(aTmpName4))
4942 deleteTestFile(aTmpName4);
4943 if (checkDirectory(aTmpName4, oslCheckMode::Exist))
4944 deleteTestDirectory(aTmpName4);
4945 if (ifFileExist(aTmpName3))
4946 deleteTestFile(aTmpName3);
4947 if (checkDirectory(aTmpName3, oslCheckMode::Exist))
4948 deleteTestDirectory(aTmpName3);
4950 OUString aUStr(aUserDirectoryURL);
4951 concatURL(aUStr, aHidURL1);
4952 if (ifFileExist(aUStr))
4953 deleteTestFile(aUStr);
4955 OUString aUStr1(aRootURL);
4956 concatURL(aUStr1, aTmpName2);
4957 if (ifFileExist(aUStr1))
4958 deleteTestFile(aUStr1);
4959 #endif
4961 catch (const CppUnit::Exception &e)
4963 printf("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e.what(), e.sourceLine().lineNumber());
4965 catch (...)
4967 printf("Exception caught (...) in GlobalObject dtor()\n");
4974 static GlobalObject theGlobalObject;
4976 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */