Debian Codegraph @

sal/qa/osl/file/osl_File.cxx

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