]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/tests/testRFC1035.cc
SourceFormat Enforcement
[thirdparty/squid.git] / lib / tests / testRFC1035.cc
1 #define SQUID_UNIT_TEST 1
2 #include "squid.h"
3
4 #if HAVE_ASSERT_H
5 #include <assert.h>
6 #endif
7
8 #include "testRFC1035.h"
9
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"
13
14 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
15
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.
19
20 void testRFC1035::testHeaderUnpack()
21 {
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";
24 size_t len = 126;
25 rfc1035_message *msg = NULL;
26 int res = 0;
27 unsigned int off = 0;
28
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);
35 /* flags */
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);
42 /* RR counts */
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);
47
48 /* cleanup */
49 delete msg;
50 msg = NULL;
51 }
52
53 void testRFC1035::testParseAPacket()
54 {
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";
57 size_t len = 126;
58 rfc1035_message *msg = NULL;
59 int res = 0;
60
61 /* Test the MessageUnpack function itself */
62 res = rfc1035MessageUnpack(buf, len, &msg);
63
64 CPPUNIT_ASSERT_EQUAL(1, res);
65 CPPUNIT_ASSERT(msg != NULL);
66 /* cleanup */
67 rfc1035MessageDestroy(&msg);
68 CPPUNIT_ASSERT(msg == NULL);
69 }
70
71 void testRFC1035::testBugPacketEndingOnCompressionPtr()
72 {
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";
75 size_t len = 59;
76 rfc1035_message *msg = NULL;
77 int res = 0;
78 unsigned int off = 0;
79
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);
86 /* flags */
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);
93 /* RR counts */
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");
100 /* cleanup */
101 delete msg;
102 msg = NULL;
103
104 // TODO explicitly test RR and Name unpack functions for this packet.
105
106 /* Test the MessageUnpack function itself */
107 res = rfc1035MessageUnpack(buf, len, &msg);
108
109 CPPUNIT_ASSERT_EQUAL(1, res);
110 CPPUNIT_ASSERT(msg != NULL);
111 rfc1035MessageDestroy(&msg);
112 }
113
114 void testRFC1035::testBugPacketHeadersOnly()
115 {
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";
118 size_t len = 12;
119 rfc1035_message *msg = NULL;
120 int res = 0;
121 unsigned int off = 0;
122
123 /* Test the HeaderUnpack function results */
124 msg = new rfc1035_message;
125 res = rfc1035HeaderUnpack(buf, len, &off, msg);
126 CPPUNIT_ASSERT(0 == res);
127 /* cleanup */
128 delete msg;
129 msg = NULL;
130
131 /* Test the MessageUnpack function itself */
132 res = rfc1035MessageUnpack(buf, len, &msg);
133
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);
137 }