| 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/config.h> |
| 21 |
|
| 22 |
#include <cppunit/TestAssert.h> |
| 23 |
#include <cppunit/TestFixture.h> |
| 24 |
#include <cppunit/extensions/HelperMacros.h> |
| 25 |
|
| 26 |
#include <osl/file.hxx> |
| 27 |
|
| 28 |
#if defined(_WIN32) |
| 29 |
#define MY_PATH_IN "/c:/foo/bar" |
| 30 |
#define MY_PATH_OUT "c:\\foo\\bar" |
| 31 |
#define MY_PATH_OUT_CONT MY_PATH_OUT "\\" |
| 32 |
#define MY_PATH_OUT_REL "foo\\bar" |
| 33 |
#else |
| 34 |
#define MY_PATH_IN "/foo/bar" |
| 35 |
#define MY_PATH_OUT MY_PATH_IN |
| 36 |
#define MY_PATH_OUT_CONT MY_PATH_OUT "/" |
| 37 |
#define MY_PATH_OUT_REL "foo/bar" |
| 38 |
#endif |
| 39 |
|
| 40 |
namespace { |
| 41 |
|
| 42 |
class Test: public CppUnit::TestFixture { |
| 43 |
private: |
| 44 |
CPPUNIT_TEST_SUITE(Test); |
| 45 |
CPPUNIT_TEST(testBadScheme); |
| 46 |
CPPUNIT_TEST(testNoScheme); |
| 47 |
CPPUNIT_TEST(testBadAuthority); |
| 48 |
CPPUNIT_TEST(testLocalhost1Authority); |
| 49 |
CPPUNIT_TEST(testLocalhost2Authority); |
| 50 |
CPPUNIT_TEST(testLocalhost3Authority); |
| 51 |
CPPUNIT_TEST(testNoAuthority); |
| 52 |
CPPUNIT_TEST(testEmptyPath); |
| 53 |
CPPUNIT_TEST(testHomeAbbreviation); |
| 54 |
CPPUNIT_TEST(testOtherHomeAbbreviation); |
| 55 |
CPPUNIT_TEST(testRelative); |
| 56 |
CPPUNIT_TEST(testEscape); |
| 57 |
CPPUNIT_TEST(testBadEscape2f); |
| 58 |
CPPUNIT_TEST(testBadEscape2F); |
| 59 |
CPPUNIT_TEST(testBad0); |
| 60 |
CPPUNIT_TEST(testBadEscape0); |
| 61 |
CPPUNIT_TEST(testBadQuery); |
| 62 |
CPPUNIT_TEST(testBadFragment); |
| 63 |
CPPUNIT_TEST_SUITE_END(); |
| 64 |
|
| 65 |
void testBadScheme(); |
| 66 |
void testNoScheme(); |
| 67 |
void testBadAuthority(); |
| 68 |
void testLocalhost1Authority(); |
| 69 |
void testLocalhost2Authority(); |
| 70 |
void testLocalhost3Authority(); |
| 71 |
void testNoAuthority(); |
| 72 |
void testEmptyPath(); |
| 73 |
void testHomeAbbreviation(); |
| 74 |
void testOtherHomeAbbreviation(); |
| 75 |
void testRelative(); |
| 76 |
void testEscape(); |
| 77 |
void testBadEscape2f(); |
| 78 |
void testBadEscape2F(); |
| 79 |
void testBad0(); |
| 80 |
void testBadEscape0(); |
| 81 |
void testBadQuery(); |
| 82 |
void testBadFragment(); |
| 83 |
}; |
| 84 |
|
| 85 |
void Test::testBadScheme() { |
| 86 |
OUString p; |
| 87 |
auto e = osl::FileBase::getSystemPathFromFileURL(u"foo:bar"_ustr, p); |
| 88 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 89 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 90 |
} |
| 91 |
|
| 92 |
void Test::testNoScheme() { |
| 93 |
#if !defined(_WIN32) //TODO |
| 94 |
OUString p; |
| 95 |
auto e = osl::FileBase::getSystemPathFromFileURL(u"//" MY_PATH_IN ""_ustr, p); |
| 96 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 97 |
CPPUNIT_ASSERT_EQUAL(u"" MY_PATH_OUT ""_ustr, p); |
| 98 |
#endif |
| 99 |
} |
| 100 |
|
| 101 |
void Test::testBadAuthority() { |
| 102 |
#if defined UNX |
| 103 |
OUString p; |
| 104 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 105 |
u"file://baz" MY_PATH_IN ""_ustr, p); |
| 106 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 107 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 108 |
#endif |
| 109 |
} |
| 110 |
|
| 111 |
void Test::testLocalhost1Authority() { |
| 112 |
OUString p; |
| 113 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 114 |
u"file://localhost" MY_PATH_IN ""_ustr, p); |
| 115 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 116 |
CPPUNIT_ASSERT_EQUAL(u"" MY_PATH_OUT ""_ustr, p); |
| 117 |
} |
| 118 |
|
| 119 |
void Test::testLocalhost2Authority() { |
| 120 |
OUString p; |
| 121 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 122 |
u"file://LOCALHOST" MY_PATH_IN ""_ustr, p); |
| 123 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 124 |
CPPUNIT_ASSERT_EQUAL(u"" MY_PATH_OUT ""_ustr, p); |
| 125 |
} |
| 126 |
|
| 127 |
void Test::testLocalhost3Authority() { |
| 128 |
OUString p; |
| 129 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 130 |
u"file://127.0.0.1" MY_PATH_IN ""_ustr, p); |
| 131 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 132 |
CPPUNIT_ASSERT_EQUAL(u"" MY_PATH_OUT ""_ustr, p); |
| 133 |
} |
| 134 |
|
| 135 |
void Test::testNoAuthority() { |
| 136 |
OUString p; |
| 137 |
auto e = osl::FileBase::getSystemPathFromFileURL(u"file:" MY_PATH_IN ""_ustr, p); |
| 138 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 139 |
CPPUNIT_ASSERT_EQUAL(u"" MY_PATH_OUT ""_ustr, p); |
| 140 |
} |
| 141 |
|
| 142 |
void Test::testEmptyPath() { |
| 143 |
#if defined UNX |
| 144 |
OUString p; |
| 145 |
auto e = osl::FileBase::getSystemPathFromFileURL(u"file://"_ustr, p); |
| 146 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 147 |
CPPUNIT_ASSERT_EQUAL(u"/"_ustr, p); |
| 148 |
#endif |
| 149 |
} |
| 150 |
|
| 151 |
void Test::testHomeAbbreviation() { |
| 152 |
#if defined UNX |
| 153 |
OUString p; |
| 154 |
auto e = osl::FileBase::getSystemPathFromFileURL(u"file:///~"_ustr, p); |
| 155 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 156 |
// could theoretically fail due to osl::Security::getHomeDir problem |
| 157 |
e = osl::FileBase::getSystemPathFromFileURL(u"file:///~/foo%2525/bar"_ustr, p); |
| 158 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 159 |
// could theoretically fail due to osl::Security::getHomeDir problem |
| 160 |
CPPUNIT_ASSERT(p.endsWith("/foo%25/bar")); |
| 161 |
#endif |
| 162 |
} |
| 163 |
|
| 164 |
void Test::testOtherHomeAbbreviation() { |
| 165 |
#if defined UNX |
| 166 |
OUString p; |
| 167 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 168 |
u"file:///~baz" MY_PATH_IN ""_ustr, p); |
| 169 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); // not supported for now |
| 170 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 171 |
#endif |
| 172 |
} |
| 173 |
|
| 174 |
void Test::testRelative() { |
| 175 |
OUString p; |
| 176 |
auto e = osl::FileBase::getSystemPathFromFileURL(u"foo/bar"_ustr, p); |
| 177 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 178 |
CPPUNIT_ASSERT(p.endsWith(MY_PATH_OUT_REL)); |
| 179 |
} |
| 180 |
|
| 181 |
void Test::testEscape() { |
| 182 |
OUString p; |
| 183 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 184 |
u"file://" MY_PATH_IN "/b%61z"_ustr, p); |
| 185 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e); |
| 186 |
CPPUNIT_ASSERT_EQUAL(u"" MY_PATH_OUT_CONT "baz" ""_ustr, p); |
| 187 |
} |
| 188 |
|
| 189 |
void Test::testBadEscape2f() { |
| 190 |
OUString p; |
| 191 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 192 |
u"file://" MY_PATH_IN "/b%2fz"_ustr, p); |
| 193 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 194 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 195 |
} |
| 196 |
|
| 197 |
void Test::testBadEscape2F() { |
| 198 |
OUString p; |
| 199 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 200 |
u"file://" MY_PATH_IN "/b%2Fz"_ustr, p); |
| 201 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 202 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 203 |
} |
| 204 |
|
| 205 |
void Test::testBad0() { |
| 206 |
OUString p; |
| 207 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 208 |
OUString(RTL_CONSTASCII_USTRINGPARAM("file://" MY_PATH_IN "/b\x00z")), |
| 209 |
p); |
| 210 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 211 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 212 |
} |
| 213 |
|
| 214 |
void Test::testBadEscape0() { |
| 215 |
OUString p; |
| 216 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 217 |
u"file://" MY_PATH_IN "/b%00z"_ustr, p); |
| 218 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 219 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 220 |
} |
| 221 |
|
| 222 |
void Test::testBadQuery() { |
| 223 |
OUString p; |
| 224 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 225 |
u"file://" MY_PATH_IN "?baz"_ustr, p); |
| 226 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 227 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 228 |
} |
| 229 |
|
| 230 |
void Test::testBadFragment() { |
| 231 |
OUString p; |
| 232 |
auto e = osl::FileBase::getSystemPathFromFileURL( |
| 233 |
u"file://" MY_PATH_IN "#baz"_ustr, p); |
| 234 |
CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); |
| 235 |
CPPUNIT_ASSERT_EQUAL(OUString(), p); |
| 236 |
} |
| 237 |
|
| 238 |
CPPUNIT_TEST_SUITE_REGISTRATION(Test); |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |