RequestRemoteAddressTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  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 OCA\WorkflowEngine\Tests\Check;
  22. use OCP\IL10N;
  23. use OCP\IRequest;
  24. class RequestRemoteAddressTest extends \Test\TestCase {
  25. /** @var \OCP\IRequest|\PHPUnit\Framework\MockObject\MockObject */
  26. protected $request;
  27. /**
  28. * @return \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject
  29. */
  30. protected function getL10NMock() {
  31. $l = $this->getMockBuilder(IL10N::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $l->expects($this->any())
  35. ->method('t')
  36. ->willReturnCallback(function ($string, $args) {
  37. return sprintf($string, $args);
  38. });
  39. return $l;
  40. }
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->request = $this->getMockBuilder(IRequest::class)
  44. ->getMock();
  45. }
  46. public function dataExecuteCheckIPv4() {
  47. return [
  48. ['127.0.0.1/32', '127.0.0.1', true],
  49. ['127.0.0.1/32', '127.0.0.0', false],
  50. ['127.0.0.1/31', '127.0.0.0', true],
  51. ['127.0.0.1/32', '127.0.0.2', false],
  52. ['127.0.0.1/31', '127.0.0.2', false],
  53. ['127.0.0.1/30', '127.0.0.2', true],
  54. ];
  55. }
  56. /**
  57. * @dataProvider dataExecuteCheckIPv4
  58. * @param string $value
  59. * @param string $ip
  60. * @param bool $expected
  61. */
  62. public function testExecuteCheckMatchesIPv4($value, $ip, $expected) {
  63. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  64. $this->request->expects($this->once())
  65. ->method('getRemoteAddress')
  66. ->willReturn($ip);
  67. $this->assertEquals($expected, $check->executeCheck('matchesIPv4', $value));
  68. }
  69. /**
  70. * @dataProvider dataExecuteCheckIPv4
  71. * @param string $value
  72. * @param string $ip
  73. * @param bool $expected
  74. */
  75. public function testExecuteCheckNotMatchesIPv4($value, $ip, $expected) {
  76. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  77. $this->request->expects($this->once())
  78. ->method('getRemoteAddress')
  79. ->willReturn($ip);
  80. $this->assertEquals(!$expected, $check->executeCheck('!matchesIPv4', $value));
  81. }
  82. public function dataExecuteCheckIPv6() {
  83. return [
  84. ['::1/128', '::1', true],
  85. ['::2/128', '::3', false],
  86. ['::2/127', '::3', true],
  87. ['::1/128', '::2', false],
  88. ['::1/127', '::2', false],
  89. ['::1/126', '::2', true],
  90. ['1234::1/127', '1234::', true],
  91. ];
  92. }
  93. /**
  94. * @dataProvider dataExecuteCheckIPv6
  95. * @param string $value
  96. * @param string $ip
  97. * @param bool $expected
  98. */
  99. public function testExecuteCheckMatchesIPv6($value, $ip, $expected) {
  100. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  101. $this->request->expects($this->once())
  102. ->method('getRemoteAddress')
  103. ->willReturn($ip);
  104. $this->assertEquals($expected, $check->executeCheck('matchesIPv6', $value));
  105. }
  106. /**
  107. * @dataProvider dataExecuteCheckIPv6
  108. * @param string $value
  109. * @param string $ip
  110. * @param bool $expected
  111. */
  112. public function testExecuteCheckNotMatchesIPv6($value, $ip, $expected) {
  113. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  114. $this->request->expects($this->once())
  115. ->method('getRemoteAddress')
  116. ->willReturn($ip);
  117. $this->assertEquals(!$expected, $check->executeCheck('!matchesIPv6', $value));
  118. }
  119. }