summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/unittests/LocalOnlyTimeoutTests.c
blob: e2959480a58ce274bff617e57b67a3843bf8480f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "LocalOnlyTimeoutTests.h"
#include "unittest_common.h"

mDNSlocal int InitUnitTest(void);
mDNSlocal int StartLocalOnlyClientQueryRequest(void);
mDNSlocal int PopulateCacheWithClientLOResponseRecords(void);
mDNSlocal int RestartLocalOnlyClientQueryRequest(void);
mDNSlocal int FinalizeUnitTest(void);
mDNSlocal mStatus InitEtcHostsRecords();

// This unit test's variables
static request_state* client_request_message;
static UDPSocket* local_socket;
static char domainname_cstr[MAX_ESCAPED_DOMAIN_NAME];

// This query request message was generated from the following command: "dns-sd -lo -timeout -Q cardinal2.apple.com. A"
char query_req_msgbuf[33]= {
	0x00, 0x01, 0x90, 0x00,
	// DNSServiceFlags.L = (kDNSServiceFlagsReturnIntermediates |kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsTimeout)
	0xff, 0xff, 0xff, 0xff,
	// interfaceIndex = mDNSInterface_LocalOnly
	0x63, 0x61, 0x72, 0x64, 0x69, 0x6e, 0x61, 0x6c,
	0x32, 0x2e, 0x61, 0x70, 0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x00, 0x00, 0x01, 0x00,
	0x01
};

UNITTEST_HEADER(LocalOnlyTimeoutTests)
	UNITTEST_TEST(InitUnitTest)
	UNITTEST_TEST(StartLocalOnlyClientQueryRequest)
	UNITTEST_TEST(PopulateCacheWithClientLOResponseRecords)
	UNITTEST_TEST(RestartLocalOnlyClientQueryRequest)
	UNITTEST_TEST(FinalizeUnitTest)
UNITTEST_FOOTER

// The InitUnitTest() initializes a minimal mDNSResponder environment as
// well as allocates memory for a local_socket and client request.
// It also sets the domainname_cstr specified in the client's query request.
// Note: This unit test does not send packets on the wire and it does not open sockets.
UNITTEST_HEADER(InitUnitTest)

	// Init mDNSStorage
	mStatus result = init_mdns_storage();
	if (result != mStatus_NoError)
		return result;

	// Allocate a client request
	local_socket = calloc(1, sizeof(request_state));

	// Allocate memory for a request that is used to make client requests.
	client_request_message = calloc(1, sizeof(request_state));

	// Init domainname that is used by unit tests
	strlcpy(domainname_cstr, "cardinal2.apple.com.", sizeof(domainname_cstr));

UNITTEST_FOOTER

// This unit test starts a local only request for "cardinal2.apple.com.".  It first
// calls start_client_request to start a query, it then verifies the
// req and query data structures are set as expected. Next, the cache is verified to
// be empty by AnswerNewLocalOnlyQuestion() and so results in GenerateNegativeResponse()
// getting called which sets up a reply with a negative answer in it for the client.
// On return from mDNS_Execute, the client's reply structure is verified to be set as
// expected. Lastly the timeout is simulated and mDNS_Execute is called. This results
// in a call to TimeoutQuestions(). And again, the GenerateNegativeResponse() is called
// which returns a negative response to the client.  This time the client reply is verified
// to be setup with a timeout result.
UNITTEST_HEADER(StartLocalOnlyClientQueryRequest)

	mDNS *const m = &mDNSStorage;
    request_state* req = client_request_message;
	char *msgptr = (char *)query_req_msgbuf;
	size_t msgsz = sizeof(query_req_msgbuf);
	DNSQuestion *q;
	mDNSs32 min_size = sizeof(DNSServiceFlags) + sizeof(mDNSu32) + 4;
	mStatus err = mStatus_NoError;
	char qname_cstr[MAX_ESCAPED_DOMAIN_NAME];
	struct reply_state *reply;
	size_t len;

	// Process the unit test's client request
	start_client_request(req, msgptr, msgsz, query_request, local_socket);
	UNITTEST_ASSERT(err == mStatus_NoError);

	// Verify the query initialized and request fields were set as expected
	UNITTEST_ASSERT(err == mStatus_NoError);
	UNITTEST_ASSERT(req->hdr.version == VERSION);
	UNITTEST_ASSERT((mDNSs32)req->data_bytes > min_size);
	UNITTEST_ASSERT(req->flags == (kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsReturnIntermediates | kDNSServiceFlagsTimeout));
	UNITTEST_ASSERT(req->interfaceIndex == kDNSServiceInterfaceIndexLocalOnly);
	UNITTEST_ASSERT(req->terminate != mDNSNULL);

	q = &req->u.queryrecord.q;
	UNITTEST_ASSERT(q == m->NewLocalOnlyQuestions);
	UNITTEST_ASSERT(m->Questions == NULL);
	UNITTEST_ASSERT(m->NewQuestions == NULL);
	UNITTEST_ASSERT(q->SuppressUnusable == 1);
	UNITTEST_ASSERT(q->ReturnIntermed == 1);
	UNITTEST_ASSERT(q->SuppressQuery == 0);									// Regress <rdar://problem/27571734>

	UNITTEST_ASSERT(q->qnameOrig == mDNSNULL);
	ConvertDomainNameToCString(&q->qname, qname_cstr);
	UNITTEST_ASSERT(!strcmp(qname_cstr, domainname_cstr));
	UNITTEST_ASSERT(q->qnamehash == DomainNameHashValue(&q->qname));

	UNITTEST_ASSERT(q->InterfaceID == mDNSInterface_LocalOnly);
	UNITTEST_ASSERT(q->flags == req->flags);
	UNITTEST_ASSERT(q->qtype == 1);
	UNITTEST_ASSERT(q->qclass == 1);
	UNITTEST_ASSERT(q->LongLived == 0);
	UNITTEST_ASSERT(q->ExpectUnique == mDNSfalse);
	UNITTEST_ASSERT(q->ForceMCast == 0);
	UNITTEST_ASSERT(q->TimeoutQuestion == 1);
	UNITTEST_ASSERT(q->WakeOnResolve == 0);
	UNITTEST_ASSERT(q->UseBackgroundTrafficClass == 0);
	UNITTEST_ASSERT(q->ValidationRequired == 0);
	UNITTEST_ASSERT(q->ValidatingResponse == 0);
	UNITTEST_ASSERT(q->ProxyQuestion == 0);
	UNITTEST_ASSERT(q->AnonInfo == mDNSNULL);
	UNITTEST_ASSERT(q->QuestionCallback != mDNSNULL);
	UNITTEST_ASSERT(q->QuestionContext == req);
	UNITTEST_ASSERT(q->SearchListIndex == 0);
	UNITTEST_ASSERT(q->DNSSECAuthInfo == mDNSNULL);
	UNITTEST_ASSERT(q->DAIFreeCallback == mDNSNULL);
	UNITTEST_ASSERT(q->RetryWithSearchDomains == 0);
	UNITTEST_ASSERT(q->StopTime != 0);
	UNITTEST_ASSERT(q->AppendSearchDomains == 0);
	UNITTEST_ASSERT(q->AppendLocalSearchDomains == 0);
	UNITTEST_ASSERT(q->DuplicateOf == mDNSNULL);

	// At this point the the cache is empty. Calling mDNS_Execute will answer the local-only
	// question with a negative response.
	m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
	mDNS_Execute(m);  // Regress <rdar://problem/28721294>

	// Verify reply is a negative response and error code is set to kDNSServiceErr_NoSuchRecord error.
	reply = req->replies;
	UNITTEST_ASSERT(reply != mDNSNULL);

	UNITTEST_ASSERT(m->NewLocalOnlyQuestions == NULL);
	UNITTEST_ASSERT(q->LOAddressAnswers == 0);

	len = get_reply_len(qname_cstr, 0);

	UNITTEST_ASSERT(reply->next == mDNSNULL);
	UNITTEST_ASSERT(reply->totallen == reply->mhdr->datalen + sizeof(ipc_msg_hdr));
	UNITTEST_ASSERT(reply->mhdr->version == VERSION);
	UNITTEST_ASSERT(reply->mhdr->datalen == len);
	UNITTEST_ASSERT(reply->mhdr->ipc_flags == 0);
	UNITTEST_ASSERT(reply->mhdr->op == query_reply_op);

	UNITTEST_ASSERT(reply->rhdr->flags == htonl(kDNSServiceFlagsAdd));
	UNITTEST_ASSERT(reply->rhdr->ifi == kDNSServiceInterfaceIndexLocalOnly);	// Regress <rdar://problem/27340874>
	UNITTEST_ASSERT(reply->rhdr->error ==
					(DNSServiceErrorType)htonl(kDNSServiceErr_NoSuchRecord));	// Regress <rdar://problem/24827555>

	// Simulate what udsserver_idle normally does for clean up
	freeL("StartLocalOnlyClientQueryRequest:reply", reply);
	req->replies = NULL;

	// Simulate the query time out of the local-only question.
	// The expected behavior is a negative answer with time out error
	m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
	q->StopTime = mDNS_TimeNow_NoLock(m);
	m->NextScheduledStopTime -= mDNSPlatformOneSecond*5;
	mDNS_Execute(m);

	// Verify the reply is a negative response with timeout error.
	reply = req->replies;
	UNITTEST_ASSERT(reply != NULL);
	UNITTEST_ASSERT(m->NewLocalOnlyQuestions == NULL);
	UNITTEST_ASSERT(q->LOAddressAnswers == 0);

	len = get_reply_len(qname_cstr, 0);

	UNITTEST_ASSERT(reply->next == mDNSNULL);
	UNITTEST_ASSERT(reply->totallen == len + sizeof(ipc_msg_hdr));
	UNITTEST_ASSERT(reply->mhdr->version == VERSION);
	UNITTEST_ASSERT(reply->mhdr->datalen == len);
	UNITTEST_ASSERT(reply->mhdr->ipc_flags == 0);
	UNITTEST_ASSERT(reply->mhdr->op == query_reply_op);
	UNITTEST_ASSERT(reply->rhdr->flags == htonl(kDNSServiceFlagsAdd));
	UNITTEST_ASSERT(reply->rhdr->ifi == kDNSServiceInterfaceIndexLocalOnly);	// Regress <rdar://problem/27340874>
	UNITTEST_ASSERT(reply->rhdr->error ==
					(DNSServiceErrorType)htonl(kDNSServiceErr_Timeout));		// Regress <rdar://problem/27562965>

	// Free request and reallocate to use when query is restarted
	free_req(req);
	client_request_message = calloc(1, sizeof(request_state));

UNITTEST_FOOTER

// This unit test populates the cache with four /etc/hosts records and then
// verifies there are four entries in the cache.
UNITTEST_HEADER(PopulateCacheWithClientLOResponseRecords)

	mDNS *const m = &mDNSStorage;

	// Verify cache is empty
	int count = LogEtcHosts_ut(m);
	UNITTEST_ASSERT(count == 0);

	// Populate /etc/hosts
	mStatus result = InitEtcHostsRecords();
	UNITTEST_ASSERT(result == mStatus_NoError);

	// mDNS_Execute is called to populate the /etc/hosts cache.
	m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
	mDNS_Execute(m);

	count = LogEtcHosts_ut(m);
	UNITTEST_ASSERT(count == 4);

UNITTEST_FOOTER

// This unit test starts a local only request for "cardinal2.apple.com.".  It first
// calls start_client_request to start a query, it then verifies the
// req and query data structures are set as expected. Next, the cache is verified to
// contain the answer by AnswerNewLocalOnlyQuestion() and so results in setting up an
// answer reply to the client. On return from mDNS_Execute, the client's reply structure
// is verified to be set as expected. Lastly the timeout is simulated and mDNS_Execute is
// called. This results in a call to TimeoutQuestions(). And this time, the
// GenerateNegativeResponse() is called which returns a negative response to the client
// which specifies the timeout occurred. Again, the answer reply is verified to
// to specify a timeout.
UNITTEST_HEADER(RestartLocalOnlyClientQueryRequest)

	mDNS *const m = &mDNSStorage;
	request_state* req = client_request_message;
	char *msgptr = (char *)query_req_msgbuf;
	size_t msgsz = sizeof(query_req_msgbuf);	DNSQuestion *q;
	mDNSs32 min_size = sizeof(DNSServiceFlags) + sizeof(mDNSu32) + 4;
	mStatus err = mStatus_NoError;
	char qname_cstr[MAX_ESCAPED_DOMAIN_NAME];
	struct reply_state *reply;
	size_t len;

	// Process the unit test's client request
	start_client_request(req, msgptr, msgsz, query_request, local_socket);
	UNITTEST_ASSERT(err == mStatus_NoError);

	UNITTEST_ASSERT(err == mStatus_NoError);
	UNITTEST_ASSERT(req->hdr.version == VERSION);
	UNITTEST_ASSERT((mDNSs32)req->data_bytes > min_size);
	UNITTEST_ASSERT(req->flags == (kDNSServiceFlagsSuppressUnusable | kDNSServiceFlagsReturnIntermediates | kDNSServiceFlagsTimeout));
	UNITTEST_ASSERT(req->interfaceIndex == kDNSServiceInterfaceIndexLocalOnly);
	UNITTEST_ASSERT(req->terminate != mDNSNULL);
	UNITTEST_ASSERT(m->Questions == NULL);

	q = &req->u.queryrecord.q;
	UNITTEST_ASSERT(q == m->NewLocalOnlyQuestions);
	UNITTEST_ASSERT(q->SuppressUnusable == 1);
	UNITTEST_ASSERT(q->ReturnIntermed == 1);
	UNITTEST_ASSERT(q->SuppressQuery == 0);										// Regress <rdar://problem/27571734>
	UNITTEST_ASSERT(q->qnamehash == DomainNameHashValue(&q->qname));
	UNITTEST_ASSERT(q->InterfaceID == mDNSInterface_LocalOnly);
	UNITTEST_ASSERT(q->flags == req->flags);
	UNITTEST_ASSERT(q->qtype == 1);
	UNITTEST_ASSERT(q->qclass == 1);
	UNITTEST_ASSERT(q->LongLived == 0);
	UNITTEST_ASSERT(q->ExpectUnique == mDNSfalse);
	UNITTEST_ASSERT(q->ForceMCast == 0);
	UNITTEST_ASSERT(q->TimeoutQuestion == 1);
	UNITTEST_ASSERT(q->WakeOnResolve == 0);
	UNITTEST_ASSERT(q->UseBackgroundTrafficClass == 0);
	UNITTEST_ASSERT(q->ValidationRequired == 0);
	UNITTEST_ASSERT(q->ValidatingResponse == 0);
	UNITTEST_ASSERT(q->ProxyQuestion == 0);
	UNITTEST_ASSERT(q->AnonInfo == mDNSNULL);
	UNITTEST_ASSERT(q->QuestionCallback != mDNSNULL);
	UNITTEST_ASSERT(q->QuestionContext == req);
	UNITTEST_ASSERT(q->SearchListIndex == 0);
	UNITTEST_ASSERT(q->DNSSECAuthInfo == mDNSNULL);
	UNITTEST_ASSERT(q->DAIFreeCallback == mDNSNULL);
	UNITTEST_ASSERT(q->RetryWithSearchDomains == 0);
	UNITTEST_ASSERT(q->StopTime != 0);
	UNITTEST_ASSERT(q->AppendSearchDomains == 0);
	UNITTEST_ASSERT(q->AppendLocalSearchDomains == 0);
	UNITTEST_ASSERT(q->DuplicateOf == mDNSNULL);
	ConvertDomainNameToCString(&q->qname, qname_cstr);
	UNITTEST_ASSERT(!strcmp(qname_cstr, domainname_cstr));

	// Answer local-only question with found cache entry
	m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
	mDNS_Execute(m);															// Regress <rdar://problem/28721294>
	UNITTEST_ASSERT(m->NewLocalOnlyQuestions == NULL);
	UNITTEST_ASSERT(req->u.queryrecord.ans == 1);
	UNITTEST_ASSERT(q->LOAddressAnswers == 1);
	UNITTEST_ASSERT(q == m->LocalOnlyQuestions);

	reply = req->replies;
	len = get_reply_len(qname_cstr, 4);

	UNITTEST_ASSERT(reply->next == mDNSNULL);
	UNITTEST_ASSERT(reply->totallen == len + sizeof(ipc_msg_hdr));
	UNITTEST_ASSERT(reply->mhdr->version == VERSION);
	UNITTEST_ASSERT(reply->mhdr->datalen == len);
	UNITTEST_ASSERT(reply->mhdr->ipc_flags == 0);
	UNITTEST_ASSERT(reply->mhdr->op == query_reply_op);
	UNITTEST_ASSERT(reply->rhdr->flags == htonl(kDNSServiceFlagsAdd));
	UNITTEST_ASSERT(reply->rhdr->ifi == kDNSServiceInterfaceIndexLocalOnly);	// Regress <rdar://problem/27340874>
	UNITTEST_ASSERT(reply->rhdr->error == kDNSServiceErr_NoError);

	// Simulate the query time out of the local-only question.
	// The expected behavior is a negative answer with time out error
	m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
	q->StopTime = mDNS_TimeNow_NoLock(m);
	m->NextScheduledStopTime -= mDNSPlatformOneSecond*5;
	mDNS_Execute(m);

	reply = req->replies->next;
	UNITTEST_ASSERT(reply != NULL);
	UNITTEST_ASSERT(reply->next == NULL);
	UNITTEST_ASSERT(m->NewLocalOnlyQuestions == NULL);
	UNITTEST_ASSERT(q->LOAddressAnswers == 0);
	len = get_reply_len(qname_cstr, 0);

	UNITTEST_ASSERT(reply->next == mDNSNULL);
	UNITTEST_ASSERT(reply->totallen == len + + sizeof(ipc_msg_hdr));
	UNITTEST_ASSERT(reply->mhdr->version == VERSION);
	UNITTEST_ASSERT(reply->mhdr->datalen == len);
	UNITTEST_ASSERT(reply->mhdr->ipc_flags == 0);
	UNITTEST_ASSERT(reply->mhdr->op == query_reply_op);
	UNITTEST_ASSERT(reply->rhdr->flags == htonl(kDNSServiceFlagsAdd));
	UNITTEST_ASSERT(reply->rhdr->ifi == kDNSServiceInterfaceIndexLocalOnly);	// Regress <rdar://problem/27340874>
	UNITTEST_ASSERT(reply->rhdr->error ==
					(DNSServiceErrorType)htonl(kDNSServiceErr_Timeout));		// Regress <rdar://problem/27562965>

	free_req(req);
UNITTEST_FOOTER

// This function does memory cleanup and no verification.
UNITTEST_HEADER(FinalizeUnitTest)
	mDNSPlatformMemFree(local_socket);
UNITTEST_FOOTER

mDNSlocal mStatus InitEtcHostsRecords(void)
{
	mDNS *m = &mDNSStorage;
	struct sockaddr_storage hostaddr;

	AuthHash newhosts;
	mDNSPlatformMemZero(&newhosts, sizeof(AuthHash));

	memset(&hostaddr, 0, sizeof(hostaddr));
	get_ip("127.0.0.1", &hostaddr);

	domainname domain;
	MakeDomainNameFromDNSNameString(&domain, "localhost");

	mDNSMacOSXCreateEtcHostsEntry_ut(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, mDNSNULL, &newhosts);

	memset(&hostaddr, 0, sizeof(hostaddr));
	get_ip("0000:0000:0000:0000:0000:0000:0000:0001", &hostaddr);

	MakeDomainNameFromDNSNameString(&domain, "localhost");

	mDNSMacOSXCreateEtcHostsEntry_ut(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, mDNSNULL, &newhosts);

	memset(&hostaddr, 0, sizeof(hostaddr));
	get_ip("255.255.255.255", &hostaddr);

	MakeDomainNameFromDNSNameString(&domain, "broadcasthost");

	mDNSMacOSXCreateEtcHostsEntry_ut(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, mDNSNULL, &newhosts);

	memset(&hostaddr, 0, sizeof(hostaddr));
	get_ip("17.226.40.200", &hostaddr);

	MakeDomainNameFromDNSNameString(&domain, "cardinal2.apple.com");

	mDNSMacOSXCreateEtcHostsEntry_ut(&domain, (struct sockaddr *) &hostaddr, mDNSNULL, mDNSNULL, &newhosts);
	UpdateEtcHosts_ut(&newhosts);

	m->NextScheduledEvent = mDNS_TimeNow_NoLock(m);
	mDNS_Execute(m);

	return mStatus_NoError;
}