r23792: convert Samba4 to GPLv3
[samba.git] / source4 / torture / nbt / register.c
index a8be5dbb9a1f9ce7aee4f6e9c61e612eef3d35e4..03cc4e6ceefa008ac8c7cfcc613d858e27d5b414 100644 (file)
@@ -7,7 +7,7 @@
    
    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 2 of the License, or
+   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,
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
-#include "libcli/nbt/libnbt.h"
-#include "librpc/gen_ndr/ndr_nbt.h"
-
-#define CHECK_VALUE(v, correct) do { \
-       if ((v) != (correct)) { \
-               printf("(%s) Incorrect value %s=%d - should be %d\n", \
-                      __location__, #v, v, correct); \
-               ret = False; \
-       }} while (0)
-
-#define CHECK_STRING(v, correct) do { \
-       if (StrCaseCmp(v, correct) != 0) { \
-               printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
-                      __location__, #v, v, correct); \
-               ret = False; \
-       }} while (0)
+#include "lib/socket/socket.h"
+#include "libcli/resolve/resolve.h"
+#include "system/network.h"
+#include "lib/socket/netif.h"
+#include "torture/torture.h"
+#include "torture/nbt/proto.h"
+
+#define CHECK_VALUE(tctx, v, correct) \
+       torture_assert_int_equal(tctx, v, correct, "Incorrect value")
+
+#define CHECK_STRING(tctx, v, correct) \
+       torture_assert_casestr_equal(tctx, v, correct, "Incorrect value")
+
+
+
 
 /*
   test that a server responds correctly to attempted registrations of its name
 */
-static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
-                            const char *address)
+static bool nbt_register_own(struct torture_context *tctx)
 {
        struct nbt_name_register io;
        NTSTATUS status;
-       struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
-       BOOL ret = True;
-       const char *myaddress = iface_n_ip(0);
+       struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
+       struct socket_address *socket_address;
+       struct nbt_name name;
+       const char *address;
+       const char *myaddress;
+
+       if (!torture_nbt_get_name(tctx, &name, &address))
+               return false;
+
+       myaddress = iface_best_ip(address);
 
-       socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
+       socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
+                                                    myaddress, 0);
+       torture_assert(tctx, socket_address != NULL, "Unable to get address");
 
-       printf("Testing name defense to name registration\n");
+       status = socket_listen(nbtsock->sock, socket_address, 0, 0);
+       torture_assert_ntstatus_ok(tctx, status, 
+                               "socket_listen for nbt_register_own failed");
 
-       io.in.name = *name;
+       torture_comment(tctx, "Testing name defense to name registration\n");
+
+       io.in.name = name;
        io.in.dest_addr = address;
        io.in.address = myaddress;
        io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
@@ -65,61 +75,62 @@ static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
        io.in.timeout = 3;
        io.in.retries = 0;
        
-       status = nbt_name_register(nbtsock, mem_ctx, &io);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
-               printf("No response from %s for name register\n", address);
-               return False;
-       }
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("Bad response from %s for name register - %s\n",
-                      address, nt_errstr(status));
-               return False;
-       }
+       status = nbt_name_register(nbtsock, tctx, &io);
+       torture_assert_ntstatus_ok(tctx, status, 
+                               talloc_asprintf(tctx, "Bad response from %s for name register",
+                      address));
        
-       CHECK_STRING(io.out.name.name, name->name);
-       CHECK_VALUE(io.out.name.type, name->type);
-       CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
+       CHECK_STRING(tctx, io.out.name.name, name.name);
+       CHECK_VALUE(tctx, io.out.name.type, name.type);
+       CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
 
        /* check a register demand */
        io.in.address = myaddress;
        io.in.register_demand = True;
 
-       status = nbt_name_register(nbtsock, mem_ctx, &io);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
-               printf("No response from %s for name register\n", address);
-               return False;
-       }
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("Bad response from %s for name register - %s\n",
-                      address, nt_errstr(status));
-               return False;
-       }
+       status = nbt_name_register(nbtsock, tctx, &io);
+
+       torture_assert_ntstatus_ok(tctx, status, 
+                               talloc_asprintf(tctx, "Bad response from %s for name register demand", address));
        
-       CHECK_STRING(io.out.name.name, name->name);
-       CHECK_VALUE(io.out.name.type, name->type);
-       CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
+       CHECK_STRING(tctx, io.out.name.name, name.name);
+       CHECK_VALUE(tctx, io.out.name.type, name.type);
+       CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
 
-       return ret;
+       return true;
 }
 
 
 /*
   test that a server responds correctly to attempted name refresh requests
 */
-static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name, 
-                           const char *address)
+static bool nbt_refresh_own(struct torture_context *tctx)
 {
        struct nbt_name_refresh io;
        NTSTATUS status;
-       struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
-       BOOL ret = True;
-       const char *myaddress = iface_n_ip(0);
+       struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
+       const char *myaddress;
+       struct socket_address *socket_address;
+       struct nbt_name name;
+       const char *address;
+
+       if (!torture_nbt_get_name(tctx, &name, &address))
+               return false;
+       
+       myaddress = iface_best_ip(address);
 
-       socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
+       socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
+                                                    myaddress, 0);
+       torture_assert(tctx, socket_address != NULL, 
+                                  "Can't parse socket address");
 
-       printf("Testing name defense to name refresh\n");
+       status = socket_listen(nbtsock->sock, socket_address, 0, 0);
+       torture_assert_ntstatus_ok(tctx, status, 
+                                                          "socket_listen for nbt_referesh_own failed");
 
-       io.in.name = *name;
+       torture_comment(tctx, "Testing name defense to name refresh\n");
+
+       io.in.name = name;
        io.in.dest_addr = address;
        io.in.address = myaddress;
        io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
@@ -128,54 +139,29 @@ static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
        io.in.timeout = 3;
        io.in.retries = 0;
        
-       status = nbt_name_refresh(nbtsock, mem_ctx, &io);
-       if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
-               printf("No response from %s for name refresh\n", address);
-               return False;
-       }
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("Bad response from %s for name refresh - %s\n",
-                      address, nt_errstr(status));
-               return False;
-       }
+       status = nbt_name_refresh(nbtsock, tctx, &io);
+
+       torture_assert_ntstatus_ok(tctx, status, 
+                               talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
        
-       CHECK_STRING(io.out.name.name, name->name);
-       CHECK_VALUE(io.out.name.type, name->type);
-       CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
+       CHECK_STRING(tctx, io.out.name.name, name.name);
+       CHECK_VALUE(tctx, io.out.name.type, name.type);
+       CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);
 
-       return ret;
+       return true;
 }
 
 
-
 /*
   test name registration to a server
 */
-BOOL torture_nbt_register(void)
+struct torture_suite *torture_nbt_register(void)
 {
-       const char *address;
-       struct nbt_name name;
-       TALLOC_CTX *mem_ctx = talloc_new(NULL);
-       NTSTATUS status;
-       BOOL ret = True;
-       
-       name.name = lp_parm_string(-1, "torture", "host");
-       name.type = NBT_NAME_SERVER;
-       name.scope = NULL;
-
-       /* do an initial name resolution to find its IP */
-       status = resolve_name(&name, mem_ctx, &address);
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("Failed to resolve %s - %s\n",
-                      name.name, nt_errstr(status));
-               talloc_free(mem_ctx);
-               return False;
-       }
-
-       ret &= nbt_register_own(mem_ctx, &name, address);
-       ret &= nbt_refresh_own(mem_ctx, &name, address);
+       struct torture_suite *suite;
 
-       talloc_free(mem_ctx);
+       suite = torture_suite_create(talloc_autofree_context(), "REGISTER");
+       torture_suite_add_simple_test(suite, "register_own", nbt_register_own);
+       torture_suite_add_simple_test(suite, "refresh_own", nbt_refresh_own);
 
-       return ret;
+       return suite;
 }