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