]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/tests/testRFC1035.cc
SourceFormat Enforcement
[thirdparty/squid.git] / lib / tests / testRFC1035.cc
CommitLineData
0545caaa 1/*
bde978a6 2 * Copyright (C) 1996-2015 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"
22aa015b 10#include "testRFC1035.h"
7f861c77 11#include "unitTestMain.h"
cc192b50 12
074d6a40 13#include <cassert>
cc192b50 14
cc192b50 15/* Being a C library code it is best bodily included and tested with C++ type-safe techniques. */
189f1e66 16#include "lib/rfc1035.c"
e210930b 17#include "lib/rfc2671.c"
cc192b50 18
19CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
20
21// TODO Test each function in the Library independently
f53969cc
SM
22// Just because we can for global functions.
23// It's good for the code too.
cc192b50 24
25void testRFC1035::testHeaderUnpack()
26{
26ac0430 27 /* Setup a buffer with the known-content packet */
cc192b50 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
26ac0430 34 /* Test the HeaderUnpack function */
cc192b50 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);
26ac0430 40 /* flags */
cc192b50 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);
26ac0430 47 /* RR counts */
cc192b50 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
26ac0430
AJ
53 /* cleanup */
54 delete msg;
55 msg = NULL;
cc192b50 56}
57
58void testRFC1035::testParseAPacket()
59{
26ac0430 60 /* Setup a buffer with the known-content packet */
cc192b50 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
26ac0430 66 /* Test the MessageUnpack function itself */
cc192b50 67 res = rfc1035MessageUnpack(buf, len, &msg);
68
69 CPPUNIT_ASSERT_EQUAL(1, res);
70 CPPUNIT_ASSERT(msg != NULL);
26ac0430 71 /* cleanup */
cc192b50 72 rfc1035MessageDestroy(&msg);
73 CPPUNIT_ASSERT(msg == NULL);
74}
75
76void testRFC1035::testBugPacketEndingOnCompressionPtr()
77{
26ac0430 78 /* Setup a buffer with the known-to-fail packet */
cc192b50 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
26ac0430 85 /* Test the HeaderUnpack function results */
cc192b50 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);
26ac0430 91 /* flags */
cc192b50 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);
26ac0430 98 /* RR counts */
cc192b50 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");
26ac0430
AJ
105 /* cleanup */
106 delete msg;
107 msg = NULL;
cc192b50 108
109// TODO explicitly test RR and Name unpack functions for this packet.
110
26ac0430 111 /* Test the MessageUnpack function itself */
cc192b50 112 res = rfc1035MessageUnpack(buf, len, &msg);
113
cc192b50 114 CPPUNIT_ASSERT_EQUAL(1, res);
115 CPPUNIT_ASSERT(msg != NULL);
116 rfc1035MessageDestroy(&msg);
117}
fd7b53a4
AJ
118
119void 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
42687bb2 139 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res), 63));
13e07e44 140 CPPUNIT_ASSERT(res < 0);
fd7b53a4
AJ
141 CPPUNIT_ASSERT(msg == NULL);
142}
f53969cc 143