| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
| 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 |
|
| 10 |
#include <sal/config.h> |
| 11 |
|
| 12 |
#include <cppunit/plugin/TestPlugIn.h> |
| 13 |
|
| 14 |
#if (defined UNX) |
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
|
| 18 |
#include <cppunit/TestAssert.h> |
| 19 |
#include <cppunit/TestFixture.h> |
| 20 |
#include <cppunit/extensions/HelperMacros.h> |
| 21 |
|
| 22 |
#include <osl/file.hxx> |
| 23 |
#include <rtl/ustring.hxx> |
| 24 |
#include <sal/types.h> |
| 25 |
|
| 26 |
#include <setallowedpaths.hxx> |
| 27 |
|
| 28 |
#include "../../../osl/unx/file_impl.hxx" |
| 29 |
|
| 30 |
using namespace osl; |
| 31 |
|
| 32 |
namespace osl::qa { |
| 33 |
|
| 34 |
static bool isForbidden(OUString const & path, sal_uInt32 flags) { |
| 35 |
OString utf8; |
| 36 |
auto const ok = path.convertToString(&utf8, RTL_TEXTENCODING_UTF8, RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR); |
| 37 |
CPPUNIT_ASSERT(ok); |
| 38 |
return ::isForbidden(utf8, flags); |
| 39 |
} |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
namespace osl_Forbidden |
| 44 |
{ |
| 45 |
|
| 46 |
class Forbidden : public CppUnit::TestFixture |
| 47 |
{ |
| 48 |
OUString maScratchBad; |
| 49 |
OUString maScratchGood; |
| 50 |
public: |
| 51 |
void setUp() override |
| 52 |
{ |
| 53 |
// create a directory to play in |
| 54 |
createTestDirectory(aTmpName3); |
| 55 |
OUString aBadURL = aTmpName3 + "/bad"; |
| 56 |
OUString aGoodURL = aTmpName3 + "/good"; |
| 57 |
createTestDirectory(aBadURL); |
| 58 |
createTestDirectory(aGoodURL); |
| 59 |
File::getSystemPathFromFileURL(aBadURL, maScratchBad); |
| 60 |
File::getSystemPathFromFileURL(aGoodURL, maScratchGood); |
| 61 |
} |
| 62 |
|
| 63 |
void tearDown() override |
| 64 |
{ |
| 65 |
setAllowedPaths(u""); |
| 66 |
OUString aBadURL = aTmpName3 + "/bad"; |
| 67 |
OUString aGoodURL = aTmpName3 + "/good"; |
| 68 |
deleteTestDirectory(aBadURL); |
| 69 |
deleteTestDirectory(aGoodURL); |
| 70 |
deleteTestDirectory(aTmpName3); |
| 71 |
} |
| 72 |
|
| 73 |
void forbidden() |
| 74 |
{ |
| 75 |
setAllowedPaths(maScratchGood); |
| 76 |
|
| 77 |
// check some corner cases first |
| 78 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read bad should be forbidden", |
| 79 |
true, osl::qa::isForbidden(".", osl_File_OpenFlag_Read)); |
| 80 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read bad should be forbidden", |
| 81 |
true, osl::qa::isForbidden("", osl_File_OpenFlag_Read)); |
| 82 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read bad should be forbidden", |
| 83 |
true, osl::qa::isForbidden("a", osl_File_OpenFlag_Read)); |
| 84 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read bad should be forbidden", |
| 85 |
true, osl::qa::isForbidden("/", osl_File_OpenFlag_Read)); |
| 86 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read from non-existent should be allowed", |
| 87 |
false, osl::qa::isForbidden(maScratchGood + "/notthere/file", osl_File_OpenFlag_Read)); |
| 88 |
|
| 89 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read bad should be forbidden", |
| 90 |
true, osl::qa::isForbidden(maScratchBad, osl_File_OpenFlag_Read)); |
| 91 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read from good should be allowed", |
| 92 |
false, osl::qa::isForbidden(maScratchGood, osl_File_OpenFlag_Read)); |
| 93 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to good should be forbidden", |
| 94 |
true, osl::qa::isForbidden(maScratchGood, osl_File_OpenFlag_Write)); |
| 95 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("create in good should be forbidden", |
| 96 |
true, osl::qa::isForbidden(maScratchGood, osl_File_OpenFlag_Create)); |
| 97 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("exec from good should be forbidden", |
| 98 |
true, osl::qa::isForbidden(maScratchGood, 0x80)); |
| 99 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to non-existent should be forbidden", |
| 100 |
true, osl::qa::isForbidden(maScratchBad + "/notthere", osl_File_OpenFlag_Write)); |
| 101 |
|
| 102 |
setAllowedPaths(Concat2View("w:" + maScratchGood + ":x:" + maScratchBad)); |
| 103 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read bad should be forbidden", |
| 104 |
true, osl::qa::isForbidden(maScratchBad, osl_File_OpenFlag_Read)); |
| 105 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("read from good should be allowed", // w implies 'r' |
| 106 |
false, osl::qa::isForbidden(maScratchGood, osl_File_OpenFlag_Read)); |
| 107 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to good should be allowed", |
| 108 |
false, osl::qa::isForbidden(maScratchGood, osl_File_OpenFlag_Write)); |
| 109 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("exec from good should be forbidden", |
| 110 |
true, osl::qa::isForbidden(maScratchGood, 0x80)); |
| 111 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("exec from bad should be allowed", |
| 112 |
false, osl::qa::isForbidden(maScratchBad, 0x80)); |
| 113 |
|
| 114 |
setAllowedPaths(Concat2View(":r:" + maScratchBad)); |
| 115 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to non-existent should be forbidden", |
| 116 |
true, osl::qa::isForbidden(maScratchGood + "/notthere", osl_File_OpenFlag_Write)); |
| 117 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to non-existent should be forbidden 2", |
| 118 |
true, osl::qa::isForbidden(maScratchGood + "/notthere/file", osl_File_OpenFlag_Write)); |
| 119 |
|
| 120 |
setAllowedPaths(Concat2View(":r:" + maScratchBad + ":w:" + maScratchGood + "/notthere")); |
| 121 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to non-existent should be allowed", |
| 122 |
false, osl::qa::isForbidden(maScratchGood + "/notthere", osl_File_OpenFlag_Write)); |
| 123 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("write to non-existent should be allowed 2", |
| 124 |
false, osl::qa::isForbidden(maScratchGood + "/notthere/file", osl_File_OpenFlag_Write)); |
| 125 |
} |
| 126 |
|
| 127 |
/* |
| 128 |
void open() |
| 129 |
{ |
| 130 |
setAllowedPaths(maScratchGood); |
| 131 |
File testFile(maScratchBad + "/open"); |
| 132 |
auto nError1 = testFile.open(osl_File_OpenFlag_Read | osl_File_OpenFlag_Write); |
| 133 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("disabled path allowed", osl::FileBase::E_ACCES, nError1); |
| 134 |
deleteTestFile(testFile.getURL()); |
| 135 |
} |
| 136 |
|
| 137 |
void copy() |
| 138 |
{ |
| 139 |
setAllowedPaths("w:" + maScratchGood); |
| 140 |
File testGood(maScratchGood + "/good"); |
| 141 |
File testGoodTo(maScratchGood + "/good_to"); |
| 142 |
File testBad(maScratchBad + "/bad"); |
| 143 |
|
| 144 |
auto nError1 = testGood.open(osl_File_OpenFlag_Create); |
| 145 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nError1); |
| 146 |
|
| 147 |
auto nErrorCopy = File::copy(maScratchGood + "/good", maScratchGood + "/good_to"); |
| 148 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, nErrorCopy); |
| 149 |
|
| 150 |
auto nErrorCopyBad = File::copy(maScratchGood + "/good_to", maScratchBad + "/bad"); |
| 151 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_ACCES, nErrorCopyBad); |
| 152 |
|
| 153 |
deleteTestFile(maScratchGood + "/good_to"); |
| 154 |
deleteTestFile(maScratchGood + "/good"); |
| 155 |
} |
| 156 |
|
| 157 |
void nextTests() |
| 158 |
{ |
| 159 |
// more entry points to test |
| 160 |
#if 0 |
| 161 |
auto nError1 = File::move(aTmpName4, aCanURL1); |
| 162 |
auto nError2 = File::remove(aTmpName4); |
| 163 |
auto nError3 = File::setAttributes(aTmpName6, osl_File_Attribute_ReadOnly); |
| 164 |
bool bOk = osl_getSystemTime(pTV_current); |
| 165 |
CPPUNIT_ASSERT(bOk); |
| 166 |
auto nError4 = File::setTime(aTmpName6, *pTV_current, *pTV_current, *pTV_current); |
| 167 |
CPPUNIT_ASSERT_EQUAL_MESSAGE(errorToStr(nError2).getStr(), osl::FileBase::E_None, nError2); |
| 168 |
#endif |
| 169 |
} |
| 170 |
*/ |
| 171 |
CPPUNIT_TEST_SUITE(Forbidden); |
| 172 |
CPPUNIT_TEST(forbidden); |
| 173 |
// CPPUNIT_TEST(open); |
| 174 |
// CPPUNIT_TEST(copy); |
| 175 |
// CPPUNIT_TEST(nextTests); |
| 176 |
CPPUNIT_TEST_SUITE_END(); |
| 177 |
|
| 178 |
//TODO: The following parts are mainly copied from sal/qa/osl/file/osl_File.cxx and |
| 179 |
// sal/qa/osl/file/osl_File_Const.h (but the latter cannot be included directly, as its use of |
| 180 |
// static C++ objects would cause issues with the use of gb_CppunitTest_use_library_objects in |
| 181 |
// this test's sal/CppunitTest_sal_osl_file_details.mk): |
| 182 |
private: |
| 183 |
static OUString getTempDirectoryURL_() |
| 184 |
{ |
| 185 |
OUString aDir; |
| 186 |
CPPUNIT_ASSERT_EQUAL_MESSAGE("couldn't get system temp URL", |
| 187 |
osl::FileBase::E_None, osl::FileBase::getTempDirURL(aDir)); |
| 188 |
// This resolves symlinks in the temp path if any |
| 189 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, |
| 190 |
osl::FileBase::getAbsoluteFileURL(aDir, aDir, aDir)); |
| 191 |
return aDir; |
| 192 |
} |
| 193 |
|
| 194 |
OUString aTempDirectoryURL; |
| 195 |
|
| 196 |
OUString aTmpName3; |
| 197 |
|
| 198 |
/** simple version to judge if a file name or directory name is a URL or a system path, just to see if it |
| 199 |
is start with "file:///";. |
| 200 |
*/ |
| 201 |
static bool isURL(const OUString& pathname) |
| 202 |
{ |
| 203 |
return pathname.startsWith(u"file:///"); |
| 204 |
} |
| 205 |
|
| 206 |
/** create a temp test directory using OUString name of full qualified URL or system path. |
| 207 |
*/ |
| 208 |
static void createTestDirectory(const OUString& dirname) |
| 209 |
{ |
| 210 |
OUString aPathURL = dirname.copy(0); |
| 211 |
osl::FileBase::RC nError; |
| 212 |
|
| 213 |
if (!isURL(dirname)) |
| 214 |
osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL |
| 215 |
nError = Directory::create(aPathURL); |
| 216 |
if ((nError != osl::FileBase::E_None) && (nError != osl::FileBase::E_EXIST)) |
| 217 |
printf("createTestDirectory failed: %d!\n", int(nError)); |
| 218 |
} |
| 219 |
|
| 220 |
/** delete a temp test directory using OUString name of full qualified URL or system path. |
| 221 |
*/ |
| 222 |
static void deleteTestDirectory(const OUString& dirname) |
| 223 |
{ |
| 224 |
OUString aPathURL = dirname.copy(0); |
| 225 |
if (!isURL(dirname)) |
| 226 |
osl::FileBase::getFileURLFromSystemPath(dirname, aPathURL); // convert if not full qualified URL |
| 227 |
|
| 228 |
Directory testDir(aPathURL); |
| 229 |
if (testDir.isOpen()) |
| 230 |
testDir.close(); // close if still open. |
| 231 |
|
| 232 |
osl::FileBase::RC nError = Directory::remove(aPathURL); |
| 233 |
|
| 234 |
OString strError = "In deleteTestDirectory function: remove Directory " + |
| 235 |
OUStringToOString(aPathURL, RTL_TEXTENCODING_ASCII_US) + " -> result: " + OString::number(nError); |
| 236 |
CPPUNIT_ASSERT_MESSAGE(strError.getStr(), (osl::FileBase::E_None == nError) || (nError == osl::FileBase::E_NOENT)); |
| 237 |
} |
| 238 |
|
| 239 |
public: |
| 240 |
Forbidden(): |
| 241 |
aTempDirectoryURL(getTempDirectoryURL_()), |
| 242 |
aTmpName3( aTempDirectoryURL + "/tmpdir-forbidden" ) |
| 243 |
{} |
| 244 |
}; |
| 245 |
|
| 246 |
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(osl_Forbidden::Forbidden, "osl_Forbidden"); |
| 247 |
|
| 248 |
CPPUNIT_REGISTRY_ADD_TO_DEFAULT("osl_Forbidden"); |
| 249 |
} |
| 250 |
#endif |
| 251 |
|
| 252 |
CPPUNIT_PLUGIN_IMPLEMENT(); |
| 253 |
|
| 254 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |