testRFC1035.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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 "compat/cppunit.h"
11#include "dns/rfc1035.h"
12#include "unitTestMain.h"
13
14// #include <cassert>
15
16/*
17 * test the DNS resolver RFC 1035 Engine
18 */
19
20class TestRfc1035 : public CPPUNIT_NS::TestFixture
21{
25
29
30public:
31protected:
32 void testHeaderUnpack();
33 void testParseAPacket();
34
35 // bugs.
38};
39
41
42// TODO Test each function in the Library independently
43// Just because we can for global functions.
44// It's good for the code too.
45
47{
48 /* Setup a buffer with the known-content packet */
49 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";
50 size_t len = 126;
51 rfc1035_message *msg = nullptr;
52 int res = 0;
53 unsigned int off = 0;
54
55 /* Test the HeaderUnpack function */
56 msg = new rfc1035_message;
57 res = rfc1035HeaderUnpack(buf, len, &off, msg);
58 CPPUNIT_ASSERT(res == 0);
59 CPPUNIT_ASSERT_EQUAL((short unsigned int)0x76b1, msg->id);
60 CPPUNIT_ASSERT(msg->qr == 1);
61 /* flags */
62 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->opcode);
63 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->aa);
64 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->tc);
65 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg->rd);
66 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg->ra);
67 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->rcode);
68 /* RR counts */
69 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg->qdcount);
70 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg->ancount);
71 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg->nscount);
72 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg->arcount);
73
74 /* cleanup */
75 delete msg;
76 msg = nullptr;
77}
78
80{
81 /* Setup a buffer with the known-content packet */
82 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";
83 size_t len = 126;
84 rfc1035_message *msg = nullptr;
85 int res = 0;
86
87 /* Test the MessageUnpack function itself */
88 res = rfc1035MessageUnpack(buf, len, &msg);
89
90 CPPUNIT_ASSERT_EQUAL(1, res);
91 CPPUNIT_ASSERT(msg != nullptr);
92 /* cleanup */
94 CPPUNIT_ASSERT(msg == nullptr);
95}
96
98{
99 /* Setup a buffer with the known-to-fail packet */
100 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";
101 size_t len = 59;
102 rfc1035_message *msg = nullptr;
103 int res = 0;
104 unsigned int off = 0;
105
106 /* Test the HeaderUnpack function results */
107 msg = new rfc1035_message;
108 res = rfc1035HeaderUnpack(buf, len, &off, msg);
109 CPPUNIT_ASSERT(0 == res);
110 CPPUNIT_ASSERT(0xec7b == msg->id);
111 CPPUNIT_ASSERT(1 == msg->qr);
112 /* flags */
113 CPPUNIT_ASSERT(0 == msg->opcode);
114 CPPUNIT_ASSERT(0 == msg->aa);
115 CPPUNIT_ASSERT(0 == msg->tc);
116 CPPUNIT_ASSERT(1 == msg->rd);
117 CPPUNIT_ASSERT(1 == msg->ra);
118 CPPUNIT_ASSERT(0 == msg->rcode);
119 /* RR counts */
120 CPPUNIT_ASSERT(1 == msg->qdcount);
121 CPPUNIT_ASSERT(1 == msg->ancount);
122 CPPUNIT_ASSERT(0 == msg->nscount);
123 CPPUNIT_ASSERT(0 == msg->arcount);
124 CPPUNIT_ASSERT(12 == off);
125 printf("\n Header : OK");
126 /* cleanup */
127 delete msg;
128 msg = nullptr;
129
130// TODO explicitly test RR and Name unpack functions for this packet.
131
132 /* Test the MessageUnpack function itself */
133 res = rfc1035MessageUnpack(buf, len, &msg);
134
135 CPPUNIT_ASSERT_EQUAL(1, res);
136 CPPUNIT_ASSERT(msg != nullptr);
138}
139
141{
142 /* Setup a buffer with the known-to-fail headers-only packet */
143 const char *buf = "\xab\xcd\x81\x80\x00\x01\x00\x05\x00\x04\x00\x04";
144 size_t len = 12;
145 rfc1035_message *msg = nullptr;
146 int res = 0;
147 unsigned int off = 0;
148
149 /* Test the HeaderUnpack function results */
150 msg = new rfc1035_message;
151 res = rfc1035HeaderUnpack(buf, len, &off, msg);
152 CPPUNIT_ASSERT(0 == res);
153 /* cleanup */
154 delete msg;
155 msg = nullptr;
156
157 /* Test the MessageUnpack function itself */
158 res = rfc1035MessageUnpack(buf, len, &msg);
159
160 CPPUNIT_ASSERT(0 == memcmp("The DNS reply message is corrupt or could not be safely parsed.", rfc1035ErrorMessage(res), 63));
161 CPPUNIT_ASSERT(res < 0);
162 CPPUNIT_ASSERT(msg == nullptr);
163}
164
165int
166main(int argc, char *argv[])
167{
168 return TestProgram().run(argc, argv);
169}
170
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
CPPUNIT_TEST_SUITE(TestRfc1035)
CPPUNIT_TEST(testHeaderUnpack)
CPPUNIT_TEST_SUITE_END()
CPPUNIT_TEST(testBugPacketHeadersOnly)
void testBugPacketHeadersOnly()
Definition: testRFC1035.cc:140
void testParseAPacket()
Definition: testRFC1035.cc:79
CPPUNIT_TEST(testBugPacketEndingOnCompressionPtr)
void testBugPacketEndingOnCompressionPtr()
Definition: testRFC1035.cc:97
CPPUNIT_TEST(testParseAPacket)
void testHeaderUnpack()
Definition: testRFC1035.cc:46
int rfc1035HeaderUnpack(const char *buf, size_t sz, unsigned int *off, rfc1035_message *h)
Definition: rfc1035.cc:180
int rfc1035MessageUnpack(const char *buf, size_t sz, rfc1035_message **answer)
Definition: rfc1035.cc:584
const char * rfc1035ErrorMessage(int n)
Definition: rfc1035.cc:444
void rfc1035MessageDestroy(rfc1035_message **msg)
Definition: rfc1035.cc:530
struct _rfc1035_message rfc1035_message
Definition: rfc1035.h:54
unsigned short ancount
Definition: rfc1035.h:65
unsigned short nscount
Definition: rfc1035.h:66
unsigned int rd
Definition: rfc1035.h:61
unsigned int tc
Definition: rfc1035.h:60
unsigned short qdcount
Definition: rfc1035.h:64
unsigned int opcode
Definition: rfc1035.h:58
unsigned int rcode
Definition: rfc1035.h:63
unsigned int aa
Definition: rfc1035.h:59
unsigned short id
Definition: rfc1035.h:56
unsigned int ra
Definition: rfc1035.h:62
unsigned int qr
Definition: rfc1035.h:57
unsigned short arcount
Definition: rfc1035.h:67
int main(int argc, char *argv[])
Definition: testRFC1035.cc:166
CPPUNIT_TEST_SUITE_REGISTRATION(TestRfc1035)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors