summaryrefslogtreecommitdiff
path: root/ext/libxml/tests
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2013-03-14 05:42:27 +0000
committer <>2013-04-03 16:25:08 +0000
commitc4dd7a1a684490673e25aaf4fabec5df138854c4 (patch)
tree4d57c44caae4480efff02b90b9be86f44bf25409 /ext/libxml/tests
downloadphp2-master.tar.gz
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'ext/libxml/tests')
-rw-r--r--ext/libxml/tests/001.phpt31
-rw-r--r--ext/libxml/tests/002.phpt87
-rw-r--r--ext/libxml/tests/003.phpt28
-rw-r--r--ext/libxml/tests/004.phpt57
-rw-r--r--ext/libxml/tests/bug42112.phpt31
-rw-r--r--ext/libxml/tests/bug54440.phpt51
-rw-r--r--ext/libxml/tests/bug61367-read.phpt60
-rw-r--r--ext/libxml/tests/bug61367-write.phpt46
-rw-r--r--ext/libxml/tests/bug63389.phpt14
-rw-r--r--ext/libxml/tests/libxml_set_external_entity_loader_basic.phpt48
-rw-r--r--ext/libxml/tests/libxml_set_external_entity_loader_error1.phpt39
-rw-r--r--ext/libxml/tests/libxml_set_external_entity_loader_variation1.phpt72
-rw-r--r--ext/libxml/tests/libxml_set_external_entity_loader_variation2.phpt45
-rw-r--r--ext/libxml/tests/test.xml8
14 files changed, 617 insertions, 0 deletions
diff --git a/ext/libxml/tests/001.phpt b/ext/libxml/tests/001.phpt
new file mode 100644
index 0000000..6f68cb4
--- /dev/null
+++ b/ext/libxml/tests/001.phpt
@@ -0,0 +1,31 @@
+--TEST--
+libxml_use_internal_errors()
+--SKIPIF--
+<?php if (!extension_loaded('libxml')) die('skip'); ?>
+--FILE--
+<?php
+
+var_dump(libxml_use_internal_errors(false));
+var_dump(libxml_use_internal_errors(true));
+var_dump(libxml_use_internal_errors());
+var_dump(libxml_use_internal_errors(new stdclass));
+
+var_dump(libxml_get_errors());
+var_dump(libxml_get_last_error());
+
+var_dump(libxml_clear_errors());
+
+echo "Done\n";
+?>
+--EXPECTF--
+bool(false)
+bool(false)
+bool(true)
+
+Warning: libxml_use_internal_errors() expects parameter 1 to be boolean, object given in %s001.php on line 6
+NULL
+array(0) {
+}
+bool(false)
+NULL
+Done
diff --git a/ext/libxml/tests/002.phpt b/ext/libxml/tests/002.phpt
new file mode 100644
index 0000000..f803d7a
--- /dev/null
+++ b/ext/libxml/tests/002.phpt
@@ -0,0 +1,87 @@
+--TEST--
+libxml_get_errors()
+--SKIPIF--
+<?php if (!extension_loaded('simplexml')) die('skip'); ?>
+--FILE--
+<?php
+
+var_dump(libxml_use_internal_errors(true));
+
+$xmlstr = <<< XML
+<?xml version='1.0' standalone='yes'?>
+ <movies>
+ <movie>
+ <titles>PHP: Behind the Parser</title>
+ </movie>
+ </movies>
+XML;
+
+$doc = simplexml_load_string($xmlstr);
+$xml = explode("\n", $xmlstr);
+
+if (!$doc) {
+ $errors = libxml_get_errors();
+
+ foreach ($errors as $error) {
+ echo display_xml_error($error, $xml);
+ }
+
+ var_dump(libxml_get_last_error());
+}
+
+
+function display_xml_error($error, $xml)
+{
+ $return = $xml[$error->line - 1] . "\n";
+ $return .= str_repeat('-', $error->column) . "^\n";
+
+ switch ($error->level) {
+ case LIBXML_ERR_WARNING:
+ $return .= "Warning $error->code: ";
+ break;
+ case LIBXML_ERR_ERROR:
+ $return .= "Error $error->code: ";
+ break;
+ case LIBXML_ERR_FATAL:
+ $return .= "Fatal Error $error->code: ";
+ break;
+ }
+
+ $return .= trim($error->message) . "\n Line: $error->line" . "\n Column: $error->column";
+
+ if ($error->file) {
+ $return .= "\n File: $error->file";
+ }
+
+ return "$return\n\n--------------------------------------------\n\n";
+}
+
+
+echo "Done\n";
+?>
+--EXPECTF--
+bool(false)
+ <titles>PHP: Behind the Parser</title>
+%s
+Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title
+ Line: 4
+ Column: %d
+
+--------------------------------------------
+
+object(LibXMLError)#%d (6) {
+ ["level"]=>
+ int(3)
+ ["code"]=>
+ int(76)
+ ["column"]=>
+ int(%d)
+ ["message"]=>
+ string(57) "Opening and ending tag mismatch: titles line 4 and title
+"
+ ["file"]=>
+ string(0) ""
+ ["line"]=>
+ int(4)
+}
+Done
diff --git a/ext/libxml/tests/003.phpt b/ext/libxml/tests/003.phpt
new file mode 100644
index 0000000..dcf6c0b
--- /dev/null
+++ b/ext/libxml/tests/003.phpt
@@ -0,0 +1,28 @@
+--TEST--
+libxml_use_internal_errors() memory leaks
+--SKIPIF--
+<?php if (!extension_loaded('simplexml')) die('skip'); ?>
+--FILE--
+<?php
+var_dump(libxml_use_internal_errors(true));
+
+$xmlstr = <<< XML
+<?xml version='1.0' standalone='yes'?>
+ <movies>
+ <movie>
+ <titles>PHP: Behind the Parser</title>
+ </movie>
+ </movies>
+XML;
+
+simplexml_load_string($xmlstr);
+
+// test memleaks here
+var_dump(libxml_use_internal_errors(false));
+
+echo "Done\n";
+?>
+--EXPECTF--
+bool(false)
+bool(true)
+Done
diff --git a/ext/libxml/tests/004.phpt b/ext/libxml/tests/004.phpt
new file mode 100644
index 0000000..aa87ab7
--- /dev/null
+++ b/ext/libxml/tests/004.phpt
@@ -0,0 +1,57 @@
+--TEST--
+libxml_set_streams_context()
+--SKIPIF--
+<?php if (!extension_loaded('dom')) die('skip'); ?>
+--FILE--
+<?php
+
+$ctxs = array(
+ NULL,
+ 'bogus',
+ 123,
+ new stdclass,
+ array('a'),
+ stream_context_create(),
+ stream_context_create(array('file')),
+ stream_context_create(array('file' => array('some_opt' => 'aaa')))
+);
+
+
+foreach ($ctxs as $ctx) {
+ var_dump(libxml_set_streams_context($ctx));
+ $dom = new DOMDocument();
+ var_dump($dom->load(dirname(__FILE__).'/test.xml'));
+}
+
+echo "Done\n";
+
+?>
+--EXPECTF--
+Warning: stream_context_create(): options should have the form ["wrappername"]["optionname"] = $value in %s004.php on line %d
+
+Warning: libxml_set_streams_context() expects parameter 1 to be resource, null given in %s004.php on line %d
+NULL
+bool(true)
+
+Warning: libxml_set_streams_context() expects parameter 1 to be resource, string given in %s004.php on line %d
+NULL
+bool(true)
+
+Warning: libxml_set_streams_context() expects parameter 1 to be resource, integer given in %s004.php on line %d
+NULL
+bool(true)
+
+Warning: libxml_set_streams_context() expects parameter 1 to be resource, object given in %s004.php on line %d
+NULL
+bool(true)
+
+Warning: libxml_set_streams_context() expects parameter 1 to be resource, array given in %s004.php on line %d
+NULL
+bool(true)
+NULL
+bool(true)
+NULL
+bool(true)
+NULL
+bool(true)
+Done
diff --git a/ext/libxml/tests/bug42112.phpt b/ext/libxml/tests/bug42112.phpt
new file mode 100644
index 0000000..b5a3f40
--- /dev/null
+++ b/ext/libxml/tests/bug42112.phpt
@@ -0,0 +1,31 @@
+--TEST--
+Bug #42112 (deleting a node produces memory corruption)
+--SKIPIF--
+<?php if (!extension_loaded('dom')) die('skip'); ?>
+--FILE--
+<?php
+$xml = <<<EOXML
+<root><child xml:id="id1">baz</child></root>
+EOXML;
+
+function remove_node($doc) {
+ $node = $doc->getElementById( 'id1' );
+ print 'Deleting Node: '.$node->nodeName."\n";
+ $node->parentNode->removeChild( $node );
+}
+
+$doc = new DOMDocument();
+$doc->loadXML($xml);
+
+remove_node($doc);
+
+$node = $doc->getElementById( 'id1' );
+if ($node) {
+ print 'Found Node: '.$node->nodeName."\n";
+}
+$root = $doc->documentElement;
+print 'Root Node: '.$root->nodeName."\n";
+?>
+--EXPECT--
+Deleting Node: child
+Root Node: root
diff --git a/ext/libxml/tests/bug54440.phpt b/ext/libxml/tests/bug54440.phpt
new file mode 100644
index 0000000..4074ff9
--- /dev/null
+++ b/ext/libxml/tests/bug54440.phpt
@@ -0,0 +1,51 @@
+--TEST--
+Bug #54440: libxml extension ignores default context
+--SKIPIF--
+<?php if (!extension_loaded('simplexml')) die('skip simplexml required for this test'); ?>
+--FILE--
+<?php
+
+class TestWrapper {
+
+function stream_open($path, $mode, $options, &$opened_path)
+{
+ if ($this->context)
+ print_r(stream_context_get_options($this->context));
+ return false;
+}
+
+function url_stat($path, $flags)
+{
+return array();
+}
+
+}
+
+stream_wrapper_register("test", "TestWrapper")
+ or die("Failed to register protocol");
+
+$ctx1 = stream_context_create(array('test'=>array('test'=>'test 1')));
+$ctx2 = stream_context_create(array('test'=>array('test'=>'test 2')));
+
+stream_context_set_default(stream_context_get_options($ctx1));
+@simplexml_load_file('test://sdfsdf');
+
+libxml_set_streams_context($ctx2);
+@simplexml_load_file('test://sdfsdf');
+--EXPECT--
+Array
+(
+ [test] => Array
+ (
+ [test] => test 1
+ )
+
+)
+Array
+(
+ [test] => Array
+ (
+ [test] => test 2
+ )
+
+)
diff --git a/ext/libxml/tests/bug61367-read.phpt b/ext/libxml/tests/bug61367-read.phpt
new file mode 100644
index 0000000..94da3d8
--- /dev/null
+++ b/ext/libxml/tests/bug61367-read.phpt
@@ -0,0 +1,60 @@
+--TEST--
+Bug #61367: open_basedir bypass in libxml RSHUTDOWN: read test
+--SKIPIF--
+<?php if(!extension_loaded('dom')) echo 'skip'; ?>
+--INI--
+open_basedir=.
+error_reporting=E_ALL & ~E_NOTICE
+--FILE--
+<?php
+/*
+ * Note: Using error_reporting=E_ALL & ~E_NOTICE to supress "Trying to get property of non-object" notices.
+ */
+class StreamExploiter {
+ public function stream_close ( ) {
+ $doc = new DOMDocument;
+ $doc->resolveExternals = true;
+ $doc->substituteEntities = true;
+ $dir = htmlspecialchars(dirname(getcwd()));
+ $dir = str_replace('\\', '/', $dir); // fix for windows
+ $doc->loadXML( <<<XML
+<!DOCTYPE doc [
+ <!ENTITY file SYSTEM "file:///$dir/bad">
+]>
+<doc>&file;</doc>
+XML
+ );
+ print $doc->documentElement->firstChild->nodeValue;
+ }
+
+ public function stream_open ( $path , $mode , $options , &$opened_path ) {
+ return true;
+ }
+}
+
+var_dump(mkdir('test_bug_61367'));
+var_dump(mkdir('test_bug_61367/base'));
+var_dump(file_put_contents('test_bug_61367/bad', 'blah'));
+var_dump(chdir('test_bug_61367/base'));
+
+stream_wrapper_register( 'exploit', 'StreamExploiter' );
+$s = fopen( 'exploit://', 'r' );
+
+?>
+--CLEAN--
+<?php
+unlink('test_bug_61367/bad');
+rmdir('test_bug_61367/base');
+rmdir('test_bug_61367');
+?>
+--EXPECTF--
+bool(true)
+bool(true)
+int(4)
+bool(true)
+
+Warning: DOMDocument::loadXML(): I/O warning : failed to load external entity "file:///%s/test_bug_61367/bad" in %s on line %d
+
+Warning: DOMDocument::loadXML(): Failure to process entity file in Entity, line: 4 in %s on line %d
+
+Warning: DOMDocument::loadXML(): Entity 'file' not defined in Entity, line: 4 in %s on line %d
diff --git a/ext/libxml/tests/bug61367-write.phpt b/ext/libxml/tests/bug61367-write.phpt
new file mode 100644
index 0000000..e18b071
--- /dev/null
+++ b/ext/libxml/tests/bug61367-write.phpt
@@ -0,0 +1,46 @@
+--TEST--
+Bug #61367: open_basedir bypass in libxml RSHUTDOWN: write test
+--SKIPIF--
+<?php if(!extension_loaded('dom')) echo 'skip'; ?>
+--INI--
+open_basedir=.
+--FILE--
+<?php
+
+class StreamExploiter {
+ public function stream_close ( ) {
+ $doc = new DOMDocument;
+ $doc->appendChild($doc->createTextNode('hello'));
+ var_dump($doc->save(dirname(getcwd()) . '/bad'));
+ }
+
+ public function stream_open ( $path , $mode , $options , &$opened_path ) {
+ return true;
+ }
+}
+
+var_dump(mkdir('test_bug_61367'));
+var_dump(mkdir('test_bug_61367/base'));
+var_dump(file_put_contents('test_bug_61367/bad', 'blah'));
+var_dump(chdir('test_bug_61367/base'));
+
+stream_wrapper_register( 'exploit', 'StreamExploiter' );
+$s = fopen( 'exploit://', 'r' );
+
+?>
+--CLEAN--
+<?php
+@unlink('test_bug_61367/bad');
+rmdir('test_bug_61367/base');
+rmdir('test_bug_61367');
+?>
+--EXPECTF--
+bool(true)
+bool(true)
+int(4)
+bool(true)
+
+Warning: DOMDocument::save(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
+
+Warning: DOMDocument::save(%s): failed to open stream: Operation not permitted in %s on line %d
+bool(false)
diff --git a/ext/libxml/tests/bug63389.phpt b/ext/libxml/tests/bug63389.phpt
new file mode 100644
index 0000000..e9498aa
--- /dev/null
+++ b/ext/libxml/tests/bug63389.phpt
@@ -0,0 +1,14 @@
+--TEST--
+Bug #63389 (Missing context check on libxml_set_streams_context() causes memleak)
+--SKIPIF--
+<?php if (!extension_loaded('libxml')) die('skip'); ?>
+--FILE--
+<?php
+$fp = fopen("php://input", "r");
+libxml_set_streams_context($fp);
+libxml_set_streams_context("a");
+echo "okey";
+?>
+--EXPECTF--
+Warning: libxml_set_streams_context() expects parameter 1 to be resource, string given in %sbug63389.php on line %d
+okey
diff --git a/ext/libxml/tests/libxml_set_external_entity_loader_basic.phpt b/ext/libxml/tests/libxml_set_external_entity_loader_basic.phpt
new file mode 100644
index 0000000..51ab777
--- /dev/null
+++ b/ext/libxml/tests/libxml_set_external_entity_loader_basic.phpt
@@ -0,0 +1,48 @@
+--TEST--
+libxml_set_external_entity_loader() basic test
+--SKIPIF--
+<?php if (!extension_loaded('dom')) die('skip'); ?>
+--FILE--
+<?php
+$xml = <<<XML
+<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar">
+<foo>bar</foo>
+XML;
+
+$dtd = <<<DTD
+<!ELEMENT foo (#PCDATA)>
+DTD;
+
+libxml_set_external_entity_loader(
+ function ($public, $system, $context) use($dtd){
+ var_dump($public);
+ var_dump($system);
+ var_dump($context);
+ $f = fopen("php://temp", "r+");
+ fwrite($f, $dtd);
+ rewind($f);
+ return $f;
+ }
+);
+
+$dd = new DOMDocument;
+$r = $dd->loadXML($xml);
+var_dump($dd->validate());
+
+echo "Done.\n";
+
+--EXPECT--
+string(10) "-//FOO/BAR"
+string(25) "http://example.com/foobar"
+array(4) {
+ ["directory"]=>
+ NULL
+ ["intSubName"]=>
+ NULL
+ ["extSubURI"]=>
+ NULL
+ ["extSubSystem"]=>
+ NULL
+}
+bool(true)
+Done.
diff --git a/ext/libxml/tests/libxml_set_external_entity_loader_error1.phpt b/ext/libxml/tests/libxml_set_external_entity_loader_error1.phpt
new file mode 100644
index 0000000..5ed079d
--- /dev/null
+++ b/ext/libxml/tests/libxml_set_external_entity_loader_error1.phpt
@@ -0,0 +1,39 @@
+--TEST--
+libxml_set_external_entity_loader() error: bad arguments
+--SKIPIF--
+<?php if (!extension_loaded('dom')) die('skip'); ?>
+--FILE--
+<?php
+$xml = <<<XML
+<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar">
+<foo>bar</foo>
+XML;
+
+$dd = new DOMDocument;
+$r = $dd->loadXML($xml);
+
+var_dump(libxml_set_external_entity_loader([]));
+var_dump(libxml_set_external_entity_loader());
+var_dump(libxml_set_external_entity_loader(function() {}, 2));
+
+var_dump(libxml_set_external_entity_loader(function($a, $b, $c, $d) {}));
+var_dump($dd->validate());
+
+echo "Done.\n";
+
+--EXPECTF--
+Warning: libxml_set_external_entity_loader() expects parameter 1 to be a valid callback, array must have exactly two members in %s on line %d
+NULL
+
+Warning: libxml_set_external_entity_loader() expects exactly 1 parameter, 0 given in %s on line %d
+NULL
+
+Warning: libxml_set_external_entity_loader() expects exactly 1 parameter, 2 given in %s on line %d
+NULL
+bool(true)
+
+Warning: Missing argument 4 for {closure}() in %s on line %d
+
+Warning: DOMDocument::validate(): Could not load the external subset "http://example.com/foobar" in %s on line %d
+bool(false)
+Done.
diff --git a/ext/libxml/tests/libxml_set_external_entity_loader_variation1.phpt b/ext/libxml/tests/libxml_set_external_entity_loader_variation1.phpt
new file mode 100644
index 0000000..c9c4594
--- /dev/null
+++ b/ext/libxml/tests/libxml_set_external_entity_loader_variation1.phpt
@@ -0,0 +1,72 @@
+--TEST--
+libxml_set_external_entity_loader() variation: resolve externals and entities
+--SKIPIF--
+<?php if (!extension_loaded('dom')) die('skip'); ?>
+--FILE--
+<?php
+chdir(__DIR__);
+$xml = <<<XML
+<!DOCTYPE foo PUBLIC "-//FOO/BAR" "http://example.com/foobar">
+<foo>bar&fooz;</foo>
+XML;
+
+$dtd = <<<DTD
+<!ELEMENT foo (#PCDATA)>
+<!ENTITY % fooentity PUBLIC
+ "-//FOO/ENTITY"
+ "fooentity.ent">
+%fooentity;
+DTD;
+
+$entity = <<<ENT
+<!ENTITY fooz "baz">
+ENT;
+
+libxml_set_external_entity_loader(
+ function ($public, $system, $context) use($dtd,$entity){
+ static $first = true;
+ var_dump($public);
+ var_dump($system);
+ var_dump($context);
+ $f = fopen("php://temp", "r+");
+ fwrite($f, $first ? $dtd : $entity);
+ $first = false;
+ rewind($f);
+ return $f;
+ }
+);
+
+$dd = new DOMDocument;
+$dd->resolveExternals = true;
+$r = $dd->loadXML($xml);
+var_dump($dd->validate());
+
+echo "Done.\n";
+
+--EXPECTF--
+string(10) "-//FOO/BAR"
+string(25) "http://example.com/foobar"
+array(4) {
+ ["directory"]=>
+ string(%d) "%s"
+ ["intSubName"]=>
+ string(3) "foo"
+ ["extSubURI"]=>
+ string(25) "http://example.com/foobar"
+ ["extSubSystem"]=>
+ string(10) "-//FOO/BAR"
+}
+string(13) "-//FOO/ENTITY"
+string(32) "http://example.com/fooentity.ent"
+array(4) {
+ ["directory"]=>
+ string(%d) "%s"
+ ["intSubName"]=>
+ string(3) "foo"
+ ["extSubURI"]=>
+ string(25) "http://example.com/foobar"
+ ["extSubSystem"]=>
+ string(10) "-//FOO/BAR"
+}
+bool(true)
+Done.
diff --git a/ext/libxml/tests/libxml_set_external_entity_loader_variation2.phpt b/ext/libxml/tests/libxml_set_external_entity_loader_variation2.phpt
new file mode 100644
index 0000000..b6251be
--- /dev/null
+++ b/ext/libxml/tests/libxml_set_external_entity_loader_variation2.phpt
@@ -0,0 +1,45 @@
+--TEST--
+libxml_set_external_entity_loader() variation: restore original handler; returning NULL
+--SKIPIF--
+<?php if (!extension_loaded('dom')) die('skip'); ?>
+--CLEAN--
+<?php
+@unlink(__DIR__ . "/foobar.dtd");
+--FILE--
+<?php
+chdir(__DIR__);
+$xml = <<<XML
+<!DOCTYPE foo PUBLIC "-//FOO/BAR" "foobar.dtd">
+<foo>bar</foo>
+XML;
+
+$dtd = <<<DTD
+<!ELEMENT foo (#PCDATA)>
+DTD;
+
+
+libxml_set_external_entity_loader(
+ function ($public, $system, $context) {
+ var_dump($public,$system);
+ return null;
+ }
+);
+
+$dd = new DOMDocument;
+$r = $dd->loadXML($xml);
+var_dump($dd->validate());
+
+libxml_set_external_entity_loader(NULL);
+file_put_contents(__DIR__ . "/foobar.dtd", $dtd);
+var_dump($dd->validate());
+
+echo "Done.\n";
+
+--EXPECTF--
+string(10) "-//FOO/BAR"
+string(%d) "%sfoobar.dtd"
+
+Warning: DOMDocument::validate(): Could not load the external subset "foobar.dtd" in %s on line %d
+bool(false)
+bool(true)
+Done.
diff --git a/ext/libxml/tests/test.xml b/ext/libxml/tests/test.xml
new file mode 100644
index 0000000..fc1d328
--- /dev/null
+++ b/ext/libxml/tests/test.xml
@@ -0,0 +1,8 @@
+<library>
+ <book>
+ <title>PHP made simple</title>
+ </book>
+ <book>
+ <title>learn PHP easily</title>
+ </book>
+</library>