BxAntispamModuleTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. /**
  3. * Test Antispam module
  4. */
  5. class BxAntispamModuleTest extends BxDolTestCase
  6. {
  7. protected $_sSampleEmail = 'some@email.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(): void
  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(): void
  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. static public function providerForServiceIsSpam()
  37. {
  38. $any = null;//'ANY';
  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. $sContent = $this->anything();
  81. $this->assertEquals($bRes, $this->_oModule->serviceIsSpam($sContent, $this->_sSampleIP));
  82. }
  83. static public function providerForServiceCheckJoin()
  84. {
  85. $any = null;//'ANY';
  86. return array(
  87. array(true, $any, $any, $any, $any, false, false), // join is NOT allowed for blocked IPs
  88. array(false, '', $any, $any, false, false, true), // join is allowed if IP isn't blocked, DNSBL checking disabled, not listed in StopForumSpam
  89. array(false, 'on', 'approval', false, false, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "approval" mode, but not listed
  90. array(false, 'on', 'block', false, false, false, true), // join is allowed if IP isn't blocked and DNSBL enabled in "block", but not listed
  91. // array(false, 'on', 'approval', true, false, true, true), // join is allowed if IP isn't blocked and DNSBL enabled in "approval" and IP listed
  92. // array(false, 'on', 'block', true, $any, false, false), // join is NOT allowed if IP isn't blocked and DNSBL enabled in "block" and IP listed
  93. array(false, '', $any, $any, true, false, false), // join is NOT allowed if IP isn't blocked but listed in StopForumSpam
  94. );
  95. }
  96. /**
  97. * @dataProvider providerForServiceCheckJoin
  98. */
  99. public function testServiceCheckJoin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bStopForumSpamSpammer, $bResultSetApprove, $bResultEmptyString)
  100. {
  101. // set different options
  102. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  103. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour_join', $sDnsblBehaviour);
  104. // first IP address block checking is called
  105. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  106. ->with($this->equalTo($this->_sSampleIP))
  107. ->will($this->returnValue($bIpBlocked));
  108. if ($bIpBlocked) { // if ip is blocked no other methods should be called
  109. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  110. $this->_oMockStopForumSpam->expects($this->never())->method('isSpammer');
  111. } elseif (!$bIpBlocked) { // if ip is NOT blocked - perform further checks
  112. if ('on' != $sDnsblEnable) { // DNSBL shouldn't be called if it isn't enabled
  113. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  114. } elseif ('on' == $sDnsblEnable) {
  115. /*
  116. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  117. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  118. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  119. */
  120. $this->_oMockDNSBlacklists->expects($this->atLeastOnce())->method('dnsbl_lookup_ip');
  121. }
  122. if ('on' == $sDnsblEnable && $bDnsblIpBlacklisted && 'block' == $sDnsblBehaviour) { // StopForumSpam shouldn't be called if DNSBL detected spam
  123. $this->_oMockStopForumSpam->expects($this->never())->method('isSpammer');
  124. } else {
  125. $this->_oMockStopForumSpam->expects($this->once())->method('isSpammer')
  126. ->will($this->returnValue($bStopForumSpamSpammer));
  127. }
  128. }
  129. // empty string - no spam detected
  130. $bSetApprove = false;
  131. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckJoin($this->_sSampleEmail, $bSetApprove, $this->_sSampleIP)));
  132. $this->assertTrue($bResultSetApprove == $bSetApprove);
  133. }
  134. static public function providerForServiceCheckLogin()
  135. {
  136. $any = null;//'ANY';
  137. return array(
  138. array(true, $any, $any, $any, false),
  139. array(false, false, $any, $any, true),
  140. array(false, true, 'log', false, true),
  141. array(false, true, 'block', false, true),
  142. array(false, true, 'log', true, true),
  143. // array(false, true, 'block', true, false),
  144. );
  145. }
  146. /**
  147. * @dataProvider providerForServiceCheckLogin
  148. */
  149. public function testServiceCheckLogin($bIpBlocked, $sDnsblEnable, $sDnsblBehaviour, $bDnsblIpBlacklisted, $bResultEmptyString)
  150. {
  151. // set different options
  152. $this->_oModule->_oConfig->setAntispamOption('dnsbl_enable', $sDnsblEnable);
  153. $this->_oModule->_oConfig->setAntispamOption('dnsbl_behaviour_login', $sDnsblBehaviour);
  154. // first IP address block checking is called
  155. $this->_oMockIP->expects($this->once())->method('isIpBlocked')
  156. ->will($this->returnValue($bIpBlocked));
  157. if ($bIpBlocked || 'on' != $sDnsblEnable) { // if ip is blocked, or DNSBL isn't enabled - DNSBL checking shouldn't be called
  158. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  159. } elseif (!$bIpBlocked && 'on' == $sDnsblEnable) { // call DNSBL checking only if enabled and IP isn't already blocked
  160. /*
  161. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  162. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->_sSampleIP)
  163. ->will($this->returnValue($bDnsblIpBlacklisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  164. */
  165. $this->_oMockDNSBlacklists->expects($this->atLeastOnce())->method('dnsbl_lookup_ip');
  166. }
  167. // empty string - no spam detected
  168. $this->assertTrue($bResultEmptyString == ('' == $this->_oModule->serviceCheckLogin($this->_sSampleIP)));
  169. }
  170. static public function providerForServiceIsIpDnsBlacklisted()
  171. {
  172. return array(
  173. array(false, false, false),
  174. //array(true, false, true),
  175. //array(true, true, false),
  176. array(false, true, false),
  177. );
  178. }
  179. /**
  180. * @dataProvider providerForServiceIsIpDnsBlacklisted
  181. */
  182. public function testServiceIsIpDnsBlacklisted($bDnsblSpammer, $bDnsblWhitelisted, $bRes)
  183. {
  184. // check of whitelisting should be called once
  185. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  186. ->with($this->equalTo($this->_sSampleIP))
  187. ->will($this->returnValue(false));
  188. /*
  189. // check in 'spammers' chain
  190. $this->_oMockDNSBlacklists->expects($this->at(0))->method('dnsbl_lookup_ip')
  191. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_SPAMMERS), $this->equalTo($this->_sSampleIP))
  192. ->will($this->returnValue($bDnsblSpammer ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  193. if ($bDnsblSpammer) { // if found in 'spammers' chain, then check if it is whitelisted in 'whitelist' chain
  194. $this->_oMockDNSBlacklists->expects($this->at(1))->method('dnsbl_lookup_ip')
  195. ->with($this->equalTo(BX_DOL_DNSBL_CHAIN_WHITELIST), $this->equalTo($this->_sSampleIP))
  196. ->will($this->returnValue($bDnsblWhitelisted ? BX_DOL_DNSBL_POSITIVE : BX_DOL_DNSBL_NEGATIVE));
  197. }
  198. */
  199. $this->_oMockDNSBlacklists
  200. ->expects($this->exactly($bDnsblSpammer ? 2 : 1))
  201. ->method('dnsbl_lookup_ip');
  202. // check result boolean value
  203. $this->assertEquals($bRes, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  204. }
  205. public function testServiceIsIpDnsBlacklistedWhenIpIsWhiletisted()
  206. {
  207. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted')
  208. ->will($this->returnValue(true));
  209. $this->_oMockDNSBlacklists->expects($this->never())->method('dnsbl_lookup_ip');
  210. $this->assertEquals(false, $this->_oModule->serviceIsIpDnsBlacklisted($this->_sSampleIP));
  211. }
  212. public function testServiceIsIpWhitelisted()
  213. {
  214. $this->_oMockIP->expects($this->once())->method('isIpWhitelisted');
  215. $this->_oModule->serviceIsIpWhitelisted($this->_sSampleIP);
  216. }
  217. public function testServiceIsIpBlocked()
  218. {
  219. $this->_oMockIP->expects($this->once())->method('isIpBlocked');
  220. $this->_oModule->serviceIsIpBlocked($this->_sSampleIP);
  221. }
  222. public function testServiceBlockIp()
  223. {
  224. $this->_oMockIP->expects($this->once())->method('blockIp');
  225. $this->_oModule->serviceBlockIp($this->_sSampleIP);
  226. }
  227. }