1--TEST--
2mysqli_set_local_infile_handler() - report shorter buffer
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8
9if (!function_exists('mysqli_set_local_infile_handler'))
10	die("skip - function not available.");
11
12require_once('connect.inc');
13if (!$TEST_EXPERIMENTAL)
14	die("skip - experimental (= unsupported) feature");
15
16if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
17	die("skip Cannot connect to MySQL");
18
19include_once("local_infile_tools.inc");
20if ($msg = check_local_infile_support($link, $engine))
21	die(sprintf("skip %s, [%d] %s", $msg, $link->errno, $link->error));
22
23mysqli_close($link);
24?>
25--INI--
26mysqli.allow_local_infile=1
27--FILE--
28<?php
29	require_once('connect.inc');
30	require_once('local_infile_tools.inc');
31	require_once('table.inc');
32
33	function callback_short_len($fp, &$buffer, $buflen, &$error) {
34		static $invocation = 0;
35
36		printf("Callback: %d\n", $invocation);
37
38		$invocation++;
39		if (!is_resource($fp))
40			printf("[012] First argument passed to callback is not a resource but %s/%s\n",
41				$fp, gettype($fp));
42
43		if (!$buffer = fread($fp, $buflen)) {
44			if ($invocation == 1) {
45				printf("[013] Cannot read from stream\n");
46				$error = 'Cannot read from stream';
47			} else {
48				return strlen($buffer);
49			}
50		}
51
52		$lines = explode("\n", $buffer);
53		if (count($lines) != 4 && strlen($buffer) > 0) {
54			printf("[014] Test is too simple to handle a buffer of size %d that cannot hold all lines\n", $buflen);
55			$error = 'Parser too simple';
56		}
57
58		$buffer = '';
59		foreach ($lines as $k => $line) {
60			if ('' === trim($line))
61					continue;
62
63			$columns = explode(';', $line);
64			if (empty($columns)) {
65				printf("[015] Cannot parse columns\n");
66				$error = 'Cannot parse columns';
67			}
68
69			// increase id column value
70			$columns[0] += 1;
71			$buffer .= implode(';', $columns);
72			$buffer .= "\n";
73		}
74
75		/* report the wrong length */
76		return strlen($buffer) - 1;
77	}
78
79	$file = create_standard_csv(1);
80	$expected = array(
81		array('id' => 98,   'label' => 'x'),
82		array('id' => 99,   'label' => 'y'),
83		array('id' => 100,  'label' => 'z'),
84	);
85	try_handler(20, $link, $file, 'callback_short_len', $expected);
86
87	mysqli_close($link);
88	print "done!";
89?>
90--CLEAN--
91<?php
92	require_once("clean_table.inc");
93?>
94--EXPECTF--
95Callback set to 'callback_short_len'
96Callback: 0
97
98Warning: mysqli_query(): Mismatch between the return value of the callback and the content length of the buffer. in %s on line %d
99[022] LOAD DATA failed, [2000] Mismatch between the return value of the callback and the content length of the buffer.
100[024/0] [0] ''
101done!