Annotation of embedaddon/php/ext/mysql/tests/mysql_pconn_max_links.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: Persistent connections and mysql.max_persistent
                      3: --SKIPIF--
                      4: <?php
                      5:        require_once('skipif.inc');
                      6:        require_once('skipifconnectfailure.inc');
                      7:        require_once('table.inc');
                      8: 
                      9:        if ($socket)
                     10:                $host = sprintf("%s:%s", $host, $socket);
                     11:        else if ($port)
                     12:                $host = sprintf("%s:%s", $host, $port);
                     13: 
                     14:        // we need a second DB user to test for a possible flaw in the ext/mysql[i] code
                     15:        if (!$link = mysql_connect($host, $user, $passwd, true))
                     16:                die(sprintf("skip Cannot connect [%d] %s", mysql_errno(), mysql_error()));
                     17: 
                     18:        if (!mysql_select_db($db, $link))
                     19:                die(sprintf("skip [%d] %s", mysql_errno($link), mysql_error($link)));
                     20: 
                     21:        if (!$res = mysql_query('SHOW VARIABLES LIKE "old_passwords"', $link)) {
                     22:                die(sprintf("skip [%d] %s", mysql_errno($link), mysql_error($link)));
                     23:        }
                     24: 
                     25:        if (mysql_num_rows($res) != 1) {
                     26:                die(sprintf("skip Can't check if old_passwords = ON"));
                     27:        }
                     28: 
                     29:        $row = mysql_fetch_assoc($res);
                     30:        mysql_free_result($res);
                     31:        if ($row['Value'] == "ON")
                     32:                die(sprintf("skip Test will fail because old_passwords = ON. Hint: old passwords are insecure!"));
                     33: 
                     34:        if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
                     35:                die(sprintf("skip [%d] %s", mysql_errno($link), mysql_error($link)));
                     36: 
                     37:        $row = mysql_fetch_assoc($res);
                     38:        mysql_free_result($res);
                     39:        $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
                     40: 
                     41:        mysql_query('DROP USER pcontest', $link);
                     42:        mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                     43:        if (!mysql_query(sprintf('CREATE USER pcontest@"%s" IDENTIFIED BY "pcontest"', mysql_real_escape_string($host, $link)), $link)) {
                     44:                printf("skip Cannot create second DB user [%d] %s", mysql_errno($link), mysql_error($link));
                     45:                mysql_close($link);
                     46:                die();
                     47:        }
                     48: 
                     49:        // we might be able to specify the host using CURRENT_USER(), but...
                     50:        if (!mysql_query(sprintf('GRANT SELECT ON TABLE %s.test TO pcontest@"%s"', $db, mysql_real_escape_string($host, $link)), $link)) {
                     51:                printf("skip Cannot GRANT SELECT to second DB user [%d] %s", mysql_errno($link), mysql_error($link));
                     52:                mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                     53:                mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                     54:                mysql_close($link);
                     55:                die();
                     56:        }
                     57: 
                     58:        mysql_close($link);
                     59: ?>
                     60: --INI--
                     61: mysql.max_links=3
                     62: mysql.max_persistent=2
                     63: mysql.allow_persistent=1
                     64: --FILE--
                     65: <?php
                     66:        require_once("connect.inc");
                     67:        require_once('table.inc');
                     68: 
                     69:        if ($socket)
                     70:                $host = sprintf("%s:%s", $host, $socket);
                     71:        else if ($port)
                     72:                $host = sprintf("%s:%s", $host, $port);
                     73: 
                     74:        if (!$plink = mysql_pconnect($host, $user, $passwd))
                     75:                printf("[001] Cannot connect using the second DB user created during SKIPIF, [%d] %s\n",
                     76:                        mysql_errno(), mysql_error());
                     77: 
                     78:        if (!mysql_select_db($db, $plink))
                     79:                printf("[002] [%d] %s\n", mysql_errno($plink), mysql_error($plink));
                     80: 
                     81:        ob_start();
                     82:        phpinfo();
                     83:        $phpinfo = strip_tags(ob_get_contents());
                     84:        ob_end_clean();
                     85:        $phpinfo = substr($phpinfo, strpos($phpinfo, 'MySQL Support => enabled'), 500);
                     86:        if (!preg_match('@Active Persistent Links\s+=>\s+(\d+)@ismU', $phpinfo, $matches))
                     87:                printf("[003] Cannot get # active persistent links from phpinfo()");
                     88:        $num_plinks = $matches[1];
                     89: 
                     90:        if (!$res = mysql_query('SELECT id, label FROM test WHERE id = 1', $plink))
                     91:                printf("[004] Cannot run query on persistent connection of second DB user, [%d] %s\n",
                     92:                        mysql_errno($plink), mysql_error($plink));
                     93: 
                     94:        if (!$row = mysql_fetch_assoc($res))
                     95:                printf("[005] Cannot run fetch result, [%d] %s\n",
                     96:                        mysql_errno($plink), mysql_error($plink));
                     97:        mysql_free_result($res);
                     98:        var_dump($row);
                     99: 
                    100:        // change the password for the second DB user and kill the persistent connection
                    101:        if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
                    102:                printf("[006] [%d] %s", mysql_errno($link), mysql_error($link));
                    103: 
                    104:        $row = mysql_fetch_assoc($res);
                    105:        mysql_free_result($res);
                    106:        $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
                    107: 
                    108:        $sql = sprintf('SET PASSWORD FOR pcontest@"%s" = PASSWORD("newpass")', mysql_real_escape_string($host, $link));
                    109:        if (!mysql_query($sql, $link))
                    110:                printf("[007] Cannot change PW of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link));
                    111: 
                    112:        // persistent connections cannot be closed but only be killed
                    113:        $pthread_id = mysql_thread_id($plink);
                    114:        if (!mysql_query(sprintf('KILL %d', $pthread_id), $link))
                    115:                printf("[008] Cannot KILL persistent connection of second DB user, [%d] %s\n", mysql_errno($link), mysql_error($link));
                    116:        // give the server a second to really kill the thread
                    117:        sleep(1);
                    118: 
                    119:        if (!$res = mysql_query("SHOW FULL PROCESSLIST", $link))
                    120:                printf("[009] [%d] %s\n", mysql_errno($link), mysql_error($link));
                    121: 
                    122:        $running_threads = array();
                    123:        while ($row = mysql_fetch_assoc($res))
                    124:                $running_threads[$row['Id']] = $row;
                    125:        mysql_free_result($res);
                    126: 
                    127:        if (isset($running_threads[$pthread_id]))
                    128:                printf("[010] Persistent connection has not been killed\n");
                    129: 
                    130:        // we might get the old handle
                    131:        if ($plink = @mysql_pconnect($host, 'pcontest', 'pcontest'))
                    132:                printf("[011] Can connect using the old password, [%d] %s\n",
                    133:                        mysql_errno(), mysql_error());
                    134: 
                    135:        ob_start();
                    136:        phpinfo();
                    137:        $phpinfo = strip_tags(ob_get_contents());
                    138:        ob_end_clean();
                    139:        $phpinfo = substr($phpinfo, strpos($phpinfo, 'MySQL Support => enabled'), 500);
                    140:        if (!preg_match('@Active Persistent Links\s+=>\s+(\d+)@ismU', $phpinfo, $matches))
                    141:                printf("[012] Cannot get # active persistent links from phpinfo()\n");
                    142: 
                    143:        $num_plinks_kill = $matches[1];
                    144:        if ($num_plinks_kill > $num_plinks)
                    145:                printf("[013] Statistics seems to be wrong, got %d active persistent links, expecting < %d links\n",
                    146:                        $num_plinks_kill, $num_plinks);
                    147: 
                    148:        // The first connection has been closed, the last pconnect() was unable to connect -> no connection open
                    149:        // We must be able to connect because max_persistent limit has not been reached
                    150:        if (!$plink = mysql_pconnect($host, 'pcontest', 'newpass'))
                    151:                die(sprintf("[014] Cannot connect using the second DB, [%d] %s\n",
                    152:                        mysql_errno(), mysql_error()));
                    153: 
                    154:        if (!mysql_select_db($db, $plink))
                    155:                printf("[015] [%d] %s\n", mysql_errno($plink), mysql_error($plink));
                    156: 
                    157:        if (!$res = mysql_query('SELECT id, label FROM test WHERE id = 1', $plink))
                    158:                printf("[016] Cannot run query on persistent connection of second DB user, [%d] %s\n",
                    159:                        mysql_errno($plink), mysql_error($plink));
                    160: 
                    161:        if (!$row = mysql_fetch_assoc($res))
                    162:                printf("[017] Cannot run fetch result, [%d] %s\n",
                    163:                        mysql_errno($plink), mysql_error($plink));
                    164:        mysql_free_result($res);
                    165:        var_dump($row);
                    166: 
                    167:        mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                    168:        mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                    169:        mysql_close($link);
                    170:        print "done!";
                    171: ?>
                    172: --CLEAN--
                    173: <?php
                    174: // connect + select_db
                    175: require_once("connect.inc");
                    176: if (!$link = my_mysql_connect($host, $user, $passwd, $db, $port, $socket)) {
                    177:        printf("[c001] Cannot connect to the server using host=%s/%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
                    178:          $host, $myhost, $user, $db, $port, $socket);
                    179: }
                    180: 
                    181: if (!$res = mysql_query("SELECT CURRENT_USER() AS _user", $link))
                    182:        printf("[c002] [%d] %s", mysql_errno($link), mysql_error($link));
                    183: 
                    184: $row = mysql_fetch_assoc($res);
                    185: mysql_free_result($res);
                    186: $host = substr($row['_user'], strrpos($row['_user'], "@") + 1, strlen($row['_user']));
                    187: 
                    188: @mysql_query(sprintf('REVOKE ALL PRIVILEGES, GRANT OPTION FROM pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                    189: @mysql_query(sprintf('DROP USER pcontest@"%s"', mysql_real_escape_string($host, $link)), $link);
                    190: 
                    191: mysql_close($link);
                    192: ?>
                    193: --EXPECTF--
                    194: array(2) {
                    195:   [%u|b%"id"]=>
                    196:   %unicode|string%(1) "1"
                    197:   [%u|b%"label"]=>
                    198:   %unicode|string%(1) "a"
                    199: }
                    200: array(2) {
                    201:   [%u|b%"id"]=>
                    202:   %unicode|string%(1) "1"
                    203:   [%u|b%"label"]=>
                    204:   %unicode|string%(1) "a"
                    205: }
                    206: done!

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>