nfs-ganesha 1.4

test_liblog_functions.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  * ---------------------------------------
00024  * Test de libaglae
00025  *
00026  *
00027  */
00028 
00029 #pragma GCC diagnostic ignored "-Wformat"
00030 
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif
00034 
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include "log.h"
00039 
00040 #ifndef TRUE
00041 #define TRUE 1
00042 #endif
00043 #ifndef FALSE
00044 #define FALSE 0
00045 #endif
00046 
00047 #define ERR_DUMMY 3
00048 static family_error_t tab_test_err[] = {
00049 #define ERR_DUMMY_1  0
00050   {ERR_DUMMY_1, "ERR_DUMMY_1", "First Dummy Error"},
00051 #define ERR_DUMMY_2  1
00052   {ERR_DUMMY_2, "ERR_DUMMY_2", "Second Dummy Error"},
00053 
00054   {ERR_NULL, "ERR_NULL", ""}
00055 };
00056 
00057 void TestAlways(int expect, char *buff, log_components_t component, char *string)
00058 {
00059   char compare[2048];
00060   sprintf(compare, "%s: %s", LogComponents[component].comp_str, string);
00061   buff[0] = '\0';
00062   LogAlways(component, "%s", string);
00063   if ((expect && (strcmp(compare, buff) != 0)) || (!expect && (buff[0] != '\0')))
00064     {
00065       LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",
00066               string, buff, compare);
00067       exit(1);
00068     }
00069   LogTest("SUCCESS: %s produced \"%s\"", string, buff);
00070 }
00071 
00072 void TestMajor(int expect, char *buff, log_components_t component, char *string)
00073 {
00074   char compare[2048];
00075   sprintf(compare, "%s: MAJOR ERROR: %s", LogComponents[component].comp_str, string);
00076   buff[0] = '\0';
00077   LogMajor(component, "%s", string);
00078   if ((expect && (strcmp(compare, buff) != 0)) || (!expect && (buff[0] != '\0')))
00079   {
00080       LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",
00081               string, buff, compare);
00082       exit(1);
00083     }
00084   if (expect)
00085     LogTest("SUCCESS: %s produced \"%s\"", string, buff);
00086   else
00087     LogTest("SUCCESS: %s didn't produce anything", string);
00088 }
00089 
00090 void TestCrit(int expect, char *buff, log_components_t component, char *string)
00091 {
00092   char compare[2048];
00093   sprintf(compare, "%s: CRITICAL ERROR: %s", LogComponents[component].comp_str, string);
00094   buff[0] = '\0';
00095   LogCrit(component, "%s", string);
00096   if ((expect && (strcmp(compare, buff) != 0)) || (!expect && (buff[0] != '\0')))
00097   {
00098       LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",
00099               string, buff, compare);
00100       exit(1);
00101     }
00102   if (expect)
00103     LogTest("SUCCESS: %s produced \"%s\"", string, buff);
00104   else
00105     LogTest("SUCCESS: %s didn't produce anything", string);
00106 }
00107 
00108 void TestEvent(int expect, char *buff, log_components_t component, char *string)
00109 {
00110   char compare[2048];
00111   sprintf(compare, "%s: EVENT: %s", LogComponents[component].comp_str, string);
00112   buff[0] = '\0';
00113   LogEvent(component, "%s", string);
00114   if ((expect && (strcmp(compare, buff) != 0)) || (!expect && (buff[0] != '\0')))
00115   {
00116       LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",
00117               string, buff, compare);
00118       exit(1);
00119     }
00120   if (expect)
00121     LogTest("SUCCESS: %s produced \"%s\"", string, buff);
00122   else
00123     LogTest("SUCCESS: %s didn't produce anything", string);
00124 }
00125 
00126 void TestDebug(int expect, char *buff, log_components_t component, char *string)
00127 {
00128   char compare[2048];
00129   sprintf(compare, "%s: DEBUG: %s", LogComponents[component].comp_str, string);
00130   buff[0] = '\0';
00131   LogDebug(component, "%s", string);
00132   
00133   if ((expect && (strcmp(compare, buff) != 0)) || (!expect && (buff[0] != '\0')))
00134     {
00135       LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",
00136               string, buff, compare);
00137       exit(1);
00138     }
00139   if (expect)
00140     LogTest("SUCCESS: %s produced \"%s\"", string, buff);
00141   else
00142     LogTest("SUCCESS: %s didn't produce anything", string);
00143 }
00144 
00145 void TestFullDebug(int expect, char *buff, log_components_t component, char *string)
00146 {
00147   char compare[2048];
00148   sprintf(compare, "%s: FULLDEBUG: %s", LogComponents[component].comp_str, string);
00149   buff[0] = '\0';
00150   LogFullDebug(component, "%s", string);
00151   if ((expect && (strcmp(compare, buff) != 0)) || (!expect && (buff[0] != '\0')))
00152     {
00153       LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",
00154               string, buff, compare);
00155       exit(1);
00156     }
00157   if (expect)
00158     LogTest("SUCCESS: %s produced \"%s\"", string, buff);
00159   else
00160     LogTest("SUCCESS: %s didn't produce anything", string);
00161 }
00162 
00163 #define TestFormat(format, args...)                     \
00164   do {                                                  \
00165     char compare[2048], buff[2048];                     \
00166     sprintf(compare, format, ## args);                  \
00167     log_snprintf(buff, 2048, format, ## args);          \
00168     if (strcmp(compare, buff) != 0)                     \
00169       {                                                 \
00170         LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",  \
00171                 format, buff, compare);                 \
00172         exit(1);                                        \
00173       }                                                 \
00174     else                                                \
00175       LogTest("SUCCESS: %s produced \"%s\"", format, buff); \
00176   } while (0)
00177 
00178 #define TestGaneshaFormat(expect, compare, format, args...) \
00179   do {                                                  \
00180     char buff[2048];                                    \
00181     log_snprintf(buff, 2048, format, ## args);          \
00182     if (strcmp(compare, buff) != 0 && expect)           \
00183       {                                                 \
00184         LogTest("FAILURE: %s produced \"%s\" expected \"%s\"",  \
00185                 format, buff, compare);                 \
00186         exit(1);                                        \
00187       }                                                 \
00188     else if (expect)                                    \
00189       LogTest("SUCCESS: %s produced \"%s\"", format, buff); \
00190     else                                                \
00191       LogTest("FAILURE (EXPECTED):  %s produced \"%s\"", format, buff); \
00192   } while (0)
00193 
00197 void Test1(char *str, char *file)
00198 {
00199   char tempstr[2048];
00200   int  i;
00201 
00202   SetComponentLogFile(COMPONENT_INIT, "STDOUT");
00203   LogAlways(COMPONENT_INIT, "%s", "Starting Log Tests");
00204   LogTest("My PID = %d", getpid());
00205 
00206   LogTest("------------------------------------------------------");
00207   LogTest("Test conversion of log levels between string and integer");
00208   for (i = NIV_NULL; i < NB_LOG_LEVEL; i++)
00209     {
00210       int j;
00211       if (strcmp(tabLogLevel[i].str, ReturnLevelInt(i)) != 0)
00212         {
00213           LogTest("FAILURE: Log level %d did not convert to %s, it converted to %s",
00214                   i, tabLogLevel[i].str, ReturnLevelInt(i));
00215           exit(1);
00216         }
00217       j = ReturnLevelAscii(tabLogLevel[i].str);
00218       if (j != i)
00219         {
00220           LogTest("FAILURE: Log level %s did not convert to %d, it converted to %d",
00221                   tabLogLevel[i].str, i, j);
00222           exit(1);
00223         }
00224     }
00225 
00226   LogTest("------------------------------------------------------");
00227 
00228   log_snprintf(tempstr, sizeof(tempstr), "Test log_snprintf");
00229   LogTest("%s", tempstr);
00230   LogTest("\nTesting possible environment variable");
00231   LogTest("COMPONENT_MEMCORRUPT debug level is %s",
00232           ReturnLevelInt(LogComponents[COMPONENT_MEMCORRUPT].comp_log_level));
00233   LogFullDebug(COMPONENT_MEMCORRUPT,
00234                "This should appear if environment is set properly");
00235 
00236   LogTest("------------------------------------------------------");
00237   LogTest("Send some messages to various files");
00238   SetComponentLogFile(COMPONENT_DISPATCH, "STDERR");
00239   LogEvent(COMPONENT_DISPATCH, "This should go to stderr");
00240   SetComponentLogFile(COMPONENT_DISPATCH, "STDOUT");
00241   LogEvent(COMPONENT_DISPATCH, "This should go to stdout");
00242   SetComponentLogFile(COMPONENT_DISPATCH, "SYSLOG");
00243   LogEvent(COMPONENT_DISPATCH, "This should go to syslog (verf = %s)", str);
00244   LogTest("About to set %s", file);
00245   SetComponentLogFile(COMPONENT_DISPATCH, file);
00246   LogTest("Got it set");
00247   LogEvent(COMPONENT_DISPATCH, "This should go to %s", file);
00248 
00249   /*
00250    * Set up for tests that will verify what was actually produced by log messages.
00251    * This is used to test log levels and to test the log_vnsprintf function.
00252    */
00253   SetComponentLogBuffer(COMPONENT_MAIN, tempstr);
00254   SetComponentLogBuffer(COMPONENT_INIT, tempstr);
00255 
00256 #ifdef _SNMP_ADM_ACTIVE
00257   {
00258     snmp_adm_type_union param;
00259     int rc;
00260     strcpy(param.string, "FAILED");
00261 
00262     LogTest("------------------------------------------------------");
00263     LogTest("Test SNMP functions");
00264     SetLevelDebug(NIV_DEBUG);
00265 
00266     rc = getComponentLogLevel(&param, (void *)COMPONENT_ALL);
00267     LogTest("getComponentLogLevel(&param, (void *)COMPONENT_ALL) rc=%d result=%s",
00268             rc, param.string);
00269     if (rc != 0)
00270     {
00271       LogTest("FAILURE");
00272       exit(1);
00273     }
00274     strcpy(param.string, "NIV_EVENT");
00275     rc = setComponentLogLevel(&param, (void *)COMPONENT_MAIN);
00276     LogTest("setComponentLogLevel(&param, (void *)COMPONENT_MAIN) rc=%d", rc);
00277     if (rc != 0)
00278     {
00279       LogTest("FAILURE");
00280       exit(1);
00281     }
00282     TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00283     TestMajor     (TRUE,  tempstr, COMPONENT_MAIN, "LogMajor (should print)");
00284     TestCrit      (TRUE,  tempstr, COMPONENT_MAIN, "LogCrit (should print)");
00285     TestEvent     (TRUE,  tempstr, COMPONENT_MAIN, "LogEvent (should print)");
00286     TestDebug     (FALSE, tempstr, COMPONENT_MAIN, "LogDebug (shouldn't print)");
00287     TestFullDebug (FALSE, tempstr, COMPONENT_MAIN, "LogFullDebug (shouldn't print)");
00288     TestAlways    (TRUE,  tempstr, COMPONENT_INIT, "LogAlways (should print)");
00289     TestMajor     (TRUE,  tempstr, COMPONENT_INIT, "LogMajor (should print)");
00290     TestCrit      (TRUE,  tempstr, COMPONENT_INIT, "LogCrit (should print)");
00291     TestEvent     (TRUE,  tempstr, COMPONENT_INIT, "LogEvent (should print)");
00292     TestDebug     (TRUE,  tempstr, COMPONENT_INIT, "LogDebug (should print)");
00293     TestFullDebug (FALSE, tempstr, COMPONENT_INIT, "LogFullDebug (shouldn't print)");
00294   }
00295 #endif /* _SNMP_ADM_ACTIVE */
00296 
00297   LogTest("------------------------------------------------------");
00298   LogTest("Test all levels of log filtering");
00299   SetComponentLogLevel(COMPONENT_MAIN, NIV_NULL);
00300   TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00301   TestMajor     (FALSE, tempstr, COMPONENT_MAIN, "LogMajor (shouldn't print)");
00302   TestCrit      (FALSE, tempstr, COMPONENT_MAIN, "LogCrit (shouldn't print)");
00303   TestEvent     (FALSE, tempstr, COMPONENT_MAIN, "LogEvent (shouldn't print)");
00304   TestDebug     (FALSE, tempstr, COMPONENT_MAIN, "LogDebug (shouldn't print)");
00305   TestFullDebug (FALSE, tempstr, COMPONENT_MAIN, "LogFullDebug (shouldn't print)");
00306   SetComponentLogLevel(COMPONENT_MAIN, NIV_MAJOR);
00307   TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00308   TestMajor     (TRUE,  tempstr, COMPONENT_MAIN, "LogMajor (should print)");
00309   TestCrit      (FALSE, tempstr, COMPONENT_MAIN, "LogCrit (shouldn't print)");
00310   TestEvent     (FALSE, tempstr, COMPONENT_MAIN, "LogEvent (shouldn't print)");
00311   TestDebug     (FALSE, tempstr, COMPONENT_MAIN, "LogDebug (shouldn't print)");
00312   TestFullDebug (FALSE, tempstr, COMPONENT_MAIN, "LogFullDebug (shouldn't print)");
00313   SetComponentLogLevel(COMPONENT_MAIN, NIV_CRIT);
00314   TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00315   TestMajor     (TRUE,  tempstr, COMPONENT_MAIN, "LogMajor (should print)");
00316   TestCrit      (TRUE,  tempstr, COMPONENT_MAIN, "LogCrit (should print)");
00317   TestEvent     (FALSE, tempstr, COMPONENT_MAIN, "LogEvent (shouldn't print)");
00318   TestDebug     (FALSE, tempstr, COMPONENT_MAIN, "LogDebug (shouldn't print)");
00319   TestFullDebug (FALSE, tempstr, COMPONENT_MAIN, "LogFullDebug (shouldn't print)");
00320   SetComponentLogLevel(COMPONENT_MAIN, NIV_EVENT);
00321   TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00322   TestMajor     (TRUE,  tempstr, COMPONENT_MAIN, "LogMajor (should print)");
00323   TestCrit      (TRUE,  tempstr, COMPONENT_MAIN, "LogCrit (should print)");
00324   TestEvent     (TRUE,  tempstr, COMPONENT_MAIN, "LogEvent (should print)");
00325   TestDebug     (FALSE, tempstr, COMPONENT_MAIN, "LogDebug (shouldn't print)");
00326   TestFullDebug (FALSE, tempstr, COMPONENT_MAIN, "LogFullDebug (shouldn't print)");
00327   SetComponentLogLevel(COMPONENT_MAIN, NIV_DEBUG);
00328   TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00329   TestMajor     (TRUE,  tempstr, COMPONENT_MAIN, "LogMajor (should print)");
00330   TestCrit      (TRUE,  tempstr, COMPONENT_MAIN, "LogCrit (should print)");
00331   TestEvent     (TRUE,  tempstr, COMPONENT_MAIN, "LogEvent (should print)");
00332   TestDebug     (TRUE,  tempstr, COMPONENT_MAIN, "LogDebug (should print)");
00333   TestFullDebug (FALSE, tempstr, COMPONENT_MAIN, "LogFullDebug (shouldn't print)");
00334   SetComponentLogLevel(COMPONENT_MAIN, NIV_FULL_DEBUG);
00335   TestAlways    (TRUE,  tempstr, COMPONENT_MAIN, "LogAlways (should print)");
00336   TestMajor     (TRUE,  tempstr, COMPONENT_MAIN, "LogMajor (should print)");
00337   TestCrit      (TRUE,  tempstr, COMPONENT_MAIN, "LogCrit (should print)");
00338   TestEvent     (TRUE,  tempstr, COMPONENT_MAIN, "LogEvent (should print)");
00339   TestDebug     (TRUE,  tempstr, COMPONENT_MAIN, "LogDebug (should print)");
00340   TestFullDebug (TRUE,  tempstr, COMPONENT_MAIN, "LogFullDebug (should print)");
00341 }
00342 
00343 void Test2()
00344 {
00345   char tempstr[2048];
00346   int  n1, n2;
00347 
00348   /*
00349    * Set up for tests that will verify what was actually produced by log messages.
00350    * This is used to test log levels and to test the log_vnsprintf function.
00351    */
00352   SetComponentLogBuffer(COMPONENT_MAIN, tempstr);
00353   SetComponentLogBuffer(COMPONENT_INIT, tempstr);
00354   SetComponentLogLevel(COMPONENT_MAIN, NIV_EVENT);
00355 
00356   LogTest("------------------------------------------------------");
00357   LogTest("Test string/char formats");
00358   TestFormat("none");
00359   TestFormat("String: %s", "str");
00360   TestFormat("String: %12s", "str");
00361   TestFormat("String: %-12s", "str");
00362   TestFormat("String: %12s", "too long string");
00363   TestFormat("String: %-12s", "too long string");
00364   TestFormat("%c", (char) 65);
00365   // Not tested lc, ls, C, S
00366 
00367   LogTest("------------------------------------------------------");
00368   LogTest("Test integer formats");
00369   TestFormat("Integer: %d %d %i %i %u %s", 1, -1, 2, -2, 3, "extra");
00370   TestFormat("Octal and Hex: 0%o 0x%x 0x%X %s", 0123, 0xabcdef, 0xABCDEF, "extra");
00371   TestFormat("Field Length: %3d %s", 1, "extra");
00372   TestFormat("Variable Field Length: %*d %s", 5, 123, "extra");
00373   TestFormat("Alignment flags: %+d %+d %-5d %-5d %05d %05d % d % d %s", 2, -2, 333, -333, 444, -444, 5, -5, "extra");
00374   // TestFormat("Two Flags: %-05d %-05d %0-5d %0-5d %s", 333, -333, 444, -444, "extra");
00375   // TestFormat("Two Flags: %+ d %+ d % +d % +d %s", 333, -333, 444, -444, "extra");
00376   TestFormat("Two Flags: %-+5d %-+5d %+-5d %+-5d %s", 333, -333, 444, -444, "extra");
00377   TestFormat("Two Flags: %- 5d %- 5d % -5d % -5d %s", 333, -333, 444, -444, "extra");
00378   TestFormat("Two Flags: %+05d %+05d %0+5d %0+5d %s", 333, -333, 444, -444, "extra");
00379   TestFormat("Two Flags: % 05d % 05d %0 5d %0 5d %s", 333, -333, 444, -444, "extra");
00380   TestFormat("Use of # Flag: %#x %#3x %#05x %#-5x %-#5x %0#5x", 1, 2, 3, 4, 5, 6);
00381   // TestFormat("Many Flags: %#-0 +#-0 +#-0 +5d", 4);
00382   TestFormat("Special Flags (may not be supported) %'d %Id %s", 12345, 67, "extra");
00383 
00384   LogTest("------------------------------------------------------");
00385   LogTest("Test floating point formats");
00386   TestFormat("%e %E %e %E %s", 1.1, 1.1, 1.1E10, 1.1E10, "extra");
00387   TestFormat("%f %F %f %F %s", 1.1, 1.1, 1.1E10, 1.1E10, "extra");
00388   TestFormat("%g %G %g %G %s", 1.1, 1.1, 1.1E10, 1.1E10, "extra");
00389   TestFormat("%a %A %a %A %s", 1.1, 1.1, 1.1E10, 1.1E10, "extra");
00390   TestFormat("%Le %LE %Le %LE %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00391   TestFormat("%Lf %LF %Lf %LF %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00392   TestFormat("%Lg %LG %Lg %LG %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00393   TestFormat("%La %LA %La %LA %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00394   TestFormat("%lle %llE %lle %llE %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00395   TestFormat("%llf %llF %llf %llf %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00396   TestFormat("%llg %llG %llg %llG %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00397   TestFormat("%lla %llA %lla %llA %s", (long double) 1.1, (long double) 1.1, (long double) 1.1E10, (long double) 1.1E10, "extra");
00398   TestFormat("Field Length: %8f %8.2f %8f %8.2f %s", 1.1, 1.1, 1.1E10, 1.1E3, "extra");
00399   TestFormat("Field Length: %08f %08.2f %08f %08.2f %s", 1.1, 1.1, 1.1E10, 1.1E3, "extra");
00400   TestFormat("Field Length: %-8f %-8.2f %-8f %-8.2f %s", 1.1, 1.1, 1.1E10, 1.1E3, "extra");
00401   TestFormat("Variable Field Length: %*.*f %*.2f %6.*f %s", 6, 2, 1.1, 6, 2.2, 2, 3.3, "extra");
00402   TestFormat("Negative:      %e %E %e %E %s    ", -1.1, -1.1, -1.1E10, -1.1E10, "extra");
00403   TestFormat("With '+' flag: %+e %+E %+e %+E %s", 1.1, 1.1, 1.1E10, 1.1E10, "extra");
00404   TestFormat("With ' ' flag: % e % E % e % E %s", 1.1, 1.1, 1.1E10, 1.1E10, "extra");
00405   TestFormat("With '#' flag: %#8.0e %8.0e %s", 1.0, 1.0, "extra");
00406   TestFormat("With '#' flag: %#g %g %#5g %#5g %5g %s", 1.0, 1.0, 2.0, 10.0, 2.0, "extra");
00407 
00408   LogTest("------------------------------------------------------");
00409   LogTest("Test some special formats");
00410   TestFormat("pointer: %p %s", &n1, "extra");
00411 #if 0
00412 // we can't support %n due to security considerations
00413   TestFormat("count: 12345678%n %s", &n1, "extra");
00414   snprintf(tempstr, 2048, "count: 12345678%n %s", &n1, "extra");
00415   log_snprintf(tempstr, 2048, "count: 12345678%n %s", &n2, "extra");
00416   if (n1 != n2)
00417     {
00418       LogTest("FAILURE: 12345678%%n produced %d expected %d", n2, n1);
00419       exit(1);
00420     }
00421   LogTest("SUCCESS: 12345678%%n produced %d", n2);
00422 #endif
00423   errno = EIO;
00424   TestFormat("strerror: %m %64m %s", "extra");
00425   TestFormat("percent char: %% %s", "extra");
00426 
00427   LogTest("------------------------------------------------------");
00428   LogTest("Test integer size qualifier tags");
00429   TestFormat("%hhd %s", (char) 1, "extra");
00430   TestFormat("%hd %s", (short) 500, "extra");
00431   TestFormat("%lld %s", (long long) 12345678, "extra");
00432   TestFormat("%Ld %s", (long long) 12345678, "extra");
00433   TestFormat("%ld %s", (long) 12345, "extra");
00434   TestFormat("%qd %s", (long long) 12345678, "extra");
00435   TestFormat("%jd %s", (long long) 1, "extra");
00436   TestFormat("%td %s", (char *) &n1 - (char *) &n2, "extra");
00437   TestFormat("%zd %s", sizeof(int), "extra");
00438 
00439   /*
00440    * Ganesha can't properly support the $ parameter index tag, so don't bother testing, even if it does work
00441    * when the indices are in ascending order.
00442   TestFormat("%1$08x", 6);
00443   TestFormat("%3$llx %2$d %1d", 1, 2, (long long)0x12345678);
00444    */
00445 
00446 }
00447 
00448 void run_Tests(int all, char *arg, char *str, char *file)
00449 {
00450   SetNameFunction(arg);
00451 
00452   if (all)
00453     {
00454     Test1(str, file);
00455     }
00456   Test2();
00457 }
00458 
00459 void *run_MT_Tests(void *arg)
00460 {
00461   run_Tests(FALSE, (char *)arg, "none", NULL);
00462 
00463   return NULL ;
00464 }
00465 
00466 static char usage[] = "usage:\n\ttest_liblog STD|MT\n";
00467 
00468 #define NB_THREADS 20
00469 
00470 int main(int argc, char *argv[])
00471 {
00472 
00473   if(argc >= 2)
00474     {
00475 
00476       /* TEST 1 Standard */
00477 
00478       if(!strcmp(argv[1], "STD"))
00479         {
00480           char *str = "No extra string provided";
00481           char *file = NULL;
00482 
00483           if (argc >= 3)
00484             str = argv[2];
00485 
00486           if (argc >= 4)
00487             file = argv[3];
00488 
00489           SetNamePgm("test_liblog");
00490           SetNameHost("localhost");
00491           SetDefaultLogging("TEST");
00492           InitLogging();
00493           AddFamilyError(ERR_POSIX, "POSIX Errors", tab_systeme_status);
00494           LogTest("AddFamilyError = %d",
00495                   AddFamilyError(ERR_DUMMY, "Family Dummy", tab_test_err));
00496           LogTest("The family which was added is %s",
00497                   ReturnNameFamilyError(ERR_DUMMY));
00498 
00499           run_Tests(TRUE,  "monothread", str, file);
00500         }
00501 
00502       /* TEST 1 multithread */
00503 
00504       else if(!strcmp(argv[1], "MT"))
00505         {
00506 
00507           /* multithread test */
00508           pthread_attr_t th_attr[NB_THREADS];
00509           pthread_t threads[NB_THREADS];
00510           int th_index, i;
00511           void *result;
00512 
00513           SetNamePgm("test_liblog");
00514           SetNameHost("localhost");
00515           SetDefaultLogging("STDOUT");
00516           InitLogging();
00517           AddFamilyError(ERR_POSIX, "POSIX Errors", tab_systeme_status);
00518           LogTest("AddFamilyError = %d",
00519                   AddFamilyError(ERR_DUMMY, "Family Dummy", tab_test_err));
00520           LogTest("The family which was added is %s",
00521                   ReturnNameFamilyError(ERR_DUMMY));
00522 
00523           /* creation of attributes */
00524           for(th_index = 0; th_index < NB_THREADS; th_index++)
00525             {
00526               pthread_attr_init(&th_attr[th_index]);
00527               pthread_attr_setdetachstate(&th_attr[th_index], PTHREAD_CREATE_JOINABLE);
00528             }
00529 
00530           /* creation of threads with their names */
00531           for(i = 0; i < NB_THREADS; i++)
00532             {
00533               int rc;
00534               char *thread_name = malloc(256);
00535               snprintf(thread_name, 256, "thread %3d", i);
00536               rc = pthread_create(&(threads[i]), &th_attr[i], run_MT_Tests,
00537                                   (void *)thread_name);
00538             }
00539 
00540           /* waiting for threads termination */
00541           for(i = 0; i < NB_THREADS; i++)
00542             {
00543               pthread_join(threads[i], &result);
00544               if(result)
00545                 return 1;
00546             }
00547 
00548           return 0;
00549 
00550         }
00551 
00552       /* unknown test */
00553       else
00554         {
00555           fprintf(stderr, "%s", usage);
00556           exit(1);
00557         }
00558 
00559     }
00560   else
00561     {
00562       fprintf(stderr, "%s", usage);
00563       exit(1);
00564     }
00565   return 0;
00566 }