xref: /PHP-5.5/ext/xml/tests/bug32001b.phpt (revision 610c7fbe)
1--TEST--
2Bug #32001 (xml_parse*() goes into infinite loop when autodetection in effect), using EUC-JP, Shift_JIS, GB2312
3--SKIPIF--
4<?php
5require_once("skipif.inc");
6if (!extension_loaded('iconv')) die ("skip iconv extension not available");
7foreach(array('EUC-JP', 'Shift_JISP', 'GB2312') as $encoding) {
8	if (@xml_parser_create($encoding) === false) die("skip libxml2 does not support $encoding encoding");
9}
10?>
11--FILE--
12<?php
13class testcase {
14	private $encoding;
15	private $bom;
16	private $prologue;
17	private $tags;
18	private $chunk_size;
19
20	function testcase($enc, $chunk_size = 0, $bom = 0, $omit_prologue = 0) {
21		$this->encoding = $enc;
22		$this->chunk_size = $chunk_size;
23		$this->bom = $bom;
24		$this->prologue = !$omit_prologue;
25		$this->tags = array();
26	}
27
28	function start_element($parser, $name, $attrs) {
29		$attrs = array_map('bin2hex', $attrs);
30		$this->tags[] = bin2hex($name).": ".implode(', ', $attrs);
31	}
32
33	function end_element($parser, $name) {
34	}
35
36	function run() {
37		$data = '';
38
39		if ($this->prologue) {
40			$canonical_name = preg_replace('/BE|LE/i', '', $this->encoding);
41			$data .= "<?xml version=\"1.0\" encoding=\"$canonical_name\" ?>\n";
42		}
43
44		$data .= <<<HERE
45<テスト:テスト1 xmlns:テスト="http://www.example.com/テスト/" テスト="テスト">
46  <テスト:テスト2 テスト="テスト">
47	<テスト:テスト3>
48	  test!
49	</テスト:テスト3>
50  </テスト:テスト2>
51</テスト:テスト1>
52HERE;
53
54		$data = iconv("UTF-8", $this->encoding, $data);
55
56		$parser = xml_parser_create(NULL);
57		xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
58		xml_set_element_handler($parser, "start_element", "end_element");
59		xml_set_object($parser, $this);
60
61		if ($this->chunk_size == 0) {
62			$success = @xml_parse($parser, $data, true);
63		} else {
64			for ($offset = 0; $offset < strlen($data);
65					$offset += $this->chunk_size) {
66				$success = @xml_parse($parser, substr($data, $offset, $this->chunk_size), false);
67				if (!$success) {
68					break;
69				}
70			}
71			if ($success) {
72				$success = @xml_parse($parser, "", true);
73			}
74		}
75
76		echo "Encoding: $this->encoding\n";
77		echo "XML Prologue: ".($this->prologue ? 'present': 'not present'), "\n";
78		echo "Chunk size: ".($this->chunk_size ? "$this->chunk_size byte(s)\n": "all data at once\n");
79		echo "BOM: ".($this->bom ? 'prepended': 'not prepended'), "\n";
80
81		if ($success) {
82			var_dump($this->tags);
83		} else {
84			echo "[Error] ", xml_error_string(xml_get_error_code($parser)), "\n";
85		}
86	}
87}
88$suite = array(
89	new testcase("EUC-JP"  ,  0),
90	new testcase("EUC-JP"  ,  1),
91	new testcase("Shift_JIS", 0),
92	new testcase("Shift_JIS", 1),
93	new testcase("GB2312",    0),
94	new testcase("GB2312",    1),
95);
96
97if (XML_SAX_IMPL == 'libxml') {
98  echo "libxml2 Version => " . LIBXML_DOTTED_VERSION. "\n";
99} else {
100  echo "libxml2 Version => NONE\n";
101}
102
103foreach ($suite as $testcase) {
104	$testcase->run();
105}
106
107// vim600: sts=4 sw=4 ts=4 encoding=UTF-8
108?>
109--EXPECTF--
110libxml2 Version => %s
111Encoding: EUC-JP
112XML Prologue: present
113Chunk size: all data at once
114BOM: not prepended
115array(3) {
116  [0]=>
117  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
118  [1]=>
119  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
120  [2]=>
121  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
122}
123Encoding: EUC-JP
124XML Prologue: present
125Chunk size: 1 byte(s)
126BOM: not prepended
127array(3) {
128  [0]=>
129  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
130  [1]=>
131  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
132  [2]=>
133  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
134}
135Encoding: Shift_JIS
136XML Prologue: present
137Chunk size: all data at once
138BOM: not prepended
139array(3) {
140  [0]=>
141  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
142  [1]=>
143  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
144  [2]=>
145  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
146}
147Encoding: Shift_JIS
148XML Prologue: present
149Chunk size: 1 byte(s)
150BOM: not prepended
151array(3) {
152  [0]=>
153  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
154  [1]=>
155  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
156  [2]=>
157  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
158}
159Encoding: GB2312
160XML Prologue: present
161Chunk size: all data at once
162BOM: not prepended
163array(3) {
164  [0]=>
165  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
166  [1]=>
167  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
168  [2]=>
169  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
170}
171Encoding: GB2312
172XML Prologue: present
173Chunk size: 1 byte(s)
174BOM: not prepended
175array(3) {
176  [0]=>
177  string(128) "e38386e382b9e383883ae38386e382b9e3838831: 687474703a2f2f7777772e6578616d706c652e636f6d2fe38386e382b9e383882f, e38386e382b9e38388"
178  [1]=>
179  string(60) "e38386e382b9e383883ae38386e382b9e3838832: e38386e382b9e38388"
180  [2]=>
181  string(42) "e38386e382b9e383883ae38386e382b9e3838833: "
182}
183