|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AndrewAndante\SubMuncher\Test; |
|
4
|
|
|
|
|
5
|
|
|
use AndrewAndante\SubMuncher\SubMuncher; |
|
6
|
|
|
use AndrewAndante\SubMuncher\Util; |
|
7
|
|
|
|
|
8
|
|
|
class RealWorldTest extends \PHPUnit_Framework_TestCase |
|
9
|
|
|
{ |
|
10
|
|
|
private $json_data; |
|
11
|
|
|
|
|
12
|
|
|
protected function setUp() |
|
13
|
|
|
{ |
|
14
|
|
|
parent::setUp(); |
|
15
|
|
|
$this->json_data = json_decode(file_get_contents(__DIR__ . '/../data/real_world_data.json'), true); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function testConsolidateSubnets() |
|
19
|
|
|
{ |
|
20
|
|
|
$this->assertEquals( |
|
21
|
|
|
$this->json_data['consolidated_subnets'], |
|
22
|
|
|
SubMuncher::consolidate_subnets($this->json_data['subnets']) |
|
23
|
|
|
); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function testConsolidateSubnetsVerbose() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->assertEquals( |
|
29
|
|
|
[ |
|
30
|
|
|
'consolidated_subnets' => $this->json_data['consolidated_subnets'], |
|
31
|
|
|
'initial_IPs' => $this->json_data['raw_ips'], |
|
32
|
|
|
'total_IPs' => $this->json_data['raw_ips'], |
|
33
|
|
|
], |
|
34
|
|
|
SubMuncher::consolidate_subnets_verbose($this->json_data['subnets']) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @dataProvider provideConsolidatedSubnets |
|
40
|
|
|
*/ |
|
41
|
|
|
public function testConsolidateSubnetsWithMaxRules($rawIP) |
|
42
|
|
|
{ |
|
43
|
|
|
$result = SubMuncher::consolidate_subnets($this->json_data['subnets'], 25); |
|
44
|
|
|
$this->assertLessThanOrEqual(25, count($result)); |
|
45
|
|
|
$fail = true; |
|
46
|
|
|
foreach ($result as $subnet) { |
|
47
|
|
|
if (Util::cidr_contains($subnet, $rawIP)) { |
|
48
|
|
|
$fail = false; |
|
49
|
|
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
if ($fail) { |
|
53
|
|
|
$this->fail($rawIP . " not contained in any subnets returned"); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
public function provideConsolidatedSubnets() |
|
59
|
|
|
{ |
|
60
|
|
|
$ips = json_decode(file_get_contents(__DIR__ . '/../data/real_world_data.json'), true)['raw_ips']; |
|
61
|
|
|
$return = array_map(function ($value) { |
|
62
|
|
|
return [$value]; |
|
63
|
|
|
}, $ips); |
|
64
|
|
|
return $return; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|