Fixes broken export to PDF with links.
[inkscape.git] / testfiles / src / uri-test.cpp
blob5b474706d23038306ec7046cb033db51a64e504f
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /**
3 * @file
4 * Test Inkscape::URI
5 */
6 /*
7 * Authors:
8 * Thomas Holder
10 * Copyright (C) 2018 Authors
12 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
15 #include "object/uri.h"
16 #include <gtest/gtest.h>
18 using Inkscape::URI;
20 #define BASE64_HELLO_WORLD_P1 "SGVsbG8g"
21 #define BASE64_HELLO_WORLD_P2 "V29ybGQ="
22 #define DATA_BASE64_HEADER "data:text/plain;charset=utf-8;base64,"
23 char const *DATA_BASE64_HELLO_WORLD = DATA_BASE64_HEADER BASE64_HELLO_WORLD_P1 BASE64_HELLO_WORLD_P2;
24 char const *DATA_BASE64_HELLO_WORLD_WRAPPED = DATA_BASE64_HEADER BASE64_HELLO_WORLD_P1 "\n" BASE64_HELLO_WORLD_P2;
26 char const *win_url_unc = "file://laptop/My%20Documents/FileSchemeURIs.doc";
27 char const *win_url_local = "file:///C:/Documents%20and%20Settings/davris/FileSchemeURIs.doc";
28 char const *win_filename_local = "C:\\Documents and Settings\\davris\\FileSchemeURIs.doc";
30 TEST(UriTest, Malformed)
32 ASSERT_ANY_THROW(URI(nullptr));
33 ASSERT_ANY_THROW(URI("nonhex-%XX"));
36 TEST(UriTest, GetPath)
38 ASSERT_STREQ(URI().getPath(), nullptr);
39 ASSERT_STREQ(URI("foo.svg").getPath(), "foo.svg");
40 ASSERT_STREQ(URI("foo.svg#bar").getPath(), "foo.svg");
41 ASSERT_STREQ(URI("#bar").getPath(), nullptr);
42 ASSERT_STREQ(URI("scheme://host").getPath(), nullptr);
43 ASSERT_STREQ(URI("scheme://host/path").getPath(), "/path");
44 ASSERT_STREQ(URI("scheme://host/path?query").getPath(), "/path");
45 ASSERT_STREQ(URI("scheme:/path").getPath(), "/path");
48 TEST(UriTest, FromDir)
50 #ifdef _WIN32
51 ASSERT_EQ(URI::from_dirname("C:\\tmp").str(), "file:///C:/tmp/");
52 ASSERT_EQ(URI::from_dirname("C:\\").str(), "file:///C:/");
53 ASSERT_EQ(URI::from_href_and_basedir("uri.svg", "C:\\tmp").str(), "file:///C:/tmp/uri.svg");
54 #else
55 ASSERT_EQ(URI::from_dirname("/").str(), "file:///");
56 ASSERT_EQ(URI::from_dirname("/tmp").str(), "file:///tmp/");
57 ASSERT_EQ(URI::from_href_and_basedir("uri.svg", "/tmp").str(), "file:///tmp/uri.svg");
58 #endif
61 TEST(UriTest, Str)
63 ASSERT_EQ(URI().str(), "");
64 ASSERT_EQ(URI("").str(), "");
65 ASSERT_EQ(URI("", "http://a/b").str(), "http://a/b");
67 ASSERT_EQ(URI("uri.svg").str(), "uri.svg");
68 ASSERT_EQ(URI("tmp/uri.svg").str(), "tmp/uri.svg");
69 ASSERT_EQ(URI("/tmp/uri.svg").str(), "/tmp/uri.svg");
70 ASSERT_EQ(URI("../uri.svg").str(), "../uri.svg");
72 ASSERT_EQ(URI("file:///tmp/uri.svg").str(), "file:///tmp/uri.svg");
73 ASSERT_EQ(URI("uri.svg", "file:///tmp/").str(), "file:///tmp/uri.svg");
74 ASSERT_EQ(URI("file:///tmp/uri.svg").str("file:///tmp/"), "uri.svg");
75 ASSERT_EQ(URI("file:///tmp/up/uri.svg").str("file:///tmp/"), "up/uri.svg");
76 ASSERT_EQ(URI("file:///tmp/uri.svg").str("file:///tmp/up/"), "../uri.svg");
77 ASSERT_EQ(URI("file:///tmp/uri.svg").str("http://web/url"), "file:///tmp/uri.svg");
78 ASSERT_EQ(URI("file:///tmp/uri.svg").str("http://web/url"), "file:///tmp/uri.svg");
79 ASSERT_EQ(URI("foo/uri.svg", "http://web/a/b/c").str(), "http://web/a/b/foo/uri.svg");
80 ASSERT_EQ(URI("foo/uri.svg", "http://web/a/b/c").str("http://web/a/"), "b/foo/uri.svg");
81 ASSERT_EQ(URI("foo/uri.svg", "http://web/a/b/c").str("http://other/a/"), "http://web/a/b/foo/uri.svg");
83 ASSERT_EQ(URI("http://web/").str("http://web/"), "");
84 ASSERT_EQ(URI("http://web/").str("http://web/url"), "./");
86 // special case: don't cross filesystem root
87 ASSERT_EQ(URI("file:///a").str("file:///"), "a");
88 ASSERT_EQ(URI("file:///ax/b").str("file:///ay/"), "file:///ax/b"); // special case
89 ASSERT_EQ(URI("file:///C:/b").str("file:///D:/"), "file:///C:/b"); // special case
90 ASSERT_EQ(URI("file:///C:/a/b").str("file:///C:/b/"), "../a/b");
92 ASSERT_EQ(URI(win_url_unc).str(), win_url_unc);
93 ASSERT_EQ(URI(win_url_unc).str("file://laptop/My%20Documents/"), "FileSchemeURIs.doc");
94 ASSERT_EQ(URI(win_url_local).str(), win_url_local);
95 ASSERT_EQ(URI(win_url_local).str("file:///C:/Documents%20and%20Settings/"), "davris/FileSchemeURIs.doc");
96 ASSERT_EQ(URI(win_url_local).str(win_url_unc), win_url_local);
97 #ifdef _WIN32
98 ASSERT_EQ(URI(win_url_local).toNativeFilename(), win_filename_local);
99 #else
100 ASSERT_EQ(URI("file:///tmp/uri.svg").toNativeFilename(), "/tmp/uri.svg");
101 ASSERT_EQ(URI("file:///tmp/x%20y.svg").toNativeFilename(), "/tmp/x y.svg");
102 ASSERT_EQ(URI("file:///a/b#hash").toNativeFilename(), "/a/b");
103 #endif
105 ASSERT_ANY_THROW(URI("http://a/b").toNativeFilename());
108 TEST(UriTest, StrDataScheme)
110 ASSERT_EQ(URI("data:,text").str(), "data:,text");
111 ASSERT_EQ(URI("data:,white%20space").str(), "data:,white%20space");
112 ASSERT_EQ(URI("data:,umlaut-%C3%96").str(), "data:,umlaut-%C3%96");
113 ASSERT_EQ(URI(DATA_BASE64_HELLO_WORLD).str(), DATA_BASE64_HELLO_WORLD);
116 TEST(UriTest, Escape)
118 ASSERT_EQ(URI("data:,white space").str(), "data:,white%20space");
119 ASSERT_EQ(URI("data:,white\nspace").str(), "data:,white%0Aspace");
120 ASSERT_EQ(URI("data:,umlaut-\xC3\x96").str(), "data:,umlaut-%C3%96");
123 TEST(UriTest, GetContents)
125 ASSERT_EQ(URI("data:,white space").getContents(), "white space");
126 ASSERT_EQ(URI("data:,white%20space").getContents(), "white space");
127 ASSERT_EQ(URI("data:,white\nspace").getContents(), "white\nspace");
128 ASSERT_EQ(URI("data:,white%0Aspace").getContents(), "white\nspace");
129 ASSERT_EQ(URI("data:,umlaut-%C3%96").getContents(), "umlaut-\xC3\x96");
130 ASSERT_EQ(URI(DATA_BASE64_HELLO_WORLD).getContents(), "Hello World");
131 ASSERT_EQ(URI(DATA_BASE64_HELLO_WORLD_WRAPPED).getContents(), "Hello World");
133 ASSERT_ANY_THROW(URI().getContents());
136 TEST(UriTest, CssStr)
138 ASSERT_EQ(URI("file:///tmp/uri.svg").cssStr(), "url(file:///tmp/uri.svg)");
139 ASSERT_EQ(URI("uri.svg").cssStr(), "url(uri.svg)");
142 TEST(UriTest, GetMimeType)
144 ASSERT_EQ(URI("data:image/png;base64,").getMimeType(), "image/png");
145 ASSERT_EQ(URI("data:text/plain,xxx").getMimeType(), "text/plain");
146 ASSERT_EQ(URI("file:///tmp/uri.png").getMimeType(), "image/png");
147 ASSERT_EQ(URI("uri.png").getMimeType(), "image/png");
148 ASSERT_EQ(URI("uri.svg").getMimeType(), "image/svg+xml");
150 // can be "text/plain" or "text/*"
151 ASSERT_EQ(URI("file:///tmp/uri.txt").getMimeType().substr(0, 5), "text/");
154 TEST(UriTest, HasScheme)
156 ASSERT_FALSE(URI().hasScheme("file"));
157 ASSERT_FALSE(URI("uri.svg").hasScheme("file"));
158 ASSERT_FALSE(URI("uri.svg").hasScheme("data"));
160 ASSERT_TRUE(URI("file:///uri.svg").hasScheme("file"));
161 ASSERT_TRUE(URI("FILE:///uri.svg").hasScheme("file"));
162 ASSERT_FALSE(URI("file:///uri.svg").hasScheme("data"));
164 ASSERT_TRUE(URI("data:,").hasScheme("data"));
165 ASSERT_TRUE(URI("DaTa:,").hasScheme("data"));
166 ASSERT_FALSE(URI("data:,").hasScheme("file"));
168 ASSERT_TRUE(URI("http://web/").hasScheme("http"));
169 ASSERT_FALSE(URI("http://web/").hasScheme("file"));
171 ASSERT_TRUE(URI::from_href_and_basedir("data:,white\nspace", "/tmp").hasScheme("data"));
174 TEST(UriTest, isOpaque)
176 ASSERT_FALSE(URI().isOpaque());
177 ASSERT_FALSE(URI("file:///uri.svg").isOpaque());
178 ASSERT_FALSE(URI("/uri.svg").isOpaque());
179 ASSERT_FALSE(URI("uri.svg").isOpaque());
180 ASSERT_FALSE(URI("foo://bar/baz").isOpaque());
181 ASSERT_FALSE(URI("foo://bar").isOpaque());
182 ASSERT_FALSE(URI("foo:/bar").isOpaque());
184 ASSERT_TRUE(URI("foo:bar").isOpaque());
185 ASSERT_TRUE(URI("mailto:user@host.xy").isOpaque());
186 ASSERT_TRUE(URI("news:comp.lang.java").isOpaque());
189 TEST(UriTest, isRelative)
191 ASSERT_TRUE(URI().isRelative());
193 ASSERT_FALSE(URI("http://web/uri.svg").isRelative());
194 ASSERT_FALSE(URI("file:///uri.svg").isRelative());
195 ASSERT_FALSE(URI("mailto:user@host.xy").isRelative());
196 ASSERT_FALSE(URI("data:,").isRelative());
198 ASSERT_TRUE(URI("//web/uri.svg").isRelative());
199 ASSERT_TRUE(URI("/uri.svg").isRelative());
200 ASSERT_TRUE(URI("uri.svg").isRelative());
201 ASSERT_TRUE(URI("./uri.svg").isRelative());
202 ASSERT_TRUE(URI("../uri.svg").isRelative());
205 TEST(UriTest, isNetPath)
207 ASSERT_FALSE(URI().isNetPath());
208 ASSERT_FALSE(URI("http://web/uri.svg").isNetPath());
209 ASSERT_FALSE(URI("file:///uri.svg").isNetPath());
210 ASSERT_FALSE(URI("/uri.svg").isNetPath());
211 ASSERT_FALSE(URI("uri.svg").isNetPath());
213 ASSERT_TRUE(URI("//web/uri.svg").isNetPath());
216 TEST(UriTest, isRelativePath)
218 ASSERT_FALSE(URI("foo:bar").isRelativePath());
219 ASSERT_TRUE(URI("foo%3Abar").isRelativePath());
221 ASSERT_FALSE(URI("http://web/uri.svg").isRelativePath());
222 ASSERT_FALSE(URI("//web/uri.svg").isRelativePath());
223 ASSERT_FALSE(URI("/uri.svg").isRelativePath());
225 ASSERT_TRUE(URI("uri.svg").isRelativePath());
226 ASSERT_TRUE(URI("./uri.svg").isRelativePath());
227 ASSERT_TRUE(URI("../uri.svg").isRelativePath());
230 TEST(UriTest, isAbsolutePath)
232 ASSERT_FALSE(URI().isAbsolutePath());
233 ASSERT_FALSE(URI("http://web/uri.svg").isAbsolutePath());
234 ASSERT_FALSE(URI("//web/uri.svg").isAbsolutePath());
235 ASSERT_FALSE(URI("uri.svg").isAbsolutePath());
236 ASSERT_FALSE(URI("../uri.svg").isAbsolutePath());
238 ASSERT_TRUE(URI("/uri.svg").isAbsolutePath());
241 TEST(UriTest, getScheme)
243 ASSERT_STREQ(URI().getScheme(), nullptr);
245 ASSERT_STREQ(URI("https://web/uri.svg").getScheme(), "https");
246 ASSERT_STREQ(URI("file:///uri.svg").getScheme(), "file");
247 ASSERT_STREQ(URI("data:,").getScheme(), "data");
249 ASSERT_STREQ(URI("data").getScheme(), nullptr);
252 TEST(UriTest, getQuery)
254 ASSERT_STREQ(URI().getQuery(), nullptr);
255 ASSERT_STREQ(URI("uri.svg?a=b&c=d").getQuery(), "a=b&c=d");
256 ASSERT_STREQ(URI("?a=b&c=d#hash").getQuery(), "a=b&c=d");
259 TEST(UriTest, getFragment)
261 ASSERT_STREQ(URI().getFragment(), nullptr);
262 ASSERT_STREQ(URI("uri.svg").getFragment(), nullptr);
263 ASSERT_STREQ(URI("uri.svg#hash").getFragment(), "hash");
264 ASSERT_STREQ(URI("?a=b&c=d#hash").getFragment(), "hash");
265 ASSERT_STREQ(URI("urn:isbn:096139210x#hash").getFragment(), "hash");
268 TEST(UriTest, getOpaque)
270 ASSERT_STREQ(URI().getOpaque(), nullptr);
271 ASSERT_STREQ(URI("urn:isbn:096139210x#hash").getOpaque(), "isbn:096139210x");
272 ASSERT_STREQ(URI("data:,foo").getOpaque(), ",foo");
275 TEST(UriTest, from_native_filename)
277 #ifdef _WIN32
278 ASSERT_EQ(URI::from_native_filename(win_filename_local).str(), win_url_local);
279 #else
280 ASSERT_EQ(URI::from_native_filename("/tmp/uri.svg").str(), "file:///tmp/uri.svg");
281 ASSERT_EQ(URI::from_native_filename("/tmp/x y.svg").str(), "file:///tmp/x%20y.svg");
282 #endif
285 TEST(UriTest, uri_to_iri)
287 // unescape UTF-8 (U+00D6)
288 ASSERT_EQ(Inkscape::uri_to_iri("data:,umlaut-%C3%96"), "data:,umlaut-\xC3\x96");
289 // don't unescape ASCII (U+003A)
290 ASSERT_EQ(Inkscape::uri_to_iri("foo%3Abar"), "foo%3Abar");
291 // sequence (U+00D6 U+1F37A U+003A)
292 ASSERT_EQ(Inkscape::uri_to_iri("%C3%96%F0%9F%8D%BA%3A"), "\xC3\x96\xF0\x9F\x8D\xBA%3A");
296 Local Variables:
297 mode:c++
298 c-file-style:"stroustrup"
299 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
300 indent-tabs-mode:nil
301 fill-column:99
302 End:
304 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :