RequestRemoteAddressTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\WorkflowEngine\Tests\Check;
  26. use OCP\IL10N;
  27. use OCP\IRequest;
  28. class RequestRemoteAddressTest extends \Test\TestCase {
  29. /** @var \OCP\IRequest|\PHPUnit\Framework\MockObject\MockObject */
  30. protected $request;
  31. /**
  32. * @return \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject
  33. */
  34. protected function getL10NMock() {
  35. $l = $this->getMockBuilder(IL10N::class)
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $l->expects($this->any())
  39. ->method('t')
  40. ->willReturnCallback(function ($string, $args) {
  41. return sprintf($string, $args);
  42. });
  43. return $l;
  44. }
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->request = $this->getMockBuilder(IRequest::class)
  48. ->getMock();
  49. }
  50. public function dataExecuteCheckIPv4() {
  51. return [
  52. ['127.0.0.1/32', '127.0.0.1', true],
  53. ['127.0.0.1/32', '127.0.0.0', false],
  54. ['127.0.0.1/31', '127.0.0.0', true],
  55. ['127.0.0.1/32', '127.0.0.2', false],
  56. ['127.0.0.1/31', '127.0.0.2', false],
  57. ['127.0.0.1/30', '127.0.0.2', true],
  58. ];
  59. }
  60. /**
  61. * @dataProvider dataExecuteCheckIPv4
  62. * @param string $value
  63. * @param string $ip
  64. * @param bool $expected
  65. */
  66. public function testExecuteCheckMatchesIPv4($value, $ip, $expected) {
  67. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  68. $this->request->expects($this->once())
  69. ->method('getRemoteAddress')
  70. ->willReturn($ip);
  71. $this->assertEquals($expected, $check->executeCheck('matchesIPv4', $value));
  72. }
  73. /**
  74. * @dataProvider dataExecuteCheckIPv4
  75. * @param string $value
  76. * @param string $ip
  77. * @param bool $expected
  78. */
  79. public function testExecuteCheckNotMatchesIPv4($value, $ip, $expected) {
  80. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  81. $this->request->expects($this->once())
  82. ->method('getRemoteAddress')
  83. ->willReturn($ip);
  84. $this->assertEquals(!$expected, $check->executeCheck('!matchesIPv4', $value));
  85. }
  86. public function dataExecuteCheckIPv6() {
  87. return [
  88. ['::1/128', '::1', true],
  89. ['::2/128', '::3', false],
  90. ['::2/127', '::3', true],
  91. ['::1/128', '::2', false],
  92. ['::1/127', '::2', false],
  93. ['::1/126', '::2', true],
  94. ['1234::1/127', '1234::', true],
  95. ];
  96. }
  97. /**
  98. * @dataProvider dataExecuteCheckIPv6
  99. * @param string $value
  100. * @param string $ip
  101. * @param bool $expected
  102. */
  103. public function testExecuteCheckMatchesIPv6($value, $ip, $expected) {
  104. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  105. $this->request->expects($this->once())
  106. ->method('getRemoteAddress')
  107. ->willReturn($ip);
  108. $this->assertEquals($expected, $check->executeCheck('matchesIPv6', $value));
  109. }
  110. /**
  111. * @dataProvider dataExecuteCheckIPv6
  112. * @param string $value
  113. * @param string $ip
  114. * @param bool $expected
  115. */
  116. public function testExecuteCheckNotMatchesIPv6($value, $ip, $expected) {
  117. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  118. $this->request->expects($this->once())
  119. ->method('getRemoteAddress')
  120. ->willReturn($ip);
  121. $this->assertEquals(!$expected, $check->executeCheck('!matchesIPv6', $value));
  122. }
  123. }