Skip to content

Commit

Permalink
Test for parsing XML with WebCore and libxml2 in the same process
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273553
<rdar://127295719>

Reviewed by Alex Christensen.

The xmlCtxtReadFile() function calls xmlLoadExternalEntity() as part of
its implementation (not sure why), which would then trigger the external
entity loader path.  Prior to the fix in 278168@main, WebCore replaced
the global external entity loader function and didn't change it back
when it was done, which caused libxml2 loads to fail.  The fix restores
the default external entity loader function when WebCore is done parsing
XML, so making WebCore parse XMl content, then calling xmlCtxtReadFile()
is sufficient to test the fix.

API Test:  TestWebKitAPI.XMLParsing.WebCoreDoesNotBreakLibxml2

* Tools/TestWebKitAPI/Configurations/TestWebKitAPI.xcconfig:
(OTHER_LDFLAGS): Link libxml2 to TestWebKitAPI.
* Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
- Add XMLParsing.mm and test.xml to the project.
* Tools/TestWebKitAPI/Tests/WebCore/XMLParsing.mm: Add.
(TestWebKitAPI::TEST(XMLParsing, WebCoreDoesNotBreakLibxml2)):
* Tools/TestWebKitAPI/Tests/WebKit/test.xml: Add.

Canonical link: https://commits.webkit.org/278253@main
  • Loading branch information
David Kilzer authored and ddkilzer committed May 2, 2024
1 parent a425103 commit 8130984
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Tools/TestWebKitAPI/Configurations/TestWebKitAPI.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ WK_IMAGEIO_LDFLAGS_MACOS_SINCE_1300 = -framework ImageIO;

OTHER_CPLUSPLUSFLAGS = $(inherited) -isystem $(SDKROOT)/System/Library/Frameworks/System.framework/PrivateHeaders;

OTHER_LDFLAGS = $(inherited) -lgtest -force_load $(BUILT_PRODUCTS_DIR)/libTestWebKitAPI.a -framework JavaScriptCore -framework WebCore -framework WebKit -lWebCoreTestSupport -framework Metal -framework IOSurface $(WK_APPSERVERSUPPORT_LDFLAGS) $(WK_AUTHKIT_LDFLAGS) -framework Network -framework UniformTypeIdentifiers -framework CoreFoundation -framework CoreServices $(WK_BROWSERENGINEKIT_LDFLAGS) $(WK_HID_LDFLAGS) $(WK_IMAGEIO_LDFLAGS) $(WK_OPENGL_LDFLAGS) $(WK_PDFKIT_LDFLAGS) $(WK_SYSTEM_LDFLAGS) $(WK_UIKITMACHELPER_LDFLAGS) $(WK_VISIONKITCORE_LDFLAGS) $(WK_WEBCORE_LDFLAGS) $(WK_REVEAL_LDFLAGS) $(OTHER_LDFLAGS_DELAY_INIT) $(OTHER_LDFLAGS_PLATFORM_$(WK_COCOA_TOUCH));
OTHER_LDFLAGS = $(inherited) -lgtest -lxml2 -force_load $(BUILT_PRODUCTS_DIR)/libTestWebKitAPI.a -framework JavaScriptCore -framework WebCore -framework WebKit -lWebCoreTestSupport -framework Metal -framework IOSurface $(WK_APPSERVERSUPPORT_LDFLAGS) $(WK_AUTHKIT_LDFLAGS) -framework Network -framework UniformTypeIdentifiers -framework CoreFoundation -framework CoreServices $(WK_BROWSERENGINEKIT_LDFLAGS) $(WK_HID_LDFLAGS) $(WK_IMAGEIO_LDFLAGS) $(WK_OPENGL_LDFLAGS) $(WK_PDFKIT_LDFLAGS) $(WK_SYSTEM_LDFLAGS) $(WK_UIKITMACHELPER_LDFLAGS) $(WK_VISIONKITCORE_LDFLAGS) $(WK_WEBCORE_LDFLAGS) $(WK_REVEAL_LDFLAGS) $(OTHER_LDFLAGS_DELAY_INIT) $(OTHER_LDFLAGS_PLATFORM_$(WK_COCOA_TOUCH));

OTHER_LDFLAGS_DELAY_INIT[sdk=iphone*] = -Wl,-delay_framework,CoreTelephony;
OTHER_LDFLAGS_DELAY_INIT[sdk=iphone*17.*] = ;
Expand Down
8 changes: 8 additions & 0 deletions Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@
44B28A0728D18ADA0010172C /* VectorCF.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 44B289FC28D18AAA0010172C /* VectorCF.cpp */; };
44CDE4D426EE6E4A009F6ACB /* TypeCastsCocoa.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44CDE4D326EE6E41009F6ACB /* TypeCastsCocoa.mm */; };
44CF31FD249941E8009CB6CB /* ContextMenuAction.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 44CF31FB24993F66009CB6CB /* ContextMenuAction.cpp */; };
44D3F8402BE1A74200BE0218 /* XMLParsing.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44D3F8382BE1A6BE00BE0218 /* XMLParsing.mm */; };
44D3F8432BE1DB0000BE0218 /* test.xml in Copy Resources */ = {isa = PBXBuildFile; fileRef = 44D3F8422BE1DAD800BE0218 /* test.xml */; };
44D5008E26FE9ED6000EB12F /* RetainPtrARC.mm in Sources */ = {isa = PBXBuildFile; fileRef = 44D5008D26FE9ED6000EB12F /* RetainPtrARC.mm */; settings = {COMPILER_FLAGS = "-fobjc-arc"; }; };
4628C8E92367ABD100B073F0 /* WKSecurityOrigin.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4628C8E82367ABBC00B073F0 /* WKSecurityOrigin.cpp */; };
462B4E8028E7B0E400653230 /* fragment-navigation-before-load-event.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 462B4E7828E7B0AF00653230 /* fragment-navigation-before-load-event.html */; };
Expand Down Expand Up @@ -1925,6 +1927,7 @@
524BBCA119E30C77002F1AF1 /* test.mp4 in Copy Resources */,
49C64FD728349C38005BF0C2 /* test.pages in Copy Resources */,
7AE9E5091AE5AE8B00CF874B /* test.pdf in Copy Resources */,
44D3F8432BE1DB0000BE0218 /* test.xml in Copy Resources */,
5C7101C725DD98B600686200 /* test_print.pdf in Copy Resources */,
F45EB60427739F34003571AE /* TestModalContainerControls.mlmodelc in Copy Resources */,
2E9896151D8F093800739892 /* text-and-password-inputs.html in Copy Resources */,
Expand Down Expand Up @@ -2423,6 +2426,8 @@
44C2FBE125E7592C00ABC72F /* WKAppHighlights.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKAppHighlights.mm; sourceTree = "<group>"; };
44CDE4D326EE6E41009F6ACB /* TypeCastsCocoa.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = TypeCastsCocoa.mm; sourceTree = "<group>"; };
44CF31FB24993F66009CB6CB /* ContextMenuAction.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContextMenuAction.cpp; sourceTree = "<group>"; };
44D3F8382BE1A6BE00BE0218 /* XMLParsing.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = XMLParsing.mm; sourceTree = "<group>"; };
44D3F8422BE1DAD800BE0218 /* test.xml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = test.xml; sourceTree = "<group>"; };
44D5008D26FE9ED6000EB12F /* RetainPtrARC.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = RetainPtrARC.mm; path = ns/RetainPtrARC.mm; sourceTree = "<group>"; };
44DBE9512BD2272900307F65 /* TextPlaceholderTests.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TextPlaceholderTests.mm; sourceTree = "<group>"; };
46061545275824B400AB613D /* WebLocks.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WebLocks.mm; sourceTree = "<group>"; };
Expand Down Expand Up @@ -5596,6 +5601,7 @@
C95984F61E36BCD7002C0D45 /* test-without-audio-track.mp4 */,
524BBCA019E30C63002F1AF1 /* test.mp4 */,
7AE9E5081AE5AE8B00CF874B /* test.pdf */,
44D3F8422BE1DAD800BE0218 /* test.xml */,
1C51534B261596BD00FBC4FE /* UserInstalledAhem.html */,
C9C9A91C21DED79400FDE96E /* video-with-play-button.html */,
07CD32F72065B72A0064A4BE /* video.html */,
Expand Down Expand Up @@ -5872,6 +5878,7 @@
74DEF21D2A946FD700E034A3 /* TestUTIRegistry.cpp */,
7498C3CF2A94435F009A387E /* TestUTIUtilities.cpp */,
CD89D0381C4EDB2A00040A04 /* WebCoreNSURLSession.mm */,
44D3F8382BE1A6BE00BE0218 /* XMLParsing.mm */,
);
path = cocoa;
sourceTree = "<group>";
Expand Down Expand Up @@ -6973,6 +6980,7 @@
0DE559ED2A6B0AAF009AA320 /* WKWebViewResize.mm in Sources */,
E520A36B25AFB76C00526CB9 /* WKWebViewTitlebarSeparatorTests.mm in Sources */,
3545E58927E2AD1300F1910E /* WritingModeTests.cpp in Sources */,
44D3F8402BE1A74200BE0218 /* XMLParsing.mm in Sources */,
C14D304624B4C3BA00480387 /* XPCEndpoint.mm in Sources */,
5C581D3227C8A01C006B3BC4 /* YouTubePluginReplacement.cpp in Sources */,
);
Expand Down
53 changes: 53 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebCore/cocoa/XMLParsing.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2024 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"

#include "PlatformUtilities.h"
#include "WebCoreTestSupport.h"
#include <WebCore/ParserContentPolicy.h>
#include <libxml/parser.h>

namespace TestWebKitAPI {

TEST(XMLParsing, WebCoreDoesNotBreakLibxml2)
{
// Parse XHTML in WebKit.
String chunk = "<x/>"_s;
auto result = WebCoreTestSupport::testDocumentFragmentParseXML(chunk, WebCore::DefaultParserContentPolicy);
EXPECT_TRUE(result);

NSString *resourceURL = [[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"xml" subdirectory:@"TestWebKitAPI.resources"] path];

// Parse an XML document in libxml2.
xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
EXPECT_TRUE(!!ctxt);
xmlDocPtr doc = xmlCtxtReadFile(ctxt, resourceURL.UTF8String, nullptr, XML_PARSE_NONET);
EXPECT_TRUE(!!doc);
xmlFreeDoc(doc);
xmlFreeParserCtxt(ctxt);
}

} // namespace TestWebKitAPI
2 changes: 2 additions & 0 deletions Tools/TestWebKitAPI/Tests/WebKit/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" ?>
<x></x>

0 comments on commit 8130984

Please sign in to comment.