File: CidrValidatorTest.php

package info (click to toggle)
symfony 7.3.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,804 kB
  • sloc: php: 1,506,509; xml: 6,816; javascript: 1,043; sh: 586; makefile: 241; pascal: 70
file content (259 lines) | stat: -rw-r--r-- 8,525 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\Cidr;
use Symfony\Component\Validator\Constraints\CidrValidator;
use Symfony\Component\Validator\Constraints\Ip;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
use Symfony\Component\Validator\Tests\Constraints\Fixtures\StringableValue;

class CidrValidatorTest extends ConstraintValidatorTestCase
{
    protected function createValidator(): CidrValidator
    {
        return new CidrValidator();
    }

    public function testNullIsValid()
    {
        $this->validator->validate(null, new Cidr());

        $this->assertNoViolation();
    }

    public function testEmptyStringIsValid()
    {
        $this->validator->validate('', new Cidr());

        $this->assertNoViolation();
    }

    public function testInvalidConstraint()
    {
        $this->expectException(UnexpectedTypeException::class);

        $this->validator->validate('neko', new NotNull());
    }

    public function testExpectsStringCompatibleType()
    {
        $this->expectException(UnexpectedValueException::class);

        $this->validator->validate(123456, new Cidr());
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getWithInvalidNetmask')]
    public function testInvalidNetmask(string $cidr)
    {
        $this->validator->validate($cidr, new Cidr());

        $this
            ->buildViolation('This value is not a valid CIDR notation.')
            ->setCode(Cidr::INVALID_CIDR_ERROR)
            ->assertRaised();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getWithInvalidIps')]
    public function testInvalidIpValue(string $cidr)
    {
        $this->validator->validate($cidr, new Cidr());

        $this
            ->buildViolation('This value is not a valid CIDR notation.')
            ->setCode(Cidr::INVALID_CIDR_ERROR)
            ->assertRaised();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getValid')]
    public function testValidCidr(string|\Stringable $cidr, string $version)
    {
        $this->validator->validate($cidr, new Cidr(version: $version));

        $this->assertNoViolation();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getWithInvalidMasksAndIps')]
    public function testInvalidIpAddressAndNetmask(string|\Stringable $cidr)
    {
        $this->validator->validate($cidr, new Cidr());
        $this
            ->buildViolation('This value is not a valid CIDR notation.')
            ->setCode(Cidr::INVALID_CIDR_ERROR)
            ->assertRaised();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getOutOfRangeNetmask')]
    public function testOutOfRangeNetmask(string $cidr, int $maxExpected, ?string $version = null, ?int $min = null, ?int $max = null)
    {
        $cidrConstraint = new Cidr(
            version: $version,
            netmaskMin: $min,
            netmaskMax: $max,
        );
        $this->validator->validate($cidr, $cidrConstraint);

        $this
            ->buildViolation('The value of the netmask should be between {{ min }} and {{ max }}.')
            ->setParameter('{{ min }}', $cidrConstraint->netmaskMin)
            ->setParameter('{{ max }}', $maxExpected)
            ->setCode(Cidr::OUT_OF_RANGE_ERROR)
            ->assertRaised();
    }

    #[\PHPUnit\Framework\Attributes\DataProvider('getWithWrongVersion')]
    public function testWrongVersion(string $cidr, string $version)
    {
        $this->validator->validate($cidr, new Cidr(version: $version));

        $this
            ->buildViolation('This value is not a valid CIDR notation.')
            ->setCode(Cidr::INVALID_CIDR_ERROR)
            ->assertRaised();
    }

    public static function getWithInvalidIps(): array
    {
        return [
            ['0/20'],
            ['0.0/20'],
            ['0.0.0/20'],
            ['256.0.0.0/20'],
            ['0.256.0.0/21'],
            ['0.0.256.0/22'],
            ['0.0.0.256/30'],
            ['-1.0.0.0/15'],
            ['foobar/10'],
            ['z001:0db8:85a3:0000:0000:8a2e:0370:7334/20'],
            ['fe80/100'],
            ['fe80:8329/15'],
            ['fe80:::202:b3ff:fe1e:8329/128'],
            ['fe80::202:b3ff::fe1e:8329/48'],
            ['2001:0db8:85a3:0000:0000:8a2e:0370:0.0.0.0/32'],
            ['::0.0/32'],
            ['::0.0.0/32'],
            ['::256.0.0.0/32'],
            ['::0.256.0.0/32'],
            ['::0.0.256.0/32'],
            ['::0.0.0.256/32'],
            ['/32'],
            ['/128'],
        ];
    }

    public static function getValid(): array
    {
        return [
            ['127.0.0.0/32', Ip::ALL],
            ['0.0.0.0/32', Ip::V4],
            ['10.0.0.0/24', Ip::V4],
            ['123.45.67.178/20', Ip::V4],
            ['172.16.0.0/12', Ip::V4],
            ['192.168.1.0/25', Ip::V4],
            ['224.0.0.1/10', Ip::V4],
            ['255.255.255.255/20', Ip::V4],
            ['127.0.0.0/32', Ip::V4],
            ['2001:0db8:85a3:0000:0000:8a2e:0370:7334/128', Ip::V6],
            ['2001:0DB8:85A3:0000:0000:8A2E:0370:7334/128', Ip::V6],
            ['2001:0Db8:85a3:0000:0000:8A2e:0370:7334/32', Ip::V6],
            ['fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c/28', Ip::V6],
            ['fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c/55', Ip::V6],
            ['fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334/60', Ip::V6],
            ['fe80:0000:0000:0000:0202:b3ff:fe1e:8329/20', Ip::V6],
            ['fe80:0:0:0:202:b3ff:fe1e:8329/4', Ip::V6],
            ['fe80::202:b3ff:fe1e:8329/0', Ip::V6],
            ['0:0:0:0:0:0:0:0/1', Ip::V6],
            ['::/20', Ip::V6],
            ['0::/120', Ip::V6],
            ['::0/128', Ip::V6],
            ['0::0/56', Ip::V6],
            ['2001:0db8:85a3:0000:0000:8a2e:0.0.0.0/128', Ip::V6],
            ['::0.0.0.0/128', Ip::V6],
            ['::255.255.255.255/32', Ip::V6],
            ['::123.45.67.178/120', Ip::V6],
            ['::123.45.67.178/120', Ip::ALL],
            [new StringableValue('::123.45.67.178/120'), Ip::ALL],
        ];
    }

    public static function getWithInvalidNetmask(): array
    {
        return [
            ['192.168.1.0/-1'],
            ['0.0.0.0/foobar'],
            ['123.45.67.178/aaa'],
            ['172.16.0.0//'],
            ['255.255.255.255/1/4'],
            ['224.0.0.1'],
            ['127.0.0.0/28c'],
            ['2001:0Db8:85a3:0000:0000:8A2e:0370:7334/28a'],
            ['fdfe:dcba:9876:ffff:fdc6:c46b:bb8f:7d4c/neko'],
            ['fdc6:c46b:bb8f:7d4c:fdc6:c46b:bb8f:7d4c/-8amba'],
            ['fdc6:c46b:bb8f:7d4c:0000:8a2e:0370:7334/-1aa'],
            ['fe80:0000:0000:0000:0202:b3ff:fe1e:8329/11*'],
        ];
    }

    public static function getWithInvalidMasksAndIps(): array
    {
        return [
            ['0.0.0.0/foobar'],
            ['123.45.67.178/aaa'],
            ['172.16.0.0//'],
            ['172.16.0.0/a/'],
            ['172.16.0.0/1/'],
            ['fe80/neko'],
            ['fe80:8329/-8'],
            ['fe80:::202:b3ff:fe1e:8329//'],
            ['fe80::202:b3ff::fe1e:8329/1/'],
            ['::0.0.0/a/'],
            ['::256.0.0.0/-1aa'],
            ['::0.256.0.0/1b'],
            [new StringableValue('::0.256.0.0/1b')],
        ];
    }

    public static function getOutOfRangeNetmask(): array
    {
        return [
            ['10.0.0.0/24', 20, Ip::V4, 10, 20],
            ['10.0.0.0/128', 32],
            ['2001:0DB8:85A3:0000:0000:8A2E:0370:7334/24', 20, Ip::V6, 10, 20],
        ];
    }

    public static function getWithWrongVersion(): array
    {
        return [
            ['2001:0db8:85a3:0000:0000:8a2e:0370:7334/12', Ip::V4],
            ['0.0.0.0/31', Ip::V6],
            ['10.0.0.0/24', Ip::V6],
            ['2001:0db8:85a3:0000:0000:8a2e:0370:7334/13', Ip::V4],
        ];
    }

    public function testDoesNotModifyContextBetweenValidations()
    {
        $constraint = new Cidr();

        $this->validator->validate('1.2.3.4/28', $constraint);

        $this->assertNoViolation();

        $this->validator->validate('::1/64', $constraint);

        $this->assertNoViolation();
    }
}