1
0

RequestRemoteAddressTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\WorkflowEngine\Tests\Check;
  7. use OCP\IL10N;
  8. use OCP\IRequest;
  9. class RequestRemoteAddressTest extends \Test\TestCase {
  10. /** @var \OCP\IRequest|\PHPUnit\Framework\MockObject\MockObject */
  11. protected $request;
  12. /**
  13. * @return \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject
  14. */
  15. protected function getL10NMock() {
  16. $l = $this->getMockBuilder(IL10N::class)
  17. ->disableOriginalConstructor()
  18. ->getMock();
  19. $l->expects($this->any())
  20. ->method('t')
  21. ->willReturnCallback(function ($string, $args) {
  22. return sprintf($string, $args);
  23. });
  24. return $l;
  25. }
  26. protected function setUp(): void {
  27. parent::setUp();
  28. $this->request = $this->getMockBuilder(IRequest::class)
  29. ->getMock();
  30. }
  31. public function dataExecuteCheckIPv4() {
  32. return [
  33. ['127.0.0.1/32', '127.0.0.1', true],
  34. ['127.0.0.1/32', '127.0.0.0', false],
  35. ['127.0.0.1/31', '127.0.0.0', true],
  36. ['127.0.0.1/32', '127.0.0.2', false],
  37. ['127.0.0.1/31', '127.0.0.2', false],
  38. ['127.0.0.1/30', '127.0.0.2', true],
  39. ];
  40. }
  41. /**
  42. * @dataProvider dataExecuteCheckIPv4
  43. * @param string $value
  44. * @param string $ip
  45. * @param bool $expected
  46. */
  47. public function testExecuteCheckMatchesIPv4($value, $ip, $expected) {
  48. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  49. $this->request->expects($this->once())
  50. ->method('getRemoteAddress')
  51. ->willReturn($ip);
  52. $this->assertEquals($expected, $check->executeCheck('matchesIPv4', $value));
  53. }
  54. /**
  55. * @dataProvider dataExecuteCheckIPv4
  56. * @param string $value
  57. * @param string $ip
  58. * @param bool $expected
  59. */
  60. public function testExecuteCheckNotMatchesIPv4($value, $ip, $expected) {
  61. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  62. $this->request->expects($this->once())
  63. ->method('getRemoteAddress')
  64. ->willReturn($ip);
  65. $this->assertEquals(!$expected, $check->executeCheck('!matchesIPv4', $value));
  66. }
  67. public function dataExecuteCheckIPv6() {
  68. return [
  69. ['::1/128', '::1', true],
  70. ['::2/128', '::3', false],
  71. ['::2/127', '::3', true],
  72. ['::1/128', '::2', false],
  73. ['::1/127', '::2', false],
  74. ['::1/126', '::2', true],
  75. ['1234::1/127', '1234::', true],
  76. ];
  77. }
  78. /**
  79. * @dataProvider dataExecuteCheckIPv6
  80. * @param string $value
  81. * @param string $ip
  82. * @param bool $expected
  83. */
  84. public function testExecuteCheckMatchesIPv6($value, $ip, $expected) {
  85. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  86. $this->request->expects($this->once())
  87. ->method('getRemoteAddress')
  88. ->willReturn($ip);
  89. $this->assertEquals($expected, $check->executeCheck('matchesIPv6', $value));
  90. }
  91. /**
  92. * @dataProvider dataExecuteCheckIPv6
  93. * @param string $value
  94. * @param string $ip
  95. * @param bool $expected
  96. */
  97. public function testExecuteCheckNotMatchesIPv6($value, $ip, $expected) {
  98. $check = new \OCA\WorkflowEngine\Check\RequestRemoteAddress($this->getL10NMock(), $this->request);
  99. $this->request->expects($this->once())
  100. ->method('getRemoteAddress')
  101. ->willReturn($ip);
  102. $this->assertEquals(!$expected, $check->executeCheck('!matchesIPv6', $value));
  103. }
  104. }