]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRFC1035.cc
2 * Copyright (C) 1996-2022 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "dns/rfc1035.h"
11 #include "testRFC1035.h"
12 #include "unitTestMain.h"
16 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035
);
18 // TODO Test each function in the Library independently
19 // Just because we can for global functions.
20 // It's good for the code too.
22 void testRFC1035::testHeaderUnpack()
24 /* Setup a buffer with the known-content packet */
25 const char *buf
= "\x76\xb1\x81\x80\x00\x01\x00\x01\x00\x02\x00\x02\x03\x77\x77\x77\x07\x67\x61\x6d\x65\x64\x65\x76\x03\x6e\x65\x74\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\xef\x00\x04\xd8\xb9\x60\xea\xc0\x10\x00\x02\x00\x01\x00\x00\x00\xef\x00\x0f\x03\x6e\x73\x32\x05\x7a\x77\x61\x76\x65\x03\x63\x6f\x6d\x00\xc0\x10\x00\x02\x00\x01\x00\x00\x00\xef\x00\x06\x03\x6e\x73\x31\xc0\x41\xc0\x3d\x00\x01\x00\x01\x00\x00\x29\x6b\x00\x04\xd8\xea\xee\x4a\xc0\x58\x00\x01\x00\x01\x00\x00\x29\x6b\x00\x04\xd8\xea\xee\x4b";
27 rfc1035_message
*msg
= nullptr;
31 /* Test the HeaderUnpack function */
32 msg
= new rfc1035_message
;
33 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
34 CPPUNIT_ASSERT(res
== 0);
35 CPPUNIT_ASSERT_EQUAL((short unsigned int)0x76b1, msg
->id
);
36 CPPUNIT_ASSERT(msg
->qr
== 1);
38 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->opcode
);
39 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->aa
);
40 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->tc
);
41 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg
->rd
);
42 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg
->ra
);
43 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->rcode
);
45 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg
->qdcount
);
46 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg
->ancount
);
47 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg
->nscount
);
48 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg
->arcount
);
55 void testRFC1035::testParseAPacket()
57 /* Setup a buffer with the known-content packet */
58 const char *buf
= "\x76\xb1\x81\x80\x00\x01\x00\x01\x00\x02\x00\x02\x03\x77\x77\x77\x07\x67\x61\x6d\x65\x64\x65\x76\x03\x6e\x65\x74\x00\x00\x01\x00\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\xef\x00\x04\xd8\xb9\x60\xea\xc0\x10\x00\x02\x00\x01\x00\x00\x00\xef\x00\x0f\x03\x6e\x73\x32\x05\x7a\x77\x61\x76\x65\x03\x63\x6f\x6d\x00\xc0\x10\x00\x02\x00\x01\x00\x00\x00\xef\x00\x06\x03\x6e\x73\x31\xc0\x41\xc0\x3d\x00\x01\x00\x01\x00\x00\x29\x6b\x00\x04\xd8\xea\xee\x4a\xc0\x58\x00\x01\x00\x01\x00\x00\x29\x6b\x00\x04\xd8\xea\xee\x4b";
60 rfc1035_message
*msg
= nullptr;
63 /* Test the MessageUnpack function itself */
64 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
66 CPPUNIT_ASSERT_EQUAL(1, res
);
67 CPPUNIT_ASSERT(msg
!= nullptr);
69 rfc1035MessageDestroy(&msg
);
70 CPPUNIT_ASSERT(msg
== nullptr);
73 void testRFC1035::testBugPacketEndingOnCompressionPtr()
75 /* Setup a buffer with the known-to-fail packet */
76 const char *buf
= "\xec\x7b\x81\x80\x00\x01\x00\x01\x00\x00\x00\x00\x05\x62\x75\x72\x73\x74\x02\x74\x65\x06\x74\x61\x63\x6f\x64\x61\x03\x6e\x65\x74\x00\x00\x1c\x00\x01\xc0\x0c\x00\x05\x00\x01\x00\x00\x19\xe5\x00\x0a\x02\x74\x65\x04\x67\x73\x6c\x62\xc0\x15";
78 rfc1035_message
*msg
= nullptr;
82 /* Test the HeaderUnpack function results */
83 msg
= new rfc1035_message
;
84 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
85 CPPUNIT_ASSERT(0 == res
);
86 CPPUNIT_ASSERT(0xec7b == msg
->id
);
87 CPPUNIT_ASSERT(1 == msg
->qr
);
89 CPPUNIT_ASSERT(0 == msg
->opcode
);
90 CPPUNIT_ASSERT(0 == msg
->aa
);
91 CPPUNIT_ASSERT(0 == msg
->tc
);
92 CPPUNIT_ASSERT(1 == msg
->rd
);
93 CPPUNIT_ASSERT(1 == msg
->ra
);
94 CPPUNIT_ASSERT(0 == msg
->rcode
);
96 CPPUNIT_ASSERT(1 == msg
->qdcount
);
97 CPPUNIT_ASSERT(1 == msg
->ancount
);
98 CPPUNIT_ASSERT(0 == msg
->nscount
);
99 CPPUNIT_ASSERT(0 == msg
->arcount
);
100 CPPUNIT_ASSERT(12 == off
);
101 printf("\n Header : OK");
106 // TODO explicitly test RR and Name unpack functions for this packet.
108 /* Test the MessageUnpack function itself */
109 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
111 CPPUNIT_ASSERT_EQUAL(1, res
);
112 CPPUNIT_ASSERT(msg
!= nullptr);
113 rfc1035MessageDestroy(&msg
);
116 void testRFC1035::testBugPacketHeadersOnly()
118 /* Setup a buffer with the known-to-fail headers-only packet */
119 const char *buf
= "\xab\xcd\x81\x80\x00\x01\x00\x05\x00\x04\x00\x04";
121 rfc1035_message
*msg
= nullptr;
123 unsigned int off
= 0;
125 /* Test the HeaderUnpack function results */
126 msg
= new rfc1035_message
;
127 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
128 CPPUNIT_ASSERT(0 == res
);
133 /* Test the MessageUnpack function itself */
134 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
136 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res
), 63));
137 CPPUNIT_ASSERT(res
< 0);
138 CPPUNIT_ASSERT(msg
== nullptr);