PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - assert.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 152 162 93.8 %
Date: 2015-05-21 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Thies C. Arntzen <thies@thieso.net>                          |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : /* {{{ includes */
      22             : #include "php.h"
      23             : #include "php_assert.h"
      24             : #include "php_ini.h"
      25             : #include "zend_exceptions.h"
      26             : /* }}} */
      27             : 
      28             : ZEND_BEGIN_MODULE_GLOBALS(assert)
      29             :         zval callback;
      30             :         char *cb;
      31             :         zend_bool active;
      32             :         zend_bool bail;
      33             :         zend_bool warning;
      34             :         zend_bool quiet_eval;
      35             :         zend_bool exception;
      36             : ZEND_END_MODULE_GLOBALS(assert)
      37             : 
      38             : ZEND_DECLARE_MODULE_GLOBALS(assert)
      39             : 
      40             : static zend_class_entry *assertion_exception_ce;
      41             : 
      42             : #ifdef ZTS
      43             : #define ASSERTG(v) ZEND_TSRMG(assert_globals_id, zend_assert_globals *, v)
      44             : #else
      45             : #define ASSERTG(v) (assert_globals.v)
      46             : #endif
      47             : 
      48             : #define SAFE_STRING(s) ((s)?(s):"")
      49             : 
      50             : enum {
      51             :         ASSERT_ACTIVE=1,
      52             :         ASSERT_CALLBACK,
      53             :         ASSERT_BAIL,
      54             :         ASSERT_WARNING,
      55             :         ASSERT_QUIET_EVAL,
      56             :         ASSERT_EXCEPTION
      57             : };
      58             : 
      59       20920 : static PHP_INI_MH(OnChangeCallback) /* {{{ */
      60             : {
      61       20920 :         if (EG(current_execute_data)) {
      62           2 :                 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
      63           2 :                         zval_ptr_dtor(&ASSERTG(callback));
      64           2 :                         ZVAL_UNDEF(&ASSERTG(callback));
      65             :                 }
      66           4 :                 if (new_value && (Z_TYPE(ASSERTG(callback)) != IS_UNDEF || new_value->len)) {
      67           2 :                         ZVAL_STR_COPY(&ASSERTG(callback), new_value);
      68             :                 }
      69             :         } else {
      70       20918 :                 if (ASSERTG(cb)) {
      71           1 :                         pefree(ASSERTG(cb), 1);
      72             :                 }
      73       20932 :                 if (new_value && new_value->len) {
      74          28 :                         ASSERTG(cb) = pemalloc(new_value->len + 1, 1);
      75          14 :                         memcpy(ASSERTG(cb), new_value->val, new_value->len);
      76          14 :                         ASSERTG(cb)[new_value->len] = '\0';
      77             :                 } else {
      78       20904 :                         ASSERTG(cb) = NULL;
      79             :                 }
      80             :         }
      81       20920 :         return SUCCESS;
      82             : }
      83             : /* }}} */
      84             : 
      85             : PHP_INI_BEGIN()
      86             :          STD_PHP_INI_ENTRY("assert.active",           "1",  PHP_INI_ALL,    OnUpdateBool,           active,                         zend_assert_globals,            assert_globals)
      87             :          STD_PHP_INI_ENTRY("assert.bail",             "0",  PHP_INI_ALL,    OnUpdateBool,           bail,                           zend_assert_globals,            assert_globals)
      88             :          STD_PHP_INI_ENTRY("assert.warning",  "1",  PHP_INI_ALL,    OnUpdateBool,           warning,                        zend_assert_globals,            assert_globals)
      89             :          PHP_INI_ENTRY("assert.callback",             NULL,   PHP_INI_ALL,    OnChangeCallback)
      90             :          STD_PHP_INI_ENTRY("assert.quiet_eval", "0",        PHP_INI_ALL,    OnUpdateBool,           quiet_eval,                     zend_assert_globals,            assert_globals)
      91             :          STD_PHP_INI_ENTRY("assert.exception",        "0",  PHP_INI_ALL,    OnUpdateBool,           exception,                      zend_assert_globals,            assert_globals)
      92             : PHP_INI_END()
      93             : 
      94       20916 : static void php_assert_init_globals(zend_assert_globals *assert_globals_p) /* {{{ */
      95             : {
      96       20916 :         ZVAL_UNDEF(&assert_globals_p->callback);
      97       20916 :         assert_globals_p->cb = NULL;
      98       20916 : }
      99             : /* }}} */
     100             : 
     101       20916 : PHP_MINIT_FUNCTION(assert) /* {{{ */
     102             : {
     103             :         zend_class_entry ce;
     104             : 
     105       20916 :         ZEND_INIT_MODULE_GLOBALS(assert, php_assert_init_globals, NULL);
     106             : 
     107       20916 :         REGISTER_INI_ENTRIES();
     108             : 
     109       20916 :         REGISTER_LONG_CONSTANT("ASSERT_ACTIVE", ASSERT_ACTIVE, CONST_CS|CONST_PERSISTENT);
     110       20916 :         REGISTER_LONG_CONSTANT("ASSERT_CALLBACK", ASSERT_CALLBACK, CONST_CS|CONST_PERSISTENT);
     111       20916 :         REGISTER_LONG_CONSTANT("ASSERT_BAIL", ASSERT_BAIL, CONST_CS|CONST_PERSISTENT);
     112       20916 :         REGISTER_LONG_CONSTANT("ASSERT_WARNING", ASSERT_WARNING, CONST_CS|CONST_PERSISTENT);
     113       20916 :         REGISTER_LONG_CONSTANT("ASSERT_QUIET_EVAL", ASSERT_QUIET_EVAL, CONST_CS|CONST_PERSISTENT);
     114       20916 :         REGISTER_LONG_CONSTANT("ASSERT_EXCEPTION", ASSERT_EXCEPTION, CONST_CS|CONST_PERSISTENT);
     115             : 
     116       20916 :         INIT_CLASS_ENTRY(ce, "AssertionException", NULL);
     117       20916 :         assertion_exception_ce = zend_register_internal_class_ex(&ce, zend_exception_get_default());
     118             : 
     119       20916 :         return SUCCESS;
     120             : }
     121             : /* }}} */
     122             : 
     123       20952 : PHP_MSHUTDOWN_FUNCTION(assert) /* {{{ */
     124             : {
     125       20952 :         if (ASSERTG(cb)) {
     126          13 :                 pefree(ASSERTG(cb), 1);
     127          13 :                 ASSERTG(cb) = NULL;
     128             :         }
     129       20952 :         return SUCCESS;
     130             : }
     131             : /* }}} */
     132             : 
     133       20911 : PHP_RSHUTDOWN_FUNCTION(assert) /* {{{ */
     134             : {
     135       20911 :         if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
     136          10 :                 zval_ptr_dtor(&ASSERTG(callback));
     137          10 :                 ZVAL_UNDEF(&ASSERTG(callback));
     138             :         }
     139             : 
     140       20911 :         return SUCCESS;
     141             : }
     142             : /* }}} */
     143             : 
     144         143 : PHP_MINFO_FUNCTION(assert) /* {{{ */
     145             : {
     146         143 :         DISPLAY_INI_ENTRIES();
     147         143 : }
     148             : /* }}} */
     149             : 
     150             : /* {{{ proto int assert(string|bool assertion[, mixed description])
     151             :    Checks if assertion is false */
     152      100276 : PHP_FUNCTION(assert)
     153             : {
     154             :         zval *assertion;
     155      100276 :         zval *description = NULL;
     156             :         int val;
     157      100276 :         char *myeval = NULL;
     158             :         char *compiled_string_description;
     159             : 
     160      100276 :         if (! ASSERTG(active)) {
     161           5 :                 RETURN_TRUE;
     162             :         }
     163             : 
     164      100271 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|z", &assertion, &description) == FAILURE) {
     165           2 :                 return;
     166             :         }
     167             : 
     168      200538 :         if (Z_TYPE_P(assertion) == IS_STRING) {
     169             :                 zval retval;
     170          71 :                 int old_error_reporting = 0; /* shut up gcc! */
     171             : 
     172          71 :                 myeval = Z_STRVAL_P(assertion);
     173             : 
     174          71 :                 if (ASSERTG(quiet_eval)) {
     175          10 :                         old_error_reporting = EG(error_reporting);
     176          10 :                         EG(error_reporting) = 0;
     177             :                 }
     178             : 
     179          71 :                 compiled_string_description = zend_make_compiled_string_description("assert code");
     180          71 :                 if (zend_eval_stringl(myeval, Z_STRLEN_P(assertion), &retval, compiled_string_description) == FAILURE) {
     181           5 :                         efree(compiled_string_description);
     182           5 :                         if (!description) {
     183           4 :                                 php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s", PHP_EOL, myeval);
     184             :                         } else {
     185           2 :                                 zend_string *str = zval_get_string(description);
     186           1 :                                 php_error_docref(NULL, E_RECOVERABLE_ERROR, "Failure evaluating code: %s%s:\"%s\"", PHP_EOL, str->val, myeval);
     187             :                                 zend_string_release(str);
     188             :                         }
     189           2 :                         if (ASSERTG(bail)) {
     190           1 :                                 zend_bailout();
     191             :                         }
     192           1 :                         RETURN_FALSE;
     193             :                 }
     194          66 :                 efree(compiled_string_description);
     195             : 
     196          66 :                 if (ASSERTG(quiet_eval)) {
     197           9 :                         EG(error_reporting) = old_error_reporting;
     198             :                 }
     199             : 
     200          66 :                 convert_to_boolean(&retval);
     201          66 :                 val = Z_TYPE(retval) == IS_TRUE;
     202             :         } else {
     203      100198 :                 val = zend_is_true(assertion);
     204             :         }
     205             : 
     206      100264 :         if (val) {
     207      100218 :                 RETURN_TRUE;
     208             :         }
     209             : 
     210          46 :         if (Z_TYPE(ASSERTG(callback)) == IS_UNDEF && ASSERTG(cb)) {
     211          14 :                 ZVAL_STRING(&ASSERTG(callback), ASSERTG(cb));
     212             :         }
     213             : 
     214          46 :         if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
     215          20 :                 zval *args = safe_emalloc(!description ? 3 : 4, sizeof(zval), 0);
     216             :                 zval retval;
     217             :                 int i;
     218          20 :                 uint lineno = zend_get_executed_lineno();
     219          20 :                 const char *filename = zend_get_executed_filename();
     220             : 
     221          40 :                 ZVAL_STRING(&args[0], SAFE_STRING(filename));
     222          20 :                 ZVAL_LONG (&args[1], lineno);
     223          40 :                 ZVAL_STRING(&args[2], SAFE_STRING(myeval));
     224             : 
     225          20 :                 ZVAL_FALSE(&retval);
     226             : 
     227             :                 /* XXX do we want to check for error here? */
     228          20 :                 if (!description) {
     229           6 :                         call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 3, args);
     230          24 :                         for (i = 0; i <= 2; i++) {
     231          18 :                                 zval_ptr_dtor(&(args[i]));
     232             :                         }
     233             :                 } else {
     234          28 :                         ZVAL_STR(&args[3], zval_get_string(description));
     235          14 :                         call_user_function(CG(function_table), NULL, &ASSERTG(callback), &retval, 4, args);
     236          70 :                         for (i = 0; i <= 3; i++) {
     237          56 :                                 zval_ptr_dtor(&(args[i]));
     238             :                         }
     239             :                 }
     240             : 
     241          20 :                 efree(args);
     242          20 :                 zval_ptr_dtor(&retval);
     243             :         }
     244             : 
     245          46 :         if (ASSERTG(exception)) {
     246           9 :                 if (!description) {
     247           0 :                         zend_throw_exception(assertion_exception_ce, NULL, E_ERROR);
     248          20 :                 } else if (Z_TYPE_P(description) == IS_OBJECT &&
     249           1 :                         instanceof_function(Z_OBJCE_P(description), assertion_exception_ce)) {
     250           1 :                         Z_ADDREF_P(description);
     251           1 :                         zend_throw_exception_object(description);
     252             :                 } else {
     253          16 :                         zend_string *str = zval_get_string(description);
     254           8 :                         zend_throw_exception(assertion_exception_ce, str->val, E_ERROR);
     255             :                         zend_string_release(str);
     256             :                 }
     257          37 :         } else if (ASSERTG(warning)) {
     258          24 :                 if (!description) {
     259           3 :                         if (myeval) {
     260           3 :                                 php_error_docref(NULL, E_WARNING, "Assertion \"%s\" failed", myeval);
     261             :                         } else {
     262           0 :                                 php_error_docref(NULL, E_WARNING, "Assertion failed");
     263             :                         }
     264             :                 } else {
     265          42 :                         zend_string *str = zval_get_string(description);
     266          21 :                         if (myeval) {
     267           3 :                                 php_error_docref(NULL, E_WARNING, "%s: \"%s\" failed", str->val, myeval);
     268             :                         } else {
     269          18 :                                 php_error_docref(NULL, E_WARNING, "%s failed", str->val);
     270             :                         }
     271             :                         zend_string_release(str);
     272             :                 }
     273             :         }
     274             : 
     275          46 :         if (ASSERTG(bail)) {
     276           6 :                 zend_bailout();
     277             :         }
     278             : }
     279             : /* }}} */
     280             : 
     281             : /* {{{ proto mixed assert_options(int what [, mixed value])
     282             :    Set/get the various assert flags */
     283          44 : PHP_FUNCTION(assert_options)
     284             : {
     285          44 :         zval *value = NULL;
     286             :         zend_long what;
     287             :         zend_bool oldint;
     288          44 :         int ac = ZEND_NUM_ARGS();
     289             :         zend_string *key;
     290             : 
     291          44 :         if (zend_parse_parameters(ac, "l|z", &what, &value) == FAILURE) {
     292           4 :                 return;
     293             :         }
     294             : 
     295          40 :         switch (what) {
     296             :         case ASSERT_ACTIVE:
     297           5 :                 oldint = ASSERTG(active);
     298           5 :                 if (ac == 2) {
     299           8 :                         zend_string *value_str = zval_get_string(value);
     300           4 :                         key = zend_string_init("assert.active", sizeof("assert.active")-1, 0);
     301           4 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     302             :                         zend_string_release(key);
     303             :                         zend_string_release(value_str);
     304             :                 }
     305           5 :                 RETURN_LONG(oldint);
     306             :                 break;
     307             : 
     308             :         case ASSERT_BAIL:
     309           6 :                 oldint = ASSERTG(bail);
     310           6 :                 if (ac == 2) {
     311          10 :                         zend_string *value_str = zval_get_string(value);
     312           5 :                         key = zend_string_init("assert.bail", sizeof("assert.bail")-1, 0);
     313           5 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     314             :                         zend_string_release(key);
     315             :                         zend_string_release(value_str);
     316             :                 }
     317           6 :                 RETURN_LONG(oldint);
     318             :                 break;
     319             : 
     320             :         case ASSERT_QUIET_EVAL:
     321           4 :                 oldint = ASSERTG(quiet_eval);
     322           4 :                 if (ac == 2) {
     323           6 :                         zend_string *value_str = zval_get_string(value);
     324           3 :                         key = zend_string_init("assert.quiet_eval", sizeof("assert.quiet_eval")-1, 0);
     325           3 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     326             :                         zend_string_release(key);
     327             :                         zend_string_release(value_str);
     328             :                 }
     329           4 :                 RETURN_LONG(oldint);
     330             :                 break;
     331             : 
     332             :         case ASSERT_WARNING:
     333           6 :                 oldint = ASSERTG(warning);
     334           6 :                 if (ac == 2) {
     335          10 :                         zend_string *value_str = zval_get_string(value);
     336           5 :                         key = zend_string_init("assert.warning", sizeof("assert.warning")-1, 0);
     337           5 :                         zend_alter_ini_entry_ex(key, value_str, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     338             :                         zend_string_release(key);
     339             :                         zend_string_release(value_str);
     340             :                 }
     341           6 :                 RETURN_LONG(oldint);
     342             :                 break;
     343             : 
     344             :         case ASSERT_CALLBACK:
     345          19 :                 if (Z_TYPE(ASSERTG(callback)) != IS_UNDEF) {
     346          26 :                         RETVAL_ZVAL(&ASSERTG(callback), 1, 0);
     347           6 :                 } else if (ASSERTG(cb)) {
     348           6 :                         RETVAL_STRING(ASSERTG(cb));
     349             :                 } else {
     350           3 :                         RETVAL_NULL();
     351             :                 }
     352          19 :                 if (ac == 2) {
     353          10 :                         zval_ptr_dtor(&ASSERTG(callback));
     354          10 :                         ZVAL_COPY(&ASSERTG(callback), value);
     355             :                 }
     356          19 :                 return;
     357             : 
     358             :         case ASSERT_EXCEPTION:
     359           0 :                 oldint = ASSERTG(exception);
     360           0 :                 if (ac == 2) {
     361           0 :                         zend_string *key = zend_string_init("assert.exception", sizeof("assert.exception")-1, 0);
     362           0 :                         zend_string *val = zval_get_string(value);
     363           0 :                         zend_alter_ini_entry_ex(key, val, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0);
     364             :                         zend_string_release(val);
     365             :                         zend_string_release(key);
     366             :                 }
     367           0 :                 RETURN_LONG(oldint);
     368             :                 break;
     369             : 
     370             :         default:
     371           0 :                 php_error_docref(NULL, E_WARNING, "Unknown value %pd", what);
     372             :                 break;
     373             :         }
     374             : 
     375           0 :         RETURN_FALSE;
     376             : }
     377             : /* }}} */
     378             : 
     379             : /*
     380             :  * Local variables:
     381             :  * tab-width: 4
     382             :  * c-basic-offset: 4
     383             :  * End:
     384             :  * vim600: sw=4 ts=4 fdm=marker
     385             :  * vim<600: sw=4 ts=4
     386             :  */
     387             : 

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:06 +0000 (3 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.