Source for file Test.php

Documentation is available at Test.php

  1. <?php
  2. /**
  3. * Skeleton Test class file
  4. *
  5. * @package PhpSecInfo
  6. * @author Ed Finkler <coj@funkatron.com>
  7. */
  8.  
  9. /**
  10. * require the main PhpSecInfo class
  11. */
  12. require_once('PhpSecInfo/PhpSecInfo.php');
  13.  
  14.  
  15.  
  16. define ('PHPSECINFO_TEST_RESULT_OK', -1);
  17.  
  18. define ('PHPSECINFO_TEST_RESULT_NOTICE', -2);
  19.  
  20. define ('PHPSECINFO_TEST_RESULT_WARN', -4);
  21.  
  22. define ('PHPSECINFO_TEST_RESULT_ERROR', -1024);
  23.  
  24. define ('PHPSECINFO_TEST_RESULT_NOTRUN', -2048);
  25.  
  26. define ('PHPSECINFO_TEST_COMMON_TMPDIR', '/tmp');
  27.  
  28.  
  29. /**
  30. * This is a skeleton class for PhpSecInfo tests You should extend this to make a "group" skeleton
  31. * to categorize tests under, then make a subdir with your group name that contains test classes
  32. * extending your group skeleton class.
  33. * @package PhpSecInfo
  34. */
  35. class PhpSecInfo_Test
  36. {
  37. /**
  38. * This value is used to group test results together.
  39. *
  40. * For example, all tests related to the mysql lib should be grouped under "mysql."
  41. *
  42. * @var string
  43. */
  44. var $test_group = 'misc';
  45. /**
  46. * This should be a <b>unique</b>, human-readable identifier for this test
  47. *
  48. * @var string
  49. */
  50. var $test_name = 'misc_test';
  51. /**
  52. * The result returned from the test
  53. *
  54. * @var integer
  55. */
  56. var $_result = PHPSECINFO_TEST_RESULT_NOTRUN;
  57. /**
  58. * The message corresponding to the result of the test
  59. *
  60. * @var string
  61. */
  62. var $_message;
  63. /**
  64. * the language code. Should be a pointer to the setting in the PhpSecInfo object
  65. *
  66. * @var string
  67. */
  68. var $_language = PHPSECINFO_LANG_DEFAULT;
  69. /**
  70. * This is a hash of messages that correspond to various test result levels.
  71. *
  72. * There are five messages, each corresponding to one of the result constants
  73. * (PHPSECINFO_TEST_RESULT_OK, PHPSECINFO_TEST_RESULT_NOTICE, PHPSECINFO_TEST_RESULT_WARN,
  74. * PHPSECINFO_TEST_RESULT_ERROR, PHPSECINFO_TEST_RESULT_NOTRUN)
  75. *
  76. *
  77. * @var array array
  78. */
  79. var $_messages = array();
  80. /*var $_messages = array(
  81. PHPSECINFO_TEST_RESULT_OK => array(
  82. 'en' => 'This setting should be safe',
  83. ),
  84. PHPSECINFO_TEST_RESULT_NOTICE => array(
  85. 'en' => 'This could potentially be a security issue',
  86. ),
  87. PHPSECINFO_TEST_RESULT_WARN => array(
  88. 'en' => 'This setting may be a serious security problem',
  89. ),
  90. PHPSECINFO_TEST_RESULT_ERROR => array(
  91. 'en' => 'There was an error running this test',
  92. ),
  93. PHPSECINFO_TEST_RESULT_NOTRUN => array(
  94. 'en' => 'This test was not run',
  95. ),
  96. );*/
  97. /**
  98. * Constructor for Test skeleton class
  99. *
  100. * @return PhpSecInfo_Test
  101. */
  102. function PhpSecInfo_Test() {
  103. $this->_setMessages();
  104. }
  105. /**
  106. * Determines whether or not it's appropriate to run this test (for example, if
  107. * this test is for a particular library, it shouldn't be run if the lib isn't
  108. * loaded).
  109. *
  110. * This is a terrible name, but I couldn't think of a better one atm.
  111. *
  112. * @return boolean
  113. */
  114. function isTestable() {
  115. return true;
  116. }
  117. /**
  118. * The "meat" of the test. This is where the real test code goes. You should override this when extending
  119. *
  120. * @var integer
  121. *
  122. */
  123. function _execTest() {
  124. return PHPSECINFO_TEST_RESULT_NOTRUN;
  125. }
  126. /**
  127. * This function loads up result messages into the $this->_messages array.
  128. *
  129. * Using this method rather than setting $this->_messages directly allows result
  130. * messages to be inherited. This is broken out into a separate function rather
  131. * than the constructor for ease of extending purposes (php4 is whack, man).
  132. *
  133. */
  134. function _setMessages() {
  135. $this->setMessageForResult(PHPSECINFO_TEST_RESULT_OK, 'en', 'This setting should be safe');
  136. $this->setMessageForResult(PHPSECINFO_TEST_RESULT_NOTICE, 'en', 'This could potentially be a security issue');
  137. $this->setMessageForResult(PHPSECINFO_TEST_RESULT_WARN, 'en', 'This setting may be a serious security problem');
  138. $this->setMessageForResult(PHPSECINFO_TEST_RESULT_ERROR, 'en', 'There was an error running this test');
  139. $this->setMessageForResult(PHPSECINFO_TEST_RESULT_NOTRUN, 'en', 'This test cannot be run');
  140. }
  141. /**
  142. * This is the wrapper that executes the test and sets the result code and message
  143. */
  144. function test() {
  145. $result = $this->_execTest();
  146. $this->_setResult($result);
  147. }
  148.  
  149. /**
  150. * Retrieves the result
  151. *
  152. * @return integer
  153. */
  154. function getResult() {
  155. return $this->_result;
  156. }
  157.  
  158. /**
  159. * Retrieves the message for the current result
  160. *
  161. * @return string
  162. */
  163. function getMessage() {
  164. if (!isset($this->_message) || empty($this->_message)) {
  165. $this->_setMessage($this->_result, $this->_language);
  166. }
  167. return $this->_message;
  168. }
  169. /**
  170. * Sets the message for a given result code and language
  171. *
  172. * <code>
  173. * $this->setMessageForResult(PHPSECINFO_TEST_RESULT_NOTRUN, 'en', 'This test cannot be run');
  174. * </code>
  175. *
  176. * @param integer $result_code
  177. * @param string $language_code
  178. * @param string $message
  179. */
  180. function setMessageForResult($result_code, $language_code, $message) {
  181. if ( !isset($this->_messages[$result_code]) ) {
  182. $this->_messages[$result_code] = array();
  183. }
  184. if ( !is_array($this->_messages[$result_code]) ) {
  185. $this->_messages[$result_code] = array();
  186. }
  187. $this->_messages[$result_code][$language_code] = $message;
  188.  
  189. }
  190. /**
  191. * Sets the result code
  192. *
  193. * @param integer $result_code
  194. */
  195. function _setResult($result_code) {
  196. $this->_result = $result_code;
  197. }
  198. /**
  199. * Sets the $this->_message variable based on the passed result and language codes
  200. *
  201. * @param integer $result_code
  202. * @param string $language_code
  203. */
  204. function _setMessage($result_code, $language_code) {
  205. $messages = $this->_messages[$result_code];
  206. $message = $messages[$language_code];
  207. $this->_message = $message;
  208. }
  209. /**
  210. * This retrieves the name of this test.
  211. *
  212. * If a name has not been set, this returns a formatted version of the class name.
  213. *
  214. * @return string
  215. */
  216. function getTestName() {
  217. if (isset($this->test_name) && !empty($this->test_name)) {
  218. return $this->test_name;
  219. } else {
  220. return ucwords(
  221. str_replace('_', ' ',
  222. get_class($this)
  223. )
  224. );
  225. }
  226. }
  227.  
  228. /**
  229. * sets the test name
  230. *
  231. * @param string $test_name
  232. */
  233. function setTestName($test_name) {
  234. $this->test_name = $test_name;
  235. }
  236.  
  237. /**
  238. * Returns the test group this test belongs to
  239. *
  240. * @return string
  241. */
  242. function getTestGroup() {
  243. return $this->test_group;
  244. }
  245. /**
  246. * This function takes the shorthand notation used in memory limit settings for PHP
  247. * and returns the byte value. Totally stolen from http://us3.php.net/manual/en/function.ini-get.php
  248. *
  249. * <code>
  250. * echo 'post_max_size in bytes = ' . $this->return_bytes(ini_get('post_max_size'));
  251. * </code>
  252. *
  253. * @link http://php.net/manual/en/function.ini-get.php
  254. * @param string $val
  255. * @return integer
  256. */
  257. function returnBytes($val) {
  258. $val = trim($val);
  259. $last = strtolower($val{strlen($val)-1});
  260. switch($last) {
  261. // The 'G' modifier is available since PHP 5.1.0
  262. case 'g':
  263. $val *= 1024;
  264. case 'm':
  265. $val *= 1024;
  266. case 'k':
  267. $val *= 1024;
  268. }
  269. return $val;
  270. }
  271. /**
  272. * This method converts the several possible return values from
  273. * allegedly "boolean" ini settings to proper booleans
  274. *
  275. * Properly converted input values are: 'off', 'on', 'false', 'true', '0', '1'
  276. * (the last two might not be neccessary, but I'd rather be safe)
  277. *
  278. * If the ini_value doesn't match any of those, the value is returned as-is.
  279. *
  280. * @param string $ini_key the ini_key you need the value of
  281. * @return boolean|mixed
  282. */
  283. function getBooleanIniValue($ini_key) {
  284. $ini_val = ini_get($ini_key);
  285. switch ( strtolower($ini_val) ) {
  286. case 'off':
  287. return false;
  288. break;
  289. case 'on':
  290. return true;
  291. break;
  292. case 'false':
  293. return false;
  294. break;
  295. case 'true':
  296. return true;
  297. break;
  298. case '0':
  299. return false;
  300. break;
  301. case '1':
  302. return true;
  303. break;
  304. default:
  305. return $ini_val;
  306. }
  307. }
  308. }

Documentation generated on Tue, 24 Oct 2006 10:53:40 -0400 by phpDocumentor 1.3.0RC3