From 2af8129085042b51ac052653942116ad5998f701 Mon Sep 17 00:00:00 2001 From: Kai Blin Date: Mon, 10 Sep 2012 22:22:43 +0200 Subject: [PATCH] s4 dns: Add libaddns-based simple tests --- source4/selftest/tests.py | 2 + source4/torture/dns/dlz_bind9.c | 2 +- source4/torture/dns/internal_dns.c | 190 +++++++++++++++++++++++++++++++++++++ source4/torture/dns/wscript_build | 12 ++- 4 files changed, 203 insertions(+), 3 deletions(-) create mode 100644 source4/torture/dns/internal_dns.c diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py index b7b9d45abdc..575d61b6522 100755 --- a/source4/selftest/tests.py +++ b/source4/selftest/tests.py @@ -296,6 +296,8 @@ for f in sorted(os.listdir(os.path.join(samba4srcdir, "../pidl/tests"))): # DNS tests planpythontestsuite("fl2003dc", "samba.tests.dns") +for t in smb4torture_testsuites("dns_internal."): + plansmbtorturetestsuite(t, "dc:local", '//$SERVER/whavever') # Local tests for t in smb4torture_testsuites("dlz_bind9."): diff --git a/source4/torture/dns/dlz_bind9.c b/source4/torture/dns/dlz_bind9.c index 6372ca8df62..18d65a32689 100644 --- a/source4/torture/dns/dlz_bind9.c +++ b/source4/torture/dns/dlz_bind9.c @@ -138,7 +138,7 @@ static struct torture_suite *dlz_bind9_suite(TALLOC_CTX *ctx) /** * DNS torture module initialization */ -NTSTATUS torture_dns_init(void) +NTSTATUS torture_bind_dns_init(void) { struct torture_suite *suite; TALLOC_CTX *mem_ctx = talloc_autofree_context(); diff --git a/source4/torture/dns/internal_dns.c b/source4/torture/dns/internal_dns.c new file mode 100644 index 00000000000..b1cb9672e8b --- /dev/null +++ b/source4/torture/dns/internal_dns.c @@ -0,0 +1,190 @@ +/* + Unix SMB/CIFS implementation. + SMB torture tester + Copyright (C) Kai Blin 2012 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "includes.h" +#include "torture/smbtorture.h" +#include +#include "lib/addns/dns.h" + +static struct dns_connection *setup_connection(struct torture_context *tctx) +{ + DNS_ERROR err; + struct dns_connection *conn; + + err = dns_open_connection(getenv("DC_SERVER_IP"), DNS_TCP, tctx, &conn); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to open connection to DNS server\n"); + return NULL; + } + + return conn; +} + +static char *get_dns_domain(struct torture_context *tctx) +{ + return strlower_talloc(tctx, getenv("REALM")); +} + +static struct sockaddr_storage *str_to_sockaddr(TALLOC_CTX *mem_ctx, const char *ip_string) +{ + struct sockaddr_storage *ss = talloc_zero(mem_ctx, struct sockaddr_storage); + int ret; + + if (ss == NULL) { + return NULL; + } + + ss->ss_family = AF_INET; + + ret = inet_pton(AF_INET, ip_string, &(((struct sockaddr_in *)ss)->sin_addr)); + if (ret != 1) { + return NULL; + } + + return ss; +} + +static bool test_internal_dns_query_self(struct torture_context *tctx) +{ + struct dns_connection *conn; + struct dns_request *req, *resp; + char *host; + DNS_ERROR err; + + conn = setup_connection(tctx); + if (conn == NULL) { + return false; + } + + host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx)); + if (host == NULL) { + return false; + } + + err = dns_create_query(conn, host, QTYPE_A, DNS_CLASS_IN, &req); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to create A record query\n"); + return false; + } + + err = dns_transaction(conn, conn, req, &resp); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to query DNS server\n"); + return false; + } + + if (dns_response_code(resp->flags) != DNS_NO_ERROR) { + printf("Query returned %u\n", dns_response_code(resp->flags)); + return false; + } + + /* FIXME: is there _any_ way to unmarshal the response to check this? */ + + return true; +} + +static bool test_internal_dns_update_self(struct torture_context *tctx) +{ + struct dns_connection *conn; + struct dns_update_request *req, *resp; + struct dns_rrec *rec = NULL; + char *host; + DNS_ERROR err; + struct sockaddr_storage *ss; + + conn = setup_connection(tctx); + if (conn == NULL) { + return false; + } + + host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx)); + if (host == NULL) { + return false; + } + + err = dns_create_update(conn, get_dns_domain(tctx), &req); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to update packet\n"); + return false; + } + + ss = str_to_sockaddr(conn, getenv("DC_SERVER_IP")); + if (ss == NULL) { + printf("Converting '%s' to sockaddr_storage failed\n", getenv("DC_SERVER_IP")); + return false; + } + + err = dns_create_a_record(req, host, 300, ss, &rec); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to create A update record\n"); + return false; + } + + err = dns_add_rrec(req, rec, &req->num_updates, &req->updates); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to add A update record to update packet\n"); + return false; + } + + err = dns_update_transaction(conn, conn, req, &resp); + if (!ERR_DNS_IS_OK(err)) { + printf("Failed to send update\n"); + return false; + } + + if (dns_response_code(resp->flags) != DNS_REFUSED) { + printf("Update returned %u\n", dns_response_code(resp->flags)); + return false; + } + + /* FIXME: is there _any_ way to unmarshal the response to check this? */ + + return true; +} + +static struct torture_suite *internal_dns_suite(TALLOC_CTX *ctx) +{ + struct torture_suite *suite = torture_suite_create(ctx, "dns_internal"); + + suite->description = talloc_strdup(suite, + "Tests for the internal DNS server"); + torture_suite_add_simple_test(suite, "queryself", test_internal_dns_query_self); + torture_suite_add_simple_test(suite, "updateself", test_internal_dns_update_self); + return suite; +} + + +/* Silence silly compiler warning */ +NTSTATUS torture_internal_dns_init(void); + +/** + * DNS torture module initialization + */ +NTSTATUS torture_internal_dns_init(void) +{ + struct torture_suite *suite; + TALLOC_CTX *mem_ctx = talloc_autofree_context(); + + /* register internal DNS torture test cases */ + suite = internal_dns_suite(mem_ctx); + if (!suite) return NT_STATUS_NO_MEMORY; + torture_register_suite(suite); + + return NT_STATUS_OK; +} diff --git a/source4/torture/dns/wscript_build b/source4/torture/dns/wscript_build index 674a3a0c2bd..a6773831be9 100644 --- a/source4/torture/dns/wscript_build +++ b/source4/torture/dns/wscript_build @@ -1,11 +1,19 @@ #!/usr/bin/env python if bld.AD_DC_BUILD_IS_ENABLED(): - bld.SAMBA_MODULE('TORTURE_DNS', + bld.SAMBA_MODULE('TORTURE_BIND_DNS', source='dlz_bind9.c', subsystem='smbtorture', - init_function='torture_dns_init', + init_function='torture_bind_dns_init', cflags='-DBIND_VERSION_9_8', deps='torture talloc torturemain dlz_bind9_for_torture', internal_module=True ) + + bld.SAMBA_MODULE('TORTURE_INTERNAL_DNS', + source='internal_dns.c', + subsystem='smbtorture', + init_function='torture_internal_dns_init', + deps='torture talloc torturemain', + internal_module=True + ) -- 2.11.4.GIT