clrghouz/tests/Feature/PacketTest.php

256 lines
8.4 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Feature;
use Tests\TestCase;
2022-11-13 13:29:55 +00:00
use App\Classes\File;
use App\Classes\FTN\Packet;
use App\Models\{Address,Domain,System,Zone};
class PacketTest extends TestCase
{
private System $so;
private Domain $do;
private function init()
{
System::unguard();
Domain::unguard();
$this->so = System::firstOrCreate(['name'=>'test','sysop'=>'sysop','location'=>'location','active'=>TRUE]);
2021-08-29 14:10:28 +00:00
$this->do = Domain::firstOrCreate(['name'=>'packets','active'=>TRUE]);
}
public function test_nomsgid_origin()
{
$this->init();
Zone::unguard();
Address::unguard();
$zo = Zone::firstOrCreate(['zone_id'=>21,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>3,'node_id'=>151,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
$hub = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>1,'node_id'=>1,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
$ao = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>1,'node_id'=>4,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
// This packet has an incorrect zone in the Origin
$f = new File(__DIR__.'/data/test_nomsgid_origin.pkt');
2022-11-13 13:29:55 +00:00
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());
2022-11-13 13:29:55 +00:00
$this->assertEquals(1,$pkt->count());
2022-11-13 13:29:55 +00:00
$messages = FALSE;
foreach ($pkt as $msg) {
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
2022-11-13 13:29:55 +00:00
$this->assertNotFalse($msg->pathaddress->search($hub->id));
2022-11-13 13:29:55 +00:00
$this->assertCount(1,$msg->rogue_path);
$this->assertNotFalse($msg->rogue_path->search('21:999/1'));
2022-11-13 13:29:55 +00:00
$this->assertCount(3,$msg->rogue_seenby);
$this->assertNotFalse($msg->rogue_seenby->search('21:999/1'));
$this->assertNotFalse($msg->rogue_seenby->search('21:999/999'));
}
2022-11-13 13:29:55 +00:00
$this->assertTrue($messages);
}
}
public function test_nomsgid_noorigin()
{
$this->init();
Zone::unguard();
Address::unguard();
$zo = Zone::firstOrCreate(['zone_id'=>10,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>999,'node_id'=>1,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
// This packet has an incorrect zone in the Origin
$f = new File(__DIR__.'/data/test_nomsgid_noorigin.pkt');
2022-11-13 13:29:55 +00:00
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize(),$this->so);
2022-11-13 13:29:55 +00:00
$this->assertEquals(1,$pkt->count());
2022-11-13 13:29:55 +00:00
$messages = FALSE;
foreach ($pkt as $msg) {
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
2022-11-13 13:29:55 +00:00
$this->assertCount(1,$msg->rogue_path);
$this->assertNotFalse($msg->rogue_path->search('10:1/1'));
2022-11-13 13:29:55 +00:00
$this->assertCount(0,$msg->rogue_seenby);
}
2022-11-13 13:29:55 +00:00
$this->assertTrue($messages);
}
}
public function test_msgid_origin()
{
$this->init();
Zone::unguard();
Address::unguard();
$zo = Zone::firstOrCreate(['zone_id'=>10,'default'=>TRUE,'active'=>TRUE,'domain_id'=>$this->do->id,'system_id'=>$this->so->id]);
$src = Address::firstOrCreate(['zone_id'=>$zo->id,'region_id'=>0,'host_id'=>999,'node_id'=>1,'point_id'=>0,'role'=>Address::NODE_ACTIVE,'active'=>TRUE,'system_id'=>$this->so->id]);
// This packet has an incorrect zone in the Origin
$f = new File(__DIR__.'/data/test_msgid_origin.pkt');
2022-11-13 13:29:55 +00:00
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize(),$this->so);
2022-11-13 13:29:55 +00:00
$this->assertEquals(1,$pkt->count());
2022-11-13 13:29:55 +00:00
$messages = FALSE;
foreach ($pkt as $msg) {
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
$this->assertCount(0,$msg->rogue_path);
2022-11-13 13:29:55 +00:00
$this->assertCount(1,$msg->rogue_seenby);
$this->assertNotFalse($msg->rogue_seenby->search('10:1/1'));
2022-11-13 13:29:55 +00:00
$this->assertNotFalse($msg->seenaddress->search($src->id));
}
2022-11-13 13:29:55 +00:00
$this->assertTrue($messages);
}
}
public function test_packet_parse()
{
// This packet has a SOH<char>SOH sequence
$f = new File(__DIR__.'/data/test_binary_content-2.pkt');
2022-11-13 13:29:55 +00:00
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());
2022-11-13 13:29:55 +00:00
$this->assertEquals(1,$pkt->count());
$messages = FALSE;
foreach ($pkt as $msg) {
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
$this->assertSame('21:1/151 6189F64C',$msg->msgid);
$this->assertSame('db727bd3778ddd457784ada4bf016010',md5($msg->message));
2022-11-13 13:29:55 +00:00
$this->assertSame('5b627ab5936b0550a97b738f4deff419',md5($msg->message_src));
$this->assertCount(3,$msg->rogue_path);
$this->assertCount(170,$msg->rogue_seenby);
$this->assertContains('3/2744 4/100 106 5/100',$msg->seenby);
$this->assertContains('1/151 100 3/100',$msg->path);
$this->assertCount(11,$msg->seenby);
$this->assertCount(0,$msg->unknown);
}
$this->assertTrue($messages);
}
// This packet has SOH in the message content
$f = new File(__DIR__.'/data/test_binary_content.pkt');
2022-11-13 13:29:55 +00:00
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());
2022-11-13 13:29:55 +00:00
$this->assertEquals(1,$pkt->count());
$messages = FALSE;
foreach ($pkt as $msg) {
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
$this->assertSame('21:1/126 eec6e958',$msg->msgid);
$this->assertSame('5a525cc1c393292dc65160a852d4d615',md5($msg->message));
2022-11-13 13:29:55 +00:00
$this->assertSame('a3193edcc68521d4ed07da6db2aeb0b6',md5($msg->message_src));
$this->assertCount(3,$msg->rogue_path);
$this->assertCount(161,$msg->rogue_seenby);
$this->assertContains('1/995 2/100 116 1202 3/100 105 107 108 109 110 111 112 113 117 119',$msg->seenby);
$this->assertContains('1/126 100 3/100',$msg->path);
$this->assertCount(10,$msg->seenby);
$this->assertCount(0,$msg->unknown);
}
$this->assertTrue($messages);
}
// This packet has an incorrect zone in the Origin
$f = new File(__DIR__.'/data/test_msgid_origin.pkt');
2022-11-13 13:29:55 +00:00
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());
2022-11-13 13:29:55 +00:00
$this->assertEquals(1,$pkt->count());
$messages = FALSE;
foreach ($pkt as $msg) {
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
$this->assertSame('10:999/1 612aecda',$msg->msgid);
$this->assertSame('61078e680cda04c8b5eba0f712582e70',md5($msg->message));
$this->assertSame('b9d65d4f7319ded282f3f1986276ae79',md5($msg->message_src));
$this->assertCount(1,$msg->pathaddress);
$this->assertCount(1,$msg->rogue_seenby);
$this->assertContains('1/1 999/1 999',$msg->seenby);
$this->assertContains('999/1',$msg->path);
$this->assertCount(1,$msg->seenby);
$this->assertCount(0,$msg->unknown);
}
$this->assertTrue($messages);
}
}
public function test_soh_in_origin()
{
// This packet has a SOH<char>SOH sequence
$f = new File(__DIR__.'/data/test_msg_with_soh_in_origin.pkt');
foreach ($f as $packet) {
$pkt = Packet::process($packet,$f->itemName(),$f->itemSize());
$this->assertEquals(9,$pkt->count());
$messages = FALSE;
$c = 0;
foreach ($pkt as $msg) {
$c++;
$messages = TRUE;
$this->assertNotTrue($msg->isNetmail());
switch ($c) {
case 1:
$this->assertSame('3:712/886 220da89f',$msg->msgid);
$this->assertSame('9f5544bea46ef57a45f561b9e07dd71e',md5($msg->message));
$this->assertSame('9bf4b8c348ac235cc218577abf7140af',md5($msg->message_src));
$this->assertCount(3,$msg->rogue_path);
$this->assertCount(16,$msg->rogue_seenby);
$this->assertContains('633/0 267 280 281 408 410 412 509 509 640/1384 712/114 550 620 848',$msg->seenby);
$this->assertContains('712/886 848 633/280',$msg->path);
$this->assertCount(2,$msg->seenby);
$this->assertCount(0,$msg->unknown);
break;
case 4:
$this->assertSame('',$msg->msgid);
$this->assertSame('b975057002def556c5a9497aacd000fb',md5($msg->message));
$this->assertSame('c90dd234d2aa029af22c453a25b79a4e',md5($msg->message_src));
$this->assertCount(3,$msg->rogue_path);
$this->assertCount(19,$msg->rogue_seenby);
$this->assertContains('633/267 280 281 384 408 410 412 418 420 509 509 712/848 770/1 100 330',$msg->seenby);
$this->assertContains('772/210 770/1 633/280',$msg->path);
$this->assertCount(2,$msg->seenby);
$this->assertCount(0,$msg->unknown);
default:
continue 2;
}
}
$this->assertTrue($messages);
}
}
}