PageRenderTime 27ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Services_Trackback/pear/tests/Net_DNSBL/tests/testNetDNSBL.php

http://nothing-at-all.googlecode.com/
PHP | 241 lines | 119 code | 16 blank | 106 comment | 1 complexity | da49f75d93a7f5c23c7d3e278672971e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * PEAR::Net_DNSBL
  5. *
  6. * This class acts as interface to generic Realtime Blocking Lists
  7. * (RBL)
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * LICENSE: This source file is subject to version 3.01 of the PHP license
  12. * that is available through the world-wide-web at the following URI:
  13. * http://www.php.net/license/3_01.txt. If you did not receive a copy of
  14. * the PHP License and are unable to obtain it through the web, please
  15. * send a note to license@php.net so we can mail you a copy immediately.
  16. *
  17. * Net_DNSBL looks up an supplied host if it's listed in 1-n supplied
  18. * Blacklists
  19. *
  20. * @category Net
  21. * @package Net_DNSBL
  22. * @author Sebastian Nohn <sebastian@nohn.net>
  23. * @copyright 2004-2007 Sebastian Nohn <sebastian@nohn.net>
  24. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  25. * @version CVS: $Id: testNetDNSBL.php,v 1.18 2007/08/10 14:59:04 nohn Exp $
  26. * @link http://pear.php.net/package/Net_DNSBL Package Home
  27. * @see Net_DNS
  28. * @since File available since Release 1.0.0
  29. */
  30. require_once "Net/DNSBL.php";
  31. require_once "PHPUnit/Framework/TestCase.php";
  32. /**
  33. * TestNetDNSBL
  34. *
  35. * This class tests all public Net_DNSBL methods
  36. *
  37. * @category Net
  38. * @package Net_DNSBL
  39. * @author Sebastian Nohn <sebastian@nohn.net>
  40. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  41. * @version Release: 1.3.0
  42. * @link http://pear.php.net/package/net_dnsbl Package Home
  43. */
  44. class TestNetDNSBL extends PHPUnit_Framework_TestCase
  45. {
  46. private $rbl;
  47. /**
  48. * Set up Testcase for Net_DNSBL
  49. *
  50. * @return boolean true on success, false on failure
  51. */
  52. protected function setUp()
  53. {
  54. $this->rbl = new Net_DNSBL;
  55. }
  56. /**
  57. * Test if known spam hosts are always identified correctly as such.
  58. *
  59. * @return boolean true on success, false on failure
  60. */
  61. public function testHostsAlwaysAreListed()
  62. {
  63. $this->assertTrue($this->rbl->isListed("127.0.0.2"));
  64. $this->assertContains("http://www.spamhaus.org/query/bl?ip=127.0.0.2", $this->rbl->getTxt('127.0.0.2'));
  65. $this->assertContains("http://www.spamhaus.org/SBL/sbl.lasso?query=SBL233", $this->rbl->getTxt('127.0.0.2'));
  66. }
  67. /**
  68. * Test if hosts that should not be know as spam hostsare always
  69. * identified correctly as such.
  70. *
  71. * @return boolean true on success, false on failure
  72. */
  73. public function testTrustworthyHostsArentListed()
  74. {
  75. $this->rbl->setBlacklists(array('dun.dnsrbl.net'));
  76. $this->assertFalse($this->rbl->isListed("mail.nohn.net"));
  77. $this->assertFalse($this->rbl->isListed("212.112.226.205"));
  78. $this->assertFalse($this->rbl->isListed("smtp1.google.com"));
  79. }
  80. /**
  81. * Test public setters
  82. *
  83. * @return boolean true on success, false on failure
  84. */
  85. public function testSetters()
  86. {
  87. $this->assertTrue($this->rbl->setBlacklists(array('dun.dnsrbl.net')));
  88. $this->assertEquals(array('dun.dnsrbl.net'), $this->rbl->getBlacklists());
  89. $this->assertFalse($this->rbl->setBlacklists('dnsbl.sorbs.net'));
  90. }
  91. /**
  92. * Test public setters and include some lookups.
  93. *
  94. * @return boolean true on success, false on failure
  95. */
  96. public function testSettersAndLookups()
  97. {
  98. $this->rbl->setBlacklists(array('dnsbl.sorbs.net'));
  99. $this->assertEquals(array('dnsbl.sorbs.net'), $this->rbl->getBlacklists());
  100. $this->assertFalse($this->rbl->isListed("mail.nohn.net"));
  101. $this->assertTrue($this->rbl->isListed("p50927464.dip.t-dialin.net"));
  102. }
  103. /**
  104. * Test getDetails()
  105. *
  106. * @return boolean true on success, false on failure
  107. */
  108. public function testGetDetails()
  109. {
  110. $this->rbl->setBlacklists(array('dnsbl.sorbs.net'));
  111. $this->assertTrue($this->rbl->isListed("p50927464.dip.t-dialin.net"));
  112. $this->assertEquals(array(
  113. "dnsbl" => "dnsbl.sorbs.net",
  114. "record" => "127.0.0.10",
  115. "txt" => array(
  116. 0 => "Dynamic IP Addresses See: http://www.sorbs.net/lookup.shtml?80.146.116.100"
  117. )
  118. ), $this->rbl->getDetails("p50927464.dip.t-dialin.net"));
  119. $this->assertFalse($this->rbl->getDetails("mail.nohn.net"));
  120. $this->assertFalse($this->rbl->getDetails("somehost.we.never.queried"));
  121. }
  122. /**
  123. * Test getListingBl()
  124. *
  125. * @return boolean true on success, false on failure
  126. */
  127. public function testGetListingBl()
  128. {
  129. $this->rbl->setBlacklists(array('dnsbl.sorbs.net'));
  130. $this->assertTrue($this->rbl->isListed("p50927464.dip.t-dialin.net"));
  131. $this->assertEquals("dnsbl.sorbs.net", $this->rbl->getListingBl("p50927464.dip.t-dialin.net"));
  132. $this->assertFalse($this->rbl->getListingBl("www.google.de"));
  133. }
  134. /**
  135. * Test getListingRecord()
  136. *
  137. * @return boolean true on success, false on failure
  138. */
  139. public function testGetListingRecord()
  140. {
  141. $this->rbl->setBlacklists(array('dnsbl.sorbs.net'));
  142. $this->assertTrue($this->rbl->isListed("p50927464.dip.t-dialin.net"));
  143. $this->assertEquals("127.0.0.10", $this->rbl->getListingRecord("p50927464.dip.t-dialin.net"));
  144. $this->assertFalse($this->rbl->getListingRecord("www.google.de"));
  145. }
  146. /**
  147. * Test getTxt()
  148. *
  149. * @return boolean true on success, false on failure
  150. */
  151. public function testGetTxt()
  152. {
  153. $this->rbl->setBlacklists(array('dnsbl.sorbs.net'));
  154. $this->assertTrue($this->rbl->isListed("p50927464.dip.t-dialin.net"));
  155. $this->assertEquals("127.0.0.10", $this->rbl->getListingRecord("p50927464.dip.t-dialin.net"));
  156. $this->assertEquals(array(0 => "Dynamic IP Addresses See: http://www.sorbs.net/lookup.shtml?80.146.116.100"), $this->rbl->getTxt("p50927464.dip.t-dialin.net"));
  157. $this->assertFalse($this->rbl->getTxt("www.google.de"));
  158. }
  159. /**
  160. * Test results with multiple blacklists (host not listed)
  161. *
  162. * @return boolean true on success, false on failure
  163. */
  164. public function testMultipleBlacklists()
  165. {
  166. $this->rbl->setBlackLists(array(
  167. 'sbl-xbl.spamhaus.org',
  168. 'bl.spamcop.net'
  169. ));
  170. $this->assertFalse($this->rbl->isListed('212.112.226.205'));
  171. $this->assertFalse($this->rbl->getListingBl('212.112.226.205'));
  172. }
  173. /**
  174. * Test results with multiple blacklists (listed test host)
  175. *
  176. * @return boolean true on success, false on failure
  177. */
  178. public function testIsListedMulti()
  179. {
  180. $this->rbl->setBlackLists(array(
  181. 'sbl-xbl.spamhaus.org',
  182. 'bl.spamcop.net'
  183. ));
  184. $this->assertTrue($this->rbl->isListed('127.0.0.2', true));
  185. }
  186. /**
  187. * Test getBlacklists() with multiple blacklists (listed test host)
  188. *
  189. * @return boolean true on success, false on failure
  190. */
  191. public function testGetListingBls()
  192. {
  193. $this->rbl->setBlackLists(array(
  194. 'sbl-xbl.spamhaus.org',
  195. 'bl.spamcop.net'
  196. ));
  197. $this->assertTrue($this->rbl->isListed('127.0.0.2', true));
  198. $this->assertEquals(array(
  199. 'sbl-xbl.spamhaus.org',
  200. 'bl.spamcop.net'
  201. ), $this->rbl->getListingBls('127.0.0.2'));
  202. $this->assertFalse($this->rbl->isListed('smtp1.google.com', true));
  203. $this->assertEquals(false, $this->rbl->getListingBls('smtp1.google.com'));
  204. $result = $this->rbl->getDetails('127.0.0.2');
  205. $this->assertContains('127.0.0.2', $result['sbl-xbl.spamhaus.org']['record']);
  206. $this->assertContains('http://www.spamhaus.org/SBL/sbl.lasso?query=SBL233', $result['sbl-xbl.spamhaus.org']['txt']);
  207. $this->assertContains('http://www.spamhaus.org/query/bl?ip=127.0.0.2', $result['sbl-xbl.spamhaus.org']['txt']);
  208. $this->assertContains('127.0.0.2', $result['bl.spamcop.net']['record']);
  209. $this->assertContains('Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2', $result['bl.spamcop.net']['txt']);
  210. $this->assertFalse($this->rbl->getDetails('smtp1.google.com'));
  211. }
  212. /**
  213. * Test without caching.
  214. *
  215. * @return boolean true on success, false on failure
  216. */
  217. public function testCacheNoCache()
  218. {
  219. for ($i=1; $i<=10; $i++) {
  220. $this->assertFalse($this->rbl->isListed($i.'.nohn.net'));
  221. $this->assertFalse($this->rbl->isListed(md5(rand()).'.nohn.net'));
  222. }
  223. }
  224. }
  225. ?>