Created SILC Runtime Toolkit git repository Part II.
[runtime.git] / lib / silcutil / tests / test_silcstrutil.c
index 348126208b4c9619319ecc5275793d1544640f7e..33bfc6215133047e8422d36c7704d152ab58576b 100644 (file)
@@ -1,6 +1,7 @@
 /* UTF-8 decoding tests */
+/* Other string util tests too */
 
-#include "silcincludes.h"
+#include "silcruntime.h"
 
 #define utf8fail(n, data, len)                 \
 const unsigned char u##n[] = (data);           \
@@ -48,13 +49,22 @@ utf8fail(28, "\xfc\x20\xfd\x20", 4);
 utf8fail(29, "\xf8\xf9\xfa\xfb", 4);
 utf8fail(30, "\xf0\x20\xf9\x20\xfa\x20\xfb\x20", 8);
 
+char *render(void *data)
+{
+  char *buf = data;
+  return strdup(buf);
+}
+
 int main(int argc, char **argv)
 {
-  bool success = FALSE;
+  SilcBool success = FALSE;
   unsigned char *s1, *s2, *s3, *s4;
-  int l, opt;
+  unsigned char t[16];
+  char h[32 + 1], str[40];
+  int l, opt, i;
+  SilcUInt32 len;
 
-  while ((opt = getopt(argc, argv, "hVd")) != EOF) {
+  while ((opt = getopt(argc, argv, "hVd:")) != EOF) {
       switch(opt) {
         case 'h':
           printf("usage: test_silcstrutil\n");
@@ -71,7 +81,7 @@ int main(int argc, char **argv)
           if (optarg)
             silc_log_set_debug_string(optarg);
          else
-           silc_log_set_debug_string("*strutil*");
+           silc_log_set_debug_string("*strutil*,*errno*");
           break;
        default:
          exit(1);
@@ -111,7 +121,7 @@ int main(int argc, char **argv)
   /* To UTF-8 */
   l = silc_utf8_encoded_len(s3, strlen(s3), SILC_STRING_LDAP_DN);
   if (!l)
-    goto err;  
+    goto err;
   s4 = silc_calloc(l + 1, sizeof(*s4));
   silc_utf8_encode(s3, strlen(s3), SILC_STRING_LDAP_DN, s4, l);
   SILC_LOG_DEBUG(("utf8 = %s", s4));
@@ -129,13 +139,13 @@ int main(int argc, char **argv)
   s2 = "PÄIVÄÄ VUAN YRJÖ";
   l = silc_utf8_encoded_len(s1, strlen(s1), SILC_STRING_LOCALE);
   if (!l)
-    goto err;  
+    goto err;
   s3 = silc_calloc(l + 1, sizeof(*s3));
   silc_utf8_encode(s1, strlen(s1), SILC_STRING_LOCALE, s3, l);
 
   l = silc_utf8_encoded_len(s2, strlen(s2), SILC_STRING_LOCALE);
   if (!l)
-    goto err;  
+    goto err;
   s4 = silc_calloc(l + 1, sizeof(*s4));
   silc_utf8_encode(s2, strlen(s2), SILC_STRING_LOCALE, s4, l);
 
@@ -149,11 +159,38 @@ int main(int argc, char **argv)
   silc_free(s3);
   silc_free(s4);
 
+  /* Regex test */
+  SILC_LOG_DEBUG(("Simple regex test"));
+  s1 = "foo,bar,silc,com";
+  SILC_LOG_DEBUG(("Find 'silc' from %s", s1));
+  if (!silc_string_match(s1, "silc"))
+    goto err;
+  SILC_LOG_DEBUG(("Regex match"));
+  SILC_LOG_DEBUG(("Find 'foobar' from %s", s1));
+  if (silc_string_match(s1, "foobar"))
+    goto err;
+  SILC_LOG_DEBUG(("Regex not found (Ok)"));
+
+  /* HEX to data, data to HEX tests */
+  for (i = 0; i < sizeof(t); i++)
+    t[i] = i;
+  silc_data2hex(t, sizeof(t), h, sizeof(h));
+  silc_hex2data(h, t, sizeof(t), &len);
+  silc_snprintf(h, sizeof(h), "010203ffabdef9ab");
+  silc_hex2data(h, t, sizeof(t), &len);
+  silc_data2hex(t, sizeof(t), h, sizeof(h));
+
+  /* snprintf test */
+  silc_snprintf(str, sizeof(str), "This is %@ rendered\n",
+               render, "automatically");
+  SILC_LOG_DEBUG((str));
+  SILC_LOG_DEBUG(("This too %@ rendered", render, "is automatically"));
+
   success = TRUE;
 
  err:
   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
 
-  return success;
+  return !success;
 }