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