BxAntispamModuleTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * Test Antispam module
  4. */
  5. class BxAntispamModuleTest extends BxDolTestCase
  6. {
  7. protected $_sSampleEmail = 'some@example.com';
  8. protected $_sSampleIP = '0.0.0.0';
  9. protected $_oModule;
  10. protected $_oMockIP;
  11. protected $_oMockDNSBlacklists;
  12. protected $_oMockDNSURIBlacklists;
  13. protected $_oMockStopForumSpam;
  14. protected $_oMockAkismet;
  15. protected function setUp()
  16. {
  17. bx_import('BxDolModule');
  18. $this->_oModule = BxDolModule::getInstance('bx_antispam');
  19. $this->_oMockIP = $this->bxMockGet('BxAntispamIP', $this->_oModule->_aModule);
  20. $this->_oMockDNSBlacklists = $this->bxMockGet('BxAntispamDNSBlacklists', $this->_oModule->_aModule, true);
  21. $this->_oMockDNSURIBlacklists = $this->bxMockGet('BxAntispamDNSURIBlacklists', $this->_oModule->_aModule, true);
  22. $this->_oMockStopForumSpam = $this->bxMockGet('BxAntispamStopForumSpam', $this->_oModule->_aModule);
  23. $this->_oMockAkismet = $this->bxMockGet('BxAntispamAkismet', $this->_oModule->_aModule);
  24. }
  25. protected function tearDown()
  26. {
  27. // free all mock objects after each test call
  28. $this->bxMockFree($this->_oMockIP);
  29. $this->bxMockFree($this->_oMockDNSBlacklists);
  30. $this->bxMockFree($this->_oMockDNSURIBlacklists);
  31. $this->bxMockFree($this->_oMockStopForumSpam);
  32. $this->bxMockFree($this->_oMockAkismet);
  33. // restore options after each test call
  34. $this->_oModule->_oConfig->restoreAntispamOptions();
  35. }
  36. public function providerForServiceIsSpam()
  37. {
  38. $any = $this->anything();
  39. return array(
  40. array(true, $any, $any, $any, $any, $any, $any, false), // no spam detection for admin account
  41. array(false, true, $any, $any, $any, $any, $any, false), // no spam detection for whitelisted IP
  42. array(false, false, 'on', false, $any, $any, 'on', false), // no spam detection if not listed in URIDNSBL (in "blocking" mode)
  43. array(false, false, 'on', true, $any, $any, 'on', true), // it is spam if listed in URIDNSBL (in "blocking" mode)
  44. array(false, false, '', $any, 'on', false, 'on', false), // no spam detection if not listed in Akismet (in "blocking" mode)
  45. array(false, false, '', $any, 'on', true, 'on', true), // it is spam if listed in Akismet (in "blocking" mode)
  46. array(false, false, 'on', true, 'on', true, '', false), // no spam detection in URIDNSBL nor Akismet if not in "blocking" mode
  47. );
  48. }
  49. /**
  50. * @dataProvider providerForServiceIsSpam
  51. */
  52. public function testServiceIsSpam($isAdmin, $bIpWhitelisted, $sUriDnsblEnable, $bUriDnsblBlacklisted, $sAkismetlEnable, $bAkismetBlacklisted, $sBlock, $bRes)
  53. {
  54. $this->_oModule->_oConfig->setAntispamOption('antispam_report', ''); // turn off reporting during testing
  55. $this->_oModule->_oConfig->setAntispamOption('uridnsbl_enable', $sUriDnsblEnable);
  56. $this->_oModule->_oConfig->setAntispamOption('akismet_enable', $sAkismetlEnable);
  57. $this->_oModule->_oConfig->setAntispamOption('antispam_block', $sBlock);
  58. $GLOBALS['logged']['admin'] = $isAdmin;
  59. if ($isAdmin) { // if admin no futher call are performed
  60. $this->_oMockIP->expects($this->never())->method('isIpWhitelisted');
  61. $this->_oMockDNSURIBlacklists->expects($this->never())->method('isSpam');
  62. $this->_oMockAkismet->expects($this->never())->method('isSpam');
  63. } else { // if not admin then check for whitelisting
  64. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  65. ->will($this->returnValue($bIpWhitelisted));
  66. }
  67. if ('on' != $sUriDnsblEnable || $isAdmin || $bIpWhitelisted) { // don't check in DNDBL if anything is true
  68. $this->_oMockDNSURIBlacklists->expects($this->never())->method('isSpam');
  69. } else { // check in URIDNSBL if URIDNSBL is enabled, not admin and IP is not whitelisted
  70. $this->_oMockDNSURIBlacklists->expects($this->once())->method('isSpam')
  71. ->will($this->returnValue($bUriDnsblBlacklisted));
  72. }
  73. if ('on' != $sAkismetlEnable || $isAdmin || $bIpWhitelisted || ($bUriDnsblBlacklisted && $sUriDnsblEnable)) { // don't check in Akismet if anything is true
  74. $this->_oMockAkismet->expects($this->never())->method('isSpam');
  75. } else { // check in Akismet if Akismet is enabled, not admin, IP is not whitelisted and not previouslu detected in DNSBL
  76. $this->_oMockAkismet->expects($this->once())->method('isSpam')
  77. ->will($this->returnValue($bAkismetBlacklisted));
  78. }
  79. // check result boolean value
  80. $this->assertEquals($bRes, $this->_oModule->serviceIsSpam($this->anything(), $this->_sSampleIP));
  81. }
  82. public function providerForServiceCheckJoin()
  83. {
  84. $any = $this->anything();
  85. return array(
  86. array(true, $any, $any, $any, $any, false), // join is NOT allowed for blocked IPs
  87. array(false, '', $any, $any, false, true), // join is allowed if IP isn't blocked, DNSBL checking disabled, not listed in StopForumSpam
  88. array(false, 'on', 'approval', false, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "approval" mode, but not listed
  89. array(false, 'on', 'block', false, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "block", but not listed
  90. array(false, 'on', 'approval', true, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "approval" and IP listed
  91. array(false, 'on', 'block', true, $any, false), // join is NOT allowed if IP isn't blocked and DNSBL enabled in "block" and IP listed
  92. array(false, '', $any, $any, true, false), // join is NOT allowed if IP isn't blocked but listed in StopForumSpam
  93. );
  94. }
  95. /**
  96. * @dataProvider providerForServiceCheckJoin
  97. */
  98. public function testServiceCheckJoin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bStopForumSpamSpammer, $bResultEmptyString)
  99. {
  100. // set different options
  101. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  102. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour_join', $sDnsblBehaviour);
  103. // first IP address block checking is called
  104. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  105. ->with($this->equalTo($this->_sSampleIP))
  106. ->will($this->returnValue($bIpBlocked));
  107. if ($bIpBlocked) { // if ip is blocked no other methods should be called
  108. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  109. $this->_oMockStopForumSpam->expects($this->never())->method('isSpammer');
  110. } elseif (!$bIpBlocked) { // if ip is NOT blocked - perform further checks
  111. if ('on' != $sDnsblEnable) { // DNSBL shouldn't be called if it isn't enabled
  112. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  113. } elseif ('on' == $sDnsblEnable) {
  114. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  115. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  116. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  117. }
  118. if ('on' == $sDnsblEnable && $bDnsblIpBlacklisted && 'block' == $sDnsblBehaviour) { // StopForumSpam shouldn't be called if DNSBL detected spam
  119. $this->_oMockStopForumSpam->expects($this->never())->method('isSpammer');
  120. } else {
  121. $this->_oMockStopForumSpam->expects($this->once())->method('isSpammer')
  122. ->will($this->returnValue($bStopForumSpamSpammer));
  123. }
  124. }
  125. // empty string - no spam detected
  126. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckJoin($this->_sSampleEmail, $this->_sSampleIP)));
  127. }
  128. public function providerForServiceCheckLogin()
  129. {
  130. $any = $this->anything();
  131. return array(
  132. array(true, $any, $any, $any, false),
  133. array(false, false, $any, $any, true),
  134. array(false, true, 'log', false, true),
  135. array(false, true, 'block', false, true),
  136. array(false, true, 'log', true, true),
  137. array(false, true, 'block', true, false),
  138. );
  139. }
  140. /**
  141. * @dataProvider providerForServiceCheckLogin
  142. */
  143. public function testServiceCheckLogin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bResultEmptyString)
  144. {
  145. // set different options
  146. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  147. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour_login', $sDnsblBehaviour);
  148. // first IP address block checking is called
  149. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  150. ->will($this->returnValue($bIpBlocked));
  151. if ($bIpBlocked || 'on' != $sDnsblEnable) { // if ip is blocked, or DNSBL isn't enabled - DNSBL checking shouldn't be called
  152. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  153. } elseif (!$bIpBlocked && 'on' == $sDnsblEnable) { // call DNSBL checking only if enabled and IP isn't already blocked
  154. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  155. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->_sSampleIP)
  156. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  157. }
  158. // empty string - no spam detected
  159. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckLogin($this->_sSampleIP)));
  160. }
  161. public function providerForServiceIsIpDnsBlacklisted()
  162. {
  163. return array(
  164. array(false, false, false),
  165. array(true, false, true),
  166. array(true, true, false),
  167. array(false, true, false),
  168. );
  169. }
  170. /**
  171. * @dataProvider providerForServiceIsIpDnsBlacklisted
  172. */
  173. public function testServiceIsIpDnsBlacklisted($bDnsblSpammer, $bDnsblWhitelisted, $bRes)
  174. {
  175. // check of whitelisting should be called once
  176. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  177. ->with($this->equalTo($this->_sSampleIP))
  178. ->will($this->returnValue(false));
  179. // check in 'spammers' chain
  180. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  181. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  182. ->will($this->returnValue($bDnsblSpammer ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  183. if ($bDnsblSpammer) { // if found in 'spammers' chain, then check if it is whitelisted in 'whitelist' chain
  184. $this->_oMockDNSBlacklists->expects($this->at(1))->method('dnsbl_lookup_ip')
  185. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_WHITELIST), $this->equalTo($this->_sSampleIP))
  186. ->will($this->returnValue($bDnsblWhitelisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  187. }
  188. // check result boolean value
  189. $this->assertEquals($bRes, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  190. }
  191. public function testServiceIsIpDnsBlacklistedWhenIpIsWhiletisted()
  192. {
  193. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  194. ->will($this->returnValue(true));
  195. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  196. $this->assertEquals(false, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  197. }
  198. public function testServiceIsIpWhitelisted()
  199. {
  200. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted');
  201. $this->_oModule->serviceIsIpWhitelisted($this->_sSampleIP);
  202. }
  203. public function testServiceIsIpBlocked()
  204. {
  205. $this->_oMockIP->expects($this->once())->method('isIpBlocked');
  206. $this->_oModule->serviceIsIpBlocked($this->_sSampleIP);
  207. }
  208. public function testServiceBlockIp()
  209. {
  210. $this->_oMockIP->expects($this->once())->method('blockIp');
  211. $this->_oModule->serviceBlockIp($this->_sSampleIP);
  212. }
  213. }