RequestRemoteAddressTest.php 3.2 KB

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