2 Unix SMB/CIFS implementation.
4 Copyright (C) Kai Blin 2012
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "torture/smbtorture.h"
23 #include "lib/addns/dns.h"
25 static struct dns_connection *setup_connection(struct torture_context *tctx)
28 struct dns_connection *conn;
30 err = dns_open_connection(getenv("DC_SERVER_IP"), DNS_TCP, tctx, &conn);
31 if (!ERR_DNS_IS_OK(err)) {
32 printf("Failed to open connection to DNS server\n");
39 static char *get_dns_domain(struct torture_context *tctx)
41 return strlower_talloc(tctx, getenv("REALM"));
44 static struct sockaddr_storage *str_to_sockaddr(TALLOC_CTX *mem_ctx, const char *ip_string)
46 struct sockaddr_storage *ss = talloc_zero(mem_ctx, struct sockaddr_storage);
53 ss->ss_family = AF_INET;
55 ret = inet_pton(AF_INET, ip_string, &(((struct sockaddr_in *)ss)->sin_addr));
63 static bool test_internal_dns_query_self(struct torture_context *tctx)
65 struct dns_connection *conn;
66 struct dns_request *req, *resp;
70 conn = setup_connection(tctx);
75 host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx));
80 err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req);
81 if (!ERR_DNS_IS_OK(err)) {
82 printf("Failed to create A record query\n");
86 err = dns_transaction(conn, conn, req, &resp);
87 if (!ERR_DNS_IS_OK(err)) {
88 printf("Failed to query DNS server\n");
92 if (dns_response_code(resp->flags) != DNS_NO_ERROR) {
93 printf("Query returned %u\n", dns_response_code(resp->flags));
97 /* FIXME: is there _any_ way to unmarshal the response to check this? */
102 static bool test_internal_dns_update_self(struct torture_context *tctx)
104 struct dns_connection *conn;
105 struct dns_update_request *req, *resp;
106 struct dns_rrec *rec = NULL;
109 struct sockaddr_storage *ss;
111 conn = setup_connection(tctx);
116 host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx));
121 err = dns_create_update(conn, get_dns_domain(tctx), &req);
122 if (!ERR_DNS_IS_OK(err)) {
123 printf("Failed to update packet\n");
127 ss = str_to_sockaddr(conn, getenv("DC_SERVER_IP"));
129 printf("Converting '%s' to sockaddr_storage failed\n", getenv("DC_SERVER_IP"));
133 err = dns_create_a_record(req, host, 300, ss, &rec);
134 if (!ERR_DNS_IS_OK(err)) {
135 printf("Failed to create A update record\n");
139 err = dns_add_rrec(req, rec, &req->num_updates, &req->updates);
140 if (!ERR_DNS_IS_OK(err)) {
141 printf("Failed to add A update record to update packet\n");
145 err = dns_update_transaction(conn, conn, req, &resp);
146 if (!ERR_DNS_IS_OK(err)) {
147 printf("Failed to send update\n");
151 if (dns_response_code(resp->flags) != DNS_REFUSED) {
152 printf("Update returned %u\n", dns_response_code(resp->flags));
156 /* FIXME: is there _any_ way to unmarshal the response to check this? */
161 static struct torture_suite *internal_dns_suite(TALLOC_CTX *ctx)
163 struct torture_suite *suite = torture_suite_create(ctx, "dns_internal");
165 suite->description = talloc_strdup(suite,
166 "Tests for the internal DNS server");
167 torture_suite_add_simple_test(suite, "queryself", test_internal_dns_query_self);
168 torture_suite_add_simple_test(suite, "updateself", test_internal_dns_update_self);
173 /* Silence silly compiler warning */
174 NTSTATUS torture_internal_dns_init(TALLOC_CTX *);
177 * DNS torture module initialization
179 NTSTATUS torture_internal_dns_init(TALLOC_CTX *ctx)
181 struct torture_suite *suite;
182 TALLOC_CTX *mem_ctx = talloc_autofree_context();
184 /* register internal DNS torture test cases */
185 suite = internal_dns_suite(mem_ctx);
186 if (!suite) return NT_STATUS_NO_MEMORY;
187 torture_register_suite(suite);