]> git.saurik.com Git - apple/mdnsresponder.git/blob - unittests/mDNSCoreReceiveTest.c
mDNSResponder-878.240.1.tar.gz
[apple/mdnsresponder.git] / unittests / mDNSCoreReceiveTest.c
1 #include "mDNSCoreReceiveTest.h"
2 #include "unittest_common.h"
3
4 int InitmDNSCoreReceiveTest(void);
5 int ValidQueryReqTest(void);
6 int NullDstQueryReqTest(void);
7 void InitmDNSStorage(mDNS *const m);
8
9 // This DNS message was gleaned from a uDNS query request packet that was captured with Wireshark.
10 uint8_t udns_query_request_message[28] = { // contains 1 question for www.f5.com
11 0x31, 0xca, // transaction id
12 0x01, 0x00, // flags
13 0x00, 0x01, // 1 question
14 0x00, 0x00, // no anwsers
15 0x00, 0x00, // no authoritative answers
16 0x00, 0x00, // no additionals
17 0x03, 0x77, 0x77, 0x77, 0x02, 0x66, 0x35, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
18 };
19
20 // This DNS message was gleaned from a uDNS query request packet that was captured with Wireshark.
21 // Then the header id (more specifically, the msg->h.id) was deliberately cleared to force code
22 // path to traverse regression case, <rdar://problem/28556513>.
23 uint8_t udns_query_request_message_with_invalid_id[28] = { // contains 1 question for www.f5.com, msg->h.id = 0
24 0x00, 0x00, // transaction id
25 0x01, 0x00, // flags
26 0x00, 0x01, // 1 question
27 0x00, 0x00, // no anwsers
28 0x00, 0x00, // no authoritative answers
29 0x00, 0x00, // no additionals
30 0x03, 0x77, 0x77, 0x77, 0x02, 0x66, 0x35, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01
31 };
32
33 uint8_t arp_request_packet[28] = { // contains 1 question for www.f5.com, msg->h.id = 0
34 0x00, 0x01, // hardware type: enet
35 0x08, 0x00, // protocol type: IP
36 0x06, // hardware size
37 0x04, // Protcol size
38 0x00, 0x01, // opcode request
39 0x24, 0x01, 0xc7, 0x24, 0x35, 0x00, // Sender mac addr
40 0x11, 0xe2, 0x14, 0x01, // Sender ip addr
41 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // target mac addr
42 0x11, 0xe2, 0x17, 0xbe // target ip addr
43 };
44 UNITTEST_HEADER(mDNSCoreReceiveTest)
45 UNITTEST_TEST(InitmDNSCoreReceiveTest)
46 UNITTEST_TEST(ValidQueryReqTest)
47 UNITTEST_TEST(NullDstQueryReqTest)
48 UNITTEST_FOOTER
49
50 UNITTEST_HEADER(InitmDNSCoreReceiveTest)
51 mDNSPlatformTimeInit();
52 init_logging_ut();
53 mDNS_LoggingEnabled = 0;
54 mDNS_PacketLoggingEnabled = 0;
55 UNITTEST_FOOTER
56
57 UNITTEST_HEADER(ValidQueryReqTest)
58 mDNS *const m = &mDNSStorage;
59 mDNSAddr srcaddr, dstaddr;
60 mDNSIPPort srcport, dstport;
61 DNSMessage * msg;
62 const mDNSu8 * end;
63
64 // Init unit test environment and verify no error occurred.
65 mStatus result = init_mdns_environment(mDNStrue);
66 UNITTEST_ASSERT(result == mStatus_NoError);
67
68 // Used random values for srcaddr and srcport
69 srcaddr.type = mDNSAddrType_IPv4;
70 srcaddr.ip.v4.b[0] = 192;
71 srcaddr.ip.v4.b[1] = 168;
72 srcaddr.ip.v4.b[2] = 1;
73 srcaddr.ip.v4.b[3] = 10;
74 srcport.NotAnInteger = swap16((mDNSu16)53);
75
76 // Used random values for dstaddr and dstport
77 dstaddr.type = mDNSAddrType_IPv4;
78 dstaddr.ip.v4.b[0] = 192;
79 dstaddr.ip.v4.b[1] = 168;
80 dstaddr.ip.v4.b[2] = 1;
81 dstaddr.ip.v4.b[3] = 20;
82 dstport.NotAnInteger = swap16((mDNSu16)49339);
83
84 // Set message to a DNS message (copied from a WireShark packet)
85 msg = (DNSMessage *)udns_query_request_message;
86 end = udns_query_request_message + sizeof(udns_query_request_message);
87
88 // Execute mDNSCoreReceive using a valid DNS message
89 mDNSCoreReceive(m, msg, end, &srcaddr, srcport, &dstaddr, dstport, if_nametoindex("en0"));
90
91 // Verify that mDNSCoreReceiveQuery traversed the normal code path
92 UNITTEST_ASSERT(m->mDNSStats.NormalQueries == 1);
93
94 UNITTEST_FOOTER
95
96 UNITTEST_HEADER(NullDstQueryReqTest)
97
98 mDNS *const m = &mDNSStorage;
99 mDNSAddr srcaddr;
100 mDNSIPPort srcport, dstport;
101 DNSMessage * msg;
102 const mDNSu8 * end;
103
104 // This test case does not require setup of interfaces, the record's cache, or pending questions
105 // so m is initialized to all zeros.
106 InitmDNSStorage(m);
107
108 // Used random values for srcaddr and srcport
109 srcaddr.type = mDNSAddrType_IPv4;
110 srcaddr.ip.v4.b[0] = 192;
111 srcaddr.ip.v4.b[1] = 168;
112 srcaddr.ip.v4.b[2] = 1;
113 srcaddr.ip.v4.b[3] = 10;
114 srcport.NotAnInteger = swap16((mDNSu16)53);
115
116 // Used random value for dstport
117 dstport.NotAnInteger = swap16((mDNSu16)49339);
118
119 // Set message to a DNS message (copied from a WireShark packet)
120 msg = (DNSMessage *)udns_query_request_message_with_invalid_id;
121 end = udns_query_request_message_with_invalid_id + sizeof(udns_query_request_message_with_invalid_id);
122
123 // Execute mDNSCoreReceive to regress <rdar://problem/28556513>
124 mDNSCoreReceive(m, msg, end, &srcaddr, srcport, NULL, dstport, if_nametoindex("en0"));
125
126 // Verify that mDNSCoreReceiveQuery was NOT traversed through the normal code path
127 UNITTEST_ASSERT(m->mDNSStats.NormalQueries == 0);
128
129 // Verify code path that previously crashed, in <rdar://problem/28556513>, now traverses successfully
130 // by checking a counter that was incremented on code path that crashed.
131 UNITTEST_ASSERT(m->MPktNum == 1);
132
133 UNITTEST_FOOTER
134
135 void InitmDNSStorage(mDNS *const m)
136 {
137 memset(m, 0, sizeof(mDNS));
138 }
139