Skip to content

Commit bf7bb9d

Browse files
committed
TestXMLDocument: Un-XFAIL DTD tests.
- Serve the DTD from the built in HTTPServer so the tests will work without internet access. - Resolves rdar://31567922.
1 parent c946737 commit bf7bb9d

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

TestFoundation/HTTPServer.swift

+32-6
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,11 @@ struct _HTTPResponse {
332332
}
333333

334334
public class TestURLSessionServer {
335-
let capitals: [String:String] = ["Nepal":"Kathmandu",
336-
"Peru":"Lima",
337-
"Italy":"Rome",
338-
"USA":"Washington, D.C.",
339-
"UnitedStates": "USA",
335+
let capitals: [String:String] = ["Nepal": "Kathmandu",
336+
"Peru": "Lima",
337+
"Italy": "Rome",
338+
"USA": "Washington, D.C.",
339+
"UnitedStates": "USA",
340340
"country.txt": "A country is a region that is identified as a distinct national entity in political geography"]
341341
let httpServer: _HTTPServer
342342
let startDelay: TimeInterval?
@@ -395,7 +395,7 @@ public class TestURLSessionServer {
395395
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)", body: text)
396396
}
397397

398-
if uri == "/UnitedStates" {
398+
if uri == "/UnitedStates" {
399399
let value = capitals[String(uri.dropFirst())]!
400400
let text = request.getCommaSeparatedHeaders()
401401
let host = request.headers[1].components(separatedBy: " ")[1]
@@ -406,6 +406,32 @@ public class TestURLSessionServer {
406406
let httpResponse = _HTTPResponse(response: .REDIRECT, headers: "Location: http://\(newHost + "/" + value)", body: text)
407407
return httpResponse
408408
}
409+
410+
if uri == "/DTDs/PropertyList-1.0.dtd" {
411+
let dtd = """
412+
<!ENTITY % plistObject "(array | data | date | dict | real | integer | string | true | false )" >
413+
<!ELEMENT plist %plistObject;>
414+
<!ATTLIST plist version CDATA "1.0" >
415+
416+
<!-- Collections -->
417+
<!ELEMENT array (%plistObject;)*>
418+
<!ELEMENT dict (key, %plistObject;)*>
419+
<!ELEMENT key (#PCDATA)>
420+
421+
<!--- Primitive types -->
422+
<!ELEMENT string (#PCDATA)>
423+
<!ELEMENT data (#PCDATA)> <!-- Contents interpreted as Base-64 encoded -->
424+
<!ELEMENT date (#PCDATA)> <!-- Contents should conform to a subset of ISO 8601 (in particular, YYYY '-' MM '-' DD 'T' HH ':' MM ':' SS 'Z'. Smaller units may be omitted with a loss of precision) -->
425+
426+
<!-- Numerical primitives -->
427+
<!ELEMENT true EMPTY> <!-- Boolean constant true -->
428+
<!ELEMENT false EMPTY> <!-- Boolean constant false -->
429+
<!ELEMENT real (#PCDATA)> <!-- Contents should represent a floating point number matching ("+" | "-")? d+ ("."d*)? ("E" ("+" | "-") d+)? where d is a digit 0-9. -->
430+
<!ELEMENT integer (#PCDATA)> <!-- Contents should represent a (possibly signed) integer number in base 10 -->
431+
"""
432+
return _HTTPResponse(response: .OK, body: dtd)
433+
}
434+
409435
return _HTTPResponse(response: .OK, body: capitals[String(uri.dropFirst())]!)
410436
}
411437

TestFoundation/TestXMLDocument.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#endif
1818

1919

20-
class TestXMLDocument : XCTestCase {
20+
class TestXMLDocument : LoopbackServerTest {
2121

2222
static var allTests: [(String, (TestXMLDocument) -> () throws -> Void)] {
2323
return [
@@ -33,8 +33,8 @@ class TestXMLDocument : XCTestCase {
3333
("test_processingInstruction", test_processingInstruction),
3434
("test_parseXMLString", test_parseXMLString),
3535
("test_prefixes", test_prefixes),
36-
// XFAIL: <rdar://31567922> ("test_validation_success", test_validation_success),
37-
// XFAIL: <rdar://31567922> ("test_validation_failure", test_validation_failure),
36+
("test_validation_success", test_validation_success),
37+
("test_validation_failure", test_validation_failure),
3838
("test_dtd", test_dtd),
3939
("test_documentWithDTD", test_documentWithDTD),
4040
("test_dtd_attributes", test_dtd_attributes),
@@ -353,8 +353,6 @@ class TestXMLDocument : XCTestCase {
353353
XCTAssert(doc.rootElement()?.namespaces?.first?.name == "R")
354354
}
355355

356-
/*
357-
* <rdar://31567922> Re-enable these tests in a way that does not depend on the internet.
358356
func test_validation_success() throws {
359357
let validString = "<?xml version=\"1.0\" standalone=\"yes\"?><!DOCTYPE foo [ <!ELEMENT foo (#PCDATA)> ]><foo>Hello world</foo>"
360358
do {
@@ -364,7 +362,8 @@ class TestXMLDocument : XCTestCase {
364362
XCTFail("\(error)")
365363
}
366364

367-
let plistDocString = "<?xml version='1.0' encoding='utf-8'?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> <plist version='1.0'><dict><key>MyKey</key><string>Hello!</string></dict></plist>"
365+
let dtdUrl = "http://127.0.0.1:\(TestURLSession.serverPort)/DTDs/PropertyList-1.0.dtd"
366+
let plistDocString = "<?xml version='1.0' encoding='utf-8'?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"\(dtdUrl)\"> <plist version='1.0'><dict><key>MyKey</key><string>Hello!</string></dict></plist>"
368367
let plistDoc = try XMLDocument(xmlString: plistDocString, options: [])
369368
do {
370369
try plistDoc.validate()
@@ -388,15 +387,16 @@ class TestXMLDocument : XCTestCase {
388387
XCTAssert((nsError.userInfo[NSLocalizedDescriptionKey] as! String).contains("Element img was declared EMPTY this one has content"))
389388
}
390389

391-
let plistDocString = "<?xml version='1.0' encoding='utf-8'?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> <plist version='1.0'><dict><key>MyKey</key><string>Hello!</string><key>MyBooleanThing</key><true>foobar</true></dict></plist>"
390+
let dtdUrl = "http://127.0.0.1:\(TestURLSession.serverPort)/DTDs/PropertyList-1.0.dtd"
391+
let plistDocString = "<?xml version='1.0' encoding='utf-8'?><!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"\(dtdUrl)\"> <plist version='1.0'><dict><key>MyKey</key><string>Hello!</string><key>MyBooleanThing</key><true>foobar</true></dict></plist>"
392392
let plistDoc = try XMLDocument(xmlString: plistDocString, options: [])
393393
do {
394394
try plistDoc.validate()
395395
XCTFail("Should have thrown!")
396396
} catch let error as NSError {
397397
XCTAssert((error.userInfo[NSLocalizedDescriptionKey] as! String).contains("Element true was declared EMPTY this one has content"))
398398
}
399-
}*/
399+
}
400400

401401
func test_dtd() throws {
402402
let node = XMLNode.dtdNode(withXMLString:"<!ELEMENT foo (#PCDATA)>") as! XMLDTDNode

0 commit comments

Comments
 (0)