RequestRemoteAddressTest.php 3.9 KB

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