Differences From Artifact [e9cb46977b]:
- File
tests/OFURITests.m
— part of check-in
[3e455c4839]
at
2022-10-11 19:27:09
on branch trunk
— OFURI: Make scheme and path nonnull
This is as per the RFC, which says the scheme and path can never be
missing, while the path can be empty. (user: js, size: 14188) [annotate] [blame] [check-ins using] [more...]
To Artifact [96d1dc951e]:
- File tests/OFIRITests.m — part of check-in [8939cbdb52] at 2023-01-06 09:04:02 on branch trunk — Update copyright (user: js, size: 14865) [annotate] [blame] [check-ins using] [more...]
1 | /* | | | | | | | | | | | | | | | | | | | | > | | | | | | | | | | | | | | | | > > > > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | > | | > | | | | | | | | > | | | > | | | | | > | | | | | | | | | | > | | | | | | | | | > > > > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | /* * Copyright (c) 2008-2023 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * Alternatively, it may be distributed under the terms of the GNU General * Public License, either version 2 or 3, which can be found in the file * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this * file. */ #include "config.h" #import "TestsAppDelegate.h" static OFString *const module = @"OFIRI"; static OFString *IRIString = @"ht+tp://us%3Aer:p%40w@ho%3Ast:1234/" @"pa%3Fth?que%23ry=1&f%26oo=b%3dar#frag%23ment"; @implementation TestsAppDelegate (OFIRITests) - (void)IRITests { void *pool = objc_autoreleasePoolPush(); OFIRI *IRI1, *IRI2, *IRI3, *IRI4, *IRI5, *IRI6, *IRI7, *IRI8, *IRI9; OFIRI *IRI10, *IRI11; OFMutableIRI *mutableIRI; TEST(@"+[IRIWithString:]", R(IRI1 = [OFIRI IRIWithString: IRIString]) && R(IRI2 = [OFIRI IRIWithString: @"http://foo:80"]) && R(IRI3 = [OFIRI IRIWithString: @"http://bar/"]) && R(IRI4 = [OFIRI IRIWithString: @"file:///etc/passwd"]) && R(IRI5 = [OFIRI IRIWithString: @"http://foo/bar/qux/foo%2fbar"]) && R(IRI6 = [OFIRI IRIWithString: @"https://[12:34::56:abcd]/"]) && R(IRI7 = [OFIRI IRIWithString: @"https://[12:34::56:abcd]:234/"]) && R(IRI8 = [OFIRI IRIWithString: @"urn:qux:foo"]) && R(IRI9 = [OFIRI IRIWithString: @"file:/foo?query#frag"]) && R(IRI10 = [OFIRI IRIWithString: @"file:foo@bar/qux?query#frag"]) && R(IRI11 = [OFIRI IRIWithString: @"http://ä/ö?ü"])) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #1", OFInvalidFormatException, [OFIRI IRIWithString: @"ht,tp://foo"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #2", OFInvalidFormatException, [OFIRI IRIWithString: @"http://f`oo"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #3", OFInvalidFormatException, [OFIRI IRIWithString: @"http://foo/`"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #4", OFInvalidFormatException, [OFIRI IRIWithString: @"http://foo/foo?`"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #5", OFInvalidFormatException, [OFIRI IRIWithString: @"http://foo/foo?foo#`"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #6", OFInvalidFormatException, [OFIRI IRIWithString: @"https://[g]/"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #7", OFInvalidFormatException, [OFIRI IRIWithString: @"https://[f]:/"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #8", OFInvalidFormatException, [OFIRI IRIWithString: @"https://[f]:f/"]) EXPECT_EXCEPTION(@"+[IRIWithString:] fails with invalid characters #9", OFInvalidFormatException, [OFIRI IRIWithString: @"foo:"]) TEST(@"+[IRIWithString:relativeToIRI:]", [[[OFIRI IRIWithString: @"/foo" relativeToIRI: IRI1] string] isEqual: @"ht+tp://us%3Aer:p%40w@ho%3Ast:1234/foo"] && [[[OFIRI IRIWithString: @"foo/bar?q" relativeToIRI: [OFIRI IRIWithString: @"http://h/qux/quux"]] string] isEqual: @"http://h/qux/foo/bar?q"] && [[[OFIRI IRIWithString: @"foo/bar" relativeToIRI: [OFIRI IRIWithString: @"http://h/qux/?x"]] string] isEqual: @"http://h/qux/foo/bar"] && [[[OFIRI IRIWithString: @"http://foo/?q" relativeToIRI: IRI1] string] isEqual: @"http://foo/?q"] && [[[OFIRI IRIWithString: @"foo" relativeToIRI: [OFIRI IRIWithString: @"http://foo/bar"]] string] isEqual: @"http://foo/foo"] && [[[OFIRI IRIWithString: @"foo" relativeToIRI: [OFIRI IRIWithString: @"http://foo"]] string] isEqual: @"http://foo/foo"]) EXPECT_EXCEPTION( @"+[IRIWithString:relativeToIRI:] fails with invalid characters #1", OFInvalidFormatException, [OFIRI IRIWithString: @"`" relativeToIRI: IRI1]) EXPECT_EXCEPTION( @"+[IRIWithString:relativeToIRI:] fails with invalid characters #2", OFInvalidFormatException, [OFIRI IRIWithString: @"/`" relativeToIRI: IRI1]) EXPECT_EXCEPTION( @"+[IRIWithString:relativeToIRI:] fails with invalid characters #3", OFInvalidFormatException, [OFIRI IRIWithString: @"?`" relativeToIRI: IRI1]) EXPECT_EXCEPTION( @"+[IRIWithString:relativeToIRI:] fails with invalid characters #4", OFInvalidFormatException, [OFIRI IRIWithString: @"#`" relativeToIRI: IRI1]) #ifdef OF_HAVE_FILES TEST(@"+[fileIRIWithPath:]", [[[OFIRI fileIRIWithPath: @"testfile.txt"] fileSystemRepresentation] isEqual: [[OFFileManager defaultManager].currentDirectoryPath stringByAppendingPathComponent: @"testfile.txt"]]) # if defined(OF_WINDOWS) || defined(OF_MSDOS) OFIRI *tmp; TEST(@"+[fileIRIWithPath:] for c:\\", (tmp = [OFIRI fileIRIWithPath: @"c:\\"]) && [tmp.string isEqual: @"file:/c:/"] && [tmp.fileSystemRepresentation isEqual: @"c:\\"]) # endif # ifdef OF_WINDOWS TEST(@"+[fileIRIWithPath:] with UNC", (tmp = [OFIRI fileIRIWithPath: @"\\\\foo\\bar" isDirectory: false]) && [tmp.host isEqual: @"foo"] && [tmp.path isEqual: @"/bar"] && [tmp.string isEqual: @"file://foo/bar"] && [tmp.fileSystemRepresentation isEqual: @"\\\\foo\\bar"] && (tmp = [OFIRI fileIRIWithPath: @"\\\\test" isDirectory: true]) && [tmp.host isEqual: @"test"] && [tmp.path isEqual: @"/"] && [tmp.string isEqual: @"file://test/"] && [tmp.fileSystemRepresentation isEqual: @"\\\\test"]) # endif #endif TEST(@"-[string]", [IRI1.string isEqual: IRIString] && [IRI2.string isEqual: @"http://foo:80"] && [IRI3.string isEqual: @"http://bar/"] && [IRI4.string isEqual: @"file:///etc/passwd"] && [IRI5.string isEqual: @"http://foo/bar/qux/foo%2fbar"] && [IRI6.string isEqual: @"https://[12:34::56:abcd]/"] && [IRI7.string isEqual: @"https://[12:34::56:abcd]:234/"] && [IRI8.string isEqual: @"urn:qux:foo"] && [IRI9.string isEqual: @"file:/foo?query#frag"] && [IRI10.string isEqual: @"file:foo@bar/qux?query#frag"] && [IRI11.string isEqual: @"http://ä/ö?ü"]) TEST(@"-[scheme]", [IRI1.scheme isEqual: @"ht+tp"] && [IRI4.scheme isEqual: @"file"] && [IRI9.scheme isEqual: @"file"] && [IRI10.scheme isEqual: @"file"] && [IRI11.scheme isEqual: @"http"]) TEST(@"-[user]", [IRI1.user isEqual: @"us:er"] && IRI4.user == nil && IRI10.user == nil && IRI11.user == nil) TEST(@"-[password]", [IRI1.password isEqual: @"p@w"] && IRI4.password == nil && IRI10.password == nil && IRI11.password == nil) TEST(@"-[host]", [IRI1.host isEqual: @"ho:st"] && [IRI6.host isEqual: @"12:34::56:abcd"] && [IRI7.host isEqual: @"12:34::56:abcd"] && IRI8.host == nil && IRI9.host == nil && IRI10.host == nil && [IRI11.host isEqual: @"ä"]) TEST(@"-[port]", IRI1.port.unsignedShortValue == 1234 && [IRI4 port] == nil && IRI7.port.unsignedShortValue == 234 && IRI8.port == nil && IRI9.port == nil && IRI10.port == nil && IRI11.port == nil) TEST(@"-[path]", [IRI1.path isEqual: @"/pa?th"] && [IRI4.path isEqual: @"/etc/passwd"] && [IRI8.path isEqual: @"qux:foo"] && [IRI9.path isEqual: @"/foo"] && [IRI10.path isEqual: @"foo@bar/qux"] && [IRI11.path isEqual: @"/ö"]) TEST(@"-[pathComponents]", [IRI1.pathComponents isEqual: [OFArray arrayWithObjects: @"/", @"pa?th", nil]] && [IRI4.pathComponents isEqual: [OFArray arrayWithObjects: @"/", @"etc", @"passwd", nil]] && [IRI5.pathComponents isEqual: [OFArray arrayWithObjects: @"/", @"bar", @"qux", @"foo/bar", nil]]) TEST(@"-[lastPathComponent]", [[[OFIRI IRIWithString: @"http://host/foo//bar/baz"] lastPathComponent] isEqual: @"baz"] && [[[OFIRI IRIWithString: @"http://host/foo//bar/baz/"] lastPathComponent] isEqual: @"baz"] && [[[OFIRI IRIWithString: @"http://host/foo/"] lastPathComponent] isEqual: @"foo"] && [[[OFIRI IRIWithString: @"http://host/"] lastPathComponent] isEqual: @"/"] && [IRI5.lastPathComponent isEqual: @"foo/bar"]) TEST(@"-[query]", [IRI1.query isEqual: @"que#ry=1&f&oo=b=ar"] && IRI4.query == nil && [IRI9.query isEqual: @"query"] && [IRI10.query isEqual: @"query"] && [IRI11.query isEqual: @"ü"]) TEST(@"-[queryItems]", [IRI1.queryItems isEqual: [OFArray arrayWithObjects: [OFPair pairWithFirstObject: @"que#ry" secondObject: @"1"], [OFPair pairWithFirstObject: @"f&oo" secondObject: @"b=ar"], nil]]); TEST(@"-[fragment]", [IRI1.fragment isEqual: @"frag#ment"] && IRI4.fragment == nil && [IRI9.fragment isEqual: @"frag"] && [IRI10.fragment isEqual: @"frag"]) TEST(@"-[copy]", R(IRI4 = [[IRI1 copy] autorelease])) TEST(@"-[isEqual:]", [IRI1 isEqual: IRI4] && ![IRI2 isEqual: IRI3] && [[OFIRI IRIWithString: @"HTTP://bar/"] isEqual: IRI3]) TEST(@"-[hash:]", IRI1.hash == IRI4.hash && IRI2.hash != IRI3.hash) EXPECT_EXCEPTION(@"Detection of invalid format", OFInvalidFormatException, [OFIRI IRIWithString: @"http"]) TEST(@"-[IRIByAddingPercentEncodingForUnicodeCharacters]", [IRI11.IRIByAddingPercentEncodingForUnicodeCharacters isEqual: [OFIRI IRIWithString: @"http://%C3%A4/%C3%B6?%C3%BC"]]) mutableIRI = [OFMutableIRI IRIWithScheme: @"dummy"]; EXPECT_EXCEPTION( @"-[setPercentEncodedScheme:] with invalid characters fails", OFInvalidFormatException, mutableIRI.scheme = @"%20") TEST(@"-[setHost:]", (mutableIRI.host = @"ho:st") && [mutableIRI.percentEncodedHost isEqual: @"ho%3Ast"] && (mutableIRI.host = @"12:34:ab") && [mutableIRI.percentEncodedHost isEqual: @"[12:34:ab]"] && (mutableIRI.host = @"12:34:aB") && [mutableIRI.percentEncodedHost isEqual: @"[12:34:aB]"] && (mutableIRI.host = @"12:34:g") && [mutableIRI.percentEncodedHost isEqual: @"12%3A34%3Ag"]) TEST(@"-[setPercentEncodedHost:]", (mutableIRI.percentEncodedHost = @"ho%3Ast") && [mutableIRI.host isEqual: @"ho:st"] && (mutableIRI.percentEncodedHost = @"[12:34]") && [mutableIRI.host isEqual: @"12:34"] && (mutableIRI.percentEncodedHost = @"[12::ab]") && [mutableIRI.host isEqual: @"12::ab"]) EXPECT_EXCEPTION( @"-[setPercentEncodedHost:] with invalid characters fails #1", OFInvalidFormatException, mutableIRI.percentEncodedHost = @"/") EXPECT_EXCEPTION( @"-[setPercentEncodedHost:] with invalid characters fails #2", OFInvalidFormatException, mutableIRI.percentEncodedHost = @"[12:34") EXPECT_EXCEPTION( @"-[setPercentEncodedHost:] with invalid characters fails #3", OFInvalidFormatException, mutableIRI.percentEncodedHost = @"[a::g]") TEST(@"-[setUser:]", (mutableIRI.user = @"us:er") && [mutableIRI.percentEncodedUser isEqual: @"us%3Aer"]) TEST(@"-[setPercentEncodedUser:]", (mutableIRI.percentEncodedUser = @"us%3Aer") && [mutableIRI.user isEqual: @"us:er"]) EXPECT_EXCEPTION( @"-[setPercentEncodedUser:] with invalid characters fails", OFInvalidFormatException, mutableIRI.percentEncodedHost = @"/") TEST(@"-[setPassword:]", (mutableIRI.password = @"pass:word") && [mutableIRI.percentEncodedPassword isEqual: @"pass%3Aword"]) TEST(@"-[setPercentEncodedPassword:]", (mutableIRI.percentEncodedPassword = @"pass%3Aword") && [mutableIRI.password isEqual: @"pass:word"]) EXPECT_EXCEPTION( @"-[setPercentEncodedPassword:] with invalid characters fails", OFInvalidFormatException, mutableIRI.percentEncodedPassword = @"/") TEST(@"-[setPath:]", (mutableIRI.path = @"pa/th@?") && [mutableIRI.percentEncodedPath isEqual: @"pa/th@%3F"]) TEST(@"-[setPercentEncodedPath:]", (mutableIRI.percentEncodedPath = @"pa/th@%3F") && [mutableIRI.path isEqual: @"pa/th@?"]) EXPECT_EXCEPTION( @"-[setPercentEncodedPath:] with invalid characters fails", OFInvalidFormatException, mutableIRI.percentEncodedPath = @"?") TEST(@"-[setQuery:]", (mutableIRI.query = @"que/ry?#") && [mutableIRI.percentEncodedQuery isEqual: @"que/ry?%23"]) TEST(@"-[setPercentEncodedQuery:]", (mutableIRI.percentEncodedQuery = @"que/ry?%23") && [mutableIRI.query isEqual: @"que/ry?#"]) EXPECT_EXCEPTION( @"-[setPercentEncodedQuery:] with invalid characters fails", OFInvalidFormatException, mutableIRI.percentEncodedQuery = @"`") TEST(@"-[setQueryItems:]", (mutableIRI.queryItems = [OFArray arrayWithObjects: [OFPair pairWithFirstObject: @"foo&bar" secondObject: @"baz=qux"], [OFPair pairWithFirstObject: @"f=oobar" secondObject: @"b&azqux"], nil]) && [mutableIRI.percentEncodedQuery isEqual: @"foo%26bar=baz%3Dqux&f%3Doobar=b%26azqux"]) TEST(@"-[setFragment:]", (mutableIRI.fragment = @"frag/ment?#") && [mutableIRI.percentEncodedFragment isEqual: @"frag/ment?%23"]) TEST(@"-[setPercentEncodedFragment:]", (mutableIRI.percentEncodedFragment = @"frag/ment?%23") && [mutableIRI.fragment isEqual: @"frag/ment?#"]) EXPECT_EXCEPTION( @"-[setPercentEncodedFragment:] with invalid characters fails", OFInvalidFormatException, mutableIRI.percentEncodedFragment = @"`") TEST(@"-[IRIByAppendingPathComponent:isDirectory:]", [[[OFIRI IRIWithString: @"file:///foo/bar"] IRIByAppendingPathComponent: @"qux" isDirectory: false] isEqual: [OFIRI IRIWithString: @"file:///foo/bar/qux"]] && [[[OFIRI IRIWithString: @"file:///foo/bar/"] IRIByAppendingPathComponent: @"qux" isDirectory: false] isEqual: [OFIRI IRIWithString: @"file:///foo/bar/qux"]] && [[[OFIRI IRIWithString: @"file:///foo/bar/"] IRIByAppendingPathComponent: @"qu?x" isDirectory: false] isEqual: [OFIRI IRIWithString: @"file:///foo/bar/qu%3Fx"]] && [[[OFIRI IRIWithString: @"file:///foo/bar/"] IRIByAppendingPathComponent: @"qu?x" isDirectory: true] isEqual: [OFIRI IRIWithString: @"file:///foo/bar/qu%3Fx/"]]) TEST(@"-[IRIByStandardizingPath]", [[[OFIRI IRIWithString: @"http://foo/bar/.."] IRIByStandardizingPath] isEqual: [OFIRI IRIWithString: @"http://foo/"]] && [[[OFIRI IRIWithString: @"http://foo/bar/%2E%2E/../qux/"] IRIByStandardizingPath] isEqual: [OFIRI IRIWithString: @"http://foo/bar/qux/"]] && [[[OFIRI IRIWithString: @"http://foo/bar/./././qux/./"] IRIByStandardizingPath] isEqual: [OFIRI IRIWithString: @"http://foo/bar/qux/"]] && [[[OFIRI IRIWithString: @"http://foo/bar/../../qux"] IRIByStandardizingPath] isEqual: [OFIRI IRIWithString: @"http://foo/../qux"]]) objc_autoreleasePoolPop(pool); } @end |