1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/types.h>
21 #include <rtl/byteseq.hxx>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
24 
25 #include <osl/thread.h>
26 #include <osl/file.hxx>
27 #include "osl_File_Const.h"
28 
29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/plugin/TestPlugIn.h>
32 
33 #include <tools/urlobj.hxx>
34 
35 #include <memory>
36 
37 #ifdef _WIN32
38 #include <prewin.h>
39 #include <postwin.h>
40 #include <o3tl/char16_t2wchar_t.hxx>
41 #endif
42 
43 using namespace osl;
44 
45 /** detailed wrong message.
46 */
errorToString(const osl::FileBase::RC _nError)47 static OString errorToString(const osl::FileBase::RC _nError)
48 {
49     OString sResult;
50     switch (_nError) {
51         case osl::FileBase::E_None:
52             sResult = "Success";
53             break;
54         case osl::FileBase::E_PERM:
55             sResult = "Operation not permitted";
56             break;
57         case osl::FileBase::E_NOENT:
58             sResult = "No such file or directory";
59             break;
60         case osl::FileBase::E_EXIST:
61             sResult = "Already Exist";
62             break;
63         case osl::FileBase::E_ACCES:
64             sResult = "Permission denied";
65             break;
66         case osl::FileBase::E_INVAL:
67             sResult = "The format of the parameters was not valid";
68             break;
69         case osl::FileBase::E_NOTDIR:
70             sResult = "Not a directory";
71             break;
72         case osl::FileBase::E_ISDIR:
73             sResult = "Is a directory";
74             break;
75         case osl::FileBase::E_BADF:
76             sResult = "Bad file";
77             break;
78         case osl::FileBase::E_NOTEMPTY:
79             sResult = "The directory is not empty";
80             break;
81         default:
82             sResult = "Unknown Error";
83             break;
84     }
85     return sResult;
86 }
87 
errorToStr(osl::FileBase::RC const & nError)88 static OString errorToStr(osl::FileBase::RC const& nError)
89 {
90     OString suBuf = "The returned error is: " +
91         errorToString(nError) +
92         "!\n";
93     return suBuf;
94 }
95 
96 /** compare two TimeValue, unit is "ms", since Windows time precision is better than UNX.
97 */
98 /* FIXME: the above assertion is bogus */
99 
100 #if (defined UNX)                      // precision of time in Windows is better than UNX
101 #   define delta 2000                    // time precision, 2000ms
102 #else
103 #   define delta 1800                    // time precision, 1.8s
104 #endif
105 
t_compareTime(TimeValue * m_aEndTime,TimeValue * m_aStartTime,sal_Int32 nDelta)106 static bool t_compareTime(TimeValue *m_aEndTime,  TimeValue *m_aStartTime, sal_Int32 nDelta)
107 {
108     sal_Int32 nDeltaSeconds = m_aEndTime->Seconds - m_aStartTime->Seconds;
109     sal_Int32 nDeltaNanoSec = sal_Int32(m_aEndTime->Nanosec) - sal_Int32(m_aStartTime->Nanosec);
110     if (nDeltaNanoSec < 0)
111     {
112         nDeltaNanoSec = 1000000000 + nDeltaNanoSec;
113         nDeltaSeconds--;
114     }
115 
116     sal_Int32 nDeltaMilliSec = (nDeltaSeconds * 1000) + (nDeltaNanoSec / 1000000);
117     return (nDeltaMilliSec < nDelta);
118 }
119 
120 /** compare two OUString file name.
121 */
compareFileName(const OUString & ustr1,const OUString & ustr2)122 static bool compareFileName(const OUString & ustr1, const OUString & ustr2)
123 {
124     bool bOk;
125 // on Windows, the separator is '\', so here change to '/', then compare
126 #if defined(_WIN32)
127     OUString ustr1new,ustr2new;
128     sal_Unicode reverseSlash = '\\';
129 
130     if (ustr1.lastIndexOf(reverseSlash) != -1)
131         ustr1new = ustr1.replace(reverseSlash,'/');
132     else
133         ustr1new = ustr1;
134     if (ustr2.lastIndexOf(reverseSlash) != -1)
135         ustr2new = ustr2.replace(reverseSlash,'/');
136     else
137         ustr2new = ustr2;
138     bOk = ustr1new.equalsIgnoreAsciiCase(ustr2new);
139 #else
140     bOk = ustr1.equalsIgnoreAsciiCase(ustr2);
141 #endif
142     return bOk;
143 }
144 
145 /** simple version to judge if a file name or directory name is a URL or a system path, just to see if it
146     is start with "file:///";.
147 */
isURL(const OUString & pathname)148 static bool isURL(const OUString& pathname)
149 {
150     return pathname.startsWith(aPreURL);
151 }
152 
153 /** concat two part to form a URL or system path, add PATH_SEPARATOR between them if necessary, add "file:///" to beginning if necessary.
154 */
concatURL(OUString & pathname1,const OUString & pathname2)155 static void concatURL(OUString & pathname1, const OUString & pathname2)
156 {
157     // check if pathname1 is full qualified URL;
158     if (!isURL(pathname1))
159     {
160         OUString     aPathName   = pathname1.copy(0);
161         osl::FileBase::getFileURLFromSystemPath(pathname1, aPathName); // convert if not full qualified URL
162         pathname1   = aPathName.copy(0);
163     }
164 
165     // check if '/' is in the end of pathname1 or at the begin of pathname2;
166     if (!pathname1.endsWith(aSlashURL) && !pathname2.startsWith(aSlashURL))
167         pathname1 += aSlashURL;
168     pathname1 += pathname2;
169 }
170 
171 /** create a temp test file using OUString name of full qualified URL or system path.
172 */
createTestFile(const OUString & filename)173 static void createTestFile(const OUString& filename)
174 {
175     OUString     aPathURL   = filename.copy(0);
176     osl::FileBase::RC nError;
177 
178     if (!isURL(filename))
179         osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
180 
181     File aFile(aPathURL);
182     nError = aFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
183     if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST))
184         printf("createTestFile failed!\n");
185 
186     aFile.close();
187 
188 }
189 
190 /** create a temp test file using OUString name of full qualified URL or system path in a base directory.
191 */
createTestFile(const OUString & basename,const OUString & filename)192 static void createTestFile(const OUString& basename, const OUString& filename)
193 {
194     OUString aBaseURL = basename.copy(0);
195 
196     concatURL(aBaseURL, filename);
197     createTestFile(aBaseURL);
198 }
199 
200 /** delete a temp test file using OUString name.
201 */
deleteTestFile(const OUString & filename)202 static void deleteTestFile(const OUString& filename)
203 {
204     OUString     aPathURL   = filename.copy(0);
205     osl::FileBase::RC nError;
206 
207     if (!isURL(filename))
208         osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
209 
210     nError = File::setAttributes(aPathURL, osl_File_Attribute_GrpWrite| osl_File_Attribute_OwnWrite| osl_File_Attribute_OthWrite); // if readonly, make writable.
211     CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: set writable ", (osl::FileBase::E_None == nError) || (osl::FileBase::E_NOENT == nError));
212 
213     nError = File::remove(aPathURL);
214     CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: remove ", (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT));
215 }
216 
217 /** delete a temp test file using OUString name of full qualified URL or system path in a base directory.
218 */
deleteTestFile(const OUString & basename,const OUString & filename)219 static void deleteTestFile(const OUString& basename, const OUString& filename)
220 {
221     OUString aBaseURL   = basename.copy(0);
222 
223     concatURL(aBaseURL, filename);
224     deleteTestFile(aBaseURL);
225 }
226 
227 /** create a temp test directory using OUString name of full qualified URL or system path.
228 */
createTestDirectory(const OUString & dirname)229 static void createTestDirectory(const OUString& dirname)
230 {
231     OUString aPathURL   = dirname.copy(0);
232     osl::FileBase::RC nError;
233 
234     if (!isURL(dirname))
235         osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
236     nError = Directory::create(aPathURL);
237     if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST))
238         printf("createTestDirectory failed: %d!\n", int(nError));
239 }
240 
241 /** create a temp test directory using OUString name of full qualified URL or system path in a base directory.
242 */
createTestDirectory(const OUString & basename,const OUString & dirname)243 static void createTestDirectory(const OUString& basename, const OUString& dirname)
244 {
245     OUString aBaseURL   = basename.copy(0);
246 
247     concatURL(aBaseURL, dirname);
248     createTestDirectory(aBaseURL);
249 }
250 
251 /** delete a temp test directory using OUString name of full qualified URL or system path.
252 */
deleteTestDirectory(const OUString & dirname)253 static void deleteTestDirectory(const OUString& dirname)
254 {
255     OUString aPathURL = dirname.copy(0);
256     if (!isURL(dirname))
257         osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
258 
259     Directory testDir(aPathURL);
260     if (testDir.isOpen())
261         testDir.close();  // close if still open.
262 
263     osl::FileBase::RC nError = Directory::remove(aPathURL);
264 
265     OString strError = "In deleteTestDirectory function: remove Directory " +
266         OUStringToOString(aPathURL, RTL_TEXTENCODING_ASCII_US) + " -> result: " + OString::number(nError);
267     CPPUNIT_ASSERT_MESSAGE(strError.getStr(), (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT));
268 }
269 
270 /** delete a temp test directory using OUString name of full qualified URL or system path in a base directory.
271 */
deleteTestDirectory(const OUString & basename,const OUString & dirname)272 static void deleteTestDirectory(const OUString& basename, const OUString& dirname)
273 {
274     OUString aBaseURL   = basename.copy(0);
275 
276     concatURL(aBaseURL, dirname);
277     deleteTestDirectory(aBaseURL);
278 }
279 
280 namespace {
281 
282 /** Check for the file and directory access right.
283 */
284 enum class oslCheckMode {
285     Exist,
286     OpenAccess,
287     ReadAccess,
288     WriteAccess
289 };
290 
291 }
292 
293 /** check if the file exist
294 */
ifFileExist(const OUString & str)295 static bool ifFileExist(const OUString & str)
296 {
297     File testFile(str);
298     return (testFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None);
299 }
300 
301 /** check if the file can be written
302 */
ifFileCanWrite(const OUString & str)303 static bool ifFileCanWrite(const OUString & str)
304 {
305     // on Windows, the file has no write right, but can be written
306 #ifdef _WIN32
307     bool bCheckResult = false;
308     OUString aUStr = str.copy(0);
309     if (isURL(str))
310         osl::FileBase::getSystemPathFromFileURL(str, aUStr);
311 
312     OString aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
313     const char *path = aString.getStr();
314     if ((_access(path, 2)) != -1)
315          bCheckResult = true;
316      // on UNX, just test if open success with osl_File_OpenFlag_Write
317 #else
318     File testFile(str);
319     bool bCheckResult = (testFile.open(osl_File_OpenFlag_Write) == osl::FileBase::E_None);
320 #endif
321     return bCheckResult;
322 }
323 
checkDirectory(const OUString & str,oslCheckMode nCheckMode)324 static bool checkDirectory(const OUString& str, oslCheckMode nCheckMode)
325 {
326     OUString aUString;
327     DirectoryItem rItem;
328     osl::FileBase::RC rc;
329     bool bCheckResult= false;
330 
331     Directory aDir(str);
332     rc = aDir.open();
333 
334     if ((rc != osl::FileBase::E_NOENT) && (rc != osl::FileBase::E_ACCES))
335     {
336         switch (nCheckMode)
337         {
338             case oslCheckMode::Exist:
339                 if (rc == ::osl::FileBase::E_None)
340                     bCheckResult = true;
341                 break;
342             case oslCheckMode::OpenAccess:
343                 if (rc == osl::FileBase::E_None)
344                     bCheckResult = true;
345                 break;
346             case oslCheckMode::ReadAccess:
347                 rc = aDir.getNextItem(rItem);
348                 bCheckResult = (rc == osl::FileBase::E_None) || (rc == osl::FileBase::E_NOENT);
349                 break;
350             case oslCheckMode::WriteAccess:
351                 ((aUString += str) += aSlashURL) += aTmpName2;
352                 if (Directory::create(aUString) == osl::FileBase::E_None)
353                 {
354                     bCheckResult = true;
355                     rc = Directory::remove(aUString);
356                     CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
357                 }
358                 else
359                 {
360                     bCheckResult = false;
361                 }
362                 break;
363 
364             default:
365                 bCheckResult = false;
366         }
367 
368         rc = aDir.close();
369         CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
370     }
371 
372     return bCheckResult;
373 }
374 
375 /** construct error message
376 */
outputError(const OString & returnVal,const OString & rightVal,const char * msg="")377 static OString outputError(const OString & returnVal, const OString & rightVal, const char * msg = "")
378 {
379     if (returnVal == rightVal)
380         return OString();
381 
382     OString aString = msg +
383         OString::Concat(": the returned value is '") +
384         returnVal +
385         "', but the value should be '" +
386         rightVal +
387         "'.";
388     return aString;
389 }
390 
391 #if (defined UNX) /* chmod() method is different in Windows */
392 /** Change file mode, two version in UNIX and Windows;.
393 */
changeFileMode(OUString & filepath,sal_Int32 mode)394 static void changeFileMode(OUString & filepath, sal_Int32 mode)
395 {
396     OString aString;
397     OUString aUStr = filepath.copy(0);
398 
399     if (isURL(filepath))
400         osl::FileBase::getSystemPathFromFileURL(filepath, aUStr);
401 
402     aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
403     int ret = chmod(aString.getStr(), mode);
404     CPPUNIT_ASSERT_EQUAL(0, ret);
405 }
406 #else
hideFile(const OUString & filepath)407 static void hideFile(const OUString& filepath)
408 {
409     OUString aSysPath(filepath);
410 
411     if (isURL(filepath))
412         osl::FileBase::getSystemPathFromFileURL(filepath, aSysPath);
413 
414     bool ret = SetFileAttributesW(o3tl::toW(aSysPath.getStr()), FILE_ATTRIBUTE_HIDDEN);
415     CPPUNIT_ASSERT(ret);
416 }
417 #endif
418 
419 #if 0
420 #if defined UNX
421 static OUString getCurrentPID();
422 #endif
423 #endif
424 
425 // Beginning of the test cases for osl::FileBase class
426 
427 namespace osl_FileBase
428 {
429     // testing the method
430     // static inline RC getAbsoluteFileURL(const OUString& ustrBaseDirectoryURL,
431     //                                      const OUString& ustrRelativeFileURL,
432     //                                      OUString& ustrAbsoluteFileURL)
433 
434     class getAbsoluteFileURL : public CppUnit::TestFixture
435     {
436     public:
437         void check_getAbsoluteFileURL(OUString const& _suBaseURL,
438                                       OString const& _sRelativeURL,
439                                       osl::FileBase::RC _nAssumeError,
440                                       OUString const& _suAssumeResultStr);
441 
442         void getAbsoluteFileURL_001_1();
443         void getAbsoluteFileURL_001_2();
444         void getAbsoluteFileURL_001_3();
445         void getAbsoluteFileURL_001_4();
446         void getAbsoluteFileURL_001_5();
447         void getAbsoluteFileURL_001_6();
448         void getAbsoluteFileURL_001_7();
449         void getAbsoluteFileURL_001_8();
450         void getAbsoluteFileURL_002();
451         void getAbsoluteFileURL_003();
452         void getAbsoluteFileURL_004();
453 
454         CPPUNIT_TEST_SUITE(getAbsoluteFileURL);
455             CPPUNIT_TEST(getAbsoluteFileURL_001_1);
456             CPPUNIT_TEST(getAbsoluteFileURL_001_2);
457             CPPUNIT_TEST(getAbsoluteFileURL_001_3);
458             CPPUNIT_TEST(getAbsoluteFileURL_001_4);
459             CPPUNIT_TEST(getAbsoluteFileURL_001_5);
460             CPPUNIT_TEST(getAbsoluteFileURL_001_6);
461             CPPUNIT_TEST(getAbsoluteFileURL_001_7);
462             CPPUNIT_TEST(getAbsoluteFileURL_001_8);
463             CPPUNIT_TEST(getAbsoluteFileURL_002);
464             CPPUNIT_TEST(getAbsoluteFileURL_003);
465             CPPUNIT_TEST(getAbsoluteFileURL_004);
466         CPPUNIT_TEST_SUITE_END();
467     };
468 
check_getAbsoluteFileURL(OUString const & _suBaseURL,OString const & _sRelativeURL,osl::FileBase::RC _nAssumeError,OUString const & _suAssumeResultStr)469     void getAbsoluteFileURL::check_getAbsoluteFileURL(OUString const& _suBaseURL,
470                                                       OString const& _sRelativeURL,
471                                                       osl::FileBase::RC _nAssumeError,
472                                                       OUString const& _suAssumeResultStr)
473     {
474         OUString suRelativeURL = OStringToOUString(_sRelativeURL, RTL_TEXTENCODING_UTF8);
475         OString sBaseURL = OUStringToOString(_suBaseURL, RTL_TEXTENCODING_UTF8);
476         OUString suResultURL;
477         osl::FileBase::RC nError = osl::FileBase::getAbsoluteFileURL(_suBaseURL, suRelativeURL, suResultURL);
478         OString sResultURL = OUStringToOString(suResultURL, RTL_TEXTENCODING_UTF8);
479         OString sError = errorToString(nError);
480         printf("getAbsoluteFileURL('%s','%s') deliver absolute URL: '%s', error '%s'\n",
481                 sBaseURL.getStr(), _sRelativeURL.getStr(),sResultURL.getStr(), sError.getStr());
482         CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: error number is wrong", _nAssumeError, nError);
483 
484         if (nError == osl::FileBase::E_None)
485         {
486             CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: ResultURL is not equal to expected URL ", _suAssumeResultStr, suResultURL);
487         }
488     }
489 
getAbsoluteFileURL_001_1()490     void getAbsoluteFileURL::getAbsoluteFileURL_001_1()
491     {
492         OUString suAssume = aUserDirectoryURL + "/relative/file1";
493         check_getAbsoluteFileURL(aUserDirectoryURL, "relative/file1",osl::FileBase::E_None, suAssume);
494     }
495 
getAbsoluteFileURL_001_2()496     void getAbsoluteFileURL::getAbsoluteFileURL_001_2()
497     {
498         OUString suAssume = aUserDirectoryURL + "/relative/file2";
499         check_getAbsoluteFileURL(aUserDirectoryURL, "relative/./file2",osl::FileBase::E_None, suAssume);
500     }
501 
getAbsoluteFileURL_001_3()502     void getAbsoluteFileURL::getAbsoluteFileURL_001_3()
503     {
504         OUString suAssume = aUserDirectoryURL + "/file3";
505         check_getAbsoluteFileURL(aUserDirectoryURL, "relative/../file3",osl::FileBase::E_None, suAssume);
506     }
507 
getAbsoluteFileURL_001_4()508     void getAbsoluteFileURL::getAbsoluteFileURL_001_4()
509     {
510         OUString suAssume = aUserDirectoryURL + "/file4";
511         check_getAbsoluteFileURL(aUserDirectoryURL, "././relative/../file4",osl::FileBase::E_None, suAssume);
512     }
513 
getAbsoluteFileURL_001_5()514     void getAbsoluteFileURL::getAbsoluteFileURL_001_5()
515     {
516         OUString suAssume;
517         suAssume = aUserDirectoryURL + "/relative/";
518         check_getAbsoluteFileURL(aUserDirectoryURL, "././relative/.",osl::FileBase::E_None, suAssume);
519     }
520 
getAbsoluteFileURL_001_6()521     void getAbsoluteFileURL::getAbsoluteFileURL_001_6()
522     {
523         OUString suAssume = aUserDirectoryURL + "/.relative";
524         check_getAbsoluteFileURL(aUserDirectoryURL, "./.relative",osl::FileBase::E_None, suAssume);
525     }
526 
getAbsoluteFileURL_001_7()527     void getAbsoluteFileURL::getAbsoluteFileURL_001_7()
528     {
529         OUString suAssume;
530         suAssume = aUserDirectoryURL + "/.a/";
531         check_getAbsoluteFileURL(aUserDirectoryURL, "./.a/mydir/..",osl::FileBase::E_None, suAssume);
532     }
533 
getAbsoluteFileURL_001_8()534     void getAbsoluteFileURL::getAbsoluteFileURL_001_8()
535     {
536         OUString suAssume = aUserDirectoryURL + "/tmp/ok";
537         check_getAbsoluteFileURL(aUserDirectoryURL, "tmp//ok",osl::FileBase::E_None, suAssume);
538     }
539 
getAbsoluteFileURL_002()540     void getAbsoluteFileURL::getAbsoluteFileURL_002()
541     {
542 #if 0
543 #if (defined UNX) // Link is not defined in Windows
544         OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
545         aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/link.file";
546         aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/canonical.name";
547 
548         OString strLinkFileName, strSrcFileName;
549         strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
550         strSrcFileName =  OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
551 
552         createTestFile(aCanURL1);
553         sal_Int32 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
554         CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
555         OString sLnkURL = OUStringToOString(aLnkURL1, RTL_TEXTENCODING_ASCII_US);
556         OUString suAssume = aUserDirectoryURL + "/canonical.name";
557         check_getAbsoluteFileURL(aUserDirectoryURL, sLnkURL, osl::FileBase::E_None, suAssume);
558         deleteTestFile(aCanURL1);
559         fd = remove(strLinkFileName.getStr());
560         CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
561 #endif
562 #endif
563     }
564 
565     // please see line# 930
getAbsoluteFileURL_003()566     void getAbsoluteFileURL::getAbsoluteFileURL_003()
567     {
568     }
569 
getAbsoluteFileURL_004()570     void getAbsoluteFileURL::getAbsoluteFileURL_004()
571     {
572         // create two level directories under $Temp/PID/
573         OUString aUStrUpBase = aUserDirectoryURL + "/test1";
574         createTestDirectory(aUStrUpBase);
575         OUString aUStrBase = aUserDirectoryURL + "/test1/dir1";
576         createTestDirectory(aUStrBase);
577 
578         OUString suAssume = aUserDirectoryURL + "/mytestfile";
579         check_getAbsoluteFileURL(aUStrBase, "../../mytestfile" , osl::FileBase::E_None, suAssume);
580         deleteTestDirectory(aUStrBase);
581         deleteTestDirectory(aUStrUpBase);
582     }
583 
584     // testing two methods:
585     // static inline RC getSystemPathFromFileURL(const OUString& ustrFileURL,
586     //                OUString& ustrSystemPath)
587     // static RC getFileURLFromSystemPath(const OUString & ustrSystemPath,
588     //                OUString & ustrFileURL);
589 
590     class SystemPath_FileURL : public CppUnit::TestFixture
591     {
592     public:
593         void getSystemPathFromFileURL_001_1();
594         void getSystemPathFromFileURL_001_2();
595         void getSystemPathFromFileURL_001_21();
596         void getSystemPathFromFileURL_001_22();
597         void getSystemPathFromFileURL_001_3();
598         void getSystemPathFromFileURL_001_31();
599         void getSystemPathFromFileURL_001_4();
600         void getSystemPathFromFileURL_001_41();
601         void getSystemPathFromFileURL_001_5();
602         void getSystemPathFromFileURL_001_51();
603         void getSystemPathFromFileURL_001_52();
604         void getSystemPathFromFileURL_001_53();
605         void getSystemPathFromFileURL_001_6();
606         void getSystemPathFromFileURL_001_61();
607         void getSystemPathFromFileURL_001_7();
608         void getSystemPathFromFileURL_001_71();
609         void getSystemPathFromFileURL_001_8();
610         void getSystemPathFromFileURL_001_81();
611         void getSystemPathFromFileURL_001_9();
612         void getSystemPathFromFileURL_001_91();
613         void getSystemPathFromFileURL_001_92();
614         void getSystemPathFromFileURL_004();
615         void getSystemPathFromFileURL_005();
616 
617         // test case for getFileURLFromSystemPath
618         void getFileURLFromSystemPath_001();
619         void getFileURLFromSystemPath_002();
620         void getFileURLFromSystemPath_003();
621         void getFileURLFromSystemPath_004();
622         void getFileURLFromSystemPath_004_1();
623         void getFileURLFromSystemPath_005();
624 
625         CPPUNIT_TEST_SUITE(SystemPath_FileURL);
626             CPPUNIT_TEST(getSystemPathFromFileURL_001_1);
627             CPPUNIT_TEST(getSystemPathFromFileURL_001_2);
628             CPPUNIT_TEST(getSystemPathFromFileURL_001_21);
629             CPPUNIT_TEST(getSystemPathFromFileURL_001_22);
630             CPPUNIT_TEST(getSystemPathFromFileURL_001_3);
631             CPPUNIT_TEST(getSystemPathFromFileURL_001_31);
632             CPPUNIT_TEST(getSystemPathFromFileURL_001_4);
633             CPPUNIT_TEST(getSystemPathFromFileURL_001_41);
634             CPPUNIT_TEST(getSystemPathFromFileURL_001_5);
635             CPPUNIT_TEST(getSystemPathFromFileURL_001_51);
636             CPPUNIT_TEST(getSystemPathFromFileURL_001_52);
637             CPPUNIT_TEST(getSystemPathFromFileURL_001_53);
638             CPPUNIT_TEST(getSystemPathFromFileURL_001_6);
639             CPPUNIT_TEST(getSystemPathFromFileURL_001_61);
640             CPPUNIT_TEST(getSystemPathFromFileURL_001_7);
641             CPPUNIT_TEST(getSystemPathFromFileURL_001_71);
642             CPPUNIT_TEST(getSystemPathFromFileURL_001_8);
643             CPPUNIT_TEST(getSystemPathFromFileURL_001_81);
644             CPPUNIT_TEST(getSystemPathFromFileURL_001_9);
645             CPPUNIT_TEST(getSystemPathFromFileURL_001_91);
646             CPPUNIT_TEST(getSystemPathFromFileURL_001_92);
647             CPPUNIT_TEST(getSystemPathFromFileURL_004);
648             CPPUNIT_TEST(getSystemPathFromFileURL_005);
649             CPPUNIT_TEST(getFileURLFromSystemPath_001);
650             CPPUNIT_TEST(getFileURLFromSystemPath_002);
651             CPPUNIT_TEST(getFileURLFromSystemPath_003);
652             CPPUNIT_TEST(getFileURLFromSystemPath_004);
653             CPPUNIT_TEST(getFileURLFromSystemPath_004_1);
654             CPPUNIT_TEST(getFileURLFromSystemPath_005);
655         CPPUNIT_TEST_SUITE_END();
656 
657     private:
658         void check_SystemPath_FileURL(
659                 OString const& _sSource,
660                 osl::FileBase::RC _nAssumeError,
661                 OString const& _sAssumeResultStr,
662                 bool bDirection = true);
663 
664         void checkWNTBehaviour_getSystemPathFromFileURL(
665                 OString const& _sURL,
666                 osl::FileBase::RC _nAssumeError,
667                 OString const& _sWNTAssumeResultString);
668 
669         void checkUNXBehaviour_getSystemPathFromFileURL(
670                 OString const& _sURL,
671                 osl::FileBase::RC _nAssumeError,
672                 OString const& _sUnixAssumeResultString);
673 
674         void checkWNTBehaviour_getFileURLFromSystemPath(OString const& _sSysPath,
675                 osl::FileBase::RC _nAssumeError,
676                 OString const& _sWNTAssumeResultString);
677 
678         void checkUNXBehaviour_getFileURLFromSystemPath(
679                 OString const& _sSysPath,
680                 osl::FileBase::RC _nAssumeError,
681                 OString const& _sUnixAssumeResultString);
682 
683     };
684 
685     // if bDirection==sal_True, check getSystemPathFromFileURL
686     // if bDirection==sal_False, check getFileURLFromSystemPath
check_SystemPath_FileURL(OString const & _sSource,osl::FileBase::RC _nAssumeError,OString const & _sAssumeResultStr,bool bDirection)687     void SystemPath_FileURL::check_SystemPath_FileURL(
688             OString const& _sSource,
689             osl::FileBase::RC _nAssumeError,
690             OString const& _sAssumeResultStr,
691             bool bDirection)
692     {
693         // PRE: URL as String
694         OUString suSource;
695         OUString suStr;
696         suSource = OStringToOUString(_sSource, RTL_TEXTENCODING_UTF8);
697         osl::FileBase::RC nError;
698 
699         if (bDirection)
700             nError = osl::FileBase::getSystemPathFromFileURL(suSource, suStr);
701         else
702             nError = osl::FileBase::getFileURLFromSystemPath(suSource, suStr);
703 
704         // if the given string is gt length 0,
705         // we check also this string
706         OString sStr = OUStringToOString(suStr, RTL_TEXTENCODING_UTF8);
707         OString sError = errorToString(nError);
708 
709         if (bDirection)
710             printf("getSystemPathFromFileURL('%s') deliver system path: '%s', error '%s'\n",
711                     _sSource.getStr(), sStr.getStr(), sError.getStr());
712         else
713             printf("getFileURLFromSystemPath('%s') deliver File URL: '%s', error '%s'\n",
714                     _sSource.getStr(), sStr.getStr(), sError.getStr());
715 
716         if (!_sAssumeResultStr.isEmpty())
717         {
718             bool bStrAreEqual = _sAssumeResultStr == sStr;
719             CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong",
720                                     _nAssumeError, nError);
721             CPPUNIT_ASSERT_MESSAGE("Assumption is wrong",
722                                     bStrAreEqual);
723         }
724         else
725         {
726             CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong", _nAssumeError, nError);
727         }
728     }
729 
checkWNTBehaviour_getSystemPathFromFileURL(OString const & _sURL,osl::FileBase::RC _nAssumeError,OString const & _sWNTAssumeResultString)730     void SystemPath_FileURL::checkWNTBehaviour_getSystemPathFromFileURL(
731             OString const& _sURL,
732             osl::FileBase::RC _nAssumeError,
733             OString const& _sWNTAssumeResultString)
734     {
735 #if defined(_WIN32)
736         check_SystemPath_FileURL(_sURL, _nAssumeError, _sWNTAssumeResultString);
737 #else
738         (void)_sURL;
739         (void)_nAssumeError;
740         (void)_sWNTAssumeResultString;
741 #endif
742     }
743 
checkUNXBehaviour_getSystemPathFromFileURL(OString const & _sURL,osl::FileBase::RC _nAssumeError,OString const & _sUnixAssumeResultString)744     void SystemPath_FileURL::checkUNXBehaviour_getSystemPathFromFileURL(
745             OString const& _sURL,
746             osl::FileBase::RC _nAssumeError,
747             OString const& _sUnixAssumeResultString)
748     {
749 #if (defined UNX)
750         check_SystemPath_FileURL(_sURL, _nAssumeError, _sUnixAssumeResultString);
751 #else
752         (void)_sURL;
753         (void)_nAssumeError;
754         (void)_sUnixAssumeResultString;
755 #endif
756     }
757 
checkWNTBehaviour_getFileURLFromSystemPath(OString const & _sSysPath,osl::FileBase::RC _nAssumeError,OString const & _sWNTAssumeResultString)758     void SystemPath_FileURL::checkWNTBehaviour_getFileURLFromSystemPath(
759             OString const& _sSysPath,
760             osl::FileBase::RC _nAssumeError,
761             OString const& _sWNTAssumeResultString)
762     {
763 #if defined(_WIN32)
764         check_SystemPath_FileURL(_sSysPath, _nAssumeError, _sWNTAssumeResultString, false);
765 #else
766         (void)_sSysPath;
767         (void)_nAssumeError;
768         (void)_sWNTAssumeResultString;
769 #endif
770     }
771 
checkUNXBehaviour_getFileURLFromSystemPath(OString const & _sSysPath,osl::FileBase::RC _nAssumeError,OString const & _sUnixAssumeResultString)772     void SystemPath_FileURL::checkUNXBehaviour_getFileURLFromSystemPath(
773             OString const& _sSysPath,
774             osl::FileBase::RC _nAssumeError,
775             OString const& _sUnixAssumeResultString)
776     {
777 #if (defined UNX)
778         check_SystemPath_FileURL(_sSysPath, _nAssumeError, _sUnixAssumeResultString, false);
779 #else
780         (void)_sSysPath;
781         (void)_nAssumeError;
782         (void)_sUnixAssumeResultString;
783 #endif
784     }
785 
786     /** Test for getSystemPathFromFileURL()
787         this test is split into 2 different OS tests,
788         the first function checkUNXBehaviour... runs only on Unix based Systems,
789         the second only on windows based systems
790         the first parameter are a file URL where we want to get the system path of,
791         the second parameter is the assumed error of the osl_getSystemPathFromFileURL() function,
792         the third parameter is the assumed result string, the string will only test, if its length is greater 0
793     */
794 
getSystemPathFromFileURL_001_1()795     void SystemPath_FileURL::getSystemPathFromFileURL_001_1()
796     {
797         OString sURL("");
798         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
799         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
800     }
801 
getSystemPathFromFileURL_001_2()802     void SystemPath_FileURL::getSystemPathFromFileURL_001_2()
803     {
804         OString sURL("/");
805         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
806         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "\\");
807     }
808 
getSystemPathFromFileURL_001_21()809     void SystemPath_FileURL::getSystemPathFromFileURL_001_21()
810     {
811         /* From RFC3986, "2.2. Reserved Characters":
812 
813            "The purpose of reserved characters is to provide a set of delimiting
814            characters that are distinguishable from other data within a URI.
815            URIs that differ in the replacement of a reserved character with its
816            corresponding percent-encoded octet are not equivalent.  Percent-
817            encoding a reserved character, or decoding a percent-encoded octet
818            that corresponds to a reserved character, will change how the URI is
819            interpreted by most applications.  Thus, characters in the reserved
820            set are protected from normalization and are therefore safe to be
821            used by scheme-specific and producer-specific algorithms for
822            delimiting data subcomponents within a URI."
823 
824            In other words, %2F ("/") is NOT the same as /.
825         */
826         OString sURL("%2F");
827         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
828         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
829     }
830 
getSystemPathFromFileURL_001_22()831     void SystemPath_FileURL::getSystemPathFromFileURL_001_22()
832     {
833         OString sURL("file:///tmp%2Fmydir");
834         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
835         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
836     }
837 
getSystemPathFromFileURL_001_3()838     void SystemPath_FileURL::getSystemPathFromFileURL_001_3()
839     {
840         OString sURL("a");
841         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "a");
842         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "a");
843     }
844 
getSystemPathFromFileURL_001_31()845     void SystemPath_FileURL::getSystemPathFromFileURL_001_31()
846     {
847         OString sURL("tmpname");
848         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "tmpname");
849         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "tmpname");
850     }
851 
getSystemPathFromFileURL_001_4()852     void SystemPath_FileURL::getSystemPathFromFileURL_001_4()
853     {
854         OString sURL("file://");
855         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "");
856         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
857     }
858 
getSystemPathFromFileURL_001_41()859     void SystemPath_FileURL::getSystemPathFromFileURL_001_41()
860     {
861         OString sURL("file://localhost/tmp");
862         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "");
863         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
864     }
865 
getSystemPathFromFileURL_001_5()866     void SystemPath_FileURL::getSystemPathFromFileURL_001_5()
867     {
868         OString sURL("file:///tmp");
869         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp");
870         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
871     }
872 
getSystemPathFromFileURL_001_51()873     void SystemPath_FileURL::getSystemPathFromFileURL_001_51()
874     {
875         OString sURL("file://c:/tmp");
876         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
877         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
878     }
879 
getSystemPathFromFileURL_001_52()880     void SystemPath_FileURL::getSystemPathFromFileURL_001_52()
881     {
882         OString sURL("file:///c:/tmp");
883         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp");
884         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp");
885     }
886 
getSystemPathFromFileURL_001_53()887     void SystemPath_FileURL::getSystemPathFromFileURL_001_53()
888     {
889         OString sURL("file:///c|/tmp");
890         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c|/tmp");
891         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp");
892     }
893 
getSystemPathFromFileURL_001_6()894     void SystemPath_FileURL::getSystemPathFromFileURL_001_6()
895     {
896         OString sURL("file:///tmp/first");
897         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/first");
898         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
899     }
900 
getSystemPathFromFileURL_001_61()901     void SystemPath_FileURL::getSystemPathFromFileURL_001_61()
902     {
903         OString sURL("file:///c:/tmp/first");
904         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/first");
905         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\first");
906     }
907 
getSystemPathFromFileURL_001_7()908     void SystemPath_FileURL::getSystemPathFromFileURL_001_7()
909     {
910         OString sURL("file:///tmp/../second");
911         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/../second");
912         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
913     }
914 
getSystemPathFromFileURL_001_71()915     void SystemPath_FileURL::getSystemPathFromFileURL_001_71()
916     {
917         OString sURL("file:///c:/tmp/../second");
918         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/../second");
919         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\..\\second");
920     }
921 
getSystemPathFromFileURL_001_8()922     void SystemPath_FileURL::getSystemPathFromFileURL_001_8()
923     {
924         OString sURL("../tmp");
925         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "../tmp");
926         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "..\\tmp");
927     }
928 
getSystemPathFromFileURL_001_81()929     void SystemPath_FileURL::getSystemPathFromFileURL_001_81()
930     {
931 #if 0
932         OString sURL("file://~/tmp");
933         char* home_path;
934         home_path = getenv("HOME");
935         OString expResult(home_path ? home_path : "");
936         expResult += "/tmp";
937         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, expResult);
938 #endif
939     }
940 
getSystemPathFromFileURL_001_9()941     void SystemPath_FileURL::getSystemPathFromFileURL_001_9()
942     {
943         OString sURL("file:///tmp/first%20second");
944         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/first second");
945         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
946     }
947 
getSystemPathFromFileURL_001_91()948     void SystemPath_FileURL::getSystemPathFromFileURL_001_91()
949     {
950         OString sURL("file:///c:/tmp/first%20second");
951         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/first second");
952         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\first second");
953     }
954 
getSystemPathFromFileURL_001_92()955     void SystemPath_FileURL::getSystemPathFromFileURL_001_92()
956     {
957         OString sURL("ca@#;+.,$///78no%01ni..name");
958         checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
959         checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
960     }
961 
962     // normal legal case
getSystemPathFromFileURL_004()963     void SystemPath_FileURL::getSystemPathFromFileURL_004()
964     {
965         OUString aUStr;
966         OUString aUNormalURL(aTmpName6);
967         OUString aUResultURL (aSysPath4);
968         osl::FileBase::RC nError = osl::FileBase::getSystemPathFromFileURL(aUNormalURL, aUStr);
969 
970         bool bOk = compareFileName(aUStr, aUResultURL);
971 
972         OString sError =
973             "test for getSystemPathFromFileURL(' " +
974             OUStringToOString(aUNormalURL, RTL_TEXTENCODING_ASCII_US) +
975             " ') function:use an absolute file URL, " +
976             outputError(OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US),
977                         OUStringToOString(aUResultURL, RTL_TEXTENCODING_ASCII_US));
978 
979         CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError);
980         CPPUNIT_ASSERT_MESSAGE(sError.getStr(), bOk);
981 
982     }
983 
984     // CJK characters case
getSystemPathFromFileURL_005()985     void SystemPath_FileURL::getSystemPathFromFileURL_005()
986     {
987         OUString aUStr;
988         createTestDirectory(aTmpName10);
989         OUString aUNormalURL(aTmpName10);
990         OUString aUResultURL (aSysPath5);
991 
992         osl::FileBase::RC nError = osl::FileBase::getSystemPathFromFileURL(aUNormalURL, aUStr);
993 
994         bool bOk = compareFileName(aUStr, aUResultURL);
995 
996         OString sError =
997             "test for getSystemPathFromFileURL(' " +
998             OUStringToOString(aUNormalURL, RTL_TEXTENCODING_ASCII_US) +
999             " ') function:use a CJK coded absolute URL, " +
1000             outputError(OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US),
1001                         OUStringToOString(aUResultURL, RTL_TEXTENCODING_ASCII_US));
1002         deleteTestDirectory(aTmpName10);
1003 
1004         CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError);
1005         CPPUNIT_ASSERT_MESSAGE(sError.getStr(), bOk);
1006     }
1007 
getFileURLFromSystemPath_001()1008     void SystemPath_FileURL::getFileURLFromSystemPath_001()
1009     {
1010         OString sSysPath("~/tmp");
1011         char* home_path;
1012         home_path = getenv("HOME");
1013         OString expResult(home_path ? home_path : "");
1014         expResult = "file://"+ expResult + "/tmp";
1015         checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, expResult);
1016         checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "~/tmp");
1017     }
1018 
getFileURLFromSystemPath_002()1019     void SystemPath_FileURL::getFileURLFromSystemPath_002()
1020     {
1021         OString sSysPath("c:/tmp");
1022         checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "c:/tmp");
1023         checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///c:/tmp");
1024     }
1025 
getFileURLFromSystemPath_003()1026     void SystemPath_FileURL::getFileURLFromSystemPath_003()
1027     {
1028         OString sSysPath("file:///temp");
1029         checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1030         checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1031     }
1032 
getFileURLFromSystemPath_004()1033     void SystemPath_FileURL::getFileURLFromSystemPath_004()
1034     {
1035         OString sSysPath("//tmp//first start");
1036         checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///tmp/first%20start");
1037         checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1038     }
1039 
getFileURLFromSystemPath_004_1()1040     void SystemPath_FileURL::getFileURLFromSystemPath_004_1()
1041     {
1042         OString sSysPath("/tmp///first start");
1043         checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///tmp/first%20start");
1044         checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1045     }
1046 
getFileURLFromSystemPath_005()1047     void SystemPath_FileURL::getFileURLFromSystemPath_005()
1048     {
1049         OString sSysPath("");
1050         checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1051         checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1052     }
1053 
1054     // testing the method
1055     // static inline RC searchFileURL( const OUString& ustrFileName,
1056     //                                  const OUString& ustrSearchPath,
1057     //                                  OUString& ustrFileURL)
1058 
1059     class searchFileURL : public CppUnit::TestFixture
1060     {
1061     private:
1062         OUString aUStr;
1063 
1064     public:
searchFileURL_001()1065         void searchFileURL_001()
1066         {
1067             /* search file is passed by system filename */
1068             auto nError1 = osl::FileBase::searchFileURL(aTmpName1, aUserDirectorySys, aUStr);
1069             /* search file is passed by full qualified file URL */
1070             auto nError2 = osl::FileBase::searchFileURL(aCanURL1, aUserDirectorySys, aUStr);
1071             /* search file is passed by relative file path */
1072             auto nError3 = osl::FileBase::searchFileURL(aRelURL4, aUserDirectorySys, aUStr);
1073 
1074             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
1075                                      osl::FileBase::E_NOENT, nError1);
1076             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
1077                                      osl::FileBase::E_NOENT, nError2);
1078             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
1079                                     osl::FileBase::E_NOENT, nError3);
1080         }
1081 
searchFileURL_002()1082          void searchFileURL_002()
1083         {
1084 #ifndef UNX
1085             /* search file is passed by system filename */
1086             OUString strRootSys = INetURLObject(aTempDirectoryURL).GetLastName();
1087             auto nError1 = osl::FileBase::searchFileURL(aTempDirectorySys, strRootSys, aUStr);
1088             bool bOk1 = compareFileName(aUStr, aTempDirectoryURL);
1089             /* search file is passed by full qualified file URL */
1090             auto nError2 = osl::FileBase::searchFileURL(aTempDirectoryURL, strRootSys, aUStr);
1091             bool bOk2 = compareFileName(aUStr, aTempDirectoryURL);
1092 #ifndef _WIN32
1093             /* search file is passed by relative file path */
1094             auto nError3 = osl::FileBase::searchFileURL(aRelURL5, strRootSys, aUStr);
1095             bool bOk3 = compareFileName(aUStr, aTempDirectoryURL);
1096 #endif
1097             /* search file is passed by an exist file */
1098             createTestFile(aCanURL1);
1099             auto nError4 = osl::FileBase::searchFileURL(aCanURL4, aUserDirectorySys, aUStr);
1100             bool bOk4 = compareFileName(aUStr, aCanURL1);
1101             deleteTestFile(aCanURL1);
1102 
1103             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
1104                                     osl::FileBase::E_None, nError1);
1105             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
1106                                     osl::FileBase::E_None, nError2);
1107 #ifndef _WIN32
1108             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
1109                                     osl::FileBase::E_None, nError3);
1110 #endif
1111             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1112                                     osl::FileBase::E_None, nError4);
1113             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename, system directory, searched file already exist.",
1114                                     bOk1);
1115             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: URL filename, system directory, searched file already exist.",
1116                                     bOk2);
1117 #ifndef _WIN32
1118             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: relative path, system directory, searched file already exist.",
1119                                     bOk3);
1120 #endif
1121             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched file already exist.",
1122                                     bOk4);
1123 #endif
1124         }
1125 
searchFileURL_003()1126         void searchFileURL_003()
1127         {
1128             OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path");
1129             auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr);
1130             bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1131             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1132                                     osl::FileBase::E_None, nError1);
1133             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1134                                     bOk);
1135         }
1136 
searchFileURL_004()1137         void searchFileURL_004()
1138         {
1139             OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path/../name");
1140             auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr);
1141             bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1142             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1143                                     osl::FileBase::E_None, nError1);
1144             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1145                                     bOk);
1146         }
1147 
searchFileURL_005()1148         void searchFileURL_005()
1149         {
1150             auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aNullURL, aUStr);
1151             bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1152             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is NULL",
1153                                     osl::FileBase::E_None, nError1);
1154             CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is NULL",
1155                                     bOk);
1156         }
1157 
1158         CPPUNIT_TEST_SUITE(searchFileURL);
1159             CPPUNIT_TEST(searchFileURL_001);
1160             CPPUNIT_TEST(searchFileURL_002);
1161             CPPUNIT_TEST(searchFileURL_003);
1162             CPPUNIT_TEST(searchFileURL_004);
1163             CPPUNIT_TEST(searchFileURL_005);
1164         CPPUNIT_TEST_SUITE_END();
1165     };
1166 
1167     // testing the method
1168     // static inline RC getTempDirURL(OUString& ustrTempDirURL)
1169 
1170     class getTempDirURL : public CppUnit::TestFixture
1171     {
1172     private:
1173         OUString aUStr;
1174 
1175     public:
setUp()1176         void setUp() override
1177         {
1178             osl::FileBase::RC nError = osl::FileBase::getTempDirURL(aUStr);
1179             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getTempDirURL function: execution",
1180                                      osl::FileBase::E_None, nError);
1181         }
1182 
getTempDirURL_002()1183         void getTempDirURL_002()
1184         {
1185             CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1186                                     checkDirectory(aUStr, oslCheckMode::OpenAccess));
1187             CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1188                                     checkDirectory(aUStr, oslCheckMode::ReadAccess));
1189             CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1190                                     checkDirectory(aUStr, oslCheckMode::WriteAccess));
1191         }
1192 
1193         CPPUNIT_TEST_SUITE(getTempDirURL);
1194             CPPUNIT_TEST(getTempDirURL_002);
1195         CPPUNIT_TEST_SUITE_END();
1196     };
1197 
1198     //  testing the method
1199     //  static inline RC createTempFile(OUString* pustrDirectoryURL,
1200     //                                   oslFileHandle* pHandle,
1201     //                                   OUString* pustrTempFileURL)
1202 
1203     class createTempFile : public CppUnit::TestFixture
1204     {
1205     private:
1206         std::unique_ptr<oslFileHandle> pHandle;
1207         std::unique_ptr<OUString> pUStr_DirURL;
1208         std::unique_ptr<OUString> pUStr_FileURL;
1209 
1210     public:
setUp()1211         void setUp() override
1212         {
1213             pHandle.reset(new oslFileHandle());
1214             pUStr_DirURL.reset(new OUString(aUserDirectoryURL));
1215             pUStr_FileURL.reset(new OUString());
1216         }
1217 
tearDown()1218         void tearDown() override
1219         {
1220             pUStr_DirURL.reset();
1221             pUStr_FileURL.reset();
1222             pHandle.reset();
1223         }
1224 
createTempFile_001()1225         void createTempFile_001()
1226         {
1227             auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
1228             File testFile(*pUStr_FileURL);
1229             auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1230 
1231             if (nError2 == osl::FileBase::E_EXIST)
1232             {
1233                 osl_closeFile(*pHandle);
1234                 deleteTestFile(*pUStr_FileURL);
1235             }
1236 
1237             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1238                                      osl::FileBase::E_None, nError1);
1239             CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: create temp file and test the existence",
1240                                      (pHandle != nullptr));
1241             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1242                                      osl::FileBase::E_EXIST, nError2);
1243         }
1244 
createTempFile_002()1245         void createTempFile_002()
1246         {
1247             bool bOK = false;
1248             auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
1249             File testFile(*pUStr_FileURL);
1250             auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1251 
1252             CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1253                 osl::FileBase::E_None, nError1);
1254             CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1255                 (pHandle != nullptr));
1256             CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1257                 osl::FileBase::E_EXIST, nError2);
1258 
1259             // check file if have the write permission
1260             if (nError2 == osl::FileBase::E_EXIST)
1261             {
1262                 bOK = ifFileCanWrite(*pUStr_FileURL);
1263                 osl_closeFile(*pHandle);
1264                 deleteTestFile(*pUStr_FileURL);
1265             }
1266 
1267             CPPUNIT_ASSERT_MESSAGE("test for open and write access rights, in (W32), it did not have write access right, but it should be writable.",
1268                                      bOK);
1269         }
1270 
createTempFile_003()1271         void createTempFile_003()
1272         {
1273             auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), nullptr);
1274             // the temp file will be removed when return from createTempFile
1275             bool bOK = (pHandle != nullptr && nError1 == osl::FileBase::E_None);
1276             if (bOK)
1277                 osl_closeFile(*pHandle);
1278 
1279             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1280                                 osl::FileBase::E_None, nError1);
1281             CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1282                                 bOK);
1283         }
1284 
createTempFile_004()1285         void createTempFile_004()
1286         {
1287             auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), nullptr, pUStr_FileURL.get());
1288             bool bOK = (pUStr_FileURL != nullptr);
1289             CPPUNIT_ASSERT(bOK);
1290             File testFile(*pUStr_FileURL);
1291             auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1292             deleteTestFile(*pUStr_FileURL);
1293             CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1294                 osl::FileBase::E_None, nError1);
1295             CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1296                 osl::FileBase::E_EXIST, nError2);
1297             CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1298                 bOK);
1299 
1300         }
1301 
1302         CPPUNIT_TEST_SUITE(createTempFile);
1303             CPPUNIT_TEST(createTempFile_001);
1304             CPPUNIT_TEST(createTempFile_002);
1305             CPPUNIT_TEST(createTempFile_003);
1306             CPPUNIT_TEST(createTempFile_004);
1307         CPPUNIT_TEST_SUITE_END();
1308     };
1309 
1310     // FIXME: remove the _disabled to enable:
1311     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getAbsoluteFileURL, "osl_osl::FileBase");
1312     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::SystemPath_FileURL, "osl_osl::FileBase");
1313     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::searchFileURL, "osl_osl::FileBase");
1314     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getTempDirURL, "osl_osl::FileBase");
1315     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::createTempFile, "osl_osl::FileBase");
1316 
1317     CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_osl::FileBase");
1318 }
1319 
1320 namespace osl_FileStatus
1321 {
1322     //  testing the method
1323     //  FileStatus(sal_uInt32 nMask): _nMask(nMask)
1324     class ctors : public CppUnit::TestFixture
1325     {
1326     private:
1327         OUString aUStr;
1328         DirectoryItem rItem;
1329 
1330     public:
setUp()1331         void setUp() override
1332         {
1333             // create a tempfile in $TEMP/tmpdir/tmpname.
1334             createTestDirectory(aTmpName3);
1335             createTestFile(aTmpName4);
1336 
1337             Directory aDir(aTmpName3);
1338             auto nError1 = aDir.open();
1339             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1340             nError1 = aDir.getNextItem(rItem);
1341             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1342             aDir.close();
1343         }
1344 
tearDown()1345         void tearDown() override
1346         {
1347             // remove the tempfile in $TEMP/tmpdir/tmpname.
1348             deleteTestFile(aTmpName4);
1349             deleteTestDirectory(aTmpName3);
1350         }
1351 
ctors_001()1352         void ctors_001()
1353         {
1354             FileStatus rFileStatus(osl_FileStatus_Mask_All);
1355             auto nError1 = rItem.getFileStatus(rFileStatus);
1356             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1357             aUStr = rFileStatus.getFileName();
1358 
1359             CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask all and see the file name",
1360                                    compareFileName(aUStr, aTmpName2));
1361         }
1362 
ctors_002()1363         void ctors_002()
1364         {
1365             FileStatus rFileStatus(0);
1366             auto nError1 = rItem.getFileStatus(rFileStatus);
1367             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1368             aUStr = rFileStatus.getFileName();
1369 
1370             CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask is empty",
1371                                    compareFileName(aUStr, aNullURL));
1372         }
1373 
1374         CPPUNIT_TEST_SUITE(ctors);
1375             CPPUNIT_TEST(ctors_001);
1376             CPPUNIT_TEST(ctors_002);
1377         CPPUNIT_TEST_SUITE_END();
1378     };
1379 
1380     //  testing the method
1381     //  inline sal_Bool isValid(sal_uInt32 nMask) const
1382 
1383     class isValid : public CppUnit::TestFixture
1384     {
1385     private:
1386         std::unique_ptr<Directory> pDir;
1387         DirectoryItem rItem_file, rItem_link;
1388 
1389     public:
setUp()1390         void setUp() override
1391         {
1392             // create a tempfile in $TEMP/tmpdir/tmpname.
1393             createTestDirectory(aTmpName3);
1394             createTestFile(aTmpName4);
1395 
1396             pDir.reset(new Directory(aTmpName3));
1397             osl::FileBase::RC nError1 = pDir->open();
1398             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1399             nError1 = pDir->getNextItem(rItem_file, 1);
1400             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1401         }
1402 
tearDown()1403         void tearDown() override
1404         {
1405             osl::FileBase::RC nError1 = pDir->close();
1406             pDir.reset();
1407             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
1408 
1409             // remove the tempfile in $TEMP/tmpdir/tmpname.
1410             deleteTestFile(aTmpName4);
1411             deleteTestDirectory(aTmpName3);
1412         }
1413 
isValid_001()1414         void isValid_001()
1415         {
1416             sal_uInt32 mask = 0;
1417             FileStatus rFileStatus(mask);
1418             osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus);
1419             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1420             bool bOk = rFileStatus.isValid(mask);
1421 
1422             CPPUNIT_ASSERT_MESSAGE("test for isValid function: no fields specified", bOk);
1423         }
1424 
check_FileStatus(FileStatus const & _aStatus)1425         void check_FileStatus(FileStatus const& _aStatus)
1426         {
1427             OString sStat;
1428             if (_aStatus.isValid(osl_FileStatus_Mask_Type))
1429                 sStat += "type ";
1430             if (_aStatus.isValid(osl_FileStatus_Mask_Attributes))
1431                 sStat += "attributes ";
1432             if (_aStatus.isValid(osl_FileStatus_Mask_CreationTime))
1433                 sStat += "ctime ";
1434             if (_aStatus.isValid(osl_FileStatus_Mask_AccessTime))
1435                 sStat += "atime ";
1436             if (_aStatus.isValid(osl_FileStatus_Mask_ModifyTime))
1437                 sStat += "mtime ";
1438             if (_aStatus.isValid(osl_FileStatus_Mask_FileSize))
1439                 sStat += "filesize ";
1440             if (_aStatus.isValid(osl_FileStatus_Mask_FileName))
1441                 sStat += "filename ";
1442             if (_aStatus.isValid(osl_FileStatus_Mask_FileURL))
1443                 sStat += "fileurl ";
1444             printf("mask: %s\n", sStat.getStr());
1445         }
1446 
isValid_002()1447         void isValid_002()
1448         {
1449             createTestFile(aTmpName6);
1450             sal_uInt32 mask_file = osl_FileStatus_Mask_Type |
1451                                    osl_FileStatus_Mask_Attributes |
1452                                    osl_FileStatus_Mask_CreationTime |
1453                                    osl_FileStatus_Mask_AccessTime |
1454                                    osl_FileStatus_Mask_ModifyTime |
1455                                    osl_FileStatus_Mask_FileSize |
1456                                    osl_FileStatus_Mask_FileName |
1457                                    osl_FileStatus_Mask_FileURL;
1458 
1459             FileStatus rFileStatus(mask_file);
1460             DirectoryItem::get(aTmpName6, rItem_file);
1461             osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus);
1462 
1463             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
1464 
1465             check_FileStatus(rFileStatus);
1466             deleteTestFile(aTmpName6);
1467 
1468         }
1469 
1470         /** Check if is a valid linked file.
1471 
1472             Link is not defined in Windows, and on Linux, we can not get the directory item of the linked file.
1473             We have to defer to filesystems, normal filesystems support links (EXT2, ...), castrated filesystems
1474             don't have links (FAT, FAT32) and Windows NT NTFS support links, but the Windows API doesn't :-(
1475         */
isValid_003()1476         void isValid_003()
1477         {
1478 #if 0
1479 #if defined (UNX)
1480             sal_Int32 fd;
1481 
1482             OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
1483             aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/tmpdir/link.file";
1484             aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/tmpdir/tmpname";
1485 
1486             OString strLinkFileName;
1487             OString strSrcFileName;
1488             strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
1489             strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
1490 
1491             // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
1492             fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
1493             CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
1494 
1495             // testDirectory is "/tmp/PID/tmpdir/"
1496             Directory testDirectory(aTmpName3);
1497             testDirectory.open();
1498             OUString aFileName ("link.file");
1499             bool bOk = false;
1500             while (true)
1501             {
1502                 osl::FileBase::RC nError1 = testDirectory.getNextItem(rItem_link, 4);
1503 
1504                 if (nError1 == osl::FileBase::E_None)
1505                 {
1506                     sal_uInt32 mask_link = osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_LinkTargetURL;
1507                     FileStatus rFileStatus(mask_link);
1508                     rItem_link.getFileStatus(rFileStatus);
1509 
1510                     if (compareFileName(rFileStatus.getFileName(), aFileName))
1511                     {
1512                         if (rFileStatus.isValid(osl_FileStatus_Mask_LinkTargetURL))
1513                         {
1514                             bOk = true;
1515                             break;
1516                         }
1517                     }
1518                 }
1519                 else
1520                 {
1521                     break;
1522                 }
1523             };
1524 
1525             fd = remove(strLinkFileName.getStr());
1526             CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
1527 
1528             CPPUNIT_ASSERT_MESSAGE("test for isValid function: link file, check for LinkTargetURL", bOk);
1529 #endif
1530 #endif
1531         }
1532 
isValid_004()1533         void isValid_004()
1534         {
1535             sal_uInt32 mask_file_all = osl_FileStatus_Mask_All;
1536             FileStatus   rFileStatus_all(mask_file_all);
1537             osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus_all);
1538             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1539 
1540             check_FileStatus(rFileStatus_all);
1541 
1542             sal_uInt32 mask_file_val = osl_FileStatus_Mask_Validate;
1543             FileStatus   rFileStatus_val(mask_file_val);
1544             nError1 = rItem_file.getFileStatus(rFileStatus_val);
1545             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1546 
1547             check_FileStatus(rFileStatus_val);
1548         }
1549 
1550         CPPUNIT_TEST_SUITE(isValid);
1551             CPPUNIT_TEST(isValid_001);
1552             CPPUNIT_TEST(isValid_002);
1553             CPPUNIT_TEST(isValid_003);
1554             CPPUNIT_TEST(isValid_004);
1555         CPPUNIT_TEST_SUITE_END();
1556     };
1557 
1558     // testing the method
1559     // inline Type getFileType() const
1560 
1561     class getFileType : public CppUnit::TestFixture
1562     {
1563     private:
1564         DirectoryItem m_aItem_1, m_aItem_2, m_aVolumeItem, m_aFifoItem;
1565         DirectoryItem m_aLinkItem, m_aSocketItem, m_aSpecialItem;
1566 
1567     public:
setUp()1568         void setUp() override
1569         {
1570             // create a tempfile: $TEMP/tmpdir/tmpname.
1571             //        a tempdirectory: $TEMP/tmpdir/tmpdir.
1572             //        use $ROOT/staroffice as volume ---> use dev/fd as volume.
1573             // and get their directory item.
1574             createTestDirectory(aTmpName3);
1575             createTestFile(aTmpName3, aTmpName2);
1576             createTestDirectory(aTmpName3, aTmpName1);
1577 
1578             std::unique_ptr<Directory> xDir(new Directory(aTmpName3));
1579             auto nError1 = xDir->open();
1580             CPPUNIT_ASSERT_EQUAL_MESSAGE("open aTmpName3 failed!", osl::FileBase::E_None, nError1);
1581             // getNextItem can not assure which item retrieved
1582             nError1 = xDir->getNextItem(m_aItem_1, 1);
1583             CPPUNIT_ASSERT_EQUAL_MESSAGE("get first item failed!", osl::FileBase::E_None, nError1);
1584 
1585             nError1 = xDir->getNextItem(m_aItem_2);
1586             CPPUNIT_ASSERT_EQUAL_MESSAGE("get second item failed!", osl::FileBase::E_None, nError1);
1587             xDir->close();
1588             // FIXME mindy: failed on my RH9, so removed temporarily
1589             // nError1 = DirectoryItem::get(aVolURL2, m_aVolumeItem);
1590             // CPPUNIT_ASSERT_MESSAGE("get volume item failed!", osl::FileBase::E_None == nError1);
1591         }
1592 
tearDown()1593         void tearDown() override
1594         {
1595             // remove all in $TEMP/tmpdir.
1596             deleteTestDirectory(aTmpName3, aTmpName1);
1597             deleteTestFile(aTmpName3, aTmpName2);
1598             deleteTestDirectory(aTmpName3);
1599         }
1600 
getFileType_001()1601         void getFileType_001()
1602         {
1603             FileStatus   rFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName);
1604             auto nError1 = m_aItem_1.getFileStatus(rFileStatus);
1605             CPPUNIT_ASSERT_EQUAL_MESSAGE("getFileStatus failed", osl::FileBase::E_None, nError1);
1606 
1607             check_FileType(rFileStatus);
1608         }
1609 
check_FileType(osl::FileStatus const & _rFileStatus)1610         void check_FileType(osl::FileStatus const& _rFileStatus)
1611         {
1612             if (_rFileStatus.isValid(osl_FileStatus_Mask_FileName))
1613             {
1614                 OUString suFilename = _rFileStatus.getFileName();
1615 
1616                 if (_rFileStatus.isValid(osl_FileStatus_Mask_Type))
1617                 {
1618                     osl::FileStatus::Type eType = _rFileStatus.getFileType();
1619                     bool bOK = false;
1620 
1621                     if (compareFileName(suFilename, aTmpName2))
1622                         bOK = (eType == osl::FileStatus::Regular);
1623 
1624                     if (compareFileName(suFilename, aTmpName1))
1625                         bOK = (eType == FileStatus::Directory);
1626 
1627                     CPPUNIT_ASSERT_MESSAGE("test for getFileType function: ", bOK);
1628                 }
1629             }
1630             // LLA: it's not a bug, if a FileStatus not exist, so no else
1631         }
1632 
getFileType_002()1633         void getFileType_002()
1634         {
1635             FileStatus rFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName);
1636             auto nError1 = m_aItem_2.getFileStatus(rFileStatus);
1637 
1638             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1639             check_FileType(rFileStatus);
1640         }
1641 
getFileType_003()1642         void getFileType_003()
1643         {
1644         }
1645 
getFileType_007()1646         void getFileType_007()
1647         {
1648 #if defined(__sun) // Special file is different in Windows
1649             auto nError1 = DirectoryItem::get(aTypeURL2, m_aSpecialItem);
1650             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1651 
1652             // check for File type
1653             FileStatus   rFileStatus(osl_FileStatus_Mask_Type);
1654             nError1 = m_aSpecialItem.getFileStatus(rFileStatus);
1655             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1656 
1657             if (rFileStatus.isValid(osl_FileStatus_Mask_Type))
1658             {
1659                 osl::FileStatus::Type eType = rFileStatus.getFileType();
1660 
1661                 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: Special, Solaris version ",
1662                                         (eType == FileStatus::Special));
1663             }
1664 #endif
1665         }
1666 
1667         CPPUNIT_TEST_SUITE(getFileType);
1668             CPPUNIT_TEST(getFileType_001);
1669             CPPUNIT_TEST(getFileType_002);
1670             CPPUNIT_TEST(getFileType_003);
1671             CPPUNIT_TEST(getFileType_007);
1672         CPPUNIT_TEST_SUITE_END();
1673     };
1674 
1675     //  testing the method
1676     //  inline sal_uInt64 getAttributes() const
1677 
1678     class getAttributes : public CppUnit::TestFixture
1679     {
1680     private:
1681         OUString aTypeURL, aTypeURL_Hid;
1682         DirectoryItem rItem, rItem_hidden;
1683 
1684     public:
setUp()1685         void setUp() override
1686         {
1687             aTypeURL = aUserDirectoryURL.copy(0);
1688             concatURL(aTypeURL, aTmpName2);
1689             createTestFile(aTypeURL);
1690             osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1691             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1692 
1693             aTypeURL_Hid = aUserDirectoryURL.copy(0);
1694             concatURL(aTypeURL_Hid, aHidURL1);
1695             createTestFile(aTypeURL_Hid);
1696 #ifdef _WIN32
1697             hideFile(aTypeURL_Hid);
1698 #endif
1699             nError = DirectoryItem::get(aTypeURL_Hid, rItem_hidden);
1700             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1701         }
1702 
tearDown()1703         void tearDown() override
1704         {
1705             deleteTestFile(aTypeURL);
1706             deleteTestFile(aTypeURL_Hid);
1707         }
1708 
1709 #if (defined UNX)
1710 // windows only has 3 file attributes: normal, readonly and hidden
getAttributes_001()1711         void getAttributes_001()
1712         {
1713             changeFileMode(aTypeURL, S_IRUSR | S_IRGRP | S_IROTH);
1714 
1715             FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1716             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1717             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1718 
1719             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(UNX version) ",
1720                                     static_cast<sal_uInt64>(osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead),
1721                                     rFileStatus.getAttributes());
1722         }
1723 #else // Windows version
getAttributes_001()1724         void getAttributes_001()
1725         {
1726             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(Windows version)",
1727                                      1, 1);
1728         }
1729 #endif
1730 
getAttributes_002()1731         void getAttributes_002()
1732         {
1733 #if (defined UNX)
1734             changeFileMode(aTypeURL, S_IXUSR | S_IXGRP | S_IXOTH);
1735 
1736             FileStatus   rFileStatus(osl_FileStatus_Mask_Attributes);
1737             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1738             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1739 
1740             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)",
1741                                     static_cast<sal_uInt64>(osl_File_Attribute_ReadOnly | osl_File_Attribute_Executable | osl_File_Attribute_GrpExe | osl_File_Attribute_OwnExe | osl_File_Attribute_OthExe),
1742                                     rFileStatus.getAttributes());
1743 #endif
1744         }
1745 
1746 #if (defined UNX)
getAttributes_003()1747         void getAttributes_003()
1748         {
1749             changeFileMode(aTypeURL, S_IWUSR | S_IWGRP | S_IWOTH);
1750 
1751             FileStatus   rFileStatus(osl_FileStatus_Mask_Attributes);
1752             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1753             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1754 
1755             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Solaris version)",
1756                                     static_cast<sal_uInt64>(osl_File_Attribute_GrpWrite | osl_File_Attribute_OwnWrite | osl_File_Attribute_OthWrite),
1757                                     rFileStatus.getAttributes());
1758         }
1759 #else // Windows version
getAttributes_003()1760         void getAttributes_003()
1761         {
1762             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Windows version)",
1763                                      1, 1);
1764         }
1765 #endif
1766 
getAttributes_004()1767         void getAttributes_004()
1768         {
1769             sal_Int32 test_Attributes = osl_File_Attribute_Hidden;
1770             FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1771             osl::FileBase::RC nError = rItem_hidden.getFileStatus(rFileStatus);
1772             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1773             test_Attributes &= rFileStatus.getAttributes();
1774 
1775             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Hidden files",
1776                                     static_cast<sal_Int32>(osl_File_Attribute_Hidden), test_Attributes);
1777         }
1778 
1779         CPPUNIT_TEST_SUITE(getAttributes);
1780             CPPUNIT_TEST(getAttributes_001);
1781             CPPUNIT_TEST(getAttributes_002);
1782             CPPUNIT_TEST(getAttributes_003);
1783             CPPUNIT_TEST(getAttributes_004);
1784         CPPUNIT_TEST_SUITE_END();
1785     };
1786 
1787     //  testing the method
1788     //  inline TimeValue getAccessTime() const
1789 
1790     class getAccessTime : public CppUnit::TestFixture
1791     {
1792     private:
1793         OUString aTypeURL;
1794         DirectoryItem rItem;
1795 
1796     public:
setUp()1797         void setUp() override
1798         {
1799             aTypeURL = aUserDirectoryURL.copy(0);
1800             concatURL(aTypeURL, aTmpName2);
1801             createTestFile(aTypeURL);
1802             osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1803             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1804 
1805         }
1806 
tearDown()1807         void tearDown() override
1808         {
1809             deleteTestFile(aTypeURL);
1810         }
1811 
getAccessTime_001()1812         void getAccessTime_001()
1813         {
1814             TimeValue *pTV_current = nullptr;
1815             CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1816             TimeValue *pTV_access = nullptr;
1817             CPPUNIT_ASSERT((pTV_access = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1818 
1819             FileStatus   rFileStatus(osl_FileStatus_Mask_AccessTime);
1820             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1821             bool bOk = osl_getSystemTime(pTV_current);
1822             CPPUNIT_ASSERT(bOk);
1823             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1824 
1825             *pTV_access = rFileStatus.getAccessTime();
1826 
1827             bool bOK = t_compareTime(pTV_access, pTV_current, delta);
1828             free(pTV_current);
1829             free(pTV_access);
1830 
1831             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. ",
1832                                     bOK);
1833         }
1834 
1835         CPPUNIT_TEST_SUITE(getAccessTime);
1836             CPPUNIT_TEST(getAccessTime_001);
1837         CPPUNIT_TEST_SUITE_END();
1838     };
1839 
1840     //  testing the method
1841     //  inline TimeValue getModifyTime() const
1842 
1843     class getModifyTime : public CppUnit::TestFixture
1844     {
1845     private:
1846         OUString aTypeURL;
1847         DirectoryItem rItem;
1848 
1849     public:
getModifyTime_001()1850         void getModifyTime_001()
1851         {
1852             TimeValue *pTV_current = nullptr;
1853             CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1854 
1855             // create file
1856             aTypeURL = aUserDirectoryURL.copy(0);
1857             concatURL(aTypeURL, aTmpName2);
1858             createTestFile(aTypeURL);
1859 
1860             // get current time
1861             bool bOk = osl_getSystemTime(pTV_current);
1862             CPPUNIT_ASSERT(bOk);
1863 
1864             // get instance item and filestatus
1865             osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1866             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1867             FileStatus   rFileStatus(osl_FileStatus_Mask_ModifyTime);
1868             nError = rItem.getFileStatus(rFileStatus);
1869             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1870 
1871             // get modify time
1872             TimeValue* pTV_modify = nullptr;
1873             CPPUNIT_ASSERT((pTV_modify = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1874             *pTV_modify = rFileStatus.getModifyTime();
1875 
1876             bool bOK = t_compareTime(pTV_modify, pTV_current, delta);
1877             // delete file
1878             deleteTestFile(aTypeURL);
1879             free(pTV_current);
1880             free(pTV_modify);
1881 
1882             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.  ",
1883                                     bOK);
1884         }
1885 
1886         CPPUNIT_TEST_SUITE(getModifyTime);
1887             CPPUNIT_TEST(getModifyTime_001);
1888         CPPUNIT_TEST_SUITE_END();
1889     };
1890 
1891     //  testing the method
1892     //  inline sal_uInt64 getFileSize() const
1893 
1894     class getFileSize : public CppUnit::TestFixture
1895     {
1896     private:
1897         OUString aTypeURL;
1898         DirectoryItem rItem;
1899 
1900     public:
setUp()1901         void setUp() override
1902         {
1903             aTypeURL = aUserDirectoryURL.copy(0);
1904             concatURL(aTypeURL, aTmpName2);
1905             createTestFile(aTypeURL);
1906             osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1907             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1908         }
1909 
tearDown()1910         void tearDown() override
1911         {
1912             deleteTestFile(aTypeURL);
1913         }
1914 
getFileSize_001()1915         void getFileSize_001()
1916         {
1917             FileStatus   rFileStatus(osl_FileStatus_Mask_FileSize);
1918             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1919             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1920 
1921             sal_uInt64 uFileSize = rFileStatus.getFileSize();
1922 
1923             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: empty file ",
1924                                     static_cast<sal_uInt64>(0), uFileSize);
1925         }
1926 
getFileSize_002()1927         void getFileSize_002()
1928         {
1929             File testfile(aTypeURL);
1930             osl::FileBase::RC nError = testfile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Read);
1931             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1932             nError = testfile.setSize(TEST_FILE_SIZE);
1933             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1934 
1935             nError = DirectoryItem::get(aTypeURL, rItem);
1936             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1937             FileStatus   rFileStatus(osl_FileStatus_Mask_FileSize);
1938             nError = rItem.getFileStatus(rFileStatus);
1939             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1940             sal_uInt64 uFileSize = rFileStatus.getFileSize();
1941 
1942             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: file with size of TEST_FILE_SIZE, did not pass in (W32). ",
1943                                     static_cast<sal_uInt64>(TEST_FILE_SIZE), uFileSize);
1944         }
1945 
1946         CPPUNIT_TEST_SUITE(getFileSize);
1947             CPPUNIT_TEST(getFileSize_001);
1948             CPPUNIT_TEST(getFileSize_002);
1949         CPPUNIT_TEST_SUITE_END();
1950     };
1951 
1952     // testing the method
1953     // inline OUString getFileName() const
1954 
1955     class getFileName : public CppUnit::TestFixture
1956     {
1957     private:
1958         OUString aTypeURL;
1959         DirectoryItem rItem;
1960 
1961     public:
setUp()1962         void setUp() override
1963         {
1964             aTypeURL = aUserDirectoryURL.copy(0);
1965             concatURL(aTypeURL, aTmpName2);
1966             createTestFile(aTypeURL);
1967             osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1968             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1969         }
1970 
tearDown()1971         void tearDown() override
1972         {
1973             deleteTestFile(aTypeURL);
1974         }
1975 
1976 
getFileName_001()1977         void getFileName_001()
1978         {
1979             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
1980             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1981             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1982 
1983             OUString aFileName = rFileStatus.getFileName();
1984 
1985             CPPUNIT_ASSERT_MESSAGE("test for getFileName function: name compare with specify",
1986                                     compareFileName(aFileName, aTmpName2));
1987         }
1988 
1989         CPPUNIT_TEST_SUITE(getFileName);
1990             CPPUNIT_TEST(getFileName_001);
1991         CPPUNIT_TEST_SUITE_END();
1992     };
1993 
1994     // testing the method
1995     // inline OUString getFileURL() const
1996 
1997     class getFileURL : public CppUnit::TestFixture
1998     {
1999         DirectoryItem rItem;
2000 
2001     public:
setUp()2002         void setUp() override
2003         {
2004             createTestFile(aTmpName6);
2005             osl::FileBase::RC nError = DirectoryItem::get(aTmpName6, rItem);
2006             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
2007         }
2008 
tearDown()2009         void tearDown() override
2010         {
2011             deleteTestFile(aTmpName6);
2012         }
2013 
2014 
getFileURL_001()2015         void getFileURL_001()
2016         {
2017             FileStatus   rFileStatus(osl_FileStatus_Mask_FileURL);
2018             osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
2019             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
2020 
2021             OUString aFileURL = rFileStatus.getFileURL();
2022 
2023             CPPUNIT_ASSERT_MESSAGE("test for getFileURL function: ",
2024                                     compareFileName(aFileURL, aTmpName6));
2025         }
2026 
2027         CPPUNIT_TEST_SUITE(getFileURL);
2028             CPPUNIT_TEST(getFileURL_001);
2029         CPPUNIT_TEST_SUITE_END();
2030     };
2031 
2032     //  testing the method
2033     //  inline OUString getLinkTargetURL() const
2034 
2035     class getLinkTargetURL : public CppUnit::TestFixture
2036     {
2037     private:
2038         OUString aTypeURL;
2039         DirectoryItem rItem;
2040 
2041     public:
setUp()2042         void setUp() override
2043         {
2044             aTypeURL = aUserDirectoryURL.copy(0);
2045             concatURL(aTypeURL, aTmpName2);
2046             createTestFile(aTypeURL);
2047         }
2048 
tearDown()2049         void tearDown() override
2050         {
2051             deleteTestFile(aTypeURL);
2052         }
2053 
getLinkTargetURL_001()2054         void getLinkTargetURL_001()
2055         {
2056 #if 0
2057 #if (defined UNX) // Link file is not defined in Windows
2058             // create a link file;
2059             OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
2060             aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/link.file";
2061             aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/tmpname";
2062 
2063             OString strLinkFileName, strSrcFileName;
2064             strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
2065             strSrcFileName  = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
2066 
2067             sal_Int32 fd;
2068             fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
2069             CPPUNIT_ASSERT_EQUAL_MESSAGE("in creating link file",  static_cast<sal_Int32>(0), fd);
2070 
2071             // get linkTarget URL
2072             auto nError = DirectoryItem::get(aLnkURL1, rItem);
2073             CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file item", osl::FileBase::E_None, nError);
2074 
2075             FileStatus   rFileStatus(osl_FileStatus_Mask_LinkTargetURL);
2076             nError = rItem.getFileStatus(rFileStatus);
2077             CPPUNIT_ASSERT_EQUAL_MESSAGE("in getting link file status", osl::FileBase::E_None, nError);
2078             OUString aFileURL = rFileStatus.getLinkTargetURL();
2079 
2080             // remove link file
2081             fd = remove(strLinkFileName.getStr());
2082             CPPUNIT_ASSERT_EQUAL_MESSAGE("in deleting link file",  static_cast<sal_Int32>(0), fd);
2083 
2084             CPPUNIT_ASSERT_MESSAGE("test for getLinkTargetURL function: Solaris version, create a file, and a link file link to it, get its LinkTargetURL and compare",
2085                                     compareFileName(aFileURL, aTypeURL));
2086 #endif
2087 #endif
2088         }
2089 
2090         CPPUNIT_TEST_SUITE(getLinkTargetURL);
2091             CPPUNIT_TEST(getLinkTargetURL_001);
2092         CPPUNIT_TEST_SUITE_END();
2093     };
2094 
2095     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::ctors, "osl_FileStatus");
2096     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::isValid, "osl_FileStatus");
2097     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileType, "osl_FileStatus");
2098     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAttributes, "osl_FileStatus");
2099     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getAccessTime, "osl_FileStatus");
2100     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getModifyTime, "osl_FileStatus");
2101     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileSize, "osl_FileStatus");
2102     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileName, "osl_FileStatus");
2103     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getFileURL, "osl_FileStatus");
2104     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileStatus::getLinkTargetURL, "osl_FileStatus");
2105 
2106     CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_FileStatus");
2107 }
2108 
2109 namespace osl_File
2110 {
2111 
2112     //  testing the method
2113     //  File(const OUString& ustrFileURL)
2114 
2115     class ctors : public CppUnit::TestFixture
2116     {
2117     public:
setUp()2118         void setUp() override
2119         {
2120             // create a tempfile in $TEMP/tmpdir/tmpname.
2121             createTestDirectory(aTmpName3);
2122             createTestFile(aTmpName4);
2123         }
2124 
tearDown()2125         void tearDown() override
2126         {
2127             // remove the tempfile in $TEMP/tmpdir/tmpname.
2128             deleteTestFile(aTmpName4);
2129             deleteTestDirectory(aTmpName3);
2130         }
2131 
ctors_001()2132         void ctors_001()
2133         {
2134             File testFile(aTmpName4);
2135 
2136             osl::FileBase::RC nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2137             osl::FileBase::RC nError2 = testFile.close();
2138             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2139                                      osl::FileBase::E_None, nError1);
2140             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a File and test its open and close",
2141                                      osl::FileBase::E_None, nError2);
2142         }
2143 
ctors_002()2144         void ctors_002()
2145         {
2146             File testFile(aTmpName5);
2147             char buffer[30] = "Test for File constructor";
2148             sal_uInt64 nCount;
2149 
2150             osl::FileBase::RC nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2151             osl::FileBase::RC nError2 = testFile.write(buffer, 30, nCount);
2152             testFile.close();
2153 
2154             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2155                                      osl::FileBase::E_None, nError1);
2156             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: test relative file URL, this test show that relative file URL is also acceptable",
2157                                      osl::FileBase::E_None, nError2);
2158         }
2159 
2160         CPPUNIT_TEST_SUITE(ctors);
2161             CPPUNIT_TEST(ctors_001);
2162             CPPUNIT_TEST(ctors_002);
2163         CPPUNIT_TEST_SUITE_END();
2164     };
2165 
2166     //  testing the method
2167     //  inline RC open(sal_uInt32 uFlags)
2168 
2169     class open : public CppUnit::TestFixture
2170     {
2171     public:
setUp()2172         void setUp() override
2173         {
2174             // create a tempfile in $TEMP/tmpdir/tmpname.
2175             createTestDirectory(aTmpName3);
2176             createTestFile(aTmpName4);
2177         }
2178 
tearDown()2179         void tearDown() override
2180         {
2181             // remove the tempfile in $TEMP/tmpdir/tmpname.
2182             deleteTestFile(aTmpName4);
2183             deleteTestDirectory(aTmpName3);
2184         }
2185 
2186 
open_001()2187         void open_001()
2188         {
2189             File testFile(aTmpName4);
2190 
2191             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2192             auto nError2 = testFile.close();
2193             CPPUNIT_ASSERT_EQUAL_MESSAGE("close error", osl::FileBase::E_None, nError2);
2194 
2195             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a regular file",
2196                                      osl::FileBase::E_None, nError1);
2197         }
2198 
open_002()2199         void open_002()
2200         {
2201             File testFile(aTmpName3);
2202 
2203             auto nError1 = testFile.open(osl_File_OpenFlag_Read);
2204 
2205             CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory",
2206                                      (File::E_INVAL == nError1) || (File::E_ACCES == nError1));
2207         }
2208 
open_003()2209         void open_003()
2210         {
2211             File testFile(aCanURL1);
2212 
2213             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2214 
2215             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a non-exist file",
2216                                      File::E_NOENT, nError1);
2217         }
2218 
open_004()2219         void open_004()
2220         {
2221             OUString  aTestFile(aRootURL);
2222             concatURL(aTestFile, aTmpName2);
2223             File testFile(aTestFile);
2224 
2225             auto nError1 = testFile.open(osl_File_OpenFlag_Create);
2226             bool bOK = (nError1 == File::E_ACCES || nError1 == File::E_ROFS);
2227 #ifdef _WIN32
2228             bOK = true;  /// in Windows, you can create file in c:\ any way.
2229             testFile.close();
2230             deleteTestFile(aTestFile);
2231 #endif
2232 
2233             CPPUNIT_ASSERT_MESSAGE("test for open function: create an illegal file",
2234                                     bOK);
2235         }
2236 
open_005()2237         void open_005()
2238         {
2239             File testFile(aTmpName4);
2240 
2241             auto nError1 = testFile.open(osl_File_OpenFlag_Create);
2242 
2243             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: create an exist file",
2244                                      File::E_EXIST, nError1);
2245         }
2246 
open_006()2247         void open_006()
2248         {
2249             File testFile(aCanURL1);
2250             char buffer_write[30] = "Test for File open";
2251             char buffer_read[30];
2252             sal_uInt64 nCount_write, nCount_read;
2253 
2254             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
2255             auto nError2 = testFile.write(buffer_write, 30, nCount_write);
2256             osl::FileBase::RC nError4 = testFile.setPos(osl_Pos_Absolut, 0);
2257             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError4);
2258             auto nError3 = testFile.read(buffer_read, 10, nCount_read);
2259 
2260             osl::FileBase::RC nError5 = testFile.close();
2261             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError5);
2262             osl::FileBase::RC nError6 = osl::File::remove(aCanURL1);
2263             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError6);
2264 
2265             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2266                                     osl::FileBase::E_None, nError1);
2267             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2268                                     osl::FileBase::E_None, nError2);
2269             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2270                                     osl::FileBase::E_None, nError3);
2271             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2272                                     sal_uInt64(30), nCount_write);
2273             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2274                                     sal_uInt64(10), nCount_read);
2275         }
2276 
2277         CPPUNIT_TEST_SUITE(open);
2278             CPPUNIT_TEST(open_001);
2279             CPPUNIT_TEST(open_002);
2280             CPPUNIT_TEST(open_003);
2281             CPPUNIT_TEST(open_004);
2282             CPPUNIT_TEST(open_005);
2283             CPPUNIT_TEST(open_006);
2284         CPPUNIT_TEST_SUITE_END();
2285     };
2286 
2287     //  testing the method
2288     //  inline RC close()
2289 
2290     class close : public CppUnit::TestFixture
2291     {
2292     public:
setUp()2293         void setUp() override
2294         {
2295             // create a tempfile in $TEMP/tmpdir/tmpname.
2296             createTestDirectory(aTmpName3);
2297             createTestFile(aTmpName4);
2298         }
2299 
tearDown()2300         void tearDown() override
2301         {
2302             // remove the tempfile in $TEMP/tmpdir/tmpname.
2303             deleteTestFile(aTmpName4);
2304             deleteTestDirectory(aTmpName3);
2305         }
2306 
2307 
close_001()2308         void close_001()
2309         {
2310             File testFile(aTmpName4);
2311 
2312             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2313             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2314 
2315             auto nError2 = testFile.close();
2316 
2317             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: close a regular file",
2318                                      osl::FileBase::E_None, nError2);
2319         }
2320 
close_002()2321         void close_002()
2322         {
2323             File testFile(aTmpName4);
2324 
2325             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2326             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2327 
2328             auto nError2 = testFile.close();
2329 
2330             auto nError3 = testFile.setPos(osl_Pos_Absolut, 0);
2331 
2332             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: manipulate a file after it has been closed",
2333                                      osl::FileBase::E_None, nError2);
2334             CPPUNIT_ASSERT_MESSAGE("test for close function: manipulate a file after it has been closed",
2335                                     (osl::FileBase::E_None != nError3));
2336         }
2337 
2338         CPPUNIT_TEST_SUITE(close);
2339             CPPUNIT_TEST(close_001);
2340             CPPUNIT_TEST(close_002);
2341         CPPUNIT_TEST_SUITE_END();
2342     };
2343 
2344     //  testing the method
2345     //  inline RC setPos(sal_uInt32 uHow, sal_Int64 uPos)
2346 
2347     class setPos : public CppUnit::TestFixture
2348     {
2349     public:
setUp()2350         void setUp() override
2351         {
2352             // create a tempfile in $TEMP/tmpdir/tmpname.
2353             createTestDirectory(aTmpName3);
2354             createTestFile(aTmpName4);
2355 
2356             // write chars into the file.
2357             File testFile(aTmpName4);
2358 
2359             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2360             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2361             sal_uInt64 nCount_write = 0;
2362             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2363             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2364             nError1 = testFile.close();
2365             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2366         }
2367 
tearDown()2368         void tearDown() override
2369         {
2370             // remove the tempfile in $TEMP/tmpdir/tmpname.
2371             deleteTestFile(aTmpName4);
2372             deleteTestDirectory(aTmpName3);
2373         }
2374 
setPos_001()2375         void setPos_001()
2376         {
2377             File testFile(aTmpName4);
2378             char buffer_read[2];
2379 
2380             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2381             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2382             nError1 = testFile.setPos(osl_Pos_Absolut, 26);
2383             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2384             sal_uInt64 nCount_read = 0;
2385             nError1 = testFile.read(buffer_read, 1, nCount_read);
2386             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2387             nError1 = testFile.close();
2388             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2389 
2390             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",
2391                                      pBuffer_Char[26], buffer_read[0]);
2392         }
2393 
setPos_002()2394         void setPos_002()
2395         {
2396             File testFile(aTmpName4);
2397             char buffer_read[2];
2398 
2399             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2400             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2401             nError1 = testFile.setPos(osl_Pos_Absolut, sizeof(pBuffer_Char) - 2);
2402             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2403             nError1 = testFile.setPos(osl_Pos_Current, 0);
2404             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2405             sal_uInt64 nCount_read = 0;
2406             nError1 = testFile.read(buffer_read, 1, nCount_read);
2407             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2408             nError1 = testFile.close();
2409             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2410 
2411             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",
2412                                      pBuffer_Char[sizeof(pBuffer_Char) - 2], buffer_read[0]);
2413         }
2414 
setPos_003()2415         void setPos_003()
2416         {
2417             File testFile(aTmpName4);
2418             char buffer_read[2];
2419 
2420             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2421             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2422             // the file size is smaller than 100
2423             nError1 = testFile.setPos(osl_Pos_End,  -100);
2424             CPPUNIT_ASSERT_EQUAL_MESSAGE("should return error", osl::FileBase::E_INVAL, nError1);
2425 
2426             nError1 = testFile.setPos(osl_Pos_End, -53);
2427             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2428             sal_uInt64 nCount_read = 0;
2429             nError1 = testFile.read(buffer_read, 1, nCount_read);
2430             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2431             nError1 = testFile.close();
2432             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2433 
2434             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",
2435                                      pBuffer_Char[0], buffer_read[0]);
2436         }
2437 
2438         CPPUNIT_TEST_SUITE(setPos);
2439             CPPUNIT_TEST(setPos_001);
2440             CPPUNIT_TEST(setPos_002);
2441             CPPUNIT_TEST(setPos_003);
2442         CPPUNIT_TEST_SUITE_END();
2443     };
2444 
2445     //  testing the method
2446     //  inline RC getPos(sal_uInt64& uPos)
2447 
2448     class getPos : public CppUnit::TestFixture
2449     {
2450     public:
setUp()2451         void setUp() override
2452         {
2453             // create a tempfile in $TEMP/tmpdir/tmpname.
2454             createTestDirectory(aTmpName3);
2455             createTestFile(aTmpName4);
2456 
2457             // write chars into the file.
2458             File testFile(aTmpName4);
2459 
2460             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2461             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2462             sal_uInt64 nCount_write = 0;
2463             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2464             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2465             nError1 = testFile.close();
2466             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2467         }
2468 
tearDown()2469         void tearDown() override
2470         {
2471             // remove the tempfile in $TEMP/tmpdir/tmpname.
2472             deleteTestFile(aTmpName4);
2473             deleteTestDirectory(aTmpName3);
2474         }
2475 
2476 
getPos_001()2477         void getPos_001()
2478         {
2479             File testFile(aTmpName4);
2480             sal_uInt64 nFilePointer;
2481 
2482             auto nError1 = testFile.getPos(nFilePointer);
2483             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, nError1);
2484 
2485             nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2486             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2487 
2488             nError1 = testFile.setPos(osl_Pos_Absolut, 26);
2489             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2490             nError1 = testFile.getPos(nFilePointer);
2491             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2492 
2493             nError1 = testFile.close();
2494             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2495 
2496             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getPos function: set the position to 26, get position and check if it is right",
2497                                      static_cast<sal_uInt64>(26), nFilePointer);
2498         }
2499 
2500         CPPUNIT_TEST_SUITE(getPos);
2501             CPPUNIT_TEST(getPos_001);
2502         CPPUNIT_TEST_SUITE_END();
2503     };
2504 
2505     //  testing the method
2506     //  inline RC isEndOfFile(sal_Bool *pIsEOF)
2507 
2508     class isEndOfFile : public CppUnit::TestFixture
2509     {
2510     public:
setUp()2511         void setUp() override
2512         {
2513             // create a tempfile in $TEMP/tmpdir/tmpname.
2514             createTestDirectory(aTmpName3);
2515             createTestFile(aTmpName4);
2516 
2517             // write chars into the file.
2518             File testFile(aTmpName4);
2519 
2520             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2521             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2522             sal_uInt64 nCount_write = 0;
2523             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2524             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2525             nError1 = testFile.close();
2526             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2527         }
2528 
tearDown()2529         void tearDown() override
2530         {
2531             // remove the tempfile in $TEMP/tmpdir/tmpname.
2532             deleteTestFile(aTmpName4);
2533             deleteTestDirectory(aTmpName3);
2534         }
2535 
2536 
isEndOfFile_001()2537         void isEndOfFile_001()
2538         {
2539             File testFile(aTmpName4);
2540             sal_Bool bEOF  = false;
2541             sal_Bool *pEOF = &bEOF;
2542 
2543             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2544             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2545 
2546             nError1 = testFile.setPos(osl_Pos_End, 0);
2547             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2548             nError1 = testFile.isEndOfFile(pEOF);
2549             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2550 
2551             nError1 = testFile.close();
2552             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2553 
2554             CPPUNIT_ASSERT_MESSAGE("test for isEndOfFile function: set the position to end, check if reach end",
2555                                      *pEOF);
2556         }
2557 
isEndOfFile_002()2558         void isEndOfFile_002()
2559         {
2560             File testFile(aTmpName4);
2561             sal_Bool bEOF  = false;
2562             sal_Bool *pEOF = &bEOF;
2563             sal_uInt64 nFilePointer = 0;
2564 
2565             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2566             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2567 
2568             nError1 = testFile.setPos(osl_Pos_Absolut, 0);
2569             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2570             *pEOF = false;
2571 
2572             while (!(*pEOF))
2573             {
2574                 nError1 = testFile.isEndOfFile(pEOF);
2575                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2576                 nError1 = testFile.setPos(osl_Pos_Current, 1);
2577                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2578             }
2579 
2580             nError1 = testFile.getPos(nFilePointer);
2581             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2582 
2583             nError1 = testFile.close();
2584             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2585 
2586             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isEndOfFile function: use isEndOfFile to move pointer step by step",
2587                                       static_cast<sal_uInt64>(sizeof(pBuffer_Char) + 1), nFilePointer);
2588         }
2589         CPPUNIT_TEST_SUITE(isEndOfFile);
2590             CPPUNIT_TEST(isEndOfFile_001);
2591             CPPUNIT_TEST(isEndOfFile_002);
2592         CPPUNIT_TEST_SUITE_END();
2593     };
2594 
2595     //  testing the method
2596     //  inline RC setSize(sal_uInt64 uSize)
2597 
2598     class setSize : public CppUnit::TestFixture
2599     {
2600     public:
setUp()2601         void setUp() override
2602         {
2603             // create a tempfile in $TEMP/tmpdir/tmpname.
2604             createTestDirectory(aTmpName3);
2605             createTestFile(aTmpName4);
2606 
2607             // write chars into the file.
2608             File testFile(aTmpName4);
2609 
2610             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2611             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2612             sal_uInt64 nCount_write = 0;
2613             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2614             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2615             nError1 = testFile.close();
2616             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2617         }
2618 
tearDown()2619         void tearDown() override
2620         {
2621             // remove the tempfile in $TEMP/tmpdir/tmpname.
2622             deleteTestFile(aTmpName4);
2623             deleteTestDirectory(aTmpName3);
2624         }
2625 
2626 
setSize_001()2627         void setSize_001()
2628         {
2629             File testFile(aTmpName4);
2630             sal_uInt64 nFilePointer;
2631 
2632             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2633             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2634 
2635             // enlarge the file to size of 100;
2636             nError1 = testFile.setSize(100);
2637             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2638 
2639             // get the file size;
2640             nError1 = testFile.setPos(osl_Pos_End, 0);
2641             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2642             nError1 = testFile.getPos(nFilePointer);
2643             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2644 
2645             nError1 = testFile.close();
2646             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2647 
2648             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: enlarge the file ",
2649                                      static_cast<sal_uInt64>(100), nFilePointer);
2650         }
2651 
setSize_002()2652         void setSize_002()
2653         {
2654             File testFile(aTmpName4);
2655             sal_uInt64 nFilePointer;
2656 
2657             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2658             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2659 
2660             // enlarge the file to size of 100;
2661             nError1 = testFile.setSize(10);
2662             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2663 
2664             // get the file size;
2665             nError1 = testFile.setPos(osl_Pos_End, 0);
2666             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2667             nError1 = testFile.getPos(nFilePointer);
2668             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2669 
2670             nError1 = testFile.close();
2671             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2672 
2673             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: truncate the file ",
2674                                      static_cast<sal_uInt64>(10), nFilePointer);
2675         }
2676 
2677         CPPUNIT_TEST_SUITE(setSize);
2678             CPPUNIT_TEST(setSize_001);
2679             CPPUNIT_TEST(setSize_002);
2680         CPPUNIT_TEST_SUITE_END();
2681     };
2682 
2683     //  testing the method
2684     //  inline RC read(void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead)
2685 
2686     class read : public CppUnit::TestFixture
2687     {
2688     public:
setUp()2689         void setUp() override
2690         {
2691             // create a tempfile in $TEMP/tmpdir/tmpname.
2692             createTestDirectory(aTmpName3);
2693             createTestFile(aTmpName4);
2694 
2695             // write chars into the file.
2696             File testFile(aTmpName4);
2697 
2698             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2699             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2700             sal_uInt64 nCount_write = 0;
2701             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2702             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2703             nError1 = testFile.close();
2704             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2705         }
2706 
tearDown()2707         void tearDown() override
2708         {
2709             // remove the tempfile in $TEMP/tmpdir/tmpname.
2710             deleteTestFile(aTmpName4);
2711             deleteTestDirectory(aTmpName3);
2712         }
2713 
2714 
read_001()2715         void read_001()
2716         {
2717             File testFile(aTmpName4);
2718             sal_uInt64 nFilePointer;
2719             char buffer_read[10];
2720 
2721             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2722             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2723             sal_uInt64 nCount_read = 0;
2724             nError1 = testFile.read(buffer_read, 10, nCount_read);
2725             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2726             nError1 = testFile.getPos(nFilePointer);
2727             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2728 
2729             nError1 = testFile.close();
2730             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2731 
2732             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2733                                      sal_uInt64(10), nFilePointer);
2734             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read whole content in the file to a buffer",
2735                                      0, strncmp(buffer_read, pBuffer_Char, 10));
2736         }
2737 
read_002()2738         void read_002()
2739         {
2740             File testFile(aTmpName4);
2741             sal_uInt64 nFilePointer;
2742             char buffer_read[26];
2743 
2744             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2745             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2746 
2747             nError1 = testFile.setPos(osl_Pos_Absolut, 26);
2748             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2749             sal_uInt64 nCount_read = 0;
2750             nError1 = testFile.read(buffer_read, 26, nCount_read);
2751             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2752             nError1 = testFile.getPos(nFilePointer);
2753             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2754 
2755             nError1 = testFile.close();
2756             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2757 
2758             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2759                                      sal_uInt64(52), nFilePointer);
2760             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2761                                      sal_uInt64(26), nCount_read);
2762             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for read function: read from a special position in the file",
2763                                      0, strncmp(buffer_read, &pBuffer_Char[26], 26));
2764         }
2765 
2766         CPPUNIT_TEST_SUITE(read);
2767             CPPUNIT_TEST(read_001);
2768             CPPUNIT_TEST(read_002);
2769         CPPUNIT_TEST_SUITE_END();
2770     };
2771 
2772     //  testing the method
2773     //  inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
2774 
2775     class write : public CppUnit::TestFixture
2776     {
2777     public:
setUp()2778         void setUp() override
2779         {
2780             // create a tempfile in $TEMP/tmpname.
2781             createTestFile(aTmpName6);
2782         }
2783 
tearDown()2784         void tearDown() override
2785         {
2786             // remove the tempfile in $TEMP/tmpname.
2787             deleteTestFile(aTmpName6);
2788         }
2789 
2790 
write_001()2791         void write_001()
2792         {
2793             File testFile(aTmpName6);
2794             sal_uInt64 nFilePointer;
2795             char buffer_read[10];
2796 
2797             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2798             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2799 
2800             sal_uInt64 nCount_write = 0, nCount_read = 0;
2801             // write chars into the file.
2802             nError1 = testFile.write(pBuffer_Char, 10, nCount_write);
2803             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2804             // get the current pointer;
2805             nError1 = testFile.getPos(nFilePointer);
2806             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2807             // reset pointer to the beginning;
2808             nError1 = testFile.setPos(osl_Pos_Absolut, 0);
2809             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2810             nError1 = testFile.read(buffer_read, 10, nCount_read);
2811             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2812 
2813             nError1 = testFile.close();
2814             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2815 
2816             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",
2817                                      sal_uInt64(10), nFilePointer);
2818             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",
2819                                     0, strncmp(buffer_read, pBuffer_Char, 10));
2820             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",
2821                                     sal_uInt64(10), nCount_write);
2822         }
2823 
2824         CPPUNIT_TEST_SUITE(write);
2825             CPPUNIT_TEST(write_001);
2826         CPPUNIT_TEST_SUITE_END();
2827     };
2828 
2829     //  testing the method
2830     //  inline RC readLine(::ByteSequence& aSeq)
2831 
2832     class readLine : public CppUnit::TestFixture
2833     {
2834         rtl::ByteSequence aSequence;
2835 
2836     public:
setUp()2837         void setUp() override
2838         {
2839             // create a tempfile in $TEMP/tmpname.
2840             createTestFile(aTmpName6);
2841 
2842             // write some strings into the file.
2843             File testFile(aTmpName6);
2844             char ppStrSeq[3][27]  =  { "abcde\n",
2845                                         "1234567890\n",
2846                                         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2847                                       };
2848 
2849             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2850             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2851 
2852             sal_uInt64 nCount_write = 0;
2853             for (int nCount = 0; nCount < 3; nCount++)
2854             {
2855                 nError1 = testFile.write(ppStrSeq[nCount], strlen(ppStrSeq[nCount]), nCount_write);
2856                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2857             }
2858 
2859             nError1 = testFile.close();
2860             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2861         }
2862 
tearDown()2863         void tearDown() override
2864         {
2865             // remove the tempfile in $TEMP/tmpname.
2866             deleteTestFile(aTmpName6);
2867         }
2868 
2869 
readLine_001()2870         void readLine_001()
2871         {
2872             File testFile(aTmpName6);
2873 
2874             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2875             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2876             nError1 = testFile.readLine(aSequence);
2877             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2878             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
2879                                     osl::FileBase::E_None, nError1);
2880             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read the first line of the file.",
2881                                     0, strncmp(reinterpret_cast<char *>(aSequence.getArray()), pBuffer_Char, 5));
2882         }
2883 
readLine_002()2884         void readLine_002()
2885         {
2886             File testFile(aTmpName6);
2887             sal_Bool bEOF  = false;
2888             sal_Bool *pEOF = &bEOF;
2889 
2890             auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2891             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2892             for (int nCount = 0; nCount < 3; nCount++)
2893             {
2894                 nError1 = testFile.readLine(aSequence);
2895                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2896             }
2897             nError1 = testFile.isEndOfFile(pEOF);
2898             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2899 
2900             CPPUNIT_ASSERT_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
2901                                      *pEOF);
2902             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for readLine function: read three lines of the file and check the file pointer moving.",
2903                                     0, strncmp(reinterpret_cast<char *>(aSequence.getArray()), &pBuffer_Char[26], 26));
2904         }
2905         CPPUNIT_TEST_SUITE(readLine);
2906             CPPUNIT_TEST(readLine_001);
2907             CPPUNIT_TEST(readLine_002);
2908         CPPUNIT_TEST_SUITE_END();
2909     };
2910 
2911     //  testing the method
2912     //  inline static RC copy(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
2913 
2914     class copy : public CppUnit::TestFixture
2915     {
2916     public:
setUp()2917         void setUp() override
2918         {
2919             // create a tempfile in $TEMP/tmpdir/tmpname.
2920             createTestDirectory(aTmpName3);
2921             createTestFile(aTmpName4);
2922 
2923             // write chars into the file.
2924             File testFile(aTmpName4);
2925 
2926             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2927             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2928             sal_uInt64 nCount_write = 0;
2929             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
2930             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2931             nError1 = testFile.close();
2932             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2933         }
2934 
tearDown()2935         void tearDown() override
2936         {
2937             // remove the tempfile in $TEMP/tmpdir/tmpname.
2938             deleteTestFile(aTmpName4);
2939             deleteTestDirectory(aTmpName3);
2940         }
2941 
copy_001()2942         void copy_001()
2943         {
2944             File    testFile(aTmpName6);
2945 
2946             // copy $TEMP/tmpdir/tmpname to $TEMP/tmpname.
2947             auto nError1 = File::copy(aTmpName4, aTmpName6);
2948             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2949             // check
2950             nError1 = testFile.open(osl_File_OpenFlag_Create);
2951             deleteTestFile(aTmpName6);
2952 
2953             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy file to upper directory",
2954                                      osl::FileBase::E_EXIST, nError1);
2955         }
2956 
copy_002()2957         void copy_002()
2958         {
2959             // copy $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
2960             auto nError1 = File::copy(aTmpName4, aTmpName3);
2961 
2962             CPPUNIT_ASSERT_MESSAGE("test for copy function: use directory as destination",
2963                                      (osl::FileBase::E_ISDIR == nError1) ||(osl::FileBase::E_ACCES == nError1));
2964         }
2965 
copy_003()2966         void copy_003()
2967         {
2968 #if 0
2969             // copy $TEMP/tmpdir/tmpname to $ROOT/tmpname.
2970             auto nError1 = File::copy(aTmpName4, aTmpName7);
2971             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy to an illegal place",
2972                                      osl::FileBase::E_ACCES, nError1);
2973 #endif
2974         }
2975 
copy_004()2976         void copy_004()
2977         {
2978             // copy $TEMP/tmpname to $TEMP/tmpdir/tmpname.
2979             auto nError1 = File::copy(aTmpName6, aTmpName4);
2980 
2981             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a not exist file",
2982                                      osl::FileBase::E_NOENT, nError1);
2983         }
2984 
copy_005()2985         void copy_005()
2986         {
2987             // copy $TEMP/tmpname to $TEMP/system.path using system path.
2988             auto nError1 = File::copy(aTmpName6, aSysPath1);
2989 
2990             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a file using system file path",
2991                                      osl::FileBase::E_INVAL, nError1);
2992         }
2993 
copy_006()2994         void copy_006()
2995         {
2996             createTestFile(aTmpName6);
2997             File tmpFile(aTmpName6);
2998             tmpFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Read);
2999             tmpFile.setSize(200);
3000             tmpFile.close();
3001             // copy to new path
3002             auto nError1 = File::copy(aTmpName6, aTmpName4);
3003             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3004 
3005             // check if is the new file
3006             File newFile(aTmpName4);
3007             newFile.open(osl_File_OpenFlag_Write | osl_File_OpenFlag_Read);
3008             nError1 = newFile.setPos(osl_Pos_End, 0);
3009             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3010 
3011             sal_uInt64 nFilePointer;
3012             nError1 = newFile.getPos(nFilePointer);
3013             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3014             newFile.close();
3015             deleteTestFile(aTmpName6);
3016             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: the dest file exist",
3017                         static_cast<sal_uInt64>(200), nFilePointer);
3018         }
3019 
3020         CPPUNIT_TEST_SUITE(copy);
3021             CPPUNIT_TEST(copy_001);
3022             CPPUNIT_TEST(copy_002);
3023             CPPUNIT_TEST(copy_003);
3024             CPPUNIT_TEST(copy_004);
3025             CPPUNIT_TEST(copy_005);
3026             CPPUNIT_TEST(copy_006);
3027         CPPUNIT_TEST_SUITE_END();
3028     };
3029 
3030     //  testing the method
3031     //  inline static RC move(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
3032 
3033     class move : public CppUnit::TestFixture
3034     {
3035     public:
setUp()3036         void setUp() override
3037         {
3038             // create a tempfile in $TEMP/tmpdir/tmpname.
3039             createTestDirectory(aTmpName3);
3040             createTestFile(aTmpName4);
3041 
3042             // write chars into the file.
3043             File testFile(aTmpName4);
3044 
3045             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
3046             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3047             sal_uInt64 nCount_write = 0;
3048             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
3049             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3050             nError1 = testFile.close();
3051             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3052         }
3053 
tearDown()3054         void tearDown() override
3055         {
3056             // remove the tempfile in $TEMP/tmpdir/tmpname.
3057             deleteTestFile(aTmpName4);
3058             deleteTestDirectory(aTmpName3);
3059         }
3060 
3061 
move_001()3062         void move_001()
3063         {
3064             // rename $TEMP/tmpdir/tmpname to $TEMP/canonical.name.
3065             auto nError1 = File::move(aTmpName4, aCanURL1);
3066             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3067             // check
3068             File  testFile(aCanURL1);
3069             auto nError2 = testFile.open(osl_File_OpenFlag_Create);
3070             deleteTestFile(aCanURL1);
3071 
3072             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: rename file to another directory",
3073                                      osl::FileBase::E_EXIST, nError2);
3074         }
3075 
move_002()3076         void move_002()
3077         {
3078 #ifdef _WIN32
3079             // move $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
3080             auto nError1 = File::move(aTmpName4, aTmpName3);
3081             // returned osl::FileBase::E_ACCES on WNT
3082             CPPUNIT_ASSERT_MESSAGE("test for move function: use directory as destination",
3083                  (osl::FileBase::E_ACCES == nError1 || osl::FileBase::E_ISDIR == nError1) ||(osl::FileBase::E_EXIST == nError1));
3084 #endif
3085         }
3086 
move_003()3087         void move_003()
3088         {
3089 #if 0
3090             // move $TEMP/tmpdir/tmpname to $ROOT/tmpname.
3091             auto nError1 = File::move(aTmpName4, aTmpName7);
3092             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move to an illegal place",
3093                                      osl::FileBase::E_ACCES, nError1);
3094 #endif
3095         }
3096 
move_004()3097         void move_004()
3098         {
3099             // move $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3100             auto nError1 = File::move(aTmpName6, aTmpName4);
3101 
3102             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a not exist file",
3103                                      osl::FileBase::E_NOENT, nError1);
3104         }
3105 
move_005()3106         void move_005()
3107         {
3108             // move $TEMP/tmpname to $TEMP/system.path using system path.
3109             auto nError1 = File::move(aTmpName6, aSysPath1);
3110 
3111             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a file using system file",
3112                                      osl::FileBase::E_INVAL, nError1);
3113         }
3114 
move_006()3115         void move_006()
3116         {
3117             // move directory $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3118             createTestDirectory(aTmpName6);
3119             auto nError1 = File::move(aTmpName6, aTmpName4);
3120             // move file $TEMP/tmpdir/tmpname to $TEMP/tmpname
3121             auto nError2 = File::move(aTmpName4, aTmpName6);
3122             deleteTestDirectory(aTmpName6);
3123 #if defined(_WIN32)
3124             deleteTestDirectory(aTmpName4);// in Windows, it can be moved!!!!! this is only for not influence the following test.
3125             deleteTestFile(aTmpName6);
3126             nError1 = osl::FileBase::E_NOTDIR;
3127             nError2 = osl::FileBase::E_ISDIR;
3128 #endif
3129             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3130                                      osl::FileBase::E_NOTDIR, nError1);
3131             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name, did not pass in (W32)",
3132                                      osl::FileBase::E_ISDIR, nError2);
3133         }
3134 
move_007()3135         void move_007()
3136         {
3137             // create directory $TEMP/tmpname.
3138             createTestDirectory(aTmpName6);
3139             // move directory $TEMP/tmpdir to $TEMP/tmpname/tmpdir
3140             auto nError1 = File::move(aTmpName3, aTmpName8);
3141             // check
3142             auto nError2 = Directory::create(aTmpName8);
3143             File::move(aTmpName8, aTmpName3);
3144             deleteTestDirectory(aTmpName6);
3145 
3146             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3147                                      osl::FileBase::E_None, nError1);
3148             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a directory to an exist file with same name",
3149                                     osl::FileBase::E_EXIST, nError2);
3150         }
3151 
3152         // bugid# 115420, after the bug fix, add the case
3153         CPPUNIT_TEST_SUITE(move);
3154             CPPUNIT_TEST(move_001);
3155             CPPUNIT_TEST(move_002);
3156             CPPUNIT_TEST(move_003);
3157             CPPUNIT_TEST(move_004);
3158             CPPUNIT_TEST(move_005);
3159             CPPUNIT_TEST(move_006);
3160             CPPUNIT_TEST(move_007);
3161         CPPUNIT_TEST_SUITE_END();
3162     };
3163 
3164     //  testing the method
3165     //  inline static RC remove(const OUString& ustrFileURL)
3166 
3167     class remove : public CppUnit::TestFixture
3168     {
3169     public:
setUp()3170         void setUp() override
3171         {
3172             // create a tempfile in $TEMP/tmpdir/tmpname.
3173             createTestDirectory(aTmpName3);
3174             createTestFile(aTmpName4);
3175 
3176             // write chars into the file.
3177             File testFile(aTmpName4);
3178 
3179             auto nError1 = testFile.open(osl_File_OpenFlag_Write);
3180             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3181             sal_uInt64 nCount_write = 0;
3182             nError1 = testFile.write(pBuffer_Char, sizeof(pBuffer_Char), nCount_write);
3183             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3184             nError1 = testFile.close();
3185             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3186         }
3187 
tearDown()3188         void tearDown() override
3189         {
3190             // remove the tempfile in $TEMP/tmpdir/tmpname.
3191             deleteTestFile(aTmpName4);
3192             deleteTestDirectory(aTmpName3);
3193         }
3194 
3195 
remove_001()3196         void remove_001()
3197         {
3198             // remove $TEMP/tmpdir/tmpname.
3199             auto nError1 = File::remove(aTmpName4);
3200             // check
3201             File    testFile(aTmpName4);
3202             auto nError2 = testFile.open(osl_File_OpenFlag_Create);
3203 
3204             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file",
3205                                     osl::FileBase::E_None, nError1);
3206             CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a file",
3207                                      (osl::FileBase::E_EXIST != nError2));
3208         }
3209 
remove_002()3210         void remove_002()
3211         {
3212             // remove $TEMP/tmpname.
3213             auto nError1 = File::remove(aTmpName6);
3214 
3215             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file not exist",
3216                                     osl::FileBase::E_NOENT, nError1);
3217         }
3218 
remove_003()3219         void remove_003()
3220         {
3221             // remove $TEMP/system/path.
3222             auto nError1 = File::remove(aSysPath2);
3223 
3224             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: removing a file not using full qualified URL",
3225                                     osl::FileBase::E_INVAL, nError1);
3226         }
3227 
remove_004()3228         void remove_004()
3229         {
3230             // remove $TEMP/tmpdir.
3231             auto nError1 = File::remove(aTmpName3);
3232 
3233             CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a directory",
3234                                     (osl::FileBase::E_ISDIR == nError1) || (osl::FileBase::E_ACCES == nError1));
3235         }
3236 
3237         CPPUNIT_TEST_SUITE(remove);
3238             CPPUNIT_TEST(remove_001);
3239             CPPUNIT_TEST(remove_002);
3240             CPPUNIT_TEST(remove_003);
3241             CPPUNIT_TEST(remove_004);
3242         CPPUNIT_TEST_SUITE_END();
3243     };
3244 
3245     //  testing the method
3246     //  inline static RC setAttributes(const OUString& ustrFileURL, sal_uInt64 uAttributes)
3247 
3248     class setAttributes : public CppUnit::TestFixture
3249     {
3250     private:
3251         DirectoryItem rItem;
3252 
3253     public:
setUp()3254         void setUp() override
3255         {
3256             // create a tempfile in $TEMP/tmpdir/tmpname.
3257             createTestFile(aTmpName6);
3258         }
3259 
tearDown()3260         void tearDown() override
3261         {
3262             // remove the tempfile in $TEMP/tmpdir/tmpname.
3263             deleteTestFile(aTmpName6);
3264         }
3265 
3266 
setAttributes_001()3267         void setAttributes_001()
3268         {
3269         // on windows, only can set 2 attributes: osl_File_Attribute_ReadOnly,  osl_File_Attribute_Hidden
3270 #ifdef UNX
3271             // set the file to readonly
3272             auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead);
3273             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3274             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3275             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3276             // get the file attributes
3277             FileStatus   rFileStatus(osl_FileStatus_Mask_Attributes);
3278             nError1 = rItem.getFileStatus(rFileStatus);
3279             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3280 
3281             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3282                                     static_cast<sal_uInt64>(osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead),
3283                                     rFileStatus.getAttributes());
3284 #else
3285             // please see GetFileAttributes
3286             auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly);
3287             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3288             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3289             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3290             // get the file attributes
3291             FileStatus   rFileStatus(osl_FileStatus_Mask_Attributes);
3292             nError1 = rItem.getFileStatus(rFileStatus);
3293             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3294             // here the file has 2 Attributes: FILE_ATTRIBUTE_READONLY and FILE_ATTRIBUTE_NORMAL,
3295             // but FILE_ATTRIBUTE_NORMAL is valid only if used alone, so this is maybe a bug
3296             /*OString aString = OUStringToOString(aTmpName6, RTL_TEXTENCODING_ASCII_US);
3297             DWORD dwFileAttributes = GetFileAttributes(aString.getStr());
3298             if (dwFileAttributes & FILE_ATTRIBUTE_NORMAL)
3299                 printf("has normal attribute");
3300             if (dwFileAttributes & FILE_ATTRIBUTE_READONLY)
3301                 printf("has readonly attribute");
3302             */
3303             CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes READONLY and get it to verify.",
3304                                     (osl_File_Attribute_ReadOnly & rFileStatus.getAttributes()) != 0);
3305 #endif
3306     }
setAttributes_002()3307         void setAttributes_002()
3308         {
3309         // on UNX, can not set hidden attribute to file, rename file can set the attribute
3310 #ifdef _WIN32
3311             // set the file to hidden
3312             auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_Hidden);
3313 
3314             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3315             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3316             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3317             // get the file attributes
3318             FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
3319             nError1 = rItem.getFileStatus(rFileStatus);
3320             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3321 
3322             CPPUNIT_ASSERT_MESSAGE("test for setAttributes function: set file attributes and get it to verify.",
3323                                     (osl_File_Attribute_Hidden & rFileStatus.getAttributes()) != 0);
3324 #endif
3325         }
3326 
3327         CPPUNIT_TEST_SUITE(setAttributes);
3328             CPPUNIT_TEST(setAttributes_001);
3329             CPPUNIT_TEST(setAttributes_002);
3330         CPPUNIT_TEST_SUITE_END();
3331     };
3332 
3333     //  testing the method
3334     //  inline static RC setTime(
3335     //         const OUString& ustrFileURL,
3336     //         const TimeValue& rCreationTime,
3337     //         const TimeValue& rLastAccessTime,
3338     //         const TimeValue& rLastWriteTime)
3339 
3340     class setTime : public CppUnit::TestFixture
3341     {
3342     private:
3343         DirectoryItem rItem;
3344 
3345     public:
setUp()3346         void setUp() override
3347         {
3348             // create a tempfile in $TEMP/tmpdir/tmpname.
3349             createTestFile(aTmpName6);
3350         }
3351 
tearDown()3352         void tearDown() override
3353         {
3354             // remove the tempfile in $TEMP/tmpdir/tmpname.
3355             deleteTestFile(aTmpName6);
3356         }
3357 
3358 
setTime_001()3359         void setTime_001()
3360         {
3361             TimeValue *pTV_current  = nullptr;
3362             CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3363             TimeValue *pTV_creation = nullptr;
3364             CPPUNIT_ASSERT((pTV_creation = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3365             TimeValue *pTV_access   = nullptr;
3366             CPPUNIT_ASSERT((pTV_access = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3367             TimeValue *pTV_modify   = nullptr;
3368             CPPUNIT_ASSERT((pTV_modify = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
3369 
3370             // get current time
3371             bool bOk = osl_getSystemTime(pTV_current);
3372             CPPUNIT_ASSERT(bOk);
3373 
3374             // set the file time
3375             auto nError2 = File::setTime(aTmpName6, *pTV_current, *pTV_current, *pTV_current);
3376             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError2).getStr(), osl::FileBase::E_None, nError2);
3377 
3378              // get the file access time, creation time, modify time
3379             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3380             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3381 
3382             FileStatus   rFileStatus(osl_FileStatus_Mask_AccessTime);
3383             nError1 = rItem.getFileStatus(rFileStatus);
3384             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3385             *pTV_access = rFileStatus.getAccessTime();
3386 
3387             FileStatus   rFileStatus1(osl_FileStatus_Mask_CreationTime);
3388             nError1 = rItem.getFileStatus(rFileStatus1);
3389             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3390             *pTV_creation = rFileStatus1.getCreationTime();
3391 
3392             FileStatus   rFileStatus2(osl_FileStatus_Mask_ModifyTime);
3393             nError1 = rItem.getFileStatus(rFileStatus2);
3394             CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
3395             *pTV_modify = rFileStatus2.getModifyTime();
3396 
3397             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.",
3398                 t_compareTime(pTV_access, pTV_current, delta));
3399 #if defined(_WIN32)
3400             // Unfortunately there is no way to get the creation time of a file under Unix (it's a Windows only feature).
3401             // That means the flag osl_FileStatus_Mask_CreationTime should be deprecated under Unix.
3402             CPPUNIT_ASSERT_MESSAGE("test for setTime function: set creation time then get it. ",
3403                 t_compareTime(pTV_creation, pTV_current, delta));
3404 #endif
3405             CPPUNIT_ASSERT_MESSAGE("test for setTime function: set modify time then get it. ",
3406                 t_compareTime(pTV_modify, pTV_current, delta));
3407             free(pTV_current);
3408             free(pTV_creation);
3409             free(pTV_access);
3410             free(pTV_modify);
3411         }
3412 
3413         CPPUNIT_TEST_SUITE(setTime);
3414             CPPUNIT_TEST(setTime_001);
3415         CPPUNIT_TEST_SUITE_END();
3416     };
3417 
3418     //  testing the method
3419     //  inline static RC sync()
3420 
3421     class sync : public CppUnit::TestFixture
3422     {
3423     private:
3424         DirectoryItem rItem;
3425 
3426     public:
setUp()3427         void setUp() override
3428         {
3429             // create a tempfile in $TEMP/tmpdir/tmpname.
3430             createTestFile(aTmpName6);
3431 
3432         }
3433 
tearDown()3434         void tearDown() override
3435         {
3436             // remove the tempfile in $TEMP/tmpdir/tmpname.
3437             deleteTestFile(aTmpName6);
3438         }
3439 
3440         // test case: if The file is located on a read only file system.
sync_001()3441         void sync_001()
3442         {
3443             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3444             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3445 
3446             File tmp_file(aTmpName6);
3447             osl::FileBase::RC err = tmp_file.open(osl_File_OpenFlag_Write);
3448 
3449             CPPUNIT_ASSERT_EQUAL_MESSAGE("File open failed", osl::FileBase::E_None, err);
3450 
3451             char buffer[50000];
3452             sal_uInt64 written = 0;
3453             nError1 = tmp_file.write(static_cast<void*>(buffer), sizeof(buffer), written);
3454             CPPUNIT_ASSERT_EQUAL_MESSAGE("write failed!", osl::FileBase::E_None, nError1);
3455 
3456             // set the file to readonly
3457             auto nError2 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly | osl_File_Attribute_GrpRead | osl_File_Attribute_OwnRead | osl_File_Attribute_OthRead);
3458             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3459 
3460             nError2 = tmp_file.sync();
3461 
3462             CPPUNIT_ASSERT_EQUAL_MESSAGE("can not sync to readonly file!", osl::FileBase::E_None, nError2);
3463 
3464             tmp_file.close();
3465         }
3466       // test case:no enough space, how to create such case???see test_cpy_wrt_file.cxx::test_osl_writeFile
3467 
3468         CPPUNIT_TEST_SUITE(sync);
3469             CPPUNIT_TEST(sync_001);
3470         CPPUNIT_TEST_SUITE_END();
3471     };
3472 
3473     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::ctors, "osl_File");
3474     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::open, "osl_File");
3475     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::close, "osl_File");
3476     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setPos, "osl_File");
3477     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::getPos, "osl_File");
3478     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::isEndOfFile, "osl_File");
3479     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setSize, "osl_File");
3480     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::read, "osl_File");
3481     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::write, "osl_File");
3482     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::readLine, "osl_File");
3483     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::copy, "osl_File");
3484     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::move, "osl_File");
3485     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::remove, "osl_File");
3486     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setAttributes, "osl_File");
3487     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::setTime, "osl_File");
3488     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_File::sync, "osl_File");
3489 
3490     CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_File");
3491 }
3492 
3493 // Beginning of the test cases for DirectoryItem class
3494 
3495 namespace osl_DirectoryItem
3496 {
3497     //  testing the method
3498     //  DirectoryItem(): _pData(NULL)
3499 
3500     class ctors : public CppUnit::TestFixture
3501     {
3502     public:
setUp()3503         void setUp() override
3504         {
3505             // create a tempfile in $TEMP/tmpname.
3506             createTestFile(aTmpName6);
3507         }
3508 
tearDown()3509         void tearDown() override
3510         {
3511             // remove the tempfile in $TEMP/tmpname.
3512             deleteTestFile(aTmpName6);
3513         }
3514 
ctors_001()3515         void ctors_001()
3516         {
3517             File testFile(aTmpName6);
3518             DirectoryItem rItem;  // constructor
3519 
3520              // get the DirectoryItem.
3521             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3522             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3523 
3524             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: initialize a new instance of DirectoryItem and get an item to check.",
3525                                       osl::FileBase::E_None, nError1);
3526         }
3527 
3528         CPPUNIT_TEST_SUITE(ctors);
3529             CPPUNIT_TEST(ctors_001);
3530         CPPUNIT_TEST_SUITE_END();
3531     };
3532 
3533     //  testing the method
3534     //  DirectoryItem(const DirectoryItem& rItem): _pData(rItem._pData)
3535 
3536     class copy_assin_Ctors : public CppUnit::TestFixture
3537     {
3538     public:
setUp()3539         void setUp() override
3540         {
3541             // create a tempfile in $TEMP/tmpname.
3542             createTestFile(aTmpName6);
3543         }
3544 
tearDown()3545         void tearDown() override
3546         {
3547             // remove the tempfile in $TEMP/tmpname.
3548             deleteTestFile(aTmpName6);
3549         }
3550 
3551 
copy_assin_Ctors_001()3552         void copy_assin_Ctors_001()
3553         {
3554             DirectoryItem rItem;  // constructor
3555              // get the DirectoryItem.
3556             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3557             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3558 
3559             DirectoryItem copyItem(rItem); // copy constructor
3560             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3561             nError1 = copyItem.getFileStatus(rFileStatus);
3562             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3563 
3564             CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: use copy constructor to get an item and check filename.",
3565                                     compareFileName(rFileStatus.getFileName(), aTmpName2));
3566         }
3567 
copy_assin_Ctors_002()3568         void copy_assin_Ctors_002()
3569         {
3570             DirectoryItem rItem;  // constructor
3571              // get the DirectoryItem.
3572             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3573             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3574 
3575             DirectoryItem copyItem;
3576             copyItem = rItem;               // assignment operator
3577             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3578             nError1 = copyItem.getFileStatus(rFileStatus);
3579             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3580 
3581             CPPUNIT_ASSERT_MESSAGE("test for copy_assin_Ctors function: test assignment operator here since it is same as copy constructor in test way.",
3582                                     compareFileName(rFileStatus.getFileName(), aTmpName2));
3583         }
3584 
3585         CPPUNIT_TEST_SUITE(copy_assin_Ctors);
3586             CPPUNIT_TEST(copy_assin_Ctors_001);
3587             CPPUNIT_TEST(copy_assin_Ctors_002);
3588         CPPUNIT_TEST_SUITE_END();
3589     };
3590 
3591     //  testing the method
3592     //  inline sal_Bool is()
3593 
3594     class is : public CppUnit::TestFixture
3595     {
3596     public:
setUp()3597         void setUp() override
3598         {
3599             // create a tempfile in $TEMP/tmpname.
3600             createTestFile(aTmpName6);
3601         }
3602 
tearDown()3603         void tearDown() override
3604         {
3605             // remove the tempfile in $TEMP/tmpname.
3606             deleteTestFile(aTmpName6);
3607         }
3608 
is_001()3609         void is_001()
3610         {
3611             DirectoryItem rItem;  // constructor
3612 
3613             CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3614                                     !rItem.is());
3615         }
3616 
is_002()3617         void is_002()
3618         {
3619             DirectoryItem    rItem;  // constructor
3620              // get the DirectoryItem.
3621             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3622             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3623 
3624             CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3625                                     rItem.is());
3626         }
3627 
3628         CPPUNIT_TEST_SUITE(is);
3629             CPPUNIT_TEST(is_001);
3630             CPPUNIT_TEST(is_002);
3631         CPPUNIT_TEST_SUITE_END();
3632     };
3633 
3634     //  testing the method
3635     //  static inline RC get(const OUString& ustrFileURL, DirectoryItem& rItem)
3636 
3637     class get : public CppUnit::TestFixture
3638     {
3639     public:
setUp()3640         void setUp() override
3641         {
3642             // create a tempfile in $TEMP/tmpname.
3643             createTestFile(aTmpName6);
3644         }
3645 
tearDown()3646         void tearDown() override
3647         {
3648             // remove the tempfile in $TEMP/tmpname.
3649             deleteTestFile(aTmpName6);
3650         }
3651 
3652 
get_001()3653         void get_001()
3654         {
3655             DirectoryItem rItem;
3656             auto nError2 = DirectoryItem::get(aTmpName6, rItem);
3657 
3658             // check the file name
3659             FileStatus   rFileStatus(osl_FileStatus_Mask_FileName);
3660             auto nError1 = rItem.getFileStatus(rFileStatus);
3661             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3662 
3663             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3664                                     osl::FileBase::E_None, nError2);
3665             CPPUNIT_ASSERT_MESSAGE("test for get function: use copy constructor to get an item and check filename.",
3666                                     compareFileName(rFileStatus.getFileName(), aTmpName2));
3667         }
3668 
get_002()3669         void get_002()
3670         {
3671             DirectoryItem rItem;
3672             auto nError1 = DirectoryItem::get(aSysPath1, rItem);
3673 
3674             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a system name instead of a URL.",
3675                                     osl::FileBase::E_INVAL, nError1);
3676         }
3677 
get_003()3678         void get_003()
3679         {
3680             DirectoryItem rItem;
3681 
3682             auto nError1 = DirectoryItem::get(aTmpName3, rItem);
3683 
3684             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a non existed file URL.",
3685                                     osl::FileBase::E_NOENT, nError1);
3686         }
3687 
3688         CPPUNIT_TEST_SUITE(get);
3689             CPPUNIT_TEST(get_001);
3690             CPPUNIT_TEST(get_002);
3691             CPPUNIT_TEST(get_003);
3692         CPPUNIT_TEST_SUITE_END();
3693     };
3694 
3695     //  testing the method
3696     //  inline RC getFileStatus(FileStatus& rStatus)
3697 
3698     class getFileStatus : public CppUnit::TestFixture
3699     {
3700     public:
setUp()3701         void setUp() override
3702         {
3703             // create a tempfile in $TEMP/tmpdir/tmpname.
3704             createTestDirectory(aTmpName3);
3705             createTestFile(aTmpName4);
3706         }
3707 
tearDown()3708         void tearDown() override
3709         {
3710             // remove the tempfile in $TEMP/tmpdir/tmpname.
3711             deleteTestFile(aTmpName4);
3712             deleteTestDirectory(aTmpName3);
3713         }
3714 
3715 
getFileStatus_001()3716         void getFileStatus_001()
3717         {
3718             DirectoryItem rItem;
3719              // get the DirectoryItem.
3720             auto nError1 = DirectoryItem::get(aTmpName4, rItem);
3721             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3722 
3723             // check the file name
3724             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3725             auto nError2 = rItem.getFileStatus(rFileStatus);
3726 
3727             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get file status and check filename",
3728                                     osl::FileBase::E_None, nError2);
3729             CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get file status and check filename",
3730                                     compareFileName(rFileStatus.getFileName(), aTmpName2));
3731         }
3732 
getFileStatus_002()3733         void getFileStatus_002()
3734         {
3735             DirectoryItem rItem;  // constructor
3736              // get the DirectoryItem.
3737             auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3738             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_NOENT, nError1);
3739 
3740             // check the file name
3741             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3742             auto nError2 = rItem.getFileStatus(rFileStatus);
3743 
3744             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: file not existed",
3745                                     osl::FileBase::E_INVAL, nError2);
3746         }
3747 
getFileStatus_003()3748         void getFileStatus_003()
3749         {
3750             DirectoryItem    rItem;  // constructor
3751              // get the DirectoryItem.
3752             auto nError1 = DirectoryItem::get(aTmpName3, rItem);
3753             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3754 
3755             // check the file name
3756             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3757             auto nError2 = rItem.getFileStatus(rFileStatus);
3758 
3759             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: get directory information",
3760                                     osl::FileBase::E_None, nError2);
3761             CPPUNIT_ASSERT_MESSAGE("test for getFileStatus function: get directory information",
3762                                     compareFileName(rFileStatus.getFileName(), aTmpName1));
3763         }
3764 
3765         CPPUNIT_TEST_SUITE(getFileStatus);
3766             CPPUNIT_TEST(getFileStatus_001);
3767             CPPUNIT_TEST(getFileStatus_002);
3768             CPPUNIT_TEST(getFileStatus_003);
3769         CPPUNIT_TEST_SUITE_END();
3770     };
3771 
3772     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::ctors, "osl_DirectoryItem");
3773     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::copy_assin_Ctors, "osl_DirectoryItem");
3774     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::is, "osl_DirectoryItem");
3775     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::get, "osl_DirectoryItem");
3776     CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_DirectoryItem::getFileStatus, "osl_DirectoryItem");
3777 
3778     CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_DirectoryItem");
3779 }
3780 
3781 // Beginning of the test cases for Directory class
3782 
3783 namespace osl_Directory
3784 {
3785     //  testing the method
3786     //  Directory(const OUString& strPath): _pData(0), _aPath(strPath)
3787 
3788     class ctors : public CppUnit::TestFixture
3789     {
3790     public:
setUp()3791         void setUp() override
3792         {
3793             // create a tempfile in $TEMP/tmpdir/tmpname.
3794             createTestDirectory(aTmpName3);
3795             createTestFile(aTmpName4);
3796         }
3797 
tearDown()3798         void tearDown() override
3799         {
3800             // remove the tempfile in $TEMP/tmpdir/tmpname.
3801             deleteTestFile(aTmpName4);
3802             deleteTestDirectory(aTmpName3);
3803             // LLA: t_print("tearDown done.\n");
3804         }
3805 
3806 
ctors_001()3807         void ctors_001()
3808         {
3809             Directory testDirectory(aTmpName3); // constructor
3810 
3811             // open a directory
3812             auto nError1 = testDirectory.open();
3813             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3814             // close a directory
3815             auto nError2 = testDirectory.close();
3816             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3817 
3818             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
3819                                      osl::FileBase::E_None, nError1);
3820             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: create an instance and check open and close",
3821                                     osl::FileBase::E_None, nError2);
3822         }
3823 
ctors_002()3824         void ctors_002()
3825         {
3826             Directory testDirectory(aTmpName9); // constructor
3827 
3828             // open a directory
3829             auto nError1 = testDirectory.open();
3830             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3831             // close a directory
3832             auto nError2 = testDirectory.close();
3833             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3834 
3835             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
3836                                      osl::FileBase::E_None, nError1);
3837             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for ctors function: relative URL, :-), it is also worked",
3838                                     osl::FileBase::E_None, nError2);
3839         }
3840 
3841         CPPUNIT_TEST_SUITE(ctors);
3842             CPPUNIT_TEST(ctors_001);
3843             CPPUNIT_TEST(ctors_002);
3844         CPPUNIT_TEST_SUITE_END();
3845     };
3846 
3847     //  testing the method
3848     //  inline RC open()
3849 
3850     class open : public CppUnit::TestFixture
3851     {
3852     public:
setUp()3853         void setUp() override
3854         {
3855             // create a tempfile in $TEMP/tmpdir/tmpname.
3856             createTestDirectory(aTmpName3);
3857             createTestFile(aTmpName4);
3858         }
3859 
tearDown()3860         void tearDown() override
3861         {
3862             // remove the tempfile in $TEMP/tmpdir/tmpname.
3863             deleteTestFile(aTmpName4);
3864             deleteTestDirectory(aTmpName3);
3865         }
3866 
open_001()3867         void open_001()
3868         {
3869             Directory testDirectory(aTmpName3);
3870 
3871             // open a directory
3872             auto nError1 = testDirectory.open();
3873             // check if directory is opened.
3874             bool bOk = testDirectory.isOpen();
3875             // close a directory
3876             auto nError2 = testDirectory.close();
3877 
3878             CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory and check for open",
3879                                     bOk);
3880             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
3881                                      osl::FileBase::E_None, nError1);
3882             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a directory and check for open",
3883                                     osl::FileBase::E_None, nError2);
3884         }
3885 
open_002()3886         void open_002()
3887         {
3888             Directory testDirectory(aTmpName6);
3889 
3890             auto nError1 = testDirectory.open();
3891             if (nError1 == osl::FileBase::E_None)
3892             {
3893                 auto nError2 = testDirectory.close();
3894                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3895             }
3896 
3897             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a file that is not existed",
3898                                      osl::FileBase::E_NOENT, nError1);
3899         }
3900 
open_003()3901         void open_003()
3902         {
3903             Directory testDirectory(aUserDirectorySys);
3904 
3905             auto nError1 = testDirectory.open();
3906             if (nError1 == osl::FileBase::E_None)
3907             {
3908                 auto nError2 = testDirectory.close();
3909                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3910             }
3911 
3912             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: using system path",
3913                                      osl::FileBase::E_INVAL, nError1);
3914         }
3915 
open_004()3916         void open_004()
3917         {
3918             Directory testDirectory(aTmpName4);
3919 
3920             auto nError1 = testDirectory.open();
3921             if (nError1 == osl::FileBase::E_None)
3922             {
3923                 auto nError2 = testDirectory.close();
3924                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3925             }
3926 
3927             CPPUNIT_ASSERT_MESSAGE("test for open function: open a file instead of a directory",
3928                                      (osl::FileBase::E_NOTDIR == nError1) || (osl::FileBase::E_ACCES == nError1));
3929         }
3930 
3931         CPPUNIT_TEST_SUITE(open);
3932             CPPUNIT_TEST(open_001);
3933             CPPUNIT_TEST(open_002);
3934             CPPUNIT_TEST(open_003);
3935             CPPUNIT_TEST(open_004);
3936         CPPUNIT_TEST_SUITE_END();
3937     };
3938 
3939     //  testing the method
3940     //  inline sal_Bool isOpen() { return _pData != NULL; };
3941 
3942     class isOpen : public CppUnit::TestFixture
3943     {
3944     public:
setUp()3945         void setUp() override
3946         {
3947             // create a tempfile in $TEMP/tmpdir/tmpname.
3948             createTestDirectory(aTmpName3);
3949             createTestFile(aTmpName4);
3950         }
3951 
tearDown()3952         void tearDown() override
3953         {
3954             // remove the tempfile in $TEMP/tmpdir/tmpname.
3955             deleteTestFile(aTmpName4);
3956             deleteTestDirectory(aTmpName3);
3957         }
3958 
3959 
isOpen_001()3960         void isOpen_001()
3961         {
3962             Directory testDirectory(aTmpName3); // constructor
3963 
3964             // open a directory
3965             auto nError1 = testDirectory.open();
3966             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3967             // check if directory is opened.
3968             bool bOk = testDirectory.isOpen();
3969             // close a directory
3970             auto nError2 = testDirectory.close();
3971             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3972             CPPUNIT_ASSERT_MESSAGE("test for isOpen function: open a directory and check for open",
3973                                     bOk);
3974         }
3975 
isOpen_002()3976         void isOpen_002()
3977         {
3978             Directory testDirectory(aTmpName3); // constructor
3979 
3980             // check if directory is opened.
3981             bool bOk = testDirectory.isOpen();
3982 
3983             CPPUNIT_ASSERT_MESSAGE("test for isOpen function: do not open a directory and check for open",
3984                                     !bOk);
3985         }
3986 
3987         CPPUNIT_TEST_SUITE(isOpen);
3988             CPPUNIT_TEST(isOpen_001);
3989             CPPUNIT_TEST(isOpen_002);
3990         CPPUNIT_TEST_SUITE_END();
3991     };
3992 
3993     //  testing the method
3994     //  inline RC close()
3995 
3996     class close : public CppUnit::TestFixture
3997     {
3998     public:
setUp()3999         void setUp() override
4000         {
4001             // create a tempdirectory : $TEMP/tmpdir.
4002             createTestDirectory(aTmpName3);
4003         }
4004 
tearDown()4005         void tearDown() override
4006         {
4007             // remove a tempdirectory : $TEMP/tmpdir.
4008             deleteTestDirectory(aTmpName3);
4009         }
4010 
close_001()4011         void close_001()
4012         {
4013             Directory testDirectory(aTmpName3);
4014 
4015             // open a directory
4016             auto nError1 = testDirectory.open();
4017             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4018             // close a directory
4019             auto nError2 = testDirectory.close();
4020             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
4021             // check if directory is opened.
4022             bool bOk = testDirectory.isOpen();
4023 
4024             CPPUNIT_ASSERT_MESSAGE("test for isOpen function: close a directory and check for open",
4025                                     !bOk);
4026         }
4027 
close_002()4028         void close_002()
4029         {
4030             Directory testDirectory(aTmpName3);
4031 
4032             // close a directory
4033             auto nError1 = testDirectory.close();
4034 
4035             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isOpen function: close a not opened directory",
4036                                     osl::FileBase::E_BADF, nError1);
4037         }
4038 
4039         CPPUNIT_TEST_SUITE(close);
4040             CPPUNIT_TEST(close_001);
4041             CPPUNIT_TEST(close_002);
4042         CPPUNIT_TEST_SUITE_END();
4043     };
4044 
4045     //  testing the method
4046     //  inline RC reset()
4047 
4048     class reset : public CppUnit::TestFixture
4049     {
4050     private:
4051         DirectoryItem    rItem;
4052 
4053     public:
setUp()4054         void setUp() override
4055         {
4056             // create a tempdirectory : $TEMP/tmpdir.
4057             createTestDirectory(aTmpName3);
4058             // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4059             createTestFile(aTmpName3, aTmpName2);
4060             createTestFile(aTmpName3, aTmpName1);
4061             createTestFile(aTmpName3, aHidURL1);
4062         }
4063 
tearDown()4064         void tearDown() override
4065         {
4066             // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4067             deleteTestFile(aTmpName3, aHidURL1);
4068             deleteTestFile(aTmpName3, aTmpName1);
4069             deleteTestFile(aTmpName3, aTmpName2);
4070             // remove a tempdirectory : $TEMP/tmpdir.
4071             deleteTestDirectory(aTmpName3);
4072         }
4073 
4074 
reset_001()4075         void reset_001()
4076         {
4077             Directory testDirectory(aTmpName3); // constructor
4078 
4079             // open a directory
4080             auto nError1 = testDirectory.open();
4081             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4082             // get first Item
4083             nError1 = testDirectory.getNextItem(rItem, 1);
4084             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4085             // check the file name of first Item
4086             FileStatus   rFileStatusFirst(osl_FileStatus_Mask_FileName);
4087             nError1 = rItem.getFileStatus(rFileStatusFirst);
4088 
4089             // get second Item
4090             // mindy: nError1 = testDirectory.getNextItem(rItem, 0);
4091             // mindy: CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4092 
4093             // reset enumeration
4094             auto nError2 = testDirectory.reset();
4095             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
4096             // get reset Item, if reset does not work, getNextItem() should return the second Item (aTmpName1)
4097             nError1 = testDirectory.getNextItem(rItem);
4098             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4099 
4100             // check the file name again
4101             FileStatus   rFileStatus(osl_FileStatus_Mask_FileName);
4102             nError1 = rItem.getFileStatus(rFileStatus);
4103             // close a directory
4104             nError1 = testDirectory.close();
4105             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4106 
4107             bool bOK1,bOK2,bOK3;
4108             bOK1 = compareFileName(rFileStatus.getFileName(), aTmpName2);
4109             bOK2 = compareFileName(rFileStatus.getFileName(), aHidURL1);
4110             bOK3 = compareFileName(rFileStatus.getFileName(), rFileStatusFirst.getFileName());
4111             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4112                                     osl::FileBase::E_None, nError2);
4113             CPPUNIT_ASSERT_MESSAGE("test for reset function: get two directory item, reset it, then get again, check the filename",
4114                                     (bOK1 || bOK2 || bOK3));
4115         }
4116 
reset_002()4117         void reset_002()
4118         {
4119             Directory testDirectory(aTmpName6); // constructor
4120 
4121             // close a directory
4122             auto nError1 = testDirectory.reset();
4123 
4124             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: reset a non existed directory",
4125                                     osl::FileBase::E_NOENT, nError1);
4126         }
4127 
reset_003()4128         void reset_003()
4129         {
4130             Directory testDirectory(aTmpName4); // constructor
4131 
4132             // close a directory
4133             auto nError1 = testDirectory.reset();
4134 
4135             CPPUNIT_ASSERT_MESSAGE("test for reset function: reset a file instead of a directory",
4136                                     (osl::FileBase::E_NOTDIR == nError1) || (osl::FileBase::E_NOENT == nError1));
4137         }
4138 
reset_004()4139         void reset_004()
4140         {
4141             Directory testDirectory(aUserDirectorySys); // constructor
4142 
4143             // close a directory
4144             auto nError1 = testDirectory.reset();
4145 
4146             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: use a system path",
4147                                     osl::FileBase::E_INVAL, nError1);
4148         }
4149 
4150         CPPUNIT_TEST_SUITE(reset);
4151             CPPUNIT_TEST(reset_001);
4152             CPPUNIT_TEST(reset_002);
4153             CPPUNIT_TEST(reset_003);
4154             CPPUNIT_TEST(reset_004);
4155         CPPUNIT_TEST_SUITE_END();
4156     };
4157 
4158     //  testing the method
4159     //  inline RC getNextItem(DirectoryItem& rItem, sal_uInt32 nHint = 0)
4160 
4161     class getNextItem : public CppUnit::TestFixture
4162     {
4163     private:
4164         DirectoryItem rItem;
4165 
4166     public:
setUp()4167         void setUp() override
4168         {
4169             // create a tempdirectory : $TEMP/tmpdir.
4170             createTestDirectory(aTmpName3);
4171             // create three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4172             createTestFile(aTmpName3, aTmpName2);
4173             createTestFile(aTmpName3, aTmpName1);
4174             createTestFile(aTmpName3, aHidURL1);
4175 
4176         }
4177 
tearDown()4178         void tearDown() override
4179         {
4180             // remove three files : $TEMP/tmpdir/tmpname, $TEMP/tmpdir/tmpdir, $TEMP/tmpdir/hiddenfile,
4181             deleteTestFile(aTmpName3, aHidURL1);
4182             deleteTestFile(aTmpName3, aTmpName1);
4183             deleteTestFile(aTmpName3, aTmpName2);
4184             // remove a tempdirectory : $TEMP/tmpdir.
4185             deleteTestDirectory(aTmpName3);
4186         }
4187 
4188 
getNextItem_001()4189         void getNextItem_001()
4190         {
4191             Directory testDirectory(aTmpName3); // constructor
4192 
4193             // open a directory
4194             auto nError1 = testDirectory.open();
4195             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4196 
4197             // check the file name
4198             bool bOk1 = false;
4199             bool bOk2 = false;
4200             bool bOk3 = false;
4201             FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
4202 
4203             for (int nCount = 0; nCount < 3; nCount++)
4204             {
4205                 // get three Items
4206                 nError1 = testDirectory.getNextItem(rItem, 2);
4207                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4208                 nError1 = rItem.getFileStatus(rFileStatus);
4209                 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4210 
4211                 // a special order is not guaranteed. So any file may occur on any time.
4212                 // But every file name should occur only once.
4213                 if (!bOk1 && compareFileName(rFileStatus.getFileName(), aTmpName1))
4214                 {
4215                     bOk1 = true;
4216                 }
4217 
4218                 if (!bOk2 && compareFileName(rFileStatus.getFileName(), aTmpName2))
4219                 {
4220                     bOk2 = true;
4221                 }
4222 
4223                 if (!bOk3 && compareFileName(rFileStatus.getFileName(), aHidURL1))
4224                 {
4225                     bOk3 = true;
4226                 }
4227            }
4228 
4229             // close a directory
4230             nError1 = testDirectory.close();
4231             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4232 
4233             CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4234                                     bOk1);
4235             CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4236                                     bOk2);
4237             CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: retrieve three items and check their names.",
4238                                     bOk3);
4239         }
4240 
getNextItem_002()4241         void getNextItem_002()
4242         {
4243             Directory testDirectory(aTmpName3); // constructor
4244             auto nError1 = testDirectory.getNextItem(rItem);
4245 
4246             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.",
4247                                     osl::FileBase::E_INVAL, nError1);
4248         }
4249 
getNextItem_003()4250         void getNextItem_003()
4251         {
4252             Directory testDirectory(aTmpName3); // constructor
4253 
4254             // open a directory
4255             auto nError1 = testDirectory.open();
4256             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4257 
4258             osl::FileBase::RC nError2 = osl::FileBase::E_None;
4259             for (int nCount = 0; nCount < 4; nCount++)
4260             {
4261                 nError2 = testDirectory.getNextItem(rItem, 3);
4262             }
4263 
4264             // close a directory
4265             nError1 = testDirectory.close();
4266             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4267 
4268             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getNextItem function: retrieve 4 times in a directory which contain only 3 files.",
4269                                     osl::FileBase::E_NOENT, nError2);
4270         }
4271 
getNextItem_004()4272         void getNextItem_004()
4273         {
4274         // create a link file(can not on Windows), then check if getNextItem can get it.
4275 #ifdef UNX
4276             bool bLnkOK = false;
4277             bool bFoundOK = false;
4278 
4279             OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
4280             aUStr_LnkFileSys += aSlashURL + "/tmpdir/link.file";
4281             aUStr_SrcFileSys += aSlashURL + "/tmpdir/tmpname";
4282 
4283             OString strLinkFileName, strSrcFileName;
4284             strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
4285             strSrcFileName  = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
4286 
4287             // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
4288             sal_Int32 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
4289             CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
4290             Directory testDirectory(aTmpName3);
4291 
4292             // open a directory
4293             auto nError1 = testDirectory.open();
4294             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4295             OUString aFileName ("link.file");
4296 
4297             while (true) {
4298                 nError1 = testDirectory.getNextItem(rItem, 4);
4299                 if (nError1 == osl::FileBase::E_None) {
4300                     FileStatus   rFileStatus(osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_Type);
4301                     rItem.getFileStatus(rFileStatus);
4302                     if (compareFileName(rFileStatus.getFileName(), aFileName))
4303                     {
4304                         bFoundOK = true;
4305                         if (rFileStatus.getFileType() == FileStatus::Link)
4306                         {
4307                             bLnkOK = true;
4308                             break;
4309                         }
4310                     }
4311                 }
4312                 else
4313                     break;
4314             }
4315             fd = std::remove(strLinkFileName.getStr());
4316             CPPUNIT_ASSERT_EQUAL_MESSAGE("remove link file failed", static_cast<sal_Int32>(0), fd);
4317             CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if can retrieve the link file name",
4318                                     bFoundOK);
4319             CPPUNIT_ASSERT_MESSAGE("test for getNextItem function: check if link file has file type link",
4320                                     bLnkOK);
4321 #endif
4322         }
4323 
4324         CPPUNIT_TEST_SUITE(getNextItem);
4325             CPPUNIT_TEST(getNextItem_001);
4326             CPPUNIT_TEST(getNextItem_002);
4327             CPPUNIT_TEST(getNextItem_003);
4328             CPPUNIT_TEST(getNextItem_004);
4329         CPPUNIT_TEST_SUITE_END();
4330     };
4331 
4332     //  testing the method
4333     //  inline static RC getVolumeInfo(const OUString& ustrDirectoryURL, VolumeInfo& rInfo)
4334 
4335     class getVolumeInfo : public CppUnit::TestFixture
4336     {
4337     public:
checkValidMask(osl::VolumeInfo const & _aVolumeInfo,sal_Int32 _nMask)4338         void checkValidMask(osl::VolumeInfo const& _aVolumeInfo, sal_Int32 _nMask)
4339         {
4340             if (_nMask == osl_VolumeInfo_Mask_FileSystemName)
4341             {
4342                 // get file system name
4343                 OUString aFileSysName = _aVolumeInfo.getFileSystemName();
4344 
4345                 bool bRes2 = compareFileName(aFileSysName, aNullURL);
4346                 CPPUNIT_ASSERT_MESSAGE("test for getVolumeInfo function: getVolumeInfo of root directory.",
4347                                         !bRes2);
4348             }
4349 
4350             if (_nMask == osl_VolumeInfo_Mask_Attributes)
4351             {
4352                 bool b1 = _aVolumeInfo.getRemoteFlag();
4353                 bool b2 = _aVolumeInfo.getRemoveableFlag();
4354                 bool b3 = _aVolumeInfo.getCompactDiscFlag();
4355                 bool b4 = _aVolumeInfo.getFloppyDiskFlag();
4356                 bool b5 = _aVolumeInfo.getFixedDiskFlag();
4357                 bool b6 = _aVolumeInfo.getRAMDiskFlag();
4358 
4359                 OString sAttr;
4360                 if (b1) sAttr =  "Remote";
4361                 if (b2) sAttr += " Removeable";
4362                 if (b3) sAttr += " CDROM";
4363                 if (b4) sAttr += " Floppy";
4364                 if (b5) sAttr += " FixedDisk";
4365                 if (b6) sAttr += " RAMDisk";
4366 
4367                 printf("Attributes: %s\n", sAttr.getStr());
4368             }
4369             if (_nMask == osl_VolumeInfo_Mask_TotalSpace)
4370             {
4371                 // within Linux, df / * 1024 bytes is the result
4372                 sal_uInt64 nSize = _aVolumeInfo.getTotalSpace();
4373                 printf("Total space: %" SAL_PRIuUINT64 "\n", nSize);
4374             }
4375             if (_nMask == osl_VolumeInfo_Mask_UsedSpace)
4376             {
4377                 sal_uInt64 nSize = _aVolumeInfo.getUsedSpace();
4378                 printf(" Used space: %" SAL_PRIuUINT64 "\n", nSize);
4379             }
4380             if (_nMask == osl_VolumeInfo_Mask_FreeSpace)
4381             {
4382                 sal_uInt64 nSize = _aVolumeInfo.getFreeSpace();
4383                 printf(" Free space: %" SAL_PRIuUINT64 "\n", nSize);
4384             }
4385             if (_nMask == osl_VolumeInfo_Mask_MaxNameLength)
4386             {
4387                 sal_uInt32 nLength = _aVolumeInfo.getMaxNameLength();
4388                 printf("max name length: %" SAL_PRIuUINT32 "\n", nLength);
4389             }
4390             if (_nMask == osl_VolumeInfo_Mask_MaxPathLength)
4391             {
4392                 sal_uInt32 nLength = _aVolumeInfo.getMaxPathLength();
4393                 printf("max path length: %" SAL_PRIuUINT32 "\n", nLength);
4394             }
4395             if (_nMask == osl_VolumeInfo_Mask_FileSystemCaseHandling)
4396             {
4397                 bool bIsCase = _aVolumeInfo.isCaseSensitiveFileSystem();
4398                 printf("filesystem case sensitive: %s\n", bIsCase ? "yes" : "no");
4399             }
4400         }
4401 
checkVolumeInfo(sal_Int32 _nMask)4402         void checkVolumeInfo(sal_Int32 _nMask)
4403         {
4404             VolumeInfo aVolumeInfo(_nMask);
4405             // call getVolumeInfo here
4406             auto nError1 = Directory::getVolumeInfo(aVolURL1, aVolumeInfo);
4407             CPPUNIT_ASSERT_EQUAL_MESSAGE(
4408                 "test for getVolumeInfo function: getVolumeInfo of root directory.",
4409                 osl::FileBase::E_None, nError1);
4410             // LLA: IMHO it's not a bug, if VolumeInfo is not valid, it's a feature
4411             // LLA: CPPUNIT_ASSERT_MESSAGE("mask is not valid", sal_True == aVolumeInfo.isValid(_nMask));
4412             if (aVolumeInfo.isValid(_nMask))
4413                 checkValidMask(aVolumeInfo, _nMask);
4414         }
4415 
getVolumeInfo_001_1()4416         void getVolumeInfo_001_1()
4417         {
4418             sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4419             checkVolumeInfo(mask);
4420         }
4421 
getVolumeInfo_001_2()4422         void getVolumeInfo_001_2()
4423         {
4424             sal_Int32 mask = osl_VolumeInfo_Mask_Attributes;
4425             checkVolumeInfo(mask);
4426         }
4427 
getVolumeInfo_001_3()4428         void getVolumeInfo_001_3()
4429         {
4430             sal_Int32 mask = osl_VolumeInfo_Mask_TotalSpace;
4431             checkVolumeInfo(mask);
4432         }
4433 
getVolumeInfo_001_4()4434         void getVolumeInfo_001_4()
4435         {
4436             sal_Int32 mask = osl_VolumeInfo_Mask_UsedSpace;
4437             checkVolumeInfo(mask);
4438         }
4439 
getVolumeInfo_001_5()4440         void getVolumeInfo_001_5()
4441         {
4442             sal_Int32 mask = osl_VolumeInfo_Mask_FreeSpace;
4443             checkVolumeInfo(mask);
4444         }
4445 
getVolumeInfo_001_6()4446         void getVolumeInfo_001_6()
4447         {
4448             sal_Int32 mask = osl_VolumeInfo_Mask_MaxNameLength;
4449             checkVolumeInfo(mask);
4450         }
4451 
getVolumeInfo_001_7()4452         void getVolumeInfo_001_7()
4453         {
4454             sal_Int32 mask = osl_VolumeInfo_Mask_MaxPathLength;
4455             checkVolumeInfo(mask);
4456         }
4457 
getVolumeInfo_001_8()4458         void getVolumeInfo_001_8()
4459         {
4460             sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemCaseHandling;
4461             checkVolumeInfo(mask);
4462         }
4463 
getVolumeInfo_002()4464         void getVolumeInfo_002()
4465         {
4466             sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4467             VolumeInfo aVolumeInfo(mask);
4468             // call getVolumeInfo here
4469 
4470             OUString aRootSysURL;
4471             auto nError1 = osl::File::getFileURLFromSystemPath(aRootSys, aRootSysURL);
4472             CPPUNIT_ASSERT_EQUAL_MESSAGE("can't convert root path to file url", osl::FileBase::E_None, nError1);
4473 
4474             nError1 = Directory::getVolumeInfo(aRootSys, aVolumeInfo);
4475 
4476             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: use system path as parameter.",
4477                                     osl::FileBase::E_INVAL, nError1);
4478         }
4479 
getVolumeInfo_003()4480         void getVolumeInfo_003()
4481         {
4482 // LLA: in Windows, it reply no error, it did not pass in (W32).
4483 #if defined(UNX) && !defined(IOS)
4484             sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4485             VolumeInfo aVolumeInfo(mask);
4486             // call getVolumeInfo here
4487             auto nError1 = Directory::getVolumeInfo(aTmpName3, aVolumeInfo);
4488 
4489             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: non-existence test. ",
4490                                     osl::FileBase::E_NOENT, nError1);
4491 #endif
4492         }
4493 
4494         CPPUNIT_TEST_SUITE(getVolumeInfo);
4495             CPPUNIT_TEST(getVolumeInfo_001_1);
4496             CPPUNIT_TEST(getVolumeInfo_001_2);
4497             CPPUNIT_TEST(getVolumeInfo_001_3);
4498             CPPUNIT_TEST(getVolumeInfo_001_4);
4499             CPPUNIT_TEST(getVolumeInfo_001_5);
4500             CPPUNIT_TEST(getVolumeInfo_001_6);
4501             CPPUNIT_TEST(getVolumeInfo_001_7);
4502             CPPUNIT_TEST(getVolumeInfo_001_8);
4503             CPPUNIT_TEST(getVolumeInfo_002);
4504             CPPUNIT_TEST(getVolumeInfo_003);
4505         CPPUNIT_TEST_SUITE_END();
4506     };
4507 
4508     //  testing the method
4509     //  inline static RC create(const OUString& ustrDirectoryURL)
4510 
4511     class create : public CppUnit::TestFixture
4512     {
4513     public:
create_001()4514         void create_001()
4515         {
4516             // create directory in $TEMP/tmpdir
4517             auto nError1 = Directory::create(aTmpName3);
4518             // check for existence
4519             auto nError2 = Directory::create(aTmpName3);
4520             // remove it
4521             deleteTestDirectory(aTmpName3);
4522 
4523             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4524                                     osl::FileBase::E_None, nError1);
4525             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory and check its existence.",
4526                                     osl::FileBase::E_EXIST, nError2);
4527         }
4528 
create_002()4529         void create_002()
4530         {
4531 #if !defined(_WIN32) && !defined(MACOSX) && defined(SAL_UNX)
4532             if (geteuid() == 0) // don't test if building as root
4533                 return;
4534 
4535             OUString aTmpDir;
4536             auto nError1 = osl::FileBase::createTempFile(nullptr, nullptr, &aTmpDir);
4537             CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File creation failed", osl::FileBase::E_None, nError1);
4538 
4539             nError1 = File::remove(aTmpDir);
4540             CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File removal failed", osl::FileBase::E_None, nError1);
4541 
4542             nError1 = Directory::create(aTmpDir);
4543             OString sError = "test for create function: create a directory '" +
4544                 OUStringToOString(aTmpDir, RTL_TEXTENCODING_ASCII_US) +
4545                 "' and check its existence.";
4546             CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError1);
4547             osl_setFileAttributes(aTmpDir.pData, 0); // no access allowed now
4548 
4549             // Shouldn't be possible now to create a dir underneath it
4550             OUString aTmpSubLevel = aTmpDir + "/notallowedhere";
4551             nError1 = Directory::create(aTmpSubLevel);
4552 
4553             // allow removal
4554             osl_setFileAttributes(aTmpDir.pData,
4555                 osl_File_Attribute_OwnRead |
4556                 osl_File_Attribute_OwnWrite |
4557                 osl_File_Attribute_OwnExe);
4558             deleteTestDirectory(aTmpDir);
4559             sError = "test for create function: create a directory under '" +
4560                 OUStringToOString(aTmpDir, RTL_TEXTENCODING_ASCII_US) +
4561                 "' for access test.";
4562             CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_ACCES, nError1);
4563 #endif
4564         }
4565 
create_003()4566         void create_003()
4567         {
4568             // create directory in /tmpname
4569             auto nError1 = Directory::create(aSysPath1);
4570 
4571             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory using system path.",
4572                                     osl::FileBase::E_INVAL, nError1);
4573         }
4574 
4575         CPPUNIT_TEST_SUITE(create);
4576             CPPUNIT_TEST(create_001);
4577             CPPUNIT_TEST(create_002);
4578             CPPUNIT_TEST(create_003);
4579         CPPUNIT_TEST_SUITE_END();
4580     };
4581 
4582     //  testing the method
4583     //  inline static RC remove(const OUString& ustrDirectoryURL)
4584 
4585     class remove : public CppUnit::TestFixture
4586     {
4587     public:
remove_001()4588         void remove_001()
4589         {
4590             // create directory in $TEMP/tmpdir
4591             auto nError1 = Directory::create(aTmpName3);
4592             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4593             // remove it
4594             nError1 = Directory::remove(aTmpName3);
4595             // check for existence
4596             Directory rDirectory(aTmpName3);
4597             auto nError2 = rDirectory.open();
4598 
4599             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4600                                     osl::FileBase::E_None, nError1);
4601             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory and check its existence.",
4602                                     osl::FileBase::E_NOENT, nError2);
4603         }
4604 
remove_002()4605         void remove_002()
4606         {
4607             // create directory in $TEMP/tmpdir
4608             auto nError1 = Directory::create(aTmpName3);
4609             CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4610             // try to remove it by system path
4611             nError1 = Directory::remove(aSysPath3);
4612              // check for existence
4613             Directory rDirectory(aTmpName3);
4614             auto nError2 = rDirectory.open();
4615 
4616             if (nError2 != osl::FileBase::E_NOENT)
4617                 Directory::remove(aTmpName3);
4618 
4619             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a directory by its system path, and check its existence.",
4620                                     osl::FileBase::E_INVAL, nError1);
4621         }
4622 
remove_003()4623         void remove_003()
4624         {
4625             // try to remove a non-existed directory
4626             auto nError1 = Directory::remove(aTmpName6);
4627 
4628             CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: try to remove a non-existed directory.",
4629                                     osl::FileBase::E_NOENT, nError1);
4630         }
4631 
remove_004()4632         void remove_004()
4633         {
4634             createTestFile(aTmpName6);
4635             bool bExist = ifFileExist(aTmpName6);
4636             // try to remove file.
4637             auto nError1 = Directory::remove(aTmpName6);
4638             deleteTestFile(aTmpName6);
4639 
4640             CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4641                                     bExist);
4642             CPPUNIT_ASSERT_MESSAGE("test for remove function: try to remove a file but not directory.",
4643                                     (osl::FileBase::E_NOTDIR == nError1) || (osl::FileBase::E_NOENT == nError1));
4644         }
4645 
remove_005()4646         void remove_005()
4647         {
4648             createTestDirectory(aTmpName3);
4649             createTestFile(aTmpName4);
4650             auto nError1 = Directory::remove(aTmpName3);
4651             deleteTestFile(aTmpName4);
4652             deleteTestDirectory(aTmpName3);
4653             OString sError = "test for remove function: try to remove a directory that is not empty." +
4654                 errorToStr(nError1);
4655 #if defined(__sun)
4656             // 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.
4657             // EEXIST The directory contains entries other than those for "." and "..".
4658             printf("#Solaris test\n");
4659             CPPUNIT_ASSERT_MESSAGE(sError.getStr(), (osl::FileBase::E_EXIST == nError1));
4660 #else
4661             CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_NOTEMPTY, nError1);
4662 #endif
4663         }
4664 
4665         CPPUNIT_TEST_SUITE(remove);
4666             CPPUNIT_TEST(remove_001);
4667             CPPUNIT_TEST(remove_002);
4668             CPPUNIT_TEST(remove_003);
4669             CPPUNIT_TEST(remove_004);
4670             CPPUNIT_TEST(remove_005);
4671         CPPUNIT_TEST_SUITE_END();
4672     };
4673 
4674     // TEST Directory::createPath
4675 
4676     #ifdef _WIN32
4677     #   define PATH_BUFFER_SIZE MAX_PATH
4678     #else
4679     #   define PATH_BUFFER_SIZE PATH_MAX
4680     #endif
4681 
4682 #define TEST_PATH_POSTFIX "hello/world"
4683 
get_test_path()4684     static OUString const & get_test_path()
4685     {
4686         static OUString test_path = [&]()
4687         {
4688             OUString tmp;
4689             osl::FileBase::RC rc = osl::FileBase::getTempDirURL(tmp);
4690 
4691             CPPUNIT_ASSERT_EQUAL_MESSAGE
4692             (
4693              "Getting the location of TMP dir failed",
4694              osl::FileBase::E_None, rc
4695           );
4696 
4697             OUString system_path;
4698             rc = osl::FileBase::getSystemPathFromFileURL(tmp, system_path);
4699 
4700             CPPUNIT_ASSERT_EQUAL_MESSAGE
4701             (
4702              "Cannot convert the TMP dir to system path",
4703              osl::FileBase::E_None, rc
4704           );
4705 
4706             OString tmp_x(OUStringToOString(system_path, RTL_TEXTENCODING_UTF8));
4707             if (tmp_x.lastIndexOf('/') != (tmp_x.getLength() - 1))
4708                 tmp_x += "/";
4709 
4710 #if !defined(_WIN32) && !defined(ANDROID) && !defined(AIX)
4711             // FIXME would be nice to create unique dir even on Windows
4712             tmp_x += "XXXXXX";
4713             char *out = mkdtemp(const_cast<char*>(tmp_x.getStr()));
4714 
4715             CPPUNIT_ASSERT_MESSAGE
4716             (
4717              "mkdtemp call failed",
4718              out != nullptr
4719           );
4720 
4721             tmp_x += "/";
4722 #endif
4723             tmp_x += TEST_PATH_POSTFIX;
4724 
4725             OUString tmpTestPath;
4726             rc = osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x, RTL_TEXTENCODING_UTF8), tmpTestPath);
4727 
4728             CPPUNIT_ASSERT_EQUAL_MESSAGE
4729             (
4730              "Cannot convert the system path back to a URL",
4731              osl::FileBase::E_None, rc
4732             );
4733             return tmpTestPath;
4734         }();
4735         return test_path;
4736     }
4737 
rm_test_path(const OUString & path)4738     static void rm_test_path(const OUString& path)
4739     {
4740         sal_Unicode buffer[PATH_BUFFER_SIZE];
4741         memcpy(buffer, path.getStr(), (path.getLength() + 1) * sizeof(sal_Unicode));
4742 
4743         sal_Int32 i = rtl_ustr_lastIndexOfChar(buffer, '/');
4744         if (i == path.getLength())
4745             buffer[i] = 0;
4746 
4747         Directory::remove(OUString(buffer));
4748 
4749         i = rtl_ustr_lastIndexOfChar(buffer, '/');
4750         assert(i != -1);
4751         if (i != -1)
4752         {
4753             buffer[i] = 0;
4754             Directory::remove(OUString(buffer));
4755         }
4756     }
4757 
4758     namespace {
4759 
4760     class DirCreatedObserver : public DirectoryCreationObserver
4761     {
4762     public:
DirCreatedObserver()4763         DirCreatedObserver() : i(0) {}
DirectoryCreated(const OUString &)4764         virtual void DirectoryCreated(const OUString&) override { i++; };
4765 
number_of_dirs_created() const4766         int number_of_dirs_created() const { return i; }
4767 
4768     private:
4769         int i;
4770     };
4771 
4772     }
4773 
4774     class createPath : public CppUnit::TestFixture
4775     {
4776     public:
createPath()4777         createPath()
4778         {}
4779 
with_relative_path()4780         void with_relative_path()
4781         {
4782             osl::FileBase::RC rc = Directory::createPath(TEST_PATH_POSTFIX);
4783 
4784             CPPUNIT_ASSERT_EQUAL_MESSAGE
4785             (
4786                 "osl_createDirectoryPath contract broken",
4787                 osl::FileBase::E_INVAL, rc
4788           );
4789         }
4790 
without_callback()4791         void without_callback()
4792         {
4793             OUString tp_url = get_test_path();
4794 
4795             rm_test_path(tp_url);
4796 
4797             osl::FileBase::RC rc = Directory::createPath(tp_url);
4798 
4799             rm_test_path(tp_url);
4800 
4801             CPPUNIT_ASSERT_EQUAL_MESSAGE
4802             (
4803                 "osl_createDirectoryPath failed",
4804                 osl::FileBase::E_None, rc
4805           );
4806         }
4807 
with_callback()4808         void with_callback()
4809         {
4810             OUString tp_url = get_test_path();
4811 
4812             rm_test_path(tp_url);
4813 
4814             DirCreatedObserver* observer = new DirCreatedObserver;
4815             osl::FileBase::RC rc = Directory::createPath(tp_url, observer);
4816             int nDirs = observer->number_of_dirs_created();
4817             delete observer;
4818 
4819             rm_test_path(tp_url);
4820 
4821             CPPUNIT_ASSERT_EQUAL_MESSAGE
4822             (
4823                 "osl_createDirectoryPath failed",
4824                 osl::FileBase::E_None, rc
4825           );
4826             CPPUNIT_ASSERT_MESSAGE
4827             (
4828                 "osl_createDirectoryPath failed",
4829                 nDirs > 0
4830           );
4831 
4832         }
4833 
4834 #ifdef _WIN32
4835 
get_unused_drive_letter()4836         const char* get_unused_drive_letter()
4837         {
4838             static const char m_aBuff[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4839 
4840             DWORD ld = GetLogicalDrives();
4841             DWORD i = 4;
4842             DWORD j = 2;
4843 
4844             while ((ld & i) && (i > 1))
4845             { i = i << 1; j++; }
4846 
4847             if (i > 2)
4848                 return m_aBuff + j;
4849 
4850             return nullptr;
4851         }
4852 
at_invalid_logical_drive()4853         void at_invalid_logical_drive()
4854         {
4855             const char* drv = get_unused_drive_letter();
4856             char buff[PATH_BUFFER_SIZE];
4857             memset(buff, 0, sizeof(buff));
4858 
4859             strncpy(buff, drv, 1);
4860             strcat(buff, ":\\");
4861             strcat(buff, TEST_PATH_POSTFIX);
4862 
4863             OUString path = OUString::createFromAscii(buff);
4864             OUString tp_url;
4865             osl::FileBase::getFileURLFromSystemPath(path, tp_url);
4866 
4867             osl::FileBase::RC rc = Directory::createPath(tp_url);
4868 
4869             CPPUNIT_ASSERT_MESSAGE
4870             (
4871                 "osl_createDirectoryPath doesn't fail on unused logical drive letters",
4872                 rc != osl::FileBase::E_None
4873           );
4874         }
4875 #endif /* _WIN32 */
4876 
4877     CPPUNIT_TEST_SUITE(createPath);
4878         CPPUNIT_TEST(with_relative_path);
4879         CPPUNIT_TEST(without_callback);
4880         CPPUNIT_TEST(with_callback);
4881 #ifdef _WIN32
4882         CPPUNIT_TEST(at_invalid_logical_drive);
4883 #endif
4884     CPPUNIT_TEST_SUITE_END();
4885 
4886     };
4887 
4888     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::ctors);
4889     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::open);
4890     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::isOpen);
4891     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::close);
4892     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::reset);
4893     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getNextItem);
4894     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::getVolumeInfo);
4895     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::create);
4896     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::remove);
4897     CPPUNIT_TEST_SUITE_REGISTRATION(osl_Directory::createPath);
4898 }
4899 
4900 #if 0
4901 #if defined UNX
4902 /** get Current PID.
4903 */
4904 OUString getCurrentPID()
4905 {
4906     //~ Get current PID and turn it into OUString;
4907     int nPID = 0;
4908 #ifdef _WIN32
4909     nPID = GetCurrentProcessId();
4910 #else
4911     nPID = getpid();
4912 #endif
4913     return OUString::number(nPID);
4914 }
4915 #endif
4916 #endif
4917 
4918 namespace {
4919 
4920 //~ do some clean up work after all test completed.
4921 class GlobalObject
4922 {
4923 public:
~GlobalObject()4924     ~GlobalObject()
4925     {
4926         try
4927         {
4928             //~ special clean up task in Windows and Unix separately;
4929 #if (defined UNX)
4930             //~ some clean up task  for UNIX OS
4931             ;
4932 #else
4933             //~ some clean up task  for Windows OS
4934             //~ check if some files are in the way, remove them if necessary.
4935             if (ifFileExist(aTmpName6))
4936                 deleteTestFile(aTmpName6);
4937             if (ifFileExist(aTmpName4))
4938                 deleteTestFile(aTmpName4);
4939             if (checkDirectory(aTmpName4, oslCheckMode::Exist))
4940                 deleteTestDirectory(aTmpName4);
4941             if (ifFileExist(aTmpName3))
4942                 deleteTestFile(aTmpName3);
4943             if (checkDirectory(aTmpName3, oslCheckMode::Exist))
4944                 deleteTestDirectory(aTmpName3);
4945 
4946             OUString aUStr(aUserDirectoryURL);
4947             concatURL(aUStr, aHidURL1);
4948             if (ifFileExist(aUStr))
4949                 deleteTestFile(aUStr);
4950 
4951             OUString aUStr1(aRootURL);
4952             concatURL(aUStr1, aTmpName2);
4953             if (ifFileExist(aUStr1))
4954                 deleteTestFile(aUStr1);
4955 #endif
4956         }
4957         catch (const CppUnit::Exception &e)
4958         {
4959             printf("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e.what(), e.sourceLine().lineNumber());
4960         }
4961         catch (...)
4962         {
4963             printf("Exception caught (...) in GlobalObject dtor()\n");
4964         }
4965     }
4966 };
4967 
4968 }
4969 
4970 static GlobalObject theGlobalObject;
4971 
4972 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
4973