summaryrefslogtreecommitdiffstats
path: root/test/php/library/X509/JobTest.php
blob: 8b5ba53719334af3877d34145d5759b970bb71e9 (plain)
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
<?php

namespace Tests\Icinga\Modules\X509;

use Icinga\Module\X509\Job;
use PHPUnit\Framework\TestCase;

class JobTest extends TestCase
{
    public function testBinaryTransformsHumanReadableIPToItsPaddedVersionCorrectly()
    {
        $this->assertSame('0000000000000000000000000ad33720', bin2hex(Job::binary('10.211.55.32')));
        $this->assertSame(
            '2a0104a00004210208a951031cba4915',
            bin2hex(Job::binary('2a01:4a0:4:2102:8a9:5103:1cba:4915'))
        );
    }

    public function testIsIPV6()
    {
        $this->assertTrue(Job::isIPV6('::1'), 'Job::isIPV6() could not determine valid IPv6 as an IPv6');
        $this->assertFalse(Job::isIPV6('10.211.54.35'), 'Job::isIPV6() could determines IPv4 as an IPv6');
    }

    public function testAddrToNumberUndViceVersa()
    {
        $this->assertSame('10.211.55.32', Job::numberToAddr(Job::addrToNumber('10.211.55.32'), false));

        $this->assertSame('::1', Job::numberToAddr(Job::addrToNumber('::1')));
        $this->assertSame(
            '2a01:4a0:4:2102:8a9:5103:1cba:4915',
            Job::numberToAddr(Job::addrToNumber('2a01:4a0:4:2102:8a9:5103:1cba:4915'))
        );
    }

    public function testIsAddrInsideCidr()
    {
        $this->assertTrue(Job::isAddrInside(Job::addrToNumber('10.211.55.31'), '10.211.55.30', 24));
        $this->assertFalse(Job::isAddrInside(Job::addrToNumber('10.211.54.35'), '10.211.55.30', 24));

        $this->assertTrue(
            Job::isAddrInside(Job::addrToNumber('2001:db8:abcd:0012::1'), '2001:db8:abcd:0012::', 64)
        );
        $this->assertTrue(
            Job::isAddrInside(Job::addrToNumber('2001:db8:abcd:0012:ffff::1'), '2001:db8:abcd:0012::', 64)
        );

        $this->assertFalse(Job::isAddrInside(Job::addrToNumber('2001:db8:abcd::1'), '2001:db8:abcd:0012::', 64));
        $this->assertFalse(
            Job::isAddrInside(Job::addrToNumber('2001:db8:abcd:0011::'), '2001:db8:abcd:0012::', 64)
        );
    }
}