]> git.ipfire.org Git - thirdparty/squid.git/blame - lib/tests/testRFC1035.cc
Updates for running on squid-cache.org
[thirdparty/squid.git] / lib / tests / testRFC1035.cc
CommitLineData
cc192b50 1#include "config.h"
2
3#if HAVE_ASSERT_H
4#include <assert.h>
5#endif
6
7#include "testRFC1035.h"
8
9/* Being a C library code it is best bodily included and tested with C++ type-safe techniques. */
10#include "rfc1035.c"
11
12CPPUNIT_TEST_SUITE_REGISTRATION( testRFC1035 );
13
14// TODO Test each function in the Library independently
15// Just because we can for global functions.
16// It's good for the code too.
17
18void testRFC1035::testHeaderUnpack()
19{
20 /* Setup a buffer with the known-content packet */
21 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";
22 size_t len = 126;
23 rfc1035_message *msg = NULL;
24 int res = 0;
25 unsigned int off = 0;
26
27 /* Test the HeaderUnpack function */
28 msg = new rfc1035_message;
29 res = rfc1035HeaderUnpack(buf, len, &off, msg);
30 CPPUNIT_ASSERT(res == 0);
31 CPPUNIT_ASSERT_EQUAL((short unsigned int)0x76b1, msg->id);
32 CPPUNIT_ASSERT(msg->qr == 1);
33 /* flags */
34 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->opcode);
35 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->aa);
36 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->tc);
37 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg->rd);
38 CPPUNIT_ASSERT_EQUAL((unsigned int)1, msg->ra);
39 CPPUNIT_ASSERT_EQUAL((unsigned int)0, msg->rcode);
40 /* RR counts */
41 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg->qdcount);
42 CPPUNIT_ASSERT_EQUAL((unsigned short)1, msg->ancount);
43 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg->nscount);
44 CPPUNIT_ASSERT_EQUAL((unsigned short)2, msg->arcount);
45
46 /* cleanup */
47 delete msg; msg = NULL;
48}
49
50void testRFC1035::testParseAPacket()
51{
52 /* Setup a buffer with the known-content packet */
53 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";
54 size_t len = 126;
55 rfc1035_message *msg = NULL;
56 int res = 0;
57
58 /* Test the MessageUnpack function itself */
59 res = rfc1035MessageUnpack(buf, len, &msg);
60
61 CPPUNIT_ASSERT_EQUAL(1, res);
62 CPPUNIT_ASSERT(msg != NULL);
63 /* cleanup */
64 rfc1035MessageDestroy(&msg);
65 CPPUNIT_ASSERT(msg == NULL);
66}
67
68void testRFC1035::testBugPacketEndingOnCompressionPtr()
69{
70 /* Setup a buffer with the known-to-fail packet */
71 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";
72 size_t len = 59;
73 rfc1035_message *msg = NULL;
74 int res = 0;
75 unsigned int off = 0;
76
77
78 /* Test the HeaderUnpack function results */
79 msg = new rfc1035_message;
80 res = rfc1035HeaderUnpack(buf, len, &off, msg);
81 CPPUNIT_ASSERT(0 == res);
82 CPPUNIT_ASSERT(0xec7b == msg->id);
83 CPPUNIT_ASSERT(1 == msg->qr);
84 /* flags */
85 CPPUNIT_ASSERT(0 == msg->opcode);
86 CPPUNIT_ASSERT(0 == msg->aa);
87 CPPUNIT_ASSERT(0 == msg->tc);
88 CPPUNIT_ASSERT(1 == msg->rd);
89 CPPUNIT_ASSERT(1 == msg->ra);
90 CPPUNIT_ASSERT(0 == msg->rcode);
91 /* RR counts */
92 CPPUNIT_ASSERT(1 == msg->qdcount);
93 CPPUNIT_ASSERT(1 == msg->ancount);
94 CPPUNIT_ASSERT(0 == msg->nscount);
95 CPPUNIT_ASSERT(0 == msg->arcount);
96 CPPUNIT_ASSERT(12 == off);
97 printf("\n Header : OK");
98 /* cleanup */
99 delete msg; msg = NULL;
100
101// TODO explicitly test RR and Name unpack functions for this packet.
102
103 /* Test the MessageUnpack function itself */
104 res = rfc1035MessageUnpack(buf, len, &msg);
105
106 CPPUNIT_ASSERT_EQUAL((const char*)NULL, rfc1035_error_message);
107 CPPUNIT_ASSERT_EQUAL(1, res);
108 CPPUNIT_ASSERT(msg != NULL);
109 rfc1035MessageDestroy(&msg);
110}