]>
git.ipfire.org Git - thirdparty/squid.git/blob - lib/tests/testRFC1035.cc
e238ec097f5f90c7cac5b3bbf28c91be0c5097ff
2 * Copyright (C) 1996-2015 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 "testRFC1035.h"
11 #include "unitTestMain.h"
15 /* Being a C library code it is best bodily included and tested with C++ type-safe techniques. */
16 #include "lib/rfc1035.c"
17 #include "lib/rfc2671.c"
19 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035
);
21 // TODO Test each function in the Library independently
22 // Just because we can for global functions.
23 // It's good for the code too.
25 void testRFC1035::testHeaderUnpack()
27 /* Setup a buffer with the known-content packet */
28 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";
30 rfc1035_message
*msg
= NULL
;
34 /* Test the HeaderUnpack function */
35 msg
= new rfc1035_message
;
36 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
37 CPPUNIT_ASSERT(res
== 0);
38 CPPUNIT_ASSERT_EQUAL((short unsigned int)0x76b1, msg
->id
);
39 CPPUNIT_ASSERT(msg
->qr
== 1);
41 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->opcode
);
42 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->aa
);
43 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->tc
);
44 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg
->rd
);
45 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg
->ra
);
46 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg
->rcode
);
48 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg
->qdcount
);
49 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg
->ancount
);
50 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg
->nscount
);
51 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg
->arcount
);
58 void testRFC1035::testParseAPacket()
60 /* Setup a buffer with the known-content packet */
61 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";
63 rfc1035_message
*msg
= NULL
;
66 /* Test the MessageUnpack function itself */
67 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
69 CPPUNIT_ASSERT_EQUAL(1, res
);
70 CPPUNIT_ASSERT(msg
!= NULL
);
72 rfc1035MessageDestroy(&msg
);
73 CPPUNIT_ASSERT(msg
== NULL
);
76 void testRFC1035::testBugPacketEndingOnCompressionPtr()
78 /* Setup a buffer with the known-to-fail packet */
79 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";
81 rfc1035_message
*msg
= NULL
;
85 /* Test the HeaderUnpack function results */
86 msg
= new rfc1035_message
;
87 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
88 CPPUNIT_ASSERT(0 == res
);
89 CPPUNIT_ASSERT(0xec7b == msg
->id
);
90 CPPUNIT_ASSERT(1 == msg
->qr
);
92 CPPUNIT_ASSERT(0 == msg
->opcode
);
93 CPPUNIT_ASSERT(0 == msg
->aa
);
94 CPPUNIT_ASSERT(0 == msg
->tc
);
95 CPPUNIT_ASSERT(1 == msg
->rd
);
96 CPPUNIT_ASSERT(1 == msg
->ra
);
97 CPPUNIT_ASSERT(0 == msg
->rcode
);
99 CPPUNIT_ASSERT(1 == msg
->qdcount
);
100 CPPUNIT_ASSERT(1 == msg
->ancount
);
101 CPPUNIT_ASSERT(0 == msg
->nscount
);
102 CPPUNIT_ASSERT(0 == msg
->arcount
);
103 CPPUNIT_ASSERT(12 == off
);
104 printf("\n Header : OK");
109 // TODO explicitly test RR and Name unpack functions for this packet.
111 /* Test the MessageUnpack function itself */
112 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
114 CPPUNIT_ASSERT_EQUAL(1, res
);
115 CPPUNIT_ASSERT(msg
!= NULL
);
116 rfc1035MessageDestroy(&msg
);
119 void testRFC1035::testBugPacketHeadersOnly()
121 /* Setup a buffer with the known-to-fail headers-only packet */
122 const char *buf
= "\xab\xcd\x81\x80\x00\x01\x00\x05\x00\x04\x00\x04";
124 rfc1035_message
*msg
= NULL
;
126 unsigned int off
= 0;
128 /* Test the HeaderUnpack function results */
129 msg
= new rfc1035_message
;
130 res
= rfc1035HeaderUnpack(buf
, len
, &off
, msg
);
131 CPPUNIT_ASSERT(0 == res
);
136 /* Test the MessageUnpack function itself */
137 res
= rfc1035MessageUnpack(buf
, len
, &msg
);
139 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res
), 63));
140 CPPUNIT_ASSERT(res
< 0);
141 CPPUNIT_ASSERT(msg
== NULL
);