1
0

ThrottlerTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Security\Bruteforce;
  22. use OC\AppFramework\Utility\TimeFactory;
  23. use OC\Security\Bruteforce\Throttler;
  24. use OCP\IConfig;
  25. use OCP\IDBConnection;
  26. use OCP\ILogger;
  27. use Test\TestCase;
  28. /**
  29. * Based on the unit tests from Paragonie's Airship CMS
  30. * Ref: https://github.com/paragonie/airship/blob/7e5bad7e3c0fbbf324c11f963fd1f80e59762606/test/unit/Engine/Security/AirBrakeTest.php
  31. *
  32. * @package Test\Security\Bruteforce
  33. */
  34. class ThrottlerTest extends TestCase {
  35. /** @var Throttler */
  36. private $throttler;
  37. /** @var IDBConnection */
  38. private $dbConnection;
  39. /** @var ILogger */
  40. private $logger;
  41. /** @var IConfig */
  42. private $config;
  43. public function setUp() {
  44. $this->dbConnection = $this->createMock(IDBConnection::class);
  45. $this->logger = $this->createMock(ILogger::class);
  46. $this->config = $this->createMock(IConfig::class);
  47. $this->throttler = new Throttler(
  48. $this->dbConnection,
  49. new TimeFactory(),
  50. $this->logger,
  51. $this->config
  52. );
  53. return parent::setUp();
  54. }
  55. public function testCutoff() {
  56. // precisely 31 second shy of 12 hours
  57. $cutoff = $this->invokePrivate($this->throttler, 'getCutoff', [43169]);
  58. $this->assertSame(0, $cutoff->y);
  59. $this->assertSame(0, $cutoff->m);
  60. $this->assertSame(0, $cutoff->d);
  61. $this->assertSame(11, $cutoff->h);
  62. $this->assertSame(59, $cutoff->i);
  63. $this->assertSame(29, $cutoff->s);
  64. $cutoff = $this->invokePrivate($this->throttler, 'getCutoff', [86401]);
  65. $this->assertSame(0, $cutoff->y);
  66. $this->assertSame(0, $cutoff->m);
  67. $this->assertSame(1, $cutoff->d);
  68. $this->assertSame(0, $cutoff->h);
  69. $this->assertSame(0, $cutoff->i);
  70. // Leap second tolerance:
  71. $this->assertLessThan(2, $cutoff->s);
  72. }
  73. public function testSubnet() {
  74. // IPv4
  75. $this->assertSame(
  76. '64.233.191.254/32',
  77. $this->invokePrivate($this->throttler, 'getIPv4Subnet', ['64.233.191.254', 32])
  78. );
  79. $this->assertSame(
  80. '64.233.191.252/30',
  81. $this->invokePrivate($this->throttler, 'getIPv4Subnet', ['64.233.191.254', 30])
  82. );
  83. $this->assertSame(
  84. '64.233.191.240/28',
  85. $this->invokePrivate($this->throttler, 'getIPv4Subnet', ['64.233.191.254', 28])
  86. );
  87. $this->assertSame(
  88. '64.233.191.0/24',
  89. $this->invokePrivate($this->throttler, 'getIPv4Subnet', ['64.233.191.254', 24])
  90. );
  91. $this->assertSame(
  92. '64.233.188.0/22',
  93. $this->invokePrivate($this->throttler, 'getIPv4Subnet', ['64.233.191.254', 22])
  94. );
  95. // IPv6
  96. $this->assertSame(
  97. '2001:db8:85a3::8a2e:370:7334/127',
  98. $this->invokePrivate($this->throttler, 'getIPv6Subnet', ['2001:0db8:85a3:0000:0000:8a2e:0370:7334', 127])
  99. );
  100. $this->assertSame(
  101. '2001:db8:85a3::8a2e:370:7300/120',
  102. $this->invokePrivate($this->throttler, 'getIPv6Subnet', ['2001:0db8:85a3:0000:0000:8a2e:0370:7300', 120])
  103. );
  104. $this->assertSame(
  105. '2001:db8:85a3::/64',
  106. $this->invokePrivate($this->throttler, 'getIPv6Subnet', ['2001:0db8:85a3:0000:0000:8a2e:0370:7334', 64])
  107. );
  108. $this->assertSame(
  109. '2001:db8:85a3::/48',
  110. $this->invokePrivate($this->throttler, 'getIPv6Subnet', ['2001:0db8:85a3:0000:0000:8a2e:0370:7334', 48])
  111. );
  112. $this->assertSame(
  113. '2001:db8:8500::/40',
  114. $this->invokePrivate($this->throttler, 'getIPv6Subnet', ['2001:0db8:85a3:0000:0000:8a2e:0370:7334', 40])
  115. );
  116. }
  117. }