LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sal / qa / osl / file / osl_File.cxx
blob232bd87693f20f96b43d7ca4a0d92be91ed77cb0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/types.h>
21 #include <rtl/byteseq.hxx>
22 #include <rtl/ustring.hxx>
23 #include <rtl/ustrbuf.hxx>
25 #include <osl/thread.h>
26 #include <osl/file.hxx>
27 #include "osl_File_Const.h"
29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/plugin/TestPlugIn.h>
33 #include <tools/urlobj.hxx>
35 #include <memory>
37 #ifdef _WIN32
38 #include <prewin.h>
39 #include <postwin.h>
40 #include <o3tl/char16_t2wchar_t.hxx>
41 #endif
43 using namespace osl;
45 /** detailed wrong message.
47 static OString errorToString(const osl::FileBase::RC _nError)
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;
85 return sResult;
88 static OString errorToStr(osl::FileBase::RC const& nError)
90 OString suBuf = "The returned error is: " +
91 errorToString(nError) +
92 "!\n";
93 return suBuf;
96 /** compare two TimeValue, unit is "ms", since Windows time precision is better than UNX.
98 /* FIXME: the above assertion is bogus */
100 #if (defined UNX) // precision of time in Windows is better than UNX
101 # define delta 2000 // time precision, 2000ms
102 #else
103 # define delta 1800 // time precision, 1.8s
104 #endif
106 static bool t_compareTime(TimeValue *m_aEndTime, TimeValue *m_aStartTime, sal_Int32 nDelta)
108 sal_Int32 nDeltaSeconds = m_aEndTime->Seconds - m_aStartTime->Seconds;
109 sal_Int32 nDeltaNanoSec = sal_Int32(m_aEndTime->Nanosec) - sal_Int32(m_aStartTime->Nanosec);
110 if (nDeltaNanoSec < 0)
112 nDeltaNanoSec = 1000000000 + nDeltaNanoSec;
113 nDeltaSeconds--;
116 sal_Int32 nDeltaMilliSec = (nDeltaSeconds * 1000) + (nDeltaNanoSec / 1000000);
117 return (nDeltaMilliSec < nDelta);
120 /** compare two OUString file name.
122 static bool compareFileName(const OUString & ustr1, const OUString & ustr2)
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 = '\\';
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;
145 /** simple version to judge if a file name or directory name is a URL or a system path, just to see if it
146 is start with "file:///";.
148 static bool isURL(const OUString& pathname)
150 return pathname.startsWith(aPreURL);
153 /** concat two part to form a URL or system path, add PATH_SEPARATOR between them if necessary, add "file:///" to beginning if necessary.
155 static void concatURL(OUString & pathname1, const OUString & pathname2)
157 // check if pathname1 is full qualified URL;
158 if (!isURL(pathname1))
160 OUString aPathName = pathname1.copy(0);
161 osl::FileBase::getFileURLFromSystemPath(pathname1, aPathName); // convert if not full qualified URL
162 pathname1 = aPathName.copy(0);
165 // check if '/' is in the end of pathname1 or at the begin of pathname2;
166 if (!pathname1.endsWith(aSlashURL) && !pathname2.startsWith(aSlashURL))
167 pathname1 += aSlashURL;
168 pathname1 += pathname2;
171 /** create a temp test file using OUString name of full qualified URL or system path.
173 static void createTestFile(const OUString& filename)
175 OUString aPathURL = filename.copy(0);
176 osl::FileBase::RC nError;
178 if (!isURL(filename))
179 osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
181 File aFile(aPathURL);
182 nError = aFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
183 if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST))
184 printf("createTestFile failed!\n");
186 aFile.close();
190 /** create a temp test file using OUString name of full qualified URL or system path in a base directory.
192 static void createTestFile(const OUString& basename, const OUString& filename)
194 OUString aBaseURL = basename.copy(0);
196 concatURL(aBaseURL, filename);
197 createTestFile(aBaseURL);
200 /** delete a temp test file using OUString name.
202 static void deleteTestFile(const OUString& filename)
204 OUString aPathURL = filename.copy(0);
205 osl::FileBase::RC nError;
207 if (!isURL(filename))
208 osl::FileBase::getFileURLFromSystemPath(filename, aPathURL); // convert if not full qualified URL
210 nError = File::setAttributes(aPathURL, osl_File_Attribute_GrpWrite| osl_File_Attribute_OwnWrite| osl_File_Attribute_OthWrite); // if readonly, make writable.
211 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: set writable ", (osl::FileBase::E_None == nError) || (osl::FileBase::E_NOENT == nError));
213 nError = File::remove(aPathURL);
214 CPPUNIT_ASSERT_MESSAGE("In deleteTestFile Function: remove ", (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT));
217 /** delete a temp test file using OUString name of full qualified URL or system path in a base directory.
219 static void deleteTestFile(const OUString& basename, const OUString& filename)
221 OUString aBaseURL = basename.copy(0);
223 concatURL(aBaseURL, filename);
224 deleteTestFile(aBaseURL);
227 /** create a temp test directory using OUString name of full qualified URL or system path.
229 static void createTestDirectory(const OUString& dirname)
231 OUString aPathURL = dirname.copy(0);
232 osl::FileBase::RC nError;
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));
241 /** create a temp test directory using OUString name of full qualified URL or system path in a base directory.
243 static void createTestDirectory(const OUString& basename, const OUString& dirname)
245 OUString aBaseURL = basename.copy(0);
247 concatURL(aBaseURL, dirname);
248 createTestDirectory(aBaseURL);
251 /** delete a temp test directory using OUString name of full qualified URL or system path.
253 static void deleteTestDirectory(const OUString& dirname)
255 OUString aPathURL = dirname.copy(0);
256 if (!isURL(dirname))
257 osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL
259 Directory testDir(aPathURL);
260 if (testDir.isOpen())
261 testDir.close(); // close if still open.
263 osl::FileBase::RC nError = Directory::remove(aPathURL);
265 OString strError = "In deleteTestDirectory function: remove Directory " +
266 OUStringToOString(aPathURL, RTL_TEXTENCODING_ASCII_US) + " -> result: " + OString::number(nError);
267 CPPUNIT_ASSERT_MESSAGE(strError.getStr(), (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT));
270 /** delete a temp test directory using OUString name of full qualified URL or system path in a base directory.
272 static void deleteTestDirectory(const OUString& basename, const OUString& dirname)
274 OUString aBaseURL = basename.copy(0);
276 concatURL(aBaseURL, dirname);
277 deleteTestDirectory(aBaseURL);
280 namespace {
282 /** Check for the file and directory access right.
284 enum class oslCheckMode {
285 Exist,
286 OpenAccess,
287 ReadAccess,
288 WriteAccess
293 /** check if the file exist
295 static bool ifFileExist(const OUString & str)
297 File testFile(str);
298 return (testFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None);
301 /** check if the file can be written
303 static bool ifFileCanWrite(const OUString & str)
305 // on Windows, the file has no write right, but can be written
306 #ifdef _WIN32
307 bool bCheckResult = false;
308 OUString aUStr = str.copy(0);
309 if (isURL(str))
310 osl::FileBase::getSystemPathFromFileURL(str, aUStr);
312 OString aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
313 const char *path = aString.getStr();
314 if ((_access(path, 2)) != -1)
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;
324 static bool checkDirectory(const OUString& str, oslCheckMode nCheckMode)
326 OUString aUString;
327 DirectoryItem rItem;
328 osl::FileBase::RC rc;
329 bool bCheckResult= false;
331 Directory aDir(str);
332 rc = aDir.open();
334 if ((rc != osl::FileBase::E_NOENT) && (rc != osl::FileBase::E_ACCES))
336 switch (nCheckMode)
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)
354 bCheckResult = true;
355 rc = Directory::remove(aUString);
356 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
358 else
360 bCheckResult = false;
362 break;
364 default:
365 bCheckResult = false;
368 rc = aDir.close();
369 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, rc);
372 return bCheckResult;
375 /** construct error message
377 static OString outputError(const OString & returnVal, const OString & rightVal, const char * msg = "")
379 if (returnVal == rightVal)
380 return OString();
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;
391 #if (defined UNX) /* chmod() method is different in Windows */
392 /** Change file mode, two version in UNIX and Windows;.
394 static void changeFileMode(OUString & filepath, sal_Int32 mode)
396 OString aString;
397 OUString aUStr = filepath.copy(0);
399 if (isURL(filepath))
400 osl::FileBase::getSystemPathFromFileURL(filepath, aUStr);
402 aString = OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
403 int ret = chmod(aString.getStr(), mode);
404 CPPUNIT_ASSERT_EQUAL(0, ret);
406 #else
407 static void hideFile(const OUString& filepath)
409 OUString aSysPath(filepath);
411 if (isURL(filepath))
412 osl::FileBase::getSystemPathFromFileURL(filepath, aSysPath);
414 bool ret = SetFileAttributesW(o3tl::toW(aSysPath.getStr()), FILE_ATTRIBUTE_HIDDEN);
415 CPPUNIT_ASSERT(ret);
417 #endif
419 #if 0
420 #if defined UNX
421 static OUString getCurrentPID();
422 #endif
423 #endif
425 // Beginning of the test cases for osl::FileBase class
427 namespace osl_FileBase
429 // testing the method
430 // static inline RC getAbsoluteFileURL(const OUString& ustrBaseDirectoryURL,
431 // const OUString& ustrRelativeFileURL,
432 // OUString& ustrAbsoluteFileURL)
434 class getAbsoluteFileURL : public CppUnit::TestFixture
436 public:
437 void check_getAbsoluteFileURL(OUString const& _suBaseURL,
438 OString const& _sRelativeURL,
439 osl::FileBase::RC _nAssumeError,
440 OUString const& _suAssumeResultStr);
442 void getAbsoluteFileURL_001_1();
443 void getAbsoluteFileURL_001_2();
444 void getAbsoluteFileURL_001_3();
445 void getAbsoluteFileURL_001_4();
446 void getAbsoluteFileURL_001_5();
447 void getAbsoluteFileURL_001_6();
448 void getAbsoluteFileURL_001_7();
449 void getAbsoluteFileURL_001_8();
450 void getAbsoluteFileURL_002();
451 void getAbsoluteFileURL_003();
452 void getAbsoluteFileURL_004();
454 CPPUNIT_TEST_SUITE(getAbsoluteFileURL);
455 CPPUNIT_TEST(getAbsoluteFileURL_001_1);
456 CPPUNIT_TEST(getAbsoluteFileURL_001_2);
457 CPPUNIT_TEST(getAbsoluteFileURL_001_3);
458 CPPUNIT_TEST(getAbsoluteFileURL_001_4);
459 CPPUNIT_TEST(getAbsoluteFileURL_001_5);
460 CPPUNIT_TEST(getAbsoluteFileURL_001_6);
461 CPPUNIT_TEST(getAbsoluteFileURL_001_7);
462 CPPUNIT_TEST(getAbsoluteFileURL_001_8);
463 CPPUNIT_TEST(getAbsoluteFileURL_002);
464 CPPUNIT_TEST(getAbsoluteFileURL_003);
465 CPPUNIT_TEST(getAbsoluteFileURL_004);
466 CPPUNIT_TEST_SUITE_END();
469 void getAbsoluteFileURL::check_getAbsoluteFileURL(OUString const& _suBaseURL,
470 OString const& _sRelativeURL,
471 osl::FileBase::RC _nAssumeError,
472 OUString const& _suAssumeResultStr)
474 OUString suRelativeURL = OStringToOUString(_sRelativeURL, RTL_TEXTENCODING_UTF8);
475 OString sBaseURL = OUStringToOString(_suBaseURL, RTL_TEXTENCODING_UTF8);
476 OUString suResultURL;
477 osl::FileBase::RC nError = osl::FileBase::getAbsoluteFileURL(_suBaseURL, suRelativeURL, suResultURL);
478 OString sResultURL = OUStringToOString(suResultURL, RTL_TEXTENCODING_UTF8);
479 OString sError = errorToString(nError);
480 printf("getAbsoluteFileURL('%s','%s') deliver absolute URL: '%s', error '%s'\n",
481 sBaseURL.getStr(), _sRelativeURL.getStr(),sResultURL.getStr(), sError.getStr());
482 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: error number is wrong", _nAssumeError, nError);
484 if (nError == osl::FileBase::E_None)
486 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong: ResultURL is not equal to expected URL ", _suAssumeResultStr, suResultURL);
490 void getAbsoluteFileURL::getAbsoluteFileURL_001_1()
492 OUString suAssume = aUserDirectoryURL + "/relative/file1";
493 check_getAbsoluteFileURL(aUserDirectoryURL, "relative/file1",osl::FileBase::E_None, suAssume);
496 void getAbsoluteFileURL::getAbsoluteFileURL_001_2()
498 OUString suAssume = aUserDirectoryURL + "/relative/file2";
499 check_getAbsoluteFileURL(aUserDirectoryURL, "relative/./file2",osl::FileBase::E_None, suAssume);
502 void getAbsoluteFileURL::getAbsoluteFileURL_001_3()
504 OUString suAssume = aUserDirectoryURL + "/file3";
505 check_getAbsoluteFileURL(aUserDirectoryURL, "relative/../file3",osl::FileBase::E_None, suAssume);
508 void getAbsoluteFileURL::getAbsoluteFileURL_001_4()
510 OUString suAssume = aUserDirectoryURL + "/file4";
511 check_getAbsoluteFileURL(aUserDirectoryURL, "././relative/../file4",osl::FileBase::E_None, suAssume);
514 void getAbsoluteFileURL::getAbsoluteFileURL_001_5()
516 OUString suAssume;
517 suAssume = aUserDirectoryURL + "/relative/";
518 check_getAbsoluteFileURL(aUserDirectoryURL, "././relative/.",osl::FileBase::E_None, suAssume);
521 void getAbsoluteFileURL::getAbsoluteFileURL_001_6()
523 OUString suAssume = aUserDirectoryURL + "/.relative";
524 check_getAbsoluteFileURL(aUserDirectoryURL, "./.relative",osl::FileBase::E_None, suAssume);
527 void getAbsoluteFileURL::getAbsoluteFileURL_001_7()
529 OUString suAssume;
530 suAssume = aUserDirectoryURL + "/.a/";
531 check_getAbsoluteFileURL(aUserDirectoryURL, "./.a/mydir/..",osl::FileBase::E_None, suAssume);
534 void getAbsoluteFileURL::getAbsoluteFileURL_001_8()
536 OUString suAssume = aUserDirectoryURL + "/tmp/ok";
537 check_getAbsoluteFileURL(aUserDirectoryURL, "tmp//ok",osl::FileBase::E_None, suAssume);
540 void getAbsoluteFileURL::getAbsoluteFileURL_002()
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";
548 OString strLinkFileName, strSrcFileName;
549 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
550 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
552 createTestFile(aCanURL1);
553 sal_Int32 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
554 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
555 OString sLnkURL = OUStringToOString(aLnkURL1, RTL_TEXTENCODING_ASCII_US);
556 OUString suAssume = aUserDirectoryURL + "/canonical.name";
557 check_getAbsoluteFileURL(aUserDirectoryURL, sLnkURL, osl::FileBase::E_None, suAssume);
558 deleteTestFile(aCanURL1);
559 fd = remove(strLinkFileName.getStr());
560 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
561 #endif
562 #endif
565 // please see line# 930
566 void getAbsoluteFileURL::getAbsoluteFileURL_003()
570 void getAbsoluteFileURL::getAbsoluteFileURL_004()
572 // create two level directories under $Temp/PID/
573 OUString aUStrUpBase = aUserDirectoryURL + "/test1";
574 createTestDirectory(aUStrUpBase);
575 OUString aUStrBase = aUserDirectoryURL + "/test1/dir1";
576 createTestDirectory(aUStrBase);
578 OUString suAssume = aUserDirectoryURL + "/mytestfile";
579 check_getAbsoluteFileURL(aUStrBase, "../../mytestfile" , osl::FileBase::E_None, suAssume);
580 deleteTestDirectory(aUStrBase);
581 deleteTestDirectory(aUStrUpBase);
584 // testing two methods:
585 // static inline RC getSystemPathFromFileURL(const OUString& ustrFileURL,
586 // OUString& ustrSystemPath)
587 // static RC getFileURLFromSystemPath(const OUString & ustrSystemPath,
588 // OUString & ustrFileURL);
590 class SystemPath_FileURL : public CppUnit::TestFixture
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();
617 // test case for getFileURLFromSystemPath
618 void getFileURLFromSystemPath_001();
619 void getFileURLFromSystemPath_002();
620 void getFileURLFromSystemPath_003();
621 void getFileURLFromSystemPath_004();
622 void getFileURLFromSystemPath_004_1();
623 void getFileURLFromSystemPath_005();
625 CPPUNIT_TEST_SUITE(SystemPath_FileURL);
626 CPPUNIT_TEST(getSystemPathFromFileURL_001_1);
627 CPPUNIT_TEST(getSystemPathFromFileURL_001_2);
628 CPPUNIT_TEST(getSystemPathFromFileURL_001_21);
629 CPPUNIT_TEST(getSystemPathFromFileURL_001_22);
630 CPPUNIT_TEST(getSystemPathFromFileURL_001_3);
631 CPPUNIT_TEST(getSystemPathFromFileURL_001_31);
632 CPPUNIT_TEST(getSystemPathFromFileURL_001_4);
633 CPPUNIT_TEST(getSystemPathFromFileURL_001_41);
634 CPPUNIT_TEST(getSystemPathFromFileURL_001_5);
635 CPPUNIT_TEST(getSystemPathFromFileURL_001_51);
636 CPPUNIT_TEST(getSystemPathFromFileURL_001_52);
637 CPPUNIT_TEST(getSystemPathFromFileURL_001_53);
638 CPPUNIT_TEST(getSystemPathFromFileURL_001_6);
639 CPPUNIT_TEST(getSystemPathFromFileURL_001_61);
640 CPPUNIT_TEST(getSystemPathFromFileURL_001_7);
641 CPPUNIT_TEST(getSystemPathFromFileURL_001_71);
642 CPPUNIT_TEST(getSystemPathFromFileURL_001_8);
643 CPPUNIT_TEST(getSystemPathFromFileURL_001_81);
644 CPPUNIT_TEST(getSystemPathFromFileURL_001_9);
645 CPPUNIT_TEST(getSystemPathFromFileURL_001_91);
646 CPPUNIT_TEST(getSystemPathFromFileURL_001_92);
647 CPPUNIT_TEST(getSystemPathFromFileURL_004);
648 CPPUNIT_TEST(getSystemPathFromFileURL_005);
649 CPPUNIT_TEST(getFileURLFromSystemPath_001);
650 CPPUNIT_TEST(getFileURLFromSystemPath_002);
651 CPPUNIT_TEST(getFileURLFromSystemPath_003);
652 CPPUNIT_TEST(getFileURLFromSystemPath_004);
653 CPPUNIT_TEST(getFileURLFromSystemPath_004_1);
654 CPPUNIT_TEST(getFileURLFromSystemPath_005);
655 CPPUNIT_TEST_SUITE_END();
657 private:
658 void check_SystemPath_FileURL(
659 OString const& _sSource,
660 osl::FileBase::RC _nAssumeError,
661 OString const& _sAssumeResultStr,
662 bool bDirection = true);
664 void checkWNTBehaviour_getSystemPathFromFileURL(
665 OString const& _sURL,
666 osl::FileBase::RC _nAssumeError,
667 OString const& _sWNTAssumeResultString);
669 void checkUNXBehaviour_getSystemPathFromFileURL(
670 OString const& _sURL,
671 osl::FileBase::RC _nAssumeError,
672 OString const& _sUnixAssumeResultString);
674 void checkWNTBehaviour_getFileURLFromSystemPath(OString const& _sSysPath,
675 osl::FileBase::RC _nAssumeError,
676 OString const& _sWNTAssumeResultString);
678 void checkUNXBehaviour_getFileURLFromSystemPath(
679 OString const& _sSysPath,
680 osl::FileBase::RC _nAssumeError,
681 OString const& _sUnixAssumeResultString);
685 // if bDirection==sal_True, check getSystemPathFromFileURL
686 // if bDirection==sal_False, check getFileURLFromSystemPath
687 void SystemPath_FileURL::check_SystemPath_FileURL(
688 OString const& _sSource,
689 osl::FileBase::RC _nAssumeError,
690 OString const& _sAssumeResultStr,
691 bool bDirection)
693 // PRE: URL as String
694 OUString suSource;
695 OUString suStr;
696 suSource = OStringToOUString(_sSource, RTL_TEXTENCODING_UTF8);
697 osl::FileBase::RC nError;
699 if (bDirection)
700 nError = osl::FileBase::getSystemPathFromFileURL(suSource, suStr);
701 else
702 nError = osl::FileBase::getFileURLFromSystemPath(suSource, suStr);
704 // if the given string is gt length 0,
705 // we check also this string
706 OString sStr = OUStringToOString(suStr, RTL_TEXTENCODING_UTF8);
707 OString sError = errorToString(nError);
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());
716 if (!_sAssumeResultStr.isEmpty())
718 bool bStrAreEqual = _sAssumeResultStr == sStr;
719 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong",
720 _nAssumeError, nError);
721 CPPUNIT_ASSERT_MESSAGE("Assumption is wrong",
722 bStrAreEqual);
724 else
726 CPPUNIT_ASSERT_EQUAL_MESSAGE("Assumption is wrong", _nAssumeError, nError);
730 void SystemPath_FileURL::checkWNTBehaviour_getSystemPathFromFileURL(
731 OString const& _sURL,
732 osl::FileBase::RC _nAssumeError,
733 OString const& _sWNTAssumeResultString)
735 #if defined(_WIN32)
736 check_SystemPath_FileURL(_sURL, _nAssumeError, _sWNTAssumeResultString);
737 #else
738 (void)_sURL;
739 (void)_nAssumeError;
740 (void)_sWNTAssumeResultString;
741 #endif
744 void SystemPath_FileURL::checkUNXBehaviour_getSystemPathFromFileURL(
745 OString const& _sURL,
746 osl::FileBase::RC _nAssumeError,
747 OString const& _sUnixAssumeResultString)
749 #if (defined UNX)
750 check_SystemPath_FileURL(_sURL, _nAssumeError, _sUnixAssumeResultString);
751 #else
752 (void)_sURL;
753 (void)_nAssumeError;
754 (void)_sUnixAssumeResultString;
755 #endif
758 void SystemPath_FileURL::checkWNTBehaviour_getFileURLFromSystemPath(
759 OString const& _sSysPath,
760 osl::FileBase::RC _nAssumeError,
761 OString const& _sWNTAssumeResultString)
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
772 void SystemPath_FileURL::checkUNXBehaviour_getFileURLFromSystemPath(
773 OString const& _sSysPath,
774 osl::FileBase::RC _nAssumeError,
775 OString const& _sUnixAssumeResultString)
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
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
795 void SystemPath_FileURL::getSystemPathFromFileURL_001_1()
797 OString sURL("");
798 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
799 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
802 void SystemPath_FileURL::getSystemPathFromFileURL_001_2()
804 OString sURL("/");
805 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
806 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "\\");
809 void SystemPath_FileURL::getSystemPathFromFileURL_001_21()
811 /* From RFC3986, "2.2. Reserved Characters":
813 "The purpose of reserved characters is to provide a set of delimiting
814 characters that are distinguishable from other data within a URI.
815 URIs that differ in the replacement of a reserved character with its
816 corresponding percent-encoded octet are not equivalent. Percent-
817 encoding a reserved character, or decoding a percent-encoded octet
818 that corresponds to a reserved character, will change how the URI is
819 interpreted by most applications. Thus, characters in the reserved
820 set are protected from normalization and are therefore safe to be
821 used by scheme-specific and producer-specific algorithms for
822 delimiting data subcomponents within a URI."
824 In other words, %2F ("/") is NOT the same as /.
826 OString sURL("%2F");
827 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
828 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
831 void SystemPath_FileURL::getSystemPathFromFileURL_001_22()
833 OString sURL("file:///tmp%2Fmydir");
834 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
835 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
838 void SystemPath_FileURL::getSystemPathFromFileURL_001_3()
840 OString sURL("a");
841 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "a");
842 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "a");
845 void SystemPath_FileURL::getSystemPathFromFileURL_001_31()
847 OString sURL("tmpname");
848 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "tmpname");
849 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "tmpname");
852 void SystemPath_FileURL::getSystemPathFromFileURL_001_4()
854 OString sURL("file://");
855 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "");
856 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
859 void SystemPath_FileURL::getSystemPathFromFileURL_001_41()
861 OString sURL("file://localhost/tmp");
862 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "");
863 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
866 void SystemPath_FileURL::getSystemPathFromFileURL_001_5()
868 OString sURL("file:///tmp");
869 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp");
870 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
873 void SystemPath_FileURL::getSystemPathFromFileURL_001_51()
875 OString sURL("file://c:/tmp");
876 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
877 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
880 void SystemPath_FileURL::getSystemPathFromFileURL_001_52()
882 OString sURL("file:///c:/tmp");
883 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp");
884 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp");
887 void SystemPath_FileURL::getSystemPathFromFileURL_001_53()
889 OString sURL("file:///c|/tmp");
890 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c|/tmp");
891 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp");
894 void SystemPath_FileURL::getSystemPathFromFileURL_001_6()
896 OString sURL("file:///tmp/first");
897 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/first");
898 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
901 void SystemPath_FileURL::getSystemPathFromFileURL_001_61()
903 OString sURL("file:///c:/tmp/first");
904 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/first");
905 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\first");
908 void SystemPath_FileURL::getSystemPathFromFileURL_001_7()
910 OString sURL("file:///tmp/../second");
911 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/../second");
912 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
915 void SystemPath_FileURL::getSystemPathFromFileURL_001_71()
917 OString sURL("file:///c:/tmp/../second");
918 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/../second");
919 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\..\\second");
922 void SystemPath_FileURL::getSystemPathFromFileURL_001_8()
924 OString sURL("../tmp");
925 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "../tmp");
926 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "..\\tmp");
929 void SystemPath_FileURL::getSystemPathFromFileURL_001_81()
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
941 void SystemPath_FileURL::getSystemPathFromFileURL_001_9()
943 OString sURL("file:///tmp/first%20second");
944 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/tmp/first second");
945 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
948 void SystemPath_FileURL::getSystemPathFromFileURL_001_91()
950 OString sURL("file:///c:/tmp/first%20second");
951 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "/c:/tmp/first second");
952 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_None, "c:\\tmp\\first second");
955 void SystemPath_FileURL::getSystemPathFromFileURL_001_92()
957 OString sURL("ca@#;+.,$///78no%01ni..name");
958 checkUNXBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
959 checkWNTBehaviour_getSystemPathFromFileURL(sURL, osl::FileBase::E_INVAL, "");
962 // normal legal case
963 void SystemPath_FileURL::getSystemPathFromFileURL_004()
965 OUString aUStr;
966 OUString aUNormalURL(aTmpName6);
967 OUString aUResultURL (aSysPath4);
968 osl::FileBase::RC nError = osl::FileBase::getSystemPathFromFileURL(aUNormalURL, aUStr);
970 bool bOk = compareFileName(aUStr, aUResultURL);
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));
979 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError);
980 CPPUNIT_ASSERT_MESSAGE(sError.getStr(), bOk);
984 // CJK characters case
985 void SystemPath_FileURL::getSystemPathFromFileURL_005()
987 OUString aUStr;
988 createTestDirectory(aTmpName10);
989 OUString aUNormalURL(aTmpName10);
990 OUString aUResultURL (aSysPath5);
992 osl::FileBase::RC nError = osl::FileBase::getSystemPathFromFileURL(aUNormalURL, aUStr);
994 bool bOk = compareFileName(aUStr, aUResultURL);
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);
1004 CPPUNIT_ASSERT_EQUAL_MESSAGE(sError.getStr(), osl::FileBase::E_None, nError);
1005 CPPUNIT_ASSERT_MESSAGE(sError.getStr(), bOk);
1008 void SystemPath_FileURL::getFileURLFromSystemPath_001()
1010 OString sSysPath("~/tmp");
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");
1019 void SystemPath_FileURL::getFileURLFromSystemPath_002()
1021 OString sSysPath("c:/tmp");
1022 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "c:/tmp");
1023 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///c:/tmp");
1026 void SystemPath_FileURL::getFileURLFromSystemPath_003()
1028 OString sSysPath("file:///temp");
1029 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1030 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1033 void SystemPath_FileURL::getFileURLFromSystemPath_004()
1035 OString sSysPath("//tmp//first start");
1036 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///tmp/first%20start");
1037 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1040 void SystemPath_FileURL::getFileURLFromSystemPath_004_1()
1042 OString sSysPath("/tmp///first start");
1043 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_None, "file:///tmp/first%20start");
1044 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1047 void SystemPath_FileURL::getFileURLFromSystemPath_005()
1049 OString sSysPath("");
1050 checkUNXBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1051 checkWNTBehaviour_getFileURLFromSystemPath(sSysPath, osl::FileBase::E_INVAL, "");
1054 // testing the method
1055 // static inline RC searchFileURL( const OUString& ustrFileName,
1056 // const OUString& ustrSearchPath,
1057 // OUString& ustrFileURL)
1059 class searchFileURL : public CppUnit::TestFixture
1061 private:
1062 OUString aUStr;
1064 public:
1065 void searchFileURL_001()
1067 /* search file is passed by system filename */
1068 auto nError1 = osl::FileBase::searchFileURL(aTmpName1, aUserDirectorySys, aUStr);
1069 /* search file is passed by full qualified file URL */
1070 auto nError2 = osl::FileBase::searchFileURL(aCanURL1, aUserDirectorySys, aUStr);
1071 /* search file is passed by relative file path */
1072 auto nError3 = osl::FileBase::searchFileURL(aRelURL4, aUserDirectorySys, aUStr);
1074 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
1075 osl::FileBase::E_NOENT, nError1);
1076 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
1077 osl::FileBase::E_NOENT, nError2);
1078 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: system filename/URL filename/relative path, system directory, searched files that is not exist, but it reply invalid error, did not pass in (W32) ",
1079 osl::FileBase::E_NOENT, nError3);
1082 void searchFileURL_002()
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);
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
1126 void searchFileURL_003()
1128 OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path");
1129 auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr);
1130 bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1131 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1132 osl::FileBase::E_None, nError1);
1133 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1134 bOk);
1137 void searchFileURL_004()
1139 OUString aSystemPathList(aRootSys + PATH_LIST_DELIMITER + aTempDirectorySys + PATH_LIST_DELIMITER + aRootSys + "system/path/../name");
1140 auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aSystemPathList, aUStr);
1141 bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1142 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1143 osl::FileBase::E_None, nError1);
1144 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is a list of system paths",
1145 bOk);
1148 void searchFileURL_005()
1150 auto nError1 = osl::FileBase::searchFileURL(aUserDirectoryURL, aNullURL, aUStr);
1151 bool bOk = compareFileName(aUStr, aUserDirectoryURL);
1152 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for searchFileURL function: search directory is NULL",
1153 osl::FileBase::E_None, nError1);
1154 CPPUNIT_ASSERT_MESSAGE("test for searchFileURL function: search directory is NULL",
1155 bOk);
1158 CPPUNIT_TEST_SUITE(searchFileURL);
1159 CPPUNIT_TEST(searchFileURL_001);
1160 CPPUNIT_TEST(searchFileURL_002);
1161 CPPUNIT_TEST(searchFileURL_003);
1162 CPPUNIT_TEST(searchFileURL_004);
1163 CPPUNIT_TEST(searchFileURL_005);
1164 CPPUNIT_TEST_SUITE_END();
1167 // testing the method
1168 // static inline RC getTempDirURL(OUString& ustrTempDirURL)
1170 class getTempDirURL : public CppUnit::TestFixture
1172 private:
1173 OUString aUStr;
1175 public:
1176 void setUp() override
1178 osl::FileBase::RC nError = osl::FileBase::getTempDirURL(aUStr);
1179 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getTempDirURL function: execution",
1180 osl::FileBase::E_None, nError);
1183 void getTempDirURL_002()
1185 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1186 checkDirectory(aUStr, oslCheckMode::OpenAccess));
1187 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1188 checkDirectory(aUStr, oslCheckMode::ReadAccess));
1189 CPPUNIT_ASSERT_MESSAGE("test for getTempDirURL function: test for open and write access rights",
1190 checkDirectory(aUStr, oslCheckMode::WriteAccess));
1193 CPPUNIT_TEST_SUITE(getTempDirURL);
1194 CPPUNIT_TEST(getTempDirURL_002);
1195 CPPUNIT_TEST_SUITE_END();
1198 // testing the method
1199 // static inline RC createTempFile(OUString* pustrDirectoryURL,
1200 // oslFileHandle* pHandle,
1201 // OUString* pustrTempFileURL)
1203 class createTempFile : public CppUnit::TestFixture
1205 private:
1206 std::unique_ptr<oslFileHandle> pHandle;
1207 std::unique_ptr<OUString> pUStr_DirURL;
1208 std::unique_ptr<OUString> pUStr_FileURL;
1210 public:
1211 void setUp() override
1213 pHandle.reset(new oslFileHandle());
1214 pUStr_DirURL.reset(new OUString(aUserDirectoryURL));
1215 pUStr_FileURL.reset(new OUString());
1218 void tearDown() override
1220 pUStr_DirURL.reset();
1221 pUStr_FileURL.reset();
1222 pHandle.reset();
1225 void createTempFile_001()
1227 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), pUStr_FileURL.get());
1228 File testFile(*pUStr_FileURL);
1229 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1231 if (nError2 == osl::FileBase::E_EXIST)
1233 osl_closeFile(*pHandle);
1234 deleteTestFile(*pUStr_FileURL);
1237 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1238 osl::FileBase::E_None, nError1);
1239 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: create temp file and test the existence",
1240 (pHandle != nullptr));
1241 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: create temp file and test the existence",
1242 osl::FileBase::E_EXIST, nError2);
1245 void createTempFile_002()
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);
1252 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1253 osl::FileBase::E_None, nError1);
1254 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1255 (pHandle != nullptr));
1256 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1257 osl::FileBase::E_EXIST, nError2);
1259 // check file if have the write permission
1260 if (nError2 == osl::FileBase::E_EXIST)
1262 bOK = ifFileCanWrite(*pUStr_FileURL);
1263 osl_closeFile(*pHandle);
1264 deleteTestFile(*pUStr_FileURL);
1267 CPPUNIT_ASSERT_MESSAGE("test for open and write access rights, in (W32), it did not have write access right, but it should be writable.",
1268 bOK);
1271 void createTempFile_003()
1273 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), pHandle.get(), nullptr);
1274 // the temp file will be removed when return from createTempFile
1275 bool bOK = (pHandle != nullptr && nError1 == osl::FileBase::E_None);
1276 if (bOK)
1277 osl_closeFile(*pHandle);
1279 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1280 osl::FileBase::E_None, nError1);
1281 CPPUNIT_ASSERT_MESSAGE("test for createTempFile function: set pUStrFileURL to 0 to let it remove the file after call.",
1282 bOK);
1285 void createTempFile_004()
1287 auto nError1 = osl::FileBase::createTempFile(pUStr_DirURL.get(), nullptr, pUStr_FileURL.get());
1288 bool bOK = (pUStr_FileURL != nullptr);
1289 CPPUNIT_ASSERT(bOK);
1290 File testFile(*pUStr_FileURL);
1291 auto nError2 = testFile.open(osl_File_OpenFlag_Create);
1292 deleteTestFile(*pUStr_FileURL);
1293 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1294 osl::FileBase::E_None, nError1);
1295 CPPUNIT_ASSERT_EQUAL_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1296 osl::FileBase::E_EXIST, nError2);
1297 CPPUNIT_ASSERT_MESSAGE("createTempFile function: create a temp file, but it does not exist",
1298 bOK);
1302 CPPUNIT_TEST_SUITE(createTempFile);
1303 CPPUNIT_TEST(createTempFile_001);
1304 CPPUNIT_TEST(createTempFile_002);
1305 CPPUNIT_TEST(createTempFile_003);
1306 CPPUNIT_TEST(createTempFile_004);
1307 CPPUNIT_TEST_SUITE_END();
1310 // FIXME: remove the _disabled to enable:
1311 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getAbsoluteFileURL, "osl_osl::FileBase");
1312 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::SystemPath_FileURL, "osl_osl::FileBase");
1313 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::searchFileURL, "osl_osl::FileBase");
1314 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::getTempDirURL, "osl_osl::FileBase");
1315 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_FileBase::createTempFile, "osl_osl::FileBase");
1317 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_osl::FileBase");
1320 namespace osl_FileStatus
1322 // testing the method
1323 // FileStatus(sal_uInt32 nMask): _nMask(nMask)
1324 class ctors : public CppUnit::TestFixture
1326 private:
1327 OUString aUStr;
1328 DirectoryItem rItem;
1330 public:
1331 void setUp() override
1333 // create a tempfile in $TEMP/tmpdir/tmpname.
1334 createTestDirectory(aTmpName3);
1335 createTestFile(aTmpName4);
1337 Directory aDir(aTmpName3);
1338 auto nError1 = aDir.open();
1339 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1340 nError1 = aDir.getNextItem(rItem);
1341 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1342 aDir.close();
1345 void tearDown() override
1347 // remove the tempfile in $TEMP/tmpdir/tmpname.
1348 deleteTestFile(aTmpName4);
1349 deleteTestDirectory(aTmpName3);
1352 void ctors_001()
1354 FileStatus rFileStatus(osl_FileStatus_Mask_All);
1355 auto nError1 = rItem.getFileStatus(rFileStatus);
1356 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1357 aUStr = rFileStatus.getFileName();
1359 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask all and see the file name",
1360 compareFileName(aUStr, aTmpName2));
1363 void ctors_002()
1365 FileStatus rFileStatus(0);
1366 auto nError1 = rItem.getFileStatus(rFileStatus);
1367 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1368 aUStr = rFileStatus.getFileName();
1370 CPPUNIT_ASSERT_MESSAGE("test for ctors function: mask is empty",
1371 compareFileName(aUStr, aNullURL));
1374 CPPUNIT_TEST_SUITE(ctors);
1375 CPPUNIT_TEST(ctors_001);
1376 CPPUNIT_TEST(ctors_002);
1377 CPPUNIT_TEST_SUITE_END();
1380 // testing the method
1381 // inline sal_Bool isValid(sal_uInt32 nMask) const
1383 class isValid : public CppUnit::TestFixture
1385 private:
1386 std::unique_ptr<Directory> pDir;
1387 DirectoryItem rItem_file, rItem_link;
1389 public:
1390 void setUp() override
1392 // create a tempfile in $TEMP/tmpdir/tmpname.
1393 createTestDirectory(aTmpName3);
1394 createTestFile(aTmpName4);
1396 pDir.reset(new Directory(aTmpName3));
1397 osl::FileBase::RC nError1 = pDir->open();
1398 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1399 nError1 = pDir->getNextItem(rItem_file, 1);
1400 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1403 void tearDown() override
1405 osl::FileBase::RC nError1 = pDir->close();
1406 pDir.reset();
1407 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
1409 // remove the tempfile in $TEMP/tmpdir/tmpname.
1410 deleteTestFile(aTmpName4);
1411 deleteTestDirectory(aTmpName3);
1414 void isValid_001()
1416 sal_uInt32 mask = 0;
1417 FileStatus rFileStatus(mask);
1418 osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus);
1419 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1420 bool bOk = rFileStatus.isValid(mask);
1422 CPPUNIT_ASSERT_MESSAGE("test for isValid function: no fields specified", bOk);
1425 void check_FileStatus(FileStatus const& _aStatus)
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());
1447 void isValid_002()
1449 createTestFile(aTmpName6);
1450 sal_uInt32 mask_file = osl_FileStatus_Mask_Type |
1451 osl_FileStatus_Mask_Attributes |
1452 osl_FileStatus_Mask_CreationTime |
1453 osl_FileStatus_Mask_AccessTime |
1454 osl_FileStatus_Mask_ModifyTime |
1455 osl_FileStatus_Mask_FileSize |
1456 osl_FileStatus_Mask_FileName |
1457 osl_FileStatus_Mask_FileURL;
1459 FileStatus rFileStatus(mask_file);
1460 DirectoryItem::get(aTmpName6, rItem_file);
1461 osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus);
1463 CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError1).getStr(), osl::FileBase::E_None, nError1);
1465 check_FileStatus(rFileStatus);
1466 deleteTestFile(aTmpName6);
1470 /** Check if is a valid linked file.
1472 Link is not defined in Windows, and on Linux, we can not get the directory item of the linked file.
1473 We have to defer to filesystems, normal filesystems support links (EXT2, ...), castrated filesystems
1474 don't have links (FAT, FAT32) and Windows NT NTFS support links, but the Windows API doesn't :-(
1476 void isValid_003()
1478 #if 0
1479 #if defined (UNX)
1480 sal_Int32 fd;
1482 OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
1483 aUStr_LnkFileSys += aSlashURL + getCurrentPID() + "/tmpdir/link.file";
1484 aUStr_SrcFileSys += aSlashURL + getCurrentPID() + "/tmpdir/tmpname";
1486 OString strLinkFileName;
1487 OString strSrcFileName;
1488 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
1489 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
1491 // create a link file and link it to file "/tmp/PID/tmpdir/tmpname"
1492 fd = symlink(strSrcFileName.getStr(), strLinkFileName.getStr());
1493 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
1495 // testDirectory is "/tmp/PID/tmpdir/"
1496 Directory testDirectory(aTmpName3);
1497 testDirectory.open();
1498 OUString aFileName ("link.file");
1499 bool bOk = false;
1500 while (true)
1502 osl::FileBase::RC nError1 = testDirectory.getNextItem(rItem_link, 4);
1504 if (nError1 == osl::FileBase::E_None)
1506 sal_uInt32 mask_link = osl_FileStatus_Mask_FileName | osl_FileStatus_Mask_LinkTargetURL;
1507 FileStatus rFileStatus(mask_link);
1508 rItem_link.getFileStatus(rFileStatus);
1510 if (compareFileName(rFileStatus.getFileName(), aFileName))
1512 if (rFileStatus.isValid(osl_FileStatus_Mask_LinkTargetURL))
1514 bOk = true;
1515 break;
1519 else
1521 break;
1525 fd = remove(strLinkFileName.getStr());
1526 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), fd);
1528 CPPUNIT_ASSERT_MESSAGE("test for isValid function: link file, check for LinkTargetURL", bOk);
1529 #endif
1530 #endif
1533 void isValid_004()
1535 sal_uInt32 mask_file_all = osl_FileStatus_Mask_All;
1536 FileStatus rFileStatus_all(mask_file_all);
1537 osl::FileBase::RC nError1 = rItem_file.getFileStatus(rFileStatus_all);
1538 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1540 check_FileStatus(rFileStatus_all);
1542 sal_uInt32 mask_file_val = osl_FileStatus_Mask_Validate;
1543 FileStatus rFileStatus_val(mask_file_val);
1544 nError1 = rItem_file.getFileStatus(rFileStatus_val);
1545 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1547 check_FileStatus(rFileStatus_val);
1550 CPPUNIT_TEST_SUITE(isValid);
1551 CPPUNIT_TEST(isValid_001);
1552 CPPUNIT_TEST(isValid_002);
1553 CPPUNIT_TEST(isValid_003);
1554 CPPUNIT_TEST(isValid_004);
1555 CPPUNIT_TEST_SUITE_END();
1558 // testing the method
1559 // inline Type getFileType() const
1561 class getFileType : public CppUnit::TestFixture
1563 private:
1564 DirectoryItem m_aItem_1, m_aItem_2, m_aVolumeItem, m_aFifoItem;
1565 DirectoryItem m_aLinkItem, m_aSocketItem, m_aSpecialItem;
1567 public:
1568 void setUp() override
1570 // create a tempfile: $TEMP/tmpdir/tmpname.
1571 // a tempdirectory: $TEMP/tmpdir/tmpdir.
1572 // use $ROOT/staroffice as volume ---> use dev/fd as volume.
1573 // and get their directory item.
1574 createTestDirectory(aTmpName3);
1575 createTestFile(aTmpName3, aTmpName2);
1576 createTestDirectory(aTmpName3, aTmpName1);
1578 std::unique_ptr<Directory> xDir(new Directory(aTmpName3));
1579 auto nError1 = xDir->open();
1580 CPPUNIT_ASSERT_EQUAL_MESSAGE("open aTmpName3 failed!", osl::FileBase::E_None, nError1);
1581 // getNextItem can not assure which item retrieved
1582 nError1 = xDir->getNextItem(m_aItem_1, 1);
1583 CPPUNIT_ASSERT_EQUAL_MESSAGE("get first item failed!", osl::FileBase::E_None, nError1);
1585 nError1 = xDir->getNextItem(m_aItem_2);
1586 CPPUNIT_ASSERT_EQUAL_MESSAGE("get second item failed!", osl::FileBase::E_None, nError1);
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);
1593 void tearDown() override
1595 // remove all in $TEMP/tmpdir.
1596 deleteTestDirectory(aTmpName3, aTmpName1);
1597 deleteTestFile(aTmpName3, aTmpName2);
1598 deleteTestDirectory(aTmpName3);
1601 void getFileType_001()
1603 FileStatus rFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName);
1604 auto nError1 = m_aItem_1.getFileStatus(rFileStatus);
1605 CPPUNIT_ASSERT_EQUAL_MESSAGE("getFileStatus failed", osl::FileBase::E_None, nError1);
1607 check_FileType(rFileStatus);
1610 void check_FileType(osl::FileStatus const& _rFileStatus)
1612 if (_rFileStatus.isValid(osl_FileStatus_Mask_FileName))
1614 OUString suFilename = _rFileStatus.getFileName();
1616 if (_rFileStatus.isValid(osl_FileStatus_Mask_Type))
1618 osl::FileStatus::Type eType = _rFileStatus.getFileType();
1619 bool bOK = false;
1621 if (compareFileName(suFilename, aTmpName2))
1622 bOK = (eType == osl::FileStatus::Regular);
1624 if (compareFileName(suFilename, aTmpName1))
1625 bOK = (eType == FileStatus::Directory);
1627 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: ", bOK);
1630 // LLA: it's not a bug, if a FileStatus not exist, so no else
1633 void getFileType_002()
1635 FileStatus rFileStatus(osl_FileStatus_Mask_Type | osl_FileStatus_Mask_FileName);
1636 auto nError1 = m_aItem_2.getFileStatus(rFileStatus);
1638 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1639 check_FileType(rFileStatus);
1642 void getFileType_003()
1646 void getFileType_007()
1648 #if defined(__sun) // Special file is different in Windows
1649 auto nError1 = DirectoryItem::get(aTypeURL2, m_aSpecialItem);
1650 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1652 // check for File type
1653 FileStatus rFileStatus(osl_FileStatus_Mask_Type);
1654 nError1 = m_aSpecialItem.getFileStatus(rFileStatus);
1655 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
1657 if (rFileStatus.isValid(osl_FileStatus_Mask_Type))
1659 osl::FileStatus::Type eType = rFileStatus.getFileType();
1661 CPPUNIT_ASSERT_MESSAGE("test for getFileType function: Special, Solaris version ",
1662 (eType == FileStatus::Special));
1664 #endif
1667 CPPUNIT_TEST_SUITE(getFileType);
1668 CPPUNIT_TEST(getFileType_001);
1669 CPPUNIT_TEST(getFileType_002);
1670 CPPUNIT_TEST(getFileType_003);
1671 CPPUNIT_TEST(getFileType_007);
1672 CPPUNIT_TEST_SUITE_END();
1675 // testing the method
1676 // inline sal_uInt64 getAttributes() const
1678 class getAttributes : public CppUnit::TestFixture
1680 private:
1681 OUString aTypeURL, aTypeURL_Hid;
1682 DirectoryItem rItem, rItem_hidden;
1684 public:
1685 void setUp() override
1687 aTypeURL = aUserDirectoryURL.copy(0);
1688 concatURL(aTypeURL, aTmpName2);
1689 createTestFile(aTypeURL);
1690 osl::FileBase::RC nError = DirectoryItem::get(aTypeURL, rItem);
1691 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1693 aTypeURL_Hid = aUserDirectoryURL.copy(0);
1694 concatURL(aTypeURL_Hid, aHidURL1);
1695 createTestFile(aTypeURL_Hid);
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);
1703 void tearDown() override
1705 deleteTestFile(aTypeURL);
1706 deleteTestFile(aTypeURL_Hid);
1709 #if (defined UNX)
1710 // windows only has 3 file attributes: normal, readonly and hidden
1711 void getAttributes_001()
1713 changeFileMode(aTypeURL, S_IRUSR | S_IRGRP | S_IROTH);
1715 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1716 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1717 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1719 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());
1723 #else // Windows version
1724 void getAttributes_001()
1726 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: ReadOnly, GrpRead, OwnRead, OthRead(Windows version)",
1727 1, 1);
1729 #endif
1731 void getAttributes_002()
1733 #if (defined UNX)
1734 changeFileMode(aTypeURL, S_IXUSR | S_IXGRP | S_IXOTH);
1736 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1737 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1738 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
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
1746 #if (defined UNX)
1747 void getAttributes_003()
1749 changeFileMode(aTypeURL, S_IWUSR | S_IWGRP | S_IWOTH);
1751 FileStatus rFileStatus(osl_FileStatus_Mask_Attributes);
1752 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1753 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
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());
1759 #else // Windows version
1760 void getAttributes_003()
1762 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: GrpWrite, OwnWrite, OthWrite(Windows version)",
1763 1, 1);
1765 #endif
1767 void getAttributes_004()
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();
1775 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getAttributes function: Hidden files",
1776 static_cast<sal_Int32>(osl_File_Attribute_Hidden), test_Attributes);
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();
1787 // testing the method
1788 // inline TimeValue getAccessTime() const
1790 class getAccessTime : public CppUnit::TestFixture
1792 private:
1793 OUString aTypeURL;
1794 DirectoryItem rItem;
1796 public:
1797 void setUp() override
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);
1807 void tearDown() override
1809 deleteTestFile(aTypeURL);
1812 void getAccessTime_001()
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);
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);
1825 *pTV_access = rFileStatus.getAccessTime();
1827 bool bOK = t_compareTime(pTV_access, pTV_current, delta);
1828 free(pTV_current);
1829 free(pTV_access);
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);
1835 CPPUNIT_TEST_SUITE(getAccessTime);
1836 CPPUNIT_TEST(getAccessTime_001);
1837 CPPUNIT_TEST_SUITE_END();
1840 // testing the method
1841 // inline TimeValue getModifyTime() const
1843 class getModifyTime : public CppUnit::TestFixture
1845 private:
1846 OUString aTypeURL;
1847 DirectoryItem rItem;
1849 public:
1850 void getModifyTime_001()
1852 TimeValue *pTV_current = nullptr;
1853 CPPUNIT_ASSERT((pTV_current = static_cast<TimeValue*>(malloc(sizeof(TimeValue)))) != nullptr);
1855 // create file
1856 aTypeURL = aUserDirectoryURL.copy(0);
1857 concatURL(aTypeURL, aTmpName2);
1858 createTestFile(aTypeURL);
1860 // get current time
1861 bool bOk = osl_getSystemTime(pTV_current);
1862 CPPUNIT_ASSERT(bOk);
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);
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();
1876 bool bOK = t_compareTime(pTV_modify, pTV_current, delta);
1877 // delete file
1878 deleteTestFile(aTypeURL);
1879 free(pTV_current);
1880 free(pTV_modify);
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);
1886 CPPUNIT_TEST_SUITE(getModifyTime);
1887 CPPUNIT_TEST(getModifyTime_001);
1888 CPPUNIT_TEST_SUITE_END();
1891 // testing the method
1892 // inline sal_uInt64 getFileSize() const
1894 class getFileSize : public CppUnit::TestFixture
1896 private:
1897 OUString aTypeURL;
1898 DirectoryItem rItem;
1900 public:
1901 void setUp() override
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);
1910 void tearDown() override
1912 deleteTestFile(aTypeURL);
1915 void getFileSize_001()
1917 FileStatus rFileStatus(osl_FileStatus_Mask_FileSize);
1918 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1919 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1921 sal_uInt64 uFileSize = rFileStatus.getFileSize();
1923 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileSize function: empty file ",
1924 static_cast<sal_uInt64>(0), uFileSize);
1927 void getFileSize_002()
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);
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();
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);
1946 CPPUNIT_TEST_SUITE(getFileSize);
1947 CPPUNIT_TEST(getFileSize_001);
1948 CPPUNIT_TEST(getFileSize_002);
1949 CPPUNIT_TEST_SUITE_END();
1952 // testing the method
1953 // inline OUString getFileName() const
1955 class getFileName : public CppUnit::TestFixture
1957 private:
1958 OUString aTypeURL;
1959 DirectoryItem rItem;
1961 public:
1962 void setUp() override
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);
1971 void tearDown() override
1973 deleteTestFile(aTypeURL);
1977 void getFileName_001()
1979 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
1980 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
1981 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
1983 OUString aFileName = rFileStatus.getFileName();
1985 CPPUNIT_ASSERT_MESSAGE("test for getFileName function: name compare with specify",
1986 compareFileName(aFileName, aTmpName2));
1989 CPPUNIT_TEST_SUITE(getFileName);
1990 CPPUNIT_TEST(getFileName_001);
1991 CPPUNIT_TEST_SUITE_END();
1994 // testing the method
1995 // inline OUString getFileURL() const
1997 class getFileURL : public CppUnit::TestFixture
1999 DirectoryItem rItem;
2001 public:
2002 void setUp() override
2004 createTestFile(aTmpName6);
2005 osl::FileBase::RC nError = DirectoryItem::get(aTmpName6, rItem);
2006 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
2009 void tearDown() override
2011 deleteTestFile(aTmpName6);
2015 void getFileURL_001()
2017 FileStatus rFileStatus(osl_FileStatus_Mask_FileURL);
2018 osl::FileBase::RC nError = rItem.getFileStatus(rFileStatus);
2019 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError);
2021 OUString aFileURL = rFileStatus.getFileURL();
2023 CPPUNIT_ASSERT_MESSAGE("test for getFileURL function: ",
2024 compareFileName(aFileURL, aTmpName6));
2027 CPPUNIT_TEST_SUITE(getFileURL);
2028 CPPUNIT_TEST(getFileURL_001);
2029 CPPUNIT_TEST_SUITE_END();
2032 // testing the method
2033 // inline OUString getLinkTargetURL() const
2035 class getLinkTargetURL : public CppUnit::TestFixture
2037 private:
2038 OUString aTypeURL;
2039 DirectoryItem rItem;
2041 public:
2042 void setUp() override
2044 aTypeURL = aUserDirectoryURL.copy(0);
2045 concatURL(aTypeURL, aTmpName2);
2046 createTestFile(aTypeURL);
2049 void tearDown() override
2051 deleteTestFile(aTypeURL);
2054 void getLinkTargetURL_001()
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";
2063 OString strLinkFileName, strSrcFileName;
2064 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
2065 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
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);
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);
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();
2080 // remove link file
2081 fd = remove(strLinkFileName.getStr());
2082 CPPUNIT_ASSERT_EQUAL_MESSAGE("in deleting link file", static_cast<sal_Int32>(0), fd);
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
2090 CPPUNIT_TEST_SUITE(getLinkTargetURL);
2091 CPPUNIT_TEST(getLinkTargetURL_001);
2092 CPPUNIT_TEST_SUITE_END();
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");
2106 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_FileStatus");
2109 namespace osl_File
2112 // testing the method
2113 // File(const OUString& ustrFileURL)
2115 class ctors : public CppUnit::TestFixture
2117 public:
2118 void setUp() override
2120 // create a tempfile in $TEMP/tmpdir/tmpname.
2121 createTestDirectory(aTmpName3);
2122 createTestFile(aTmpName4);
2125 void tearDown() override
2127 // remove the tempfile in $TEMP/tmpdir/tmpname.
2128 deleteTestFile(aTmpName4);
2129 deleteTestDirectory(aTmpName3);
2132 void ctors_001()
2134 File testFile(aTmpName4);
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);
2144 void ctors_002()
2146 File testFile(aTmpName5);
2147 char buffer[30] = "Test for File constructor";
2148 sal_uInt64 nCount;
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();
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);
2160 CPPUNIT_TEST_SUITE(ctors);
2161 CPPUNIT_TEST(ctors_001);
2162 CPPUNIT_TEST(ctors_002);
2163 CPPUNIT_TEST_SUITE_END();
2166 // testing the method
2167 // inline RC open(sal_uInt32 uFlags)
2169 class open : public CppUnit::TestFixture
2171 public:
2172 void setUp() override
2174 // create a tempfile in $TEMP/tmpdir/tmpname.
2175 createTestDirectory(aTmpName3);
2176 createTestFile(aTmpName4);
2179 void tearDown() override
2181 // remove the tempfile in $TEMP/tmpdir/tmpname.
2182 deleteTestFile(aTmpName4);
2183 deleteTestDirectory(aTmpName3);
2187 void open_001()
2189 File testFile(aTmpName4);
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);
2195 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a regular file",
2196 osl::FileBase::E_None, nError1);
2199 void open_002()
2201 File testFile(aTmpName3);
2203 auto nError1 = testFile.open(osl_File_OpenFlag_Read);
2205 CPPUNIT_ASSERT_MESSAGE("test for open function: open a directory",
2206 (File::E_INVAL == nError1) || (File::E_ACCES == nError1));
2209 void open_003()
2211 File testFile(aCanURL1);
2213 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2215 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a non-exist file",
2216 File::E_NOENT, nError1);
2219 void open_004()
2221 OUString aTestFile(aRootURL);
2222 concatURL(aTestFile, aTmpName2);
2223 File testFile(aTestFile);
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
2233 CPPUNIT_ASSERT_MESSAGE("test for open function: create an illegal file",
2234 bOK);
2237 void open_005()
2239 File testFile(aTmpName4);
2241 auto nError1 = testFile.open(osl_File_OpenFlag_Create);
2243 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: create an exist file",
2244 File::E_EXIST, nError1);
2247 void open_006()
2249 File testFile(aCanURL1);
2250 char buffer_write[30] = "Test for File open";
2251 char buffer_read[30];
2252 sal_uInt64 nCount_write, nCount_read;
2254 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write | osl_File_OpenFlag_Create);
2255 auto nError2 = testFile.write(buffer_write, 30, nCount_write);
2256 osl::FileBase::RC nError4 = testFile.setPos(osl_Pos_Absolut, 0);
2257 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError4);
2258 auto nError3 = testFile.read(buffer_read, 10, nCount_read);
2260 osl::FileBase::RC nError5 = testFile.close();
2261 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError5);
2262 osl::FileBase::RC nError6 = osl::File::remove(aCanURL1);
2263 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError6);
2265 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2266 osl::FileBase::E_None, nError1);
2267 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2268 osl::FileBase::E_None, nError2);
2269 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2270 osl::FileBase::E_None, nError3);
2271 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2272 sal_uInt64(30), nCount_write);
2273 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: test for osl_File_OpenFlag_Read, osl_File_OpenFlag_Write and osl_File_OpenFlag_Create",
2274 sal_uInt64(10), nCount_read);
2277 CPPUNIT_TEST_SUITE(open);
2278 CPPUNIT_TEST(open_001);
2279 CPPUNIT_TEST(open_002);
2280 CPPUNIT_TEST(open_003);
2281 CPPUNIT_TEST(open_004);
2282 CPPUNIT_TEST(open_005);
2283 CPPUNIT_TEST(open_006);
2284 CPPUNIT_TEST_SUITE_END();
2287 // testing the method
2288 // inline RC close()
2290 class close : public CppUnit::TestFixture
2292 public:
2293 void setUp() override
2295 // create a tempfile in $TEMP/tmpdir/tmpname.
2296 createTestDirectory(aTmpName3);
2297 createTestFile(aTmpName4);
2300 void tearDown() override
2302 // remove the tempfile in $TEMP/tmpdir/tmpname.
2303 deleteTestFile(aTmpName4);
2304 deleteTestDirectory(aTmpName3);
2308 void close_001()
2310 File testFile(aTmpName4);
2312 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2313 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2315 auto nError2 = testFile.close();
2317 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for close function: close a regular file",
2318 osl::FileBase::E_None, nError2);
2321 void close_002()
2323 File testFile(aTmpName4);
2325 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2326 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2328 auto nError2 = testFile.close();
2330 auto nError3 = testFile.setPos(osl_Pos_Absolut, 0);
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));
2338 CPPUNIT_TEST_SUITE(close);
2339 CPPUNIT_TEST(close_001);
2340 CPPUNIT_TEST(close_002);
2341 CPPUNIT_TEST_SUITE_END();
2344 // testing the method
2345 // inline RC setPos(sal_uInt32 uHow, sal_Int64 uPos)
2347 class setPos : public CppUnit::TestFixture
2349 public:
2350 void setUp() override
2352 // create a tempfile in $TEMP/tmpdir/tmpname.
2353 createTestDirectory(aTmpName3);
2354 createTestFile(aTmpName4);
2356 // write chars into the file.
2357 File testFile(aTmpName4);
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);
2368 void tearDown() override
2370 // remove the tempfile in $TEMP/tmpdir/tmpname.
2371 deleteTestFile(aTmpName4);
2372 deleteTestDirectory(aTmpName3);
2375 void setPos_001()
2377 File testFile(aTmpName4);
2378 char buffer_read[2];
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);
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]);
2394 void setPos_002()
2396 File testFile(aTmpName4);
2397 char buffer_read[2];
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);
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]);
2415 void setPos_003()
2417 File testFile(aTmpName4);
2418 char buffer_read[2];
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);
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);
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]);
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();
2445 // testing the method
2446 // inline RC getPos(sal_uInt64& uPos)
2448 class getPos : public CppUnit::TestFixture
2450 public:
2451 void setUp() override
2453 // create a tempfile in $TEMP/tmpdir/tmpname.
2454 createTestDirectory(aTmpName3);
2455 createTestFile(aTmpName4);
2457 // write chars into the file.
2458 File testFile(aTmpName4);
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);
2469 void tearDown() override
2471 // remove the tempfile in $TEMP/tmpdir/tmpname.
2472 deleteTestFile(aTmpName4);
2473 deleteTestDirectory(aTmpName3);
2477 void getPos_001()
2479 File testFile(aTmpName4);
2480 sal_uInt64 nFilePointer;
2482 auto nError1 = testFile.getPos(nFilePointer);
2483 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, nError1);
2485 nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2486 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2493 nError1 = testFile.close();
2494 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2500 CPPUNIT_TEST_SUITE(getPos);
2501 CPPUNIT_TEST(getPos_001);
2502 CPPUNIT_TEST_SUITE_END();
2505 // testing the method
2506 // inline RC isEndOfFile(sal_Bool *pIsEOF)
2508 class isEndOfFile : public CppUnit::TestFixture
2510 public:
2511 void setUp() override
2513 // create a tempfile in $TEMP/tmpdir/tmpname.
2514 createTestDirectory(aTmpName3);
2515 createTestFile(aTmpName4);
2517 // write chars into the file.
2518 File testFile(aTmpName4);
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);
2529 void tearDown() override
2531 // remove the tempfile in $TEMP/tmpdir/tmpname.
2532 deleteTestFile(aTmpName4);
2533 deleteTestDirectory(aTmpName3);
2537 void isEndOfFile_001()
2539 File testFile(aTmpName4);
2540 sal_Bool bEOF = false;
2541 sal_Bool *pEOF = &bEOF;
2543 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2544 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2551 nError1 = testFile.close();
2552 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2554 CPPUNIT_ASSERT_MESSAGE("test for isEndOfFile function: set the position to end, check if reach end",
2555 *pEOF);
2558 void isEndOfFile_002()
2560 File testFile(aTmpName4);
2561 sal_Bool bEOF = false;
2562 sal_Bool *pEOF = &bEOF;
2563 sal_uInt64 nFilePointer = 0;
2565 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2566 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2568 nError1 = testFile.setPos(osl_Pos_Absolut, 0);
2569 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2570 *pEOF = false;
2572 while (!(*pEOF))
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);
2580 nError1 = testFile.getPos(nFilePointer);
2581 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2583 nError1 = testFile.close();
2584 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2589 CPPUNIT_TEST_SUITE(isEndOfFile);
2590 CPPUNIT_TEST(isEndOfFile_001);
2591 CPPUNIT_TEST(isEndOfFile_002);
2592 CPPUNIT_TEST_SUITE_END();
2595 // testing the method
2596 // inline RC setSize(sal_uInt64 uSize)
2598 class setSize : public CppUnit::TestFixture
2600 public:
2601 void setUp() override
2603 // create a tempfile in $TEMP/tmpdir/tmpname.
2604 createTestDirectory(aTmpName3);
2605 createTestFile(aTmpName4);
2607 // write chars into the file.
2608 File testFile(aTmpName4);
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);
2619 void tearDown() override
2621 // remove the tempfile in $TEMP/tmpdir/tmpname.
2622 deleteTestFile(aTmpName4);
2623 deleteTestDirectory(aTmpName3);
2627 void setSize_001()
2629 File testFile(aTmpName4);
2630 sal_uInt64 nFilePointer;
2632 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2633 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2635 // enlarge the file to size of 100;
2636 nError1 = testFile.setSize(100);
2637 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2645 nError1 = testFile.close();
2646 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2648 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: enlarge the file ",
2649 static_cast<sal_uInt64>(100), nFilePointer);
2652 void setSize_002()
2654 File testFile(aTmpName4);
2655 sal_uInt64 nFilePointer;
2657 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2658 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2660 // enlarge the file to size of 100;
2661 nError1 = testFile.setSize(10);
2662 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2670 nError1 = testFile.close();
2671 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2673 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for setSize function: truncate the file ",
2674 static_cast<sal_uInt64>(10), nFilePointer);
2677 CPPUNIT_TEST_SUITE(setSize);
2678 CPPUNIT_TEST(setSize_001);
2679 CPPUNIT_TEST(setSize_002);
2680 CPPUNIT_TEST_SUITE_END();
2683 // testing the method
2684 // inline RC read(void *pBuffer, sal_uInt64 uBytesRequested, sal_uInt64& rBytesRead)
2686 class read : public CppUnit::TestFixture
2688 public:
2689 void setUp() override
2691 // create a tempfile in $TEMP/tmpdir/tmpname.
2692 createTestDirectory(aTmpName3);
2693 createTestFile(aTmpName4);
2695 // write chars into the file.
2696 File testFile(aTmpName4);
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);
2707 void tearDown() override
2709 // remove the tempfile in $TEMP/tmpdir/tmpname.
2710 deleteTestFile(aTmpName4);
2711 deleteTestDirectory(aTmpName3);
2715 void read_001()
2717 File testFile(aTmpName4);
2718 sal_uInt64 nFilePointer;
2719 char buffer_read[10];
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);
2729 nError1 = testFile.close();
2730 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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));
2738 void read_002()
2740 File testFile(aTmpName4);
2741 sal_uInt64 nFilePointer;
2742 char buffer_read[26];
2744 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2745 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2755 nError1 = testFile.close();
2756 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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));
2766 CPPUNIT_TEST_SUITE(read);
2767 CPPUNIT_TEST(read_001);
2768 CPPUNIT_TEST(read_002);
2769 CPPUNIT_TEST_SUITE_END();
2772 // testing the method
2773 // inline RC write(const void *pBuffer, sal_uInt64 uBytesToWrite, sal_uInt64& rBytesWritten)
2775 class write : public CppUnit::TestFixture
2777 public:
2778 void setUp() override
2780 // create a tempfile in $TEMP/tmpname.
2781 createTestFile(aTmpName6);
2784 void tearDown() override
2786 // remove the tempfile in $TEMP/tmpname.
2787 deleteTestFile(aTmpName6);
2791 void write_001()
2793 File testFile(aTmpName6);
2794 sal_uInt64 nFilePointer;
2795 char buffer_read[10];
2797 auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write);
2798 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2813 nError1 = testFile.close();
2814 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
2824 CPPUNIT_TEST_SUITE(write);
2825 CPPUNIT_TEST(write_001);
2826 CPPUNIT_TEST_SUITE_END();
2829 // testing the method
2830 // inline RC readLine(::ByteSequence& aSeq)
2832 class readLine : public CppUnit::TestFixture
2834 rtl::ByteSequence aSequence;
2836 public:
2837 void setUp() override
2839 // create a tempfile in $TEMP/tmpname.
2840 createTestFile(aTmpName6);
2842 // write some strings into the file.
2843 File testFile(aTmpName6);
2844 char ppStrSeq[3][27] = { "abcde\n",
2845 "1234567890\n",
2846 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2849 auto nError1 = testFile.open(osl_File_OpenFlag_Write);
2850 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2852 sal_uInt64 nCount_write = 0;
2853 for (int nCount = 0; nCount < 3; nCount++)
2855 nError1 = testFile.write(ppStrSeq[nCount], strlen(ppStrSeq[nCount]), nCount_write);
2856 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2859 nError1 = testFile.close();
2860 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2863 void tearDown() override
2865 // remove the tempfile in $TEMP/tmpname.
2866 deleteTestFile(aTmpName6);
2870 void readLine_001()
2872 File testFile(aTmpName6);
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));
2884 void readLine_002()
2886 File testFile(aTmpName6);
2887 sal_Bool bEOF = false;
2888 sal_Bool *pEOF = &bEOF;
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++)
2894 nError1 = testFile.readLine(aSequence);
2895 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
2897 nError1 = testFile.isEndOfFile(pEOF);
2898 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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));
2905 CPPUNIT_TEST_SUITE(readLine);
2906 CPPUNIT_TEST(readLine_001);
2907 CPPUNIT_TEST(readLine_002);
2908 CPPUNIT_TEST_SUITE_END();
2911 // testing the method
2912 // inline static RC copy(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
2914 class copy : public CppUnit::TestFixture
2916 public:
2917 void setUp() override
2919 // create a tempfile in $TEMP/tmpdir/tmpname.
2920 createTestDirectory(aTmpName3);
2921 createTestFile(aTmpName4);
2923 // write chars into the file.
2924 File testFile(aTmpName4);
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);
2935 void tearDown() override
2937 // remove the tempfile in $TEMP/tmpdir/tmpname.
2938 deleteTestFile(aTmpName4);
2939 deleteTestDirectory(aTmpName3);
2942 void copy_001()
2944 File testFile(aTmpName6);
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);
2953 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy file to upper directory",
2954 osl::FileBase::E_EXIST, nError1);
2957 void copy_002()
2959 // copy $TEMP/tmpdir/tmpname to $TEMP/tmpdir.
2960 auto nError1 = File::copy(aTmpName4, aTmpName3);
2962 CPPUNIT_ASSERT_MESSAGE("test for copy function: use directory as destination",
2963 (osl::FileBase::E_ISDIR == nError1) ||(osl::FileBase::E_ACCES == nError1));
2966 void copy_003()
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
2976 void copy_004()
2978 // copy $TEMP/tmpname to $TEMP/tmpdir/tmpname.
2979 auto nError1 = File::copy(aTmpName6, aTmpName4);
2981 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a not exist file",
2982 osl::FileBase::E_NOENT, nError1);
2985 void copy_005()
2987 // copy $TEMP/tmpname to $TEMP/system.path using system path.
2988 auto nError1 = File::copy(aTmpName6, aSysPath1);
2990 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for copy function: copy a file using system file path",
2991 osl::FileBase::E_INVAL, nError1);
2994 void copy_006()
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);
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);
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);
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();
3030 // testing the method
3031 // inline static RC move(const OUString& ustrSourceFileURL, const OUString& ustrDestFileURL)
3033 class move : public CppUnit::TestFixture
3035 public:
3036 void setUp() override
3038 // create a tempfile in $TEMP/tmpdir/tmpname.
3039 createTestDirectory(aTmpName3);
3040 createTestFile(aTmpName4);
3042 // write chars into the file.
3043 File testFile(aTmpName4);
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);
3054 void tearDown() override
3056 // remove the tempfile in $TEMP/tmpdir/tmpname.
3057 deleteTestFile(aTmpName4);
3058 deleteTestDirectory(aTmpName3);
3062 void move_001()
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);
3072 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: rename file to another directory",
3073 osl::FileBase::E_EXIST, nError2);
3076 void move_002()
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
3087 void move_003()
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
3097 void move_004()
3099 // move $TEMP/tmpname to $TEMP/tmpdir/tmpname.
3100 auto nError1 = File::move(aTmpName6, aTmpName4);
3102 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a not exist file",
3103 osl::FileBase::E_NOENT, nError1);
3106 void move_005()
3108 // move $TEMP/tmpname to $TEMP/system.path using system path.
3109 auto nError1 = File::move(aTmpName6, aSysPath1);
3111 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for move function: move a file using system file",
3112 osl::FileBase::E_INVAL, nError1);
3115 void move_006()
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);
3135 void move_007()
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);
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);
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();
3164 // testing the method
3165 // inline static RC remove(const OUString& ustrFileURL)
3167 class remove : public CppUnit::TestFixture
3169 public:
3170 void setUp() override
3172 // create a tempfile in $TEMP/tmpdir/tmpname.
3173 createTestDirectory(aTmpName3);
3174 createTestFile(aTmpName4);
3176 // write chars into the file.
3177 File testFile(aTmpName4);
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);
3188 void tearDown() override
3190 // remove the tempfile in $TEMP/tmpdir/tmpname.
3191 deleteTestFile(aTmpName4);
3192 deleteTestDirectory(aTmpName3);
3196 void remove_001()
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);
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));
3210 void remove_002()
3212 // remove $TEMP/tmpname.
3213 auto nError1 = File::remove(aTmpName6);
3215 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: remove a file not exist",
3216 osl::FileBase::E_NOENT, nError1);
3219 void remove_003()
3221 // remove $TEMP/system/path.
3222 auto nError1 = File::remove(aSysPath2);
3224 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: removing a file not using full qualified URL",
3225 osl::FileBase::E_INVAL, nError1);
3228 void remove_004()
3230 // remove $TEMP/tmpdir.
3231 auto nError1 = File::remove(aTmpName3);
3233 CPPUNIT_ASSERT_MESSAGE("test for remove function: remove a directory",
3234 (osl::FileBase::E_ISDIR == nError1) || (osl::FileBase::E_ACCES == nError1));
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();
3245 // testing the method
3246 // inline static RC setAttributes(const OUString& ustrFileURL, sal_uInt64 uAttributes)
3248 class setAttributes : public CppUnit::TestFixture
3250 private:
3251 DirectoryItem rItem;
3253 public:
3254 void setUp() override
3256 // create a tempfile in $TEMP/tmpdir/tmpname.
3257 createTestFile(aTmpName6);
3260 void tearDown() override
3262 // remove the tempfile in $TEMP/tmpdir/tmpname.
3263 deleteTestFile(aTmpName6);
3267 void setAttributes_001()
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);
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");
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
3307 void setAttributes_002()
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);
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);
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
3327 CPPUNIT_TEST_SUITE(setAttributes);
3328 CPPUNIT_TEST(setAttributes_001);
3329 CPPUNIT_TEST(setAttributes_002);
3330 CPPUNIT_TEST_SUITE_END();
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)
3340 class setTime : public CppUnit::TestFixture
3342 private:
3343 DirectoryItem rItem;
3345 public:
3346 void setUp() override
3348 // create a tempfile in $TEMP/tmpdir/tmpname.
3349 createTestFile(aTmpName6);
3352 void tearDown() override
3354 // remove the tempfile in $TEMP/tmpdir/tmpname.
3355 deleteTestFile(aTmpName6);
3359 void setTime_001()
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);
3370 // get current time
3371 bool bOk = osl_getSystemTime(pTV_current);
3372 CPPUNIT_ASSERT(bOk);
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);
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);
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();
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();
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();
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);
3413 CPPUNIT_TEST_SUITE(setTime);
3414 CPPUNIT_TEST(setTime_001);
3415 CPPUNIT_TEST_SUITE_END();
3418 // testing the method
3419 // inline static RC sync()
3421 class sync : public CppUnit::TestFixture
3423 private:
3424 DirectoryItem rItem;
3426 public:
3427 void setUp() override
3429 // create a tempfile in $TEMP/tmpdir/tmpname.
3430 createTestFile(aTmpName6);
3434 void tearDown() override
3436 // remove the tempfile in $TEMP/tmpdir/tmpname.
3437 deleteTestFile(aTmpName6);
3440 // test case: if The file is located on a read only file system.
3441 void sync_001()
3443 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3444 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3446 File tmp_file(aTmpName6);
3447 osl::FileBase::RC err = tmp_file.open(osl_File_OpenFlag_Write);
3449 CPPUNIT_ASSERT_EQUAL_MESSAGE("File open failed", osl::FileBase::E_None, err);
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);
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);
3460 nError2 = tmp_file.sync();
3462 CPPUNIT_ASSERT_EQUAL_MESSAGE("can not sync to readonly file!", osl::FileBase::E_None, nError2);
3464 tmp_file.close();
3466 // test case:no enough space, how to create such case???see test_cpy_wrt_file.cxx::test_osl_writeFile
3468 CPPUNIT_TEST_SUITE(sync);
3469 CPPUNIT_TEST(sync_001);
3470 CPPUNIT_TEST_SUITE_END();
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");
3490 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_File");
3493 // Beginning of the test cases for DirectoryItem class
3495 namespace osl_DirectoryItem
3497 // testing the method
3498 // DirectoryItem(): _pData(NULL)
3500 class ctors : public CppUnit::TestFixture
3502 public:
3503 void setUp() override
3505 // create a tempfile in $TEMP/tmpname.
3506 createTestFile(aTmpName6);
3509 void tearDown() override
3511 // remove the tempfile in $TEMP/tmpname.
3512 deleteTestFile(aTmpName6);
3515 void ctors_001()
3517 File testFile(aTmpName6);
3518 DirectoryItem rItem; // constructor
3520 // get the DirectoryItem.
3521 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3522 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
3528 CPPUNIT_TEST_SUITE(ctors);
3529 CPPUNIT_TEST(ctors_001);
3530 CPPUNIT_TEST_SUITE_END();
3533 // testing the method
3534 // DirectoryItem(const DirectoryItem& rItem): _pData(rItem._pData)
3536 class copy_assin_Ctors : public CppUnit::TestFixture
3538 public:
3539 void setUp() override
3541 // create a tempfile in $TEMP/tmpname.
3542 createTestFile(aTmpName6);
3545 void tearDown() override
3547 // remove the tempfile in $TEMP/tmpname.
3548 deleteTestFile(aTmpName6);
3552 void copy_assin_Ctors_001()
3554 DirectoryItem rItem; // constructor
3555 // get the DirectoryItem.
3556 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3557 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
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));
3568 void copy_assin_Ctors_002()
3570 DirectoryItem rItem; // constructor
3571 // get the DirectoryItem.
3572 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3573 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
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));
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();
3591 // testing the method
3592 // inline sal_Bool is()
3594 class is : public CppUnit::TestFixture
3596 public:
3597 void setUp() override
3599 // create a tempfile in $TEMP/tmpname.
3600 createTestFile(aTmpName6);
3603 void tearDown() override
3605 // remove the tempfile in $TEMP/tmpname.
3606 deleteTestFile(aTmpName6);
3609 void is_001()
3611 DirectoryItem rItem; // constructor
3613 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3614 !rItem.is());
3617 void is_002()
3619 DirectoryItem rItem; // constructor
3620 // get the DirectoryItem.
3621 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3622 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3624 CPPUNIT_ASSERT_MESSAGE("test for is function: use an uninitialized instance.",
3625 rItem.is());
3628 CPPUNIT_TEST_SUITE(is);
3629 CPPUNIT_TEST(is_001);
3630 CPPUNIT_TEST(is_002);
3631 CPPUNIT_TEST_SUITE_END();
3634 // testing the method
3635 // static inline RC get(const OUString& ustrFileURL, DirectoryItem& rItem)
3637 class get : public CppUnit::TestFixture
3639 public:
3640 void setUp() override
3642 // create a tempfile in $TEMP/tmpname.
3643 createTestFile(aTmpName6);
3646 void tearDown() override
3648 // remove the tempfile in $TEMP/tmpname.
3649 deleteTestFile(aTmpName6);
3653 void get_001()
3655 DirectoryItem rItem;
3656 auto nError2 = DirectoryItem::get(aTmpName6, rItem);
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);
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));
3669 void get_002()
3671 DirectoryItem rItem;
3672 auto nError1 = DirectoryItem::get(aSysPath1, rItem);
3674 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a system name instead of a URL.",
3675 osl::FileBase::E_INVAL, nError1);
3678 void get_003()
3680 DirectoryItem rItem;
3682 auto nError1 = DirectoryItem::get(aTmpName3, rItem);
3684 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for get function: use a non existed file URL.",
3685 osl::FileBase::E_NOENT, nError1);
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();
3695 // testing the method
3696 // inline RC getFileStatus(FileStatus& rStatus)
3698 class getFileStatus : public CppUnit::TestFixture
3700 public:
3701 void setUp() override
3703 // create a tempfile in $TEMP/tmpdir/tmpname.
3704 createTestDirectory(aTmpName3);
3705 createTestFile(aTmpName4);
3708 void tearDown() override
3710 // remove the tempfile in $TEMP/tmpdir/tmpname.
3711 deleteTestFile(aTmpName4);
3712 deleteTestDirectory(aTmpName3);
3716 void getFileStatus_001()
3718 DirectoryItem rItem;
3719 // get the DirectoryItem.
3720 auto nError1 = DirectoryItem::get(aTmpName4, rItem);
3721 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3723 // check the file name
3724 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3725 auto nError2 = rItem.getFileStatus(rFileStatus);
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));
3733 void getFileStatus_002()
3735 DirectoryItem rItem; // constructor
3736 // get the DirectoryItem.
3737 auto nError1 = DirectoryItem::get(aTmpName6, rItem);
3738 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_NOENT, nError1);
3740 // check the file name
3741 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3742 auto nError2 = rItem.getFileStatus(rFileStatus);
3744 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getFileStatus function: file not existed",
3745 osl::FileBase::E_INVAL, nError2);
3748 void getFileStatus_003()
3750 DirectoryItem rItem; // constructor
3751 // get the DirectoryItem.
3752 auto nError1 = DirectoryItem::get(aTmpName3, rItem);
3753 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
3755 // check the file name
3756 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
3757 auto nError2 = rItem.getFileStatus(rFileStatus);
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));
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();
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");
3778 CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_DirectoryItem");
3781 // Beginning of the test cases for Directory class
3783 namespace osl_Directory
3785 // testing the method
3786 // Directory(const OUString& strPath): _pData(0), _aPath(strPath)
3788 class ctors : public CppUnit::TestFixture
3790 public:
3791 void setUp() override
3793 // create a tempfile in $TEMP/tmpdir/tmpname.
3794 createTestDirectory(aTmpName3);
3795 createTestFile(aTmpName4);
3798 void tearDown() override
3800 // remove the tempfile in $TEMP/tmpdir/tmpname.
3801 deleteTestFile(aTmpName4);
3802 deleteTestDirectory(aTmpName3);
3803 // LLA: t_print("tearDown done.\n");
3807 void ctors_001()
3809 Directory testDirectory(aTmpName3); // constructor
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);
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);
3824 void ctors_002()
3826 Directory testDirectory(aTmpName9); // constructor
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);
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);
3841 CPPUNIT_TEST_SUITE(ctors);
3842 CPPUNIT_TEST(ctors_001);
3843 CPPUNIT_TEST(ctors_002);
3844 CPPUNIT_TEST_SUITE_END();
3847 // testing the method
3848 // inline RC open()
3850 class open : public CppUnit::TestFixture
3852 public:
3853 void setUp() override
3855 // create a tempfile in $TEMP/tmpdir/tmpname.
3856 createTestDirectory(aTmpName3);
3857 createTestFile(aTmpName4);
3860 void tearDown() override
3862 // remove the tempfile in $TEMP/tmpdir/tmpname.
3863 deleteTestFile(aTmpName4);
3864 deleteTestDirectory(aTmpName3);
3867 void open_001()
3869 Directory testDirectory(aTmpName3);
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();
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);
3886 void open_002()
3888 Directory testDirectory(aTmpName6);
3890 auto nError1 = testDirectory.open();
3891 if (nError1 == osl::FileBase::E_None)
3893 auto nError2 = testDirectory.close();
3894 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3897 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: open a file that is not existed",
3898 osl::FileBase::E_NOENT, nError1);
3901 void open_003()
3903 Directory testDirectory(aUserDirectorySys);
3905 auto nError1 = testDirectory.open();
3906 if (nError1 == osl::FileBase::E_None)
3908 auto nError2 = testDirectory.close();
3909 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
3912 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for open function: using system path",
3913 osl::FileBase::E_INVAL, nError1);
3916 void open_004()
3918 Directory testDirectory(aTmpName4);
3920 auto nError1 = testDirectory.open();
3921 if (nError1 == osl::FileBase::E_None)
3923 auto nError2 = testDirectory.close();
3924 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError2);
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));
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();
3939 // testing the method
3940 // inline sal_Bool isOpen() { return _pData != NULL; };
3942 class isOpen : public CppUnit::TestFixture
3944 public:
3945 void setUp() override
3947 // create a tempfile in $TEMP/tmpdir/tmpname.
3948 createTestDirectory(aTmpName3);
3949 createTestFile(aTmpName4);
3952 void tearDown() override
3954 // remove the tempfile in $TEMP/tmpdir/tmpname.
3955 deleteTestFile(aTmpName4);
3956 deleteTestDirectory(aTmpName3);
3960 void isOpen_001()
3962 Directory testDirectory(aTmpName3); // constructor
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);
3976 void isOpen_002()
3978 Directory testDirectory(aTmpName3); // constructor
3980 // check if directory is opened.
3981 bool bOk = testDirectory.isOpen();
3983 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: do not open a directory and check for open",
3984 !bOk);
3987 CPPUNIT_TEST_SUITE(isOpen);
3988 CPPUNIT_TEST(isOpen_001);
3989 CPPUNIT_TEST(isOpen_002);
3990 CPPUNIT_TEST_SUITE_END();
3993 // testing the method
3994 // inline RC close()
3996 class close : public CppUnit::TestFixture
3998 public:
3999 void setUp() override
4001 // create a tempdirectory : $TEMP/tmpdir.
4002 createTestDirectory(aTmpName3);
4005 void tearDown() override
4007 // remove a tempdirectory : $TEMP/tmpdir.
4008 deleteTestDirectory(aTmpName3);
4011 void close_001()
4013 Directory testDirectory(aTmpName3);
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();
4024 CPPUNIT_ASSERT_MESSAGE("test for isOpen function: close a directory and check for open",
4025 !bOk);
4028 void close_002()
4030 Directory testDirectory(aTmpName3);
4032 // close a directory
4033 auto nError1 = testDirectory.close();
4035 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for isOpen function: close a not opened directory",
4036 osl::FileBase::E_BADF, nError1);
4039 CPPUNIT_TEST_SUITE(close);
4040 CPPUNIT_TEST(close_001);
4041 CPPUNIT_TEST(close_002);
4042 CPPUNIT_TEST_SUITE_END();
4045 // testing the method
4046 // inline RC reset()
4048 class reset : public CppUnit::TestFixture
4050 private:
4051 DirectoryItem rItem;
4053 public:
4054 void setUp() override
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);
4064 void tearDown() override
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);
4075 void reset_001()
4077 Directory testDirectory(aTmpName3); // constructor
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);
4089 // get second Item
4090 // mindy: nError1 = testDirectory.getNextItem(rItem, 0);
4091 // mindy: CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
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);
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));
4117 void reset_002()
4119 Directory testDirectory(aTmpName6); // constructor
4121 // close a directory
4122 auto nError1 = testDirectory.reset();
4124 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: reset a non existed directory",
4125 osl::FileBase::E_NOENT, nError1);
4128 void reset_003()
4130 Directory testDirectory(aTmpName4); // constructor
4132 // close a directory
4133 auto nError1 = testDirectory.reset();
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));
4139 void reset_004()
4141 Directory testDirectory(aUserDirectorySys); // constructor
4143 // close a directory
4144 auto nError1 = testDirectory.reset();
4146 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for reset function: use a system path",
4147 osl::FileBase::E_INVAL, nError1);
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();
4158 // testing the method
4159 // inline RC getNextItem(DirectoryItem& rItem, sal_uInt32 nHint = 0)
4161 class getNextItem : public CppUnit::TestFixture
4163 private:
4164 DirectoryItem rItem;
4166 public:
4167 void setUp() override
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);
4178 void tearDown() override
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);
4189 void getNextItem_001()
4191 Directory testDirectory(aTmpName3); // constructor
4193 // open a directory
4194 auto nError1 = testDirectory.open();
4195 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4197 // check the file name
4198 bool bOk1 = false;
4199 bool bOk2 = false;
4200 bool bOk3 = false;
4201 FileStatus rFileStatus(osl_FileStatus_Mask_FileName);
4203 for (int nCount = 0; nCount < 3; nCount++)
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);
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))
4215 bOk1 = true;
4218 if (!bOk2 && compareFileName(rFileStatus.getFileName(), aTmpName2))
4220 bOk2 = true;
4223 if (!bOk3 && compareFileName(rFileStatus.getFileName(), aHidURL1))
4225 bOk3 = true;
4229 // close a directory
4230 nError1 = testDirectory.close();
4231 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
4241 void getNextItem_002()
4243 Directory testDirectory(aTmpName3); // constructor
4244 auto nError1 = testDirectory.getNextItem(rItem);
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);
4250 void getNextItem_003()
4252 Directory testDirectory(aTmpName3); // constructor
4254 // open a directory
4255 auto nError1 = testDirectory.open();
4256 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4258 osl::FileBase::RC nError2 = osl::FileBase::E_None;
4259 for (int nCount = 0; nCount < 4; nCount++)
4261 nError2 = testDirectory.getNextItem(rItem, 3);
4264 // close a directory
4265 nError1 = testDirectory.close();
4266 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
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);
4272 void getNextItem_004()
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;
4279 OUString aUStr_LnkFileSys(aTempDirectorySys), aUStr_SrcFileSys(aTempDirectorySys);
4280 aUStr_LnkFileSys += aSlashURL + "/tmpdir/link.file";
4281 aUStr_SrcFileSys += aSlashURL + "/tmpdir/tmpname";
4283 OString strLinkFileName, strSrcFileName;
4284 strLinkFileName = OUStringToOString(aUStr_LnkFileSys, RTL_TEXTENCODING_ASCII_US);
4285 strSrcFileName = OUStringToOString(aUStr_SrcFileSys, RTL_TEXTENCODING_ASCII_US);
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);
4292 // open a directory
4293 auto nError1 = testDirectory.open();
4294 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1);
4295 OUString aFileName ("link.file");
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))
4304 bFoundOK = true;
4305 if (rFileStatus.getFileType() == FileStatus::Link)
4307 bLnkOK = true;
4308 break;
4312 else
4313 break;
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
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();
4332 // testing the method
4333 // inline static RC getVolumeInfo(const OUString& ustrDirectoryURL, VolumeInfo& rInfo)
4335 class getVolumeInfo : public CppUnit::TestFixture
4337 public:
4338 void checkValidMask(osl::VolumeInfo const& _aVolumeInfo, sal_Int32 _nMask)
4340 if (_nMask == osl_VolumeInfo_Mask_FileSystemName)
4342 // get file system name
4343 OUString aFileSysName = _aVolumeInfo.getFileSystemName();
4345 bool bRes2 = compareFileName(aFileSysName, aNullURL);
4346 CPPUNIT_ASSERT_MESSAGE("test for getVolumeInfo function: getVolumeInfo of root directory.",
4347 !bRes2);
4350 if (_nMask == osl_VolumeInfo_Mask_Attributes)
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();
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";
4367 printf("Attributes: %s\n", sAttr.getStr());
4369 if (_nMask == osl_VolumeInfo_Mask_TotalSpace)
4371 // within Linux, df / * 1024 bytes is the result
4372 sal_uInt64 nSize = _aVolumeInfo.getTotalSpace();
4373 printf("Total space: %" SAL_PRIuUINT64 "\n", nSize);
4375 if (_nMask == osl_VolumeInfo_Mask_UsedSpace)
4377 sal_uInt64 nSize = _aVolumeInfo.getUsedSpace();
4378 printf(" Used space: %" SAL_PRIuUINT64 "\n", nSize);
4380 if (_nMask == osl_VolumeInfo_Mask_FreeSpace)
4382 sal_uInt64 nSize = _aVolumeInfo.getFreeSpace();
4383 printf(" Free space: %" SAL_PRIuUINT64 "\n", nSize);
4385 if (_nMask == osl_VolumeInfo_Mask_MaxNameLength)
4387 sal_uInt32 nLength = _aVolumeInfo.getMaxNameLength();
4388 printf("max name length: %" SAL_PRIuUINT32 "\n", nLength);
4390 if (_nMask == osl_VolumeInfo_Mask_MaxPathLength)
4392 sal_uInt32 nLength = _aVolumeInfo.getMaxPathLength();
4393 printf("max path length: %" SAL_PRIuUINT32 "\n", nLength);
4395 if (_nMask == osl_VolumeInfo_Mask_FileSystemCaseHandling)
4397 bool bIsCase = _aVolumeInfo.isCaseSensitiveFileSystem();
4398 printf("filesystem case sensitive: %s\n", bIsCase ? "yes" : "no");
4402 void checkVolumeInfo(sal_Int32 _nMask)
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);
4416 void getVolumeInfo_001_1()
4418 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4419 checkVolumeInfo(mask);
4422 void getVolumeInfo_001_2()
4424 sal_Int32 mask = osl_VolumeInfo_Mask_Attributes;
4425 checkVolumeInfo(mask);
4428 void getVolumeInfo_001_3()
4430 sal_Int32 mask = osl_VolumeInfo_Mask_TotalSpace;
4431 checkVolumeInfo(mask);
4434 void getVolumeInfo_001_4()
4436 sal_Int32 mask = osl_VolumeInfo_Mask_UsedSpace;
4437 checkVolumeInfo(mask);
4440 void getVolumeInfo_001_5()
4442 sal_Int32 mask = osl_VolumeInfo_Mask_FreeSpace;
4443 checkVolumeInfo(mask);
4446 void getVolumeInfo_001_6()
4448 sal_Int32 mask = osl_VolumeInfo_Mask_MaxNameLength;
4449 checkVolumeInfo(mask);
4452 void getVolumeInfo_001_7()
4454 sal_Int32 mask = osl_VolumeInfo_Mask_MaxPathLength;
4455 checkVolumeInfo(mask);
4458 void getVolumeInfo_001_8()
4460 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemCaseHandling;
4461 checkVolumeInfo(mask);
4464 void getVolumeInfo_002()
4466 sal_Int32 mask = osl_VolumeInfo_Mask_FileSystemName;
4467 VolumeInfo aVolumeInfo(mask);
4468 // call getVolumeInfo here
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);
4474 nError1 = Directory::getVolumeInfo(aRootSys, aVolumeInfo);
4476 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: use system path as parameter.",
4477 osl::FileBase::E_INVAL, nError1);
4480 void getVolumeInfo_003()
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);
4489 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for getVolumeInfo function: non-existence test. ",
4490 osl::FileBase::E_NOENT, nError1);
4491 #endif
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();
4508 // testing the method
4509 // inline static RC create(const OUString& ustrDirectoryURL)
4511 class create : public CppUnit::TestFixture
4513 public:
4514 void create_001()
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);
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);
4529 void create_002()
4531 #if !defined(_WIN32) && !defined(MACOSX) && defined(SAL_UNX)
4532 if (geteuid() == 0) // don't test if building as root
4533 return;
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);
4539 nError1 = File::remove(aTmpDir);
4540 CPPUNIT_ASSERT_EQUAL_MESSAGE("temp File removal failed", osl::FileBase::E_None, nError1);
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
4549 // Shouldn't be possible now to create a dir underneath it
4550 OUString aTmpSubLevel = aTmpDir + "/notallowedhere";
4551 nError1 = Directory::create(aTmpSubLevel);
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
4566 void create_003()
4568 // create directory in /tmpname
4569 auto nError1 = Directory::create(aSysPath1);
4571 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for create function: create a directory using system path.",
4572 osl::FileBase::E_INVAL, nError1);
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();
4582 // testing the method
4583 // inline static RC remove(const OUString& ustrDirectoryURL)
4585 class remove : public CppUnit::TestFixture
4587 public:
4588 void remove_001()
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();
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);
4605 void remove_002()
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();
4616 if (nError2 != osl::FileBase::E_NOENT)
4617 Directory::remove(aTmpName3);
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);
4623 void remove_003()
4625 // try to remove a non-existed directory
4626 auto nError1 = Directory::remove(aTmpName6);
4628 CPPUNIT_ASSERT_EQUAL_MESSAGE("test for remove function: try to remove a non-existed directory.",
4629 osl::FileBase::E_NOENT, nError1);
4632 void remove_004()
4634 createTestFile(aTmpName6);
4635 bool bExist = ifFileExist(aTmpName6);
4636 // try to remove file.
4637 auto nError1 = Directory::remove(aTmpName6);
4638 deleteTestFile(aTmpName6);
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));
4646 void remove_005()
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
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();
4674 // TEST Directory::createPath
4676 #ifdef _WIN32
4677 # define PATH_BUFFER_SIZE MAX_PATH
4678 #else
4679 # define PATH_BUFFER_SIZE PATH_MAX
4680 #endif
4682 #define TEST_PATH_POSTFIX "hello/world"
4684 static OUString const & get_test_path()
4686 static OUString test_path = []()
4688 OUString tmp;
4689 osl::FileBase::RC rc = osl::FileBase::getTempDirURL(tmp);
4691 CPPUNIT_ASSERT_EQUAL_MESSAGE
4693 "Getting the location of TMP dir failed",
4694 osl::FileBase::E_None, rc
4697 OUString system_path;
4698 rc = osl::FileBase::getSystemPathFromFileURL(tmp, system_path);
4700 CPPUNIT_ASSERT_EQUAL_MESSAGE
4702 "Cannot convert the TMP dir to system path",
4703 osl::FileBase::E_None, rc
4706 OString tmp_x(OUStringToOString(system_path, RTL_TEXTENCODING_UTF8));
4707 if (tmp_x.lastIndexOf('/') != (tmp_x.getLength() - 1))
4708 tmp_x += "/";
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()));
4715 CPPUNIT_ASSERT_MESSAGE
4717 "mkdtemp call failed",
4718 out != nullptr
4721 tmp_x += "/";
4722 #endif
4723 tmp_x += TEST_PATH_POSTFIX;
4725 OUString tmpTestPath;
4726 rc = osl::FileBase::getFileURLFromSystemPath(OStringToOUString(tmp_x, RTL_TEXTENCODING_UTF8), tmpTestPath);
4728 CPPUNIT_ASSERT_EQUAL_MESSAGE
4730 "Cannot convert the system path back to a URL",
4731 osl::FileBase::E_None, rc
4733 return tmpTestPath;
4734 }();
4735 return test_path;
4738 static void rm_test_path(const OUString& path)
4740 sal_Unicode buffer[PATH_BUFFER_SIZE];
4741 memcpy(buffer, path.getStr(), (path.getLength() + 1) * sizeof(sal_Unicode));
4743 sal_Int32 i = rtl_ustr_lastIndexOfChar(buffer, '/');
4744 if (i == path.getLength())
4745 buffer[i] = 0;
4747 Directory::remove(OUString(buffer));
4749 i = rtl_ustr_lastIndexOfChar(buffer, '/');
4750 assert(i != -1);
4751 if (i != -1)
4753 buffer[i] = 0;
4754 Directory::remove(OUString(buffer));
4758 namespace {
4760 class DirCreatedObserver : public DirectoryCreationObserver
4762 public:
4763 DirCreatedObserver() : i(0) {}
4764 virtual void DirectoryCreated(const OUString&) override { i++; };
4766 int number_of_dirs_created() const { return i; }
4768 private:
4769 int i;
4774 class createPath : public CppUnit::TestFixture
4776 public:
4777 createPath()
4780 void with_relative_path()
4782 osl::FileBase::RC rc = Directory::createPath(TEST_PATH_POSTFIX);
4784 CPPUNIT_ASSERT_EQUAL_MESSAGE
4786 "osl_createDirectoryPath contract broken",
4787 osl::FileBase::E_INVAL, rc
4791 void without_callback()
4793 OUString tp_url = get_test_path();
4795 rm_test_path(tp_url);
4797 osl::FileBase::RC rc = Directory::createPath(tp_url);
4799 rm_test_path(tp_url);
4801 CPPUNIT_ASSERT_EQUAL_MESSAGE
4803 "osl_createDirectoryPath failed",
4804 osl::FileBase::E_None, rc
4808 void with_callback()
4810 OUString tp_url = get_test_path();
4812 rm_test_path(tp_url);
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;
4819 rm_test_path(tp_url);
4821 CPPUNIT_ASSERT_EQUAL_MESSAGE
4823 "osl_createDirectoryPath failed",
4824 osl::FileBase::E_None, rc
4826 CPPUNIT_ASSERT_MESSAGE
4828 "osl_createDirectoryPath failed",
4829 nDirs > 0
4834 #ifdef _WIN32
4836 const char* get_unused_drive_letter()
4838 static const char m_aBuff[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
4840 DWORD ld = GetLogicalDrives();
4841 DWORD i = 4;
4842 DWORD j = 2;
4844 while ((ld & i) && (i > 1))
4845 { i = i << 1; j++; }
4847 if (i > 2)
4848 return m_aBuff + j;
4850 return nullptr;
4853 void at_invalid_logical_drive()
4855 const char* drv = get_unused_drive_letter();
4856 char buff[PATH_BUFFER_SIZE];
4857 memset(buff, 0, sizeof(buff));
4859 strncpy(buff, drv, 1);
4860 strcat(buff, ":\\");
4861 strcat(buff, TEST_PATH_POSTFIX);
4863 OUString path = OUString::createFromAscii(buff);
4864 OUString tp_url;
4865 osl::FileBase::getFileURLFromSystemPath(path, tp_url);
4867 osl::FileBase::RC rc = Directory::createPath(tp_url);
4869 CPPUNIT_ASSERT_MESSAGE
4871 "osl_createDirectoryPath doesn't fail on unused logical drive letters",
4872 rc != osl::FileBase::E_None
4875 #endif /* _WIN32 */
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();
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);
4900 #if 0
4901 #if defined UNX
4902 /** get Current PID.
4904 OUString getCurrentPID()
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);
4915 #endif
4916 #endif
4918 namespace {
4920 //~ do some clean up work after all test completed.
4921 class GlobalObject
4923 public:
4924 ~GlobalObject()
4928 //~ special clean up task in Windows and Unix separately;
4929 #if (defined UNX)
4930 //~ some clean up task for UNIX OS
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);
4946 OUString aUStr(aUserDirectoryURL);
4947 concatURL(aUStr, aHidURL1);
4948 if (ifFileExist(aUStr))
4949 deleteTestFile(aUStr);
4951 OUString aUStr1(aRootURL);
4952 concatURL(aUStr1, aTmpName2);
4953 if (ifFileExist(aUStr1))
4954 deleteTestFile(aUStr1);
4955 #endif
4957 catch (const CppUnit::Exception &e)
4959 printf("Exception caught in GlobalObject dtor(). Exception message: '%s'. Source line: %d\n", e.what(), e.sourceLine().lineNumber());
4961 catch (...)
4963 printf("Exception caught (...) in GlobalObject dtor()\n");
4970 static GlobalObject theGlobalObject;
4972 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */