Debian Codegraph @

tests/ns/notify_test.c

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