BxAntispamModuleTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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', $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 || 'block' != $sDnsblBehaviour) { // DNSBL shouldn't be called if it isn't enabled or set in 'approval' mode
  112. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  113. } elseif ('on' == $sDnsblEnable && 'block' == $sDnsblBehaviour) {
  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. return array(
  131. array(true, false, 'approval', false, false),
  132. array(false, false, 'approval', false, true),
  133. array(false, true, 'approval', false, true),
  134. array(false, true, 'block', false, true),
  135. array(false, true, 'approval', true, true),
  136. array(false, true, 'block', true, false),
  137. );
  138. }
  139. /**
  140. * @dataProvider providerForServiceCheckLogin
  141. */
  142. public function testServiceCheckLogin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bResultEmptyString)
  143. {
  144. // set different options
  145. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  146. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour', $sDnsblBehaviour);
  147. // first IP address block checking is called
  148. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  149. ->will($this->returnValue($bIpBlocked));
  150. if ($bIpBlocked || 'on' != $sDnsblEnable || 'block' != $sDnsblBehaviour) { // if ip is blocked, or DNSBL isn't enabled, or set in 'approval' mode - DNSBL checking shouldn't be called
  151. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  152. } elseif (!$bIpBlocked && 'on' == $sDnsblEnable && 'block' == $sDnsblBehaviour) { // call DNSBL checking only if enabled, and set in 'block' mode, and IP isn;t already blocked
  153. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  154. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->_sSampleIP)
  155. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  156. }
  157. // empty string - no spam detected
  158. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckLogin($this->_sSampleIP)));
  159. }
  160. public function providerForServiceIsIpDnsBlacklisted()
  161. {
  162. return array(
  163. array(false, false, false),
  164. array(true, false, true),
  165. array(true, true, false),
  166. array(false, true, false),
  167. );
  168. }
  169. /**
  170. * @dataProvider providerForServiceIsIpDnsBlacklisted
  171. */
  172. public function testServiceIsIpDnsBlacklisted($bDnsblSpammer, $bDnsblWhitelisted, $bRes)
  173. {
  174. // check of whitelisting should be called once
  175. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  176. ->with($this->equalTo($this->_sSampleIP))
  177. ->will($this->returnValue(false));
  178. // check in 'spammers' chain
  179. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  180. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  181. ->will($this->returnValue($bDnsblSpammer ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  182. if ($bDnsblSpammer) { // if found in 'spammers' chain, then check if it is whitelisted in 'whitelist' chain
  183. $this->_oMockDNSBlacklists->expects($this->at(1))->method('dnsbl_lookup_ip')
  184. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_WHITELIST), $this->equalTo($this->_sSampleIP))
  185. ->will($this->returnValue($bDnsblWhitelisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  186. }
  187. // check result boolean value
  188. $this->assertEquals($bRes, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  189. }
  190. public function testServiceIsIpDnsBlacklistedWhenIpIsWhiletisted()
  191. {
  192. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  193. ->will($this->returnValue(true));
  194. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  195. $this->assertEquals(false, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  196. }
  197. public function testServiceIsIpWhitelisted()
  198. {
  199. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted');
  200. $this->_oModule->serviceIsIpWhitelisted($this->_sSampleIP);
  201. }
  202. public function testServiceIsIpBlocked()
  203. {
  204. $this->_oMockIP->expects($this->once())->method('isIpBlocked');
  205. $this->_oModule->serviceIsIpBlocked($this->_sSampleIP);
  206. }
  207. public function testServiceBlockIp()
  208. {
  209. $this->_oMockIP->expects($this->once())->method('blockIp');
  210. $this->_oModule->serviceBlockIp($this->_sSampleIP);
  211. }
  212. }