1--TEST--
2mysqli_stmt_send_long_data() - exceed packet size, libmysql - bug #26824
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifemb.inc');
7require_once('skipifconnectfailure.inc');
8
9if (stristr(mysqli_get_client_info(), 'mysqlnd'))
10	die("skip: test for libmysql");
11?>
12--FILE--
13<?php
14	require('table.inc');
15
16	if (!$stmt = mysqli_stmt_init($link))
17		printf("[001] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
18
19	if (!mysqli_query($link, "DROP TABLE IF EXISTS test"))
20		printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
21
22	if (!mysqli_query($link, sprintf("CREATE TABLE test(id INT NOT NULL AUTO_INCREMENT, label LONGBLOB, PRIMARY KEY(id)) ENGINE = %s", $engine)))
23		printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
24
25	if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (?, ?)"))
26		printf("[004] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
27
28	$id = null;
29	$label = null;
30	if (!mysqli_stmt_bind_param($stmt, "ib", $id, $label))
31		printf("[005] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
32
33	if (!$res = mysqli_query($link, "SHOW VARIABLES LIKE 'max_allowed_packet'"))
34		printf("[006] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
35
36	if (!$row = mysqli_fetch_assoc($res))
37		printf("[007] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
38
39	mysqli_free_result($res);
40
41	if (0 === ($max_allowed_packet = (int)$row['Value']))
42		printf("[008] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
43
44	// let's ignore upper limits for LONGBLOB (2^32) ...
45	// maximum packet size up to which we test is 10M
46	$tmp = '';
47	$blob = '';
48	for ($i = 0; $i < 1024; $i++) {
49		$tmp .= 'a';
50	}
51
52	$limit = min(floor($max_allowed_packet / 1024 / 2), 10240);
53	for ($i = 0; $i < $limit; $i++)
54		$blob .= $tmp;
55
56	assert(strlen($blob) <= $max_allowed_packet);
57
58	if (true != ($tmp = mysqli_stmt_send_long_data($stmt, 1, $blob)))
59		printf("[009] Expecting boolean/true, got %s/%s. [%d] %s\n",
60			gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
61
62	$id = 1;
63	if (true !== mysqli_stmt_execute($stmt))
64		printf("[010] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
65
66	/*
67	TODO - we skip this part of the test for now, because of bugs.mysql.com/26824
68	if (floor($max_allowed_packet / 1024 / 2) <= 10240) {
69			// test with a blob smaller than 10M allows us to test
70			// for too long packages without wasting too much memory
71			$limit = $max_allowed_packet - strlen($blob) + 1;
72			$blob2 = $blob;
73			for ($i = 0; $i < $limit; $i++)
74					$blob2 .= 'b';
75
76			assert(strlen($blob2) > $max_allowed_packet);
77
78			if (true != ($tmp = mysqli_stmt_send_long_data($stmt, 1, $blob2)))
79					printf("[011] Expecting boolean/false, got %s/%s. [%d] %s\n",
80							gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
81
82			$id = 2;
83			if (false !== ($tmp = mysqli_stmt_execute($stmt)))
84					printf("[012] Expecting boolean/false, got %s/%s, [%d] %s\n",
85							gettype($tmp), $tmp, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
86	}
87	*/
88	mysqli_stmt_close($stmt);
89	mysqli_close($link);
90
91	print "done!";
92?>
93--CLEAN--
94<?php
95	require_once("clean_table.inc");
96?>
97--EXPECTF--
98done!