r5451: - added separate wrepl_associate(), wrepl_pull_table() and wrepl_pull_names...
[kai/samba.git] / source4 / torture / nbt / winsreplication.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    WINS replication testing
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "libcli/nbt/libnbt.h"
25 #include "libcli/wins/winsrepl.h"
26
27 #define CHECK_STATUS(status, correct) do { \
28         if (!NT_STATUS_EQUAL(status, correct)) { \
29                 printf("(%s) Incorrect status %s - should be %s\n", \
30                        __location__, nt_errstr(status), nt_errstr(correct)); \
31                 ret = False; \
32                 goto done; \
33         }} while (0)
34
35 #define CHECK_VALUE(v, correct) do { \
36         if ((v) != (correct)) { \
37                 printf("(%s) Incorrect value %s=%d - should be %d\n", \
38                        __location__, #v, v, correct); \
39                 ret = False; \
40                 goto done; \
41         }} while (0)
42
43 /*
44   display a replication entry
45 */
46 static void display_entry(TALLOC_CTX *mem_ctx, struct wrepl_name *name)
47 {
48         int i;
49
50         printf("%s\n", nbt_name_string(mem_ctx, &name->name));
51         for (i=0;i<name->num_addresses;i++) {
52                 printf("\t%s %s\n", 
53                        name->addresses[i].owner, name->addresses[i].address);
54         }
55 }
56
57 /*
58   test a full replication dump from a WINS server
59 */
60 static BOOL nbt_test_wins_replication(TALLOC_CTX *mem_ctx, const char *address)
61 {
62         BOOL ret = True;
63         struct wrepl_socket *wrepl_socket;
64         NTSTATUS status;
65         int i, j;
66         struct wrepl_associate associate;
67         struct wrepl_pull_table pull_table;
68         struct wrepl_pull_names pull_names;
69
70         wrepl_socket = wrepl_socket_init(mem_ctx, NULL);
71         
72         status = wrepl_connect(wrepl_socket, address);
73         CHECK_STATUS(status, NT_STATUS_OK);
74
75         printf("Send a start association request\n");
76
77         status = wrepl_associate(wrepl_socket, &associate);
78         CHECK_STATUS(status, NT_STATUS_OK);
79
80         printf("association context: 0x%x\n", associate.out.assoc_ctx);
81
82         printf("Send a replication table query\n");
83         pull_table.in.assoc_ctx = associate.out.assoc_ctx;
84
85         status = wrepl_pull_table(wrepl_socket, mem_ctx, &pull_table);
86         CHECK_STATUS(status, NT_STATUS_OK);
87
88         printf("Found %d replication partners\n", pull_table.out.num_partners);
89
90         for (i=0;i<pull_table.out.num_partners;i++) {
91                 struct wrepl_wins_owner *partner = &pull_table.out.partners[i];
92                 printf("%s   max_version=%6llu   min_version=%6llu type=%d\n",
93                        partner->address, 
94                        partner->max_version, 
95                        partner->min_version, 
96                        partner->type);
97
98                 pull_names.in.assoc_ctx = associate.out.assoc_ctx;
99                 pull_names.in.partner = *partner;
100                 
101                 status = wrepl_pull_names(wrepl_socket, mem_ctx, &pull_names);
102                 CHECK_STATUS(status, NT_STATUS_OK);
103
104                 printf("Received %d names\n", pull_names.out.num_names);
105
106                 for (j=0;j<pull_names.out.num_names;j++) {
107                         display_entry(mem_ctx, &pull_names.out.names[j]);
108                 }
109         }
110
111 done:
112         talloc_free(wrepl_socket);
113         return ret;
114 }
115
116 /*
117   test WINS replication operations
118 */
119 BOOL torture_nbt_winsreplication(void)
120 {
121         const char *address;
122         struct nbt_name name;
123         TALLOC_CTX *mem_ctx = talloc_new(NULL);
124         NTSTATUS status;
125         BOOL ret = True;
126         
127         name.name = lp_parm_string(-1, "torture", "host");
128         name.type = NBT_NAME_SERVER;
129         name.scope = NULL;
130
131         /* do an initial name resolution to find its IP */
132         status = resolve_name(&name, mem_ctx, &address);
133         if (!NT_STATUS_IS_OK(status)) {
134                 printf("Failed to resolve %s - %s\n",
135                        name.name, nt_errstr(status));
136                 talloc_free(mem_ctx);
137                 return False;
138         }
139
140         ret &= nbt_test_wins_replication(mem_ctx, address);
141
142         talloc_free(mem_ctx);
143
144         return ret;
145 }