1 /* $NetBSD: notify_test.c,v 1.3 2025/01/26 16:25: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/thread.h> 30 #include <isc/util.h> 31 32 #include <dns/acl.h> 33 #include <dns/rcode.h> 34 #include <dns/view.h> 35 36 #include <ns/client.h> 37 #include <ns/notify.h> 38 39 #include <tests/ns.h> 40 41 static void 42 check_response(isc_buffer_t *buf) { 43 isc_result_t result; 44 dns_message_t *message = NULL; 45 char rcodebuf[20]; 46 isc_buffer_t b; 47 48 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &message); 49 50 result = dns_message_parse(message, buf, 0); 51 assert_int_equal(result, ISC_R_SUCCESS); 52 53 isc_buffer_init(&b, rcodebuf, sizeof(rcodebuf)); 54 result = dns_rcode_totext(message->rcode, &b); 55 assert_int_equal(result, ISC_R_SUCCESS); 56 57 assert_int_equal(message->rcode, dns_rcode_noerror); 58 59 dns_message_detach(&message); 60 } 61 62 /* test ns_notify_start() */ 63 ISC_LOOP_TEST_IMPL(notify_start) { 64 isc_result_t result; 65 ns_client_t *client = NULL; 66 isc_nmhandle_t *handle = NULL; 67 dns_message_t *nmsg = NULL; 68 unsigned char ndata[4096]; 69 isc_buffer_t nbuf; 70 size_t nsize; 71 72 ns_test_getclient(NULL, false, &client); 73 74 result = dns_test_makeview("view", false, false, &client->view); 75 assert_int_equal(result, ISC_R_SUCCESS); 76 77 result = ns_test_serve_zone("example.com", 78 TESTS_DIR "/testdata/notify/zone1.db", 79 client->view); 80 assert_int_equal(result, ISC_R_SUCCESS); 81 82 /* 83 * Create a NOTIFY message by parsing a file in testdata. 84 * (XXX: use better message mocking method when available.) 85 */ 86 87 result = ns_test_getdata(TESTS_DIR "/testdata/notify/notify1.msg", 88 ndata, sizeof(ndata), &nsize); 89 assert_int_equal(result, ISC_R_SUCCESS); 90 isc_buffer_init(&nbuf, ndata, nsize); 91 isc_buffer_add(&nbuf, nsize); 92 93 dns_message_create(mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, &nmsg); 94 95 result = dns_message_parse(nmsg, &nbuf, 0); 96 assert_int_equal(result, ISC_R_SUCCESS); 97 98 /* 99 * Set up client object with this message and test the NOTIFY 100 * handler. 101 */ 102 if (client->message != NULL) { 103 dns_message_detach(&client->message); 104 } 105 client->message = nmsg; 106 nmsg = NULL; 107 client->sendcb = check_response; 108 ns_notify_start(client, client->handle); 109 110 /* 111 * Clean up 112 */ 113 ns_test_cleanup_zone(); 114 115 handle = client->handle; 116 isc_nmhandle_detach(&client->handle); 117 isc_nmhandle_detach(&handle); 118 119 isc_loop_teardown(mainloop, shutdown_interfacemgr, NULL); 120 isc_loopmgr_shutdown(loopmgr); 121 } 122 123 ISC_TEST_LIST_START 124 ISC_TEST_ENTRY_CUSTOM(notify_start, setup_server, teardown_server) 125 ISC_TEST_LIST_END 126 127 ISC_TEST_MAIN 128