r15456: Inspired by a short discussion with abartlet on IRC.
[samba.git] / source4 / torture / torture.c
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB torture tester
4    Copyright (C) Andrew Tridgell 1997-2003
5    Copyright (C) Jelmer Vernooij 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "lib/cmdline/popt_common.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "system/time.h"
26 #include "system/wait.h"
27 #include "system/filesys.h"
28 #include "libcli/raw/ioctl.h"
29 #include "libcli/libcli.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "lib/events/events.h"
32 #include "libcli/resolve/resolve.h"
33 #include "auth/credentials/credentials.h"
34 #include "libcli/ldap/ldap_client.h"
35 #include "librpc/gen_ndr/ndr_nbt.h"
36 #include "torture/torture.h"
37
38 #include "torture/raw/proto.h"
39 #include "libcli/smb2/smb2.h"
40 #include "torture/smb2/proto.h"
41 #include "torture/rap/proto.h"
42 #include "torture/auth/proto.h"
43 #include "torture/local/proto.h"
44 #include "torture/nbench/proto.h"
45 #include "torture/ldap/proto.h"
46 #include "torture/com/proto.h"
47 #include "torture/nbt/proto.h"
48 #include "torture/libnet/proto.h"
49 #include "torture/util.h"
50 #include "build.h"
51 #include "dlinklist.h"
52
53 _PUBLIC_ int torture_nprocs=4;
54 _PUBLIC_ int torture_numops=10;
55 _PUBLIC_ int torture_entries=1000;
56 _PUBLIC_ int torture_failures=1;
57 _PUBLIC_ int torture_seed=0;
58 _PUBLIC_ int torture_numasync=100;
59 _PUBLIC_ BOOL use_oplocks;
60 static int procnum; /* records process count number when forking */
61 static struct smbcli_state *current_cli;
62 _PUBLIC_ BOOL use_level_II_oplocks;
63 _PUBLIC_ BOOL torture_showall = False;
64
65
66
67
68 static BOOL wait_lock(struct smbcli_state *c, int fnum, uint32_t offset, uint32_t len)
69 {
70         while (NT_STATUS_IS_ERR(smbcli_lock(c->tree, fnum, offset, len, -1, WRITE_LOCK))) {
71                 if (!check_error(__location__, c, ERRDOS, ERRlock, NT_STATUS_LOCK_NOT_GRANTED)) return False;
72         }
73         return True;
74 }
75
76
77 static BOOL rw_torture(struct smbcli_state *c)
78 {
79         const char *lockfname = "\\torture.lck";
80         char *fname;
81         int fnum;
82         int fnum2;
83         pid_t pid2, pid = getpid();
84         int i, j;
85         uint8_t buf[1024];
86         BOOL correct = True;
87
88         fnum2 = smbcli_open(c->tree, lockfname, O_RDWR | O_CREAT | O_EXCL, 
89                          DENY_NONE);
90         if (fnum2 == -1)
91                 fnum2 = smbcli_open(c->tree, lockfname, O_RDWR, DENY_NONE);
92         if (fnum2 == -1) {
93                 printf("open of %s failed (%s)\n", lockfname, smbcli_errstr(c->tree));
94                 return False;
95         }
96
97
98         for (i=0;i<torture_numops;i++) {
99                 uint_t n = (uint_t)random()%10;
100                 if (i % 10 == 0) {
101                         printf("%d\r", i); fflush(stdout);
102                 }
103                 asprintf(&fname, "\\torture.%u", n);
104
105                 if (!wait_lock(c, fnum2, n*sizeof(int), sizeof(int))) {
106                         return False;
107                 }
108
109                 fnum = smbcli_open(c->tree, fname, O_RDWR | O_CREAT | O_TRUNC, DENY_ALL);
110                 if (fnum == -1) {
111                         printf("open failed (%s)\n", smbcli_errstr(c->tree));
112                         correct = False;
113                         break;
114                 }
115
116                 if (smbcli_write(c->tree, fnum, 0, &pid, 0, sizeof(pid)) != sizeof(pid)) {
117                         printf("write failed (%s)\n", smbcli_errstr(c->tree));
118                         correct = False;
119                 }
120
121                 for (j=0;j<50;j++) {
122                         if (smbcli_write(c->tree, fnum, 0, buf, 
123                                       sizeof(pid)+(j*sizeof(buf)), 
124                                       sizeof(buf)) != sizeof(buf)) {
125                                 printf("write failed (%s)\n", smbcli_errstr(c->tree));
126                                 correct = False;
127                         }
128                 }
129
130                 pid2 = 0;
131
132                 if (smbcli_read(c->tree, fnum, &pid2, 0, sizeof(pid)) != sizeof(pid)) {
133                         printf("read failed (%s)\n", smbcli_errstr(c->tree));
134                         correct = False;
135                 }
136
137                 if (pid2 != pid) {
138                         printf("data corruption!\n");
139                         correct = False;
140                 }
141
142                 if (NT_STATUS_IS_ERR(smbcli_close(c->tree, fnum))) {
143                         printf("close failed (%s)\n", smbcli_errstr(c->tree));
144                         correct = False;
145                 }
146
147                 if (NT_STATUS_IS_ERR(smbcli_unlink(c->tree, fname))) {
148                         printf("unlink failed (%s)\n", smbcli_errstr(c->tree));
149                         correct = False;
150                 }
151
152                 if (NT_STATUS_IS_ERR(smbcli_unlock(c->tree, fnum2, n*sizeof(int), sizeof(int)))) {
153                         printf("unlock failed (%s)\n", smbcli_errstr(c->tree));
154                         correct = False;
155                 }
156                 free(fname);
157         }
158
159         smbcli_close(c->tree, fnum2);
160         smbcli_unlink(c->tree, lockfname);
161
162         printf("%d\n", i);
163
164         return correct;
165 }
166
167 static BOOL run_torture(struct smbcli_state *cli, int dummy)
168 {
169     BOOL ret;
170
171         ret = rw_torture(cli);
172         
173         if (!torture_close_connection(cli)) {
174                 ret = False;
175         }
176
177         return ret;
178 }
179
180
181 /*
182   see how many RPC pipes we can open at once
183 */
184 static BOOL run_pipe_number(struct torture_context *torture)
185 {
186         struct smbcli_state *cli1;
187         const char *pipe_name = "\\WKSSVC";
188         int fnum;
189         int num_pipes = 0;
190
191         printf("starting pipenumber test\n");
192         if (!torture_open_connection(&cli1)) {
193                 return False;
194         }
195
196         while(1) {
197                 fnum = smbcli_nt_create_full(cli1->tree, pipe_name, 0, SEC_FILE_READ_DATA, FILE_ATTRIBUTE_NORMAL,
198                                    NTCREATEX_SHARE_ACCESS_READ|NTCREATEX_SHARE_ACCESS_WRITE, NTCREATEX_DISP_OPEN_IF, 0, 0);
199
200                 if (fnum == -1) {
201                         printf("Open of pipe %s failed with error (%s)\n", pipe_name, smbcli_errstr(cli1->tree));
202                         break;
203                 }
204                 num_pipes++;
205                 printf("%d\r", num_pipes);
206                 fflush(stdout);
207         }
208
209         printf("pipe_number test - we can open %d %s pipes.\n", num_pipes, pipe_name );
210         torture_close_connection(cli1);
211         return True;
212 }
213
214
215
216
217 /*
218   open N connections to the server and just hold them open
219   used for testing performance when there are N idle users
220   already connected
221  */
222  static BOOL torture_holdcon(struct torture_context *torture)
223 {
224         int i;
225         struct smbcli_state **cli;
226         int num_dead = 0;
227
228         printf("Opening %d connections\n", torture_numops);
229         
230         cli = malloc_array_p(struct smbcli_state *, torture_numops);
231
232         for (i=0;i<torture_numops;i++) {
233                 if (!torture_open_connection(&cli[i])) {
234                         return False;
235                 }
236                 printf("opened %d connections\r", i);
237                 fflush(stdout);
238         }
239
240         printf("\nStarting pings\n");
241
242         while (1) {
243                 for (i=0;i<torture_numops;i++) {
244                         NTSTATUS status;
245                         if (cli[i]) {
246                                 status = smbcli_chkpath(cli[i]->tree, "\\");
247                                 if (!NT_STATUS_IS_OK(status)) {
248                                         printf("Connection %d is dead\n", i);
249                                         cli[i] = NULL;
250                                         num_dead++;
251                                 }
252                                 usleep(100);
253                         }
254                 }
255
256                 if (num_dead == torture_numops) {
257                         printf("All connections dead - finishing\n");
258                         break;
259                 }
260
261                 printf(".");
262                 fflush(stdout);
263         }
264
265         return True;
266 }
267
268 /*
269 test how many open files this server supports on the one socket
270 */
271 static BOOL run_maxfidtest(struct smbcli_state *cli, int dummy)
272 {
273 #define MAXFID_TEMPLATE "\\maxfid\\fid%d\\maxfid.%d.%d"
274         char *fname;
275         int fnums[0x11000], i;
276         int retries=4, maxfid;
277         BOOL correct = True;
278
279         if (retries <= 0) {
280                 printf("failed to connect\n");
281                 return False;
282         }
283
284         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
285                 printf("Failed to deltree \\maxfid - %s\n",
286                        smbcli_errstr(cli->tree));
287                 return False;
288         }
289         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, "\\maxfid"))) {
290                 printf("Failed to mkdir \\maxfid, error=%s\n", 
291                        smbcli_errstr(cli->tree));
292                 return False;
293         }
294
295         printf("Testing maximum number of open files\n");
296
297         for (i=0; i<0x11000; i++) {
298                 if (i % 1000 == 0) {
299                         asprintf(&fname, "\\maxfid\\fid%d", i/1000);
300                         if (NT_STATUS_IS_ERR(smbcli_mkdir(cli->tree, fname))) {
301                                 printf("Failed to mkdir %s, error=%s\n", 
302                                        fname, smbcli_errstr(cli->tree));
303                                 return False;
304                         }
305                         free(fname);
306                 }
307                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
308                 if ((fnums[i] = smbcli_open(cli->tree, fname, 
309                                         O_RDWR|O_CREAT|O_TRUNC, DENY_NONE)) ==
310                     -1) {
311                         printf("open of %s failed (%s)\n", 
312                                fname, smbcli_errstr(cli->tree));
313                         printf("maximum fnum is %d\n", i);
314                         break;
315                 }
316                 free(fname);
317                 printf("%6d\r", i);
318         }
319         printf("%6d\n", i);
320         i--;
321
322         maxfid = i;
323
324         printf("cleaning up\n");
325         for (i=0;i<maxfid/2;i++) {
326                 asprintf(&fname, MAXFID_TEMPLATE, i/1000, i,(int)getpid());
327                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[i]))) {
328                         printf("Close of fnum %d failed - %s\n", fnums[i], smbcli_errstr(cli->tree));
329                 }
330                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
331                         printf("unlink of %s failed (%s)\n", 
332                                fname, smbcli_errstr(cli->tree));
333                         correct = False;
334                 }
335                 free(fname);
336
337                 asprintf(&fname, MAXFID_TEMPLATE, (maxfid-i)/1000, maxfid-i,(int)getpid());
338                 if (NT_STATUS_IS_ERR(smbcli_close(cli->tree, fnums[maxfid-i]))) {
339                         printf("Close of fnum %d failed - %s\n", fnums[maxfid-i], smbcli_errstr(cli->tree));
340                 }
341                 if (NT_STATUS_IS_ERR(smbcli_unlink(cli->tree, fname))) {
342                         printf("unlink of %s failed (%s)\n", 
343                                fname, smbcli_errstr(cli->tree));
344                         correct = False;
345                 }
346                 free(fname);
347
348                 printf("%6d %6d\r", i, maxfid-i);
349         }
350         printf("%6d\n", 0);
351
352         if (smbcli_deltree(cli->tree, "\\maxfid") == -1) {
353                 printf("Failed to deltree \\maxfid - %s\n",
354                        smbcli_errstr(cli->tree));
355                 return False;
356         }
357
358         printf("maxfid test finished\n");
359         if (!torture_close_connection(cli)) {
360                 correct = False;
361         }
362         return correct;
363 #undef MAXFID_TEMPLATE
364 }
365
366
367
368 /*
369   sees what IOCTLs are supported
370  */
371 static BOOL torture_ioctl_test(struct torture_context *torture)
372 {
373         struct smbcli_state *cli;
374         uint16_t device, function;
375         int fnum;
376         const char *fname = "\\ioctl.dat";
377         NTSTATUS status;
378         union smb_ioctl parms;
379         TALLOC_CTX *mem_ctx;
380
381         if (!torture_open_connection(&cli)) {
382                 return False;
383         }
384
385         mem_ctx = talloc_init("ioctl_test");
386
387         printf("starting ioctl test\n");
388
389         smbcli_unlink(cli->tree, fname);
390
391         fnum = smbcli_open(cli->tree, fname, O_RDWR|O_CREAT|O_EXCL, DENY_NONE);
392         if (fnum == -1) {
393                 printf("open of %s failed (%s)\n", fname, smbcli_errstr(cli->tree));
394                 return False;
395         }
396
397         parms.ioctl.level = RAW_IOCTL_IOCTL;
398         parms.ioctl.in.file.fnum = fnum;
399         parms.ioctl.in.request = IOCTL_QUERY_JOB_INFO;
400         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
401         printf("ioctl job info: %s\n", smbcli_errstr(cli->tree));
402
403         for (device=0;device<0x100;device++) {
404                 printf("testing device=0x%x\n", device);
405                 for (function=0;function<0x100;function++) {
406                         parms.ioctl.in.request = (device << 16) | function;
407                         status = smb_raw_ioctl(cli->tree, mem_ctx, &parms);
408
409                         if (NT_STATUS_IS_OK(status)) {
410                                 printf("ioctl device=0x%x function=0x%x OK : %d bytes\n", 
411                                         device, function, (int)parms.ioctl.out.blob.length);
412                         }
413                 }
414         }
415
416         if (!torture_close_connection(cli)) {
417                 return False;
418         }
419
420         return True;
421 }
422
423
424 static void sigcont(int sig)
425 {
426 }
427
428 double torture_create_procs(BOOL (*fn)(struct smbcli_state *, int), BOOL *result)
429 {
430         int i, status;
431         volatile pid_t *child_status;
432         volatile BOOL *child_status_out;
433         int synccount;
434         int tries = 8;
435         double start_time_limit = 10 + (torture_nprocs * 1.5);
436         char **unc_list = NULL;
437         const char *p;
438         int num_unc_names = 0;
439         struct timeval tv;
440
441         *result = True;
442
443         synccount = 0;
444
445         signal(SIGCONT, sigcont);
446
447         child_status = (volatile pid_t *)shm_setup(sizeof(pid_t)*torture_nprocs);
448         if (!child_status) {
449                 printf("Failed to setup shared memory\n");
450                 return -1;
451         }
452
453         child_status_out = (volatile BOOL *)shm_setup(sizeof(BOOL)*torture_nprocs);
454         if (!child_status_out) {
455                 printf("Failed to setup result status shared memory\n");
456                 return -1;
457         }
458
459         p = lp_parm_string(-1, "torture", "unclist");
460         if (p) {
461                 unc_list = file_lines_load(p, &num_unc_names, NULL);
462                 if (!unc_list || num_unc_names <= 0) {
463                         printf("Failed to load unc names list from '%s'\n", p);
464                         exit(1);
465                 }
466         }
467
468         for (i = 0; i < torture_nprocs; i++) {
469                 child_status[i] = 0;
470                 child_status_out[i] = True;
471         }
472
473         tv = timeval_current();
474
475         for (i=0;i<torture_nprocs;i++) {
476                 procnum = i;
477                 if (fork() == 0) {
478                         char *myname;
479                         char *hostname=NULL, *sharename;
480
481                         pid_t mypid = getpid();
482                         srandom(((int)mypid) ^ ((int)time(NULL)));
483
484                         asprintf(&myname, "CLIENT%d", i);
485                         lp_set_cmdline("netbios name", myname);
486                         free(myname);
487
488
489                         if (unc_list) {
490                                 if (!smbcli_parse_unc(unc_list[i % num_unc_names],
491                                                       NULL, &hostname, &sharename)) {
492                                         printf("Failed to parse UNC name %s\n",
493                                                unc_list[i % num_unc_names]);
494                                         exit(1);
495                                 }
496                         }
497
498                         while (1) {
499                                 if (hostname) {
500                                         if (torture_open_connection_share(NULL,
501                                                                           &current_cli,
502                                                                           hostname, 
503                                                                           sharename,
504                                                                           NULL)) {
505                                                 break;
506                                         }
507                                 } else if (torture_open_connection(&current_cli)) {
508                                                 break;
509                                 }
510                                 if (tries-- == 0) {
511                                         printf("pid %d failed to start\n", (int)getpid());
512                                         _exit(1);
513                                 }
514                                 msleep(100);    
515                         }
516
517                         child_status[i] = getpid();
518
519                         pause();
520
521                         if (child_status[i]) {
522                                 printf("Child %d failed to start!\n", i);
523                                 child_status_out[i] = 1;
524                                 _exit(1);
525                         }
526
527                         child_status_out[i] = fn(current_cli, i);
528                         _exit(0);
529                 }
530         }
531
532         do {
533                 synccount = 0;
534                 for (i=0;i<torture_nprocs;i++) {
535                         if (child_status[i]) synccount++;
536                 }
537                 if (synccount == torture_nprocs) break;
538                 msleep(100);
539         } while (timeval_elapsed(&tv) < start_time_limit);
540
541         if (synccount != torture_nprocs) {
542                 printf("FAILED TO START %d CLIENTS (started %d)\n", torture_nprocs, synccount);
543                 *result = False;
544                 return timeval_elapsed(&tv);
545         }
546
547         printf("Starting %d clients\n", torture_nprocs);
548
549         /* start the client load */
550         tv = timeval_current();
551         for (i=0;i<torture_nprocs;i++) {
552                 child_status[i] = 0;
553         }
554
555         printf("%d clients started\n", torture_nprocs);
556
557         kill(0, SIGCONT);
558
559         for (i=0;i<torture_nprocs;i++) {
560                 int ret;
561                 while ((ret=waitpid(0, &status, 0)) == -1 && errno == EINTR) /* noop */ ;
562                 if (ret == -1 || WEXITSTATUS(status) != 0) {
563                         *result = False;
564                 }
565         }
566
567         printf("\n");
568         
569         for (i=0;i<torture_nprocs;i++) {
570                 if (!child_status_out[i]) {
571                         *result = False;
572                 }
573         }
574         return timeval_elapsed(&tv);
575 }
576
577 #define FLAG_MULTIPROC 1
578
579 static struct {
580         const char *name;
581         BOOL (*fn)(struct torture_context *);
582         BOOL (*multi_fn)(struct smbcli_state *, int );
583 } builtin_torture_ops[] = {
584         /* benchmarking tests */
585         {"BENCH-HOLDCON",  torture_holdcon, 0},
586         {"BENCH-NBENCH",  torture_nbench, 0},
587         {"BENCH-TORTURE", NULL, run_torture},
588         {"BENCH-NBT",     torture_bench_nbt, 0},
589         {"BENCH-WINS",    torture_bench_wins, 0},
590         {"BENCH-CLDAP",   torture_bench_cldap, 0},
591         {"BENCH-OPLOCK",   torture_bench_oplock, 0},
592
593         /* RAW smb tests */
594         {"RAW-QFSINFO", torture_raw_qfsinfo, 0},
595         {"RAW-QFILEINFO", torture_raw_qfileinfo, 0},
596         {"RAW-SFILEINFO", torture_raw_sfileinfo, 0},
597         {"RAW-SFILEINFO-BUG", torture_raw_sfileinfo_bug, 0},
598         {"RAW-SEARCH", torture_raw_search, 0},
599         {"RAW-CLOSE", torture_raw_close, 0},
600         {"RAW-OPEN", torture_raw_open, 0},
601         {"RAW-MKDIR", torture_raw_mkdir, 0},
602         {"RAW-OPLOCK", torture_raw_oplock, 0},
603         {"RAW-NOTIFY", torture_raw_notify, 0},
604         {"RAW-MUX", torture_raw_mux, 0},
605         {"RAW-IOCTL", torture_raw_ioctl, 0},
606         {"RAW-CHKPATH", torture_raw_chkpath, 0},
607         {"RAW-UNLINK", torture_raw_unlink, 0},
608         {"RAW-READ", torture_raw_read, 0},
609         {"RAW-WRITE", torture_raw_write, 0},
610         {"RAW-LOCK", torture_raw_lock, 0},
611         {"RAW-CONTEXT", torture_raw_context, 0},
612         {"RAW-RENAME", torture_raw_rename, 0},
613         {"RAW-SEEK", torture_raw_seek, 0},
614         {"RAW-EAS", torture_raw_eas, 0},
615         {"RAW-STREAMS", torture_raw_streams, 0},
616         {"RAW-ACLS", torture_raw_acls, 0},
617         {"RAW-COMPOSITE", torture_raw_composite, 0},
618
619         /* SMB2 tests */
620         {"SMB2-CONNECT", torture_smb2_connect, 0},
621         {"SMB2-SCAN", torture_smb2_scan, 0},
622         {"SMB2-SCANGETINFO", torture_smb2_getinfo_scan, 0},
623         {"SMB2-SCANSETINFO", torture_smb2_setinfo_scan, 0},
624         {"SMB2-SCANFIND", torture_smb2_find_scan, 0},
625         {"SMB2-GETINFO", torture_smb2_getinfo, 0},
626         {"SMB2-SETINFO", torture_smb2_setinfo, 0},
627         {"SMB2-FIND", torture_smb2_find, 0},
628
629         /* RAP tests */
630         {"RAP-BASIC", torture_rap_basic, 0},
631
632         /* protocol scanners */
633         {"SCAN-MAXFID", NULL, run_maxfidtest},
634         {"SCAN-PIPE_NUMBER", run_pipe_number, 0},
635         {"SCAN-IOCTL",  torture_ioctl_test, 0},
636         {"SCAN-RAP",  torture_rap_scan, 0},
637         {"SCAN-EAMAX", torture_max_eas, 0},
638
639         /* local (no server) testers */
640         {"LOCAL-NTLMSSP", torture_ntlmssp_self_check, 0},
641         {"LOCAL-ICONV", torture_local_iconv, 0},
642         {"LOCAL-TALLOC", torture_local_talloc, 0},
643         {"LOCAL-MESSAGING", torture_local_messaging, 0},
644         {"LOCAL-IRPC",  torture_local_irpc, 0},
645         {"LOCAL-BINDING", torture_local_binding_string, 0},
646         {"LOCAL-STRLIST", torture_local_util_strlist, 0},
647         {"LOCAL-FILE", torture_local_util_file, 0},
648         {"LOCAL-IDTREE", torture_local_idtree, 0},
649         {"LOCAL-SOCKET", torture_local_socket, 0},
650         {"LOCAL-PAC", torture_pac, 0},
651         {"LOCAL-REGISTRY", torture_registry, 0},
652         {"LOCAL-RESOLVE", torture_local_resolve, 0},
653         {"LOCAL-SDDL", torture_local_sddl, 0},
654         {"LOCAL-NDR", torture_local_ndr, 0},
655
656         /* ldap testers */
657         {"LDAP-BASIC", torture_ldap_basic, 0},
658         {"LDAP-CLDAP", torture_cldap, 0},
659
660         /* nbt tests */
661         {"NBT-REGISTER", torture_nbt_register, 0},
662         {"NBT-WINS", torture_nbt_wins, 0},
663         {"NBT-DGRAM", torture_nbt_dgram, 0},
664         {"NBT-BROWSE", torture_nbt_browse, 0},
665         {"NBT-WINSREPLICATION-SIMPLE", torture_nbt_winsreplication_simple, 0},
666         {"NBT-WINSREPLICATION-REPLICA", torture_nbt_winsreplication_replica, 0},
667         {"NBT-WINSREPLICATION-OWNED", torture_nbt_winsreplication_owned, 0},
668         
669         {NULL, NULL, 0}};
670
671 static void register_builtin_ops(void)
672 {
673         int i;
674         for (i = 0; builtin_torture_ops[i].name; i++) {
675                 register_torture_op(builtin_torture_ops[i].name, 
676                                                         builtin_torture_ops[i].fn, 
677                                                         builtin_torture_ops[i].multi_fn);
678         }
679 }
680
681 struct torture_op *torture_ops = NULL;
682
683 _PUBLIC_ NTSTATUS register_torture_op(const char *name, BOOL (*fn)(struct torture_context *), BOOL (*multi_fn)(struct smbcli_state *, int ))
684 {
685         struct torture_op *op, *p;
686         
687         op = talloc(talloc_autofree_context(), struct torture_op);
688
689         op->name = talloc_strdup(op, name);
690         op->fn = fn;
691         op->multi_fn = multi_fn;
692
693         for (p = torture_ops; p; p = p->next) {
694                 if (strcmp(p->name, op->name) == 0) {
695                         /* Check for duplicates */
696                         DEBUG(0,("There already is a torture op registered with the name %s!\n", name));
697                         talloc_free(op);
698                         return NT_STATUS_OBJECT_NAME_COLLISION;
699                 }
700
701                 if (strcmp(p->name, op->name) < 0 && 
702                         (!p->next || strcmp(p->next->name, op->name) > 0)) {
703                         DLIST_ADD_AFTER(torture_ops, op, p);
704                         return NT_STATUS_OK;
705                 }
706         }
707
708         DLIST_ADD(torture_ops, op);
709         
710         return NT_STATUS_OK;
711 }
712
713 int torture_init(void)
714 {
715         init_module_fn static_init[] = STATIC_torture_MODULES;
716         init_module_fn *shared_init = load_samba_modules(NULL, "torture");
717         
718         register_builtin_ops();
719
720         run_init_functions(static_init);
721         run_init_functions(shared_init);
722
723         talloc_free(shared_init);
724
725         return 0;
726 }