]> git.ipfire.org Git - thirdparty/squid.git/blame - src/tests/testRFC1035.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / tests / testRFC1035.cc
CommitLineData
0545caaa 1/*
f70aedc4 2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
0545caaa
AJ
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
f7f3304a 9#include "squid.h"
4a3b98d7 10#include "dns/rfc1035.h"
22aa015b 11#include "testRFC1035.h"
7f861c77 12#include "unitTestMain.h"
cc192b50 13
074d6a40 14#include <cassert>
cc192b50 15
cc192b50 16CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
17
18// TODO Test each function in the Library independently
f53969cc
SM
19// Just because we can for global functions.
20// It's good for the code too.
cc192b50 21
22void testRFC1035::testHeaderUnpack()
23{
26ac0430 24 /* Setup a buffer with the known-content packet */
cc192b50 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
26ac0430 31 /* Test the HeaderUnpack function */
cc192b50 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);
26ac0430 37 /* flags */
cc192b50 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);
26ac0430 44 /* RR counts */
cc192b50 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
26ac0430
AJ
50 /* cleanup */
51 delete msg;
52 msg = NULL;
cc192b50 53}
54
55void testRFC1035::testParseAPacket()
56{
26ac0430 57 /* Setup a buffer with the known-content packet */
cc192b50 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
26ac0430 63 /* Test the MessageUnpack function itself */
cc192b50 64 res = rfc1035MessageUnpack(buf, len, &msg);
65
66 CPPUNIT_ASSERT_EQUAL(1, res);
67 CPPUNIT_ASSERT(msg != NULL);
26ac0430 68 /* cleanup */
cc192b50 69 rfc1035MessageDestroy(&msg);
70 CPPUNIT_ASSERT(msg == NULL);
71}
72
73void testRFC1035::testBugPacketEndingOnCompressionPtr()
74{
26ac0430 75 /* Setup a buffer with the known-to-fail packet */
cc192b50 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
26ac0430 82 /* Test the HeaderUnpack function results */
cc192b50 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);
26ac0430 88 /* flags */
cc192b50 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);
26ac0430 95 /* RR counts */
cc192b50 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");
26ac0430
AJ
102 /* cleanup */
103 delete msg;
104 msg = NULL;
cc192b50 105
106// TODO explicitly test RR and Name unpack functions for this packet.
107
26ac0430 108 /* Test the MessageUnpack function itself */
cc192b50 109 res = rfc1035MessageUnpack(buf, len, &msg);
110
cc192b50 111 CPPUNIT_ASSERT_EQUAL(1, res);
112 CPPUNIT_ASSERT(msg != NULL);
113 rfc1035MessageDestroy(&msg);
114}
fd7b53a4
AJ
115
116void 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
42687bb2 136 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res), 63));
13e07e44 137 CPPUNIT_ASSERT(res < 0);
fd7b53a4
AJ
138 CPPUNIT_ASSERT(msg == NULL);
139}
f53969cc 140