]> git.ipfire.org Git - thirdparty/squid.git/blob - lib/tests/testRFC1035.cc
Boilerplate: update copyright blurbs on libmisc* libraries
[thirdparty/squid.git] / lib / tests / testRFC1035.cc
1 /*
2 * Copyright (C) 1996-2014 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
12 #include <cassert>
13
14 /* Being a C library code it is best bodily included and tested with C++ type-safe techniques. */
15 #include "lib/rfc1035.c"
16 #include "lib/rfc2671.c"
17
18 CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
19
20 // TODO Test each function in the Library independently
21 // Just because we can for global functions.
22 // It's good for the code too.
23
24 void testRFC1035::testHeaderUnpack()
25 {
26 /* Setup a buffer with the known-content packet */
27 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";
28 size_t len = 126;
29 rfc1035_message *msg = NULL;
30 int res = 0;
31 unsigned int off = 0;
32
33 /* Test the HeaderUnpack function */
34 msg = new rfc1035_message;
35 res = rfc1035HeaderUnpack(buf, len, &off, msg);
36 CPPUNIT_ASSERT(res == 0);
37 CPPUNIT_ASSERT_EQUAL((short unsigned int)0x76b1, msg->id);
38 CPPUNIT_ASSERT(msg->qr == 1);
39 /* flags */
40 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->opcode);
41 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->aa);
42 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->tc);
43 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg->rd);
44 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg->ra);
45 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->rcode);
46 /* RR counts */
47 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg->qdcount);
48 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg->ancount);
49 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg->nscount);
50 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg->arcount);
51
52 /* cleanup */
53 delete msg;
54 msg = NULL;
55 }
56
57 void testRFC1035::testParseAPacket()
58 {
59 /* Setup a buffer with the known-content packet */
60 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";
61 size_t len = 126;
62 rfc1035_message *msg = NULL;
63 int res = 0;
64
65 /* Test the MessageUnpack function itself */
66 res = rfc1035MessageUnpack(buf, len, &msg);
67
68 CPPUNIT_ASSERT_EQUAL(1, res);
69 CPPUNIT_ASSERT(msg != NULL);
70 /* cleanup */
71 rfc1035MessageDestroy(&msg);
72 CPPUNIT_ASSERT(msg == NULL);
73 }
74
75 void testRFC1035::testBugPacketEndingOnCompressionPtr()
76 {
77 /* Setup a buffer with the known-to-fail packet */
78 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";
79 size_t len = 59;
80 rfc1035_message *msg = NULL;
81 int res = 0;
82 unsigned int off = 0;
83
84 /* Test the HeaderUnpack function results */
85 msg = new rfc1035_message;
86 res = rfc1035HeaderUnpack(buf, len, &off, msg);
87 CPPUNIT_ASSERT(0 == res);
88 CPPUNIT_ASSERT(0xec7b == msg->id);
89 CPPUNIT_ASSERT(1 == msg->qr);
90 /* flags */
91 CPPUNIT_ASSERT(0 == msg->opcode);
92 CPPUNIT_ASSERT(0 == msg->aa);
93 CPPUNIT_ASSERT(0 == msg->tc);
94 CPPUNIT_ASSERT(1 == msg->rd);
95 CPPUNIT_ASSERT(1 == msg->ra);
96 CPPUNIT_ASSERT(0 == msg->rcode);
97 /* RR counts */
98 CPPUNIT_ASSERT(1 == msg->qdcount);
99 CPPUNIT_ASSERT(1 == msg->ancount);
100 CPPUNIT_ASSERT(0 == msg->nscount);
101 CPPUNIT_ASSERT(0 == msg->arcount);
102 CPPUNIT_ASSERT(12 == off);
103 printf("\n Header : OK");
104 /* cleanup */
105 delete msg;
106 msg = NULL;
107
108 // TODO explicitly test RR and Name unpack functions for this packet.
109
110 /* Test the MessageUnpack function itself */
111 res = rfc1035MessageUnpack(buf, len, &msg);
112
113 CPPUNIT_ASSERT_EQUAL(1, res);
114 CPPUNIT_ASSERT(msg != NULL);
115 rfc1035MessageDestroy(&msg);
116 }
117
118 void testRFC1035::testBugPacketHeadersOnly()
119 {
120 /* Setup a buffer with the known-to-fail headers-only packet */
121 const char *buf = "\xab\xcd\x81\x80\x00\x01\x00\x05\x00\x04\x00\x04";
122 size_t len = 12;
123 rfc1035_message *msg = NULL;
124 int res = 0;
125 unsigned int off = 0;
126
127 /* Test the HeaderUnpack function results */
128 msg = new rfc1035_message;
129 res = rfc1035HeaderUnpack(buf, len, &off, msg);
130 CPPUNIT_ASSERT(0 == res);
131 /* cleanup */
132 delete msg;
133 msg = NULL;
134
135 /* Test the MessageUnpack function itself */
136 res = rfc1035MessageUnpack(buf, len, &msg);
137
138 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res), 63));
139 CPPUNIT_ASSERT(res < 0);
140 CPPUNIT_ASSERT(msg == NULL);
141 }