]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/tests/testRFC1035.cc
1 #define SQUID_UNIT_TEST 1
8 #include "testRFC1035.h"
10 /* Being a C library code it is best bodily included and tested with C++ type-safe techniques. */
11 #include "lib/rfc1035.c"
12 #include "lib/rfc2671.c"
14 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035
);
16 // TODO Test each function in the Library independently
17 // Just because we can for global functions.
18 // It's good for the code too.
20 void testRFC1035::testHeaderUnpack()
22 /* Setup a buffer with the known-content packet */
23 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";
25 rfc1035_message
*msg
= NULL
;
29 /* Test the HeaderUnpack function */
30 msg
= new rfc1035_message
;
31 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
32 CPPUNIT_ASSERT(res
== 0);
33 CPPUNIT_ASSERT_EQUAL((short unsigned int)0x76b1, msg
->id
);
34 CPPUNIT_ASSERT(msg
->qr
== 1);
36 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->opcode
);
37 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->aa
);
38 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->tc
);
39 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg
->rd
);
40 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg
->ra
);
41 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->rcode
);
43 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg
->qdcount
);
44 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg
->ancount
);
45 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg
->nscount
);
46 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg
->arcount
);
53 void testRFC1035::testParseAPacket()
55 /* Setup a buffer with the known-content packet */
56 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";
58 rfc1035_message
*msg
= NULL
;
61 /* Test the MessageUnpack function itself */
62 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
64 CPPUNIT_ASSERT_EQUAL(1, res
);
65 CPPUNIT_ASSERT(msg
!= NULL
);
67 rfc1035MessageDestroy(&msg
);
68 CPPUNIT_ASSERT(msg
== NULL
);
71 void testRFC1035::testBugPacketEndingOnCompressionPtr()
73 /* Setup a buffer with the known-to-fail packet */
74 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";
76 rfc1035_message
*msg
= NULL
;
80 /* Test the HeaderUnpack function results */
81 msg
= new rfc1035_message
;
82 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
83 CPPUNIT_ASSERT(0 == res
);
84 CPPUNIT_ASSERT(0xec7b == msg
->id
);
85 CPPUNIT_ASSERT(1 == msg
->qr
);
87 CPPUNIT_ASSERT(0 == msg
->opcode
);
88 CPPUNIT_ASSERT(0 == msg
->aa
);
89 CPPUNIT_ASSERT(0 == msg
->tc
);
90 CPPUNIT_ASSERT(1 == msg
->rd
);
91 CPPUNIT_ASSERT(1 == msg
->ra
);
92 CPPUNIT_ASSERT(0 == msg
->rcode
);
94 CPPUNIT_ASSERT(1 == msg
->qdcount
);
95 CPPUNIT_ASSERT(1 == msg
->ancount
);
96 CPPUNIT_ASSERT(0 == msg
->nscount
);
97 CPPUNIT_ASSERT(0 == msg
->arcount
);
98 CPPUNIT_ASSERT(12 == off
);
99 printf("\n Header : OK");
104 // TODO explicitly test RR and Name unpack functions for this packet.
106 /* Test the MessageUnpack function itself */
107 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
109 CPPUNIT_ASSERT_EQUAL(1, res
);
110 CPPUNIT_ASSERT(msg
!= NULL
);
111 rfc1035MessageDestroy(&msg
);
114 void testRFC1035::testBugPacketHeadersOnly()
116 /* Setup a buffer with the known-to-fail headers-only packet */
117 const char *buf
= "\xab\xcd\x81\x80\x00\x01\x00\x05\x00\x04\x00\x04";
119 rfc1035_message
*msg
= NULL
;
121 unsigned int off
= 0;
123 /* Test the HeaderUnpack function results */
124 msg
= new rfc1035_message
;
125 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
126 CPPUNIT_ASSERT(0 == res
);
131 /* Test the MessageUnpack function itself */
132 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
134 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res
), 63));
135 CPPUNIT_ASSERT(res
< 0);
136 CPPUNIT_ASSERT(msg
== NULL
);