Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / security / nss / cmd / libpkix / pkix / util / test_logger.c
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is the PKIX-C library.
15  *
16  * The Initial Developer of the Original Code is
17  * Sun Microsystems, Inc.
18  * Portions created by the Initial Developer are
19  * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Sun Microsystems, Inc.
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 /*
38  * test_logger.c
39  *
40  * Tests Logger Objects
41  *
42  */
43
44 #include "testutil.h"
45 #include "testutil_nss.h"
46
47 static void *plContext = NULL;
48
49 static char *levels[] = {
50         "None",
51         "Fatal Error",
52         "Error",
53         "Warning",
54         "Debug",
55         "Trace"
56 };
57
58 static
59 PKIX_Error *testLoggerCallback(
60         PKIX_Logger *logger,
61         PKIX_PL_String *message,
62         PKIX_UInt32 logLevel,
63         PKIX_ERRORCLASS logComponent,
64         void *plContext)
65 {
66         char *comp = NULL;
67         char *msg = NULL;
68         char result[100];
69         static int callCount = 0;
70
71         PKIX_TEST_STD_VARS();
72
73         msg = PKIX_String2ASCII(message, plContext);
74         PR_snprintf(result, 100, "Logging %s (%s): %s",
75                 levels[logLevel], PKIX_ERRORCLASSNAMES[logComponent], msg);
76         subTest(result);
77
78         callCount++;
79         if (callCount > 1) {
80                 testError("Incorrect number of Logger Callback <expect 1>");
81         }
82
83 cleanup:
84
85         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(msg, plContext));
86         PKIX_TEST_RETURN();
87 }
88
89 static
90 PKIX_Error *testLoggerCallback2(
91         PKIX_Logger *logger,
92         PKIX_PL_String *message,
93         PKIX_UInt32 logLevel,
94         PKIX_ERRORCLASS logComponent,
95         void *plContext)
96 {
97         char *comp = NULL;
98         char *msg = NULL;
99         char result[100];
100
101         PKIX_TEST_STD_VARS();
102
103         msg = PKIX_String2ASCII(message, plContext);
104         PR_snprintf(result, 100, "Logging %s (%s): %s",
105                 levels[logLevel], PKIX_ERRORCLASSNAMES[logComponent], msg);
106         subTest(result);
107
108 cleanup:
109         PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(msg, plContext));
110         PKIX_TEST_RETURN();
111 }
112
113 static void
114 createLogger(PKIX_Logger **logger,
115         PKIX_PL_Object *context,
116         PKIX_Logger_LogCallback cb)
117 {
118         PKIX_TEST_STD_VARS();
119
120         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_Create
121                 (cb, context, logger, plContext));
122
123 cleanup:
124
125         PKIX_TEST_RETURN();
126 }
127
128 static void
129 testContextCallback(PKIX_Logger *logger, PKIX_Logger *logger2)
130 {
131         PKIX_Logger_LogCallback cb = NULL;
132         PKIX_PL_Object *context = NULL;
133         PKIX_Boolean cmpResult = PKIX_FALSE;
134         PKIX_UInt32 length;
135
136         PKIX_TEST_STD_VARS();
137
138         subTest("PKIX_Logger_GetLoggerContext");
139         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLoggerContext
140                 (logger2, &context, plContext));
141
142         testEqualsHelper
143                 ((PKIX_PL_Object *)logger, context, PKIX_TRUE, plContext);
144
145         subTest("PKIX_Logger_GetLogCallback");
146         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLogCallback
147                 (logger, &cb, plContext));
148
149         if (cb != testLoggerCallback) {
150                 testError("Incorrect Logger Callback returned");
151         }
152
153 cleanup:
154
155         PKIX_TEST_DECREF_AC(context);
156         PKIX_TEST_RETURN();
157 }
158
159 static void
160 testComponent(PKIX_Logger *logger)
161 {
162         PKIX_ERRORCLASS compName = (PKIX_ERRORCLASS)NULL;
163         PKIX_ERRORCLASS compNameReturn = (PKIX_ERRORCLASS)NULL;
164         PKIX_Boolean cmpResult = PKIX_FALSE;
165         PKIX_TEST_STD_VARS();
166
167         subTest("PKIX_Logger_GetLoggingComponent");
168         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLoggingComponent
169                 (logger, &compName, plContext));
170
171         if (compName != (PKIX_ERRORCLASS)NULL) {
172                 testError("Incorrect Logger Component returned. expect <NULL>");
173         }
174
175         subTest("PKIX_Logger_SetLoggingComponent");
176         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetLoggingComponent
177                 (logger, PKIX_LIST_ERROR, plContext));
178
179         subTest("PKIX_Logger_GetLoggingComponent");
180         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLoggingComponent
181                 (logger, &compNameReturn, plContext));
182
183         if (compNameReturn != PKIX_LIST_ERROR) {
184                 testError("Incorrect Logger Component returned.");
185         }
186
187 cleanup:
188
189         PKIX_TEST_RETURN();
190 }
191
192 static void
193 testMaxLoggingLevel(PKIX_Logger *logger)
194 {
195         PKIX_UInt32 level = 0;
196         PKIX_TEST_STD_VARS();
197
198         subTest("PKIX_Logger_GetMaxLoggingLevel");
199         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetMaxLoggingLevel
200                 (logger, &level, plContext));
201
202         if (level != 0) {
203                 testError("Incorrect Logger MaxLoggingLevel returned");
204         }
205
206         subTest("PKIX_Logger_SetMaxLoggingLevel");
207         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetMaxLoggingLevel
208                 (logger, 3, plContext));
209
210         subTest("PKIX_Logger_GetMaxLoggingLevel");
211         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetMaxLoggingLevel
212                 (logger, &level, plContext));
213
214         if (level != 3) {
215                 testError("Incorrect Logger MaxLoggingLevel returned");
216         }
217
218 cleanup:
219
220         PKIX_TEST_RETURN();
221 }
222
223 static void
224 testLogger(PKIX_Logger *logger, PKIX_Logger *logger2)
225 {
226         PKIX_List *loggerList = NULL;
227         PKIX_List *checkList = NULL;
228         PKIX_UInt32 length;
229         PKIX_Boolean cmpResult = PKIX_FALSE;
230         char *expectedAscii = "[\n"
231                 "\tLogger: \n"
232                 "\tContext:          (null)\n"
233                 "\tMaximum Level:    3\n"
234                 "\tComponent Name:   LIST\n"
235                 "]\n";
236
237
238         PKIX_TEST_STD_VARS();
239
240         subTest("PKIX_GetLoggers");
241         PKIX_TEST_EXPECT_NO_ERROR(PKIX_GetLoggers(&loggerList, plContext));
242         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength
243                 (loggerList, &length, plContext));
244         if (length != 0){
245                 testError("Incorrect Logger List returned");
246         }
247         PKIX_TEST_DECREF_BC(loggerList);
248
249         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&loggerList, plContext));
250         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
251                 (loggerList, (PKIX_PL_Object *) logger, plContext));
252
253         subTest("PKIX_SetLoggers");
254         PKIX_TEST_EXPECT_NO_ERROR(PKIX_SetLoggers(loggerList, plContext));
255
256         subTest("PKIX_Logger_SetLoggingComponent");
257         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetLoggingComponent
258                 (logger2, PKIX_MUTEX_ERROR, plContext));
259
260         subTest("PKIX_Logger_SetMaxLoggingLevel");
261         PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetMaxLoggingLevel
262                 (logger2, 5, plContext));
263
264         subTest("PKIX_AddLogger");
265         PKIX_TEST_EXPECT_NO_ERROR(PKIX_AddLogger(logger2, plContext));
266
267         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_Create(&checkList, plContext));
268         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
269                 (checkList, (PKIX_PL_Object *) logger, plContext));
270         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_AppendItem
271                 (checkList, (PKIX_PL_Object *) logger2, plContext));
272
273         PKIX_TEST_DECREF_BC(loggerList);
274
275         subTest("PKIX_GetLoggers");
276         PKIX_TEST_EXPECT_NO_ERROR(PKIX_GetLoggers(&loggerList, plContext));
277         PKIX_TEST_EXPECT_NO_ERROR(PKIX_List_GetLength
278                 (loggerList, &length, plContext));
279
280         subTest("pkix_Loggers_Equals");
281         testEqualsHelper
282                 ((PKIX_PL_Object *) loggerList,
283                 (PKIX_PL_Object *) checkList,
284                 PKIX_TRUE,
285                 plContext);
286
287         subTest("pkix_Loggers_Duplicate");
288         testDuplicateHelper((PKIX_PL_Object *)logger, plContext);
289
290         subTest("pkix_Loggers_Hashcode");
291         testHashcodeHelper((PKIX_PL_Object *) logger,
292                            (PKIX_PL_Object *) logger,
293                            PKIX_TRUE,
294                            plContext);
295
296         subTest("pkix_Loggers_ToString");
297         testToStringHelper((PKIX_PL_Object *) logger, expectedAscii, plContext);
298
299         subTest("PKIX Logger Callback");
300         subTest("Expect to have ***Fatal Error (List): Null argument*** once");
301         PKIX_TEST_EXPECT_ERROR(PKIX_List_AppendItem
302                 (NULL, (PKIX_PL_Object *) NULL, plContext));
303
304 cleanup:
305
306         PKIX_TEST_DECREF_AC(loggerList);
307         PKIX_TEST_DECREF_AC(checkList);
308         PKIX_TEST_RETURN();
309 }
310
311 static void
312 testDestroy(PKIX_Logger *logger)
313 {
314         PKIX_TEST_STD_VARS();
315
316         PKIX_TEST_DECREF_BC(logger);
317
318 cleanup:
319
320         PKIX_TEST_RETURN();
321 }
322
323 int test_logger(int argc, char *argv[]) {
324
325         PKIX_Logger *logger, *logger2;
326         PKIX_UInt32 actualMinorVersion;
327         PKIX_UInt32 j = 0;
328
329         PKIX_TEST_STD_VARS();
330
331         startTests("Loggers");
332
333         PKIX_TEST_EXPECT_NO_ERROR(
334             PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
335
336         subTest("PKIX_Logger_Create");
337         createLogger(&logger, NULL, testLoggerCallback);
338         createLogger(&logger2, (PKIX_PL_Object *)logger, testLoggerCallback2);
339
340         subTest("Logger Context and Callback");
341         testContextCallback(logger, logger2);
342
343         subTest("Logger Component");
344         testComponent(logger);
345
346         subTest("Logger MaxLoggingLevel");
347         testMaxLoggingLevel(logger);
348
349         subTest("Logger List operations");
350         testLogger(logger, logger2);
351
352         subTest("PKIX_Logger_Destroy");
353         testDestroy(logger);
354         testDestroy(logger2);
355
356 cleanup:
357
358         PKIX_Shutdown(plContext);
359
360         PKIX_TEST_RETURN();
361
362         endTests("Loggers");
363
364         return (0);
365
366 }