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