1--TEST--
2INSERT and packet overflow
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7
8if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
9	die(sprintf("SKIP [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error()));
10
11$max_len = pow(2, 24);
12if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
13	die(sprintf("SKIP [%d] %s\n", mysqli_errno($link), mysqli_error($link)));
14
15if (!mysqli_query($link, "SET NAMES 'latin1'"))
16	die(sprintf("SKIP [%d] %s\n", mysqli_errno($link), mysqli_error($link)));
17
18mysqli_close($link);
19?>
20--INI--
21memory_limit=256M
22--FILE--
23<?php
24	require('connect.inc');
25	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
26		printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
27
28	if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
29		printf("[002] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
30
31	if (!$row = mysqli_fetch_assoc($res))
32		printf("[003] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
33
34	mysqli_free_result($res);
35
36	if (0 === ($org_max_allowed_packet = (int)$row['Value']))
37		printf("[004] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
38
39	$max_len = pow(2, 24);
40	if ($org_max_allowed_packet < $max_len) {
41		if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . ($max_len + 100))) {
42			if (1227 == mysqli_errno($link)) {
43				/* [1227] Access denied; you need the SUPER privilege for this operation */
44				print "done!";
45				exit(0);
46			} else {
47				printf("[005] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
48			}
49		}
50	}
51	mysqli_close($link);
52	if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
53		printf("[006] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
54
55	if (!mysqli_query($link, "SET NAMES 'latin1'"))
56		printf("[007] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
57
58	if (!$res = mysqli_query($link, "SHOW GLOBAL VARIABLES LIKE 'max_allowed_packet'"))
59		printf("[008] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
60
61	if (!$row = mysqli_fetch_assoc($res))
62		printf("[009] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
63
64	mysqli_free_result($res);
65
66	if (0 === ($max_allowed_packet = (int)$row['Value']))
67		printf("[010] Cannot determine max_allowed_packet size and/or bogus max_allowed_packet setting used.\n");
68
69	$max_len = pow(2, 24);
70	if ($max_allowed_packet < $max_len) {
71		printf("[011] Failed to change max_allowed_packet");
72	}
73
74	if (!mysqli_query($link, "CREATE TABLE test(col_blob LONGBLOB) ENGINE=" . $engine))
75		printf("[012] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
76
77	$query_prefix = "INSERT INTO test(col_blob) VALUES ('";
78	$query_postfix = "')";
79	$query_len = strlen($query_prefix) + strlen($query_postfix);
80	$com_query_len = 2;
81
82
83	$blob = str_repeat('a', $max_len - $com_query_len - $query_len);
84	$query = sprintf("%s%s%s", $query_prefix, $blob, $query_postfix);
85
86	if (!mysqli_query($link, $query))
87		printf("[013] max_allowed_packet = %d, strlen(query) = %d, [%d] %s\n", $max_allowed_packet, strlen($query), mysqli_errno($link), mysqli_error($link));
88
89	if (!$res = mysqli_query($link, "SELECT col_blob FROM test"))
90		printf("[014] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
91
92	if (!$row = mysqli_fetch_assoc($res)) {
93		printf("[015] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
94	} else {
95		if ($row['col_blob'] != $blob) {
96			printf("[016] Blob seems wrong, dumping data\n");
97			var_dump(strlen($row['col_blob']));
98			var_dump(strlen($blob));
99		}
100		mysqli_free_result($res);
101	}
102
103	if (!mysqli_query($link, "SET GLOBAL max_allowed_packet = " . $org_max_allowed_packet))
104		if (1227 != mysqli_errno($link))
105			printf("[017] [%d] %s\n", mysqli_errno($link), mysqli_error($link));
106
107	mysqli_close($link);
108
109	print "done!";
110?>
111--CLEAN--
112<?php
113	require_once("clean_table.inc");
114?>
115--EXPECT--
116done!
117