tests: rename logging test source
[gd/samba-autobuild/.git] / lib / util / tests / test_logging.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    A test server that only does logging.
5
6    Copyright (C) Andrew Tridgell                1992-2005
7    Copyright (C) Martin Pool                    2002
8    Copyright (C) Jelmer Vernooij                2002
9    Copyright (C) James J Myers                  2003 <myersjj@samba.org>
10    Copyright (C) Douglas Bagnall                2022
11
12    This program is free software; you can redistribute it and/or modify
13    it under the terms of the GNU General Public License as published by
14    the Free Software Foundation; either version 3 of the License, or
15    (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20    GNU General Public License for more details.
21
22    You should have received a copy of the GNU General Public License
23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
24 */
25
26 #include "includes.h"
27 #include "lib/cmdline/cmdline.h"
28
29 #ifdef USING_CMDLINE_S3
30 #include "lib/util/debug_s3.h"
31 #endif
32
33 #define BINARY_NAME "test_s4_logging"
34
35 static int log_level = 1;
36
37
38 struct debug_class {
39         int dclass;
40         const char *name;
41 };
42
43 struct debug_class classes_table[] = {
44         {DBGC_ALL, "all"},
45         {DBGC_TDB, "tdb"},
46         {DBGC_PRINTDRIVERS, "printdrivers"},
47         {DBGC_LANMAN, "lanman"},
48         {DBGC_SMB, "smb"},
49         {DBGC_RPC_PARSE, "rpc_parse"},
50         {DBGC_RPC_SRV, "rpc_srv"},
51         {DBGC_RPC_CLI, "rpc_cli"},
52         {DBGC_PASSDB, "passdb"},
53         {DBGC_SAM, "sam"},
54         {DBGC_AUTH, "auth"},
55         {DBGC_WINBIND, "winbind"},
56         {DBGC_VFS, "vfs"},
57         {DBGC_IDMAP, "idmap"},
58         {DBGC_QUOTA, "quota"},
59         {DBGC_ACLS, "acls"},
60         {DBGC_LOCKING, "locking"},
61         {DBGC_MSDFS, "msdfs"},
62         {DBGC_DMAPI, "dmapi"},
63         {DBGC_REGISTRY, "registry"},
64         {DBGC_SCAVENGER, "scavenger"},
65         {DBGC_DNS, "dns"},
66         {DBGC_LDB, "ldb"},
67         {DBGC_TEVENT, "tevent"},
68         {DBGC_AUTH_AUDIT, "auth_audit"},
69         {DBGC_AUTH_AUDIT_JSON, "auth_json_audit"},
70         {DBGC_KERBEROS, "kerberos"},
71         {DBGC_DRS_REPL, "drs_repl"},
72         {DBGC_SMB2, "smb2"},
73         {DBGC_SMB2_CREDITS, "smb2_credits"},
74         {DBGC_DSDB_AUDIT, "dsdb_audit"},
75         {DBGC_DSDB_AUDIT_JSON, "dsdb_json_audit"},
76         {DBGC_DSDB_PWD_AUDIT, "dsdb_password_audit"},
77         {DBGC_DSDB_PWD_AUDIT_JSON, "dsdb_password_json_audit"},
78         {DBGC_DSDB_TXN_AUDIT, "dsdb_transaction_audit"},
79         {DBGC_DSDB_TXN_AUDIT_JSON, "dsdb_transaction_json_audit"},
80         {DBGC_DSDB_GROUP_AUDIT, "dsdb_group_audit"},
81         {DBGC_DSDB_GROUP_AUDIT_JSON, "dsdb_group_json_audit"},
82 };
83
84
85 static int log_all_classes(int level)
86 {
87         size_t i;
88         struct debug_class c;
89         for (i = 0; i < ARRAY_SIZE(classes_table); i++) {
90                 c = classes_table[i];
91                 if (i != c.dclass) {
92                         /*
93                          * we implicitly rely on these values staying in the
94                          * right order.
95                          */
96                         fprintf(stderr,
97                                 "expected '%s' class to have value %zu\n",
98                                 c.name, i);
99                         return 1;
100                 }
101
102                 DEBUGC(c.dclass, level,
103                        ("logging for '%s' [%d], at level %d\n",
104                         c.name, c.dclass, level));
105
106                 /*
107                  * That's it for the tests *here*. The invoker of this
108                  * process will have set up an smb.conf that directs the
109                  * output in particular ways, and will be looking to see that
110                  * happens correctly.
111                  */
112         }
113         return 0;
114 }
115
116
117 static int init_daemon(TALLOC_CTX *mem_ctx,
118                        int argc,
119                        const char *argv[],
120                        const char **error)
121 {
122         poptContext pc;
123         int opt;
124         bool ok;
125         struct poptOption long_options[] = {
126                 POPT_AUTOHELP
127                 {
128                         .longName   = "level",
129                         .shortName  = 'L',
130                         .argInfo    = POPT_ARG_INT,
131                         .arg        = &log_level,
132                         .descrip    = "log at this level",
133                         .argDescrip = "LEVEL",
134                 },
135                 POPT_COMMON_SAMBA
136                 POPT_COMMON_DAEMON
137                 POPT_COMMON_VERSION
138                 POPT_TABLEEND
139         };
140
141         setproctitle(BINARY_NAME);
142
143         ok = samba_cmdline_init(mem_ctx,
144                                 SAMBA_CMDLINE_CONFIG_SERVER,
145                                 true /* require_smbconf */);
146         if (!ok) {
147                 *error = "Failed to init cmdline parser!\n";
148                 return EINVAL;
149         }
150
151         pc = samba_popt_get_context(BINARY_NAME,
152                                     argc,
153                                     argv,
154                                     long_options,
155                                     0);
156         if (pc == NULL) {
157                 *error = "Failed to setup popt context!\n";
158                 return ENOTRECOVERABLE;
159         }
160
161         while((opt = poptGetNextOpt(pc)) != -1) {
162                 fprintf(stderr, "\nInvalid option %s: %s\n\n",
163                         poptBadOption(pc, 0), poptStrerror(opt));
164                 poptPrintUsage(pc, stderr, 0);
165                 return 1;
166         }
167
168         poptFreeContext(pc);
169
170 #ifdef USING_CMDLINE_S3
171         reopen_logs();
172 #endif
173         return 0;
174 }
175
176
177 int main(int argc, const char *argv[])
178 {
179         int rc;
180         const char *error = NULL;
181         TALLOC_CTX *mem_ctx = talloc_stackframe();
182         if (mem_ctx == NULL) {
183                 exit(ENOMEM);
184         }
185
186         setproctitle_init(argc, discard_const(argv), environ);
187
188         rc = init_daemon(mem_ctx, argc, argv, &error);
189         if (rc != 0) {
190                 fprintf(stderr, "error [%d]: %s\n", rc, error);
191                 exit_daemon(error, rc);
192         }
193
194         rc = log_all_classes(log_level);
195         if (rc != 0) {
196                 fprintf(stderr, "error in log_all_classes [%d]\n", rc);
197                 exit_daemon("logging error", rc);
198         }
199
200         TALLOC_FREE(mem_ctx);
201         return rc;
202 }