1/*	$NetBSD: notify_test.c,v 1.2 2024/02/21 22:52:51 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16#include <inttypes.h>
17#include <sched.h> /* IWYU pragma: keep */
18#include <setjmp.h>
19#include <stdarg.h>
20#include <stddef.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25
26#define UNIT_TESTING
27#include <cmocka.h>
28
29#include <isc/event.h>
30#include <isc/print.h>
31#include <isc/task.h>
32#include <isc/thread.h>
33#include <isc/util.h>
34
35#include <dns/acl.h>
36#include <dns/rcode.h>
37#include <dns/view.h>
38
39#include <ns/client.h>
40#include <ns/notify.h>
41
42#include <tests/dns.h>
43#include <tests/ns.h>
44
45static int
46setup_test(void **state) {
47	isc__nm_force_tid(0);
48	return (setup_server(state));
49}
50
51static int
52teardown_test(void **state) {
53	isc__nm_force_tid(-1);
54	return (teardown_server(state));
55}
56
57static void
58check_response(isc_buffer_t *buf) {
59	isc_result_t result;
60	dns_message_t *message = NULL;
61	char rcodebuf[20];
62	isc_buffer_t b;
63
64	dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &message);
65
66	result = dns_message_parse(message, buf, 0);
67	assert_int_equal(result, ISC_R_SUCCESS);
68
69	isc_buffer_init(&b, rcodebuf, sizeof(rcodebuf));
70	result = dns_rcode_totext(message->rcode, &b);
71	assert_int_equal(result, ISC_R_SUCCESS);
72
73	assert_int_equal(message->rcode, dns_rcode_noerror);
74
75	dns_message_detach(&message);
76}
77
78/* test ns_notify_start() */
79ISC_RUN_TEST_IMPL(ns_notify_start) {
80	isc_result_t result;
81	ns_client_t *client = NULL;
82	isc_nmhandle_t *handle = NULL;
83	dns_message_t *nmsg = NULL;
84	unsigned char ndata[4096];
85	isc_buffer_t nbuf;
86	size_t nsize;
87
88	UNUSED(state);
89
90	result = ns_test_getclient(NULL, false, &client);
91	assert_int_equal(result, ISC_R_SUCCESS);
92
93	result = dns_test_makeview("view", false, &client->view);
94	assert_int_equal(result, ISC_R_SUCCESS);
95
96	result = ns_test_serve_zone("example.com",
97				    TESTS_DIR "/testdata/notify/zone1.db",
98				    client->view);
99	assert_int_equal(result, ISC_R_SUCCESS);
100
101	/*
102	 * Create a NOTIFY message by parsing a file in testdata.
103	 * (XXX: use better message mocking method when available.)
104	 */
105
106	result = ns_test_getdata(TESTS_DIR "/testdata/notify/notify1.msg",
107				 ndata, sizeof(ndata), &nsize);
108	assert_int_equal(result, ISC_R_SUCCESS);
109	isc_buffer_init(&nbuf, ndata, nsize);
110	isc_buffer_add(&nbuf, nsize);
111
112	dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &nmsg);
113
114	result = dns_message_parse(nmsg, &nbuf, 0);
115	assert_int_equal(result, ISC_R_SUCCESS);
116
117	/*
118	 * Set up client object with this message and test the NOTIFY
119	 * handler.
120	 */
121	if (client->message != NULL) {
122		dns_message_detach(&client->message);
123	}
124	client->message = nmsg;
125	nmsg = NULL;
126	client->sendcb = check_response;
127	ns_notify_start(client, client->handle);
128
129	/*
130	 * Clean up
131	 */
132	ns_test_cleanup_zone();
133
134	handle = client->handle;
135	isc_nmhandle_detach(&client->handle);
136	isc_nmhandle_detach(&handle);
137}
138
139ISC_TEST_LIST_START
140ISC_TEST_ENTRY_CUSTOM(ns_notify_start, setup_test, teardown_test)
141ISC_TEST_LIST_END
142
143ISC_TEST_MAIN
144