]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/tests/testRFC1035.cc
e238ec097f5f90c7cac5b3bbf28c91be0c5097ff
[thirdparty/squid.git] / lib / tests / testRFC1035.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
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.
7 */
8
9 #include "squid.h"
10 #include "testRFC1035.h"
11 #include "unitTestMain.h"
12
13 #include <cassert>
14
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"
18
19 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
20
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.
24
25 void testRFC1035::testHeaderUnpack()
26 {
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";
29 size_t len = 126;
30 rfc1035_message *msg = NULL;
31 int res = 0;
32 unsigned int off = 0;
33
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);
40 /* flags */
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);
47 /* RR counts */
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);
52
53 /* cleanup */
54 delete msg;
55 msg = NULL;
56 }
57
58 void testRFC1035::testParseAPacket()
59 {
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";
62 size_t len = 126;
63 rfc1035_message *msg = NULL;
64 int res = 0;
65
66 /* Test the MessageUnpack function itself */
67 res = rfc1035MessageUnpack(buf, len, &msg);
68
69 CPPUNIT_ASSERT_EQUAL(1, res);
70 CPPUNIT_ASSERT(msg != NULL);
71 /* cleanup */
72 rfc1035MessageDestroy(&msg);
73 CPPUNIT_ASSERT(msg == NULL);
74 }
75
76 void testRFC1035::testBugPacketEndingOnCompressionPtr()
77 {
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";
80 size_t len = 59;
81 rfc1035_message *msg = NULL;
82 int res = 0;
83 unsigned int off = 0;
84
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);
91 /* flags */
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);
98 /* RR counts */
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");
105 /* cleanup */
106 delete msg;
107 msg = NULL;
108
109 // TODO explicitly test RR and Name unpack functions for this packet.
110
111 /* Test the MessageUnpack function itself */
112 res = rfc1035MessageUnpack(buf, len, &msg);
113
114 CPPUNIT_ASSERT_EQUAL(1, res);
115 CPPUNIT_ASSERT(msg != NULL);
116 rfc1035MessageDestroy(&msg);
117 }
118
119 void testRFC1035::testBugPacketHeadersOnly()
120 {
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";
123 size_t len = 12;
124 rfc1035_message *msg = NULL;
125 int res = 0;
126 unsigned int off = 0;
127
128 /* Test the HeaderUnpack function results */
129 msg = new rfc1035_message;
130 res = rfc1035HeaderUnpack(buf, len, &off, msg);
131 CPPUNIT_ASSERT(0 == res);
132 /* cleanup */
133 delete msg;
134 msg = NULL;
135
136 /* Test the MessageUnpack function itself */
137 res = rfc1035MessageUnpack(buf, len, &msg);
138
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);
142 }
143