12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127 |
- <?php
- namespace React\Tests\Dns\Protocol;
- use React\Dns\Model\Message;
- use React\Dns\Protocol\Parser;
- use React\Tests\Dns\TestCase;
- class ParserTest extends TestCase
- {
- private $parser;
- /**
- * @before
- */
- public function setUpParser()
- {
- $this->parser = new Parser();
- }
- /**
- * @dataProvider provideConvertTcpDumpToBinary
- */
- public function testConvertTcpDumpToBinary($expected, $data)
- {
- $this->assertSame($expected, $this->convertTcpDumpToBinary($data));
- }
- public function provideConvertTcpDumpToBinary()
- {
- return array(
- array(chr(0x72).chr(0x62), "72 62"),
- array(chr(0x72).chr(0x62).chr(0x01).chr(0x00), "72 62 01 00"),
- array(chr(0x72).chr(0x62).chr(0x01).chr(0x00).chr(0x00).chr(0x01), "72 62 01 00 00 01"),
- array(chr(0x01).chr(0x00).chr(0x01), "01 00 01"),
- );
- }
- public function testParseRequest()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data = $this->convertTcpDumpToBinary($data);
- $request = $this->parser->parseMessage($data);
- $this->assertFalse(isset($request->data));
- $this->assertFalse(isset($request->consumed));
- $this->assertSame(0x7262, $request->id);
- $this->assertSame(false, $request->qr);
- $this->assertSame(Message::OPCODE_QUERY, $request->opcode);
- $this->assertSame(false, $request->aa);
- $this->assertSame(false, $request->tc);
- $this->assertSame(true, $request->rd);
- $this->assertSame(false, $request->ra);
- $this->assertSame(Message::RCODE_OK, $request->rcode);
- $this->assertCount(1, $request->questions);
- $this->assertSame('igor.io', $request->questions[0]->name);
- $this->assertSame(Message::TYPE_A, $request->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $request->questions[0]->class);
- }
- public function testParseResponse()
- {
- $data = "";
- $data .= "72 62 81 80 00 01 00 01 00 00 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // answer: offset pointer to igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 04"; // answer: rdlength 4
- $data .= "b2 4f a9 83"; // answer: rdata 178.79.169.131
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertSame(0x7262, $response->id);
- $this->assertSame(true, $response->qr);
- $this->assertSame(Message::OPCODE_QUERY, $response->opcode);
- $this->assertSame(false, $response->aa);
- $this->assertSame(false, $response->tc);
- $this->assertSame(true, $response->rd);
- $this->assertSame(true, $response->ra);
- $this->assertSame(Message::RCODE_OK, $response->rcode);
- $this->assertCount(1, $response->questions);
- $this->assertSame('igor.io', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_A, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_A, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame('178.79.169.131', $response->answers[0]->data);
- }
- public function testParseRequestWithTwoQuestions()
- {
- $data = "";
- $data .= "72 62 01 00 00 02 00 00 00 00 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "03 77 77 77 04 69 67 6f 72 02 69 6f 00"; // question: www.igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data = $this->convertTcpDumpToBinary($data);
- $request = $this->parser->parseMessage($data);
- $this->assertCount(2, $request->questions);
- $this->assertSame('igor.io', $request->questions[0]->name);
- $this->assertSame(Message::TYPE_A, $request->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $request->questions[0]->class);
- $this->assertSame('www.igor.io', $request->questions[1]->name);
- $this->assertSame(Message::TYPE_A, $request->questions[1]->type);
- $this->assertSame(Message::CLASS_IN, $request->questions[1]->class);
- }
- public function testParseAnswerWithInlineData()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 04"; // answer: rdlength 4
- $data .= "b2 4f a9 83"; // answer: rdata 178.79.169.131
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_A, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame('178.79.169.131', $response->answers[0]->data);
- }
- public function testParseAnswerWithExcessiveTtlReturnsZeroTtl()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "ff ff ff ff"; // answer: ttl 2^32 - 1
- $data .= "00 04"; // answer: rdlength 4
- $data .= "b2 4f a9 83"; // answer: rdata 178.79.169.131
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_A, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(0, $response->answers[0]->ttl);
- $this->assertSame('178.79.169.131', $response->answers[0]->data);
- }
- public function testParseAnswerWithTtlExactlyBoundaryReturnsZeroTtl()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "80 00 00 00"; // answer: ttl 2^31
- $data .= "00 04"; // answer: rdlength 4
- $data .= "b2 4f a9 83"; // answer: rdata 178.79.169.131
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_A, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(0, $response->answers[0]->ttl);
- $this->assertSame('178.79.169.131', $response->answers[0]->data);
- }
- public function testParseAnswerWithMaximumTtlReturnsExactTtl()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "7f ff ff ff"; // answer: ttl 2^31 - 1
- $data .= "00 04"; // answer: rdlength 4
- $data .= "b2 4f a9 83"; // answer: rdata 178.79.169.131
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_A, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(0x7fffffff, $response->answers[0]->ttl);
- $this->assertSame('178.79.169.131', $response->answers[0]->data);
- }
- public function testParseAnswerWithUnknownType()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "23 28 00 01"; // answer: type 9000, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 05"; // answer: rdlength 5
- $data .= "68 65 6c 6c 6f"; // answer: rdata "hello"
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(9000, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame('hello', $response->answers[0]->data);
- }
- public function testParseResponseWithCnameAndOffsetPointers()
- {
- $data = "";
- $data .= "9e 8d 81 80 00 01 00 01 00 00 00 00"; // header
- $data .= "04 6d 61 69 6c 06 67 6f 6f 67 6c 65 03 63 6f 6d 00"; // question: mail.google.com
- $data .= "00 05 00 01"; // question: type CNAME, class IN
- $data .= "c0 0c"; // answer: offset pointer to mail.google.com
- $data .= "00 05 00 01"; // answer: type CNAME, class IN
- $data .= "00 00 a8 9c"; // answer: ttl 43164
- $data .= "00 0f"; // answer: rdlength 15
- $data .= "0a 67 6f 6f 67 6c 65 6d 61 69 6c 01 6c"; // answer: rdata googlemail.l.
- $data .= "c0 11"; // answer: rdata offset pointer to google.com
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertCount(1, $response->questions);
- $this->assertSame('mail.google.com', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_CNAME, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(1, $response->answers);
- $this->assertSame('mail.google.com', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_CNAME, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(43164, $response->answers[0]->ttl);
- $this->assertSame('googlemail.l.google.com', $response->answers[0]->data);
- }
- public function testParseAAAAResponse()
- {
- $data = "";
- $data .= "cd 72 81 80 00 01 00 01 00 00 00 00 06"; // header
- $data .= "67 6f 6f 67 6c 65 03 63 6f 6d 00"; // question: google.com
- $data .= "00 1c 00 01"; // question: type AAAA, class IN
- $data .= "c0 0c"; // answer: offset pointer to google.com
- $data .= "00 1c 00 01"; // answer: type AAAA, class IN
- $data .= "00 00 01 2b"; // answer: ttl 299
- $data .= "00 10"; // answer: rdlength 16
- $data .= "2a 00 14 50 40 09 08 09 00 00 00 00 00 00 20 0e"; // answer: 2a00:1450:4009:809::200e
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertSame(0xcd72, $response->id);
- $this->assertSame(true, $response->qr);
- $this->assertSame(Message::OPCODE_QUERY, $response->opcode);
- $this->assertSame(false, $response->aa);
- $this->assertSame(false, $response->tc);
- $this->assertSame(true, $response->rd);
- $this->assertSame(true, $response->ra);
- $this->assertSame(Message::RCODE_OK, $response->rcode);
- $this->assertCount(1, $response->questions);
- $this->assertSame('google.com', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_AAAA, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(1, $response->answers);
- $this->assertSame('google.com', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_AAAA, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(299, $response->answers[0]->ttl);
- $this->assertSame('2a00:1450:4009:809::200e', $response->answers[0]->data);
- }
- public function testParseTXTResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 10 00 01"; // answer: type TXT, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 06"; // answer: rdlength 6
- $data .= "05 68 65 6c 6c 6f"; // answer: rdata length 5: hello
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_TXT, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(array('hello'), $response->answers[0]->data);
- }
- public function testParseSPFResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 63 00 01"; // answer: type SPF, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 06"; // answer: rdlength 6
- $data .= "05 68 65 6c 6c 6f"; // answer: rdata length 5: hello
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_SPF, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(array('hello'), $response->answers[0]->data);
- }
- public function testParseTXTResponseMultiple()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 10 00 01"; // answer: type TXT, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 0C"; // answer: rdlength 12
- $data .= "05 68 65 6c 6c 6f 05 77 6f 72 6c 64"; // answer: rdata length 5: hello, length 5: world
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_TXT, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(array('hello', 'world'), $response->answers[0]->data);
- }
- public function testParseMXResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 0f 00 01"; // answer: type MX, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 09"; // answer: rdlength 9
- $data .= "00 0a 05 68 65 6c 6c 6f 00"; // answer: rdata priority 10: hello
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_MX, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(array('priority' => 10, 'target' => 'hello'), $response->answers[0]->data);
- }
- public function testParseSRVResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 21 00 01"; // answer: type SRV, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 0C"; // answer: rdlength 12
- $data .= "00 0a 00 14 1F 90 04 74 65 73 74 00"; // answer: rdata priority 10, weight 20, port 8080 test
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_SRV, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(
- array(
- 'priority' => 10,
- 'weight' => 20,
- 'port' => 8080,
- 'target' => 'test'
- ),
- $response->answers[0]->data
- );
- }
- public function testParseMessageResponseWithTwoAnswers()
- {
- $data = "";
- $data .= "bc 73 81 80 00 01 00 02 00 00 00 00"; // header
- $data .= "02 69 6f 0d 77 68 6f 69 73 2d 73 65 72 76 65 72 73 03 6e 65 74 00";
- // question: io.whois-servers.net
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // answer: offset pointer to io.whois-servers.net
- $data .= "00 05 00 01"; // answer: type CNAME, class IN
- $data .= "00 00 00 29"; // answer: ttl 41
- $data .= "00 0e"; // answer: rdlength 14
- $data .= "05 77 68 6f 69 73 03 6e 69 63 02 69 6f 00"; // answer: rdata whois.nic.io
- $data .= "c0 32"; // answer: offset pointer to whois.nic.io
- $data .= "00 01 00 01"; // answer: type CNAME, class IN
- $data .= "00 00 0d f7"; // answer: ttl 3575
- $data .= "00 04"; // answer: rdlength 4
- $data .= "c1 df 4e 98"; // answer: rdata 193.223.78.152
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertCount(1, $response->questions);
- $this->assertSame('io.whois-servers.net', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_A, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(2, $response->answers);
- $this->assertSame('io.whois-servers.net', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_CNAME, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(41, $response->answers[0]->ttl);
- $this->assertSame('whois.nic.io', $response->answers[0]->data);
- $this->assertSame('whois.nic.io', $response->answers[1]->name);
- $this->assertSame(Message::TYPE_A, $response->answers[1]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[1]->class);
- $this->assertSame(3575, $response->answers[1]->ttl);
- $this->assertSame('193.223.78.152', $response->answers[1]->data);
- }
- public function testParseMessageResponseWithTwoAuthorityRecords()
- {
- $data = "";
- $data .= "bc 73 81 80 00 01 00 00 00 02 00 00"; // header
- $data .= "02 69 6f 0d 77 68 6f 69 73 2d 73 65 72 76 65 72 73 03 6e 65 74 00";
- // question: io.whois-servers.net
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // authority: offset pointer to io.whois-servers.net
- $data .= "00 05 00 01"; // authority: type CNAME, class IN
- $data .= "00 00 00 29"; // authority: ttl 41
- $data .= "00 0e"; // authority: rdlength 14
- $data .= "05 77 68 6f 69 73 03 6e 69 63 02 69 6f 00"; // authority: rdata whois.nic.io
- $data .= "c0 32"; // authority: offset pointer to whois.nic.io
- $data .= "00 01 00 01"; // authority: type CNAME, class IN
- $data .= "00 00 0d f7"; // authority: ttl 3575
- $data .= "00 04"; // authority: rdlength 4
- $data .= "c1 df 4e 98"; // authority: rdata 193.223.78.152
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertCount(1, $response->questions);
- $this->assertSame('io.whois-servers.net', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_A, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(0, $response->answers);
- $this->assertCount(2, $response->authority);
- $this->assertSame('io.whois-servers.net', $response->authority[0]->name);
- $this->assertSame(Message::TYPE_CNAME, $response->authority[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->authority[0]->class);
- $this->assertSame(41, $response->authority[0]->ttl);
- $this->assertSame('whois.nic.io', $response->authority[0]->data);
- $this->assertSame('whois.nic.io', $response->authority[1]->name);
- $this->assertSame(Message::TYPE_A, $response->authority[1]->type);
- $this->assertSame(Message::CLASS_IN, $response->authority[1]->class);
- $this->assertSame(3575, $response->authority[1]->ttl);
- $this->assertSame('193.223.78.152', $response->authority[1]->data);
- }
- public function testParseMessageResponseWithAnswerAndAdditionalRecord()
- {
- $data = "";
- $data .= "bc 73 81 80 00 01 00 01 00 00 00 01"; // header
- $data .= "02 69 6f 0d 77 68 6f 69 73 2d 73 65 72 76 65 72 73 03 6e 65 74 00";
- // question: io.whois-servers.net
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // answer: offset pointer to io.whois-servers.net
- $data .= "00 05 00 01"; // answer: type CNAME, class IN
- $data .= "00 00 00 29"; // answer: ttl 41
- $data .= "00 0e"; // answer: rdlength 14
- $data .= "05 77 68 6f 69 73 03 6e 69 63 02 69 6f 00"; // answer: rdata whois.nic.io
- $data .= "c0 32"; // additional: offset pointer to whois.nic.io
- $data .= "00 01 00 01"; // additional: type CNAME, class IN
- $data .= "00 00 0d f7"; // additional: ttl 3575
- $data .= "00 04"; // additional: rdlength 4
- $data .= "c1 df 4e 98"; // additional: rdata 193.223.78.152
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertCount(1, $response->questions);
- $this->assertSame('io.whois-servers.net', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_A, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(1, $response->answers);
- $this->assertSame('io.whois-servers.net', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_CNAME, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(41, $response->answers[0]->ttl);
- $this->assertSame('whois.nic.io', $response->answers[0]->data);
- $this->assertCount(0, $response->authority);
- $this->assertCount(1, $response->additional);
- $this->assertSame('whois.nic.io', $response->additional[0]->name);
- $this->assertSame(Message::TYPE_A, $response->additional[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->additional[0]->class);
- $this->assertSame(3575, $response->additional[0]->ttl);
- $this->assertSame('193.223.78.152', $response->additional[0]->data);
- }
- public function testParseNSResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 02 00 01"; // answer: type NS, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 07"; // answer: rdlength 7
- $data .= "05 68 65 6c 6c 6f 00"; // answer: rdata hello
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_NS, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame('hello', $response->answers[0]->data);
- }
- public function testParseSSHFPResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 2c 00 01"; // answer: type SSHFP, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 06"; // answer: rdlength 6
- $data .= "01 01 69 ac 09 0c"; // answer: algorithm 1 (RSA), type 1 (SHA-1), fingerprint "69ac090c"
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_SSHFP, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(array('algorithm' => 1, 'type' => 1, 'fingerprint' => '69ac090c'), $response->answers[0]->data);
- }
- public function testParseOptResponseWithoutOptions()
- {
- $data = "";
- $data .= "00"; // answer: empty domain
- $data .= "00 29 03 e8"; // answer: type OPT, class 1000 UDP size
- $data .= "00 00 00 00"; // answer: ttl 0
- $data .= "00 00"; // answer: rdlength 0
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_OPT, $response->answers[0]->type);
- $this->assertSame(1000, $response->answers[0]->class);
- $this->assertSame(0, $response->answers[0]->ttl);
- $this->assertSame(array(), $response->answers[0]->data);
- }
- public function testParseOptResponseWithOptTcpKeepaliveDesired()
- {
- $data = "";
- $data .= "00"; // answer: empty domain
- $data .= "00 29 03 e8"; // answer: type OPT, class 1000 UDP size
- $data .= "00 00 00 00"; // answer: ttl 0
- $data .= "00 04"; // answer: rdlength 4
- $data .= "00 0b 00 00"; // OPT_TCP_KEEPALIVE=null encoded
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_OPT, $response->answers[0]->type);
- $this->assertSame(1000, $response->answers[0]->class);
- $this->assertSame(0, $response->answers[0]->ttl);
- $this->assertSame(array(Message::OPT_TCP_KEEPALIVE => null), $response->answers[0]->data);
- }
- public function testParseOptResponseWithOptTcpKeepaliveGiven()
- {
- $data = "";
- $data .= "00"; // answer: empty domain
- $data .= "00 29 03 e8"; // answer: type OPT, class 1000 UDP size
- $data .= "00 00 00 00"; // answer: ttl 0
- $data .= "00 06"; // answer: rdlength 4
- $data .= "00 0b 00 02 00 0c"; // OPT_TCP_KEEPALIVE=1.2s encoded
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_OPT, $response->answers[0]->type);
- $this->assertSame(1000, $response->answers[0]->class);
- $this->assertSame(0, $response->answers[0]->ttl);
- $this->assertSame(array(Message::OPT_TCP_KEEPALIVE => 1.2), $response->answers[0]->data);
- }
- public function testParseOptResponseWithCustomOptions()
- {
- $data = "";
- $data .= "00"; // answer: empty domain
- $data .= "00 29 03 e8"; // answer: type OPT, class 1000 UDP size
- $data .= "00 00 00 00"; // answer: ttl 0
- $data .= "00 0b"; // answer: rdlength 11
- $data .= "00 a0 00 03 66 6f 6f"; // OPT code 0xa0 encoded
- $data .= "00 01 00 00 "; // OPT code 0x01 encoded
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_OPT, $response->answers[0]->type);
- $this->assertSame(1000, $response->answers[0]->class);
- $this->assertSame(0, $response->answers[0]->ttl);
- $this->assertSame(array(0xa0 => 'foo', 0x01 => ''), $response->answers[0]->data);
- }
- public function testParseSOAResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 06 00 01"; // answer: type SOA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 27"; // answer: rdlength 39
- $data .= "02 6e 73 05 68 65 6c 6c 6f 00"; // answer: rdata ns.hello (mname)
- $data .= "01 65 05 68 65 6c 6c 6f 00"; // answer: rdata e.hello (rname)
- $data .= "78 49 28 D5 00 00 2a 30 00 00 0e 10"; // answer: rdata 2018060501, 10800, 3600
- $data .= "00 09 3a 80 00 00 0e 10"; // answer: 605800, 3600
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_SOA, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(
- array(
- 'mname' => 'ns.hello',
- 'rname' => 'e.hello',
- 'serial' => 2018060501,
- 'refresh' => 10800,
- 'retry' => 3600,
- 'expire' => 604800,
- 'minimum' => 3600
- ),
- $response->answers[0]->data
- );
- }
- public function testParseCAAResponse()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "01 01 00 01"; // answer: type CAA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 16"; // answer: rdlength 22
- $data .= "00 05 69 73 73 75 65"; // answer: rdata 0, issue
- $data .= "6c 65 74 73 65 6e 63 72 79 70 74 2e 6f 72 67"; // answer: letsencrypt.org
- $response = $this->parseAnswer($data);
- $this->assertCount(1, $response->answers);
- $this->assertSame('igor.io', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_CAA, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86400, $response->answers[0]->ttl);
- $this->assertSame(array('flag' => 0, 'tag' => 'issue', 'value' => 'letsencrypt.org'), $response->answers[0]->data);
- }
- public function testParsePTRResponse()
- {
- $data = "";
- $data .= "5d d8 81 80 00 01 00 01 00 00 00 00"; // header
- $data .= "01 34 01 34 01 38 01 38 07 69 6e"; // question: 4.4.8.8.in-addr.arpa
- $data .= "2d 61 64 64 72 04 61 72 70 61 00"; // question (continued)
- $data .= "00 0c 00 01"; // question: type PTR, class IN
- $data .= "c0 0c"; // answer: offset pointer to rdata
- $data .= "00 0c 00 01"; // answer: type PTR, class IN
- $data .= "00 01 51 7f"; // answer: ttl 86399
- $data .= "00 20"; // answer: rdlength 32
- $data .= "13 67 6f 6f 67 6c 65 2d 70 75 62 6c 69 63 2d 64"; // answer: rdata google-public-dns-b.google.com.
- $data .= "6e 73 2d 62 06 67 6f 6f 67 6c 65 03 63 6f 6d 00";
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertSame(0x5dd8, $response->id);
- $this->assertSame(true, $response->qr);
- $this->assertSame(Message::OPCODE_QUERY, $response->opcode);
- $this->assertSame(false, $response->aa);
- $this->assertSame(false, $response->tc);
- $this->assertSame(true, $response->rd);
- $this->assertSame(true, $response->ra);
- $this->assertSame(Message::RCODE_OK, $response->rcode);
- $this->assertCount(1, $response->questions);
- $this->assertSame('4.4.8.8.in-addr.arpa', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_PTR, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(1, $response->answers);
- $this->assertSame('4.4.8.8.in-addr.arpa', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_PTR, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86399, $response->answers[0]->ttl);
- $this->assertSame('google-public-dns-b.google.com', $response->answers[0]->data);
- }
- public function testParsePTRResponseWithSpecialCharactersEscaped()
- {
- $data = "";
- $data .= "5d d8 81 80 00 01 00 01 00 00 00 00"; // header
- $data .= "08 5f 70 72 69 6e 74 65 72 04 5f 74 63 70 06 64 6e 73 2d 73 64 03 6f 72 67 00"; // question: _printer._tcp.dns-sd.org
- $data .= "00 0c 00 01"; // question: type PTR, class IN
- $data .= "c0 0c"; // answer: offset pointer to rdata
- $data .= "00 0c 00 01"; // answer: type PTR, class IN
- $data .= "00 01 51 7f"; // answer: ttl 86399
- $data .= "00 17"; // answer: rdlength 23
- $data .= "14 33 72 64 2e 20 46 6c 6f 6f 72 20 43 6f 70 79 20 52 6f 6f 6d"; // answer: rdata "3rd. Floor Copy Room" …
- $data .= "c0 0c"; // answer: offset pointer to rdata
- $data = $this->convertTcpDumpToBinary($data);
- $response = $this->parser->parseMessage($data);
- $this->assertCount(1, $response->questions);
- $this->assertSame('_printer._tcp.dns-sd.org', $response->questions[0]->name);
- $this->assertSame(Message::TYPE_PTR, $response->questions[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->questions[0]->class);
- $this->assertCount(1, $response->answers);
- $this->assertSame('_printer._tcp.dns-sd.org', $response->answers[0]->name);
- $this->assertSame(Message::TYPE_PTR, $response->answers[0]->type);
- $this->assertSame(Message::CLASS_IN, $response->answers[0]->class);
- $this->assertSame(86399, $response->answers[0]->ttl);
- $this->assertSame('3rd\.\ Floor\ Copy\ Room._printer._tcp.dns-sd.org', $response->answers[0]->data);
- }
- public function testParseIncompleteQuestionThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- //$data .= "00 01 00 01"; // question: type A, class IN
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseIncompleteQuestionLabelThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "04 69 67"; // question: ig …?
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseIncompleteQuestionNameThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "04 69 67 6f 72"; // question: igor. …?
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseIncompleteOffsetPointerInQuestionNameThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "ff"; // question: incomplete offset pointer
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseInvalidOffsetPointerInQuestionNameThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "ff ff"; // question: offset pointer to invalid address
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseInvalidOffsetPointerToSameLabelInQuestionNameThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "c0 0c"; // question: offset pointer to invalid address
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseInvalidOffsetPointerToPreviousLabelInQuestionNameThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "02 69 6f c0 0c"; // question: offset pointer to previous label
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseInvalidOffsetPointerToStartOfMessageInQuestionNameThrows()
- {
- $data = "";
- $data .= "72 62 01 00 00 01 00 00 00 00 00 00"; // header
- $data .= "c0 00"; // question: offset pointer to start of message
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseIncompleteAnswerFieldsThrows()
- {
- $data = "";
- $data .= "72 62 81 80 00 01 00 01 00 00 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // answer: offset pointer to igor.io
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseMessageResponseWithIncompleteAuthorityRecordThrows()
- {
- $data = "";
- $data .= "72 62 81 80 00 01 00 00 00 01 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // authority: offset pointer to igor.io
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseMessageResponseWithIncompleteAdditionalRecordThrows()
- {
- $data = "";
- $data .= "72 62 81 80 00 01 00 00 00 00 00 01"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // additional: offset pointer to igor.io
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseIncompleteAnswerRecordDataThrows()
- {
- $data = "";
- $data .= "72 62 81 80 00 01 00 01 00 00 00 00"; // header
- $data .= "04 69 67 6f 72 02 69 6f 00"; // question: igor.io
- $data .= "00 01 00 01"; // question: type A, class IN
- $data .= "c0 0c"; // answer: offset pointer to igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 04"; // answer: rdlength 4
- $data = $this->convertTcpDumpToBinary($data);
- $this->setExpectedException('InvalidArgumentException');
- $this->parser->parseMessage($data);
- }
- public function testParseInvalidNSResponseWhereDomainNameIsMissing()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 02 00 01"; // answer: type NS, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 00"; // answer: rdlength 0
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidAResponseWhereIPIsMissing()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 01 00 01"; // answer: type A, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 00"; // answer: rdlength 0
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidAAAAResponseWhereIPIsMissing()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 1c 00 01"; // answer: type AAAA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 00"; // answer: rdlength 0
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidTXTResponseWhereTxtChunkExceedsLimit()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 10 00 01"; // answer: type TXT, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 06"; // answer: rdlength 6
- $data .= "06 68 65 6c 6c 6f 6f"; // answer: rdata length 6: helloo
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidMXResponseWhereDomainNameIsIncomplete()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 0f 00 01"; // answer: type MX, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 08"; // answer: rdlength 8
- $data .= "00 0a 05 68 65 6c 6c 6f"; // answer: rdata priority 10: hello (missing label end)
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidMXResponseWhereDomainNameIsMissing()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 0f 00 01"; // answer: type MX, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 02"; // answer: rdlength 2
- $data .= "00 0a"; // answer: rdata priority 10
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidSRVResponseWhereDomainNameIsIncomplete()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 21 00 01"; // answer: type SRV, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 0b"; // answer: rdlength 11
- $data .= "00 0a 00 14 1F 90 04 74 65 73 74"; // answer: rdata priority 10, weight 20, port 8080 test (missing label end)
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidSRVResponseWhereDomainNameIsMissing()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 21 00 01"; // answer: type SRV, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 06"; // answer: rdlength 6
- $data .= "00 0a 00 14 1F 90"; // answer: rdata priority 10, weight 20, port 8080
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidSSHFPResponseWhereRecordIsTooSmall()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 2c 00 01"; // answer: type SSHFP, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 02"; // answer: rdlength 2
- $data .= "01 01"; // answer: algorithm 1 (RSA), type 1 (SHA), missing fingerprint
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidOPTResponseWhereRecordIsTooSmall()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 29 03 e8"; // answer: type OPT, 1000 bytes max size
- $data .= "00 00 00 00"; // answer: ttl 0
- $data .= "00 03"; // answer: rdlength 3
- $data .= "00 00 00"; // answer: type 0, length incomplete
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidOPTResponseWhereRecordLengthDoesNotMatchOptType()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 29 03 e8"; // answer: type OPT, 1000 bytes max size
- $data .= "00 00 00 00"; // answer: ttl 0
- $data .= "00 07"; // answer: rdlength 7
- $data .= "00 0b 00 03 01 02 03"; // answer: type OPT_TCP_KEEPALIVE, length 3 instead of 2
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidSOAResponseWhereFlagsAreMissing()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "00 06 00 01"; // answer: type SOA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 13"; // answer: rdlength 19
- $data .= "02 6e 73 05 68 65 6c 6c 6f 00"; // answer: rdata ns.hello (mname)
- $data .= "01 65 05 68 65 6c 6c 6f 00"; // answer: rdata e.hello (rname)
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidCAAResponseEmtpyData()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "01 01 00 01"; // answer: type CAA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 00"; // answer: rdlength 0
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidCAAResponseMissingValue()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "01 01 00 01"; // answer: type CAA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 07"; // answer: rdlength 22
- $data .= "00 05 69 73 73 75 65"; // answer: rdata 0, issue
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- public function testParseInvalidCAAResponseIncompleteTag()
- {
- $data = "";
- $data .= "04 69 67 6f 72 02 69 6f 00"; // answer: igor.io
- $data .= "01 01 00 01"; // answer: type CAA, class IN
- $data .= "00 01 51 80"; // answer: ttl 86400
- $data .= "00 0c"; // answer: rdlength 22
- $data .= "00 ff 69 73 73 75 65"; // answer: rdata 0, issue (incomplete due to invalid tag length)
- $data .= "68 65 6c 6c 6f"; // answer: hello
- $this->setExpectedException('InvalidArgumentException');
- $this->parseAnswer($data);
- }
- private function convertTcpDumpToBinary($input)
- {
- // sudo ngrep -d en1 -x port 53
- return pack('H*', str_replace(' ', '', $input));
- }
- private function parseAnswer($answerData)
- {
- $data = "72 62 81 80 00 00 00 01 00 00 00 00"; // header with one answer only
- $data .= $answerData;
- $data = $this->convertTcpDumpToBinary($data);
- return $this->parser->parseMessage($data);
- }
- }
|