1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <rtl/ustrbuf.hxx>
25 #include <osl/thread.h>
26 #include <osl/file.hxx>
27 #include "osl_File_Const.h"
29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/plugin/TestPlugIn.h>
33 #include <tools/urlobj.hxx>
40 #include <o3tl/char16_t2wchar_t.hxx>
45 /** detailed wrong message.
47 static OString
errorToString(const osl::FileBase::RC _nError
)
51 case osl::FileBase::E_None
:
54 case osl::FileBase::E_PERM
:
55 sResult
= "Operation not permitted";
57 case osl::FileBase::E_NOENT
:
58 sResult
= "No such file or directory";
60 case osl::FileBase::E_EXIST
:
61 sResult
= "Already Exist";
63 case osl::FileBase::E_ACCES
:
64 sResult
= "Permission denied";
66 case osl::FileBase::E_INVAL
:
67 sResult
= "The format of the parameters was not valid";
69 case osl::FileBase::E_NOTDIR
:
70 sResult
= "Not a directory";
72 case osl::FileBase::E_ISDIR
:
73 sResult
= "Is a directory";
75 case osl::FileBase::E_BADF
:
78 case osl::FileBase::E_NOTEMPTY
:
79 sResult
= "The directory is not empty";
82 sResult
= "Unknown Error";
88 static OString
errorToStr(osl::FileBase::RC
const& nError
)
90 OString suBuf
= "The returned error is: " +
91 errorToString(nError
) +
96 /** compare two TimeValue, unit is "ms", since Windows time precision is better than UNX.
98 /* FIXME: the above assertion is bogus */
100 #if (defined UNX) // precision of time in Windows is better than UNX
101 # define delta 2000 // time precision, 2000ms
103 # define delta 1800 // time precision, 1.8s
106 static bool t_compareTime(TimeValue
*m_aEndTime
, TimeValue
*m_aStartTime
, sal_Int32 nDelta
)
108 sal_Int32 nDeltaSeconds
= m_aEndTime
->Seconds
- m_aStartTime
->Seconds
;
109 sal_Int32 nDeltaNanoSec
= sal_Int32(m_aEndTime
->Nanosec
) - sal_Int32(m_aStartTime
->Nanosec
);
110 if (nDeltaNanoSec
< 0)
112 nDeltaNanoSec
= 1000000000 + nDeltaNanoSec
;
116 sal_Int32 nDeltaMilliSec
= (nDeltaSeconds
* 1000) + (nDeltaNanoSec
/ 1000000);
117 return (nDeltaMilliSec
< nDelta
);
120 /** compare two OUString file name.
122 static bool compareFileName(const OUString
& ustr1
, const OUString
& ustr2
)
125 // on Windows, the separator is '\', so here change to '/', then compare
127 OUString ustr1new
,ustr2new
;
128 sal_Unicode reverseSlash
= '\\';
130 if (ustr1
.lastIndexOf(reverseSlash
) != -1)
131 ustr1new
= ustr1
.replace(reverseSlash
,'/');
134 if (ustr2
.lastIndexOf(reverseSlash
) != -1)
135 ustr2new
= ustr2
.replace(reverseSlash
,'/');
138 bOk
= ustr1new
.equalsIgnoreAsciiCase(ustr2new
);
140 bOk
= ustr1
.equalsIgnoreAsciiCase(ustr2
);
145 /** simple version to judge if a file name or directory name is a URL or a system path, just to see if it
146 is start with "file:///";.
148 static bool isURL(const OUString
& pathname
)
150 return pathname
.startsWith(aPreURL
);
153 /** concat two part to form a URL or system path, add PATH_SEPARATOR between them if necessary, add "file:///" to beginning if necessary.
155 static void concatURL(OUString
& pathname1
, const OUString
& pathname2
)
157 // check if pathname1 is full qualified URL;
158 if (!isURL(pathname1
))
160 OUString aPathName
= pathname1
.copy(0);
161 osl::FileBase::getFileURLFromSystemPath(pathname1
, aPathName
); // convert if not full qualified URL
162 pathname1
= aPathName
.copy(0);
165 // check if '/' is in the end of pathname1 or at the begin of pathname2;
166 if (!pathname1
.endsWith(aSlashURL
) && !pathname2
.startsWith(aSlashURL
))
167 pathname1
+= aSlashURL
;
168 pathname1
+= pathname2
;
171 /** create a temp test file using OUString name of full qualified URL or system path.
173 static void createTestFile(const OUString
& filename
)
175 OUString aPathURL
= filename
.copy(0);
176 osl::FileBase::RC nError
;
178 if (!isURL(filename
))
179 osl::FileBase::getFileURLFromSystemPath(filename
, aPathURL
); // convert if not full qualified URL
181 File
aFile(aPathURL
);
182 nError
= aFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
183 if ((nError
!= osl::FileBase::E_None
) && (nError
!= osl::FileBase::E_EXIST
))
184 printf("createTestFile failed!\n");
190 /** create a temp test file using OUString name of full qualified URL or system path in a base directory.
192 static void createTestFile(const OUString
& basename
, const OUString
& filename
)
194 OUString aBaseURL
= basename
.copy(0);
196 concatURL(aBaseURL
, filename
);
197 createTestFile(aBaseURL
);
200 /** delete a temp test file using OUString name.
202 static void deleteTestFile(const OUString
& filename
)
204 OUString aPathURL
= filename
.copy(0);
205 osl::FileBase::RC nError
;
207 if (!isURL(filename
))
208 osl::FileBase::getFileURLFromSystemPath(filename
, aPathURL
); // convert if not full qualified URL
210 nError
= File::setAttributes(aPathURL
, osl_File_Attribute_GrpWrite
| osl_File_Attribute_OwnWrite
| osl_File_Attribute_OthWrite
); // if readonly, make writable.
211 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: set writable ", (osl::FileBase::E_None
== nError
) || (osl::FileBase::E_NOENT
== nError
));
213 nError
= File::remove(aPathURL
);
214 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: remove ", (osl::FileBase::E_None
== nError
) || (nError
== osl::FileBase::E_NOENT
));
217 /** delete a temp test file using OUString name of full qualified URL or system path in a base directory.
219 static void deleteTestFile(const OUString
& basename
, const OUString
& filename
)
221 OUString aBaseURL
= basename
.copy(0);
223 concatURL(aBaseURL
, filename
);
224 deleteTestFile(aBaseURL
);
227 /** create a temp test directory using OUString name of full qualified URL or system path.
229 static void createTestDirectory(const OUString
& dirname
)
231 OUString aPathURL
= dirname
.copy(0);
232 osl::FileBase::RC nError
;
235 osl::FileBase::getFileURLFromSystemPath(dirname
, aPathURL
); // convert if not full qualified URL
236 nError
= Directory::create(aPathURL
);
237 if ((nError
!= osl::FileBase::E_None
) && (nError
!= osl::FileBase::E_EXIST
))
238 printf("createTestDirectory failed: %d!\n", int(nError
));
241 /** create a temp test directory using OUString name of full qualified URL or system path in a base directory.
243 static void createTestDirectory(const OUString
& basename
, const OUString
& dirname
)
245 OUString aBaseURL
= basename
.copy(0);
247 concatURL(aBaseURL
, dirname
);
248 createTestDirectory(aBaseURL
);
251 /** delete a temp test directory using OUString name of full qualified URL or system path.
253 static void deleteTestDirectory(const OUString
& dirname
)
255 OUString aPathURL
= dirname
.copy(0);
257 osl::FileBase::getFileURLFromSystemPath(dirname
, aPathURL
); // convert if not full qualified URL
259 Directory
testDir(aPathURL
);
260 if (testDir
.isOpen())
261 testDir
.close(); // close if still open.
263 osl::FileBase::RC nError
= Directory::remove(aPathURL
);
265 OString strError
= "In deleteTestDirectory function: remove Directory " +
266 OUStringToOString(aPathURL
, RTL_TEXTENCODING_ASCII_US
) + " -> result: " + OString::number(nError
);
267 CPPUNIT_ASSERT_MESSAGE(strError
.getStr(), (osl::FileBase::E_None
== nError
) || (nError
== osl::FileBase::E_NOENT
));
270 /** delete a temp test directory using OUString name of full qualified URL or system path in a base directory.
272 static void deleteTestDirectory(const OUString
& basename
, const OUString
& dirname
)
274 OUString aBaseURL
= basename
.copy(0);
276 concatURL(aBaseURL
, dirname
);
277 deleteTestDirectory(aBaseURL
);
282 /** Check for the file and directory access right.
284 enum class oslCheckMode
{
293 /** check if the file exist
295 static bool ifFileExist(const OUString
& str
)
298 return (testFile
.open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
);
301 /** check if the file can be written
303 static bool ifFileCanWrite(const OUString
& str
)
305 // on Windows, the file has no write right, but can be written
307 bool bCheckResult
= false;
308 OUString aUStr
= str
.copy(0);
310 osl::FileBase::getSystemPathFromFileURL(str
, aUStr
);
312 OString aString
= OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
);
313 const char *path
= aString
.getStr();
314 if ((_access(path
, 2)) != -1)
316 // on UNX, just test if open success with osl_File_OpenFlag_Write
319 bool bCheckResult
= (testFile
.open(osl_File_OpenFlag_Write
) == osl::FileBase::E_None
);
324 static bool checkDirectory(const OUString
& str
, oslCheckMode nCheckMode
)
328 osl::FileBase::RC rc
;
329 bool bCheckResult
= false;
334 if ((rc
!= osl::FileBase::E_NOENT
) && (rc
!= osl::FileBase::E_ACCES
))
338 case oslCheckMode::Exist
:
339 if (rc
== ::osl::FileBase::E_None
)
342 case oslCheckMode::OpenAccess
:
343 if (rc
== osl::FileBase::E_None
)
346 case oslCheckMode::ReadAccess
:
347 rc
= aDir
.getNextItem(rItem
);
348 bCheckResult
= (rc
== osl::FileBase::E_None
) || (rc
== osl::FileBase::E_NOENT
);
350 case oslCheckMode::WriteAccess
:
351 ((aUString
+= str
) += aSlashURL
) += aTmpName2
;
352 if (Directory::create(aUString
) == osl::FileBase::E_None
)
355 rc
= Directory::remove(aUString
);
356 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, rc
);
360 bCheckResult
= false;
365 bCheckResult
= false;
369 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, rc
);
375 /** construct error message
377 static OString
outputError(const OString
& returnVal
, const OString
& rightVal
, const char * msg
= "")
379 if (returnVal
== rightVal
)
382 OString aString
= msg
+
383 OString::Concat(": the returned value is '") +
385 "', but the value should be '" +
391 #if (defined UNX) /* chmod() method is different in Windows */
392 /** Change file mode, two version in UNIX and Windows;.
394 static void changeFileMode(OUString
& filepath
, sal_Int32 mode
)
397 OUString aUStr
= filepath
.copy(0);
400 osl::FileBase::getSystemPathFromFileURL(filepath
, aUStr
);
402 aString
= OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
);
403 int ret
= chmod(aString
.getStr(), mode
);
404 CPPUNIT_ASSERT_EQUAL(0, ret
);
407 static void hideFile(const OUString
& filepath
)
409 OUString
aSysPath(filepath
);
412 osl::FileBase::getSystemPathFromFileURL(filepath
, aSysPath
);
414 bool ret
= SetFileAttributesW(o3tl::toW(aSysPath
.getStr()), FILE_ATTRIBUTE_HIDDEN
);
421 static OUString
getCurrentPID();
425 // Beginning of the test cases for osl::FileBase class
427 namespace osl_FileBase
429 // testing the method
430 // static inline RC getAbsoluteFileURL(const OUString& ustrBaseDirectoryURL,
431 // const OUString& ustrRelativeFileURL,
432 // OUString& ustrAbsoluteFileURL)
434 class getAbsoluteFileURL
: public CppUnit::TestFixture
437 void check_getAbsoluteFileURL(OUString
const& _suBaseURL
,
438 OString
const& _sRelativeURL
,
439 osl::FileBase::RC _nAssumeError
,
440 OUString
const& _suAssumeResultStr
);
442 void getAbsoluteFileURL_001_1();
443 void getAbsoluteFileURL_001_2();
444 void getAbsoluteFileURL_001_3();
445 void getAbsoluteFileURL_001_4();
446 void getAbsoluteFileURL_001_5();
447 void getAbsoluteFileURL_001_6();
448 void getAbsoluteFileURL_001_7();
449 void getAbsoluteFileURL_001_8();
450 void getAbsoluteFileURL_002();
451 void getAbsoluteFileURL_003();
452 void getAbsoluteFileURL_004();
454 CPPUNIT_TEST_SUITE(getAbsoluteFileURL
);
455 CPPUNIT_TEST(getAbsoluteFileURL_001_1
);
456 CPPUNIT_TEST(getAbsoluteFileURL_001_2
);
457 CPPUNIT_TEST(getAbsoluteFileURL_001_3
);
458 CPPUNIT_TEST(getAbsoluteFileURL_001_4
);
459 CPPUNIT_TEST(getAbsoluteFileURL_001_5
);
460 CPPUNIT_TEST(getAbsoluteFileURL_001_6
);
461 CPPUNIT_TEST(getAbsoluteFileURL_001_7
);
462 CPPUNIT_TEST(getAbsoluteFileURL_001_8
);
463 CPPUNIT_TEST(getAbsoluteFileURL_002
);
464 CPPUNIT_TEST(getAbsoluteFileURL_003
);
465 CPPUNIT_TEST(getAbsoluteFileURL_004
);
466 CPPUNIT_TEST_SUITE_END();
469 void getAbsoluteFileURL::check_getAbsoluteFileURL(OUString
const& _suBaseURL
,
470 OString
const& _sRelativeURL
,
471 osl::FileBase::RC _nAssumeError
,
472 OUString
const& _suAssumeResultStr
)
474 OUString suRelativeURL
= OStringToOUString(_sRelativeURL
, RTL_TEXTENCODING_UTF8
);
475 OString sBaseURL
= OUStringToOString(_suBaseURL
, RTL_TEXTENCODING_UTF8
);
476 OUString suResultURL
;
477 osl::FileBase::RC nError
= osl::FileBase::getAbsoluteFileURL(_suBaseURL
, suRelativeURL
, suResultURL
);
478 OString sResultURL
= OUStringToOString(suResultURL
, RTL_TEXTENCODING_UTF8
);
479 OString sError
= errorToString(nError
);
480 printf("getAbsoluteFileURL('%s','%s') deliver absolute URL: '%s', error '%s'\n",
481 sBaseURL
.getStr(), _sRelativeURL
.getStr(),sResultURL
.getStr(), sError
.getStr());
482 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: error number is wrong", _nAssumeError
, nError
);
484 if (nError
== osl::FileBase::E_None
)
486 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: ResultURL is not equal to expected URL ", _suAssumeResultStr
, suResultURL
);
490 void getAbsoluteFileURL::getAbsoluteFileURL_001_1()
492 OUString suAssume
= aUserDirectoryURL
+ "/relative/file1";
493 check_getAbsoluteFileURL(aUserDirectoryURL
, "relative/file1",osl::FileBase::E_None
, suAssume
);
496 void getAbsoluteFileURL::getAbsoluteFileURL_001_2()
498 OUString suAssume
= aUserDirectoryURL
+ "/relative/file2";
499 check_getAbsoluteFileURL(aUserDirectoryURL
, "relative/./file2",osl::FileBase::E_None
, suAssume
);
502 void getAbsoluteFileURL::getAbsoluteFileURL_001_3()
504 OUString suAssume
= aUserDirectoryURL
+ "/file3";
505 check_getAbsoluteFileURL(aUserDirectoryURL
, "relative/../file3",osl::FileBase::E_None
, suAssume
);
508 void getAbsoluteFileURL::getAbsoluteFileURL_001_4()
510 OUString suAssume
= aUserDirectoryURL
+ "/file4";
511 check_getAbsoluteFileURL(aUserDirectoryURL
, "././relative/../file4",osl::FileBase::E_None
, suAssume
);
514 void getAbsoluteFileURL::getAbsoluteFileURL_001_5()
517 suAssume
= aUserDirectoryURL
+ "/relative/";
518 check_getAbsoluteFileURL(aUserDirectoryURL
, "././relative/.",osl::FileBase::E_None
, suAssume
);
521 void getAbsoluteFileURL::getAbsoluteFileURL_001_6()
523 OUString suAssume
= aUserDirectoryURL
+ "/.relative";
524 check_getAbsoluteFileURL(aUserDirectoryURL
, "./.relative",osl::FileBase::E_None
, suAssume
);
527 void getAbsoluteFileURL::getAbsoluteFileURL_001_7()
530 suAssume
= aUserDirectoryURL
+ "/.a/";
531 check_getAbsoluteFileURL(aUserDirectoryURL
, "./.a/mydir/..",osl::FileBase::E_None
, suAssume
);
534 void getAbsoluteFileURL::getAbsoluteFileURL_001_8()
536 OUString suAssume
= aUserDirectoryURL
+ "/tmp/ok";
537 check_getAbsoluteFileURL(aUserDirectoryURL
, "tmp//ok",osl::FileBase::E_None
, suAssume
);
540 void getAbsoluteFileURL::getAbsoluteFileURL_002()
543 #if (defined UNX) // Link is not defined in Windows
544 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
545 aUStr_LnkFileSys
+= aSlashURL
+ getCurrentPID() + "/link.file";
546 aUStr_SrcFileSys
+= aSlashURL
+ getCurrentPID() + "/canonical.name";
548 OString strLinkFileName
, strSrcFileName
;
549 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
550 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
552 createTestFile(aCanURL1
);
553 sal_Int32 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
554 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
555 OString sLnkURL
= OUStringToOString(aLnkURL1
, RTL_TEXTENCODING_ASCII_US
);
556 OUString suAssume
= aUserDirectoryURL
+ "/canonical.name";
557 check_getAbsoluteFileURL(aUserDirectoryURL
, sLnkURL
, osl::FileBase::E_None
, suAssume
);
558 deleteTestFile(aCanURL1
);
559 fd
= remove(strLinkFileName
.getStr());
560 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
565 // please see line# 930
566 void getAbsoluteFileURL::getAbsoluteFileURL_003()
570 void getAbsoluteFileURL::getAbsoluteFileURL_004()
572 // create two level directories under $Temp/PID/
573 OUString aUStrUpBase
= aUserDirectoryURL
+ "/test1";
574 createTestDirectory(aUStrUpBase
);
575 OUString aUStrBase
= aUserDirectoryURL
+ "/test1/dir1";
576 createTestDirectory(aUStrBase
);
578 OUString suAssume
= aUserDirectoryURL
+ "/mytestfile";
579 check_getAbsoluteFileURL(aUStrBase
, "../../mytestfile" , osl::FileBase::E_None
, suAssume
);
580 deleteTestDirectory(aUStrBase
);
581 deleteTestDirectory(aUStrUpBase
);
584 // testing two methods:
585 // static inline RC getSystemPathFromFileURL(const OUString& ustrFileURL,
586 // OUString& ustrSystemPath)
587 // static RC getFileURLFromSystemPath(const OUString & ustrSystemPath,
588 // OUString & ustrFileURL);
590 class SystemPath_FileURL
: public CppUnit::TestFixture
593 void getSystemPathFromFileURL_001_1();
594 void getSystemPathFromFileURL_001_2();
595 void getSystemPathFromFileURL_001_21();
596 void getSystemPathFromFileURL_001_22();
597 void getSystemPathFromFileURL_001_3();
598 void getSystemPathFromFileURL_001_31();
599 void getSystemPathFromFileURL_001_4();
600 void getSystemPathFromFileURL_001_41();
601 void getSystemPathFromFileURL_001_5();
602 void getSystemPathFromFileURL_001_51();
603 void getSystemPathFromFileURL_001_52();
604 void getSystemPathFromFileURL_001_53();
605 void getSystemPathFromFileURL_001_6();
606 void getSystemPathFromFileURL_001_61();
607 void getSystemPathFromFileURL_001_7();
608 void getSystemPathFromFileURL_001_71();
609 void getSystemPathFromFileURL_001_8();
610 void getSystemPathFromFileURL_001_81();
611 void getSystemPathFromFileURL_001_9();
612 void getSystemPathFromFileURL_001_91();
613 void getSystemPathFromFileURL_001_92();
614 void getSystemPathFromFileURL_004();
615 void getSystemPathFromFileURL_005();
617 // test case for getFileURLFromSystemPath
618 void getFileURLFromSystemPath_001();
619 void getFileURLFromSystemPath_002();
620 void getFileURLFromSystemPath_003();
621 void getFileURLFromSystemPath_004();
622 void getFileURLFromSystemPath_004_1();
623 void getFileURLFromSystemPath_005();
625 CPPUNIT_TEST_SUITE(SystemPath_FileURL
);
626 CPPUNIT_TEST(getSystemPathFromFileURL_001_1
);
627 CPPUNIT_TEST(getSystemPathFromFileURL_001_2
);
628 CPPUNIT_TEST(getSystemPathFromFileURL_001_21
);
629 CPPUNIT_TEST(getSystemPathFromFileURL_001_22
);
630 CPPUNIT_TEST(getSystemPathFromFileURL_001_3
);
631 CPPUNIT_TEST(getSystemPathFromFileURL_001_31
);
632 CPPUNIT_TEST(getSystemPathFromFileURL_001_4
);
633 CPPUNIT_TEST(getSystemPathFromFileURL_001_41
);
634 CPPUNIT_TEST(getSystemPathFromFileURL_001_5
);
635 CPPUNIT_TEST(getSystemPathFromFileURL_001_51
);
636 CPPUNIT_TEST(getSystemPathFromFileURL_001_52
);
637 CPPUNIT_TEST(getSystemPathFromFileURL_001_53
);
638 CPPUNIT_TEST(getSystemPathFromFileURL_001_6
);
639 CPPUNIT_TEST(getSystemPathFromFileURL_001_61
);
640 CPPUNIT_TEST(getSystemPathFromFileURL_001_7
);
641 CPPUNIT_TEST(getSystemPathFromFileURL_001_71
);
642 CPPUNIT_TEST(getSystemPathFromFileURL_001_8
);
643 CPPUNIT_TEST(getSystemPathFromFileURL_001_81
);
644 CPPUNIT_TEST(getSystemPathFromFileURL_001_9
);
645 CPPUNIT_TEST(getSystemPathFromFileURL_001_91
);
646 CPPUNIT_TEST(getSystemPathFromFileURL_001_92
);
647 CPPUNIT_TEST(getSystemPathFromFileURL_004
);
648 CPPUNIT_TEST(getSystemPathFromFileURL_005
);
649 CPPUNIT_TEST(getFileURLFromSystemPath_001
);
650 CPPUNIT_TEST(getFileURLFromSystemPath_002
);
651 CPPUNIT_TEST(getFileURLFromSystemPath_003
);
652 CPPUNIT_TEST(getFileURLFromSystemPath_004
);
653 CPPUNIT_TEST(getFileURLFromSystemPath_004_1
);
654 CPPUNIT_TEST(getFileURLFromSystemPath_005
);
655 CPPUNIT_TEST_SUITE_END();
658 void check_SystemPath_FileURL(
659 OString
const& _sSource
,
660 osl::FileBase::RC _nAssumeError
,
661 OString
const& _sAssumeResultStr
,
662 bool bDirection
= true);
664 void checkWNTBehaviour_getSystemPathFromFileURL(
665 OString
const& _sURL
,
666 osl::FileBase::RC _nAssumeError
,
667 OString
const& _sWNTAssumeResultString
);
669 void checkUNXBehaviour_getSystemPathFromFileURL(
670 OString
const& _sURL
,
671 osl::FileBase::RC _nAssumeError
,
672 OString
const& _sUnixAssumeResultString
);
674 void checkWNTBehaviour_getFileURLFromSystemPath(OString
const& _sSysPath
,
675 osl::FileBase::RC _nAssumeError
,
676 OString
const& _sWNTAssumeResultString
);
678 void checkUNXBehaviour_getFileURLFromSystemPath(
679 OString
const& _sSysPath
,
680 osl::FileBase::RC _nAssumeError
,
681 OString
const& _sUnixAssumeResultString
);
685 // if bDirection==sal_True, check getSystemPathFromFileURL
686 // if bDirection==sal_False, check getFileURLFromSystemPath
687 void SystemPath_FileURL::check_SystemPath_FileURL(
688 OString
const& _sSource
,
689 osl::FileBase::RC _nAssumeError
,
690 OString
const& _sAssumeResultStr
,
693 // PRE: URL as String
696 suSource
= OStringToOUString(_sSource
, RTL_TEXTENCODING_UTF8
);
697 osl::FileBase::RC nError
;
700 nError
= osl::FileBase::getSystemPathFromFileURL(suSource
, suStr
);
702 nError
= osl::FileBase::getFileURLFromSystemPath(suSource
, suStr
);
704 // if the given string is gt length 0,
705 // we check also this string
706 OString sStr
= OUStringToOString(suStr
, RTL_TEXTENCODING_UTF8
);
707 OString sError
= errorToString(nError
);
710 printf("getSystemPathFromFileURL('%s') deliver system path: '%s', error '%s'\n",
711 _sSource
.getStr(), sStr
.getStr(), sError
.getStr());
713 printf("getFileURLFromSystemPath('%s') deliver File URL: '%s', error '%s'\n",
714 _sSource
.getStr(), sStr
.getStr(), sError
.getStr());
716 if (!_sAssumeResultStr
.isEmpty())
718 bool bStrAreEqual
= _sAssumeResultStr
== sStr
;
719 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong",
720 _nAssumeError
, nError
);
721 CPPUNIT_ASSERT_MESSAGE("Assumption is wrong",
726 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong", _nAssumeError
, nError
);
730 void SystemPath_FileURL::checkWNTBehaviour_getSystemPathFromFileURL(
731 OString
const& _sURL
,
732 osl::FileBase::RC _nAssumeError
,
733 OString
const& _sWNTAssumeResultString
)
736 check_SystemPath_FileURL(_sURL
, _nAssumeError
, _sWNTAssumeResultString
);
740 (void)_sWNTAssumeResultString
;
744 void SystemPath_FileURL::checkUNXBehaviour_getSystemPathFromFileURL(
745 OString
const& _sURL
,
746 osl::FileBase::RC _nAssumeError
,
747 OString
const& _sUnixAssumeResultString
)
750 check_SystemPath_FileURL(_sURL
, _nAssumeError
, _sUnixAssumeResultString
);
754 (void)_sUnixAssumeResultString
;
758 void SystemPath_FileURL::checkWNTBehaviour_getFileURLFromSystemPath(
759 OString
const& _sSysPath
,
760 osl::FileBase::RC _nAssumeError
,
761 OString
const& _sWNTAssumeResultString
)
764 check_SystemPath_FileURL(_sSysPath
, _nAssumeError
, _sWNTAssumeResultString
, false);
768 (void)_sWNTAssumeResultString
;
772 void SystemPath_FileURL::checkUNXBehaviour_getFileURLFromSystemPath(
773 OString
const& _sSysPath
,
774 osl::FileBase::RC _nAssumeError
,
775 OString
const& _sUnixAssumeResultString
)
778 check_SystemPath_FileURL(_sSysPath
, _nAssumeError
, _sUnixAssumeResultString
, false);
782 (void)_sUnixAssumeResultString
;
786 /** Test for getSystemPathFromFileURL()
787 this test is split into 2 different OS tests,
788 the first function checkUNXBehaviour... runs only on Unix based Systems,
789 the second only on windows based systems
790 the first parameter are a file URL where we want to get the system path of,
791 the second parameter is the assumed error of the osl_getSystemPathFromFileURL() function,
792 the third parameter is the assumed result string, the string will only test, if its length is greater than 0
795 void SystemPath_FileURL::getSystemPathFromFileURL_001_1()
798 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
799 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
802 void SystemPath_FileURL::getSystemPathFromFileURL_001_2()
805 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
806 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "\\");
809 void SystemPath_FileURL::getSystemPathFromFileURL_001_21()
811 /* From RFC3986, "2.2. Reserved Characters":
813 "The purpose of reserved characters is to provide a set of delimiting
814 characters that are distinguishable from other data within a URI.
815 URIs that differ in the replacement of a reserved character with its
816 corresponding percent-encoded octet are not equivalent. Percent-
817 encoding a reserved character, or decoding a percent-encoded octet
818 that corresponds to a reserved character, will change how the URI is
819 interpreted by most applications. Thus, characters in the reserved
820 set are protected from normalization and are therefore safe to be
821 used by scheme-specific and producer-specific algorithms for
822 delimiting data subcomponents within a URI."
824 In other words, %2F ("/") is NOT the same as /.
827 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
828 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
831 void SystemPath_FileURL::getSystemPathFromFileURL_001_22()
833 OString
sURL("file:///tmp%2Fmydir");
834 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
835 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
838 void SystemPath_FileURL::getSystemPathFromFileURL_001_3()
841 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "a");
842 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "a");
845 void SystemPath_FileURL::getSystemPathFromFileURL_001_31()
847 OString
sURL("tmpname");
848 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "tmpname");
849 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "tmpname");
852 void SystemPath_FileURL::getSystemPathFromFileURL_001_4()
854 OString
sURL("file://");
855 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "");
856 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
859 void SystemPath_FileURL::getSystemPathFromFileURL_001_41()
861 OString
sURL("file://localhost/tmp");
862 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "");
863 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
866 void SystemPath_FileURL::getSystemPathFromFileURL_001_5()
868 OString
sURL("file:///tmp");
869 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp");
870 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
873 void SystemPath_FileURL::getSystemPathFromFileURL_001_51()
875 OString
sURL("file://c:/tmp");
876 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
877 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
880 void SystemPath_FileURL::getSystemPathFromFileURL_001_52()
882 OString
sURL("file:///c:/tmp");
883 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp");
884 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp");
887 void SystemPath_FileURL::getSystemPathFromFileURL_001_53()
889 OString
sURL("file:///c|/tmp");
890 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c|/tmp");
891 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp");
894 void SystemPath_FileURL::getSystemPathFromFileURL_001_6()
896 OString
sURL("file:///tmp/first");
897 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp/first");
898 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
901 void SystemPath_FileURL::getSystemPathFromFileURL_001_61()
903 OString
sURL("file:///c:/tmp/first");
904 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp/first");
905 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp\\first");
908 void SystemPath_FileURL::getSystemPathFromFileURL_001_7()
910 OString
sURL("file:///tmp/../second");
911 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp/../second");
912 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
915 void SystemPath_FileURL::getSystemPathFromFileURL_001_71()
917 OString
sURL("file:///c:/tmp/../second");
918 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp/../second");
919 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp\\..\\second");
922 void SystemPath_FileURL::getSystemPathFromFileURL_001_8()
924 OString
sURL("../tmp");
925 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "../tmp");
926 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "..\\tmp");
929 void SystemPath_FileURL::getSystemPathFromFileURL_001_81()
932 OString
sURL("file://~/tmp");
934 home_path
= getenv("HOME");
935 OString
expResult(home_path
? home_path
: "");
937 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, expResult
);
941 void SystemPath_FileURL::getSystemPathFromFileURL_001_9()
943 OString
sURL("file:///tmp/first%20second");
944 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp/first second");
945 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
948 void SystemPath_FileURL::getSystemPathFromFileURL_001_91()
950 OString
sURL("file:///c:/tmp/first%20second");
951 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp/first second");
952 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp\\first second");
955 void SystemPath_FileURL::getSystemPathFromFileURL_001_92()
957 OString
sURL("ca@#;+.,$///78no%01ni..name");
958 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
959 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
963 void SystemPath_FileURL::getSystemPathFromFileURL_004()
966 OUString
aUNormalURL(aTmpName6
);
967 OUString
aUResultURL (aSysPath4
);
968 osl::FileBase::RC nError
= osl::FileBase::getSystemPathFromFileURL(aUNormalURL
, aUStr
);
970 bool bOk
= compareFileName(aUStr
, aUResultURL
);
973 "test for getSystemPathFromFileURL(' " +
974 OUStringToOString(aUNormalURL
, RTL_TEXTENCODING_ASCII_US
) +
975 " ') function:use an absolute file URL, " +
976 outputError(OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
),
977 OUStringToOString(aUResultURL
, RTL_TEXTENCODING_ASCII_US
));
979 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_None
, nError
);
980 CPPUNIT_ASSERT_MESSAGE(sError
.getStr(), bOk
);
984 // CJK characters case
985 void SystemPath_FileURL::getSystemPathFromFileURL_005()
988 createTestDirectory(aTmpName10
);
989 OUString
aUNormalURL(aTmpName10
);
990 OUString
aUResultURL (aSysPath5
);
992 osl::FileBase::RC nError
= osl::FileBase::getSystemPathFromFileURL(aUNormalURL
, aUStr
);
994 bool bOk
= compareFileName(aUStr
, aUResultURL
);
997 "test for getSystemPathFromFileURL(' " +
998 OUStringToOString(aUNormalURL
, RTL_TEXTENCODING_ASCII_US
) +
999 " ') function:use a CJK coded absolute URL, " +
1000 outputError(OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
),
1001 OUStringToOString(aUResultURL
, RTL_TEXTENCODING_ASCII_US
));
1002 deleteTestDirectory(aTmpName10
);
1004 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_None
, nError
);
1005 CPPUNIT_ASSERT_MESSAGE(sError
.getStr(), bOk
);
1008 void SystemPath_FileURL::getFileURLFromSystemPath_001()
1010 OString
sSysPath("~/tmp");
1012 home_path
= getenv("HOME");
1013 OString
expResult(home_path
? home_path
: "");
1014 expResult
= "file://"+ expResult
+ "/tmp";
1015 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, expResult
);
1016 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "~/tmp");
1019 void SystemPath_FileURL::getFileURLFromSystemPath_002()
1021 OString
sSysPath("c:/tmp");
1022 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "c:/tmp");
1023 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "file:///c:/tmp");
1026 void SystemPath_FileURL::getFileURLFromSystemPath_003()
1028 OString
sSysPath("file:///temp");
1029 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1030 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1033 void SystemPath_FileURL::getFileURLFromSystemPath_004()
1035 OString
sSysPath("//tmp//first start");
1036 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "file:///tmp/first%20start");
1037 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1040 void SystemPath_FileURL::getFileURLFromSystemPath_004_1()
1042 OString
sSysPath("/tmp///first start");
1043 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "file:///tmp/first%20start");
1044 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1047 void SystemPath_FileURL::getFileURLFromSystemPath_005()
1049 OString
sSysPath("");
1050 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1051 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1054 // testing the method
1055 // static inline RC searchFileURL( const OUString& ustrFileName,
1056 // const OUString& ustrSearchPath,
1057 // OUString& ustrFileURL)
1059 class searchFileURL
: public CppUnit::TestFixture
1065 void searchFileURL_001()
1067 /* search file is passed by system filename */
1068 auto nError1
= osl::FileBase::searchFileURL(aTmpName1
, aUserDirectorySys
, aUStr
);
1069 /* search file is passed by full qualified file URL */
1070 auto nError2
= osl::FileBase::searchFileURL(aCanURL1
, aUserDirectorySys
, aUStr
);
1071 /* search file is passed by relative file path */
1072 auto nError3
= osl::FileBase::searchFileURL(aRelURL4
, aUserDirectorySys
, aUStr
);
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
, nError1
);
1076 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) ",
1077 osl::FileBase::E_NOENT
, nError2
);
1078 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) ",
1079 osl::FileBase::E_NOENT
, nError3
);
1082 void searchFileURL_002()
1085 /* search file is passed by system filename */
1086 OUString strRootSys
= INetURLObject(aTempDirectoryURL
).GetLastName();
1087 auto nError1
= osl::FileBase::searchFileURL(aTempDirectorySys
, strRootSys
, aUStr
);
1088 bool bOk1
= compareFileName(aUStr
, aTempDirectoryURL
);
1089 /* search file is passed by full qualified file URL */
1090 auto nError2
= osl::FileBase::searchFileURL(aTempDirectoryURL
, strRootSys
, aUStr
);
1091 bool bOk2
= compareFileName(aUStr
, aTempDirectoryURL
);
1093 /* search file is passed by relative file path */
1094 auto nError3
= osl::FileBase::searchFileURL(aRelURL5
, strRootSys
, aUStr
);
1095 bool bOk3
= compareFileName(aUStr
, aTempDirectoryURL
);
1097 /* search file is passed by an exist file */
1098 createTestFile(aCanURL1
);
1099 auto nError4
= osl::FileBase::searchFileURL(aCanURL4
, aUserDirectorySys
, aUStr
);
1100 bool bOk4
= compareFileName(aUStr
, aCanURL1
);
1101 deleteTestFile(aCanURL1
);
1103 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
1104 osl::FileBase::E_None
, nError1
);
1105 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
1106 osl::FileBase::E_None
, nError2
);
1108 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
1109 osl::FileBase::E_None
, nError3
);
1111 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1112 osl::FileBase::E_None
, nError4
);
1113 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
1115 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
1118 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
1121 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1126 void searchFileURL_003()
1128 OUString
aSystemPathList(aRootSys
+ PATH_LIST_DELIMITER
+ aTempDirectorySys
+ PATH_LIST_DELIMITER
+ aRootSys
+ "system/path");
1129 auto nError1
= osl::FileBase::searchFileURL(aUserDirectoryURL
, aSystemPathList
, aUStr
);
1130 bool bOk
= compareFileName(aUStr
, aUserDirectoryURL
);
1131 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1132 osl::FileBase::E_None
, nError1
);
1133 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1137 void searchFileURL_004()
1139 OUString
aSystemPathList(aRootSys
+ PATH_LIST_DELIMITER
+ aTempDirectorySys
+ PATH_LIST_DELIMITER
+ aRootSys
+ "system/path/../name");
1140 auto nError1
= osl::FileBase::searchFileURL(aUserDirectoryURL
, aSystemPathList
, aUStr
);
1141 bool bOk
= compareFileName(aUStr
, aUserDirectoryURL
);
1142 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1143 osl::FileBase::E_None
, nError1
);
1144 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1148 void searchFileURL_005()
1150 auto nError1
= osl::FileBase::searchFileURL(aUserDirectoryURL
, aNullURL
, aUStr
);
1151 bool bOk
= compareFileName(aUStr
, aUserDirectoryURL
);
1152 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is NULL",
1153 osl::FileBase::E_None
, nError1
);
1154 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is NULL",
1158 CPPUNIT_TEST_SUITE(searchFileURL
);
1159 CPPUNIT_TEST(searchFileURL_001
);
1160 CPPUNIT_TEST(searchFileURL_002
);
1161 CPPUNIT_TEST(searchFileURL_003
);
1162 CPPUNIT_TEST(searchFileURL_004
);
1163 CPPUNIT_TEST(searchFileURL_005
);
1164 CPPUNIT_TEST_SUITE_END();
1167 // testing the method
1168 // static inline RC getTempDirURL(OUString& ustrTempDirURL)
1170 class getTempDirURL
: public CppUnit::TestFixture
1176 void setUp() override
1178 osl::FileBase::RC nError
= osl::FileBase::getTempDirURL(aUStr
);
1179 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getTempDirURL function: execution",
1180 osl::FileBase::E_None
, nError
);
1183 void getTempDirURL_002()
1185 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1186 checkDirectory(aUStr
, oslCheckMode::OpenAccess
));
1187 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1188 checkDirectory(aUStr
, oslCheckMode::ReadAccess
));
1189 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1190 checkDirectory(aUStr
, oslCheckMode::WriteAccess
));
1193 CPPUNIT_TEST_SUITE(getTempDirURL
);
1194 CPPUNIT_TEST(getTempDirURL_002
);
1195 CPPUNIT_TEST_SUITE_END();
1198 // testing the method
1199 // static inline RC createTempFile(OUString* pustrDirectoryURL,
1200 // oslFileHandle* pHandle,
1201 // OUString* pustrTempFileURL)
1203 class createTempFile
: public CppUnit::TestFixture
1206 std::unique_ptr
<oslFileHandle
> pHandle
;
1207 std::unique_ptr
<OUString
> pUStr_DirURL
;
1208 std::unique_ptr
<OUString
> pUStr_FileURL
;
1211 void setUp() override
1213 pHandle
.reset(new oslFileHandle());
1214 pUStr_DirURL
.reset(new OUString(aUserDirectoryURL
));
1215 pUStr_FileURL
.reset(new OUString());
1218 void tearDown() override
1220 pUStr_DirURL
.reset();
1221 pUStr_FileURL
.reset();
1225 void createTempFile_001()
1227 auto nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), pHandle
.get(), pUStr_FileURL
.get());
1228 File
testFile(*pUStr_FileURL
);
1229 auto nError2
= testFile
.open(osl_File_OpenFlag_Create
);
1231 if (nError2
== osl::FileBase::E_EXIST
)
1233 osl_closeFile(*pHandle
);
1234 deleteTestFile(*pUStr_FileURL
);
1237 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1238 osl::FileBase::E_None
, nError1
);
1239 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: create temp file and test the existence",
1240 (pHandle
!= nullptr));
1241 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1242 osl::FileBase::E_EXIST
, nError2
);
1245 void createTempFile_002()
1248 auto nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), pHandle
.get(), pUStr_FileURL
.get());
1249 File
testFile(*pUStr_FileURL
);
1250 auto nError2
= testFile
.open(osl_File_OpenFlag_Create
);
1252 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1253 osl::FileBase::E_None
, nError1
);
1254 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1255 (pHandle
!= nullptr));
1256 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1257 osl::FileBase::E_EXIST
, nError2
);
1259 // check file if have the write permission
1260 if (nError2
== osl::FileBase::E_EXIST
)
1262 bOK
= ifFileCanWrite(*pUStr_FileURL
);
1263 osl_closeFile(*pHandle
);
1264 deleteTestFile(*pUStr_FileURL
);
1267 CPPUNIT_ASSERT_MESSAGE("test for open and write access rights, in (W32), it did not have write access right, but it should be writable.",
1271 void createTempFile_003()
1273 auto nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), pHandle
.get(), nullptr);
1274 // the temp file will be removed when return from createTempFile
1275 bool bOK
= (pHandle
!= nullptr && nError1
== osl::FileBase::E_None
);
1277 osl_closeFile(*pHandle
);
1279 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1280 osl::FileBase::E_None
, nError1
);
1281 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1285 void createTempFile_004()
1287 auto nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), nullptr, pUStr_FileURL
.get());
1288 bool bOK
= (pUStr_FileURL
!= nullptr);
1289 CPPUNIT_ASSERT(bOK
);
1290 File
testFile(*pUStr_FileURL
);
1291 auto nError2
= testFile
.open(osl_File_OpenFlag_Create
);
1292 deleteTestFile(*pUStr_FileURL
);
1293 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1294 osl::FileBase::E_None
, nError1
);
1295 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1296 osl::FileBase::E_EXIST
, nError2
);
1297 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1302 CPPUNIT_TEST_SUITE(createTempFile
);
1303 CPPUNIT_TEST(createTempFile_001
);
1304 CPPUNIT_TEST(createTempFile_002
);
1305 CPPUNIT_TEST(createTempFile_003
);
1306 CPPUNIT_TEST(createTempFile_004
);
1307 CPPUNIT_TEST_SUITE_END();
1310 // FIXME: remove the _disabled to enable:
1311 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getAbsoluteFileURL
, "osl_osl::FileBase");
1312 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::SystemPath_FileURL
, "osl_osl::FileBase");
1313 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::searchFileURL
, "osl_osl::FileBase");
1314 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getTempDirURL
, "osl_osl::FileBase");
1315 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::createTempFile
, "osl_osl::FileBase");
1317 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_osl::FileBase");
1320 namespace osl_FileStatus
1322 // testing the method
1323 // FileStatus(sal_uInt32 nMask): _nMask(nMask)
1324 class ctors
: public CppUnit::TestFixture
1328 DirectoryItem rItem
;
1331 void setUp() override
1333 // create a tempfile in $TEMP/tmpdir/tmpname.
1334 createTestDirectory(aTmpName3
);
1335 createTestFile(aTmpName4
);
1337 Directory
aDir(aTmpName3
);
1338 auto nError1
= aDir
.open();
1339 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1340 nError1
= aDir
.getNextItem(rItem
);
1341 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1345 void tearDown() override
1347 // remove the tempfile in $TEMP/tmpdir/tmpname.
1348 deleteTestFile(aTmpName4
);
1349 deleteTestDirectory(aTmpName3
);
1354 FileStatus
rFileStatus(osl_FileStatus_Mask_All
);
1355 auto nError1
= rItem
.getFileStatus(rFileStatus
);
1356 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1357 aUStr
= rFileStatus
.getFileName();
1359 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask all and see the file name",
1360 compareFileName(aUStr
, aTmpName2
));
1365 FileStatus
rFileStatus(0);
1366 auto nError1
= rItem
.getFileStatus(rFileStatus
);
1367 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1368 aUStr
= rFileStatus
.getFileName();
1370 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask is empty",
1371 compareFileName(aUStr
, aNullURL
));
1374 CPPUNIT_TEST_SUITE(ctors
);
1375 CPPUNIT_TEST(ctors_001
);
1376 CPPUNIT_TEST(ctors_002
);
1377 CPPUNIT_TEST_SUITE_END();
1380 // testing the method
1381 // inline sal_Bool isValid(sal_uInt32 nMask) const
1383 class isValid
: public CppUnit::TestFixture
1386 std::unique_ptr
<Directory
> pDir
;
1387 DirectoryItem rItem_file
, rItem_link
;
1390 void setUp() override
1392 // create a tempfile in $TEMP/tmpdir/tmpname.
1393 createTestDirectory(aTmpName3
);
1394 createTestFile(aTmpName4
);
1396 pDir
.reset(new Directory(aTmpName3
));
1397 osl::FileBase::RC nError1
= pDir
->open();
1398 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1399 nError1
= pDir
->getNextItem(rItem_file
, 1);
1400 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1403 void tearDown() override
1405 osl::FileBase::RC nError1
= pDir
->close();
1407 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
1409 // remove the tempfile in $TEMP/tmpdir/tmpname.
1410 deleteTestFile(aTmpName4
);
1411 deleteTestDirectory(aTmpName3
);
1416 sal_uInt32 mask
= 0;
1417 FileStatus
rFileStatus(mask
);
1418 osl::FileBase::RC nError1
= rItem_file
.getFileStatus(rFileStatus
);
1419 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1420 bool bOk
= rFileStatus
.isValid(mask
);
1422 CPPUNIT_ASSERT_MESSAGE("test for isValid function: no fields specified", bOk
);
1425 void check_FileStatus(FileStatus
const& _aStatus
)
1428 if (_aStatus
.isValid(osl_FileStatus_Mask_Type
))
1430 if (_aStatus
.isValid(osl_FileStatus_Mask_Attributes
))
1431 sStat
+= "attributes ";
1432 if (_aStatus
.isValid(osl_FileStatus_Mask_CreationTime
))
1434 if (_aStatus
.isValid(osl_FileStatus_Mask_AccessTime
))
1436 if (_aStatus
.isValid(osl_FileStatus_Mask_ModifyTime
))
1438 if (_aStatus
.isValid(osl_FileStatus_Mask_FileSize
))
1439 sStat
+= "filesize ";
1440 if (_aStatus
.isValid(osl_FileStatus_Mask_FileName
))
1441 sStat
+= "filename ";
1442 if (_aStatus
.isValid(osl_FileStatus_Mask_FileURL
))
1443 sStat
+= "fileurl ";
1444 printf("mask: %s\n", sStat
.getStr());
1449 createTestFile(aTmpName6
);
1450 sal_uInt32 mask_file
= osl_FileStatus_Mask_Type
|
1451 osl_FileStatus_Mask_Attributes
|
1452 osl_FileStatus_Mask_CreationTime
|
1453 osl_FileStatus_Mask_AccessTime
|
1454 osl_FileStatus_Mask_ModifyTime
|
1455 osl_FileStatus_Mask_FileSize
|
1456 osl_FileStatus_Mask_FileName
|
1457 osl_FileStatus_Mask_FileURL
;
1459 FileStatus
rFileStatus(mask_file
);
1460 DirectoryItem::get(aTmpName6
, rItem_file
);
1461 osl::FileBase::RC nError1
= rItem_file
.getFileStatus(rFileStatus
);
1463 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
1465 check_FileStatus(rFileStatus
);
1466 deleteTestFile(aTmpName6
);
1470 /** Check if is a valid linked file.
1472 Link is not defined in Windows, and on Linux, we can not get the directory item of the linked file.
1473 We have to defer to filesystems, normal filesystems support links (EXT2, ...), castrated filesystems
1474 don't have links (FAT, FAT32) and Windows NT NTFS support links, but the Windows API doesn't :-(
1482 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
1483 aUStr_LnkFileSys
+= aSlashURL
+ getCurrentPID() + "/tmpdir/link.file";
1484 aUStr_SrcFileSys
+= aSlashURL
+ getCurrentPID() + "/tmpdir/tmpname";
1486 OString strLinkFileName
;
1487 OString strSrcFileName
;
1488 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
1489 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
1491 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
1492 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
1493 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
1495 // testDirectory is "/tmp/PID/tmpdir/"
1496 Directory
testDirectory(aTmpName3
);
1497 testDirectory
.open();
1498 OUString
aFileName ("link.file");
1502 osl::FileBase::RC nError1
= testDirectory
.getNextItem(rItem_link
, 4);
1504 if (nError1
== osl::FileBase::E_None
)
1506 sal_uInt32 mask_link
= osl_FileStatus_Mask_FileName
| osl_FileStatus_Mask_LinkTargetURL
;
1507 FileStatus
rFileStatus(mask_link
);
1508 rItem_link
.getFileStatus(rFileStatus
);
1510 if (compareFileName(rFileStatus
.getFileName(), aFileName
))
1512 if (rFileStatus
.isValid(osl_FileStatus_Mask_LinkTargetURL
))
1525 fd
= remove(strLinkFileName
.getStr());
1526 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
1528 CPPUNIT_ASSERT_MESSAGE("test for isValid function: link file, check for LinkTargetURL", bOk
);
1535 sal_uInt32 mask_file_all
= osl_FileStatus_Mask_All
;
1536 FileStatus
rFileStatus_all(mask_file_all
);
1537 osl::FileBase::RC nError1
= rItem_file
.getFileStatus(rFileStatus_all
);
1538 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1540 check_FileStatus(rFileStatus_all
);
1542 sal_uInt32 mask_file_val
= osl_FileStatus_Mask_Validate
;
1543 FileStatus
rFileStatus_val(mask_file_val
);
1544 nError1
= rItem_file
.getFileStatus(rFileStatus_val
);
1545 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1547 check_FileStatus(rFileStatus_val
);
1550 CPPUNIT_TEST_SUITE(isValid
);
1551 CPPUNIT_TEST(isValid_001
);
1552 CPPUNIT_TEST(isValid_002
);
1553 CPPUNIT_TEST(isValid_003
);
1554 CPPUNIT_TEST(isValid_004
);
1555 CPPUNIT_TEST_SUITE_END();
1558 // testing the method
1559 // inline Type getFileType() const
1561 class getFileType
: public CppUnit::TestFixture
1564 DirectoryItem m_aItem_1
, m_aItem_2
, m_aVolumeItem
, m_aFifoItem
;
1565 DirectoryItem m_aLinkItem
, m_aSocketItem
, m_aSpecialItem
;
1568 void setUp() override
1570 // create a tempfile: $TEMP/tmpdir/tmpname.
1571 // a tempdirectory: $TEMP/tmpdir/tmpdir.
1572 // use $ROOT/staroffice as volume ---> use dev/fd as volume.
1573 // and get their directory item.
1574 createTestDirectory(aTmpName3
);
1575 createTestFile(aTmpName3
, aTmpName2
);
1576 createTestDirectory(aTmpName3
, aTmpName1
);
1578 std::unique_ptr
<Directory
> xDir(new Directory(aTmpName3
));
1579 auto nError1
= xDir
->open();
1580 CPPUNIT_ASSERT_EQUAL_MESSAGE("open aTmpName3 failed!", osl::FileBase::E_None
, nError1
);
1581 // getNextItem can not assure which item retrieved
1582 nError1
= xDir
->getNextItem(m_aItem_1
, 1);
1583 CPPUNIT_ASSERT_EQUAL_MESSAGE("get first item failed!", osl::FileBase::E_None
, nError1
);
1585 nError1
= xDir
->getNextItem(m_aItem_2
);
1586 CPPUNIT_ASSERT_EQUAL_MESSAGE("get second item failed!", osl::FileBase::E_None
, nError1
);
1588 // FIXME mindy: failed on my RH9, so removed temporarily
1589 // nError1 = DirectoryItem::get(aVolURL2, m_aVolumeItem);
1590 // CPPUNIT_ASSERT_MESSAGE("get volume item failed!", osl::FileBase::E_None == nError1);
1593 void tearDown() override
1595 // remove all in $TEMP/tmpdir.
1596 deleteTestDirectory(aTmpName3
, aTmpName1
);
1597 deleteTestFile(aTmpName3
, aTmpName2
);
1598 deleteTestDirectory(aTmpName3
);
1601 void getFileType_001()
1603 FileStatus
rFileStatus(osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileName
);
1604 auto nError1
= m_aItem_1
.getFileStatus(rFileStatus
);
1605 CPPUNIT_ASSERT_EQUAL_MESSAGE("getFileStatus failed", osl::FileBase::E_None
, nError1
);
1607 check_FileType(rFileStatus
);
1610 void check_FileType(osl::FileStatus
const& _rFileStatus
)
1612 if (_rFileStatus
.isValid(osl_FileStatus_Mask_FileName
))
1614 OUString suFilename
= _rFileStatus
.getFileName();
1616 if (_rFileStatus
.isValid(osl_FileStatus_Mask_Type
))
1618 osl::FileStatus::Type eType
= _rFileStatus
.getFileType();
1621 if (compareFileName(suFilename
, aTmpName2
))
1622 bOK
= (eType
== osl::FileStatus::Regular
);
1624 if (compareFileName(suFilename
, aTmpName1
))
1625 bOK
= (eType
== FileStatus::Directory
);
1627 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: ", bOK
);
1630 // LLA: it's not a bug, if a FileStatus not exist, so no else
1633 void getFileType_002()
1635 FileStatus
rFileStatus(osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileName
);
1636 auto nError1
= m_aItem_2
.getFileStatus(rFileStatus
);
1638 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1639 check_FileType(rFileStatus
);
1642 void getFileType_003()
1646 void getFileType_007()
1648 #if defined(__sun) // Special file is different in Windows
1649 auto nError1
= DirectoryItem::get(aTypeURL2
, m_aSpecialItem
);
1650 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1652 // check for File type
1653 FileStatus
rFileStatus(osl_FileStatus_Mask_Type
);
1654 nError1
= m_aSpecialItem
.getFileStatus(rFileStatus
);
1655 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1657 if (rFileStatus
.isValid(osl_FileStatus_Mask_Type
))
1659 osl::FileStatus::Type eType
= rFileStatus
.getFileType();
1661 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: Special, Solaris version ",
1662 (eType
== FileStatus::Special
));
1667 CPPUNIT_TEST_SUITE(getFileType
);
1668 CPPUNIT_TEST(getFileType_001
);
1669 CPPUNIT_TEST(getFileType_002
);
1670 CPPUNIT_TEST(getFileType_003
);
1671 CPPUNIT_TEST(getFileType_007
);
1672 CPPUNIT_TEST_SUITE_END();
1675 // testing the method
1676 // inline sal_uInt64 getAttributes() const
1678 class getAttributes
: public CppUnit::TestFixture
1681 OUString aTypeURL
, aTypeURL_Hid
;
1682 DirectoryItem rItem
, rItem_hidden
;
1685 void setUp() override
1687 aTypeURL
= aUserDirectoryURL
.copy(0);
1688 concatURL(aTypeURL
, aTmpName2
);
1689 createTestFile(aTypeURL
);
1690 osl::FileBase::RC nError
= DirectoryItem::get(aTypeURL
, rItem
);
1691 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1693 aTypeURL_Hid
= aUserDirectoryURL
.copy(0);
1694 concatURL(aTypeURL_Hid
, aHidURL1
);
1695 createTestFile(aTypeURL_Hid
);
1697 hideFile(aTypeURL_Hid
);
1699 nError
= DirectoryItem::get(aTypeURL_Hid
, rItem_hidden
);
1700 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1703 void tearDown() override
1705 deleteTestFile(aTypeURL
);
1706 deleteTestFile(aTypeURL_Hid
);
1710 // windows only has 3 file attributes: normal, readonly and hidden
1711 void getAttributes_001()
1713 changeFileMode(aTypeURL
, S_IRUSR
| S_IRGRP
| S_IROTH
);
1715 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1716 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
1717 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1719 if (geteuid() == 0) // as root, access(W_OK) may be true despite mode
1721 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: (not ReadOnly,) GrpRead, OwnRead, OthRead(UNX version) ",
1722 static_cast<sal_uInt64
>(osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
),
1723 rFileStatus
.getAttributes());
1727 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(UNX version) ",
1728 static_cast<sal_uInt64
>(osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
),
1729 rFileStatus
.getAttributes());
1732 #else // Windows version
1733 void getAttributes_001()
1735 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(Windows version)",
1740 void getAttributes_002()
1743 changeFileMode(aTypeURL
, S_IXUSR
| S_IXGRP
| S_IXOTH
);
1745 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1746 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
1747 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1749 if (geteuid() == 0) // as root, access(W_OK) may be true despite mode
1751 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)",
1752 static_cast<sal_uInt64
>(osl_File_Attribute_Executable
| osl_File_Attribute_GrpExe
| osl_File_Attribute_OwnExe
| osl_File_Attribute_OthExe
),
1753 rFileStatus
.getAttributes());
1757 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)",
1758 static_cast<sal_uInt64
>(osl_File_Attribute_ReadOnly
| osl_File_Attribute_Executable
| osl_File_Attribute_GrpExe
| osl_File_Attribute_OwnExe
| osl_File_Attribute_OthExe
),
1759 rFileStatus
.getAttributes());
1765 void getAttributes_003()
1767 changeFileMode(aTypeURL
, S_IWUSR
| S_IWGRP
| S_IWOTH
);
1769 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1770 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
1771 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1773 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Solaris version)",
1774 static_cast<sal_uInt64
>(osl_File_Attribute_GrpWrite
| osl_File_Attribute_OwnWrite
| osl_File_Attribute_OthWrite
),
1775 rFileStatus
.getAttributes());
1777 #else // Windows version
1778 void getAttributes_003()
1780 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Windows version)",
1785 void getAttributes_004()
1787 sal_Int32 test_Attributes
= osl_File_Attribute_Hidden
;
1788 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1789 osl::FileBase::RC nError
= rItem_hidden
.getFileStatus(rFileStatus
);
1790 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1791 test_Attributes
&= rFileStatus
.getAttributes();
1793 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Hidden files",
1794 static_cast<sal_Int32
>(osl_File_Attribute_Hidden
), test_Attributes
);
1797 CPPUNIT_TEST_SUITE(getAttributes
);
1798 CPPUNIT_TEST(getAttributes_001
);
1799 CPPUNIT_TEST(getAttributes_002
);
1800 CPPUNIT_TEST(getAttributes_003
);
1801 CPPUNIT_TEST(getAttributes_004
);
1802 CPPUNIT_TEST_SUITE_END();
1805 // testing the method
1806 // inline TimeValue getAccessTime() const
1808 class getAccessTime
: public CppUnit::TestFixture
1812 DirectoryItem rItem
;
1815 void setUp() override
1817 aTypeURL
= aUserDirectoryURL
.copy(0);
1818 concatURL(aTypeURL
, aTmpName2
);
1819 createTestFile(aTypeURL
);
1820 osl::FileBase::RC nError
= DirectoryItem::get(aTypeURL
, rItem
);
1821 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1825 void tearDown() override
1827 deleteTestFile(aTypeURL
);
1830 void getAccessTime_001()
1832 TimeValue
*pTV_current
= nullptr;
1833 CPPUNIT_ASSERT((pTV_current
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1834 TimeValue
*pTV_access
= nullptr;
1835 CPPUNIT_ASSERT((pTV_access
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1837 FileStatus
rFileStatus(osl_FileStatus_Mask_AccessTime
);
1838 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
1839 bool bOk
= osl_getSystemTime(pTV_current
);
1840 CPPUNIT_ASSERT(bOk
);
1841 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1843 *pTV_access
= rFileStatus
.getAccessTime();
1845 bool bOK
= t_compareTime(pTV_access
, pTV_current
, delta
);
1849 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. ",
1853 CPPUNIT_TEST_SUITE(getAccessTime
);
1854 CPPUNIT_TEST(getAccessTime_001
);
1855 CPPUNIT_TEST_SUITE_END();
1858 // testing the method
1859 // inline TimeValue getModifyTime() const
1861 class getModifyTime
: public CppUnit::TestFixture
1865 DirectoryItem rItem
;
1868 void getModifyTime_001()
1870 TimeValue
*pTV_current
= nullptr;
1871 CPPUNIT_ASSERT((pTV_current
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1874 aTypeURL
= aUserDirectoryURL
.copy(0);
1875 concatURL(aTypeURL
, aTmpName2
);
1876 createTestFile(aTypeURL
);
1879 bool bOk
= osl_getSystemTime(pTV_current
);
1880 CPPUNIT_ASSERT(bOk
);
1882 // get instance item and filestatus
1883 osl::FileBase::RC nError
= DirectoryItem::get(aTypeURL
, rItem
);
1884 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1885 FileStatus
rFileStatus(osl_FileStatus_Mask_ModifyTime
);
1886 nError
= rItem
.getFileStatus(rFileStatus
);
1887 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1890 TimeValue
* pTV_modify
= nullptr;
1891 CPPUNIT_ASSERT((pTV_modify
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1892 *pTV_modify
= rFileStatus
.getModifyTime();
1894 bool bOK
= t_compareTime(pTV_modify
, pTV_current
, delta
);
1896 deleteTestFile(aTypeURL
);
1900 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. ",
1904 CPPUNIT_TEST_SUITE(getModifyTime
);
1905 CPPUNIT_TEST(getModifyTime_001
);
1906 CPPUNIT_TEST_SUITE_END();
1909 // testing the method
1910 // inline sal_uInt64 getFileSize() const
1912 class getFileSize
: public CppUnit::TestFixture
1916 DirectoryItem rItem
;
1919 void setUp() override
1921 aTypeURL
= aUserDirectoryURL
.copy(0);
1922 concatURL(aTypeURL
, aTmpName2
);
1923 createTestFile(aTypeURL
);
1924 osl::FileBase::RC nError
= DirectoryItem::get(aTypeURL
, rItem
);
1925 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1928 void tearDown() override
1930 deleteTestFile(aTypeURL
);
1933 void getFileSize_001()
1935 FileStatus
rFileStatus(osl_FileStatus_Mask_FileSize
);
1936 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
1937 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1939 sal_uInt64 uFileSize
= rFileStatus
.getFileSize();
1941 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: empty file ",
1942 static_cast<sal_uInt64
>(0), uFileSize
);
1945 void getFileSize_002()
1947 File
testfile(aTypeURL
);
1948 osl::FileBase::RC nError
= testfile
.open(osl_File_OpenFlag_Write
| osl_File_OpenFlag_Read
);
1949 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1950 nError
= testfile
.setSize(TEST_FILE_SIZE
);
1951 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1953 nError
= DirectoryItem::get(aTypeURL
, rItem
);
1954 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1955 FileStatus
rFileStatus(osl_FileStatus_Mask_FileSize
);
1956 nError
= rItem
.getFileStatus(rFileStatus
);
1957 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1958 sal_uInt64 uFileSize
= rFileStatus
.getFileSize();
1960 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: file with size of TEST_FILE_SIZE, did not pass in (W32). ",
1961 static_cast<sal_uInt64
>(TEST_FILE_SIZE
), uFileSize
);
1964 CPPUNIT_TEST_SUITE(getFileSize
);
1965 CPPUNIT_TEST(getFileSize_001
);
1966 CPPUNIT_TEST(getFileSize_002
);
1967 CPPUNIT_TEST_SUITE_END();
1970 // testing the method
1971 // inline OUString getFileName() const
1973 class getFileName
: public CppUnit::TestFixture
1977 DirectoryItem rItem
;
1980 void setUp() override
1982 aTypeURL
= aUserDirectoryURL
.copy(0);
1983 concatURL(aTypeURL
, aTmpName2
);
1984 createTestFile(aTypeURL
);
1985 osl::FileBase::RC nError
= DirectoryItem::get(aTypeURL
, rItem
);
1986 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1989 void tearDown() override
1991 deleteTestFile(aTypeURL
);
1995 void getFileName_001()
1997 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
1998 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
1999 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2001 OUString aFileName
= rFileStatus
.getFileName();
2003 CPPUNIT_ASSERT_MESSAGE("test for getFileName function: name compare with specify",
2004 compareFileName(aFileName
, aTmpName2
));
2007 CPPUNIT_TEST_SUITE(getFileName
);
2008 CPPUNIT_TEST(getFileName_001
);
2009 CPPUNIT_TEST_SUITE_END();
2012 // testing the method
2013 // inline OUString getFileURL() const
2015 class getFileURL
: public CppUnit::TestFixture
2017 DirectoryItem rItem
;
2020 void setUp() override
2022 createTestFile(aTmpName6
);
2023 osl::FileBase::RC nError
= DirectoryItem::get(aTmpName6
, rItem
);
2024 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2027 void tearDown() override
2029 deleteTestFile(aTmpName6
);
2033 void getFileURL_001()
2035 FileStatus
rFileStatus(osl_FileStatus_Mask_FileURL
);
2036 osl::FileBase::RC nError
= rItem
.getFileStatus(rFileStatus
);
2037 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2039 OUString aFileURL
= rFileStatus
.getFileURL();
2041 CPPUNIT_ASSERT_MESSAGE("test for getFileURL function: ",
2042 compareFileName(aFileURL
, aTmpName6
));
2045 CPPUNIT_TEST_SUITE(getFileURL
);
2046 CPPUNIT_TEST(getFileURL_001
);
2047 CPPUNIT_TEST_SUITE_END();
2050 // testing the method
2051 // inline OUString getLinkTargetURL() const
2053 class getLinkTargetURL
: public CppUnit::TestFixture
2057 DirectoryItem rItem
;
2060 void setUp() override
2062 aTypeURL
= aUserDirectoryURL
.copy(0);
2063 concatURL(aTypeURL
, aTmpName2
);
2064 createTestFile(aTypeURL
);
2067 void tearDown() override
2069 deleteTestFile(aTypeURL
);
2072 void getLinkTargetURL_001()
2075 #if (defined UNX) // Link file is not defined in Windows
2076 // create a link file;
2077 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
2078 aUStr_LnkFileSys
+= aSlashURL
+ getCurrentPID() + "/link.file";
2079 aUStr_SrcFileSys
+= aSlashURL
+ getCurrentPID() + "/tmpname";
2081 OString strLinkFileName
, strSrcFileName
;
2082 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
2083 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
2086 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
2087 CPPUNIT_ASSERT_EQUAL_MESSAGE("in creating link file", static_cast<sal_Int32
>(0), fd
);
2089 // get linkTarget URL
2090 auto nError
= DirectoryItem::get(aLnkURL1
, rItem
);
2091 CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file item", osl::FileBase::E_None
, nError
);
2093 FileStatus
rFileStatus(osl_FileStatus_Mask_LinkTargetURL
);
2094 nError
= rItem
.getFileStatus(rFileStatus
);
2095 CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file status", osl::FileBase::E_None
, nError
);
2096 OUString aFileURL
= rFileStatus
.getLinkTargetURL();
2099 fd
= remove(strLinkFileName
.getStr());
2100 CPPUNIT_ASSERT_EQUAL_MESSAGE("in deleting link file", static_cast<sal_Int32
>(0), fd
);
2102 CPPUNIT_ASSERT_MESSAGE("test for getLinkTargetURL function: Solaris version, create a file, and a link file link to it, get its LinkTargetURL and compare",
2103 compareFileName(aFileURL
, aTypeURL
));
2108 CPPUNIT_TEST_SUITE(getLinkTargetURL
);
2109 CPPUNIT_TEST(getLinkTargetURL_001
);
2110 CPPUNIT_TEST_SUITE_END();
2113 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::ctors
, "osl_FileStatus");
2114 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::isValid
, "osl_FileStatus");
2115 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileType
, "osl_FileStatus");
2116 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAttributes
, "osl_FileStatus");
2117 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAccessTime
, "osl_FileStatus");
2118 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getModifyTime
, "osl_FileStatus");
2119 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileSize
, "osl_FileStatus");
2120 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileName
, "osl_FileStatus");
2121 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileURL
, "osl_FileStatus");
2122 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getLinkTargetURL
, "osl_FileStatus");
2124 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_FileStatus");
2130 // testing the method
2131 // File(const OUString& ustrFileURL)
2133 class ctors
: public CppUnit::TestFixture
2136 void setUp() override
2138 // create a tempfile in $TEMP/tmpdir/tmpname.
2139 createTestDirectory(aTmpName3
);
2140 createTestFile(aTmpName4
);
2143 void tearDown() override
2145 // remove the tempfile in $TEMP/tmpdir/tmpname.
2146 deleteTestFile(aTmpName4
);
2147 deleteTestDirectory(aTmpName3
);
2152 File
testFile(aTmpName4
);
2154 osl::FileBase::RC nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2155 osl::FileBase::RC nError2
= testFile
.close();
2156 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2157 osl::FileBase::E_None
, nError1
);
2158 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2159 osl::FileBase::E_None
, nError2
);
2164 File
testFile(aTmpName5
);
2165 char buffer
[30] = "Test for File constructor";
2168 osl::FileBase::RC nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2169 osl::FileBase::RC nError2
= testFile
.write(buffer
, 30, nCount
);
2172 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2173 osl::FileBase::E_None
, nError1
);
2174 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2175 osl::FileBase::E_None
, nError2
);
2178 CPPUNIT_TEST_SUITE(ctors
);
2179 CPPUNIT_TEST(ctors_001
);
2180 CPPUNIT_TEST(ctors_002
);
2181 CPPUNIT_TEST_SUITE_END();
2184 // testing the method
2185 // inline RC open(sal_uInt32 uFlags)
2187 class open
: public CppUnit::TestFixture
2190 void setUp() override
2192 // create a tempfile in $TEMP/tmpdir/tmpname.
2193 createTestDirectory(aTmpName3
);
2194 createTestFile(aTmpName4
);
2197 void tearDown() override
2199 // remove the tempfile in $TEMP/tmpdir/tmpname.
2200 deleteTestFile(aTmpName4
);
2201 deleteTestDirectory(aTmpName3
);
2207 File
testFile(aTmpName4
);
2209 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2210 auto nError2
= testFile
.close();
2211 CPPUNIT_ASSERT_EQUAL_MESSAGE("close error", osl::FileBase::E_None
, nError2
);
2213 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a regular file",
2214 osl::FileBase::E_None
, nError1
);
2219 File
testFile(aTmpName3
);
2221 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
);
2223 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory",
2224 (File::E_INVAL
== nError1
) || (File::E_ACCES
== nError1
));
2229 File
testFile(aCanURL1
);
2231 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2233 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a non-exist file",
2234 File::E_NOENT
, nError1
);
2239 File
testFile(aTmpName4
);
2241 auto nError1
= testFile
.open(osl_File_OpenFlag_Create
);
2243 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: create an exist file",
2244 File::E_EXIST
, nError1
);
2249 File
testFile(aCanURL1
);
2250 char buffer_write
[30] = "Test for File open";
2251 char buffer_read
[30];
2252 sal_uInt64 nCount_write
, nCount_read
;
2254 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
2255 auto nError2
= testFile
.write(buffer_write
, 30, nCount_write
);
2256 osl::FileBase::RC nError4
= testFile
.setPos(osl_Pos_Absolut
, 0);
2257 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError4
);
2258 auto nError3
= testFile
.read(buffer_read
, 10, nCount_read
);
2260 osl::FileBase::RC nError5
= testFile
.close();
2261 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError5
);
2262 osl::FileBase::RC nError6
= osl::File::remove(aCanURL1
);
2263 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError6
);
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
, nError1
);
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 osl::FileBase::E_None
, nError2
);
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 osl::FileBase::E_None
, nError3
);
2271 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2272 sal_uInt64(30), nCount_write
);
2273 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2274 sal_uInt64(10), nCount_read
);
2277 CPPUNIT_TEST_SUITE(open
);
2278 CPPUNIT_TEST(open_001
);
2279 CPPUNIT_TEST(open_002
);
2280 CPPUNIT_TEST(open_003
);
2281 CPPUNIT_TEST(open_005
);
2282 CPPUNIT_TEST(open_006
);
2283 CPPUNIT_TEST_SUITE_END();
2286 // testing the method
2287 // inline RC close()
2289 class close
: public CppUnit::TestFixture
2292 void setUp() override
2294 // create a tempfile in $TEMP/tmpdir/tmpname.
2295 createTestDirectory(aTmpName3
);
2296 createTestFile(aTmpName4
);
2299 void tearDown() override
2301 // remove the tempfile in $TEMP/tmpdir/tmpname.
2302 deleteTestFile(aTmpName4
);
2303 deleteTestDirectory(aTmpName3
);
2309 File
testFile(aTmpName4
);
2311 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2312 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2314 auto nError2
= testFile
.close();
2316 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: close a regular file",
2317 osl::FileBase::E_None
, nError2
);
2322 File
testFile(aTmpName4
);
2324 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2325 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2327 auto nError2
= testFile
.close();
2329 auto nError3
= testFile
.setPos(osl_Pos_Absolut
, 0);
2331 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: manipulate a file after it has been closed",
2332 osl::FileBase::E_None
, nError2
);
2333 CPPUNIT_ASSERT_MESSAGE("test for close function: manipulate a file after it has been closed",
2334 (osl::FileBase::E_None
!= nError3
));
2337 CPPUNIT_TEST_SUITE(close
);
2338 CPPUNIT_TEST(close_001
);
2339 CPPUNIT_TEST(close_002
);
2340 CPPUNIT_TEST_SUITE_END();
2343 // testing the method
2344 // inline RC setPos(sal_uInt32 uHow, sal_Int64 uPos)
2346 class setPos
: public CppUnit::TestFixture
2349 void setUp() override
2351 // create a tempfile in $TEMP/tmpdir/tmpname.
2352 createTestDirectory(aTmpName3
);
2353 createTestFile(aTmpName4
);
2355 // write chars into the file.
2356 File
testFile(aTmpName4
);
2358 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2359 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2360 sal_uInt64 nCount_write
= 0;
2361 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2362 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2363 nError1
= testFile
.close();
2364 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2367 void tearDown() override
2369 // remove the tempfile in $TEMP/tmpdir/tmpname.
2370 deleteTestFile(aTmpName4
);
2371 deleteTestDirectory(aTmpName3
);
2376 File
testFile(aTmpName4
);
2377 char buffer_read
[2];
2379 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2380 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2381 nError1
= testFile
.setPos(osl_Pos_Absolut
, 26);
2382 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2383 sal_uInt64 nCount_read
= 0;
2384 nError1
= testFile
.read(buffer_read
, 1, nCount_read
);
2385 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2386 nError1
= testFile
.close();
2387 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2389 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",
2390 pBuffer_Char
[26], buffer_read
[0]);
2395 File
testFile(aTmpName4
);
2396 char buffer_read
[2];
2398 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2399 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2400 nError1
= testFile
.setPos(osl_Pos_Absolut
, sizeof(pBuffer_Char
) - 2);
2401 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2402 nError1
= testFile
.setPos(osl_Pos_Current
, 0);
2403 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2404 sal_uInt64 nCount_read
= 0;
2405 nError1
= testFile
.read(buffer_read
, 1, nCount_read
);
2406 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2407 nError1
= testFile
.close();
2408 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2410 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",
2411 pBuffer_Char
[sizeof(pBuffer_Char
) - 2], buffer_read
[0]);
2416 File
testFile(aTmpName4
);
2417 char buffer_read
[2];
2419 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2420 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2421 // the file size is smaller than 100
2422 nError1
= testFile
.setPos(osl_Pos_End
, -100);
2423 CPPUNIT_ASSERT_EQUAL_MESSAGE("should return error", osl::FileBase::E_INVAL
, nError1
);
2425 nError1
= testFile
.setPos(osl_Pos_End
, -53);
2426 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2427 sal_uInt64 nCount_read
= 0;
2428 nError1
= testFile
.read(buffer_read
, 1, nCount_read
);
2429 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2430 nError1
= testFile
.close();
2431 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2433 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",
2434 pBuffer_Char
[0], buffer_read
[0]);
2437 CPPUNIT_TEST_SUITE(setPos
);
2438 CPPUNIT_TEST(setPos_001
);
2439 CPPUNIT_TEST(setPos_002
);
2440 CPPUNIT_TEST(setPos_003
);
2441 CPPUNIT_TEST_SUITE_END();
2444 // testing the method
2445 // inline RC getPos(sal_uInt64& uPos)
2447 class getPos
: public CppUnit::TestFixture
2450 void setUp() override
2452 // create a tempfile in $TEMP/tmpdir/tmpname.
2453 createTestDirectory(aTmpName3
);
2454 createTestFile(aTmpName4
);
2456 // write chars into the file.
2457 File
testFile(aTmpName4
);
2459 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2460 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2461 sal_uInt64 nCount_write
= 0;
2462 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2463 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2464 nError1
= testFile
.close();
2465 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2468 void tearDown() override
2470 // remove the tempfile in $TEMP/tmpdir/tmpname.
2471 deleteTestFile(aTmpName4
);
2472 deleteTestDirectory(aTmpName3
);
2478 File
testFile(aTmpName4
);
2479 sal_uInt64 nFilePointer
;
2481 auto nError1
= testFile
.getPos(nFilePointer
);
2482 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL
, nError1
);
2484 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2485 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2487 nError1
= testFile
.setPos(osl_Pos_Absolut
, 26);
2488 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2489 nError1
= testFile
.getPos(nFilePointer
);
2490 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2492 nError1
= testFile
.close();
2493 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2495 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getPos function: set the position to 26, get position and check if it is right",
2496 static_cast<sal_uInt64
>(26), nFilePointer
);
2499 CPPUNIT_TEST_SUITE(getPos
);
2500 CPPUNIT_TEST(getPos_001
);
2501 CPPUNIT_TEST_SUITE_END();
2504 // testing the method
2505 // inline RC isEndOfFile(sal_Bool *pIsEOF)
2507 class isEndOfFile
: public CppUnit::TestFixture
2510 void setUp() override
2512 // create a tempfile in $TEMP/tmpdir/tmpname.
2513 createTestDirectory(aTmpName3
);
2514 createTestFile(aTmpName4
);
2516 // write chars into the file.
2517 File
testFile(aTmpName4
);
2519 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2520 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2521 sal_uInt64 nCount_write
= 0;
2522 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2523 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2524 nError1
= testFile
.close();
2525 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2528 void tearDown() override
2530 // remove the tempfile in $TEMP/tmpdir/tmpname.
2531 deleteTestFile(aTmpName4
);
2532 deleteTestDirectory(aTmpName3
);
2536 void isEndOfFile_001()
2538 File
testFile(aTmpName4
);
2539 sal_Bool bEOF
= false;
2540 sal_Bool
*pEOF
= &bEOF
;
2542 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2543 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2545 nError1
= testFile
.setPos(osl_Pos_End
, 0);
2546 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2547 nError1
= testFile
.isEndOfFile(pEOF
);
2548 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2550 nError1
= testFile
.close();
2551 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2553 CPPUNIT_ASSERT_MESSAGE("test for isEndOfFile function: set the position to end, check if reach end",
2557 void isEndOfFile_002()
2559 File
testFile(aTmpName4
);
2560 sal_Bool bEOF
= false;
2561 sal_Bool
*pEOF
= &bEOF
;
2562 sal_uInt64 nFilePointer
= 0;
2564 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2565 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2567 nError1
= testFile
.setPos(osl_Pos_Absolut
, 0);
2568 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2573 nError1
= testFile
.isEndOfFile(pEOF
);
2574 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2575 nError1
= testFile
.setPos(osl_Pos_Current
, 1);
2576 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2579 nError1
= testFile
.getPos(nFilePointer
);
2580 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2582 nError1
= testFile
.close();
2583 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2585 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isEndOfFile function: use isEndOfFile to move pointer step by step",
2586 static_cast<sal_uInt64
>(sizeof(pBuffer_Char
) + 1), nFilePointer
);
2588 CPPUNIT_TEST_SUITE(isEndOfFile
);
2589 CPPUNIT_TEST(isEndOfFile_001
);
2590 CPPUNIT_TEST(isEndOfFile_002
);
2591 CPPUNIT_TEST_SUITE_END();
2594 // testing the method
2595 // inline RC setSize(sal_uInt64 uSize)
2597 class setSize
: public CppUnit::TestFixture
2600 void setUp() override
2602 // create a tempfile in $TEMP/tmpdir/tmpname.
2603 createTestDirectory(aTmpName3
);
2604 createTestFile(aTmpName4
);
2606 // write chars into the file.
2607 File
testFile(aTmpName4
);
2609 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2610 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2611 sal_uInt64 nCount_write
= 0;
2612 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2613 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2614 nError1
= testFile
.close();
2615 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2618 void tearDown() override
2620 // remove the tempfile in $TEMP/tmpdir/tmpname.
2621 deleteTestFile(aTmpName4
);
2622 deleteTestDirectory(aTmpName3
);
2628 File
testFile(aTmpName4
);
2629 sal_uInt64 nFilePointer
;
2631 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2632 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2634 // enlarge the file to size of 100;
2635 nError1
= testFile
.setSize(100);
2636 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2638 // get the file size;
2639 nError1
= testFile
.setPos(osl_Pos_End
, 0);
2640 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2641 nError1
= testFile
.getPos(nFilePointer
);
2642 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2644 nError1
= testFile
.close();
2645 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2647 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: enlarge the file ",
2648 static_cast<sal_uInt64
>(100), nFilePointer
);
2653 File
testFile(aTmpName4
);
2654 sal_uInt64 nFilePointer
;
2656 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2657 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2659 // enlarge the file to size of 100;
2660 nError1
= testFile
.setSize(10);
2661 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2663 // get the file size;
2664 nError1
= testFile
.setPos(osl_Pos_End
, 0);
2665 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2666 nError1
= testFile
.getPos(nFilePointer
);
2667 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2669 nError1
= testFile
.close();
2670 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2672 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: truncate the file ",
2673 static_cast<sal_uInt64
>(10), nFilePointer
);
2676 CPPUNIT_TEST_SUITE(setSize
);
2677 CPPUNIT_TEST(setSize_001
);
2678 CPPUNIT_TEST(setSize_002
);
2679 CPPUNIT_TEST_SUITE_END();
2682 // testing the method
2683 // inline RC read(void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead)
2685 class read
: public CppUnit::TestFixture
2688 void setUp() override
2690 // create a tempfile in $TEMP/tmpdir/tmpname.
2691 createTestDirectory(aTmpName3
);
2692 createTestFile(aTmpName4
);
2694 // write chars into the file.
2695 File
testFile(aTmpName4
);
2697 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2698 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2699 sal_uInt64 nCount_write
= 0;
2700 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2701 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2702 nError1
= testFile
.close();
2703 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2706 void tearDown() override
2708 // remove the tempfile in $TEMP/tmpdir/tmpname.
2709 deleteTestFile(aTmpName4
);
2710 deleteTestDirectory(aTmpName3
);
2716 File
testFile(aTmpName4
);
2717 sal_uInt64 nFilePointer
;
2718 char buffer_read
[10];
2720 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2721 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2722 sal_uInt64 nCount_read
= 0;
2723 nError1
= testFile
.read(buffer_read
, 10, nCount_read
);
2724 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2725 nError1
= testFile
.getPos(nFilePointer
);
2726 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2728 nError1
= testFile
.close();
2729 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2731 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2732 sal_uInt64(10), nFilePointer
);
2733 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2734 0, strncmp(buffer_read
, pBuffer_Char
, 10));
2739 File
testFile(aTmpName4
);
2740 sal_uInt64 nFilePointer
;
2741 char buffer_read
[26];
2743 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2744 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2746 nError1
= testFile
.setPos(osl_Pos_Absolut
, 26);
2747 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2748 sal_uInt64 nCount_read
= 0;
2749 nError1
= testFile
.read(buffer_read
, 26, nCount_read
);
2750 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2751 nError1
= testFile
.getPos(nFilePointer
);
2752 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2754 nError1
= testFile
.close();
2755 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2757 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2758 sal_uInt64(52), nFilePointer
);
2759 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2760 sal_uInt64(26), nCount_read
);
2761 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2762 0, strncmp(buffer_read
, &pBuffer_Char
[26], 26));
2765 CPPUNIT_TEST_SUITE(read
);
2766 CPPUNIT_TEST(read_001
);
2767 CPPUNIT_TEST(read_002
);
2768 CPPUNIT_TEST_SUITE_END();
2771 // testing the method
2772 // inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
2774 class write
: public CppUnit::TestFixture
2777 void setUp() override
2779 // create a tempfile in $TEMP/tmpname.
2780 createTestFile(aTmpName6
);
2783 void tearDown() override
2785 // remove the tempfile in $TEMP/tmpname.
2786 deleteTestFile(aTmpName6
);
2792 File
testFile(aTmpName6
);
2793 sal_uInt64 nFilePointer
;
2794 char buffer_read
[10];
2796 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2797 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2799 sal_uInt64 nCount_write
= 0, nCount_read
= 0;
2800 // write chars into the file.
2801 nError1
= testFile
.write(pBuffer_Char
, 10, nCount_write
);
2802 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2803 // get the current pointer;
2804 nError1
= testFile
.getPos(nFilePointer
);
2805 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2806 // reset pointer to the beginning;
2807 nError1
= testFile
.setPos(osl_Pos_Absolut
, 0);
2808 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2809 nError1
= testFile
.read(buffer_read
, 10, nCount_read
);
2810 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2812 nError1
= testFile
.close();
2813 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
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), nFilePointer
);
2817 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",
2818 0, strncmp(buffer_read
, pBuffer_Char
, 10));
2819 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",
2820 sal_uInt64(10), nCount_write
);
2823 CPPUNIT_TEST_SUITE(write
);
2824 CPPUNIT_TEST(write_001
);
2825 CPPUNIT_TEST_SUITE_END();
2828 // testing the method
2829 // inline RC readLine(::ByteSequence& aSeq)
2831 class readLine
: public CppUnit::TestFixture
2833 rtl::ByteSequence aSequence
;
2836 void setUp() override
2838 // create a tempfile in $TEMP/tmpname.
2839 createTestFile(aTmpName6
);
2841 // write some strings into the file.
2842 File
testFile(aTmpName6
);
2843 char ppStrSeq
[3][27] = { "abcde\n",
2845 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2848 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2849 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2851 sal_uInt64 nCount_write
= 0;
2852 for (int nCount
= 0; nCount
< 3; nCount
++)
2854 nError1
= testFile
.write(ppStrSeq
[nCount
], strlen(ppStrSeq
[nCount
]), nCount_write
);
2855 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2858 nError1
= testFile
.close();
2859 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2862 void tearDown() override
2864 // remove the tempfile in $TEMP/tmpname.
2865 deleteTestFile(aTmpName6
);
2871 File
testFile(aTmpName6
);
2873 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2874 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2875 nError1
= testFile
.readLine(aSequence
);
2876 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2877 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
2878 osl::FileBase::E_None
, nError1
);
2879 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
2880 0, strncmp(reinterpret_cast<char *>(aSequence
.getArray()), pBuffer_Char
, 5));
2885 File
testFile(aTmpName6
);
2886 sal_Bool bEOF
= false;
2887 sal_Bool
*pEOF
= &bEOF
;
2889 auto nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2890 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2891 for (int nCount
= 0; nCount
< 3; nCount
++)
2893 nError1
= testFile
.readLine(aSequence
);
2894 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2896 nError1
= testFile
.isEndOfFile(pEOF
);
2897 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2899 CPPUNIT_ASSERT_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
2901 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
2902 0, strncmp(reinterpret_cast<char *>(aSequence
.getArray()), &pBuffer_Char
[26], 26));
2904 CPPUNIT_TEST_SUITE(readLine
);
2905 CPPUNIT_TEST(readLine_001
);
2906 CPPUNIT_TEST(readLine_002
);
2907 CPPUNIT_TEST_SUITE_END();
2910 // testing the method
2911 // inline static RC copy(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
2913 class copy
: public CppUnit::TestFixture
2916 void setUp() override
2918 // create a tempfile in $TEMP/tmpdir/tmpname.
2919 createTestDirectory(aTmpName3
);
2920 createTestFile(aTmpName4
);
2922 // write chars into the file.
2923 File
testFile(aTmpName4
);
2925 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2926 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2927 sal_uInt64 nCount_write
= 0;
2928 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2929 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2930 nError1
= testFile
.close();
2931 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2934 void tearDown() override
2936 // remove the tempfile in $TEMP/tmpdir/tmpname.
2937 deleteTestFile(aTmpName4
);
2938 deleteTestDirectory(aTmpName3
);
2943 File
testFile(aTmpName6
);
2945 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpname.
2946 auto nError1
= File::copy(aTmpName4
, aTmpName6
);
2947 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2949 nError1
= testFile
.open(osl_File_OpenFlag_Create
);
2950 deleteTestFile(aTmpName6
);
2952 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy file to upper directory",
2953 osl::FileBase::E_EXIST
, nError1
);
2958 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
2959 auto nError1
= File::copy(aTmpName4
, aTmpName3
);
2961 CPPUNIT_ASSERT_MESSAGE("test for copy function: use directory as destination",
2962 (osl::FileBase::E_ISDIR
== nError1
) ||(osl::FileBase::E_ACCES
== nError1
));
2968 // copy $TEMP/tmpdir/tmpname to $ROOT/tmpname.
2969 auto nError1
= File::copy(aTmpName4
, aTmpName7
);
2970 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy to an illegal place",
2971 osl::FileBase::E_ACCES
, nError1
);
2977 // copy $TEMP/tmpname to $TEMP/tmpdir/tmpname.
2978 auto nError1
= File::copy(aTmpName6
, aTmpName4
);
2980 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a not exist file",
2981 osl::FileBase::E_NOENT
, nError1
);
2986 // copy $TEMP/tmpname to $TEMP/system.path using system path.
2987 auto nError1
= File::copy(aTmpName6
, aSysPath1
);
2989 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a file using system file path",
2990 osl::FileBase::E_INVAL
, nError1
);
2995 createTestFile(aTmpName6
);
2996 File
tmpFile(aTmpName6
);
2997 tmpFile
.open(osl_File_OpenFlag_Write
| osl_File_OpenFlag_Read
);
2998 tmpFile
.setSize(200);
3001 auto nError1
= File::copy(aTmpName6
, aTmpName4
);
3002 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3004 // check if is the new file
3005 File
newFile(aTmpName4
);
3006 newFile
.open(osl_File_OpenFlag_Write
| osl_File_OpenFlag_Read
);
3007 nError1
= newFile
.setPos(osl_Pos_End
, 0);
3008 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3010 sal_uInt64 nFilePointer
;
3011 nError1
= newFile
.getPos(nFilePointer
);
3012 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3014 deleteTestFile(aTmpName6
);
3015 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: the dest file exist",
3016 static_cast<sal_uInt64
>(200), nFilePointer
);
3019 CPPUNIT_TEST_SUITE(copy
);
3020 CPPUNIT_TEST(copy_001
);
3021 CPPUNIT_TEST(copy_002
);
3022 CPPUNIT_TEST(copy_003
);
3023 CPPUNIT_TEST(copy_004
);
3024 CPPUNIT_TEST(copy_005
);
3025 CPPUNIT_TEST(copy_006
);
3026 CPPUNIT_TEST_SUITE_END();
3029 // testing the method
3030 // inline static RC move(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
3032 class move
: public CppUnit::TestFixture
3035 void setUp() override
3037 // create a tempfile in $TEMP/tmpdir/tmpname.
3038 createTestDirectory(aTmpName3
);
3039 createTestFile(aTmpName4
);
3041 // write chars into the file.
3042 File
testFile(aTmpName4
);
3044 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
3045 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3046 sal_uInt64 nCount_write
= 0;
3047 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
3048 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3049 nError1
= testFile
.close();
3050 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3053 void tearDown() override
3055 // remove the tempfile in $TEMP/tmpdir/tmpname.
3056 deleteTestFile(aTmpName4
);
3057 deleteTestDirectory(aTmpName3
);
3063 // rename $TEMP/tmpdir/tmpname to $TEMP/canonical.name.
3064 auto nError1
= File::move(aTmpName4
, aCanURL1
);
3065 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3067 File
testFile(aCanURL1
);
3068 auto nError2
= testFile
.open(osl_File_OpenFlag_Create
);
3069 deleteTestFile(aCanURL1
);
3071 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: rename file to another directory",
3072 osl::FileBase::E_EXIST
, nError2
);
3078 // move $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
3079 auto nError1
= File::move(aTmpName4
, aTmpName3
);
3080 // returned osl::FileBase::E_ACCES on WNT
3081 CPPUNIT_ASSERT_MESSAGE("test for move function: use directory as destination",
3082 (osl::FileBase::E_ACCES
== nError1
|| osl::FileBase::E_ISDIR
== nError1
) ||(osl::FileBase::E_EXIST
== nError1
));
3089 // move $TEMP/tmpdir/tmpname to $ROOT/tmpname.
3090 auto nError1
= File::move(aTmpName4
, aTmpName7
);
3091 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move to an illegal place",
3092 osl::FileBase::E_ACCES
, nError1
);
3098 // move $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3099 auto nError1
= File::move(aTmpName6
, aTmpName4
);
3101 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a not exist file",
3102 osl::FileBase::E_NOENT
, nError1
);
3107 // move $TEMP/tmpname to $TEMP/system.path using system path.
3108 auto nError1
= File::move(aTmpName6
, aSysPath1
);
3110 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a file using system file",
3111 osl::FileBase::E_INVAL
, nError1
);
3116 // move directory $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3117 createTestDirectory(aTmpName6
);
3118 auto nError1
= File::move(aTmpName6
, aTmpName4
);
3119 // move file $TEMP/tmpdir/tmpname to $TEMP/tmpname
3120 auto nError2
= File::move(aTmpName4
, aTmpName6
);
3121 deleteTestDirectory(aTmpName6
);
3123 deleteTestDirectory(aTmpName4
);// in Windows, it can be moved!!!!! this is only for not influence the following test.
3124 deleteTestFile(aTmpName6
);
3125 nError1
= osl::FileBase::E_NOTDIR
;
3126 nError2
= osl::FileBase::E_ISDIR
;
3128 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3129 osl::FileBase::E_NOTDIR
, nError1
);
3130 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3131 osl::FileBase::E_ISDIR
, nError2
);
3136 // create directory $TEMP/tmpname.
3137 createTestDirectory(aTmpName6
);
3138 // move directory $TEMP/tmpdir to $TEMP/tmpname/tmpdir
3139 auto nError1
= File::move(aTmpName3
, aTmpName8
);
3141 auto nError2
= Directory::create(aTmpName8
);
3142 File::move(aTmpName8
, aTmpName3
);
3143 deleteTestDirectory(aTmpName6
);
3145 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3146 osl::FileBase::E_None
, nError1
);
3147 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3148 osl::FileBase::E_EXIST
, nError2
);
3151 // bugid# 115420, after the bug fix, add the case
3152 CPPUNIT_TEST_SUITE(move
);
3153 CPPUNIT_TEST(move_001
);
3154 CPPUNIT_TEST(move_002
);
3155 CPPUNIT_TEST(move_003
);
3156 CPPUNIT_TEST(move_004
);
3157 CPPUNIT_TEST(move_005
);
3158 CPPUNIT_TEST(move_006
);
3159 CPPUNIT_TEST(move_007
);
3160 CPPUNIT_TEST_SUITE_END();
3163 // testing the method
3164 // inline static RC remove(const OUString& ustrFileURL)
3166 class remove
: public CppUnit::TestFixture
3169 void setUp() override
3171 // create a tempfile in $TEMP/tmpdir/tmpname.
3172 createTestDirectory(aTmpName3
);
3173 createTestFile(aTmpName4
);
3175 // write chars into the file.
3176 File
testFile(aTmpName4
);
3178 auto nError1
= testFile
.open(osl_File_OpenFlag_Write
);
3179 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3180 sal_uInt64 nCount_write
= 0;
3181 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
3182 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3183 nError1
= testFile
.close();
3184 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3187 void tearDown() override
3189 // remove the tempfile in $TEMP/tmpdir/tmpname.
3190 deleteTestFile(aTmpName4
);
3191 deleteTestDirectory(aTmpName3
);
3197 // remove $TEMP/tmpdir/tmpname.
3198 auto nError1
= File::remove(aTmpName4
);
3200 File
testFile(aTmpName4
);
3201 auto nError2
= testFile
.open(osl_File_OpenFlag_Create
);
3203 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file",
3204 osl::FileBase::E_None
, nError1
);
3205 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a file",
3206 (osl::FileBase::E_EXIST
!= nError2
));
3211 // remove $TEMP/tmpname.
3212 auto nError1
= File::remove(aTmpName6
);
3214 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file not exist",
3215 osl::FileBase::E_NOENT
, nError1
);
3220 // remove $TEMP/system/path.
3221 auto nError1
= File::remove(aSysPath2
);
3223 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: removing a file not using full qualified URL",
3224 osl::FileBase::E_INVAL
, nError1
);
3229 // remove $TEMP/tmpdir.
3230 auto nError1
= File::remove(aTmpName3
);
3232 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a directory",
3233 (osl::FileBase::E_ISDIR
== nError1
) || (osl::FileBase::E_ACCES
== nError1
));
3236 CPPUNIT_TEST_SUITE(remove
);
3237 CPPUNIT_TEST(remove_001
);
3238 CPPUNIT_TEST(remove_002
);
3239 CPPUNIT_TEST(remove_003
);
3240 CPPUNIT_TEST(remove_004
);
3241 CPPUNIT_TEST_SUITE_END();
3244 // testing the method
3245 // inline static RC setAttributes(const OUString& ustrFileURL, sal_uInt64 uAttributes)
3247 class setAttributes
: public CppUnit::TestFixture
3250 DirectoryItem rItem
;
3253 void setUp() override
3255 // create a tempfile in $TEMP/tmpdir/tmpname.
3256 createTestFile(aTmpName6
);
3259 void tearDown() override
3261 // remove the tempfile in $TEMP/tmpdir/tmpname.
3262 deleteTestFile(aTmpName6
);
3266 void setAttributes_001()
3268 // on windows, only can set 2 attributes: osl_File_Attribute_ReadOnly, osl_File_Attribute_Hidden
3270 // set the file to readonly
3271 auto nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
);
3272 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3273 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3274 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3275 // get the file attributes
3276 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
3277 nError1
= rItem
.getFileStatus(rFileStatus
);
3278 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3280 if (geteuid() == 0) // as root, access(W_OK) may be true despite mode
3282 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3283 static_cast<sal_uInt64
>(osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
),
3284 rFileStatus
.getAttributes());
3288 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3289 static_cast<sal_uInt64
>(osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
),
3290 rFileStatus
.getAttributes());
3293 // please see GetFileAttributes
3294 auto nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_ReadOnly
);
3295 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3296 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3297 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3298 // get the file attributes
3299 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
3300 nError1
= rItem
.getFileStatus(rFileStatus
);
3301 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3302 // here the file has 2 Attributes: FILE_ATTRIBUTE_READONLY and FILE_ATTRIBUTE_NORMAL,
3303 // but FILE_ATTRIBUTE_NORMAL is valid only if used alone, so this is maybe a bug
3304 /*OString aString = OUStringToOString(aTmpName6, RTL_TEXTENCODING_ASCII_US);
3305 DWORD dwFileAttributes = GetFileAttributes(aString.getStr());
3306 if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
3307 printf("has normal attribute");
3308 if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
3309 printf("has readonly attribute");
3311 CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes READONLY and get it to verify.",
3312 (osl_File_Attribute_ReadOnly
& rFileStatus
.getAttributes()) != 0);
3315 void setAttributes_002()
3317 // on UNX, can not set hidden attribute to file, rename file can set the attribute
3319 // set the file to hidden
3320 auto nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_Hidden
);
3322 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3323 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3324 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3325 // get the file attributes
3326 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
3327 nError1
= rItem
.getFileStatus(rFileStatus
);
3328 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3330 CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3331 (osl_File_Attribute_Hidden
& rFileStatus
.getAttributes()) != 0);
3335 CPPUNIT_TEST_SUITE(setAttributes
);
3336 CPPUNIT_TEST(setAttributes_001
);
3337 CPPUNIT_TEST(setAttributes_002
);
3338 CPPUNIT_TEST_SUITE_END();
3341 // testing the method
3342 // inline static RC setTime(
3343 // const OUString& ustrFileURL,
3344 // const TimeValue& rCreationTime,
3345 // const TimeValue& rLastAccessTime,
3346 // const TimeValue& rLastWriteTime)
3348 class setTime
: public CppUnit::TestFixture
3351 DirectoryItem rItem
;
3354 void setUp() override
3356 // create a tempfile in $TEMP/tmpdir/tmpname.
3357 createTestFile(aTmpName6
);
3360 void tearDown() override
3362 // remove the tempfile in $TEMP/tmpdir/tmpname.
3363 deleteTestFile(aTmpName6
);
3369 TimeValue
*pTV_current
= nullptr;
3370 CPPUNIT_ASSERT((pTV_current
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3371 TimeValue
*pTV_creation
= nullptr;
3372 CPPUNIT_ASSERT((pTV_creation
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3373 TimeValue
*pTV_access
= nullptr;
3374 CPPUNIT_ASSERT((pTV_access
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3375 TimeValue
*pTV_modify
= nullptr;
3376 CPPUNIT_ASSERT((pTV_modify
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3379 bool bOk
= osl_getSystemTime(pTV_current
);
3380 CPPUNIT_ASSERT(bOk
);
3382 // set the file time
3383 auto nError2
= File::setTime(aTmpName6
, *pTV_current
, *pTV_current
, *pTV_current
);
3384 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError2
).getStr(), osl::FileBase::E_None
, nError2
);
3386 // get the file access time, creation time, modify time
3387 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3388 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3390 FileStatus
rFileStatus(osl_FileStatus_Mask_AccessTime
);
3391 nError1
= rItem
.getFileStatus(rFileStatus
);
3392 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3393 *pTV_access
= rFileStatus
.getAccessTime();
3395 FileStatus
rFileStatus1(osl_FileStatus_Mask_CreationTime
);
3396 nError1
= rItem
.getFileStatus(rFileStatus1
);
3397 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3398 *pTV_creation
= rFileStatus1
.getCreationTime();
3400 FileStatus
rFileStatus2(osl_FileStatus_Mask_ModifyTime
);
3401 nError1
= rItem
.getFileStatus(rFileStatus2
);
3402 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3403 *pTV_modify
= rFileStatus2
.getModifyTime();
3405 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.",
3406 t_compareTime(pTV_access
, pTV_current
, delta
));
3408 // Unfortunately there is no way to get the creation time of a file under Unix (it's a Windows only feature).
3409 // That means the flag osl_FileStatus_Mask_CreationTime should be deprecated under Unix.
3410 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set creation time then get it. ",
3411 t_compareTime(pTV_creation
, pTV_current
, delta
));
3413 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set modify time then get it. ",
3414 t_compareTime(pTV_modify
, pTV_current
, delta
));
3421 CPPUNIT_TEST_SUITE(setTime
);
3422 CPPUNIT_TEST(setTime_001
);
3423 CPPUNIT_TEST_SUITE_END();
3426 // testing the method
3427 // inline static RC sync()
3429 class sync
: public CppUnit::TestFixture
3432 DirectoryItem rItem
;
3435 void setUp() override
3437 // create a tempfile in $TEMP/tmpdir/tmpname.
3438 createTestFile(aTmpName6
);
3442 void tearDown() override
3444 // remove the tempfile in $TEMP/tmpdir/tmpname.
3445 deleteTestFile(aTmpName6
);
3448 // test case: if The file is located on a read only file system.
3451 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3452 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3454 File
tmp_file(aTmpName6
);
3455 osl::FileBase::RC err
= tmp_file
.open(osl_File_OpenFlag_Write
);
3457 CPPUNIT_ASSERT_EQUAL_MESSAGE("File open failed", osl::FileBase::E_None
, err
);
3460 sal_uInt64 written
= 0;
3461 nError1
= tmp_file
.write(static_cast<void*>(buffer
), sizeof(buffer
), written
);
3462 CPPUNIT_ASSERT_EQUAL_MESSAGE("write failed!", osl::FileBase::E_None
, nError1
);
3464 // set the file to readonly
3465 auto nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
);
3466 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3468 nError2
= tmp_file
.sync();
3470 CPPUNIT_ASSERT_EQUAL_MESSAGE("can not sync to readonly file!", osl::FileBase::E_None
, nError2
);
3474 // test case:no enough space, how to create such case???see test_cpy_wrt_file.cxx::test_osl_writeFile
3476 CPPUNIT_TEST_SUITE(sync
);
3477 CPPUNIT_TEST(sync_001
);
3478 CPPUNIT_TEST_SUITE_END();
3481 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::ctors
, "osl_File");
3482 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::open
, "osl_File");
3483 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::close
, "osl_File");
3484 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setPos
, "osl_File");
3485 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::getPos
, "osl_File");
3486 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::isEndOfFile
, "osl_File");
3487 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setSize
, "osl_File");
3488 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::read
, "osl_File");
3489 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::write
, "osl_File");
3490 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::readLine
, "osl_File");
3491 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::copy
, "osl_File");
3492 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::move
, "osl_File");
3493 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::remove
, "osl_File");
3494 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setAttributes
, "osl_File");
3495 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setTime
, "osl_File");
3496 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::sync
, "osl_File");
3498 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_File");
3501 // Beginning of the test cases for DirectoryItem class
3503 namespace osl_DirectoryItem
3505 // testing the method
3506 // DirectoryItem(): _pData(NULL)
3508 class ctors
: public CppUnit::TestFixture
3511 void setUp() override
3513 // create a tempfile in $TEMP/tmpname.
3514 createTestFile(aTmpName6
);
3517 void tearDown() override
3519 // remove the tempfile in $TEMP/tmpname.
3520 deleteTestFile(aTmpName6
);
3525 File
testFile(aTmpName6
);
3526 DirectoryItem rItem
; // constructor
3528 // get the DirectoryItem.
3529 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3530 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3532 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a new instance of DirectoryItem and get an item to check.",
3533 osl::FileBase::E_None
, nError1
);
3536 CPPUNIT_TEST_SUITE(ctors
);
3537 CPPUNIT_TEST(ctors_001
);
3538 CPPUNIT_TEST_SUITE_END();
3541 // testing the method
3542 // DirectoryItem(const DirectoryItem& rItem): _pData(rItem._pData)
3544 class copy_assin_Ctors
: public CppUnit::TestFixture
3547 void setUp() override
3549 // create a tempfile in $TEMP/tmpname.
3550 createTestFile(aTmpName6
);
3553 void tearDown() override
3555 // remove the tempfile in $TEMP/tmpname.
3556 deleteTestFile(aTmpName6
);
3560 void copy_assin_Ctors_001()
3562 DirectoryItem rItem
; // constructor
3563 // get the DirectoryItem.
3564 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3565 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3567 DirectoryItem
copyItem(rItem
); // copy constructor
3568 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3569 nError1
= copyItem
.getFileStatus(rFileStatus
);
3570 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3572 CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: use copy constructor to get an item and check filename.",
3573 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3576 void copy_assin_Ctors_002()
3578 DirectoryItem rItem
; // constructor
3579 // get the DirectoryItem.
3580 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3581 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3583 DirectoryItem copyItem
;
3584 copyItem
= rItem
; // assignment operator
3585 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3586 nError1
= copyItem
.getFileStatus(rFileStatus
);
3587 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3589 CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: test assignment operator here since it is same as copy constructor in test way.",
3590 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3593 CPPUNIT_TEST_SUITE(copy_assin_Ctors
);
3594 CPPUNIT_TEST(copy_assin_Ctors_001
);
3595 CPPUNIT_TEST(copy_assin_Ctors_002
);
3596 CPPUNIT_TEST_SUITE_END();
3599 // testing the method
3600 // inline sal_Bool is()
3602 class is
: public CppUnit::TestFixture
3605 void setUp() override
3607 // create a tempfile in $TEMP/tmpname.
3608 createTestFile(aTmpName6
);
3611 void tearDown() override
3613 // remove the tempfile in $TEMP/tmpname.
3614 deleteTestFile(aTmpName6
);
3619 DirectoryItem rItem
; // constructor
3621 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3627 DirectoryItem rItem
; // constructor
3628 // get the DirectoryItem.
3629 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3630 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3632 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3636 CPPUNIT_TEST_SUITE(is
);
3637 CPPUNIT_TEST(is_001
);
3638 CPPUNIT_TEST(is_002
);
3639 CPPUNIT_TEST_SUITE_END();
3642 // testing the method
3643 // static inline RC get(const OUString& ustrFileURL, DirectoryItem& rItem)
3645 class get
: public CppUnit::TestFixture
3648 void setUp() override
3650 // create a tempfile in $TEMP/tmpname.
3651 createTestFile(aTmpName6
);
3654 void tearDown() override
3656 // remove the tempfile in $TEMP/tmpname.
3657 deleteTestFile(aTmpName6
);
3663 DirectoryItem rItem
;
3664 auto nError2
= DirectoryItem::get(aTmpName6
, rItem
);
3666 // check the file name
3667 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3668 auto nError1
= rItem
.getFileStatus(rFileStatus
);
3669 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3671 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3672 osl::FileBase::E_None
, nError2
);
3673 CPPUNIT_ASSERT_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3674 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3679 DirectoryItem rItem
;
3680 auto nError1
= DirectoryItem::get(aSysPath1
, rItem
);
3682 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a system name instead of a URL.",
3683 osl::FileBase::E_INVAL
, nError1
);
3688 DirectoryItem rItem
;
3690 auto nError1
= DirectoryItem::get(aTmpName3
, rItem
);
3692 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a non existed file URL.",
3693 osl::FileBase::E_NOENT
, nError1
);
3696 CPPUNIT_TEST_SUITE(get
);
3697 CPPUNIT_TEST(get_001
);
3698 CPPUNIT_TEST(get_002
);
3699 CPPUNIT_TEST(get_003
);
3700 CPPUNIT_TEST_SUITE_END();
3703 // testing the method
3704 // inline RC getFileStatus(FileStatus& rStatus)
3706 class getFileStatus
: public CppUnit::TestFixture
3709 void setUp() override
3711 // create a tempfile in $TEMP/tmpdir/tmpname.
3712 createTestDirectory(aTmpName3
);
3713 createTestFile(aTmpName4
);
3716 void tearDown() override
3718 // remove the tempfile in $TEMP/tmpdir/tmpname.
3719 deleteTestFile(aTmpName4
);
3720 deleteTestDirectory(aTmpName3
);
3724 void getFileStatus_001()
3726 DirectoryItem rItem
;
3727 // get the DirectoryItem.
3728 auto nError1
= DirectoryItem::get(aTmpName4
, rItem
);
3729 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3731 // check the file name
3732 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3733 auto nError2
= rItem
.getFileStatus(rFileStatus
);
3735 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get file status and check filename",
3736 osl::FileBase::E_None
, nError2
);
3737 CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get file status and check filename",
3738 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3741 void getFileStatus_002()
3743 DirectoryItem rItem
; // constructor
3744 // get the DirectoryItem.
3745 auto nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3746 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_NOENT
, nError1
);
3748 // check the file name
3749 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3750 auto nError2
= rItem
.getFileStatus(rFileStatus
);
3752 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: file not existed",
3753 osl::FileBase::E_INVAL
, nError2
);
3756 void getFileStatus_003()
3758 DirectoryItem rItem
; // constructor
3759 // get the DirectoryItem.
3760 auto nError1
= DirectoryItem::get(aTmpName3
, rItem
);
3761 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3763 // check the file name
3764 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3765 auto nError2
= rItem
.getFileStatus(rFileStatus
);
3767 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get directory information",
3768 osl::FileBase::E_None
, nError2
);
3769 CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get directory information",
3770 compareFileName(rFileStatus
.getFileName(), aTmpName1
));
3773 CPPUNIT_TEST_SUITE(getFileStatus
);
3774 CPPUNIT_TEST(getFileStatus_001
);
3775 CPPUNIT_TEST(getFileStatus_002
);
3776 CPPUNIT_TEST(getFileStatus_003
);
3777 CPPUNIT_TEST_SUITE_END();
3780 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::ctors
, "osl_DirectoryItem");
3781 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::copy_assin_Ctors
, "osl_DirectoryItem");
3782 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::is
, "osl_DirectoryItem");
3783 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::get
, "osl_DirectoryItem");
3784 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::getFileStatus
, "osl_DirectoryItem");
3786 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_DirectoryItem");
3789 // Beginning of the test cases for Directory class
3791 namespace osl_Directory
3793 // testing the method
3794 // Directory(const OUString& strPath): _pData(0), _aPath(strPath)
3796 class ctors
: public CppUnit::TestFixture
3799 void setUp() override
3801 // create a tempfile in $TEMP/tmpdir/tmpname.
3802 createTestDirectory(aTmpName3
);
3803 createTestFile(aTmpName4
);
3806 void tearDown() override
3808 // remove the tempfile in $TEMP/tmpdir/tmpname.
3809 deleteTestFile(aTmpName4
);
3810 deleteTestDirectory(aTmpName3
);
3811 // LLA: t_print("tearDown done.\n");
3817 Directory
testDirectory(aTmpName3
); // constructor
3820 auto nError1
= testDirectory
.open();
3821 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3822 // close a directory
3823 auto nError2
= testDirectory
.close();
3824 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3826 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
3827 osl::FileBase::E_None
, nError1
);
3828 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
3829 osl::FileBase::E_None
, nError2
);
3834 Directory
testDirectory(aTmpName9
); // constructor
3837 auto nError1
= testDirectory
.open();
3838 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3839 // close a directory
3840 auto nError2
= testDirectory
.close();
3841 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3843 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
3844 osl::FileBase::E_None
, nError1
);
3845 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
3846 osl::FileBase::E_None
, nError2
);
3849 CPPUNIT_TEST_SUITE(ctors
);
3850 CPPUNIT_TEST(ctors_001
);
3851 CPPUNIT_TEST(ctors_002
);
3852 CPPUNIT_TEST_SUITE_END();
3855 // testing the method
3858 class open
: public CppUnit::TestFixture
3861 void setUp() override
3863 // create a tempfile in $TEMP/tmpdir/tmpname.
3864 createTestDirectory(aTmpName3
);
3865 createTestFile(aTmpName4
);
3868 void tearDown() override
3870 // remove the tempfile in $TEMP/tmpdir/tmpname.
3871 deleteTestFile(aTmpName4
);
3872 deleteTestDirectory(aTmpName3
);
3877 Directory
testDirectory(aTmpName3
);
3880 auto nError1
= testDirectory
.open();
3881 // check if directory is opened.
3882 bool bOk
= testDirectory
.isOpen();
3883 // close a directory
3884 auto nError2
= testDirectory
.close();
3886 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory and check for open",
3888 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
3889 osl::FileBase::E_None
, nError1
);
3890 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
3891 osl::FileBase::E_None
, nError2
);
3896 Directory
testDirectory(aTmpName6
);
3898 auto nError1
= testDirectory
.open();
3899 if (nError1
== osl::FileBase::E_None
)
3901 auto nError2
= testDirectory
.close();
3902 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3905 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a file that is not existed",
3906 osl::FileBase::E_NOENT
, nError1
);
3911 Directory
testDirectory(aUserDirectorySys
);
3913 auto nError1
= testDirectory
.open();
3914 if (nError1
== osl::FileBase::E_None
)
3916 auto nError2
= testDirectory
.close();
3917 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3920 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: using system path",
3921 osl::FileBase::E_INVAL
, nError1
);
3926 Directory
testDirectory(aTmpName4
);
3928 auto nError1
= testDirectory
.open();
3929 if (nError1
== osl::FileBase::E_None
)
3931 auto nError2
= testDirectory
.close();
3932 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3935 CPPUNIT_ASSERT_MESSAGE("test for open function: open a file instead of a directory",
3936 (osl::FileBase::E_NOTDIR
== nError1
) || (osl::FileBase::E_ACCES
== nError1
));
3939 CPPUNIT_TEST_SUITE(open
);
3940 CPPUNIT_TEST(open_001
);
3941 CPPUNIT_TEST(open_002
);
3942 CPPUNIT_TEST(open_003
);
3943 CPPUNIT_TEST(open_004
);
3944 CPPUNIT_TEST_SUITE_END();
3947 // testing the method
3948 // inline sal_Bool isOpen() { return _pData != NULL; };
3950 class isOpen
: public CppUnit::TestFixture
3953 void setUp() override
3955 // create a tempfile in $TEMP/tmpdir/tmpname.
3956 createTestDirectory(aTmpName3
);
3957 createTestFile(aTmpName4
);
3960 void tearDown() override
3962 // remove the tempfile in $TEMP/tmpdir/tmpname.
3963 deleteTestFile(aTmpName4
);
3964 deleteTestDirectory(aTmpName3
);
3970 Directory
testDirectory(aTmpName3
); // constructor
3973 auto nError1
= testDirectory
.open();
3974 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3975 // check if directory is opened.
3976 bool bOk
= testDirectory
.isOpen();
3977 // close a directory
3978 auto nError2
= testDirectory
.close();
3979 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3980 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: open a directory and check for open",
3986 Directory
testDirectory(aTmpName3
); // constructor
3988 // check if directory is opened.
3989 bool bOk
= testDirectory
.isOpen();
3991 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: do not open a directory and check for open",
3995 CPPUNIT_TEST_SUITE(isOpen
);
3996 CPPUNIT_TEST(isOpen_001
);
3997 CPPUNIT_TEST(isOpen_002
);
3998 CPPUNIT_TEST_SUITE_END();
4001 // testing the method
4002 // inline RC close()
4004 class close
: public CppUnit::TestFixture
4007 void setUp() override
4009 // create a tempdirectory : $TEMP/tmpdir.
4010 createTestDirectory(aTmpName3
);
4013 void tearDown() override
4015 // remove a tempdirectory : $TEMP/tmpdir.
4016 deleteTestDirectory(aTmpName3
);
4021 Directory
testDirectory(aTmpName3
);
4024 auto nError1
= testDirectory
.open();
4025 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4026 // close a directory
4027 auto nError2
= testDirectory
.close();
4028 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4029 // check if directory is opened.
4030 bool bOk
= testDirectory
.isOpen();
4032 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: close a directory and check for open",
4038 Directory
testDirectory(aTmpName3
);
4040 // close a directory
4041 auto nError1
= testDirectory
.close();
4043 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isOpen function: close a not opened directory",
4044 osl::FileBase::E_BADF
, nError1
);
4047 CPPUNIT_TEST_SUITE(close
);
4048 CPPUNIT_TEST(close_001
);
4049 CPPUNIT_TEST(close_002
);
4050 CPPUNIT_TEST_SUITE_END();
4053 // testing the method
4054 // inline RC reset()
4056 class reset
: public CppUnit::TestFixture
4059 DirectoryItem rItem
;
4062 void setUp() override
4064 // create a tempdirectory : $TEMP/tmpdir.
4065 createTestDirectory(aTmpName3
);
4066 // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4067 createTestFile(aTmpName3
, aTmpName2
);
4068 createTestFile(aTmpName3
, aTmpName1
);
4069 createTestFile(aTmpName3
, aHidURL1
);
4072 void tearDown() override
4074 // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4075 deleteTestFile(aTmpName3
, aHidURL1
);
4076 deleteTestFile(aTmpName3
, aTmpName1
);
4077 deleteTestFile(aTmpName3
, aTmpName2
);
4078 // remove a tempdirectory : $TEMP/tmpdir.
4079 deleteTestDirectory(aTmpName3
);
4085 Directory
testDirectory(aTmpName3
); // constructor
4088 auto nError1
= testDirectory
.open();
4089 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4091 nError1
= testDirectory
.getNextItem(rItem
, 1);
4092 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4093 // check the file name of first Item
4094 FileStatus
rFileStatusFirst(osl_FileStatus_Mask_FileName
);
4095 nError1
= rItem
.getFileStatus(rFileStatusFirst
);
4098 // mindy: nError1 = testDirectory.getNextItem(rItem, 0);
4099 // mindy: CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4101 // reset enumeration
4102 auto nError2
= testDirectory
.reset();
4103 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4104 // get reset Item, if reset does not work, getNextItem() should return the second Item (aTmpName1)
4105 nError1
= testDirectory
.getNextItem(rItem
);
4106 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4108 // check the file name again
4109 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
4110 nError1
= rItem
.getFileStatus(rFileStatus
);
4111 // close a directory
4112 nError1
= testDirectory
.close();
4113 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4115 bool bOK1
,bOK2
,bOK3
;
4116 bOK1
= compareFileName(rFileStatus
.getFileName(), aTmpName2
);
4117 bOK2
= compareFileName(rFileStatus
.getFileName(), aHidURL1
);
4118 bOK3
= compareFileName(rFileStatus
.getFileName(), rFileStatusFirst
.getFileName());
4119 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4120 osl::FileBase::E_None
, nError2
);
4121 CPPUNIT_ASSERT_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4122 (bOK1
|| bOK2
|| bOK3
));
4127 Directory
testDirectory(aTmpName6
); // constructor
4129 // close a directory
4130 auto nError1
= testDirectory
.reset();
4132 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: reset a non existed directory",
4133 osl::FileBase::E_NOENT
, nError1
);
4138 Directory
testDirectory(aTmpName4
); // constructor
4140 // close a directory
4141 auto nError1
= testDirectory
.reset();
4143 CPPUNIT_ASSERT_MESSAGE("test for reset function: reset a file instead of a directory",
4144 (osl::FileBase::E_NOTDIR
== nError1
) || (osl::FileBase::E_NOENT
== nError1
));
4149 Directory
testDirectory(aUserDirectorySys
); // constructor
4151 // close a directory
4152 auto nError1
= testDirectory
.reset();
4154 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: use a system path",
4155 osl::FileBase::E_INVAL
, nError1
);
4158 CPPUNIT_TEST_SUITE(reset
);
4159 CPPUNIT_TEST(reset_001
);
4160 CPPUNIT_TEST(reset_002
);
4161 CPPUNIT_TEST(reset_003
);
4162 CPPUNIT_TEST(reset_004
);
4163 CPPUNIT_TEST_SUITE_END();
4166 // testing the method
4167 // inline RC getNextItem(DirectoryItem& rItem, sal_uInt32 nHint = 0)
4169 class getNextItem
: public CppUnit::TestFixture
4172 DirectoryItem rItem
;
4175 void setUp() override
4177 // create a tempdirectory : $TEMP/tmpdir.
4178 createTestDirectory(aTmpName3
);
4179 // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4180 createTestFile(aTmpName3
, aTmpName2
);
4181 createTestFile(aTmpName3
, aTmpName1
);
4182 createTestFile(aTmpName3
, aHidURL1
);
4186 void tearDown() override
4188 // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4189 deleteTestFile(aTmpName3
, aHidURL1
);
4190 deleteTestFile(aTmpName3
, aTmpName1
);
4191 deleteTestFile(aTmpName3
, aTmpName2
);
4192 // remove a tempdirectory : $TEMP/tmpdir.
4193 deleteTestDirectory(aTmpName3
);
4197 void getNextItem_001()
4199 Directory
testDirectory(aTmpName3
); // constructor
4202 auto nError1
= testDirectory
.open();
4203 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4205 // check the file name
4209 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
4211 for (int nCount
= 0; nCount
< 3; nCount
++)
4214 nError1
= testDirectory
.getNextItem(rItem
, 2);
4215 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4216 nError1
= rItem
.getFileStatus(rFileStatus
);
4217 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4219 // a special order is not guaranteed. So any file may occur on any time.
4220 // But every file name should occur only once.
4221 if (!bOk1
&& compareFileName(rFileStatus
.getFileName(), aTmpName1
))
4226 if (!bOk2
&& compareFileName(rFileStatus
.getFileName(), aTmpName2
))
4231 if (!bOk3
&& compareFileName(rFileStatus
.getFileName(), aHidURL1
))
4237 // close a directory
4238 nError1
= testDirectory
.close();
4239 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4241 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4243 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4245 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4249 void getNextItem_002()
4251 Directory
testDirectory(aTmpName3
); // constructor
4252 auto nError1
= testDirectory
.getNextItem(rItem
);
4254 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.",
4255 osl::FileBase::E_INVAL
, nError1
);
4258 void getNextItem_003()
4260 Directory
testDirectory(aTmpName3
); // constructor
4263 auto nError1
= testDirectory
.open();
4264 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4266 osl::FileBase::RC nError2
= osl::FileBase::E_None
;
4267 for (int nCount
= 0; nCount
< 4; nCount
++)
4269 nError2
= testDirectory
.getNextItem(rItem
, 3);
4272 // close a directory
4273 nError1
= testDirectory
.close();
4274 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4276 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getNextItem function: retrieve 4 times in a directory which contain only 3 files.",
4277 osl::FileBase::E_NOENT
, nError2
);
4280 void getNextItem_004()
4282 // create a link file(can not on Windows), then check if getNextItem can get it.
4284 bool bLnkOK
= false;
4285 bool bFoundOK
= false;
4287 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
4288 aUStr_LnkFileSys
+= aSlashURL
+ "/tmpdir/link.file";
4289 aUStr_SrcFileSys
+= aSlashURL
+ "/tmpdir/tmpname";
4291 OString strLinkFileName
, strSrcFileName
;
4292 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
4293 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
4295 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
4296 sal_Int32 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
4297 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
4298 Directory
testDirectory(aTmpName3
);
4301 auto nError1
= testDirectory
.open();
4302 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4303 OUString
aFileName ("link.file");
4306 nError1
= testDirectory
.getNextItem(rItem
, 4);
4307 if (nError1
== osl::FileBase::E_None
) {
4308 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
| osl_FileStatus_Mask_Type
);
4309 rItem
.getFileStatus(rFileStatus
);
4310 if (compareFileName(rFileStatus
.getFileName(), aFileName
))
4313 if (rFileStatus
.getFileType() == FileStatus::Link
)
4323 fd
= std::remove(strLinkFileName
.getStr());
4324 CPPUNIT_ASSERT_EQUAL_MESSAGE("remove link file failed", static_cast<sal_Int32
>(0), fd
);
4325 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if can retrieve the link file name",
4327 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if link file has file type link",
4332 CPPUNIT_TEST_SUITE(getNextItem
);
4333 CPPUNIT_TEST(getNextItem_001
);
4334 CPPUNIT_TEST(getNextItem_002
);
4335 CPPUNIT_TEST(getNextItem_003
);
4336 CPPUNIT_TEST(getNextItem_004
);
4337 CPPUNIT_TEST_SUITE_END();
4340 // testing the method
4341 // inline static RC getVolumeInfo(const OUString& ustrDirectoryURL, VolumeInfo& rInfo)
4343 class getVolumeInfo
: public CppUnit::TestFixture
4346 void checkValidMask(osl::VolumeInfo
const& _aVolumeInfo
, sal_Int32 _nMask
)
4348 if (_nMask
== osl_VolumeInfo_Mask_FileSystemName
)
4350 // get file system name
4351 OUString aFileSysName
= _aVolumeInfo
.getFileSystemName();
4353 bool bRes2
= compareFileName(aFileSysName
, aNullURL
);
4354 CPPUNIT_ASSERT_MESSAGE("test for getVolumeInfo function: getVolumeInfo of root directory.",
4358 if (_nMask
== osl_VolumeInfo_Mask_Attributes
)
4360 bool b1
= _aVolumeInfo
.getRemoteFlag();
4361 bool b2
= _aVolumeInfo
.getRemoveableFlag();
4362 bool b3
= _aVolumeInfo
.getCompactDiscFlag();
4363 bool b4
= _aVolumeInfo
.getFloppyDiskFlag();
4364 bool b5
= _aVolumeInfo
.getFixedDiskFlag();
4365 bool b6
= _aVolumeInfo
.getRAMDiskFlag();
4368 if (b1
) sAttr
= "Remote";
4369 if (b2
) sAttr
+= " Removeable";
4370 if (b3
) sAttr
+= " CDROM";
4371 if (b4
) sAttr
+= " Floppy";
4372 if (b5
) sAttr
+= " FixedDisk";
4373 if (b6
) sAttr
+= " RAMDisk";
4375 printf("Attributes: %s\n", sAttr
.getStr());
4377 if (_nMask
== osl_VolumeInfo_Mask_TotalSpace
)
4379 // within Linux, df / * 1024 bytes is the result
4380 sal_uInt64 nSize
= _aVolumeInfo
.getTotalSpace();
4381 printf("Total space: %" SAL_PRIuUINT64
"\n", nSize
);
4383 if (_nMask
== osl_VolumeInfo_Mask_UsedSpace
)
4385 sal_uInt64 nSize
= _aVolumeInfo
.getUsedSpace();
4386 printf(" Used space: %" SAL_PRIuUINT64
"\n", nSize
);
4388 if (_nMask
== osl_VolumeInfo_Mask_FreeSpace
)
4390 sal_uInt64 nSize
= _aVolumeInfo
.getFreeSpace();
4391 printf(" Free space: %" SAL_PRIuUINT64
"\n", nSize
);
4393 if (_nMask
== osl_VolumeInfo_Mask_MaxNameLength
)
4395 sal_uInt32 nLength
= _aVolumeInfo
.getMaxNameLength();
4396 printf("max name length: %" SAL_PRIuUINT32
"\n", nLength
);
4398 if (_nMask
== osl_VolumeInfo_Mask_MaxPathLength
)
4400 sal_uInt32 nLength
= _aVolumeInfo
.getMaxPathLength();
4401 printf("max path length: %" SAL_PRIuUINT32
"\n", nLength
);
4403 if (_nMask
== osl_VolumeInfo_Mask_FileSystemCaseHandling
)
4405 bool bIsCase
= _aVolumeInfo
.isCaseSensitiveFileSystem();
4406 printf("filesystem case sensitive: %s\n", bIsCase
? "yes" : "no");
4410 void checkVolumeInfo(sal_Int32 _nMask
)
4412 VolumeInfo
aVolumeInfo(_nMask
);
4413 // call getVolumeInfo here
4414 auto nError1
= Directory::getVolumeInfo(aVolURL1
, aVolumeInfo
);
4415 CPPUNIT_ASSERT_EQUAL_MESSAGE(
4416 "test for getVolumeInfo function: getVolumeInfo of root directory.",
4417 osl::FileBase::E_None
, nError1
);
4418 // LLA: IMHO it's not a bug, if VolumeInfo is not valid, it's a feature
4419 // LLA: CPPUNIT_ASSERT_MESSAGE("mask is not valid", sal_True == aVolumeInfo.isValid(_nMask));
4420 if (aVolumeInfo
.isValid(_nMask
))
4421 checkValidMask(aVolumeInfo
, _nMask
);
4424 void getVolumeInfo_001_1()
4426 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemName
;
4427 checkVolumeInfo(mask
);
4430 void getVolumeInfo_001_2()
4432 sal_Int32 mask
= osl_VolumeInfo_Mask_Attributes
;
4433 checkVolumeInfo(mask
);
4436 void getVolumeInfo_001_3()
4438 sal_Int32 mask
= osl_VolumeInfo_Mask_TotalSpace
;
4439 checkVolumeInfo(mask
);
4442 void getVolumeInfo_001_4()
4444 sal_Int32 mask
= osl_VolumeInfo_Mask_UsedSpace
;
4445 checkVolumeInfo(mask
);
4448 void getVolumeInfo_001_5()
4450 sal_Int32 mask
= osl_VolumeInfo_Mask_FreeSpace
;
4451 checkVolumeInfo(mask
);
4454 void getVolumeInfo_001_6()
4456 sal_Int32 mask
= osl_VolumeInfo_Mask_MaxNameLength
;
4457 checkVolumeInfo(mask
);
4460 void getVolumeInfo_001_7()
4462 sal_Int32 mask
= osl_VolumeInfo_Mask_MaxPathLength
;
4463 checkVolumeInfo(mask
);
4466 void getVolumeInfo_001_8()
4468 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemCaseHandling
;
4469 checkVolumeInfo(mask
);
4472 void getVolumeInfo_002()
4474 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemName
;
4475 VolumeInfo
aVolumeInfo(mask
);
4476 // call getVolumeInfo here
4478 OUString aRootSysURL
;
4479 auto nError1
= osl::File::getFileURLFromSystemPath(aRootSys
, aRootSysURL
);
4480 CPPUNIT_ASSERT_EQUAL_MESSAGE("can't convert root path to file url", osl::FileBase::E_None
, nError1
);
4482 nError1
= Directory::getVolumeInfo(aRootSys
, aVolumeInfo
);
4484 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: use system path as parameter.",
4485 osl::FileBase::E_INVAL
, nError1
);
4488 void getVolumeInfo_003()
4490 // LLA: in Windows, it reply no error, it did not pass in (W32).
4491 #if defined(UNX) && !defined(IOS)
4492 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemName
;
4493 VolumeInfo
aVolumeInfo(mask
);
4494 // call getVolumeInfo here
4495 auto nError1
= Directory::getVolumeInfo(aTmpName3
, aVolumeInfo
);
4497 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: non-existence test. ",
4498 osl::FileBase::E_NOENT
, nError1
);
4502 CPPUNIT_TEST_SUITE(getVolumeInfo
);
4503 CPPUNIT_TEST(getVolumeInfo_001_1
);
4504 CPPUNIT_TEST(getVolumeInfo_001_2
);
4505 CPPUNIT_TEST(getVolumeInfo_001_3
);
4506 CPPUNIT_TEST(getVolumeInfo_001_4
);
4507 CPPUNIT_TEST(getVolumeInfo_001_5
);
4508 CPPUNIT_TEST(getVolumeInfo_001_6
);
4509 CPPUNIT_TEST(getVolumeInfo_001_7
);
4510 CPPUNIT_TEST(getVolumeInfo_001_8
);
4511 CPPUNIT_TEST(getVolumeInfo_002
);
4512 CPPUNIT_TEST(getVolumeInfo_003
);
4513 CPPUNIT_TEST_SUITE_END();
4516 // testing the method
4517 // inline static RC create(const OUString& ustrDirectoryURL)
4519 class create
: public CppUnit::TestFixture
4524 // create directory in $TEMP/tmpdir
4525 auto nError1
= Directory::create(aTmpName3
);
4526 // check for existence
4527 auto nError2
= Directory::create(aTmpName3
);
4529 deleteTestDirectory(aTmpName3
);
4531 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4532 osl::FileBase::E_None
, nError1
);
4533 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4534 osl::FileBase::E_EXIST
, nError2
);
4539 #if !defined(_WIN32) && !defined(MACOSX) && defined(SAL_UNX)
4540 if (geteuid() == 0) // don't test if building as root
4544 auto nError1
= osl::FileBase::createTempFile(nullptr, nullptr, &aTmpDir
);
4545 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File creation failed", osl::FileBase::E_None
, nError1
);
4547 nError1
= File::remove(aTmpDir
);
4548 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File removal failed", osl::FileBase::E_None
, nError1
);
4550 nError1
= Directory::create(aTmpDir
);
4551 OString sError
= "test for create function: create a directory '" +
4552 OUStringToOString(aTmpDir
, RTL_TEXTENCODING_ASCII_US
) +
4553 "' and check its existence.";
4554 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_None
, nError1
);
4555 osl_setFileAttributes(aTmpDir
.pData
, 0); // no access allowed now
4557 // Shouldn't be possible now to create a dir underneath it
4558 OUString aTmpSubLevel
= aTmpDir
+ "/notallowedhere";
4559 nError1
= Directory::create(aTmpSubLevel
);
4562 osl_setFileAttributes(aTmpDir
.pData
,
4563 osl_File_Attribute_OwnRead
|
4564 osl_File_Attribute_OwnWrite
|
4565 osl_File_Attribute_OwnExe
);
4566 deleteTestDirectory(aTmpDir
);
4567 sError
= "test for create function: create a directory under '" +
4568 OUStringToOString(aTmpDir
, RTL_TEXTENCODING_ASCII_US
) +
4569 "' for access test.";
4570 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_ACCES
, nError1
);
4576 // create directory in /tmpname
4577 auto nError1
= Directory::create(aSysPath1
);
4579 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory using system path.",
4580 osl::FileBase::E_INVAL
, nError1
);
4583 CPPUNIT_TEST_SUITE(create
);
4584 CPPUNIT_TEST(create_001
);
4585 CPPUNIT_TEST(create_002
);
4586 CPPUNIT_TEST(create_003
);
4587 CPPUNIT_TEST_SUITE_END();
4590 // testing the method
4591 // inline static RC remove(const OUString& ustrDirectoryURL)
4593 class remove
: public CppUnit::TestFixture
4598 // create directory in $TEMP/tmpdir
4599 auto nError1
= Directory::create(aTmpName3
);
4600 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4602 nError1
= Directory::remove(aTmpName3
);
4603 // check for existence
4604 Directory
rDirectory(aTmpName3
);
4605 auto nError2
= rDirectory
.open();
4607 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4608 osl::FileBase::E_None
, nError1
);
4609 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4610 osl::FileBase::E_NOENT
, nError2
);
4615 // create directory in $TEMP/tmpdir
4616 auto nError1
= Directory::create(aTmpName3
);
4617 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4618 // try to remove it by system path
4619 nError1
= Directory::remove(aSysPath3
);
4620 // check for existence
4621 Directory
rDirectory(aTmpName3
);
4622 auto nError2
= rDirectory
.open();
4624 if (nError2
!= osl::FileBase::E_NOENT
)
4625 Directory::remove(aTmpName3
);
4627 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory by its system path, and check its existence.",
4628 osl::FileBase::E_INVAL
, nError1
);
4633 // try to remove a non-existed directory
4634 auto nError1
= Directory::remove(aTmpName6
);
4636 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: try to remove a non-existed directory.",
4637 osl::FileBase::E_NOENT
, nError1
);
4642 createTestFile(aTmpName6
);
4643 bool bExist
= ifFileExist(aTmpName6
);
4644 // try to remove file.
4645 auto nError1
= Directory::remove(aTmpName6
);
4646 deleteTestFile(aTmpName6
);
4648 CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4650 CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4651 (osl::FileBase::E_NOTDIR
== nError1
) || (osl::FileBase::E_NOENT
== nError1
));
4656 createTestDirectory(aTmpName3
);
4657 createTestFile(aTmpName4
);
4658 auto nError1
= Directory::remove(aTmpName3
);
4659 deleteTestFile(aTmpName4
);
4660 deleteTestDirectory(aTmpName3
);
4661 OString sError
= "test for remove function: try to remove a directory that is not empty." +
4662 errorToStr(nError1
);
4664 // 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.
4665 // EEXIST The directory contains entries other than those for "." and "..".
4666 printf("#Solaris test\n");
4667 CPPUNIT_ASSERT_MESSAGE(sError
.getStr(), (osl::FileBase::E_EXIST
== nError1
));
4669 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_NOTEMPTY
, nError1
);
4673 CPPUNIT_TEST_SUITE(remove
);
4674 CPPUNIT_TEST(remove_001
);
4675 CPPUNIT_TEST(remove_002
);
4676 CPPUNIT_TEST(remove_003
);
4677 CPPUNIT_TEST(remove_004
);
4678 CPPUNIT_TEST(remove_005
);
4679 CPPUNIT_TEST_SUITE_END();
4682 // TEST Directory::createPath
4685 # define PATH_BUFFER_SIZE MAX_PATH
4687 # define PATH_BUFFER_SIZE PATH_MAX
4690 #define TEST_PATH_POSTFIX "hello/world"
4692 static OUString
const & get_test_path()
4694 static OUString test_path
= []()
4697 osl::FileBase::RC rc
= osl::FileBase::getTempDirURL(tmp
);
4699 CPPUNIT_ASSERT_EQUAL_MESSAGE
4701 "Getting the location of TMP dir failed",
4702 osl::FileBase::E_None
, rc
4705 OUString system_path
;
4706 rc
= osl::FileBase::getSystemPathFromFileURL(tmp
, system_path
);
4708 CPPUNIT_ASSERT_EQUAL_MESSAGE
4710 "Cannot convert the TMP dir to system path",
4711 osl::FileBase::E_None
, rc
4714 OString
tmp_x(OUStringToOString(system_path
, RTL_TEXTENCODING_UTF8
));
4715 if (tmp_x
.lastIndexOf('/') != (tmp_x
.getLength() - 1))
4718 #if !defined(_WIN32) && !defined(ANDROID)
4719 // FIXME would be nice to create unique dir even on Windows
4721 char *out
= mkdtemp(const_cast<char*>(tmp_x
.getStr()));
4723 CPPUNIT_ASSERT_MESSAGE
4725 "mkdtemp call failed",
4731 tmp_x
+= TEST_PATH_POSTFIX
;
4733 OUString tmpTestPath
;
4734 rc
= osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x
, RTL_TEXTENCODING_UTF8
), tmpTestPath
);
4736 CPPUNIT_ASSERT_EQUAL_MESSAGE
4738 "Cannot convert the system path back to a URL",
4739 osl::FileBase::E_None
, rc
4746 static void rm_test_path(const OUString
& path
)
4748 sal_Unicode buffer
[PATH_BUFFER_SIZE
];
4749 memcpy(buffer
, path
.getStr(), (path
.getLength() + 1) * sizeof(sal_Unicode
));
4751 sal_Int32 i
= rtl_ustr_lastIndexOfChar(buffer
, '/');
4752 if (i
== path
.getLength())
4755 Directory::remove(OUString(buffer
));
4757 i
= rtl_ustr_lastIndexOfChar(buffer
, '/');
4762 Directory::remove(OUString(buffer
));
4768 class DirCreatedObserver
: public DirectoryCreationObserver
4771 DirCreatedObserver() : i(0) {}
4772 virtual void DirectoryCreated(const OUString
&) override
{ i
++; };
4774 int number_of_dirs_created() const { return i
; }
4782 class createPath
: public CppUnit::TestFixture
4788 void with_relative_path()
4790 osl::FileBase::RC rc
= Directory::createPath(TEST_PATH_POSTFIX
);
4792 CPPUNIT_ASSERT_EQUAL_MESSAGE
4794 "osl_createDirectoryPath contract broken",
4795 osl::FileBase::E_INVAL
, rc
4799 void without_callback()
4801 OUString tp_url
= get_test_path();
4803 rm_test_path(tp_url
);
4805 osl::FileBase::RC rc
= Directory::createPath(tp_url
);
4807 rm_test_path(tp_url
);
4809 CPPUNIT_ASSERT_EQUAL_MESSAGE
4811 "osl_createDirectoryPath failed",
4812 osl::FileBase::E_None
, rc
4816 void with_callback()
4818 OUString tp_url
= get_test_path();
4820 rm_test_path(tp_url
);
4822 DirCreatedObserver
* observer
= new DirCreatedObserver
;
4823 osl::FileBase::RC rc
= Directory::createPath(tp_url
, observer
);
4824 int nDirs
= observer
->number_of_dirs_created();
4827 rm_test_path(tp_url
);
4829 CPPUNIT_ASSERT_EQUAL_MESSAGE
4831 "osl_createDirectoryPath failed",
4832 osl::FileBase::E_None
, rc
4834 CPPUNIT_ASSERT_MESSAGE
4836 "osl_createDirectoryPath failed",
4844 const char* get_unused_drive_letter()
4846 static const char m_aBuff
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4848 DWORD ld
= GetLogicalDrives();
4852 while ((ld
& i
) && (i
> 1))
4853 { i
= i
<< 1; j
++; }
4861 void at_invalid_logical_drive()
4863 const char* drv
= get_unused_drive_letter();
4864 char buff
[PATH_BUFFER_SIZE
];
4865 memset(buff
, 0, sizeof(buff
));
4867 strncpy(buff
, drv
, 1);
4868 strcat(buff
, ":\\");
4869 strcat(buff
, TEST_PATH_POSTFIX
);
4871 OUString path
= OUString::createFromAscii(buff
);
4873 osl::FileBase::getFileURLFromSystemPath(path
, tp_url
);
4875 osl::FileBase::RC rc
= Directory::createPath(tp_url
);
4877 CPPUNIT_ASSERT_MESSAGE
4879 "osl_createDirectoryPath doesn't fail on unused logical drive letters",
4880 rc
!= osl::FileBase::E_None
4885 CPPUNIT_TEST_SUITE(createPath
);
4886 CPPUNIT_TEST(with_relative_path
);
4887 CPPUNIT_TEST(without_callback
);
4888 CPPUNIT_TEST(with_callback
);
4890 CPPUNIT_TEST(at_invalid_logical_drive
);
4892 CPPUNIT_TEST_SUITE_END();
4896 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::ctors
);
4897 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::open
);
4898 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::isOpen
);
4899 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::close
);
4900 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::reset
);
4901 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getNextItem
);
4902 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getVolumeInfo
);
4903 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::create
);
4904 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::remove
);
4905 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::createPath
);
4910 /** get Current PID.
4912 OUString
getCurrentPID()
4914 //~ Get current PID and turn it into OUString;
4917 nPID
= GetCurrentProcessId();
4921 return OUString::number(nPID
);
4928 //~ do some clean up work after all test completed.
4936 //~ special clean up task in Windows and Unix separately;
4938 //~ some clean up task for UNIX OS
4941 //~ some clean up task for Windows OS
4942 //~ check if some files are in the way, remove them if necessary.
4943 if (ifFileExist(aTmpName6
))
4944 deleteTestFile(aTmpName6
);
4945 if (ifFileExist(aTmpName4
))
4946 deleteTestFile(aTmpName4
);
4947 if (checkDirectory(aTmpName4
, oslCheckMode::Exist
))
4948 deleteTestDirectory(aTmpName4
);
4949 if (ifFileExist(aTmpName3
))
4950 deleteTestFile(aTmpName3
);
4951 if (checkDirectory(aTmpName3
, oslCheckMode::Exist
))
4952 deleteTestDirectory(aTmpName3
);
4954 OUString
aUStr(aUserDirectoryURL
);
4955 concatURL(aUStr
, aHidURL1
);
4956 if (ifFileExist(aUStr
))
4957 deleteTestFile(aUStr
);
4959 OUString
aUStr1(aRootURL
);
4960 concatURL(aUStr1
, aTmpName2
);
4961 if (ifFileExist(aUStr1
))
4962 deleteTestFile(aUStr1
);
4965 catch (const CppUnit::Exception
&e
)
4967 printf("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e
.what(), e
.sourceLine().lineNumber());
4971 printf("Exception caught (...) in GlobalObject dtor()\n");
4978 static GlobalObject theGlobalObject
;
4980 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */