]> git.ipfire.org Git - thirdparty/squid.git/blob - src/tests/testRFC1035.cc
Merged from trunk
[thirdparty/squid.git] / src / 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 "dns/rfc1035.h"
11 #include "testRFC1035.h"
12 #include "unitTestMain.h"
13
14 #include <cassert>
15
16 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
17
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.
21
22 void testRFC1035::testHeaderUnpack()
23 {
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";
26 size_t len = 126;
27 rfc1035_message *msg = NULL;
28 int res = 0;
29 unsigned int off = 0;
30
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);
37 /* flags */
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);
44 /* RR counts */
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);
49
50 /* cleanup */
51 delete msg;
52 msg = NULL;
53 }
54
55 void testRFC1035::testParseAPacket()
56 {
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";
59 size_t len = 126;
60 rfc1035_message *msg = NULL;
61 int res = 0;
62
63 /* Test the MessageUnpack function itself */
64 res = rfc1035MessageUnpack(buf, len, &msg);
65
66 CPPUNIT_ASSERT_EQUAL(1, res);
67 CPPUNIT_ASSERT(msg != NULL);
68 /* cleanup */
69 rfc1035MessageDestroy(&msg);
70 CPPUNIT_ASSERT(msg == NULL);
71 }
72
73 void testRFC1035::testBugPacketEndingOnCompressionPtr()
74 {
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";
77 size_t len = 59;
78 rfc1035_message *msg = NULL;
79 int res = 0;
80 unsigned int off = 0;
81
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);
88 /* flags */
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);
95 /* RR counts */
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");
102 /* cleanup */
103 delete msg;
104 msg = NULL;
105
106 // TODO explicitly test RR and Name unpack functions for this packet.
107
108 /* Test the MessageUnpack function itself */
109 res = rfc1035MessageUnpack(buf, len, &msg);
110
111 CPPUNIT_ASSERT_EQUAL(1, res);
112 CPPUNIT_ASSERT(msg != NULL);
113 rfc1035MessageDestroy(&msg);
114 }
115
116 void testRFC1035::testBugPacketHeadersOnly()
117 {
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";
120 size_t len = 12;
121 rfc1035_message *msg = NULL;
122 int res = 0;
123 unsigned int off = 0;
124
125 /* Test the HeaderUnpack function results */
126 msg = new rfc1035_message;
127 res = rfc1035HeaderUnpack(buf, len, &off, msg);
128 CPPUNIT_ASSERT(0 == res);
129 /* cleanup */
130 delete msg;
131 msg = NULL;
132
133 /* Test the MessageUnpack function itself */
134 res = rfc1035MessageUnpack(buf, len, &msg);
135
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 == NULL);
139 }
140