1 #include "mDNSCoreReceiveTest.h"
2 #include "unittest_common.h"
4 int InitmDNSCoreReceiveTest(void);
5 int ValidQueryReqTest(void);
6 int NullDstQueryReqTest(void);
7 void InitmDNSStorage(mDNS
*const m
);
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
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
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
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
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
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
44 UNITTEST_HEADER(mDNSCoreReceiveTest
)
45 UNITTEST_TEST(InitmDNSCoreReceiveTest
)
46 UNITTEST_TEST(ValidQueryReqTest
)
47 UNITTEST_TEST(NullDstQueryReqTest
)
50 UNITTEST_HEADER(InitmDNSCoreReceiveTest
)
51 mDNSPlatformTimeInit();
53 mDNS_LoggingEnabled
= 0;
54 mDNS_PacketLoggingEnabled
= 0;
57 UNITTEST_HEADER(ValidQueryReqTest
)
58 mDNS
*const m
= &mDNSStorage
;
59 mDNSAddr srcaddr
, dstaddr
;
60 mDNSIPPort srcport
, dstport
;
64 // Init unit test environment and verify no error occurred.
65 mStatus result
= init_mdns_environment(mDNStrue
);
66 UNITTEST_ASSERT(result
== mStatus_NoError
);
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);
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);
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
);
88 // Execute mDNSCoreReceive using a valid DNS message
89 mDNSCoreReceive(m
, msg
, end
, &srcaddr
, srcport
, &dstaddr
, dstport
, if_nametoindex("en0"));
91 // Verify that mDNSCoreReceiveQuery traversed the normal code path
92 UNITTEST_ASSERT(m
->mDNSStats
.NormalQueries
== 1);
96 UNITTEST_HEADER(NullDstQueryReqTest
)
98 mDNS
*const m
= &mDNSStorage
;
100 mDNSIPPort srcport
, dstport
;
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.
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);
116 // Used random value for dstport
117 dstport
.NotAnInteger
= swap16((mDNSu16
)49339);
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
);
123 // Execute mDNSCoreReceive to regress <rdar://problem/28556513>
124 mDNSCoreReceive(m
, msg
, end
, &srcaddr
, srcport
, NULL
, dstport
, if_nametoindex("en0"));
126 // Verify that mDNSCoreReceiveQuery was NOT traversed through the normal code path
127 UNITTEST_ASSERT(m
->mDNSStats
.NormalQueries
== 0);
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);
135 void InitmDNSStorage(mDNS
*const m
)
137 memset(m
, 0, sizeof(mDNS
));