Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions lib/Rdata/TXT.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,26 @@ public function toText(): string

public function toWire(): string
{
return $this->text ?? '';
$wire = '';
foreach (str_split($this->text ?? '', 255) as $chunk) {
$wire .= chr(strlen($chunk)).$chunk;
}

return '' === $wire ? "\x00" : $wire;
}

public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void
{
$rdLength = $rdLength ?? strlen($rdata);
$this->setText(substr($rdata, $offset, $rdLength));
$offset += $rdLength;
$end = $offset + $rdLength;
$text = '';
while ($offset < $end) {
$length = ord($rdata[$offset]);
++$offset;
$text .= substr($rdata, $offset, $length);
$offset += $length;
}
$this->setText($text, true);
}

public function fromText(string $text): void
Expand Down
11 changes: 6 additions & 5 deletions tests/Rdata/SpfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ public function testToText(): void

public function testWire(): void
{
$wireFormat = chr(49).'v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all';
$offset = 1;
$rdLength = 49;
$text = 'v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all';
$wireFormat = chr(strlen($text)).$text;

$spf = new SPF();
$spf->setText('v=spf1 ip4:192.0.2.0/24 ip4:198.51.100.123 a -all');
$spf->setText($text);
$this->assertEquals($wireFormat, $spf->toWire());

$offset = 0;
$fromWire = new SPF();
$fromWire->fromWire($wireFormat, $offset, $rdLength);
$fromWire->fromWire($wireFormat, $offset, strlen($wireFormat));
$this->assertEquals($spf, $fromWire);
}

Expand Down
39 changes: 34 additions & 5 deletions tests/Rdata/TxtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,44 @@ public function testFromTxt(string $text, string $expectation): void
$this->assertEquals($expectation, $txt->getText());
}

public function testWire(): void
public function testWire1(): void
{
$expectation = 'This is some text. It\'s a nice piece of text.';
$text = 'This is some text. It\'s a nice piece of text.';
$txt = new TXT();
$txt->setText($expectation);
$txt->setText($text);

$this->assertEquals(chr(strlen($text)).$text, $txt->toWire());

$this->assertEquals($expectation, $txt->toWire());
$fromWire = new TXT();
$fromWire->fromWire($expectation);
$fromWire->fromWire($txt->toWire());
$this->assertEquals($txt, $fromWire);
}

public function testWire2(): void
{
$text = str_repeat('a', 255).str_repeat('b', 100);
$txt = new TXT();
$txt->setText($text);

$this->assertEquals(
chr(255).str_repeat('a', 255).chr(100).str_repeat('b', 100),
$txt->toWire()
);

$fromWire = new TXT();
$fromWire->fromWire($txt->toWire());
$this->assertEquals($text, $fromWire->getText());
}

public function testWire3(): void
{
$txt = new TXT();
$txt->setText('');

$this->assertEquals("\x00", $txt->toWire());

$fromWire = new TXT();
$fromWire->fromWire($txt->toWire());
$this->assertEquals('', $fromWire->getText());
}
}
Loading