]> arthur.barton.de Git - ngircd-alex.git/commitdiff
portabtest: Actually test functions
authorAlexander Barton <alex@barton.de>
Wed, 1 Jan 2014 14:57:34 +0000 (15:57 +0100)
committerAlexander Barton <alex@barton.de>
Wed, 1 Jan 2014 14:57:34 +0000 (15:57 +0100)
Test functions snprintf(), strlcpy(), strlcat(), vsnprintf() for
correctness, not only existance (which was quite useless, because
if they weren't available, the program could not have been linked
at all ...).

src/portab/portabtest.c

index 8284c1d027d2af09640f7924050a8148b35bffd3..4f53ce76594a27fa0d70a551c6e213e66899de47 100644 (file)
 
 #include "exp.h"
 
 
 #include "exp.h"
 
-static void Panic PARAMS((char *Reason));
+static void
+Panic(char *Reason)
+{
+       /* Oops, something failed!? */
+       fprintf(stderr, "Oops, test for %s failed!?\n", Reason);
+       exit(1);
+} /* Panic */
+
+static void
+Check_snprintf(void)
+{
+       char str[5];
+
+       snprintf(str, sizeof(str), "%s", "1234567890");
+       if (str[4] != '\0')
+               Panic("snprintf NULL byte");
+       if (strlen(str) != 4)
+               Panic("snprintf string length");
+}
+
+static void
+Check_strlcpy(void)
+{
+       char str[5];
+
+       if (strlcpy(str, "1234567890", sizeof(str)) != 10)
+               Panic("strlcpy return code");
+       if (str[4] != '\0')
+               Panic("strlcpy NULL byte");
+       if (strlen(str) != 4)
+               Panic("strlcpy string length");
+}
+
+static void
+Check_strlcat(void)
+{
+       char str[5];
+
+       if (strlcpy(str, "12", sizeof(str)) != 2)
+               Panic("strlcpy for strlcat");
+       if (strlcat(str, "1234567890", sizeof(str)) != 12)
+               Panic("strlcat return code");
+       if (str[4] != '\0')
+               Panic("strlcat NULL byte");
+       if (strlen(str) != 4)
+               Panic("strlcat string length");
+}
+
+#ifdef PROTOTYPES
+static void
+Check_vsnprintf(const int Len, const char *Format, ...)
+#else
+static void
+Check_vsnprintf(Len, Format, va_alist)
+const int Len;
+const char *Format;
+va_dcl
+#endif
+{
+       char str[5];
+       va_list ap;
+
+#ifdef PROTOTYPES
+       va_start(ap, Format);
+#else
+       va_start(ap);
+#endif
+       if (vsnprintf(str, sizeof(str), Format, ap) != Len)
+               Panic("vsnprintf return code");
+       va_end(ap);
+
+       if (str[4] != '\0')
+               Panic("vsnprintf NULL byte");
+       if (strlen(str) != 4)
+               Panic("vsnprintf string length");
+}
 
 GLOBAL int
 main(void)
 
 GLOBAL int
 main(void)
@@ -41,28 +116,13 @@ main(void)
        if (sizeof(UINT32) != 4)
                Panic("UINT32");
 
        if (sizeof(UINT32) != 4)
                Panic("UINT32");
 
-#ifdef PROTOTYPES
        /* check functions */
        /* check functions */
-       if (!snprintf)
-               Panic("snprintf");
-       if (!vsnprintf)
-               Panic("vsnprintf");
-       if (!strlcpy)
-               Panic("strlcpy");
-       if (!strlcat)
-               Panic("strlcat");
-#endif
+       Check_snprintf();
+       Check_strlcpy();
+       Check_strlcat();
+       Check_vsnprintf(2+10, "%s%s", "ab", "1234567890");
        
        
-       /* ok, no error */
        return 0;
        return 0;
-} /* portab_check_types */
-
-static void
-Panic(char *Reason)
-{
-       /* Oops, something failed!? */
-       fprintf(stderr, "Oops, test for %s failed!?", Reason);
-       exit(1);
-} /* Panic */
+}
 
 /* -eof- */
 
 /* -eof- */