1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
14 * The Original Code is the PKIX-C library.
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.
22 * Sun Microsystems, Inc.
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.
36 * ***** END LICENSE BLOCK ***** */
40 * Tests Logger Objects
45 #include "testutil_nss.h"
47 static void *plContext = NULL;
49 static char *levels[] = {
59 PKIX_Error *testLoggerCallback(
61 PKIX_PL_String *message,
63 PKIX_ERRORCLASS logComponent,
69 static int callCount = 0;
73 msg = PKIX_String2ASCII(message, plContext);
74 PR_snprintf(result, 100, "Logging %s (%s): %s",
75 levels[logLevel], PKIX_ERRORCLASSNAMES[logComponent], msg);
80 testError("Incorrect number of Logger Callback <expect 1>");
85 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(msg, plContext));
90 PKIX_Error *testLoggerCallback2(
92 PKIX_PL_String *message,
94 PKIX_ERRORCLASS logComponent,
101 PKIX_TEST_STD_VARS();
103 msg = PKIX_String2ASCII(message, plContext);
104 PR_snprintf(result, 100, "Logging %s (%s): %s",
105 levels[logLevel], PKIX_ERRORCLASSNAMES[logComponent], msg);
109 PKIX_TEST_EXPECT_NO_ERROR(PKIX_PL_Free(msg, plContext));
114 createLogger(PKIX_Logger **logger,
115 PKIX_PL_Object *context,
116 PKIX_Logger_LogCallback cb)
118 PKIX_TEST_STD_VARS();
120 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_Create
121 (cb, context, logger, plContext));
129 testContextCallback(PKIX_Logger *logger, PKIX_Logger *logger2)
131 PKIX_Logger_LogCallback cb = NULL;
132 PKIX_PL_Object *context = NULL;
133 PKIX_Boolean cmpResult = PKIX_FALSE;
136 PKIX_TEST_STD_VARS();
138 subTest("PKIX_Logger_GetLoggerContext");
139 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLoggerContext
140 (logger2, &context, plContext));
143 ((PKIX_PL_Object *)logger, context, PKIX_TRUE, plContext);
145 subTest("PKIX_Logger_GetLogCallback");
146 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLogCallback
147 (logger, &cb, plContext));
149 if (cb != testLoggerCallback) {
150 testError("Incorrect Logger Callback returned");
155 PKIX_TEST_DECREF_AC(context);
160 testComponent(PKIX_Logger *logger)
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();
167 subTest("PKIX_Logger_GetLoggingComponent");
168 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLoggingComponent
169 (logger, &compName, plContext));
171 if (compName != (PKIX_ERRORCLASS)NULL) {
172 testError("Incorrect Logger Component returned. expect <NULL>");
175 subTest("PKIX_Logger_SetLoggingComponent");
176 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetLoggingComponent
177 (logger, PKIX_LIST_ERROR, plContext));
179 subTest("PKIX_Logger_GetLoggingComponent");
180 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetLoggingComponent
181 (logger, &compNameReturn, plContext));
183 if (compNameReturn != PKIX_LIST_ERROR) {
184 testError("Incorrect Logger Component returned.");
193 testMaxLoggingLevel(PKIX_Logger *logger)
195 PKIX_UInt32 level = 0;
196 PKIX_TEST_STD_VARS();
198 subTest("PKIX_Logger_GetMaxLoggingLevel");
199 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetMaxLoggingLevel
200 (logger, &level, plContext));
203 testError("Incorrect Logger MaxLoggingLevel returned");
206 subTest("PKIX_Logger_SetMaxLoggingLevel");
207 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetMaxLoggingLevel
208 (logger, 3, plContext));
210 subTest("PKIX_Logger_GetMaxLoggingLevel");
211 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_GetMaxLoggingLevel
212 (logger, &level, plContext));
215 testError("Incorrect Logger MaxLoggingLevel returned");
224 testLogger(PKIX_Logger *logger, PKIX_Logger *logger2)
226 PKIX_List *loggerList = NULL;
227 PKIX_List *checkList = NULL;
229 PKIX_Boolean cmpResult = PKIX_FALSE;
230 char *expectedAscii = "[\n"
232 "\tContext: (null)\n"
233 "\tMaximum Level: 3\n"
234 "\tComponent Name: LIST\n"
238 PKIX_TEST_STD_VARS();
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));
245 testError("Incorrect Logger List returned");
247 PKIX_TEST_DECREF_BC(loggerList);
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));
253 subTest("PKIX_SetLoggers");
254 PKIX_TEST_EXPECT_NO_ERROR(PKIX_SetLoggers(loggerList, plContext));
256 subTest("PKIX_Logger_SetLoggingComponent");
257 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetLoggingComponent
258 (logger2, PKIX_MUTEX_ERROR, plContext));
260 subTest("PKIX_Logger_SetMaxLoggingLevel");
261 PKIX_TEST_EXPECT_NO_ERROR(PKIX_Logger_SetMaxLoggingLevel
262 (logger2, 5, plContext));
264 subTest("PKIX_AddLogger");
265 PKIX_TEST_EXPECT_NO_ERROR(PKIX_AddLogger(logger2, plContext));
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));
273 PKIX_TEST_DECREF_BC(loggerList);
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));
280 subTest("pkix_Loggers_Equals");
282 ((PKIX_PL_Object *) loggerList,
283 (PKIX_PL_Object *) checkList,
287 subTest("pkix_Loggers_Duplicate");
288 testDuplicateHelper((PKIX_PL_Object *)logger, plContext);
290 subTest("pkix_Loggers_Hashcode");
291 testHashcodeHelper((PKIX_PL_Object *) logger,
292 (PKIX_PL_Object *) logger,
296 subTest("pkix_Loggers_ToString");
297 testToStringHelper((PKIX_PL_Object *) logger, expectedAscii, plContext);
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));
306 PKIX_TEST_DECREF_AC(loggerList);
307 PKIX_TEST_DECREF_AC(checkList);
312 testDestroy(PKIX_Logger *logger)
314 PKIX_TEST_STD_VARS();
316 PKIX_TEST_DECREF_BC(logger);
323 int test_logger(int argc, char *argv[]) {
325 PKIX_Logger *logger, *logger2;
326 PKIX_UInt32 actualMinorVersion;
329 PKIX_TEST_STD_VARS();
331 startTests("Loggers");
333 PKIX_TEST_EXPECT_NO_ERROR(
334 PKIX_PL_NssContext_Create(0, PKIX_FALSE, NULL, &plContext));
336 subTest("PKIX_Logger_Create");
337 createLogger(&logger, NULL, testLoggerCallback);
338 createLogger(&logger2, (PKIX_PL_Object *)logger, testLoggerCallback2);
340 subTest("Logger Context and Callback");
341 testContextCallback(logger, logger2);
343 subTest("Logger Component");
344 testComponent(logger);
346 subTest("Logger MaxLoggingLevel");
347 testMaxLoggingLevel(logger);
349 subTest("Logger List operations");
350 testLogger(logger, logger2);
352 subTest("PKIX_Logger_Destroy");
354 testDestroy(logger2);
358 PKIX_Shutdown(plContext);