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>
36 #if !defined WIN32_LEAN_AND_MEAN
37 # define WIN32_LEAN_AND_MEAN
44 /** detailed wrong message.
46 static OString
errorToString(const osl::FileBase::RC _nError
)
50 case osl::FileBase::E_None
:
53 case osl::FileBase::E_PERM
:
54 sResult
= "Operation not permitted";
56 case osl::FileBase::E_NOENT
:
57 sResult
= "No such file or directory";
59 case osl::FileBase::E_EXIST
:
60 sResult
= "Already Exist";
62 case osl::FileBase::E_ACCES
:
63 sResult
= "Permission denied";
65 case osl::FileBase::E_INVAL
:
66 sResult
= "The format of the parameters was not valid";
68 case osl::FileBase::E_NOTDIR
:
69 sResult
= "Not a directory";
71 case osl::FileBase::E_ISDIR
:
72 sResult
= "Is a directory";
74 case osl::FileBase::E_BADF
:
77 case osl::FileBase::E_NOTEMPTY
:
78 sResult
= "The directory is not empty";
81 sResult
= "Unknown Error";
87 static OString
errorToStr(osl::FileBase::RC
const& nError
)
89 OString suBuf
= "The returned error is: " +
90 errorToString(nError
) +
95 /** compare two TimeValue, unit is "ms", since Windows time precision is better than UNX.
97 /* FIXME: the above assertion is bogus */
99 #if (defined UNX) // precision of time in Windows is better than UNX
100 # define delta 2000 // time precision, 2000ms
102 # define delta 1800 // time precision, 1.8s
105 static bool t_compareTime(TimeValue
*m_aEndTime
, TimeValue
*m_aStartTime
, sal_Int32 nDelta
)
107 sal_Int32 nDeltaSeconds
= m_aEndTime
->Seconds
- m_aStartTime
->Seconds
;
108 sal_Int32 nDeltaNanoSec
= sal_Int32(m_aEndTime
->Nanosec
) - sal_Int32(m_aStartTime
->Nanosec
);
109 if (nDeltaNanoSec
< 0)
111 nDeltaNanoSec
= 1000000000 + nDeltaNanoSec
;
115 sal_Int32 nDeltaMilliSec
= (nDeltaSeconds
* 1000) + (nDeltaNanoSec
/ 1000000);
116 return (nDeltaMilliSec
< nDelta
);
119 /** compare two OUString file name.
121 static bool compareFileName(const OUString
& ustr1
, const OUString
& ustr2
)
124 // on Windows, the separator is '\', so here change to '/', then compare
126 OUString ustr1new
,ustr2new
;
127 sal_Unicode reverseSlash
= '\\';
129 if (ustr1
.lastIndexOf(reverseSlash
) != -1)
130 ustr1new
= ustr1
.replace(reverseSlash
,'/');
133 if (ustr2
.lastIndexOf(reverseSlash
) != -1)
134 ustr2new
= ustr2
.replace(reverseSlash
,'/');
137 bOk
= ustr1new
.equalsIgnoreAsciiCase(ustr2new
);
139 bOk
= ustr1
.equalsIgnoreAsciiCase(ustr2
);
144 /** simple version to judge if a file name or directory name is a URL or a system path, just to see if it
145 is start with "file:///";.
147 static bool isURL(const OUString
& pathname
)
149 return pathname
.startsWith(aPreURL
);
152 /** concat two part to form a URL or system path, add PATH_SEPARATOR between them if necessary, add "file:///" to beginning if necessary.
154 static void concatURL(OUString
& pathname1
, const OUString
& pathname2
)
156 // check if pathname1 is full qualified URL;
157 if (!isURL(pathname1
))
159 OUString aPathName
= pathname1
.copy(0);
160 osl::FileBase::getFileURLFromSystemPath(pathname1
, aPathName
); // convert if not full qualified URL
161 pathname1
= aPathName
.copy(0);
164 // check if '/' is in the end of pathname1 or at the begin of pathname2;
165 if (!pathname1
.endsWith(aSlashURL
) && !pathname2
.startsWith(aSlashURL
))
166 pathname1
+= aSlashURL
;
167 pathname1
+= pathname2
;
170 /** create a temp test file using OUString name of full qualified URL or system path.
172 static void createTestFile(const OUString
& filename
)
174 OUString aPathURL
= filename
.copy(0);
175 osl::FileBase::RC nError
;
177 if (!isURL(filename
))
178 osl::FileBase::getFileURLFromSystemPath(filename
, aPathURL
); // convert if not full qualified URL
180 File
aFile(aPathURL
);
181 nError
= aFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
182 if ((nError
!= osl::FileBase::E_None
) && (nError
!= osl::FileBase::E_EXIST
))
183 printf("createTestFile failed!\n");
189 /** create a temp test file using OUString name of full qualified URL or system path in a base directory.
191 static void createTestFile(const OUString
& basename
, const OUString
& filename
)
193 OUString aBaseURL
= basename
.copy(0);
195 concatURL(aBaseURL
, filename
);
196 createTestFile(aBaseURL
);
199 /** delete a temp test file using OUString name.
201 static void deleteTestFile(const OUString
& filename
)
203 OUString aPathURL
= filename
.copy(0);
204 osl::FileBase::RC nError
;
206 if (!isURL(filename
))
207 osl::FileBase::getFileURLFromSystemPath(filename
, aPathURL
); // convert if not full qualified URL
209 nError
= File::setAttributes(aPathURL
, osl_File_Attribute_GrpWrite
| osl_File_Attribute_OwnWrite
| osl_File_Attribute_OthWrite
); // if readonly, make writable.
210 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: set writable ", (osl::FileBase::E_None
== nError
) || (osl::FileBase::E_NOENT
== nError
));
212 nError
= File::remove(aPathURL
);
213 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: remove ", (osl::FileBase::E_None
== nError
) || (nError
== osl::FileBase::E_NOENT
));
216 /** delete a temp test file using OUString name of full qualified URL or system path in a base directory.
218 static void deleteTestFile(const OUString
& basename
, const OUString
& filename
)
220 OUString aBaseURL
= basename
.copy(0);
222 concatURL(aBaseURL
, filename
);
223 deleteTestFile(aBaseURL
);
226 /** create a temp test directory using OUString name of full qualified URL or system path.
228 static void createTestDirectory(const OUString
& dirname
)
230 OUString aPathURL
= dirname
.copy(0);
231 osl::FileBase::RC nError
;
234 osl::FileBase::getFileURLFromSystemPath(dirname
, aPathURL
); // convert if not full qualified URL
235 nError
= Directory::create(aPathURL
);
236 if ((nError
!= osl::FileBase::E_None
) && (nError
!= osl::FileBase::E_EXIST
))
237 printf("createTestDirectory failed!\n");
240 /** create a temp test directory using OUString name of full qualified URL or system path in a base directory.
242 static void createTestDirectory(const OUString
& basename
, const OUString
& dirname
)
244 OUString aBaseURL
= basename
.copy(0);
246 concatURL(aBaseURL
, dirname
);
247 createTestDirectory(aBaseURL
);
250 /** delete a temp test directory using OUString name of full qualified URL or system path.
252 static void deleteTestDirectory(const OUString
& dirname
)
254 OUString aPathURL
= dirname
.copy(0);
255 osl::FileBase::RC nError
;
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 nError
= Directory::remove(aPathURL
);
265 OString strError
= "In deleteTestDirectory function: remove Directory " +
266 OUStringToOString(aPathURL
, RTL_TEXTENCODING_ASCII_US
);
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
);
280 /** Check for the file and directory access right.
282 enum class oslCheckMode
{
288 /** check if the file exist
290 static bool ifFileExist(const OUString
& str
)
293 return (testFile
.open(osl_File_OpenFlag_Read
) == osl::FileBase::E_None
);
296 /** check if the file can be written
298 static bool ifFileCanWrite(const OUString
& str
)
300 // on Windows, the file has no write right, but can be written
302 bool bCheckResult
= false;
303 OUString aUStr
= str
.copy(0);
305 osl::FileBase::getSystemPathFromFileURL(str
, aUStr
);
307 OString aString
= OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
);
308 const char *path
= aString
.getStr();
309 if ((_access(path
, 2)) != -1)
310 bCheckResult
= sal_True
;
311 // on UNX, just test if open success with osl_File_OpenFlag_Write
314 bool bCheckResult
= (testFile
.open(osl_File_OpenFlag_Write
) == osl::FileBase::E_None
);
319 static bool checkDirectory(const OUString
& str
, oslCheckMode nCheckMode
)
323 osl::FileBase::RC rc
;
324 bool bCheckResult
= false;
329 if ((rc
!= osl::FileBase::E_NOENT
) && (rc
!= osl::FileBase::E_ACCES
))
333 case oslCheckMode::OpenAccess
:
334 if (rc
== osl::FileBase::E_None
)
337 case oslCheckMode::ReadAccess
:
338 rc
= aDir
.getNextItem(rItem
);
339 bCheckResult
= (rc
== osl::FileBase::E_None
) || (rc
== osl::FileBase::E_NOENT
);
341 case oslCheckMode::WriteAccess
:
342 ((aUString
+= str
) += aSlashURL
) += aTmpName2
;
343 if ((rc
= Directory::create(aUString
)) == osl::FileBase::E_None
)
346 rc
= Directory::remove(aUString
);
347 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, rc
);
351 bCheckResult
= false;
356 bCheckResult
= false;
360 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, rc
);
366 /** construct error message
368 static OString
outputError(const OString
& returnVal
, const OString
& rightVal
, const sal_Char
* msg
= "")
370 if (returnVal
== rightVal
)
373 OString aString
= msg
+
374 OStringLiteral(": the returned value is '") +
376 "', but the value should be '" +
382 /** Change file mode, two version in UNIX and Windows;.
384 #if (defined UNX) /* chmod() method is different in Windows */
385 static void changeFileMode(OUString
& filepath
, sal_Int32 mode
)
388 OUString aUStr
= filepath
.copy(0);
391 osl::FileBase::getSystemPathFromFileURL(filepath
, aUStr
);
393 aString
= OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
);
394 int ret
= chmod(aString
.getStr(), mode
);
395 CPPUNIT_ASSERT_EQUAL(0, ret
);
397 #else /* Windows version */
398 inline void changeFileMode(OUString
& filepath
, sal_Int32 mode
)
402 printf("this method is not implemented yet");
406 static OUString
getCurrentPID();
408 // Beginning of the test cases for osl::FileBase class
410 namespace osl_FileBase
412 // testing the method
413 // static inline RC getAbsoluteFileURL(const OUString& ustrBaseDirectoryURL,
414 // const OUString& ustrRelativeFileURL,
415 // OUString& ustrAbsoluteFileURL)
417 class getAbsoluteFileURL
: public CppUnit::TestFixture
420 void check_getAbsoluteFileURL(OUString
const& _suBaseURL
,
421 OString
const& _sRelativeURL
,
422 osl::FileBase::RC _nAssumeError
,
423 OUString
const& _suAssumeResultStr
);
425 void getAbsoluteFileURL_001_1();
426 void getAbsoluteFileURL_001_2();
427 void getAbsoluteFileURL_001_3();
428 void getAbsoluteFileURL_001_4();
429 void getAbsoluteFileURL_001_5();
430 void getAbsoluteFileURL_001_6();
431 void getAbsoluteFileURL_001_7();
432 void getAbsoluteFileURL_001_8();
433 void getAbsoluteFileURL_002();
434 void getAbsoluteFileURL_003();
435 void getAbsoluteFileURL_004();
437 CPPUNIT_TEST_SUITE(getAbsoluteFileURL
);
438 CPPUNIT_TEST(getAbsoluteFileURL_001_1
);
439 CPPUNIT_TEST(getAbsoluteFileURL_001_2
);
440 CPPUNIT_TEST(getAbsoluteFileURL_001_3
);
441 CPPUNIT_TEST(getAbsoluteFileURL_001_4
);
442 CPPUNIT_TEST(getAbsoluteFileURL_001_5
);
443 CPPUNIT_TEST(getAbsoluteFileURL_001_6
);
444 CPPUNIT_TEST(getAbsoluteFileURL_001_7
);
445 CPPUNIT_TEST(getAbsoluteFileURL_001_8
);
446 CPPUNIT_TEST(getAbsoluteFileURL_002
);
447 CPPUNIT_TEST(getAbsoluteFileURL_003
);
448 CPPUNIT_TEST(getAbsoluteFileURL_004
);
449 CPPUNIT_TEST_SUITE_END();
452 void getAbsoluteFileURL::check_getAbsoluteFileURL(OUString
const& _suBaseURL
,
453 OString
const& _sRelativeURL
,
454 osl::FileBase::RC _nAssumeError
,
455 OUString
const& _suAssumeResultStr
)
457 OUString suRelativeURL
= OStringToOUString(_sRelativeURL
, RTL_TEXTENCODING_UTF8
);
458 OString sBaseURL
= OUStringToOString(_suBaseURL
, RTL_TEXTENCODING_UTF8
);
459 OUString suResultURL
;
460 osl::FileBase::RC nError
= osl::FileBase::getAbsoluteFileURL(_suBaseURL
, suRelativeURL
, suResultURL
);
461 OString sResultURL
= OUStringToOString(suResultURL
, RTL_TEXTENCODING_UTF8
);
462 OString sError
= errorToString(nError
);
463 printf("getAbsoluteFileURL('%s','%s') deliver absolute URL: '%s', error '%s'\n",
464 sBaseURL
.getStr(), _sRelativeURL
.getStr(),sResultURL
.getStr(), sError
.getStr());
465 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: error number is wrong", _nAssumeError
, nError
);
467 if (nError
== osl::FileBase::E_None
)
469 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: ResultURL is not equal to expected URL ", _suAssumeResultStr
, suResultURL
);
473 void getAbsoluteFileURL::getAbsoluteFileURL_001_1()
475 OUString suAssume
= aUserDirectoryURL
.concat("/relative/file1");
476 check_getAbsoluteFileURL(aUserDirectoryURL
, "relative/file1",osl::FileBase::E_None
, suAssume
);
479 void getAbsoluteFileURL::getAbsoluteFileURL_001_2()
481 OUString suAssume
= aUserDirectoryURL
.concat("/relative/file2");
482 check_getAbsoluteFileURL(aUserDirectoryURL
, "relative/./file2",osl::FileBase::E_None
, suAssume
);
485 void getAbsoluteFileURL::getAbsoluteFileURL_001_3()
487 OUString suAssume
= aUserDirectoryURL
.concat("/file3");
488 check_getAbsoluteFileURL(aUserDirectoryURL
, "relative/../file3",osl::FileBase::E_None
, suAssume
);
491 void getAbsoluteFileURL::getAbsoluteFileURL_001_4()
493 OUString suAssume
= aUserDirectoryURL
.concat("/file4");
494 check_getAbsoluteFileURL(aUserDirectoryURL
, "././relative/../file4",osl::FileBase::E_None
, suAssume
);
497 void getAbsoluteFileURL::getAbsoluteFileURL_001_5()
501 suAssume
= aUserDirectoryURL
.concat("/relative/");
503 suAssume
= aUserDirectoryURL
.concat(OUString("/relative"));
505 check_getAbsoluteFileURL(aUserDirectoryURL
, "././relative/.",osl::FileBase::E_None
, suAssume
);
508 void getAbsoluteFileURL::getAbsoluteFileURL_001_6()
510 OUString suAssume
= aUserDirectoryURL
.concat("/.relative");
511 check_getAbsoluteFileURL(aUserDirectoryURL
, "./.relative",osl::FileBase::E_None
, suAssume
);
514 void getAbsoluteFileURL::getAbsoluteFileURL_001_7()
518 suAssume
= aUserDirectoryURL
.concat("/.a/");
520 suAssume
= aUserDirectoryURL
.concat(OUString("/.a"));
522 check_getAbsoluteFileURL(aUserDirectoryURL
, "./.a/mydir/..",osl::FileBase::E_None
, suAssume
);
525 void getAbsoluteFileURL::getAbsoluteFileURL_001_8()
527 OUString suAssume
= aUserDirectoryURL
.concat("/tmp/ok");
529 check_getAbsoluteFileURL(aUserDirectoryURL
, "tmp//ok",osl::FileBase::E_None
, suAssume
);
531 check_getAbsoluteFileURL(aUserDirectoryURL
, "tmp//ok",osl::FileBase::E_INVAL
, suAssume
);
535 void getAbsoluteFileURL::getAbsoluteFileURL_002()
537 #if (defined UNX) // Link is not defined in Windows
538 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
539 aUStr_LnkFileSys
+= aSlashURL
+ getCurrentPID() + "/link.file";
540 aUStr_SrcFileSys
+= aSlashURL
+ getCurrentPID() + "/canonical.name";
542 OString strLinkFileName
, strSrcFileName
;
543 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
544 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
546 createTestFile(aCanURL1
);
547 sal_Int32 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
548 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
549 OString sLnkURL
= OUStringToOString(aLnkURL1
, RTL_TEXTENCODING_ASCII_US
);
550 OUString suAssume
= aUserDirectoryURL
.concat("/canonical.name");
551 check_getAbsoluteFileURL(aUserDirectoryURL
, sLnkURL
, osl::FileBase::E_None
, suAssume
);
552 deleteTestFile(aCanURL1
);
553 fd
= remove(strLinkFileName
.getStr());
554 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
558 // please see line# 930
559 void getAbsoluteFileURL::getAbsoluteFileURL_003()
563 void getAbsoluteFileURL::getAbsoluteFileURL_004()
565 // create two level directories under $Temp/PID/
566 OUString aUStrUpBase
= aUserDirectoryURL
+ "/test1";
567 createTestDirectory(aUStrUpBase
);
568 OUString aUStrBase
= aUserDirectoryURL
+ "/test1/dir1";
569 createTestDirectory(aUStrBase
);
571 OUString suAssume
= aUserDirectoryURL
.concat("/mytestfile");
572 check_getAbsoluteFileURL(aUStrBase
, "../../mytestfile" , osl::FileBase::E_None
, suAssume
);
573 deleteTestDirectory(aUStrBase
);
574 deleteTestDirectory(aUStrUpBase
);
577 // testing two methods:
578 // static inline RC getSystemPathFromFileURL(const OUString& ustrFileURL,
579 // OUString& ustrSystemPath)
580 // static RC getFileURLFromSystemPath(const OUString & ustrSystemPath,
581 // OUString & ustrFileURL);
583 class SystemPath_FileURL
: public CppUnit::TestFixture
586 void getSystemPathFromFileURL_001_1();
587 void getSystemPathFromFileURL_001_2();
588 void getSystemPathFromFileURL_001_21();
589 void getSystemPathFromFileURL_001_22();
590 void getSystemPathFromFileURL_001_3();
591 void getSystemPathFromFileURL_001_31();
592 void getSystemPathFromFileURL_001_4();
593 void getSystemPathFromFileURL_001_41();
594 void getSystemPathFromFileURL_001_5();
595 void getSystemPathFromFileURL_001_51();
596 void getSystemPathFromFileURL_001_52();
597 void getSystemPathFromFileURL_001_53();
598 void getSystemPathFromFileURL_001_6();
599 void getSystemPathFromFileURL_001_61();
600 void getSystemPathFromFileURL_001_7();
601 void getSystemPathFromFileURL_001_71();
602 void getSystemPathFromFileURL_001_8();
603 void getSystemPathFromFileURL_001_81();
604 void getSystemPathFromFileURL_001_9();
605 void getSystemPathFromFileURL_001_91();
606 void getSystemPathFromFileURL_001_92();
607 void getSystemPathFromFileURL_004();
608 void getSystemPathFromFileURL_005();
610 // test case for getFileURLFromSystemPath
611 void getFileURLFromSystemPath_001();
612 void getFileURLFromSystemPath_002();
613 void getFileURLFromSystemPath_003();
614 void getFileURLFromSystemPath_004();
615 void getFileURLFromSystemPath_004_1();
616 void getFileURLFromSystemPath_005();
618 CPPUNIT_TEST_SUITE(SystemPath_FileURL
);
619 CPPUNIT_TEST(getSystemPathFromFileURL_001_1
);
620 CPPUNIT_TEST(getSystemPathFromFileURL_001_2
);
621 CPPUNIT_TEST(getSystemPathFromFileURL_001_21
);
622 CPPUNIT_TEST(getSystemPathFromFileURL_001_22
);
623 CPPUNIT_TEST(getSystemPathFromFileURL_001_3
);
624 CPPUNIT_TEST(getSystemPathFromFileURL_001_31
);
625 CPPUNIT_TEST(getSystemPathFromFileURL_001_4
);
626 CPPUNIT_TEST(getSystemPathFromFileURL_001_41
);
627 CPPUNIT_TEST(getSystemPathFromFileURL_001_5
);
628 CPPUNIT_TEST(getSystemPathFromFileURL_001_51
);
629 CPPUNIT_TEST(getSystemPathFromFileURL_001_52
);
630 CPPUNIT_TEST(getSystemPathFromFileURL_001_53
);
631 CPPUNIT_TEST(getSystemPathFromFileURL_001_6
);
632 CPPUNIT_TEST(getSystemPathFromFileURL_001_61
);
633 CPPUNIT_TEST(getSystemPathFromFileURL_001_7
);
634 CPPUNIT_TEST(getSystemPathFromFileURL_001_71
);
635 CPPUNIT_TEST(getSystemPathFromFileURL_001_8
);
636 CPPUNIT_TEST(getSystemPathFromFileURL_001_81
);
637 CPPUNIT_TEST(getSystemPathFromFileURL_001_9
);
638 CPPUNIT_TEST(getSystemPathFromFileURL_001_91
);
639 CPPUNIT_TEST(getSystemPathFromFileURL_001_92
);
640 CPPUNIT_TEST(getSystemPathFromFileURL_004
);
641 CPPUNIT_TEST(getSystemPathFromFileURL_005
);
642 CPPUNIT_TEST(getFileURLFromSystemPath_001
);
643 CPPUNIT_TEST(getFileURLFromSystemPath_002
);
644 CPPUNIT_TEST(getFileURLFromSystemPath_003
);
645 CPPUNIT_TEST(getFileURLFromSystemPath_004
);
646 CPPUNIT_TEST(getFileURLFromSystemPath_004_1
);
647 CPPUNIT_TEST(getFileURLFromSystemPath_005
);
648 CPPUNIT_TEST_SUITE_END();
651 void check_SystemPath_FileURL(
652 OString
const& _sSource
,
653 osl::FileBase::RC _nAssumeError
,
654 OString
const& _sAssumeResultStr
,
655 bool bDirection
= true);
657 void checkWNTBehaviour_getSystemPathFromFileURL(
658 OString
const& _sURL
,
659 osl::FileBase::RC _nAssumeError
,
660 OString
const& _sWNTAssumeResultString
);
662 void checkUNXBehaviour_getSystemPathFromFileURL(
663 OString
const& _sURL
,
664 osl::FileBase::RC _nAssumeError
,
665 OString
const& _sUnixAssumeResultString
);
667 void checkWNTBehaviour_getFileURLFromSystemPath(OString
const& _sSysPath
,
668 osl::FileBase::RC _nAssumeError
,
669 OString
const& _sWNTAssumeResultString
);
671 void checkUNXBehaviour_getFileURLFromSystemPath(
672 OString
const& _sSysPath
,
673 osl::FileBase::RC _nAssumeError
,
674 OString
const& _sUnixAssumeResultString
);
678 // if bDirection==sal_True, check getSystemPathFromFileURL
679 // if bDirection==sal_False, check getFileURLFromSystemPath
680 void SystemPath_FileURL::check_SystemPath_FileURL(
681 OString
const& _sSource
,
682 osl::FileBase::RC _nAssumeError
,
683 OString
const& _sAssumeResultStr
,
686 // PRE: URL as String
689 suSource
= OStringToOUString(_sSource
, RTL_TEXTENCODING_UTF8
);
690 osl::FileBase::RC nError
;
693 nError
= osl::FileBase::getSystemPathFromFileURL(suSource
, suStr
);
695 nError
= osl::FileBase::getFileURLFromSystemPath(suSource
, suStr
);
697 // if the given string is gt length 0,
698 // we check also this string
699 OString sStr
= OUStringToOString(suStr
, RTL_TEXTENCODING_UTF8
);
700 OString sError
= errorToString(nError
);
703 printf("getSystemPathFromFileURL('%s') deliver system path: '%s', error '%s'\n",
704 _sSource
.getStr(), sStr
.getStr(), sError
.getStr());
706 printf("getFileURLFromSystemPath('%s') deliver File URL: '%s', error '%s'\n",
707 _sSource
.getStr(), sStr
.getStr(), sError
.getStr());
709 if (!_sAssumeResultStr
.isEmpty())
711 bool bStrAreEqual
= _sAssumeResultStr
== sStr
;
712 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong",
713 _nAssumeError
, nError
);
714 CPPUNIT_ASSERT_MESSAGE("Assumption is wrong",
719 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong", _nAssumeError
, nError
);
723 void SystemPath_FileURL::checkWNTBehaviour_getSystemPathFromFileURL(
724 OString
const& _sURL
,
725 osl::FileBase::RC _nAssumeError
,
726 OString
const& _sWNTAssumeResultString
)
729 check_SystemPath_FileURL(_sURL
, _nAssumeError
, _sWNTAssumeResultString
);
733 (void)_sWNTAssumeResultString
;
737 void SystemPath_FileURL::checkUNXBehaviour_getSystemPathFromFileURL(
738 OString
const& _sURL
,
739 osl::FileBase::RC _nAssumeError
,
740 OString
const& _sUnixAssumeResultString
)
743 check_SystemPath_FileURL(_sURL
, _nAssumeError
, _sUnixAssumeResultString
);
747 (void)_sUnixAssumeResultString
;
751 void SystemPath_FileURL::checkWNTBehaviour_getFileURLFromSystemPath(
752 OString
const& _sSysPath
,
753 osl::FileBase::RC _nAssumeError
,
754 OString
const& _sWNTAssumeResultString
)
757 check_SystemPath_FileURL(_sSysPath
, _nAssumeError
, _sWNTAssumeResultString
, sal_False
);
761 (void)_sWNTAssumeResultString
;
765 void SystemPath_FileURL::checkUNXBehaviour_getFileURLFromSystemPath(
766 OString
const& _sSysPath
,
767 osl::FileBase::RC _nAssumeError
,
768 OString
const& _sUnixAssumeResultString
)
771 check_SystemPath_FileURL(_sSysPath
, _nAssumeError
, _sUnixAssumeResultString
, false);
775 (void)_sUnixAssumeResultString
;
779 /** Test for getSystemPathFromFileURL()
780 this test is split into 2 different OS tests,
781 the first function checkUNXBehaviour... runs only on Unix based Systems,
782 the second only on windows based systems
783 the first parameter are a file URL where we want to get the system path of,
784 the second parameter is the assumed error of the osl_getSystemPathFromFileURL() function,
785 the third parameter is the assumed result string, the string will only test, if its length is greater 0
788 void SystemPath_FileURL::getSystemPathFromFileURL_001_1()
791 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
792 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
795 void SystemPath_FileURL::getSystemPathFromFileURL_001_2()
798 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
799 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "\\");
802 void SystemPath_FileURL::getSystemPathFromFileURL_001_21()
804 /* From RFC3986, "2.2. Reserved Characters":
806 "The purpose of reserved characters is to provide a set of delimiting
807 characters that are distinguishable from other data within a URI.
808 URIs that differ in the replacement of a reserved character with its
809 corresponding percent-encoded octet are not equivalent. Percent-
810 encoding a reserved character, or decoding a percent-encoded octet
811 that corresponds to a reserved character, will change how the URI is
812 interpreted by most applications. Thus, characters in the reserved
813 set are protected from normalization and are therefore safe to be
814 used by scheme-specific and producer-specific algorithms for
815 delimiting data subcomponents within a URI."
817 In other words, %2F ("/") is NOT the same as /.
820 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
821 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
824 void SystemPath_FileURL::getSystemPathFromFileURL_001_22()
826 OString
sURL("file:///tmp%2Fmydir");
827 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
828 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
831 void SystemPath_FileURL::getSystemPathFromFileURL_001_3()
834 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "a");
835 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "a");
838 void SystemPath_FileURL::getSystemPathFromFileURL_001_31()
840 OString
sURL("tmpname");
841 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "tmpname");
842 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "tmpname");
845 void SystemPath_FileURL::getSystemPathFromFileURL_001_4()
847 OString
sURL("file://");
848 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "");
849 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
852 void SystemPath_FileURL::getSystemPathFromFileURL_001_41()
854 OString
sURL("file://localhost/tmp");
855 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "");
856 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
859 void SystemPath_FileURL::getSystemPathFromFileURL_001_5()
861 OString
sURL("file:///tmp");
862 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp");
863 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
866 void SystemPath_FileURL::getSystemPathFromFileURL_001_51()
869 OString
sURL("file://c:/tmp");
870 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:/tmp"); // this is may be a BUG
871 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
875 void SystemPath_FileURL::getSystemPathFromFileURL_001_52()
877 OString
sURL("file:///c:/tmp");
878 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp");
879 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp");
882 void SystemPath_FileURL::getSystemPathFromFileURL_001_53()
884 // is this a legal file path?
885 OString
sURL("file:///c|/tmp");
886 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c|/tmp");
887 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp");
890 void SystemPath_FileURL::getSystemPathFromFileURL_001_6()
892 OString
sURL("file:///tmp/first");
893 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp/first");
894 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
897 void SystemPath_FileURL::getSystemPathFromFileURL_001_61()
899 OString
sURL("file:///c:/tmp/first");
900 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp/first");
901 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp\\first");
904 void SystemPath_FileURL::getSystemPathFromFileURL_001_7()
906 OString
sURL("file:///tmp/../second");
907 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp/../second"); // may be a BUG
908 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
911 void SystemPath_FileURL::getSystemPathFromFileURL_001_71()
913 OString
sURL("file:///c:/tmp/../second");
914 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp/../second");
915 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp\\..\\second");
918 void SystemPath_FileURL::getSystemPathFromFileURL_001_8()
920 OString
sURL("../tmp");
921 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "../tmp");
922 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "..\\tmp");
925 void SystemPath_FileURL::getSystemPathFromFileURL_001_81()
928 OString
sURL("file://~/tmp");
930 home_path
= getenv("HOME");
931 OString
expResult(home_path
);
933 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, expResult
);
937 void SystemPath_FileURL::getSystemPathFromFileURL_001_9()
939 OString
sURL("file:///tmp/first%20second");
940 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/tmp/first second");
941 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
944 void SystemPath_FileURL::getSystemPathFromFileURL_001_91()
946 OString
sURL("file:///c:/tmp/first%20second");
947 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "/c:/tmp/first second");
948 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "c:\\tmp\\first second");
951 void SystemPath_FileURL::getSystemPathFromFileURL_001_92()
954 OString
sURL("ca@#;+.,$///78no%01ni..name");
955 checkUNXBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_None
, "");
956 checkWNTBehaviour_getSystemPathFromFileURL(sURL
, osl::FileBase::E_INVAL
, "");
961 void SystemPath_FileURL::getSystemPathFromFileURL_004()
964 OUString
aUNormalURL(aTmpName6
);
965 OUString
aUResultURL (aSysPath4
);
966 osl::FileBase::RC nError
= osl::FileBase::getSystemPathFromFileURL(aUNormalURL
, aUStr
);
968 bool bOk
= compareFileName(aUStr
, aUResultURL
);
971 "test for getSystemPathFromFileURL(' " +
972 OUStringToOString(aUNormalURL
, RTL_TEXTENCODING_ASCII_US
) +
973 " ') function:use an absolute file URL, " +
974 outputError(OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
),
975 OUStringToOString(aUResultURL
, RTL_TEXTENCODING_ASCII_US
));
977 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_None
, nError
);
978 CPPUNIT_ASSERT_MESSAGE(sError
.getStr(), bOk
);
982 // CJK characters case
983 void SystemPath_FileURL::getSystemPathFromFileURL_005()
986 createTestDirectory(aTmpName10
);
987 OUString
aUNormalURL(aTmpName10
);
988 OUString
aUResultURL (aSysPath5
);
990 osl::FileBase::RC nError
= osl::FileBase::getSystemPathFromFileURL(aUNormalURL
, aUStr
);
992 bool bOk
= compareFileName(aUStr
, aUResultURL
);
995 "test for getSystemPathFromFileURL(' " +
996 OUStringToOString(aUNormalURL
, RTL_TEXTENCODING_ASCII_US
) +
997 " ') function:use a CJK coded absolute URL, " +
998 outputError(OUStringToOString(aUStr
, RTL_TEXTENCODING_ASCII_US
),
999 OUStringToOString(aUResultURL
, RTL_TEXTENCODING_ASCII_US
));
1000 deleteTestDirectory(aTmpName10
);
1002 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_None
, nError
);
1003 CPPUNIT_ASSERT_MESSAGE(sError
.getStr(), bOk
);
1006 void SystemPath_FileURL::getFileURLFromSystemPath_001()
1008 OString
sSysPath("~/tmp");
1010 home_path
= getenv("HOME");
1011 OString
expResult(home_path
);
1012 expResult
= "file://"+ expResult
+ "/tmp";
1013 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, expResult
);
1014 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "~/tmp");
1017 void SystemPath_FileURL::getFileURLFromSystemPath_002()
1019 OString
sSysPath("c:/tmp");
1020 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "c:/tmp");
1021 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "file:///c:/tmp");
1024 void SystemPath_FileURL::getFileURLFromSystemPath_003()
1026 OString
sSysPath("file:///temp");
1027 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1028 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1031 void SystemPath_FileURL::getFileURLFromSystemPath_004()
1033 OString
sSysPath("//tmp//first start");
1034 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "file:///tmp/first%20start");
1035 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1038 void SystemPath_FileURL::getFileURLFromSystemPath_004_1()
1040 OString
sSysPath("/tmp///first start");
1041 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_None
, "file:///tmp/first%20start");
1042 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1045 void SystemPath_FileURL::getFileURLFromSystemPath_005()
1047 OString
sSysPath("");
1048 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1049 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath
, osl::FileBase::E_INVAL
, "");
1052 // testing the method
1053 // static inline RC searchFileURL( const OUString& ustrFileName,
1054 // const OUString& ustrSearchPath,
1055 // OUString& ustrFileURL)
1057 class searchFileURL
: public CppUnit::TestFixture
1061 osl::FileBase::RC nError1
, nError2
, nError3
,nError4
;
1065 : nError1(osl::FileBase::E_None
)
1066 , nError2(osl::FileBase::E_None
)
1067 , nError3(osl::FileBase::E_None
)
1068 , nError4(osl::FileBase::E_None
) {}
1070 void searchFileURL_001()
1072 /* search file is passed by system filename */
1073 nError1
= osl::FileBase::searchFileURL(aTmpName1
, aUserDirectorySys
, aUStr
);
1074 /* search file is passed by full qualified file URL */
1075 nError2
= osl::FileBase::searchFileURL(aCanURL1
, aUserDirectorySys
, aUStr
);
1076 /* search file is passed by relative file path */
1077 nError3
= osl::FileBase::searchFileURL(aRelURL4
, aUserDirectorySys
, aUStr
);
1079 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) ",
1080 osl::FileBase::E_NOENT
, nError1
);
1081 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) ",
1082 osl::FileBase::E_NOENT
, nError2
);
1083 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) ",
1084 osl::FileBase::E_NOENT
, nError3
);
1087 void searchFileURL_002()
1089 /* search file is passed by system filename */
1090 nError1
= osl::FileBase::searchFileURL(aTempDirectorySys
, aRootSys
, aUStr
);
1091 bool bOk1
= compareFileName(aUStr
, aTempDirectoryURL
);
1092 /* search file is passed by full qualified file URL */
1093 nError2
= osl::FileBase::searchFileURL(aTempDirectoryURL
, aRootSys
, aUStr
);
1094 bool bOk2
= compareFileName(aUStr
, aTempDirectoryURL
);
1095 /* search file is passed by relative file path */
1096 nError3
= osl::FileBase::searchFileURL(aRelURL5
, aRootSys
, aUStr
);
1097 bool bOk3
= compareFileName(aUStr
, aTempDirectoryURL
);
1098 /* search file is passed by an exist file */
1099 createTestFile(aCanURL1
);
1100 nError4
= osl::FileBase::searchFileURL(aCanURL4
, aUserDirectorySys
, aUStr
);
1101 bool bOk4
= compareFileName(aUStr
, aCanURL1
);
1102 deleteTestFile(aCanURL1
);
1104 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1105 osl::FileBase::E_None
, nError1
);
1106 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1107 osl::FileBase::E_None
, nError2
);
1108 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1109 osl::FileBase::E_None
, nError3
);
1110 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1111 osl::FileBase::E_None
, nError4
);
1112 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1114 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1116 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1118 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1122 void searchFileURL_003()
1124 OUString
aSystemPathList(TEST_PLATFORM_ROOT
":" TEST_PLATFORM_ROOT TEST_PLATFORM_TEMP
":" TEST_PLATFORM_ROOT
"system/path");
1125 nError1
= osl::FileBase::searchFileURL(aUserDirectoryURL
, aSystemPathList
, aUStr
);
1126 bool bOk
= compareFileName(aUStr
, aUserDirectoryURL
);
1127 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1128 osl::FileBase::E_None
, nError1
);
1129 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1133 void searchFileURL_004()
1135 OUString
aSystemPathList(TEST_PLATFORM_ROOT PATH_LIST_DELIMITER TEST_PLATFORM_ROOT TEST_PLATFORM_TEMP PATH_LIST_DELIMITER TEST_PLATFORM_ROOT
"system/path/../name");
1136 nError1
= osl::FileBase::searchFileURL(aUserDirectoryURL
, aSystemPathList
, aUStr
);
1137 bool bOk
= compareFileName(aUStr
, aUserDirectoryURL
);
1138 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1139 osl::FileBase::E_None
, nError1
);
1140 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1144 void searchFileURL_005()
1146 nError1
= osl::FileBase::searchFileURL(aUserDirectoryURL
, aNullURL
, aUStr
);
1147 bool bOk
= compareFileName(aUStr
, aUserDirectoryURL
);
1148 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is NULL",
1149 osl::FileBase::E_None
, nError1
);
1150 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is NULL",
1154 CPPUNIT_TEST_SUITE(searchFileURL
);
1155 CPPUNIT_TEST(searchFileURL_001
);
1156 CPPUNIT_TEST(searchFileURL_002
);
1157 CPPUNIT_TEST(searchFileURL_003
);
1158 CPPUNIT_TEST(searchFileURL_004
);
1159 CPPUNIT_TEST(searchFileURL_005
);
1160 CPPUNIT_TEST_SUITE_END();
1163 // testing the method
1164 // static inline RC getTempDirURL(OUString& ustrTempDirURL)
1166 class getTempDirURL
: public CppUnit::TestFixture
1170 osl::FileBase::RC nError
;
1173 getTempDirURL() : nError(osl::FileBase::E_None
) {}
1175 void setUp() override
1177 nError
= osl::FileBase::getTempDirURL(aUStr
);
1180 void getTempDirURL_001()
1183 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getTempDirURL function: execution",
1184 osl::FileBase::E_None
, nError
);
1187 void getTempDirURL_002()
1189 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1190 checkDirectory(aUStr
, oslCheckMode::OpenAccess
));
1191 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1192 checkDirectory(aUStr
, oslCheckMode::ReadAccess
));
1193 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1194 checkDirectory(aUStr
, oslCheckMode::WriteAccess
));
1197 CPPUNIT_TEST_SUITE(getTempDirURL
);
1198 CPPUNIT_TEST(getTempDirURL_001
);
1199 CPPUNIT_TEST(getTempDirURL_002
);
1200 CPPUNIT_TEST_SUITE_END();
1203 // testing the method
1204 // static inline RC createTempFile(OUString* pustrDirectoryURL,
1205 // oslFileHandle* pHandle,
1206 // OUString* pustrTempFileURL)
1208 class createTempFile
: public CppUnit::TestFixture
1211 osl::FileBase::RC nError1
, nError2
;
1214 std::unique_ptr
<oslFileHandle
> pHandle
;
1215 std::unique_ptr
<OUString
> pUStr_DirURL
;
1216 std::unique_ptr
<OUString
> pUStr_FileURL
;
1220 : nError1(osl::FileBase::E_None
)
1221 , nError2(osl::FileBase::E_None
)
1226 void setUp() override
1228 pHandle
.reset(new oslFileHandle());
1229 pUStr_DirURL
.reset(new OUString(aUserDirectoryURL
));
1230 pUStr_FileURL
.reset(new OUString());
1233 void tearDown() override
1235 pUStr_DirURL
.reset();
1236 pUStr_FileURL
.reset();
1240 void createTempFile_001()
1242 nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), pHandle
.get(), pUStr_FileURL
.get());
1243 File
testFile(*pUStr_FileURL
);
1244 nError2
= testFile
.open(osl_File_OpenFlag_Create
);
1246 if (nError2
== osl::FileBase::E_EXIST
)
1248 osl_closeFile(*pHandle
);
1249 deleteTestFile(*pUStr_FileURL
);
1252 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1253 osl::FileBase::E_None
, nError1
);
1254 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: create temp file and test the existence",
1255 (pHandle
!= nullptr));
1256 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1257 osl::FileBase::E_EXIST
, nError2
);
1260 void createTempFile_002()
1263 nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), pHandle
.get(), pUStr_FileURL
.get());
1264 File
testFile(*pUStr_FileURL
);
1265 nError2
= testFile
.open(osl_File_OpenFlag_Create
);
1267 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1268 osl::FileBase::E_None
, nError1
);
1269 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1270 (pHandle
!= nullptr));
1271 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1272 osl::FileBase::E_EXIST
, nError2
);
1274 // check file if have the write permission
1275 if (nError2
== osl::FileBase::E_EXIST
)
1277 bOK
= ifFileCanWrite(*pUStr_FileURL
);
1278 osl_closeFile(*pHandle
);
1279 deleteTestFile(*pUStr_FileURL
);
1282 CPPUNIT_ASSERT_MESSAGE("test for open and write access rights, in (W32), it did not have write access right, but it should be writable.",
1286 void createTempFile_003()
1288 nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), pHandle
.get(), nullptr);
1289 // the temp file will be removed when return from createTempFile
1290 bOK
= (pHandle
!= nullptr && nError1
== osl::FileBase::E_None
);
1292 osl_closeFile(*pHandle
);
1294 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1295 osl::FileBase::E_None
, nError1
);
1296 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1300 void createTempFile_004()
1302 nError1
= osl::FileBase::createTempFile(pUStr_DirURL
.get(), nullptr, pUStr_FileURL
.get());
1303 bOK
= (pUStr_FileURL
!= nullptr);
1304 CPPUNIT_ASSERT(bOK
);
1305 File
testFile(*pUStr_FileURL
);
1306 nError2
= testFile
.open(osl_File_OpenFlag_Create
);
1307 deleteTestFile(*pUStr_FileURL
);
1308 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1309 osl::FileBase::E_None
, nError1
);
1310 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1311 osl::FileBase::E_EXIST
, nError2
);
1312 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1317 CPPUNIT_TEST_SUITE(createTempFile
);
1318 CPPUNIT_TEST(createTempFile_001
);
1319 CPPUNIT_TEST(createTempFile_002
);
1320 CPPUNIT_TEST(createTempFile_003
);
1321 CPPUNIT_TEST(createTempFile_004
);
1322 CPPUNIT_TEST_SUITE_END();
1325 // FIXME: remove the _disabled to enable:
1326 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getAbsoluteFileURL
, "osl_osl::FileBase_disabled");
1327 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::SystemPath_FileURL
, "osl_osl::FileBase");
1328 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::searchFileURL
, "osl_osl::FileBase_disabled");
1329 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getTempDirURL
, "osl_osl::FileBase_disabled");
1330 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::createTempFile
, "osl_osl::FileBase_disabled");
1332 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_osl::FileBase");
1335 namespace osl_FileStatus
1337 // testing the method
1338 // FileStatus(sal_uInt32 nMask): _nMask(nMask)
1339 class ctors
: public CppUnit::TestFixture
1343 osl::FileBase::RC nError1
;
1344 DirectoryItem rItem
;
1347 ctors() : nError1(osl::FileBase::E_None
) {}
1349 void setUp() override
1351 // create a tempfile in $TEMP/tmpdir/tmpname.
1352 createTestDirectory(aTmpName3
);
1353 createTestFile(aTmpName4
);
1355 Directory
aDir(aTmpName3
);
1356 nError1
= aDir
.open();
1357 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1358 nError1
= aDir
.getNextItem(rItem
);
1359 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1363 void tearDown() override
1365 // remove the tempfile in $TEMP/tmpdir/tmpname.
1366 deleteTestFile(aTmpName4
);
1367 deleteTestDirectory(aTmpName3
);
1372 FileStatus
rFileStatus(osl_FileStatus_Mask_All
);
1373 nError1
= rItem
.getFileStatus(rFileStatus
);
1374 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1375 aUStr
= rFileStatus
.getFileName();
1377 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask all and see the file name",
1378 compareFileName(aUStr
, aTmpName2
));
1383 FileStatus
rFileStatus(0);
1384 nError1
= rItem
.getFileStatus(rFileStatus
);
1385 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1386 aUStr
= rFileStatus
.getFileName();
1388 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask is empty",
1389 compareFileName(aUStr
, aNullURL
));
1392 CPPUNIT_TEST_SUITE(ctors
);
1393 CPPUNIT_TEST(ctors_001
);
1394 CPPUNIT_TEST(ctors_002
);
1395 CPPUNIT_TEST_SUITE_END();
1398 // testing the method
1399 // inline sal_Bool isValid(sal_uInt32 nMask) const
1401 class isValid
: public CppUnit::TestFixture
1404 std::unique_ptr
<Directory
> pDir
;
1405 DirectoryItem rItem_file
, rItem_link
;
1412 void setUp() override
1414 // create a tempfile in $TEMP/tmpdir/tmpname.
1415 createTestDirectory(aTmpName3
);
1416 createTestFile(aTmpName4
);
1418 pDir
.reset(new Directory(aTmpName3
));
1419 osl::FileBase::RC nError1
= pDir
->open();
1420 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1421 nError1
= pDir
->getNextItem(rItem_file
, 1);
1422 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1425 void tearDown() override
1427 osl::FileBase::RC nError1
= pDir
->close();
1429 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
1431 // remove the tempfile in $TEMP/tmpdir/tmpname.
1432 deleteTestFile(aTmpName4
);
1433 deleteTestDirectory(aTmpName3
);
1438 sal_uInt32 mask
= 0;
1439 FileStatus
rFileStatus(mask
);
1440 osl::FileBase::RC nError1
= rItem_file
.getFileStatus(rFileStatus
);
1441 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1442 bool bOk
= rFileStatus
.isValid(mask
);
1444 CPPUNIT_ASSERT_MESSAGE("test for isValid function: no fields specified", bOk
);
1447 void check_FileStatus(FileStatus
const& _aStatus
)
1450 if (_aStatus
.isValid(osl_FileStatus_Mask_Type
))
1452 if (_aStatus
.isValid(osl_FileStatus_Mask_Attributes
))
1453 sStat
+= "attributes ";
1454 if (_aStatus
.isValid(osl_FileStatus_Mask_CreationTime
))
1456 if (_aStatus
.isValid(osl_FileStatus_Mask_AccessTime
))
1458 if (_aStatus
.isValid(osl_FileStatus_Mask_ModifyTime
))
1460 if (_aStatus
.isValid(osl_FileStatus_Mask_FileSize
))
1461 sStat
+= "filesize ";
1462 if (_aStatus
.isValid(osl_FileStatus_Mask_FileName
))
1463 sStat
+= "filename ";
1464 if (_aStatus
.isValid(osl_FileStatus_Mask_FileURL
))
1465 sStat
+= "fileurl ";
1466 printf("mask: %s\n", sStat
.getStr());
1471 createTestFile(aTmpName6
);
1472 sal_uInt32 mask_file
= osl_FileStatus_Mask_Type
|
1473 osl_FileStatus_Mask_Attributes
|
1474 osl_FileStatus_Mask_CreationTime
|
1475 osl_FileStatus_Mask_AccessTime
|
1476 osl_FileStatus_Mask_ModifyTime
|
1477 osl_FileStatus_Mask_FileSize
|
1478 osl_FileStatus_Mask_FileName
|
1479 osl_FileStatus_Mask_FileURL
;
1481 FileStatus
rFileStatus(mask_file
);
1482 DirectoryItem::get(aTmpName6
, rItem_file
);
1483 osl::FileBase::RC nError1
= rItem_file
.getFileStatus(rFileStatus
);
1485 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
1487 check_FileStatus(rFileStatus
);
1488 deleteTestFile(aTmpName6
);
1492 /** Check if is a valid linked file.
1494 Link is not defined in Windows, and on Linux, we can not get the directory item of the linked file.
1495 We have to defer to filesystems, normal filesystems support links (EXT2, ...), castrated filesystems
1496 don't have links (FAT, FAT32) and Windows NT NTFS support links, but the Windows API doesn't :-(
1503 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
1504 aUStr_LnkFileSys
+= aSlashURL
+ getCurrentPID() + "/tmpdir/link.file";
1505 aUStr_SrcFileSys
+= aSlashURL
+ getCurrentPID() + "/tmpdir/tmpname";
1507 OString strLinkFileName
;
1508 OString strSrcFileName
;
1509 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
1510 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
1512 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
1513 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
1514 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
1516 // testDirectory is "/tmp/PID/tmpdir/"
1517 Directory
testDirectory(aTmpName3
);
1518 testDirectory
.open();
1519 OUString
aFileName ("link.file");
1523 osl::FileBase::RC nError1
= testDirectory
.getNextItem(rItem_link
, 4);
1525 if (nError1
== osl::FileBase::E_None
)
1527 sal_uInt32 mask_link
= osl_FileStatus_Mask_FileName
| osl_FileStatus_Mask_LinkTargetURL
;
1528 FileStatus
rFileStatus(mask_link
);
1529 rItem_link
.getFileStatus(rFileStatus
);
1531 if (compareFileName(rFileStatus
.getFileName(), aFileName
))
1533 if (rFileStatus
.isValid(osl_FileStatus_Mask_LinkTargetURL
))
1546 fd
= remove(strLinkFileName
.getStr());
1547 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
1549 CPPUNIT_ASSERT_MESSAGE("test for isValid function: link file, check for LinkTargetURL", bOk
);
1555 sal_uInt32 mask_file_all
= osl_FileStatus_Mask_All
;
1556 FileStatus
rFileStatus_all(mask_file_all
);
1557 osl::FileBase::RC nError1
= rItem_file
.getFileStatus(rFileStatus_all
);
1558 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1560 check_FileStatus(rFileStatus_all
);
1562 sal_uInt32 mask_file_val
= osl_FileStatus_Mask_Validate
;
1563 FileStatus
rFileStatus_val(mask_file_val
);
1564 nError1
= rItem_file
.getFileStatus(rFileStatus_val
);
1565 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1567 check_FileStatus(rFileStatus_val
);
1570 CPPUNIT_TEST_SUITE(isValid
);
1571 CPPUNIT_TEST(isValid_001
);
1572 CPPUNIT_TEST(isValid_002
);
1573 CPPUNIT_TEST(isValid_003
);
1574 CPPUNIT_TEST(isValid_004
);
1575 CPPUNIT_TEST_SUITE_END();
1578 // testing the method
1579 // inline Type getFileType() const
1581 class getFileType
: public CppUnit::TestFixture
1584 osl::FileBase::RC nError1
;
1586 DirectoryItem m_aItem_1
, m_aItem_2
, m_aVolumeItem
, m_aFifoItem
;
1587 DirectoryItem m_aLinkItem
, m_aSocketItem
, m_aSpecialItem
;
1590 getFileType() : nError1(osl::FileBase::E_None
) {}
1592 void setUp() override
1594 // create a tempfile: $TEMP/tmpdir/tmpname.
1595 // a tempdirectory: $TEMP/tmpdir/tmpdir.
1596 // use $ROOT/staroffice as volume ---> use dev/fd as volume.
1597 // and get their directory item.
1598 createTestDirectory(aTmpName3
);
1599 createTestFile(aTmpName3
, aTmpName2
);
1600 createTestDirectory(aTmpName3
, aTmpName1
);
1602 std::unique_ptr
<Directory
> xDir(new Directory(aTmpName3
));
1603 nError1
= xDir
->open();
1604 CPPUNIT_ASSERT_EQUAL_MESSAGE("open aTmpName3 failed!", osl::FileBase::E_None
, nError1
);
1605 // getNextItem can not assure which item retrieved
1606 nError1
= xDir
->getNextItem(m_aItem_1
, 1);
1607 CPPUNIT_ASSERT_EQUAL_MESSAGE("get first item failed!", osl::FileBase::E_None
, nError1
);
1609 nError1
= xDir
->getNextItem(m_aItem_2
);
1610 CPPUNIT_ASSERT_EQUAL_MESSAGE("get second item failed!", osl::FileBase::E_None
, nError1
);
1612 // FIXME mindy: failed on my RH9, so removed temporarily
1613 // nError1 = DirectoryItem::get(aVolURL2, m_aVolumeItem);
1614 // CPPUNIT_ASSERT_MESSAGE("get volume item failed!", osl::FileBase::E_None == nError1);
1617 void tearDown() override
1619 // remove all in $TEMP/tmpdir.
1620 deleteTestDirectory(aTmpName3
, aTmpName1
);
1621 deleteTestFile(aTmpName3
, aTmpName2
);
1622 deleteTestDirectory(aTmpName3
);
1625 void getFileType_001()
1627 FileStatus
rFileStatus(osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileName
);
1628 nError1
= m_aItem_1
.getFileStatus(rFileStatus
);
1629 CPPUNIT_ASSERT_EQUAL_MESSAGE("getFileStatus failed", osl::FileBase::E_None
, nError1
);
1631 check_FileType(rFileStatus
);
1634 void check_FileType(osl::FileStatus
const& _rFileStatus
)
1636 if (_rFileStatus
.isValid(osl_FileStatus_Mask_FileName
))
1638 OUString suFilename
= _rFileStatus
.getFileName();
1640 if (_rFileStatus
.isValid(osl_FileStatus_Mask_Type
))
1642 osl::FileStatus::Type eType
= _rFileStatus
.getFileType();
1645 if (compareFileName(suFilename
, aTmpName2
))
1646 bOK
= (eType
== osl::FileStatus::Regular
);
1648 if (compareFileName(suFilename
, aTmpName1
))
1649 bOK
= (eType
== FileStatus::Directory
);
1651 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: ", bOK
);
1654 // LLA: it's not a bug, if a FileStatus not exist, so no else
1657 void getFileType_002()
1659 FileStatus
rFileStatus(osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileName
);
1660 nError1
= m_aItem_2
.getFileStatus(rFileStatus
);
1662 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1663 check_FileType(rFileStatus
);
1666 void getFileType_003()
1670 void getFileType_007()
1672 #if defined(__sun) // Special file is different in Windows
1673 nError1
= DirectoryItem::get(aTypeURL2
, m_aSpecialItem
);
1674 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1676 // check for File type
1677 FileStatus
rFileStatus(osl_FileStatus_Mask_Type
);
1678 nError1
= m_aSpecialItem
.getFileStatus(rFileStatus
);
1679 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
1681 if (rFileStatus
.isValid(osl_FileStatus_Mask_Type
))
1683 osl::FileStatus::Type eType
= rFileStatus
.getFileType();
1685 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: Special, Solaris version ",
1686 (eType
== FileStatus::Special
));
1691 CPPUNIT_TEST_SUITE(getFileType
);
1692 CPPUNIT_TEST(getFileType_001
);
1693 CPPUNIT_TEST(getFileType_002
);
1694 CPPUNIT_TEST(getFileType_003
);
1695 CPPUNIT_TEST(getFileType_007
);
1696 CPPUNIT_TEST_SUITE_END();
1699 // testing the method
1700 // inline sal_uInt64 getAttributes() const
1702 class getAttributes
: public CppUnit::TestFixture
1705 OUString aTypeURL
, aTypeURL_Hid
;
1706 osl::FileBase::RC nError
;
1707 DirectoryItem rItem
, rItem_hidden
;
1710 getAttributes() : nError(osl::FileBase::E_None
) {}
1712 void setUp() override
1714 aTypeURL
= aUserDirectoryURL
.copy(0);
1715 concatURL(aTypeURL
, aTmpName2
);
1716 createTestFile(aTypeURL
);
1717 nError
= DirectoryItem::get(aTypeURL
, rItem
);
1718 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1720 aTypeURL_Hid
= aUserDirectoryURL
.copy(0);
1721 concatURL(aTypeURL_Hid
, aHidURL1
);
1722 createTestFile(aTypeURL_Hid
);
1723 nError
= DirectoryItem::get(aTypeURL_Hid
, rItem_hidden
);
1724 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1727 void tearDown() override
1729 deleteTestFile(aTypeURL
);
1730 deleteTestFile(aTypeURL_Hid
);
1734 // windows only has 3 file attributes: normal, readonly and hidden
1735 void getAttributes_001()
1737 changeFileMode(aTypeURL
, S_IRUSR
| S_IRGRP
| S_IROTH
);
1739 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1740 nError
= rItem
.getFileStatus(rFileStatus
);
1741 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1743 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(UNX version) ",
1744 static_cast<sal_uInt64
>(osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
),
1745 rFileStatus
.getAttributes());
1747 #else // Windows version
1748 void getAttributes_001()
1750 CPPUNIT_ASSERT_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(Windows version)",
1755 void getAttributes_002()
1758 changeFileMode(aTypeURL
, S_IXUSR
| S_IXGRP
| S_IXOTH
);
1760 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1761 nError
= rItem
.getFileStatus(rFileStatus
);
1762 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1764 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)",
1765 static_cast<sal_uInt64
>(osl_File_Attribute_ReadOnly
| osl_File_Attribute_Executable
| osl_File_Attribute_GrpExe
| osl_File_Attribute_OwnExe
| osl_File_Attribute_OthExe
),
1766 rFileStatus
.getAttributes());
1771 void getAttributes_003()
1773 changeFileMode(aTypeURL
, S_IWUSR
| S_IWGRP
| S_IWOTH
);
1775 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1776 nError
= rItem
.getFileStatus(rFileStatus
);
1777 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1779 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Solaris version)",
1780 static_cast<sal_uInt64
>(osl_File_Attribute_GrpWrite
| osl_File_Attribute_OwnWrite
| osl_File_Attribute_OthWrite
),
1781 rFileStatus
.getAttributes());
1783 #else // Windows version
1784 void getAttributes_003()
1786 CPPUNIT_ASSERT_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Windows version)",
1791 #if (defined UNX) // hidden file definition may be different in Windows
1792 void getAttributes_004()
1794 sal_Int32 test_Attributes
= osl_File_Attribute_Hidden
;
1795 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1796 nError
= rItem_hidden
.getFileStatus(rFileStatus
);
1797 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1798 test_Attributes
&= rFileStatus
.getAttributes();
1800 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Hidden files(Solaris version)",
1801 static_cast<sal_Int32
>(osl_File_Attribute_Hidden
), test_Attributes
);
1803 #else // Windows version
1804 void getAttributes_004()
1806 OUString
aUserHiddenFileURL ("file:///c:/AUTOEXEC.BAT");
1807 nError
= DirectoryItem::get(aUserHiddenFileURL
, rItem_hidden
);
1808 CPPUNIT_ASSERT_MESSAGE("get item fail", nError
== osl::FileBase::E_None
);
1809 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
1810 nError
= rItem_hidden
.getFileStatus(rFileStatus
);
1811 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1813 CPPUNIT_ASSERT_MESSAGE("Hidden files(Windows version), please check if hidden file c:/AUTOEXEC.BAT exists ",
1814 (rFileStatus
.getAttributes() & osl_File_Attribute_Hidden
)!= 0);
1818 CPPUNIT_TEST_SUITE(getAttributes
);
1819 CPPUNIT_TEST(getAttributes_001
);
1820 CPPUNIT_TEST(getAttributes_002
);
1821 CPPUNIT_TEST(getAttributes_003
);
1822 CPPUNIT_TEST(getAttributes_004
);
1823 CPPUNIT_TEST_SUITE_END();
1826 // testing the method
1827 // inline TimeValue getAccessTime() const
1829 class getAccessTime
: public CppUnit::TestFixture
1833 osl::FileBase::RC nError
;
1834 DirectoryItem rItem
;
1837 getAccessTime() : nError(osl::FileBase::E_None
) {}
1839 void setUp() override
1841 aTypeURL
= aUserDirectoryURL
.copy(0);
1842 concatURL(aTypeURL
, aTmpName2
);
1843 createTestFile(aTypeURL
);
1844 nError
= DirectoryItem::get(aTypeURL
, rItem
);
1845 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1849 void tearDown() override
1851 deleteTestFile(aTypeURL
);
1854 void getAccessTime_001()
1856 TimeValue
*pTV_current
= nullptr;
1857 CPPUNIT_ASSERT((pTV_current
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1858 TimeValue
*pTV_access
= nullptr;
1859 CPPUNIT_ASSERT((pTV_access
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1861 FileStatus
rFileStatus(osl_FileStatus_Mask_AccessTime
);
1862 nError
= rItem
.getFileStatus(rFileStatus
);
1863 bool bOk
= osl_getSystemTime(pTV_current
);
1864 CPPUNIT_ASSERT(bOk
);
1865 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1867 *pTV_access
= rFileStatus
.getAccessTime();
1869 bool bOK
= t_compareTime(pTV_access
, pTV_current
, delta
);
1873 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. ",
1877 CPPUNIT_TEST_SUITE(getAccessTime
);
1878 CPPUNIT_TEST(getAccessTime_001
);
1879 CPPUNIT_TEST_SUITE_END();
1882 // testing the method
1883 // inline TimeValue getModifyTime() const
1885 class getModifyTime
: public CppUnit::TestFixture
1889 osl::FileBase::RC nError
;
1890 DirectoryItem rItem
;
1893 getModifyTime() : nError(osl::FileBase::E_None
) {}
1895 void getModifyTime_001()
1897 TimeValue
*pTV_current
= nullptr;
1898 CPPUNIT_ASSERT((pTV_current
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1901 aTypeURL
= aUserDirectoryURL
.copy(0);
1902 concatURL(aTypeURL
, aTmpName2
);
1903 createTestFile(aTypeURL
);
1906 bool bOk
= osl_getSystemTime(pTV_current
);
1907 CPPUNIT_ASSERT(bOk
);
1909 // get instance item and filestatus
1910 nError
= DirectoryItem::get(aTypeURL
, rItem
);
1911 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1912 FileStatus
rFileStatus(osl_FileStatus_Mask_ModifyTime
);
1913 nError
= rItem
.getFileStatus(rFileStatus
);
1914 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1917 TimeValue
* pTV_modify
= nullptr;
1918 CPPUNIT_ASSERT((pTV_modify
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
1919 *pTV_modify
= rFileStatus
.getModifyTime();
1921 bool bOK
= t_compareTime(pTV_modify
, pTV_current
, delta
);
1923 deleteTestFile(aTypeURL
);
1927 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. ",
1931 CPPUNIT_TEST_SUITE(getModifyTime
);
1932 CPPUNIT_TEST(getModifyTime_001
);
1933 CPPUNIT_TEST_SUITE_END();
1936 // testing the method
1937 // inline sal_uInt64 getFileSize() const
1939 class getFileSize
: public CppUnit::TestFixture
1943 osl::FileBase::RC nError
;
1944 DirectoryItem rItem
;
1947 getFileSize() : nError(osl::FileBase::E_None
) {}
1949 void setUp() override
1951 aTypeURL
= aUserDirectoryURL
.copy(0);
1952 concatURL(aTypeURL
, aTmpName2
);
1953 createTestFile(aTypeURL
);
1954 nError
= DirectoryItem::get(aTypeURL
, rItem
);
1955 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1958 void tearDown() override
1960 deleteTestFile(aTypeURL
);
1963 void getFileSize_001()
1965 FileStatus
rFileStatus(osl_FileStatus_Mask_FileSize
);
1966 nError
= rItem
.getFileStatus(rFileStatus
);
1967 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1969 sal_uInt64 uFileSize
= rFileStatus
.getFileSize();
1971 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: empty file ",
1972 static_cast<sal_uInt64
>(0), uFileSize
);
1975 void getFileSize_002()
1977 File
testfile(aTypeURL
);
1978 nError
= testfile
.open(osl_File_OpenFlag_Write
| osl_File_OpenFlag_Read
);
1979 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1980 nError
= testfile
.setSize(TEST_FILE_SIZE
);
1981 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1983 nError
= DirectoryItem::get(aTypeURL
, rItem
);
1984 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1985 FileStatus
rFileStatus(osl_FileStatus_Mask_FileSize
);
1986 nError
= rItem
.getFileStatus(rFileStatus
);
1987 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
1988 sal_uInt64 uFileSize
= rFileStatus
.getFileSize();
1990 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: file with size of TEST_FILE_SIZE, did not pass in (W32). ",
1991 static_cast<sal_uInt64
>(TEST_FILE_SIZE
), uFileSize
);
1994 CPPUNIT_TEST_SUITE(getFileSize
);
1995 CPPUNIT_TEST(getFileSize_001
);
1996 CPPUNIT_TEST(getFileSize_002
);
1997 CPPUNIT_TEST_SUITE_END();
2000 // testing the method
2001 // inline OUString getFileName() const
2003 class getFileName
: public CppUnit::TestFixture
2007 osl::FileBase::RC nError
;
2008 DirectoryItem rItem
;
2011 getFileName() : nError(osl::FileBase::E_None
) {}
2012 void setUp() override
2014 aTypeURL
= aUserDirectoryURL
.copy(0);
2015 concatURL(aTypeURL
, aTmpName2
);
2016 createTestFile(aTypeURL
);
2017 nError
= DirectoryItem::get(aTypeURL
, rItem
);
2018 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2021 void tearDown() override
2023 deleteTestFile(aTypeURL
);
2027 void getFileName_001()
2029 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
2030 nError
= rItem
.getFileStatus(rFileStatus
);
2031 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2033 OUString aFileName
= rFileStatus
.getFileName();
2035 CPPUNIT_ASSERT_MESSAGE("test for getFileName function: name compare with specify",
2036 compareFileName(aFileName
, aTmpName2
));
2039 CPPUNIT_TEST_SUITE(getFileName
);
2040 CPPUNIT_TEST(getFileName_001
);
2041 CPPUNIT_TEST_SUITE_END();
2044 // testing the method
2045 // inline OUString getFileURL() const
2047 class getFileURL
: public CppUnit::TestFixture
2049 osl::FileBase::RC nError
;
2050 DirectoryItem rItem
;
2053 getFileURL() : nError(osl::FileBase::E_None
) {}
2056 void setUp() override
2058 createTestFile(aTmpName6
);
2059 nError
= DirectoryItem::get(aTmpName6
, rItem
);
2060 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2063 void tearDown() override
2065 deleteTestFile(aTmpName6
);
2069 void getFileURL_001()
2071 FileStatus
rFileStatus(osl_FileStatus_Mask_FileURL
);
2072 nError
= rItem
.getFileStatus(rFileStatus
);
2073 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError
);
2075 OUString aFileURL
= rFileStatus
.getFileURL();
2077 CPPUNIT_ASSERT_MESSAGE("test for getFileURL function: ",
2078 compareFileName(aFileURL
, aTmpName6
));
2081 CPPUNIT_TEST_SUITE(getFileURL
);
2082 CPPUNIT_TEST(getFileURL_001
);
2083 CPPUNIT_TEST_SUITE_END();
2086 // testing the method
2087 // inline OUString getLinkTargetURL() const
2089 class getLinkTargetURL
: public CppUnit::TestFixture
2093 osl::FileBase::RC nError
;
2094 DirectoryItem rItem
;
2097 getLinkTargetURL() : nError(osl::FileBase::E_None
) {}
2099 void setUp() override
2101 aTypeURL
= aUserDirectoryURL
.copy(0);
2102 concatURL(aTypeURL
, aTmpName2
);
2103 createTestFile(aTypeURL
);
2106 void tearDown() override
2108 deleteTestFile(aTypeURL
);
2111 #if (defined UNX) // Link file is not defined in Windows
2112 void getLinkTargetURL_001()
2114 // create a link file;
2115 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
2116 aUStr_LnkFileSys
+= aSlashURL
+ getCurrentPID() + "/link.file";
2117 aUStr_SrcFileSys
+= aSlashURL
+ getCurrentPID() + "/tmpname";
2119 OString strLinkFileName
, strSrcFileName
;
2120 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
2121 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
2124 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
2125 CPPUNIT_ASSERT_EQUAL_MESSAGE("in creating link file", static_cast<sal_Int32
>(0), fd
);
2127 // get linkTarget URL
2128 nError
= DirectoryItem::get(aLnkURL1
, rItem
);
2129 CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file item", osl::FileBase::E_None
, nError
);
2131 FileStatus
rFileStatus(osl_FileStatus_Mask_LinkTargetURL
);
2132 nError
= rItem
.getFileStatus(rFileStatus
);
2133 CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file status", osl::FileBase::E_None
, nError
);
2134 OUString aFileURL
= rFileStatus
.getLinkTargetURL();
2137 fd
= remove(strLinkFileName
.getStr());
2138 CPPUNIT_ASSERT_EQUAL_MESSAGE("in deleting link file", static_cast<sal_Int32
>(0), fd
);
2140 CPPUNIT_ASSERT_MESSAGE("test for getLinkTargetURL function: Solaris version, create a file, and a link file link to it, get its LinkTargetURL and compare",
2141 compareFileName(aFileURL
, aTypeURL
));
2144 void getLinkTargetURL_001()
2146 CPPUNIT_ASSERT_MESSAGE("test for getLinkTargetURL function: Windows version, not tested",
2151 CPPUNIT_TEST_SUITE(getLinkTargetURL
);
2152 CPPUNIT_TEST(getLinkTargetURL_001
);
2153 CPPUNIT_TEST_SUITE_END();
2156 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::ctors
, "osl_FileStatus");
2157 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::isValid
, "osl_FileStatus");
2158 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileType
, "osl_FileStatus");
2159 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAttributes
, "osl_FileStatus");
2160 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAccessTime
, "osl_FileStatus");
2161 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getModifyTime
, "osl_FileStatus");
2162 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileSize
, "osl_FileStatus");
2163 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileName
, "osl_FileStatus");
2164 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileURL
, "osl_FileStatus");
2165 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getLinkTargetURL
, "osl_FileStatus");
2171 // testing the method
2172 // File(const OUString& ustrFileURL)
2174 class ctors
: public CppUnit::TestFixture
2177 void setUp() override
2179 // create a tempfile in $TEMP/tmpdir/tmpname.
2180 createTestDirectory(aTmpName3
);
2181 createTestFile(aTmpName4
);
2184 void tearDown() override
2186 // remove the tempfile in $TEMP/tmpdir/tmpname.
2187 deleteTestFile(aTmpName4
);
2188 deleteTestDirectory(aTmpName3
);
2193 File
testFile(aTmpName4
);
2195 osl::FileBase::RC nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2196 osl::FileBase::RC nError2
= testFile
.close();
2197 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2198 osl::FileBase::E_None
, nError1
);
2199 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2200 osl::FileBase::E_None
, nError2
);
2205 File
testFile(aTmpName5
);
2206 sal_Char buffer
[30] = "Test for File constructor";
2209 osl::FileBase::RC nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2210 osl::FileBase::RC nError2
= testFile
.write(buffer
, 30, nCount
);
2213 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2214 osl::FileBase::E_None
, nError1
);
2215 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2216 osl::FileBase::E_None
, nError2
);
2219 CPPUNIT_TEST_SUITE(ctors
);
2220 CPPUNIT_TEST(ctors_001
);
2221 CPPUNIT_TEST(ctors_002
);
2222 CPPUNIT_TEST_SUITE_END();
2225 // testing the method
2226 // inline RC open(sal_uInt32 uFlags)
2228 class open
: public CppUnit::TestFixture
2231 osl::FileBase::RC nError1
, nError2
, nError3
;
2235 : nError1(osl::FileBase::E_None
)
2236 , nError2(osl::FileBase::E_None
)
2237 , nError3(osl::FileBase::E_None
) {}
2239 void setUp() override
2241 // create a tempfile in $TEMP/tmpdir/tmpname.
2242 createTestDirectory(aTmpName3
);
2243 createTestFile(aTmpName4
);
2246 void tearDown() override
2248 // remove the tempfile in $TEMP/tmpdir/tmpname.
2249 deleteTestFile(aTmpName4
);
2250 deleteTestDirectory(aTmpName3
);
2256 File
testFile(aTmpName4
);
2258 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2259 nError2
= testFile
.close();
2260 CPPUNIT_ASSERT_EQUAL_MESSAGE("close error", osl::FileBase::E_None
, nError2
);
2262 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a regular file",
2263 osl::FileBase::E_None
, nError1
);
2268 File
testFile(aTmpName3
);
2270 nError1
= testFile
.open(osl_File_OpenFlag_Read
);
2272 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory",
2273 (File::E_INVAL
== nError1
) || (File::E_ACCES
== nError1
));
2278 File
testFile(aCanURL1
);
2280 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2282 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a non-exist file",
2283 File::E_NOENT
, nError1
);
2288 OUString
aTestFile(aRootURL
);
2289 concatURL(aTestFile
, aTmpName2
);
2290 File
testFile(aTestFile
);
2292 nError1
= testFile
.open(osl_File_OpenFlag_Create
);
2293 bool bOK
= (nError1
== File::E_ACCES
);
2295 bOK
= true; /// in Windows, you can create file in c:\ any way.
2297 deleteTestFile(aTestFile
);
2300 CPPUNIT_ASSERT_MESSAGE("test for open function: create an illegal file",
2306 File
testFile(aTmpName4
);
2308 nError1
= testFile
.open(osl_File_OpenFlag_Create
);
2310 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: create an exist file",
2311 File::E_EXIST
, nError1
);
2316 File
testFile(aCanURL1
);
2317 sal_Char buffer_write
[30] = "Test for File open";
2318 sal_Char buffer_read
[30];
2319 sal_uInt64 nCount_write
, nCount_read
;
2321 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
| osl_File_OpenFlag_Create
);
2322 nError2
= testFile
.write(buffer_write
, 30, nCount_write
);
2323 osl::FileBase::RC nError4
= testFile
.setPos(osl_Pos_Absolut
, 0);
2324 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError4
);
2325 nError3
= testFile
.read(buffer_read
, 10, nCount_read
);
2327 osl::FileBase::RC nError5
= testFile
.close();
2328 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError5
);
2329 osl::FileBase::RC nError6
= osl::File::remove(aCanURL1
);
2330 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError6
);
2332 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2333 osl::FileBase::E_None
, nError1
);
2334 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2335 osl::FileBase::E_None
, nError2
);
2336 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2337 osl::FileBase::E_None
, nError3
);
2338 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2339 sal_uInt64(30), nCount_write
);
2340 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2341 sal_uInt64(10), nCount_read
);
2344 CPPUNIT_TEST_SUITE(open
);
2345 CPPUNIT_TEST(open_001
);
2346 CPPUNIT_TEST(open_002
);
2347 CPPUNIT_TEST(open_003
);
2348 CPPUNIT_TEST(open_004
);
2349 CPPUNIT_TEST(open_005
);
2350 CPPUNIT_TEST(open_006
);
2351 CPPUNIT_TEST_SUITE_END();
2354 // testing the method
2355 // inline RC close()
2357 class close
: public CppUnit::TestFixture
2360 osl::FileBase::RC nError1
, nError2
, nError3
;
2364 : nError1(osl::FileBase::E_None
)
2365 , nError2(osl::FileBase::E_None
)
2366 , nError3(osl::FileBase::E_None
) {}
2368 void setUp() override
2370 // create a tempfile in $TEMP/tmpdir/tmpname.
2371 createTestDirectory(aTmpName3
);
2372 createTestFile(aTmpName4
);
2375 void tearDown() override
2377 // remove the tempfile in $TEMP/tmpdir/tmpname.
2378 deleteTestFile(aTmpName4
);
2379 deleteTestDirectory(aTmpName3
);
2385 File
testFile(aTmpName4
);
2387 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2388 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2390 nError2
= testFile
.close();
2392 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: close a regular file",
2393 osl::FileBase::E_None
, nError2
);
2398 File
testFile(aTmpName4
);
2400 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2401 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2403 nError2
= testFile
.close();
2405 nError3
= testFile
.setPos(osl_Pos_Absolut
, 0);
2407 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: manipulate a file after it has been closed",
2408 osl::FileBase::E_None
, nError2
);
2409 CPPUNIT_ASSERT_MESSAGE("test for close function: manipulate a file after it has been closed",
2410 (osl::FileBase::E_None
!= nError3
));
2413 CPPUNIT_TEST_SUITE(close
);
2414 CPPUNIT_TEST(close_001
);
2415 CPPUNIT_TEST(close_002
);
2416 CPPUNIT_TEST_SUITE_END();
2419 // testing the method
2420 // inline RC setPos(sal_uInt32 uHow, sal_Int64 uPos)
2422 class setPos
: public CppUnit::TestFixture
2425 osl::FileBase::RC nError1
;
2426 sal_uInt64 nCount_write
, nCount_read
;
2430 : nError1(osl::FileBase::E_None
)
2436 void setUp() override
2438 // create a tempfile in $TEMP/tmpdir/tmpname.
2439 createTestDirectory(aTmpName3
);
2440 createTestFile(aTmpName4
);
2442 // write chars into the file.
2443 File
testFile(aTmpName4
);
2445 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2446 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2447 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2448 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2449 nError1
= testFile
.close();
2450 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2453 void tearDown() override
2455 // remove the tempfile in $TEMP/tmpdir/tmpname.
2456 deleteTestFile(aTmpName4
);
2457 deleteTestDirectory(aTmpName3
);
2462 File
testFile(aTmpName4
);
2463 sal_Char buffer_read
[2];
2465 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2466 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2467 nError1
= testFile
.setPos(osl_Pos_Absolut
, 26);
2468 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2469 nError1
= testFile
.read(buffer_read
, 1, nCount_read
);
2470 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2471 nError1
= testFile
.close();
2472 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2474 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",
2475 pBuffer_Char
[26], buffer_read
[0]);
2480 File
testFile(aTmpName4
);
2481 sal_Char buffer_read
[2];
2483 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2484 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2485 nError1
= testFile
.setPos(osl_Pos_Absolut
, sizeof(pBuffer_Char
) - 2);
2486 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2487 nError1
= testFile
.setPos(osl_Pos_Current
, 0);
2488 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2489 nError1
= testFile
.read(buffer_read
, 1, nCount_read
);
2490 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2491 nError1
= testFile
.close();
2492 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2494 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",
2495 pBuffer_Char
[sizeof(pBuffer_Char
) - 2], buffer_read
[0]);
2500 File
testFile(aTmpName4
);
2501 sal_Char buffer_read
[2];
2503 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2504 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2505 // the file size is smaller than 100
2506 nError1
= testFile
.setPos(osl_Pos_End
, -100);
2507 CPPUNIT_ASSERT_EQUAL_MESSAGE("should return error", osl::FileBase::E_INVAL
, nError1
);
2509 nError1
= testFile
.setPos(osl_Pos_End
, -53);
2510 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2511 nError1
= testFile
.read(buffer_read
, 1, nCount_read
);
2512 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2513 nError1
= testFile
.close();
2514 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2516 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",
2517 pBuffer_Char
[0], buffer_read
[0]);
2520 CPPUNIT_TEST_SUITE(setPos
);
2521 CPPUNIT_TEST(setPos_001
);
2522 CPPUNIT_TEST(setPos_002
);
2523 CPPUNIT_TEST(setPos_003
);
2524 CPPUNIT_TEST_SUITE_END();
2527 // testing the method
2528 // inline RC getPos(sal_uInt64& uPos)
2530 class getPos
: public CppUnit::TestFixture
2533 osl::FileBase::RC nError1
;
2534 sal_uInt64 nCount_write
;
2538 : nError1(osl::FileBase::E_None
)
2544 void setUp() override
2546 // create a tempfile in $TEMP/tmpdir/tmpname.
2547 createTestDirectory(aTmpName3
);
2548 createTestFile(aTmpName4
);
2550 // write chars into the file.
2551 File
testFile(aTmpName4
);
2553 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2554 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2555 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2556 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2557 nError1
= testFile
.close();
2558 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2561 void tearDown() override
2563 // remove the tempfile in $TEMP/tmpdir/tmpname.
2564 deleteTestFile(aTmpName4
);
2565 deleteTestDirectory(aTmpName3
);
2571 File
testFile(aTmpName4
);
2572 sal_uInt64 nFilePointer
;
2574 nError1
= testFile
.getPos(nFilePointer
);
2575 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL
, nError1
);
2577 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2578 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2580 nError1
= testFile
.setPos(osl_Pos_Absolut
, 26);
2581 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2582 nError1
= testFile
.getPos(nFilePointer
);
2583 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2585 nError1
= testFile
.close();
2586 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2588 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getPos function: set the position to 26, get position and check if it is right",
2589 static_cast<sal_uInt64
>(26), nFilePointer
);
2592 CPPUNIT_TEST_SUITE(getPos
);
2593 CPPUNIT_TEST(getPos_001
);
2594 CPPUNIT_TEST_SUITE_END();
2597 // testing the method
2598 // inline RC isEndOfFile(sal_Bool *pIsEOF)
2600 class isEndOfFile
: public CppUnit::TestFixture
2603 osl::FileBase::RC nError1
;
2604 sal_uInt64 nCount_write
;
2608 : nError1(osl::FileBase::E_None
)
2614 void setUp() override
2616 // create a tempfile in $TEMP/tmpdir/tmpname.
2617 createTestDirectory(aTmpName3
);
2618 createTestFile(aTmpName4
);
2620 // write chars into the file.
2621 File
testFile(aTmpName4
);
2623 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2624 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2625 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2626 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2627 nError1
= testFile
.close();
2628 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2631 void tearDown() override
2633 // remove the tempfile in $TEMP/tmpdir/tmpname.
2634 deleteTestFile(aTmpName4
);
2635 deleteTestDirectory(aTmpName3
);
2639 void isEndOfFile_001()
2641 File
testFile(aTmpName4
);
2642 sal_Bool bEOF
= false;
2643 sal_Bool
*pEOF
= &bEOF
;
2645 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2646 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2648 nError1
= testFile
.setPos(osl_Pos_End
, 0);
2649 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2650 nError1
= testFile
.isEndOfFile(pEOF
);
2651 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2653 nError1
= testFile
.close();
2654 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2656 CPPUNIT_ASSERT_MESSAGE("test for isEndOfFile function: set the position to end, check if reach end",
2660 void isEndOfFile_002()
2662 File
testFile(aTmpName4
);
2663 sal_Bool bEOF
= false;
2664 sal_Bool
*pEOF
= &bEOF
;
2665 sal_uInt64 nFilePointer
= 0;
2667 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2668 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2670 nError1
= testFile
.setPos(osl_Pos_Absolut
, 0);
2671 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2676 nError1
= testFile
.isEndOfFile(pEOF
);
2677 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2678 nError1
= testFile
.setPos(osl_Pos_Current
, 1);
2679 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2682 nError1
= testFile
.getPos(nFilePointer
);
2683 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2685 nError1
= testFile
.close();
2686 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2688 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isEndOfFile function: use isEndOfFile to move pointer step by step",
2689 static_cast<sal_uInt64
>(sizeof(pBuffer_Char
) + 1), nFilePointer
);
2691 CPPUNIT_TEST_SUITE(isEndOfFile
);
2692 CPPUNIT_TEST(isEndOfFile_001
);
2693 CPPUNIT_TEST(isEndOfFile_002
);
2694 CPPUNIT_TEST_SUITE_END();
2697 // testing the method
2698 // inline RC setSize(sal_uInt64 uSize)
2700 class setSize
: public CppUnit::TestFixture
2703 osl::FileBase::RC nError1
;
2704 sal_uInt64 nCount_write
;
2708 : nError1(osl::FileBase::E_None
)
2713 void setUp() override
2715 // create a tempfile in $TEMP/tmpdir/tmpname.
2716 createTestDirectory(aTmpName3
);
2717 createTestFile(aTmpName4
);
2719 // write chars into the file.
2720 File
testFile(aTmpName4
);
2722 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2723 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2724 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2725 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2726 nError1
= testFile
.close();
2727 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2730 void tearDown() override
2732 // remove the tempfile in $TEMP/tmpdir/tmpname.
2733 deleteTestFile(aTmpName4
);
2734 deleteTestDirectory(aTmpName3
);
2740 File
testFile(aTmpName4
);
2741 sal_uInt64 nFilePointer
;
2743 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2744 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2746 // enlarge the file to size of 100;
2747 nError1
= testFile
.setSize(100);
2748 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2750 // get the file size;
2751 nError1
= testFile
.setPos(osl_Pos_End
, 0);
2752 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2753 nError1
= testFile
.getPos(nFilePointer
);
2754 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2756 nError1
= testFile
.close();
2757 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2759 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: enlarge the file ",
2760 static_cast<sal_uInt64
>(100), nFilePointer
);
2765 File
testFile(aTmpName4
);
2766 sal_uInt64 nFilePointer
;
2768 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2769 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2771 // enlarge the file to size of 100;
2772 nError1
= testFile
.setSize(10);
2773 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2775 // get the file size;
2776 nError1
= testFile
.setPos(osl_Pos_End
, 0);
2777 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2778 nError1
= testFile
.getPos(nFilePointer
);
2779 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2781 nError1
= testFile
.close();
2782 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2784 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: truncate the file ",
2785 static_cast<sal_uInt64
>(10), nFilePointer
);
2788 CPPUNIT_TEST_SUITE(setSize
);
2789 CPPUNIT_TEST(setSize_001
);
2790 CPPUNIT_TEST(setSize_002
);
2791 CPPUNIT_TEST_SUITE_END();
2794 // testing the method
2795 // inline RC read(void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead)
2797 class read
: public CppUnit::TestFixture
2800 osl::FileBase::RC nError1
;
2801 sal_uInt64 nCount_write
, nCount_read
;
2805 : nError1(osl::FileBase::E_None
)
2811 void setUp() override
2813 // create a tempfile in $TEMP/tmpdir/tmpname.
2814 createTestDirectory(aTmpName3
);
2815 createTestFile(aTmpName4
);
2817 // write chars into the file.
2818 File
testFile(aTmpName4
);
2820 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2821 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2822 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
2823 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2824 nError1
= testFile
.close();
2825 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2828 void tearDown() override
2830 // remove the tempfile in $TEMP/tmpdir/tmpname.
2831 deleteTestFile(aTmpName4
);
2832 deleteTestDirectory(aTmpName3
);
2838 File
testFile(aTmpName4
);
2839 sal_uInt64 nFilePointer
;
2840 sal_Char buffer_read
[10];
2842 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2843 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2845 nError1
= testFile
.read(buffer_read
, 10, nCount_read
);
2846 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2847 nError1
= testFile
.getPos(nFilePointer
);
2848 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2850 nError1
= testFile
.close();
2851 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2853 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2854 sal_uInt64(10), nFilePointer
);
2855 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2856 0, strncmp(buffer_read
, pBuffer_Char
, 10));
2861 File
testFile(aTmpName4
);
2862 sal_uInt64 nFilePointer
;
2863 sal_Char buffer_read
[26];
2865 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2866 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2868 nError1
= testFile
.setPos(osl_Pos_Absolut
, 26);
2869 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2870 nError1
= testFile
.read(buffer_read
, 26, nCount_read
);
2871 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2872 nError1
= testFile
.getPos(nFilePointer
);
2873 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2875 nError1
= testFile
.close();
2876 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2878 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2879 sal_uInt64(52), nFilePointer
);
2880 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2881 sal_uInt64(26), nCount_read
);
2882 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2883 0, strncmp(buffer_read
, &pBuffer_Char
[26], 26));
2886 CPPUNIT_TEST_SUITE(read
);
2887 CPPUNIT_TEST(read_001
);
2888 CPPUNIT_TEST(read_002
);
2889 CPPUNIT_TEST_SUITE_END();
2892 // testing the method
2893 // inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
2895 class write
: public CppUnit::TestFixture
2897 osl::FileBase::RC nError1
;
2898 sal_uInt64 nCount_write
, nCount_read
;
2902 : nError1(osl::FileBase::E_None
)
2909 void setUp() override
2911 // create a tempfile in $TEMP/tmpname.
2912 createTestFile(aTmpName6
);
2915 void tearDown() override
2917 // remove the tempfile in $TEMP/tmpname.
2918 deleteTestFile(aTmpName6
);
2924 File
testFile(aTmpName6
);
2925 sal_uInt64 nFilePointer
;
2926 sal_Char buffer_read
[10];
2928 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
2929 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2931 // write chars into the file.
2932 nError1
= testFile
.write(pBuffer_Char
, 10, nCount_write
);
2933 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2934 // get the current pointer;
2935 nError1
= testFile
.getPos(nFilePointer
);
2936 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2937 // reset pointer to the beginning;
2938 nError1
= testFile
.setPos(osl_Pos_Absolut
, 0);
2939 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2940 nError1
= testFile
.read(buffer_read
, 10, nCount_read
);
2941 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2943 nError1
= testFile
.close();
2944 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2946 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",
2947 sal_uInt64(10), nFilePointer
);
2948 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",
2949 0, strncmp(buffer_read
, pBuffer_Char
, 10));
2950 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",
2951 sal_uInt64(10), nCount_write
);
2954 CPPUNIT_TEST_SUITE(write
);
2955 CPPUNIT_TEST(write_001
);
2956 CPPUNIT_TEST_SUITE_END();
2959 // testing the method
2960 // inline RC readLine(::ByteSequence& aSeq)
2962 class readLine
: public CppUnit::TestFixture
2964 osl::FileBase::RC nError1
;
2965 sal_uInt64 nCount_write
;
2966 rtl::ByteSequence aSequence
;
2970 : nError1(osl::FileBase::E_None
)
2975 void setUp() override
2977 // create a tempfile in $TEMP/tmpname.
2978 createTestFile(aTmpName6
);
2980 // write some strings into the file.
2981 File
testFile(aTmpName6
);
2982 sal_Char ppStrSeq
[3][27] = { "abcde\n",
2984 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2987 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
2988 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2990 for (int nCount
= 0; nCount
< 3; nCount
++)
2992 nError1
= testFile
.write(ppStrSeq
[nCount
], strlen(ppStrSeq
[nCount
]), nCount_write
);
2993 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
2996 nError1
= testFile
.close();
2997 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3000 void tearDown() override
3002 // remove the tempfile in $TEMP/tmpname.
3003 deleteTestFile(aTmpName6
);
3009 File
testFile(aTmpName6
);
3011 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
3012 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3013 nError1
= testFile
.readLine(aSequence
);
3014 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3015 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
3016 osl::FileBase::E_None
, nError1
);
3017 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
3018 0, strncmp(reinterpret_cast<char *>(aSequence
.getArray()), pBuffer_Char
, 5));
3023 File
testFile(aTmpName6
);
3024 sal_Bool bEOF
= false;
3025 sal_Bool
*pEOF
= &bEOF
;
3027 nError1
= testFile
.open(osl_File_OpenFlag_Read
| osl_File_OpenFlag_Write
);
3028 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3029 for (int nCount
= 0; nCount
< 3; nCount
++)
3031 nError1
= testFile
.readLine(aSequence
);
3032 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3034 nError1
= testFile
.isEndOfFile(pEOF
);
3035 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3037 CPPUNIT_ASSERT_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
3039 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
3040 0, strncmp(reinterpret_cast<char *>(aSequence
.getArray()), &pBuffer_Char
[26], 26));
3042 CPPUNIT_TEST_SUITE(readLine
);
3043 CPPUNIT_TEST(readLine_001
);
3044 CPPUNIT_TEST(readLine_002
);
3045 CPPUNIT_TEST_SUITE_END();
3048 // testing the method
3049 // inline static RC copy(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
3051 class copy
: public CppUnit::TestFixture
3053 osl::FileBase::RC nError1
;
3054 sal_uInt64 nCount_write
;
3058 : nError1(osl::FileBase::E_None
)
3063 void setUp() override
3065 // create a tempfile in $TEMP/tmpdir/tmpname.
3066 createTestDirectory(aTmpName3
);
3067 createTestFile(aTmpName4
);
3069 // write chars into the file.
3070 File
testFile(aTmpName4
);
3072 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
3073 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3074 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
3075 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3076 nError1
= testFile
.close();
3077 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3080 void tearDown() override
3082 // remove the tempfile in $TEMP/tmpdir/tmpname.
3083 deleteTestFile(aTmpName4
);
3084 deleteTestDirectory(aTmpName3
);
3089 File
testFile(aTmpName6
);
3091 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpname.
3092 nError1
= File::copy(aTmpName4
, aTmpName6
);
3093 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3095 nError1
= testFile
.open(osl_File_OpenFlag_Create
);
3096 deleteTestFile(aTmpName6
);
3098 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy file to upper directory",
3099 osl::FileBase::E_EXIST
, nError1
);
3104 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
3105 nError1
= File::copy(aTmpName4
, aTmpName3
);
3107 CPPUNIT_ASSERT_MESSAGE("test for copy function: use directory as destination",
3108 (osl::FileBase::E_ISDIR
== nError1
) ||(osl::FileBase::E_ACCES
== nError1
));
3113 // copy $TEMP/tmpdir/tmpname to $ROOT/tmpname.
3114 nError1
= File::copy(aTmpName4
, aTmpName7
);
3116 nError1
= osl::FileBase::E_ACCES
; /// for Windows, c:\ is writable anyway.
3117 deleteTestFile(aTmpName7
);
3119 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy to an illegal place",
3120 osl::FileBase::E_ACCES
, nError1
);
3125 // copy $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3126 nError1
= File::copy(aTmpName6
, aTmpName4
);
3128 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a not exist file",
3129 osl::FileBase::E_NOENT
, nError1
);
3134 // copy $TEMP/tmpname to $TEMP/system.path using system path.
3135 nError1
= File::copy(aTmpName6
, aSysPath1
);
3137 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a file using system file path",
3138 osl::FileBase::E_INVAL
, nError1
);
3143 createTestFile(aTmpName6
);
3144 File
tmpFile(aTmpName6
);
3145 tmpFile
.open(osl_File_OpenFlag_Write
| osl_File_OpenFlag_Read
);
3146 tmpFile
.setSize(200);
3149 nError1
= File::copy(aTmpName6
, aTmpName4
);
3150 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3152 // check if is the new file
3153 File
newFile(aTmpName4
);
3154 newFile
.open(osl_File_OpenFlag_Write
| osl_File_OpenFlag_Read
);
3155 nError1
= newFile
.setPos(osl_Pos_End
, 0);
3156 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3158 sal_uInt64 nFilePointer
;
3159 nError1
= newFile
.getPos(nFilePointer
);
3160 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3162 deleteTestFile(aTmpName6
);
3163 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: the dest file exist",
3164 static_cast<sal_uInt64
>(200), nFilePointer
);
3167 CPPUNIT_TEST_SUITE(copy
);
3168 CPPUNIT_TEST(copy_001
);
3169 CPPUNIT_TEST(copy_002
);
3170 CPPUNIT_TEST(copy_003
);
3171 CPPUNIT_TEST(copy_004
);
3172 CPPUNIT_TEST(copy_005
);
3173 CPPUNIT_TEST(copy_006
);
3174 CPPUNIT_TEST_SUITE_END();
3177 // testing the method
3178 // inline static RC move(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
3180 class move
: public CppUnit::TestFixture
3182 osl::FileBase::RC nError1
, nError2
;
3183 sal_uInt64 nCount_write
;
3187 : nError1(osl::FileBase::E_None
)
3188 , nError2(osl::FileBase::E_None
)
3193 void setUp() override
3195 // create a tempfile in $TEMP/tmpdir/tmpname.
3196 createTestDirectory(aTmpName3
);
3197 createTestFile(aTmpName4
);
3199 // write chars into the file.
3200 File
testFile(aTmpName4
);
3202 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
3203 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3204 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
3205 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3206 nError1
= testFile
.close();
3207 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3210 void tearDown() override
3212 // remove the tempfile in $TEMP/tmpdir/tmpname.
3213 deleteTestFile(aTmpName4
);
3214 deleteTestDirectory(aTmpName3
);
3220 // rename $TEMP/tmpdir/tmpname to $TEMP/canonical.name.
3221 nError1
= File::move(aTmpName4
, aCanURL1
);
3222 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3224 File
testFile(aCanURL1
);
3225 nError2
= testFile
.open(osl_File_OpenFlag_Create
);
3226 deleteTestFile(aCanURL1
);
3228 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: rename file to another directory",
3229 osl::FileBase::E_EXIST
, nError2
);
3234 // move $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
3235 nError1
= File::move(aTmpName4
, aTmpName3
);
3236 // returned osl::FileBase::E_ACCES on WNT
3237 CPPUNIT_ASSERT_MESSAGE("test for move function: use directory as destination",
3238 (osl::FileBase::E_ACCES
== nError1
|| osl::FileBase::E_ISDIR
== nError1
) ||(osl::FileBase::E_EXIST
== nError1
));
3243 // move $TEMP/tmpdir/tmpname to $ROOT/tmpname.
3244 nError1
= File::move(aTmpName4
, aTmpName7
);
3246 nError1
= osl::FileBase::E_ACCES
; /// for Windows, c:\ is writable any way.
3247 deleteTestFile(aTmpName7
);
3250 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move to an illegal place",
3251 osl::FileBase::E_ACCES
, nError1
);
3256 // move $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3257 nError1
= File::move(aTmpName6
, aTmpName4
);
3259 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a not exist file",
3260 osl::FileBase::E_NOENT
, nError1
);
3265 // move $TEMP/tmpname to $TEMP/system.path using system path.
3266 nError1
= File::move(aTmpName6
, aSysPath1
);
3268 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a file using system file",
3269 osl::FileBase::E_INVAL
, nError1
);
3274 // move directory $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3275 createTestDirectory(aTmpName6
);
3276 nError1
= File::move(aTmpName6
, aTmpName4
);
3277 // move file $TEMP/tmpdir/tmpname to $TEMP/tmpname
3278 nError2
= File::move(aTmpName4
, aTmpName6
);
3279 deleteTestDirectory(aTmpName6
);
3281 deleteTestDirectory(aTmpName4
);// in Windows, it can be moved!!!!! this is only for not influence the following test.
3282 deleteTestFile(aTmpName6
);
3283 nError1
= osl::FileBase::E_NOTDIR
;
3284 nError2
= osl::FileBase::E_ISDIR
;
3286 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3287 osl::FileBase::E_NOTDIR
, nError1
);
3288 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3289 osl::FileBase::E_ISDIR
, nError2
);
3294 // create directory $TEMP/tmpname.
3295 createTestDirectory(aTmpName6
);
3296 // move directory $TEMP/tmpdir to $TEMP/tmpname/tmpdir
3297 nError1
= File::move(aTmpName3
, aTmpName8
);
3299 nError2
= Directory::create(aTmpName8
);
3300 File::move(aTmpName8
, aTmpName3
);
3301 deleteTestDirectory(aTmpName6
);
3303 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3304 osl::FileBase::E_None
, nError1
);
3305 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3306 osl::FileBase::E_EXIST
, nError2
);
3309 // bugid# 115420, after the bug fix, add the case
3310 CPPUNIT_TEST_SUITE(move
);
3311 CPPUNIT_TEST(move_001
);
3312 CPPUNIT_TEST(move_002
);
3313 CPPUNIT_TEST(move_003
);
3314 CPPUNIT_TEST(move_004
);
3315 CPPUNIT_TEST(move_005
);
3316 CPPUNIT_TEST(move_006
);
3317 CPPUNIT_TEST(move_007
);
3318 CPPUNIT_TEST_SUITE_END();
3321 // testing the method
3322 // inline static RC remove(const OUString& ustrFileURL)
3324 class remove
: public CppUnit::TestFixture
3326 osl::FileBase::RC nError1
, nError2
;
3327 sal_uInt64 nCount_write
;
3331 : nError1(osl::FileBase::E_None
)
3332 , nError2(osl::FileBase::E_None
)
3338 void setUp() override
3340 // create a tempfile in $TEMP/tmpdir/tmpname.
3341 createTestDirectory(aTmpName3
);
3342 createTestFile(aTmpName4
);
3344 // write chars into the file.
3345 File
testFile(aTmpName4
);
3347 nError1
= testFile
.open(osl_File_OpenFlag_Write
);
3348 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3349 nError1
= testFile
.write(pBuffer_Char
, sizeof(pBuffer_Char
), nCount_write
);
3350 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3351 nError1
= testFile
.close();
3352 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3355 void tearDown() override
3357 // remove the tempfile in $TEMP/tmpdir/tmpname.
3358 deleteTestFile(aTmpName4
);
3359 deleteTestDirectory(aTmpName3
);
3365 // remove $TEMP/tmpdir/tmpname.
3366 nError1
= File::remove(aTmpName4
);
3368 File
testFile(aTmpName4
);
3369 nError2
= testFile
.open(osl_File_OpenFlag_Create
);
3371 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file",
3372 osl::FileBase::E_None
, nError1
);
3373 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a file",
3374 (osl::FileBase::E_EXIST
!= nError2
));
3379 // remove $TEMP/tmpname.
3380 nError1
= File::remove(aTmpName6
);
3382 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file not exist",
3383 osl::FileBase::E_NOENT
, nError1
);
3388 // remove $TEMP/system/path.
3389 nError1
= File::remove(aSysPath2
);
3391 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: removing a file not using full qualified URL",
3392 osl::FileBase::E_INVAL
, nError1
);
3397 // remove $TEMP/tmpdir.
3398 nError1
= File::remove(aTmpName3
);
3400 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a directory",
3401 (osl::FileBase::E_ISDIR
== nError1
) || (osl::FileBase::E_ACCES
== nError1
));
3404 CPPUNIT_TEST_SUITE(remove
);
3405 CPPUNIT_TEST(remove_001
);
3406 CPPUNIT_TEST(remove_002
);
3407 CPPUNIT_TEST(remove_003
);
3408 CPPUNIT_TEST(remove_004
);
3409 CPPUNIT_TEST_SUITE_END();
3412 // testing the method
3413 // inline static RC setAttributes(const OUString& ustrFileURL, sal_uInt64 uAttributes)
3415 class setAttributes
: public CppUnit::TestFixture
3418 osl::FileBase::RC nError1
, nError2
;
3419 DirectoryItem rItem
, rItem_hidden
;
3422 setAttributes() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
3424 void setUp() override
3426 // create a tempfile in $TEMP/tmpdir/tmpname.
3427 createTestFile(aTmpName6
);
3430 void tearDown() override
3432 // remove the tempfile in $TEMP/tmpdir/tmpname.
3433 deleteTestFile(aTmpName6
);
3437 void setAttributes_001()
3439 // on windows, only can set 2 attributes: osl_File_Attribute_ReadOnly, osl_File_Attribute_Hidden
3441 // set the file to readonly
3442 nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
);
3443 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3444 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3445 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3446 // get the file attributes
3447 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
3448 nError1
= rItem
.getFileStatus(rFileStatus
);
3449 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3451 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3452 static_cast<sal_uInt64
>(osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
),
3453 rFileStatus
.getAttributes());
3455 // please see GetFileAttributes
3456 nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_ReadOnly
);
3457 CPPUNIT_ASSERT(nError2
== osl::FileBase::E_None
);
3458 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3459 CPPUNIT_ASSERT(nError1
== osl::FileBase::E_None
);
3460 // get the file attributes
3461 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
3462 nError1
= rItem
.getFileStatus(rFileStatus
);
3463 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3464 // here the file has 2 Attributes: FILE_ATTRIBUTE_READONLY and FILE_ATTRIBUTE_NORMAL,
3465 // but FILE_ATTRIBUTE_NORMAL is valid only if used alone, so this is maybe a bug
3466 /*OString aString = OUStringToOString(aTmpName6, RTL_TEXTENCODING_ASCII_US);
3467 DWORD dwFileAttributes = GetFileAttributes(aString.getStr());
3468 if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
3469 printf("has normal attribute");
3470 if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
3471 printf("has readonly attribute");
3473 CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes READONLY and get it to verify.",
3474 (osl_File_Attribute_ReadOnly
& rFileStatus
.getAttributes()) != 0);
3477 void setAttributes_002()
3479 // on UNX, can not set hidden attribute to file, rename file can set the attribute
3481 // set the file to hidden
3482 nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_Hidden
);
3484 CPPUNIT_ASSERT(nError2
== osl::FileBase::E_None
);
3485 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3486 CPPUNIT_ASSERT(nError1
== osl::FileBase::E_None
);
3487 // get the file attributes
3488 FileStatus
rFileStatus(osl_FileStatus_Mask_Attributes
);
3489 nError1
= rItem
.getFileStatus(rFileStatus
);
3490 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3492 CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3493 (osl_File_Attribute_Hidden
& rFileStatus
.getAttributes()) != 0);
3497 CPPUNIT_TEST_SUITE(setAttributes
);
3498 CPPUNIT_TEST(setAttributes_001
);
3499 CPPUNIT_TEST(setAttributes_002
);
3500 CPPUNIT_TEST_SUITE_END();
3503 // testing the method
3504 // inline static RC setTime(
3505 // const OUString& ustrFileURL,
3506 // const TimeValue& rCreationTime,
3507 // const TimeValue& rLastAccessTime,
3508 // const TimeValue& rLastWriteTime)
3510 class setTime
: public CppUnit::TestFixture
3513 osl::FileBase::RC nError1
, nError2
;
3514 DirectoryItem rItem
;
3517 setTime() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
3519 void setUp() override
3521 // create a tempfile in $TEMP/tmpdir/tmpname.
3522 createTestFile(aTmpName6
);
3525 void tearDown() override
3527 // remove the tempfile in $TEMP/tmpdir/tmpname.
3528 deleteTestFile(aTmpName6
);
3534 TimeValue
*pTV_current
= nullptr;
3535 CPPUNIT_ASSERT((pTV_current
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3536 TimeValue
*pTV_creation
= nullptr;
3537 CPPUNIT_ASSERT((pTV_creation
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3538 TimeValue
*pTV_access
= nullptr;
3539 CPPUNIT_ASSERT((pTV_access
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3540 TimeValue
*pTV_modify
= nullptr;
3541 CPPUNIT_ASSERT((pTV_modify
= static_cast<TimeValue
*>(malloc(sizeof(TimeValue
)))) != nullptr);
3544 bool bOk
= osl_getSystemTime(pTV_current
);
3545 CPPUNIT_ASSERT(bOk
);
3547 // set the file time
3548 nError2
= File::setTime(aTmpName6
, *pTV_current
, *pTV_current
, *pTV_current
);
3549 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError2
).getStr(), osl::FileBase::E_None
, nError2
);
3551 // get the file access time, creation time, modify time
3552 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3553 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3555 FileStatus
rFileStatus(osl_FileStatus_Mask_AccessTime
);
3556 nError1
= rItem
.getFileStatus(rFileStatus
);
3557 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3558 *pTV_access
= rFileStatus
.getAccessTime();
3560 FileStatus
rFileStatus1(osl_FileStatus_Mask_CreationTime
);
3561 nError1
= rItem
.getFileStatus(rFileStatus1
);
3562 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3563 *pTV_creation
= rFileStatus1
.getCreationTime();
3565 FileStatus
rFileStatus2(osl_FileStatus_Mask_ModifyTime
);
3566 nError1
= rItem
.getFileStatus(rFileStatus2
);
3567 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1
).getStr(), osl::FileBase::E_None
, nError1
);
3568 *pTV_modify
= rFileStatus2
.getModifyTime();
3570 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.",
3571 t_compareTime(pTV_access
, pTV_current
, delta
));
3573 // Unfortunately there is no way to get the creation time of a file under Unix (it's a Windows only feature).
3574 // That means the flag osl_FileStatus_Mask_CreationTime should be deprecated under Unix.
3575 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set creation time then get it. ",
3576 sal_True
== t_compareTime(pTV_creation
, pTV_current
, delta
));
3578 CPPUNIT_ASSERT_MESSAGE("test for setTime function: set modify time then get it. ",
3579 t_compareTime(pTV_modify
, pTV_current
, delta
));
3586 CPPUNIT_TEST_SUITE(setTime
);
3587 CPPUNIT_TEST(setTime_001
);
3588 CPPUNIT_TEST_SUITE_END();
3591 // testing the method
3592 // inline static RC sync()
3594 class sync
: public CppUnit::TestFixture
3597 osl::FileBase::RC nError1
, nError2
;
3598 DirectoryItem rItem
;
3601 sync() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
3603 void setUp() override
3605 // create a tempfile in $TEMP/tmpdir/tmpname.
3606 createTestFile(aTmpName6
);
3610 void tearDown() override
3612 // remove the tempfile in $TEMP/tmpdir/tmpname.
3613 deleteTestFile(aTmpName6
);
3616 // test case: if The file is located on a read only file system.
3620 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3621 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3623 File
tmp_file(aTmpName6
);
3624 osl::FileBase::RC err
= tmp_file
.open(osl_File_OpenFlag_Write
);
3626 CPPUNIT_ASSERT_EQUAL_MESSAGE("File open failed", osl::FileBase::E_None
, err
);
3629 sal_uInt64 written
= 0;
3630 nError1
= tmp_file
.write(static_cast<void*>(buffer
), sizeof(buffer
), written
);
3631 CPPUNIT_ASSERT_EQUAL_MESSAGE("write failed!", osl::FileBase::E_None
, nError1
);
3633 // set the file to readonly
3634 nError2
= File::setAttributes(aTmpName6
, osl_File_Attribute_ReadOnly
| osl_File_Attribute_GrpRead
| osl_File_Attribute_OwnRead
| osl_File_Attribute_OthRead
);
3635 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
3637 nError2
= tmp_file
.sync();
3639 CPPUNIT_ASSERT_EQUAL_MESSAGE("can not sync to readonly file!", osl::FileBase::E_None
, nError2
);
3644 // test case:no enough space, how to create such case???see test_cpy_wrt_file.cxx::test_osl_writeFile
3646 CPPUNIT_TEST_SUITE(sync
);
3647 CPPUNIT_TEST(sync_001
);
3648 CPPUNIT_TEST_SUITE_END();
3651 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::ctors
, "osl_File");
3652 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::open
, "osl_File");
3653 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::close
, "osl_File");
3654 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setPos
, "osl_File");
3655 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::getPos
, "osl_File");
3656 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::isEndOfFile
, "osl_File");
3657 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setSize
, "osl_File");
3658 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::read
, "osl_File");
3659 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::write
, "osl_File");
3660 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::readLine
, "osl_File");
3661 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::copy
, "osl_File");
3662 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::move
, "osl_File");
3663 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::remove
, "osl_File");
3664 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setAttributes
, "osl_File");
3665 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setTime
, "osl_File");
3666 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::sync
, "osl_File");
3667 // FIXME: to enable these tests (when they work cross-platform) we need to add the below:
3668 // CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_File");
3672 // Beginning of the test cases for DirectoryItem class
3674 namespace osl_DirectoryItem
3676 // testing the method
3677 // DirectoryItem(): _pData(NULL)
3679 class ctors
: public CppUnit::TestFixture
3682 osl::FileBase::RC nError1
;
3685 ctors() : nError1(osl::FileBase::E_None
) {}
3687 void setUp() override
3689 // create a tempfile in $TEMP/tmpname.
3690 createTestFile(aTmpName6
);
3693 void tearDown() override
3695 // remove the tempfile in $TEMP/tmpname.
3696 deleteTestFile(aTmpName6
);
3701 File
testFile(aTmpName6
);
3702 DirectoryItem rItem
; // constructor
3704 // get the DirectoryItem.
3705 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3706 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3708 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a new instance of DirectoryItem and get an item to check.",
3709 osl::FileBase::E_None
, nError1
);
3712 CPPUNIT_TEST_SUITE(ctors
);
3713 CPPUNIT_TEST(ctors_001
);
3714 CPPUNIT_TEST_SUITE_END();
3717 // testing the method
3718 // DirectoryItem(const DirectoryItem& rItem): _pData(rItem._pData)
3720 class copy_assin_Ctors
: public CppUnit::TestFixture
3723 osl::FileBase::RC nError1
;
3726 copy_assin_Ctors() : nError1(osl::FileBase::E_None
) {}
3728 void setUp() override
3730 // create a tempfile in $TEMP/tmpname.
3731 createTestFile(aTmpName6
);
3734 void tearDown() override
3736 // remove the tempfile in $TEMP/tmpname.
3737 deleteTestFile(aTmpName6
);
3741 void copy_assin_Ctors_001()
3743 DirectoryItem rItem
; // constructor
3744 // get the DirectoryItem.
3745 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3746 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3748 DirectoryItem
copyItem(rItem
); // copy constructor
3749 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3750 nError1
= copyItem
.getFileStatus(rFileStatus
);
3751 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3753 CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: use copy constructor to get an item and check filename.",
3754 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3757 void copy_assin_Ctors_002()
3759 DirectoryItem rItem
; // constructor
3760 // get the DirectoryItem.
3761 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3762 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3764 DirectoryItem copyItem
;
3765 copyItem
= rItem
; // assignment operator
3766 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3767 nError1
= copyItem
.getFileStatus(rFileStatus
);
3768 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3770 CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: test assignment operator here since it is same as copy constructor in test way.",
3771 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3774 CPPUNIT_TEST_SUITE(copy_assin_Ctors
);
3775 CPPUNIT_TEST(copy_assin_Ctors_001
);
3776 CPPUNIT_TEST(copy_assin_Ctors_002
);
3777 CPPUNIT_TEST_SUITE_END();
3780 // testing the method
3781 // inline sal_Bool is()
3783 class is
: public CppUnit::TestFixture
3786 osl::FileBase::RC nError1
;
3789 is() : nError1(osl::FileBase::E_None
) {}
3791 void setUp() override
3793 // create a tempfile in $TEMP/tmpname.
3794 createTestFile(aTmpName6
);
3797 void tearDown() override
3799 // remove the tempfile in $TEMP/tmpname.
3800 deleteTestFile(aTmpName6
);
3805 DirectoryItem rItem
; // constructor
3807 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3813 DirectoryItem rItem
; // constructor
3814 // get the DirectoryItem.
3815 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3816 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3818 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3822 CPPUNIT_TEST_SUITE(is
);
3823 CPPUNIT_TEST(is_001
);
3824 CPPUNIT_TEST(is_002
);
3825 CPPUNIT_TEST_SUITE_END();
3828 // testing the method
3829 // static inline RC get(const OUString& ustrFileURL, DirectoryItem& rItem)
3831 class get
: public CppUnit::TestFixture
3834 osl::FileBase::RC nError1
, nError2
;
3837 get() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
3839 void setUp() override
3841 // create a tempfile in $TEMP/tmpname.
3842 createTestFile(aTmpName6
);
3845 void tearDown() override
3847 // remove the tempfile in $TEMP/tmpname.
3848 deleteTestFile(aTmpName6
);
3854 DirectoryItem rItem
;
3855 nError2
= DirectoryItem::get(aTmpName6
, rItem
);
3857 // check the file name
3858 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3859 nError1
= rItem
.getFileStatus(rFileStatus
);
3860 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3862 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3863 osl::FileBase::E_None
, nError2
);
3864 CPPUNIT_ASSERT_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3865 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3870 DirectoryItem rItem
;
3871 nError1
= DirectoryItem::get(aSysPath1
, rItem
);
3873 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a system name instead of a URL.",
3874 osl::FileBase::E_INVAL
, nError1
);
3879 DirectoryItem rItem
;
3881 nError1
= DirectoryItem::get(aTmpName3
, rItem
);
3883 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a non existed file URL.",
3884 osl::FileBase::E_NOENT
, nError1
);
3887 CPPUNIT_TEST_SUITE(get
);
3888 CPPUNIT_TEST(get_001
);
3889 CPPUNIT_TEST(get_002
);
3890 CPPUNIT_TEST(get_003
);
3891 CPPUNIT_TEST_SUITE_END();
3894 // testing the method
3895 // inline RC getFileStatus(FileStatus& rStatus)
3897 class getFileStatus
: public CppUnit::TestFixture
3900 osl::FileBase::RC nError1
, nError2
;
3903 getFileStatus() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
3905 void setUp() override
3907 // create a tempfile in $TEMP/tmpdir/tmpname.
3908 createTestDirectory(aTmpName3
);
3909 createTestFile(aTmpName4
);
3912 void tearDown() override
3914 // remove the tempfile in $TEMP/tmpdir/tmpname.
3915 deleteTestFile(aTmpName4
);
3916 deleteTestDirectory(aTmpName3
);
3920 void getFileStatus_001()
3922 DirectoryItem rItem
;
3923 // get the DirectoryItem.
3924 nError1
= DirectoryItem::get(aTmpName4
, rItem
);
3925 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3927 // check the file name
3928 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3929 nError2
= rItem
.getFileStatus(rFileStatus
);
3931 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get file status and check filename",
3932 osl::FileBase::E_None
, nError2
);
3933 CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get file status and check filename",
3934 compareFileName(rFileStatus
.getFileName(), aTmpName2
));
3937 void getFileStatus_002()
3939 DirectoryItem rItem
; // constructor
3940 // get the DirectoryItem.
3941 nError1
= DirectoryItem::get(aTmpName6
, rItem
);
3943 // check the file name
3944 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3945 nError2
= rItem
.getFileStatus(rFileStatus
);
3947 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: file not existed",
3948 osl::FileBase::E_INVAL
, nError2
);
3951 void getFileStatus_003()
3953 DirectoryItem rItem
; // constructor
3954 // get the DirectoryItem.
3955 nError1
= DirectoryItem::get(aTmpName3
, rItem
);
3956 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
3958 // check the file name
3959 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
3960 nError2
= rItem
.getFileStatus(rFileStatus
);
3962 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get directory information",
3963 osl::FileBase::E_None
, nError2
);
3964 CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get directory information",
3965 compareFileName(rFileStatus
.getFileName(), aTmpName1
));
3968 CPPUNIT_TEST_SUITE(getFileStatus
);
3969 CPPUNIT_TEST(getFileStatus_001
);
3970 CPPUNIT_TEST(getFileStatus_002
);
3971 CPPUNIT_TEST(getFileStatus_003
);
3972 CPPUNIT_TEST_SUITE_END();
3975 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::ctors
, "osl_DirectoryItem");
3976 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::copy_assin_Ctors
, "osl_DirectoryItem");
3977 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::is
, "osl_DirectoryItem");
3978 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::get
, "osl_DirectoryItem");
3979 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::getFileStatus
, "osl_DirectoryItem");
3982 // Beginning of the test cases for Directory class
3984 namespace osl_Directory
3986 // testing the method
3987 // Directory(const OUString& strPath): _pData(0), _aPath(strPath)
3989 class ctors
: public CppUnit::TestFixture
3992 osl::FileBase::RC nError1
, nError2
;
3995 ctors() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
3997 void setUp() override
3999 // create a tempfile in $TEMP/tmpdir/tmpname.
4000 createTestDirectory(aTmpName3
);
4001 createTestFile(aTmpName4
);
4004 void tearDown() override
4006 // remove the tempfile in $TEMP/tmpdir/tmpname.
4007 deleteTestFile(aTmpName4
);
4008 deleteTestDirectory(aTmpName3
);
4009 // LLA: t_print("tearDown done.\n");
4015 Directory
testDirectory(aTmpName3
); // constructor
4018 nError1
= testDirectory
.open();
4019 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4020 // close a directory
4021 nError2
= testDirectory
.close();
4022 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4024 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
4025 osl::FileBase::E_None
, nError1
);
4026 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
4027 osl::FileBase::E_None
, nError2
);
4032 Directory
testDirectory(aTmpName9
); // constructor
4035 nError1
= testDirectory
.open();
4036 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4037 // close a directory
4038 nError2
= testDirectory
.close();
4039 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4041 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
4042 osl::FileBase::E_None
, nError1
);
4043 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
4044 osl::FileBase::E_None
, nError2
);
4047 CPPUNIT_TEST_SUITE(ctors
);
4048 CPPUNIT_TEST(ctors_001
);
4049 CPPUNIT_TEST(ctors_002
);
4050 CPPUNIT_TEST_SUITE_END();
4053 // testing the method
4056 class open
: public CppUnit::TestFixture
4059 osl::FileBase::RC nError1
, nError2
;
4062 open() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4064 void setUp() override
4066 // create a tempfile in $TEMP/tmpdir/tmpname.
4067 createTestDirectory(aTmpName3
);
4068 createTestFile(aTmpName4
);
4071 void tearDown() override
4073 // remove the tempfile in $TEMP/tmpdir/tmpname.
4074 deleteTestFile(aTmpName4
);
4075 deleteTestDirectory(aTmpName3
);
4080 Directory
testDirectory(aTmpName3
);
4083 nError1
= testDirectory
.open();
4084 // check if directory is opened.
4085 bool bOk
= testDirectory
.isOpen();
4086 // close a directory
4087 nError2
= testDirectory
.close();
4089 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory and check for open",
4091 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
4092 osl::FileBase::E_None
, nError1
);
4093 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
4094 osl::FileBase::E_None
, nError2
);
4099 Directory
testDirectory(aTmpName6
);
4101 nError1
= testDirectory
.open();
4102 if (nError1
== osl::FileBase::E_None
)
4104 nError2
= testDirectory
.close();
4105 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4108 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a file that is not existed",
4109 osl::FileBase::E_NOENT
, nError1
);
4114 Directory
testDirectory(aUserDirectorySys
);
4116 nError1
= testDirectory
.open();
4117 if (nError1
== osl::FileBase::E_None
)
4119 nError2
= testDirectory
.close();
4120 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4123 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: using system path",
4124 osl::FileBase::E_INVAL
, nError1
);
4129 Directory
testDirectory(aTmpName4
);
4131 nError1
= testDirectory
.open();
4132 if (nError1
== osl::FileBase::E_None
)
4134 nError2
= testDirectory
.close();
4135 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4138 CPPUNIT_ASSERT_MESSAGE("test for open function: open a file instead of a directory",
4139 (osl::FileBase::E_NOTDIR
== nError1
) || (osl::FileBase::E_ACCES
== nError1
));
4142 CPPUNIT_TEST_SUITE(open
);
4143 CPPUNIT_TEST(open_001
);
4144 CPPUNIT_TEST(open_002
);
4145 CPPUNIT_TEST(open_003
);
4146 CPPUNIT_TEST(open_004
);
4147 CPPUNIT_TEST_SUITE_END();
4150 // testing the method
4151 // inline sal_Bool isOpen() { return _pData != NULL; };
4153 class isOpen
: public CppUnit::TestFixture
4156 osl::FileBase::RC nError1
, nError2
;
4159 isOpen() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4161 void setUp() override
4163 // create a tempfile in $TEMP/tmpdir/tmpname.
4164 createTestDirectory(aTmpName3
);
4165 createTestFile(aTmpName4
);
4168 void tearDown() override
4170 // remove the tempfile in $TEMP/tmpdir/tmpname.
4171 deleteTestFile(aTmpName4
);
4172 deleteTestDirectory(aTmpName3
);
4178 Directory
testDirectory(aTmpName3
); // constructor
4181 nError1
= testDirectory
.open();
4182 // check if directory is opened.
4183 bool bOk
= testDirectory
.isOpen();
4184 // close a directory
4185 nError2
= testDirectory
.close();
4187 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: open a directory and check for open",
4193 Directory
testDirectory(aTmpName3
); // constructor
4195 // check if directory is opened.
4196 bool bOk
= testDirectory
.isOpen();
4198 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: do not open a directory and check for open",
4202 CPPUNIT_TEST_SUITE(isOpen
);
4203 CPPUNIT_TEST(isOpen_001
);
4204 CPPUNIT_TEST(isOpen_002
);
4205 CPPUNIT_TEST_SUITE_END();
4208 // testing the method
4209 // inline RC close()
4211 class close
: public CppUnit::TestFixture
4214 osl::FileBase::RC nError1
, nError2
;
4217 close() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4219 void setUp() override
4221 // create a tempdirectory : $TEMP/tmpdir.
4222 createTestDirectory(aTmpName3
);
4225 void tearDown() override
4227 // remove a tempdirectory : $TEMP/tmpdir.
4228 deleteTestDirectory(aTmpName3
);
4233 Directory
testDirectory(aTmpName3
);
4236 nError1
= testDirectory
.open();
4237 // close a directory
4238 nError2
= testDirectory
.close();
4239 // check if directory is opened.
4240 bool bOk
= testDirectory
.isOpen();
4242 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: close a directory and check for open",
4248 Directory
testDirectory(aTmpName3
);
4250 // close a directory
4251 nError1
= testDirectory
.close();
4253 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isOpen function: close a not opened directory",
4254 osl::FileBase::E_BADF
, nError1
);
4257 CPPUNIT_TEST_SUITE(close
);
4258 CPPUNIT_TEST(close_001
);
4259 CPPUNIT_TEST(close_002
);
4260 CPPUNIT_TEST_SUITE_END();
4263 // testing the method
4264 // inline RC reset()
4266 class reset
: public CppUnit::TestFixture
4269 osl::FileBase::RC nError1
, nError2
;
4270 DirectoryItem rItem
;
4273 reset() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4275 void setUp() override
4277 // create a tempdirectory : $TEMP/tmpdir.
4278 createTestDirectory(aTmpName3
);
4279 // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4280 createTestFile(aTmpName3
, aTmpName2
);
4281 createTestFile(aTmpName3
, aTmpName1
);
4282 createTestFile(aTmpName3
, aHidURL1
);
4285 void tearDown() override
4287 // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4288 deleteTestFile(aTmpName3
, aHidURL1
);
4289 deleteTestFile(aTmpName3
, aTmpName1
);
4290 deleteTestFile(aTmpName3
, aTmpName2
);
4291 // remove a tempdirectory : $TEMP/tmpdir.
4292 deleteTestDirectory(aTmpName3
);
4298 Directory
testDirectory(aTmpName3
); // constructor
4301 nError1
= testDirectory
.open();
4302 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4304 nError1
= testDirectory
.getNextItem(rItem
, 1);
4305 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4306 // check the file name of first Item
4307 FileStatus
rFileStatusFirst(osl_FileStatus_Mask_FileName
);
4308 nError1
= rItem
.getFileStatus(rFileStatusFirst
);
4311 // mindy: nError1 = testDirectory.getNextItem(rItem, 0);
4312 // mindy: CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4314 // reset enumeration
4315 nError2
= testDirectory
.reset();
4316 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError2
);
4317 // get reset Item, if reset does not work, getNextItem() should return the second Item (aTmpName1)
4318 nError1
= testDirectory
.getNextItem(rItem
);
4319 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4321 // check the file name again
4322 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
4323 nError1
= rItem
.getFileStatus(rFileStatus
);
4324 // close a directory
4325 nError1
= testDirectory
.close();
4326 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4328 bool bOK1
,bOK2
,bOK3
;
4329 bOK1
= compareFileName(rFileStatus
.getFileName(), aTmpName2
);
4330 bOK2
= compareFileName(rFileStatus
.getFileName(), aHidURL1
);
4331 bOK3
= compareFileName(rFileStatus
.getFileName(), rFileStatusFirst
.getFileName());
4332 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4333 osl::FileBase::E_None
, nError2
);
4334 CPPUNIT_ASSERT_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4335 (bOK1
|| bOK2
|| bOK3
));
4340 Directory
testDirectory(aTmpName6
); // constructor
4342 // close a directory
4343 nError1
= testDirectory
.reset();
4345 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: reset a non existed directory",
4346 osl::FileBase::E_NOENT
, nError1
);
4351 Directory
testDirectory(aTmpName4
); // constructor
4353 // close a directory
4354 nError1
= testDirectory
.reset();
4356 CPPUNIT_ASSERT_MESSAGE("test for reset function: reset a file instead of a directory",
4357 (osl::FileBase::E_NOTDIR
== nError1
) || (osl::FileBase::E_NOENT
== nError1
));
4362 Directory
testDirectory(aUserDirectorySys
); // constructor
4364 // close a directory
4365 nError1
= testDirectory
.reset();
4367 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: use a system path",
4368 osl::FileBase::E_INVAL
, nError1
);
4371 CPPUNIT_TEST_SUITE(reset
);
4372 CPPUNIT_TEST(reset_001
);
4373 CPPUNIT_TEST(reset_002
);
4374 CPPUNIT_TEST(reset_003
);
4375 CPPUNIT_TEST(reset_004
);
4376 CPPUNIT_TEST_SUITE_END();
4379 // testing the method
4380 // inline RC getNextItem(DirectoryItem& rItem, sal_uInt32 nHint = 0)
4382 class getNextItem
: public CppUnit::TestFixture
4385 osl::FileBase::RC nError1
, nError2
;
4386 DirectoryItem rItem
;
4389 getNextItem() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4391 void setUp() override
4393 // create a tempdirectory : $TEMP/tmpdir.
4394 createTestDirectory(aTmpName3
);
4395 // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4396 createTestFile(aTmpName3
, aTmpName2
);
4397 createTestFile(aTmpName3
, aTmpName1
);
4398 createTestFile(aTmpName3
, aHidURL1
);
4402 void tearDown() override
4404 // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4405 deleteTestFile(aTmpName3
, aHidURL1
);
4406 deleteTestFile(aTmpName3
, aTmpName1
);
4407 deleteTestFile(aTmpName3
, aTmpName2
);
4408 // remove a tempdirectory : $TEMP/tmpdir.
4409 deleteTestDirectory(aTmpName3
);
4413 void getNextItem_001()
4415 Directory
testDirectory(aTmpName3
); // constructor
4418 nError1
= testDirectory
.open();
4419 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4421 // check the file name
4425 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
);
4427 for (int nCount
= 0; nCount
< 3; nCount
++)
4430 nError1
= testDirectory
.getNextItem(rItem
, 2);
4431 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4432 nError1
= rItem
.getFileStatus(rFileStatus
);
4433 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4435 // a special order is not guaranteed. So any file may occur on any time.
4436 // But every file name should occur only once.
4437 if (!bOk1
&& compareFileName(rFileStatus
.getFileName(), aTmpName1
))
4442 if (!bOk2
&& compareFileName(rFileStatus
.getFileName(), aTmpName2
))
4447 if (!bOk3
&& compareFileName(rFileStatus
.getFileName(), aHidURL1
))
4453 // close a directory
4454 nError1
= testDirectory
.close();
4455 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4457 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4459 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4461 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4465 void getNextItem_002()
4467 Directory
testDirectory(aTmpName3
); // constructor
4468 nError1
= testDirectory
.getNextItem(rItem
);
4470 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.",
4471 osl::FileBase::E_INVAL
, nError1
);
4474 void getNextItem_003()
4476 Directory
testDirectory(aTmpName3
); // constructor
4479 nError1
= testDirectory
.open();
4480 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4482 for (int nCount
= 0; nCount
< 4; nCount
++)
4484 nError2
= testDirectory
.getNextItem(rItem
, 3);
4487 // close a directory
4488 nError1
= testDirectory
.close();
4489 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4491 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getNextItem function: retrieve 4 times in a directory which contain only 3 files.",
4492 osl::FileBase::E_NOENT
, nError2
);
4495 void getNextItem_004()
4497 // create a link file(can not on Windows), then check if getNextItem can get it.
4499 bool bLnkOK
= false;
4500 bool bFoundOK
= false;
4502 OUString
aUStr_LnkFileSys(aTempDirectorySys
), aUStr_SrcFileSys(aTempDirectorySys
);
4503 aUStr_LnkFileSys
+= aSlashURL
+ "/tmpdir/link.file";
4504 aUStr_SrcFileSys
+= aSlashURL
+ "/tmpdir/tmpname";
4506 OString strLinkFileName
, strSrcFileName
;
4507 strLinkFileName
= OUStringToOString(aUStr_LnkFileSys
, RTL_TEXTENCODING_ASCII_US
);
4508 strSrcFileName
= OUStringToOString(aUStr_SrcFileSys
, RTL_TEXTENCODING_ASCII_US
);
4510 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
4511 sal_Int32 fd
= symlink(strSrcFileName
.getStr(), strLinkFileName
.getStr());
4512 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32
>(0), fd
);
4513 Directory
testDirectory(aTmpName3
);
4516 nError1
= testDirectory
.open();
4517 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4518 OUString
aFileName ("link.file");
4521 nError1
= testDirectory
.getNextItem(rItem
, 4);
4522 if (nError1
== osl::FileBase::E_None
) {
4523 FileStatus
rFileStatus(osl_FileStatus_Mask_FileName
| osl_FileStatus_Mask_Type
);
4524 rItem
.getFileStatus(rFileStatus
);
4525 if (compareFileName(rFileStatus
.getFileName(), aFileName
))
4528 if (rFileStatus
.getFileType() == FileStatus::Link
)
4538 fd
= std::remove(strLinkFileName
.getStr());
4539 CPPUNIT_ASSERT_EQUAL_MESSAGE("remove link file failed", static_cast<sal_Int32
>(0), fd
);
4540 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if can retrieve the link file name",
4542 CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if link file has file type link",
4547 CPPUNIT_TEST_SUITE(getNextItem
);
4548 CPPUNIT_TEST(getNextItem_001
);
4549 CPPUNIT_TEST(getNextItem_002
);
4550 CPPUNIT_TEST(getNextItem_003
);
4551 CPPUNIT_TEST(getNextItem_004
);
4552 CPPUNIT_TEST_SUITE_END();
4555 // testing the method
4556 // inline static RC getVolumeInfo(const OUString& ustrDirectoryURL, VolumeInfo& rInfo)
4558 class getVolumeInfo
: public CppUnit::TestFixture
4561 osl::FileBase::RC nError1
;
4564 getVolumeInfo() : nError1(osl::FileBase::E_None
) {}
4566 void checkValidMask(osl::VolumeInfo
const& _aVolumeInfo
, sal_Int32 _nMask
)
4568 if (_nMask
== osl_VolumeInfo_Mask_FileSystemName
)
4570 // get file system name
4571 OUString aFileSysName
= _aVolumeInfo
.getFileSystemName();
4573 bool bRes2
= compareFileName(aFileSysName
, aNullURL
);
4574 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: getVolumeInfo of root directory.",
4575 osl::FileBase::E_None
, nError1
);
4576 CPPUNIT_ASSERT_MESSAGE("test for getVolumeInfo function: getVolumeInfo of root directory.",
4580 if (_nMask
== osl_VolumeInfo_Mask_Attributes
)
4582 bool b1
= _aVolumeInfo
.getRemoteFlag();
4583 bool b2
= _aVolumeInfo
.getRemoveableFlag();
4584 bool b3
= _aVolumeInfo
.getCompactDiscFlag();
4585 bool b4
= _aVolumeInfo
.getFloppyDiskFlag();
4586 bool b5
= _aVolumeInfo
.getFixedDiskFlag();
4587 bool b6
= _aVolumeInfo
.getRAMDiskFlag();
4590 if (b1
) sAttr
= "Remote";
4591 if (b2
) sAttr
+= " Removeable";
4592 if (b3
) sAttr
+= " CDROM";
4593 if (b4
) sAttr
+= " Floppy";
4594 if (b5
) sAttr
+= " FixedDisk";
4595 if (b6
) sAttr
+= " RAMDisk";
4597 printf("Attributes: %s\n", sAttr
.getStr());
4599 if (_nMask
== osl_VolumeInfo_Mask_TotalSpace
)
4601 // within Linux, df / * 1024 bytes is the result
4602 sal_uInt64 nSize
= _aVolumeInfo
.getTotalSpace();
4603 printf("Total space: %" SAL_PRIuUINT64
"\n", nSize
);
4605 if (_nMask
== osl_VolumeInfo_Mask_UsedSpace
)
4607 sal_uInt64 nSize
= _aVolumeInfo
.getUsedSpace();
4608 printf(" Used space: %" SAL_PRIuUINT64
"\n", nSize
);
4610 if (_nMask
== osl_VolumeInfo_Mask_FreeSpace
)
4612 sal_uInt64 nSize
= _aVolumeInfo
.getFreeSpace();
4613 printf(" Free space: %" SAL_PRIuUINT64
"\n", nSize
);
4615 if (_nMask
== osl_VolumeInfo_Mask_MaxNameLength
)
4617 sal_uInt32 nLength
= _aVolumeInfo
.getMaxNameLength();
4618 printf("max name length: %" SAL_PRIuUINT32
"\n", nLength
);
4620 if (_nMask
== osl_VolumeInfo_Mask_MaxPathLength
)
4622 sal_uInt32 nLength
= _aVolumeInfo
.getMaxPathLength();
4623 printf("max path length: %" SAL_PRIuUINT32
"\n", nLength
);
4625 if (_nMask
== osl_VolumeInfo_Mask_FileSystemCaseHandling
)
4627 bool bIsCase
= _aVolumeInfo
.isCaseSensitiveFileSystem();
4628 printf("filesystem case sensitive: %s\n", bIsCase
? "yes" : "no");
4632 void checkVolumeInfo(sal_Int32 _nMask
)
4634 VolumeInfo
aVolumeInfo(_nMask
);
4635 // call getVolumeInfo here
4636 nError1
= Directory::getVolumeInfo(aVolURL1
, aVolumeInfo
);
4637 // LLA: IMHO it's not a bug, if VolumeInfo is not valid, it's a feature
4638 // LLA: CPPUNIT_ASSERT_MESSAGE("mask is not valid", sal_True == aVolumeInfo.isValid(_nMask));
4639 if (aVolumeInfo
.isValid(_nMask
))
4640 checkValidMask(aVolumeInfo
, _nMask
);
4643 void getVolumeInfo_001_1()
4645 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemName
;
4646 checkVolumeInfo(mask
);
4649 void getVolumeInfo_001_2()
4651 sal_Int32 mask
= osl_VolumeInfo_Mask_Attributes
;
4652 checkVolumeInfo(mask
);
4655 void getVolumeInfo_001_3()
4657 sal_Int32 mask
= osl_VolumeInfo_Mask_TotalSpace
;
4658 checkVolumeInfo(mask
);
4661 void getVolumeInfo_001_4()
4663 sal_Int32 mask
= osl_VolumeInfo_Mask_UsedSpace
;
4664 checkVolumeInfo(mask
);
4667 void getVolumeInfo_001_5()
4669 sal_Int32 mask
= osl_VolumeInfo_Mask_FreeSpace
;
4670 checkVolumeInfo(mask
);
4673 void getVolumeInfo_001_6()
4675 sal_Int32 mask
= osl_VolumeInfo_Mask_MaxNameLength
;
4676 checkVolumeInfo(mask
);
4679 void getVolumeInfo_001_7()
4681 sal_Int32 mask
= osl_VolumeInfo_Mask_MaxPathLength
;
4682 checkVolumeInfo(mask
);
4685 void getVolumeInfo_001_8()
4687 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemCaseHandling
;
4688 checkVolumeInfo(mask
);
4691 void getVolumeInfo_002()
4693 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemName
;
4694 VolumeInfo
aVolumeInfo(mask
);
4695 // call getVolumeInfo here
4697 // LLA: OUString aRootSysURL;
4698 // LLA: nError1 = osl::File::getFileURLFromSystemPath(aRootSys, aRootSysURL);
4700 // LLA: CPPUNIT_ASSERT_MESSAGE("can't convert root path to file url",
4701 // LLA: (osl::FileBase::E_NONE == nError1));
4703 nError1
= Directory::getVolumeInfo(aRootSys
, aVolumeInfo
);
4705 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: use system path as parameter.",
4706 osl::FileBase::E_INVAL
, nError1
);
4709 void getVolumeInfo_003()
4711 sal_Int32 mask
= osl_VolumeInfo_Mask_FileSystemName
;
4712 VolumeInfo
aVolumeInfo(mask
);
4713 // call getVolumeInfo here
4714 nError1
= Directory::getVolumeInfo(aTmpName3
, aVolumeInfo
);
4716 // LLA: in Windows, it reply no error, it did not pass in (W32).
4717 #if defined(UNX) && !defined(IOS)
4718 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: non-existence test. ",
4719 osl::FileBase::E_NOENT
, nError1
);
4723 CPPUNIT_TEST_SUITE(getVolumeInfo
);
4724 CPPUNIT_TEST(getVolumeInfo_001_1
);
4725 CPPUNIT_TEST(getVolumeInfo_001_2
);
4726 CPPUNIT_TEST(getVolumeInfo_001_3
);
4727 CPPUNIT_TEST(getVolumeInfo_001_4
);
4728 CPPUNIT_TEST(getVolumeInfo_001_5
);
4729 CPPUNIT_TEST(getVolumeInfo_001_6
);
4730 CPPUNIT_TEST(getVolumeInfo_001_7
);
4731 CPPUNIT_TEST(getVolumeInfo_001_8
);
4732 CPPUNIT_TEST(getVolumeInfo_002
);
4733 CPPUNIT_TEST(getVolumeInfo_003
);
4734 CPPUNIT_TEST_SUITE_END();
4737 // testing the method
4738 // inline static RC create(const OUString& ustrDirectoryURL)
4740 class create
: public CppUnit::TestFixture
4743 osl::FileBase::RC nError1
, nError2
;
4746 create() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4749 // create directory in $TEMP/tmpdir
4750 nError1
= Directory::create(aTmpName3
);
4751 // check for existence
4752 nError2
= Directory::create(aTmpName3
);
4754 deleteTestDirectory(aTmpName3
);
4756 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4757 osl::FileBase::E_None
, nError1
);
4758 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4759 osl::FileBase::E_EXIST
, nError2
);
4764 #if !defined(_WIN32) && !defined(MACOSX) && defined(SAL_UNX)
4765 if (geteuid() == 0) // don't test if building as root
4769 nError1
= osl::FileBase::createTempFile(nullptr, nullptr, &aTmpDir
);
4770 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File creation failed", osl::FileBase::E_None
, nError1
);
4772 nError1
= File::remove(aTmpDir
);
4773 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File removal failed", osl::FileBase::E_None
, nError1
);
4775 nError1
= Directory::create(aTmpDir
);
4776 OString sError
= "test for create function: create a directory '" +
4777 OUStringToOString(aTmpDir
, RTL_TEXTENCODING_ASCII_US
) +
4778 "' and check its existence.";
4779 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_None
, nError1
);
4780 osl_setFileAttributes(aTmpDir
.pData
, 0); // no access allowed now
4782 // Shouldn't be possible now to create a dir underneath it
4783 OUString aTmpSubLevel
= aTmpDir
+ "/notallowedhere";
4784 nError1
= Directory::create(aTmpSubLevel
);
4787 osl_setFileAttributes(aTmpDir
.pData
,
4788 osl_File_Attribute_OwnRead
|
4789 osl_File_Attribute_OwnWrite
|
4790 osl_File_Attribute_OwnExe
);
4791 deleteTestDirectory(aTmpDir
);
4792 sError
= "test for create function: create a directory under '" +
4793 OUStringToOString(aTmpDir
, RTL_TEXTENCODING_ASCII_US
) +
4794 "' for access test.";
4795 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_ACCES
, nError1
);
4801 // create directory in /tmpname
4802 nError1
= Directory::create(aSysPath1
);
4804 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory using system path.",
4805 osl::FileBase::E_INVAL
, nError1
);
4808 CPPUNIT_TEST_SUITE(create
);
4809 CPPUNIT_TEST(create_001
);
4810 CPPUNIT_TEST(create_002
);
4811 CPPUNIT_TEST(create_003
);
4812 CPPUNIT_TEST_SUITE_END();
4815 // testing the method
4816 // inline static RC remove(const OUString& ustrDirectoryURL)
4818 class remove
: public CppUnit::TestFixture
4821 osl::FileBase::RC nError1
, nError2
;
4824 remove() : nError1(osl::FileBase::E_None
),nError2(osl::FileBase::E_None
) {}
4828 // create directory in $TEMP/tmpdir
4829 nError1
= Directory::create(aTmpName3
);
4830 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4832 nError1
= Directory::remove(aTmpName3
);
4833 // check for existence
4834 Directory
rDirectory(aTmpName3
);
4835 nError2
= rDirectory
.open();
4837 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4838 osl::FileBase::E_None
, nError1
);
4839 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4840 osl::FileBase::E_NOENT
, nError2
);
4845 // create directory in $TEMP/tmpdir
4846 nError1
= Directory::create(aTmpName3
);
4847 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None
, nError1
);
4848 // try to remove it by system path
4849 nError1
= Directory::remove(aSysPath3
);
4850 // check for existence
4851 Directory
rDirectory(aTmpName3
);
4852 nError2
= rDirectory
.open();
4854 if (nError2
!= osl::FileBase::E_NOENT
)
4855 Directory::remove(aTmpName3
);
4857 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory by its system path, and check its existence.",
4858 osl::FileBase::E_INVAL
, nError1
);
4863 // try to remove a non-existed directory
4864 nError1
= Directory::remove(aTmpName6
);
4866 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: try to remove a non-existed directory.",
4867 osl::FileBase::E_NOENT
, nError1
);
4872 createTestFile(aTmpName6
);
4873 bool bExist
= ifFileExist(aTmpName6
);
4874 // try to remove file.
4875 nError1
= Directory::remove(aTmpName6
);
4876 deleteTestFile(aTmpName6
);
4878 CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4880 CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4881 (osl::FileBase::E_NOTDIR
== nError1
) || (osl::FileBase::E_NOENT
== nError1
));
4886 createTestDirectory(aTmpName3
);
4887 createTestFile(aTmpName4
);
4888 nError1
= Directory::remove(aTmpName3
);
4889 deleteTestFile(aTmpName4
);
4890 deleteTestDirectory(aTmpName3
);
4891 OString sError
= "test for remove function: try to remove a directory that is not empty." +
4892 errorToStr(nError1
);
4894 // 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.
4895 // EEXIST The directory contains entries other than those for "." and "..".
4896 printf("#Solaris test\n");
4897 CPPUNIT_ASSERT_MESSAGE(sError
.getStr(), (osl::FileBase::E_EXIST
== nError1
));
4899 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError
.getStr(), osl::FileBase::E_NOTEMPTY
, nError1
);
4903 CPPUNIT_TEST_SUITE(remove
);
4904 CPPUNIT_TEST(remove_001
);
4905 CPPUNIT_TEST(remove_002
);
4906 CPPUNIT_TEST(remove_003
);
4907 CPPUNIT_TEST(remove_004
);
4908 CPPUNIT_TEST(remove_005
);
4909 CPPUNIT_TEST_SUITE_END();
4912 // TEST Directory::createPath
4915 # define PATH_BUFFER_SIZE MAX_PATH
4917 # define PATH_BUFFER_SIZE PATH_MAX
4920 #define TEST_PATH_POSTFIX "hello/world"
4922 static OUString
const & get_test_path()
4924 static OUString test_path
= [&]()
4927 osl::FileBase::RC rc
= osl::FileBase::getTempDirURL(tmp
);
4929 CPPUNIT_ASSERT_EQUAL_MESSAGE
4931 "Getting the location of TMP dir failed",
4932 osl::FileBase::E_None
, rc
4935 OUString system_path
;
4936 rc
= osl::FileBase::getSystemPathFromFileURL(tmp
, system_path
);
4938 CPPUNIT_ASSERT_EQUAL_MESSAGE
4940 "Cannot convert the TMP dir to system path",
4941 osl::FileBase::E_None
, rc
4944 OString
tmp_x(OUStringToOString(system_path
, RTL_TEXTENCODING_UTF8
));
4945 if (tmp_x
.lastIndexOf('/') != (tmp_x
.getLength() - 1))
4946 tmp_x
+= OString('/');
4948 #if !defined(_WIN32) && !defined(ANDROID) && !defined(AIX)
4949 // FIXME would be nice to create unique dir even on Windows
4951 char *out
= mkdtemp(const_cast<char*>(tmp_x
.getStr()));
4953 CPPUNIT_ASSERT_MESSAGE
4955 "mkdtemp call failed",
4959 tmp_x
+= OString('/');
4961 tmp_x
+= OString(TEST_PATH_POSTFIX
);
4963 OUString tmpTestPath
;
4964 rc
= osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x
, RTL_TEXTENCODING_UTF8
), tmpTestPath
);
4966 CPPUNIT_ASSERT_EQUAL_MESSAGE
4968 "Cannot convert the system path back to a URL",
4969 osl::FileBase::E_None
, rc
4976 static void rm_test_path(const OUString
& path
)
4978 sal_Unicode buffer
[PATH_BUFFER_SIZE
];
4979 memcpy(buffer
, path
.getStr(), (path
.getLength() + 1) * sizeof(sal_Unicode
));
4981 sal_Int32 i
= rtl_ustr_lastIndexOfChar(buffer
, '/');
4982 if (i
== path
.getLength())
4985 Directory::remove(buffer
);
4987 i
= rtl_ustr_lastIndexOfChar(buffer
, '/');
4992 Directory::remove(buffer
);
4996 class DirCreatedObserver
: public DirectoryCreationObserver
4999 DirCreatedObserver() : i(0) {}
5000 virtual void DirectoryCreated(const OUString
&) override
{ i
++; };
5002 int number_of_dirs_created() const { return i
; }
5008 class createPath
: public CppUnit::TestFixture
5014 void with_relative_path()
5016 osl::FileBase::RC rc
= Directory::createPath(TEST_PATH_POSTFIX
);
5018 CPPUNIT_ASSERT_EQUAL_MESSAGE
5020 "osl_createDirectoryPath contract broken",
5021 osl::FileBase::E_INVAL
, rc
5025 void without_callback()
5027 OUString tp_url
= get_test_path();
5029 rm_test_path(tp_url
);
5031 osl::FileBase::RC rc
= Directory::createPath(tp_url
);
5033 rm_test_path(tp_url
);
5035 CPPUNIT_ASSERT_EQUAL_MESSAGE
5037 "osl_createDirectoryPath failed",
5038 osl::FileBase::E_None
, rc
5042 void with_callback()
5044 OUString tp_url
= get_test_path();
5046 rm_test_path(tp_url
);
5048 DirCreatedObserver
* observer
= new DirCreatedObserver
;
5049 osl::FileBase::RC rc
= Directory::createPath(tp_url
, observer
);
5050 int nDirs
= observer
->number_of_dirs_created();
5053 rm_test_path(tp_url
);
5055 CPPUNIT_ASSERT_EQUAL_MESSAGE
5057 "osl_createDirectoryPath failed",
5058 osl::FileBase::E_None
, rc
5060 CPPUNIT_ASSERT_MESSAGE
5062 "osl_createDirectoryPath failed",
5070 const char* get_unused_drive_letter()
5072 static const char m_aBuff
[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
5074 DWORD ld
= GetLogicalDrives();
5078 while ((ld
& i
) && (i
> 1))
5079 { i
= i
<< 1; j
++; }
5087 void at_invalid_logical_drive()
5089 const char* drv
= get_unused_drive_letter();
5090 char buff
[PATH_BUFFER_SIZE
];
5091 memset(buff
, 0, sizeof(buff
));
5093 strncpy(buff
, drv
, 1);
5094 strcat(buff
, ":\\");
5095 strcat(buff
, TEST_PATH_POSTFIX
);
5097 OUString path
= OUString::createFromAscii(buff
);
5099 osl::FileBase::getFileURLFromSystemPath(path
, tp_url
);
5101 osl::FileBase::RC rc
= Directory::createPath(tp_url
);
5103 CPPUNIT_ASSERT_MESSAGE
5105 "osl_createDirectoryPath doesn't fail on unused logical drive letters",
5106 rc
!= osl::FileBase::E_None
5111 CPPUNIT_TEST_SUITE(createPath
);
5112 CPPUNIT_TEST(with_relative_path
);
5113 CPPUNIT_TEST(without_callback
);
5114 CPPUNIT_TEST(with_callback
);
5116 CPPUNIT_TEST(at_invalid_logical_drive
);
5118 CPPUNIT_TEST_SUITE_END();
5122 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::ctors
);
5123 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::open
);
5124 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::isOpen
);
5125 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::close
);
5126 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::reset
);
5127 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getNextItem
);
5128 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getVolumeInfo
);
5129 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::create
);
5130 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::remove
);
5131 CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::createPath
);
5134 /** get Current PID.
5136 OUString
getCurrentPID()
5138 //~ Get current PID and turn it into OUString;
5141 nPID
= GetCurrentProcessId();
5145 return OUString::number(nPID
);
5148 //~ do some clean up work after all test completed.
5156 //~ special clean up task in Windows and Unix separately;
5158 //~ some clean up task for UNIX OS
5161 //~ some clean up task for Windows OS
5162 //~ check if some files are in the way, remove them if necessary.
5163 if (ifFileExist(aTmpName6
) == sal_True
)
5164 deleteTestFile(aTmpName6
);
5165 if (ifFileExist(aTmpName4
) == sal_True
)
5166 deleteTestFile(aTmpName4
);
5167 if (checkDirectory(aTmpName4
, osl_Check_Mode_Exist
) == sal_True
)
5168 deleteTestDirectory(aTmpName4
);
5169 if (ifFileExist(aTmpName3
) == sal_True
)
5170 deleteTestFile(aTmpName3
);
5171 if (checkDirectory(aTmpName3
, osl_Check_Mode_Exist
) == sal_True
)
5172 deleteTestDirectory(aTmpName3
);
5174 OUString
aUStr(aUserDirectoryURL
);
5175 concatURL(aUStr
, aHidURL1
);
5176 if (ifFileExist(aUStr
) == sal_True
)
5177 deleteTestFile(aUStr
);
5179 OUString
aUStr1(aRootURL
);
5180 concatURL(aUStr1
, aTmpName2
);
5181 if (ifFileExist(aUStr1
) == sal_True
)
5182 deleteTestFile(aUStr1
);
5185 catch (const CppUnit::Exception
&e
)
5187 printf("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e
.what(), e
.sourceLine().lineNumber());
5191 printf("Exception caught (...) in GlobalObject dtor()\n");
5196 static GlobalObject theGlobalObject
;
5198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */