param: replace P_OCTAL variable setting with s3 version which uses sscanf
[Samba.git] / source4 / torture / dns / internal_dns.c
blobb1cb9672e8b4ed986a8592a27bdc2ef2cf88c9cd
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
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/>.
20 #include "includes.h"
21 #include "torture/smbtorture.h"
22 #include <talloc.h>
23 #include "lib/addns/dns.h"
25 static struct dns_connection *setup_connection(struct torture_context *tctx)
27 DNS_ERROR err;
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");
33 return NULL;
36 return conn;
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);
47 int ret;
49 if (ss == NULL) {
50 return NULL;
53 ss->ss_family = AF_INET;
55 ret = inet_pton(AF_INET, ip_string, &(((struct sockaddr_in *)ss)->sin_addr));
56 if (ret != 1) {
57 return NULL;
60 return ss;
63 static bool test_internal_dns_query_self(struct torture_context *tctx)
65 struct dns_connection *conn;
66 struct dns_request *req, *resp;
67 char *host;
68 DNS_ERROR err;
70 conn = setup_connection(tctx);
71 if (conn == NULL) {
72 return false;
75 host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx));
76 if (host == NULL) {
77 return false;
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");
83 return false;
86 err = dns_transaction(conn, conn, req, &resp);
87 if (!ERR_DNS_IS_OK(err)) {
88 printf("Failed to query DNS server\n");
89 return false;
92 if (dns_response_code(resp->flags) != DNS_NO_ERROR) {
93 printf("Query returned %u\n", dns_response_code(resp->flags));
94 return false;
97 /* FIXME: is there _any_ way to unmarshal the response to check this? */
99 return true;
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;
107 char *host;
108 DNS_ERROR err;
109 struct sockaddr_storage *ss;
111 conn = setup_connection(tctx);
112 if (conn == NULL) {
113 return false;
116 host = talloc_asprintf(tctx, "%s.%s", getenv("DC_SERVER"), get_dns_domain(tctx));
117 if (host == NULL) {
118 return false;
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");
124 return false;
127 ss = str_to_sockaddr(conn, getenv("DC_SERVER_IP"));
128 if (ss == NULL) {
129 printf("Converting '%s' to sockaddr_storage failed\n", getenv("DC_SERVER_IP"));
130 return false;
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");
136 return false;
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");
142 return false;
145 err = dns_update_transaction(conn, conn, req, &resp);
146 if (!ERR_DNS_IS_OK(err)) {
147 printf("Failed to send update\n");
148 return false;
151 if (dns_response_code(resp->flags) != DNS_REFUSED) {
152 printf("Update returned %u\n", dns_response_code(resp->flags));
153 return false;
156 /* FIXME: is there _any_ way to unmarshal the response to check this? */
158 return true;
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);
169 return suite;
173 /* Silence silly compiler warning */
174 NTSTATUS torture_internal_dns_init(void);
177 * DNS torture module initialization
179 NTSTATUS torture_internal_dns_init(void)
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);
189 return NT_STATUS_OK;