Bump version to 6.4-15
[LibreOffice.git] / sal / qa / osl / getsystempathfromfileurl / test-getsystempathfromfileurl.cxx
blob4fd5d6b16ffb67829953897c6ea67945736e1e39
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <cppunit/TestAssert.h>
23 #include <cppunit/TestFixture.h>
24 #include <cppunit/extensions/HelperMacros.h>
25 #include <cppunit/plugin/TestPlugIn.h>
27 #include <osl/file.hxx>
29 #if defined(_WIN32)
30 #define MY_PATH_IN "/c:/foo/bar"
31 #define MY_PATH_OUT "c:\\foo\\bar"
32 #define MY_PATH_OUT_CONT MY_PATH_OUT "\\"
33 #define MY_PATH_OUT_REL "foo\\bar"
34 #else
35 #define MY_PATH_IN "/foo/bar"
36 #define MY_PATH_OUT MY_PATH_IN
37 #define MY_PATH_OUT_CONT MY_PATH_OUT "/"
38 #define MY_PATH_OUT_REL "foo/bar"
39 #endif
41 namespace {
43 class Test: public CppUnit::TestFixture {
44 private:
45 CPPUNIT_TEST_SUITE(Test);
46 CPPUNIT_TEST(testBadScheme);
47 CPPUNIT_TEST(testNoScheme);
48 CPPUNIT_TEST(testBadAuthority);
49 CPPUNIT_TEST(testLocalhost1Authority);
50 CPPUNIT_TEST(testLocalhost2Authority);
51 CPPUNIT_TEST(testLocalhost3Authority);
52 CPPUNIT_TEST(testNoAuthority);
53 CPPUNIT_TEST(testEmptyPath);
54 CPPUNIT_TEST(testHomeAbbreviation);
55 CPPUNIT_TEST(testOtherHomeAbbreviation);
56 CPPUNIT_TEST(testRelative);
57 CPPUNIT_TEST(testEscape);
58 CPPUNIT_TEST(testBadEscape2f);
59 CPPUNIT_TEST(testBadEscape2F);
60 CPPUNIT_TEST(testBad0);
61 CPPUNIT_TEST(testBadEscape0);
62 CPPUNIT_TEST(testBadQuery);
63 CPPUNIT_TEST(testBadFragment);
64 CPPUNIT_TEST_SUITE_END();
66 void testBadScheme();
67 void testNoScheme();
68 void testBadAuthority();
69 void testLocalhost1Authority();
70 void testLocalhost2Authority();
71 void testLocalhost3Authority();
72 void testNoAuthority();
73 void testEmptyPath();
74 void testHomeAbbreviation();
75 void testOtherHomeAbbreviation();
76 void testRelative();
77 void testEscape();
78 void testBadEscape2f();
79 void testBadEscape2F();
80 void testBad0();
81 void testBadEscape0();
82 void testBadQuery();
83 void testBadFragment();
86 void Test::testBadScheme() {
87 OUString p;
88 auto e = osl::FileBase::getSystemPathFromFileURL("foo:bar", p);
89 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
90 CPPUNIT_ASSERT_EQUAL(OUString(), p);
93 void Test::testNoScheme() {
94 #if !defined(_WIN32) //TODO
95 OUString p;
96 auto e = osl::FileBase::getSystemPathFromFileURL("//" MY_PATH_IN, p);
97 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
98 CPPUNIT_ASSERT_EQUAL(OUString(MY_PATH_OUT), p);
99 #endif
102 void Test::testBadAuthority() {
103 #if defined UNX
104 OUString p;
105 auto e = osl::FileBase::getSystemPathFromFileURL(
106 "file://baz" MY_PATH_IN, p);
107 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
108 CPPUNIT_ASSERT_EQUAL(OUString(), p);
109 #endif
112 void Test::testLocalhost1Authority() {
113 OUString p;
114 auto e = osl::FileBase::getSystemPathFromFileURL(
115 "file://localhost" MY_PATH_IN, p);
116 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
117 CPPUNIT_ASSERT_EQUAL(OUString(MY_PATH_OUT), p);
120 void Test::testLocalhost2Authority() {
121 OUString p;
122 auto e = osl::FileBase::getSystemPathFromFileURL(
123 "file://LOCALHOST" MY_PATH_IN, p);
124 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
125 CPPUNIT_ASSERT_EQUAL(OUString(MY_PATH_OUT), p);
128 void Test::testLocalhost3Authority() {
129 OUString p;
130 auto e = osl::FileBase::getSystemPathFromFileURL(
131 "file://127.0.0.1" MY_PATH_IN, p);
132 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
133 CPPUNIT_ASSERT_EQUAL(OUString(MY_PATH_OUT), p);
136 void Test::testNoAuthority() {
137 OUString p;
138 auto e = osl::FileBase::getSystemPathFromFileURL("file:" MY_PATH_IN, p);
139 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
140 CPPUNIT_ASSERT_EQUAL(OUString(MY_PATH_OUT), p);
143 void Test::testEmptyPath() {
144 #if defined UNX
145 OUString p;
146 auto e = osl::FileBase::getSystemPathFromFileURL("file://", p);
147 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
148 CPPUNIT_ASSERT_EQUAL(OUString("/"), p);
149 #endif
152 void Test::testHomeAbbreviation() {
153 #if defined UNX
154 OUString p;
155 auto e = osl::FileBase::getSystemPathFromFileURL("file:///~", p);
156 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
157 // could theoretically fail due to osl::Security::getHomeDir problem
158 e = osl::FileBase::getSystemPathFromFileURL("file:///~/foo%2525/bar", p);
159 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
160 // could theoretically fail due to osl::Security::getHomeDir problem
161 CPPUNIT_ASSERT(p.endsWith("/foo%25/bar"));
162 #endif
165 void Test::testOtherHomeAbbreviation() {
166 #if defined UNX
167 OUString p;
168 auto e = osl::FileBase::getSystemPathFromFileURL(
169 "file:///~baz" MY_PATH_IN, p);
170 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e); // not supported for now
171 CPPUNIT_ASSERT_EQUAL(OUString(), p);
172 #endif
175 void Test::testRelative() {
176 OUString p;
177 auto e = osl::FileBase::getSystemPathFromFileURL("foo/bar", p);
178 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
179 CPPUNIT_ASSERT(p.endsWith(MY_PATH_OUT_REL));
182 void Test::testEscape() {
183 OUString p;
184 auto e = osl::FileBase::getSystemPathFromFileURL(
185 "file://" MY_PATH_IN "/b%61z", p);
186 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_None, e);
187 CPPUNIT_ASSERT_EQUAL(OUString(MY_PATH_OUT_CONT "baz"), p);
190 void Test::testBadEscape2f() {
191 OUString p;
192 auto e = osl::FileBase::getSystemPathFromFileURL(
193 "file://" MY_PATH_IN "/b%2fz", p);
194 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
195 CPPUNIT_ASSERT_EQUAL(OUString(), p);
198 void Test::testBadEscape2F() {
199 OUString p;
200 auto e = osl::FileBase::getSystemPathFromFileURL(
201 "file://" MY_PATH_IN "/b%2Fz", p);
202 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
203 CPPUNIT_ASSERT_EQUAL(OUString(), p);
206 void Test::testBad0() {
207 OUString p;
208 auto e = osl::FileBase::getSystemPathFromFileURL(
209 OUString(RTL_CONSTASCII_USTRINGPARAM("file://" MY_PATH_IN "/b\x00z")),
211 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
212 CPPUNIT_ASSERT_EQUAL(OUString(), p);
215 void Test::testBadEscape0() {
216 OUString p;
217 auto e = osl::FileBase::getSystemPathFromFileURL(
218 "file://" MY_PATH_IN "/b%00z", p);
219 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
220 CPPUNIT_ASSERT_EQUAL(OUString(), p);
223 void Test::testBadQuery() {
224 OUString p;
225 auto e = osl::FileBase::getSystemPathFromFileURL(
226 "file://" MY_PATH_IN "?baz", p);
227 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
228 CPPUNIT_ASSERT_EQUAL(OUString(), p);
231 void Test::testBadFragment() {
232 OUString p;
233 auto e = osl::FileBase::getSystemPathFromFileURL(
234 "file://" MY_PATH_IN "#baz", p);
235 CPPUNIT_ASSERT_EQUAL(osl::FileBase::E_INVAL, e);
236 CPPUNIT_ASSERT_EQUAL(OUString(), p);
239 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */