Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silcasn1 / tests / test_silcasn1.c
index 5f153aa31d7b80b9b896c3145b7c5a8935979c34..74bd8c807f142bdb34d1c1c4fc3c8c62f237c6e1 100644 (file)
@@ -1,16 +1,16 @@
-#include "silcincludes.h"
+#include "silc.h"
 
 /*
 silc_asn1_encode(asn1, node,
-                SILC_ASN1_BOOLEAN(bool),
+                SILC_ASN1_BOOLEAN(SilcBool),
                 SILC_ASN1_END);
 silc_asn1_encode(asn1, dest,
                 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
                   SILC_ASN1_SEQUENCE_T(0, 9),
                     SILC_ASN1_SEQUENCE,
                       SILC_ASN1_ANY_T(0, 33, node),
-                      SILC_ASN1_BOOLEAN_T(0, 4, bool),
-                      SILC_ASN1_BOOLEAN(bool),
+                      SILC_ASN1_BOOLEAN_T(0, 4, boolv),
+                      SILC_ASN1_BOOLEAN(SilcBool),
                     SILC_ASN1_END,
                   SILC_ASN1_END,
                 SILC_ASN1_END);
@@ -23,15 +23,15 @@ silc_asn1_encode(asn1, dest,
 
 /*
 silc_asn1_encode(asn1, node,
-                SILC_ASN1_BOOLEAN(bool),
+                SILC_ASN1_BOOLEAN(SilcBool),
                 SILC_ASN1_END);
 silc_asn1_encode(asn1, dest,
                 SILC_ASN1_SEQUENCE_T(SILC_ASN1_PRIVATE, 101),
                   SILC_ASN1_SEQUENCE_T(0, 9),
                     SILC_ASN1_SEQUENCE,
                       SILC_ASN1_ANY_T(SILC_ASN1_EXPLICIT, 33, node),
-                      SILC_ASN1_BOOLEAN_T(0, 4, bool),
-                      SILC_ASN1_BOOLEAN(bool),
+                      SILC_ASN1_BOOLEAN_T(0, 4, boolv),
+                      SILC_ASN1_BOOLEAN(SilcBool),
                     SILC_ASN1_END,
                   SILC_ASN1_END,
                 SILC_ASN1_END);
@@ -47,11 +47,14 @@ int main(int argc, char **argv)
 {
   SilcBufferStruct node, node2;
   SilcAsn1 asn1;
-  bool success = FALSE;
-  bool val = TRUE;
+  SilcBool success = FALSE;
+  SilcBool val = TRUE;
   int i;
   unsigned char *str;
-  SilcUInt32 str_len;
+  SilcUInt32 str_len, tmpint;
+  char tmp[32];
+  SilcRng rng;
+  SilcMPInt mpint, mpint2;
 
   memset(&node, 0, sizeof(node));
   memset(&node2, 0, sizeof(node2));
@@ -62,6 +65,10 @@ int main(int argc, char **argv)
     silc_log_set_debug_string("*asn1*,*ber*");
   }
 
+  silc_hash_register_default();
+  rng = silc_rng_alloc();
+  silc_rng_init(rng);
+
   SILC_LOG_DEBUG(("Allocating ASN.1 context"));
   asn1 = silc_asn1_alloc();
   if (!asn1)
@@ -323,6 +330,112 @@ int main(int argc, char **argv)
   printf("\n");
 
 
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
+  memset(tmp, 0, sizeof(tmp));
+  tmp[0] = 0xff;
+  silc_buffer_set(&node2, tmp, 1);
+  SILC_LOG_DEBUG(("Encoding success"));
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_SEQUENCE,
+                      SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
+                                              &node2),
+                    SILC_ASN1_END, SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  memset(&node2, 0, sizeof(node2));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_SEQUENCE,
+                      SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
+                                              &node2),
+                    SILC_ASN1_END, SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Boolean val %d", node2.data[0]));
+  if (node2.data[0] != 0xff) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Decoding success"));
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
+  memset(tmp, 0, sizeof(tmp));
+  tmp[0] = 0xff;
+  silc_buffer_set(&node2, tmp, 1);
+  SILC_LOG_DEBUG(("Encoding success"));
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_SEQUENCE,
+                      SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
+                                              &node2),
+                    SILC_ASN1_END, SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  memset(&node2, 0, sizeof(node2));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_SEQUENCE,
+                      SILC_ASN1_BOOLEAN(&val),
+                    SILC_ASN1_END, SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Decoding success"));
+  SILC_LOG_DEBUG(("Boolean val %d", val));
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree (ANY_PRIMITIVE)"));
+  memset(tmp, 0, sizeof(tmp));
+  tmp[0] = 0xff;
+  silc_buffer_set(&node2, tmp, 1);
+  SILC_LOG_DEBUG(("Encoding success"));
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_SEQUENCE,
+                      SILC_ASN1_BOOLEAN(val),
+                    SILC_ASN1_END, SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  memset(&node2, 0, sizeof(node2));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree (ANY_PRIMITIVE)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_SEQUENCE,
+                      SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_BOOLEAN,
+                                              &node2),
+                    SILC_ASN1_END, SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Boolean val %d", node2.data[0]));
+  if (node2.data[0] != 0xff) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Decoding success"));
+  memset(&node2, 0, sizeof(node2));
+  printf("\n");
+
+
   memset(&node, 0, sizeof(node));
   SILC_LOG_DEBUG(("Encoding ASN.1 tree 6"));
   success =
@@ -476,6 +589,7 @@ int main(int argc, char **argv)
   memset(&node, 0, sizeof(node));
   printf("\n");
 
+
   memset(&node, 0, sizeof(node));
   SILC_LOG_DEBUG(("Encoding ASN.1 tree 9"));
   success =
@@ -534,9 +648,107 @@ int main(int argc, char **argv)
   SILC_LOG_DEBUG(("Ooctet-string %s, len %d", str, str_len));
   printf("\n");
 
-#endif
+
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree 10 (INTEGER)"));
+  str = silc_rng_get_rn_data(rng, 256);
+  silc_mp_init(&mpint);
+  silc_mp_init(&mpint2);
+  silc_mp_bin2mp(str, 256, &mpint);
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_INT(&mpint),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree 10 (INTEGER)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_INT(&mpint2),
+                    SILC_ASN1_END);
+  if (silc_mp_cmp(&mpint, &mpint2) != 0) {
+    SILC_LOG_DEBUG(("INTEGER MISMATCH"));
+    goto out;
+  }
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Decoding success"));
+  printf("\n");
+
+
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree 11 (OID)"));
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
+                    SILC_ASN1_OID("1.2.840.113549"),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree 11 (OID)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_OPTS(SILC_ASN1_ACCUMUL),
+                    SILC_ASN1_OID(&str),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Decoding success"));
+  SILC_LOG_DEBUG(("OID %s", str));
+  printf("\n");
+
+
+  memset(&node, 0, sizeof(node));
+  SILC_LOG_DEBUG(("Encoding ASN.1 tree 12 (SHORT INTEGER)"));
+  str_len = 198761;
+  tmpint = 0;
+  SILC_LOG_DEBUG(("Short integer: %d", str_len));
+  SILC_LOG_DEBUG(("Short integer: %d", tmpint));
+  success =
+    silc_asn1_encode(asn1, &node,
+                    SILC_ASN1_SHORT_INT(str_len),
+                    SILC_ASN1_SHORT_INT_T(SILC_ASN1_IMPLICIT, 100, tmpint),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Encoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Encoding success"));
+  SILC_LOG_HEXDUMP(("ASN.1 tree"), node.data, silc_buffer_len(&node));
+  SILC_LOG_DEBUG(("Decoding ASN.1 tree 12 (SHORT INTEGER)"));
+  success =
+    silc_asn1_decode(asn1, &node,
+                    SILC_ASN1_SHORT_INT(&str_len),
+                    SILC_ASN1_SHORT_INT_T(SILC_ASN1_IMPLICIT, 100, &tmpint),
+                    SILC_ASN1_END);
+  if (!success) {
+    SILC_LOG_DEBUG(("Decoding failed"));
+    goto out;
+  }
+  SILC_LOG_DEBUG(("Short integer: %d", str_len));
+  SILC_LOG_DEBUG(("Short integer: %d", tmpint));
+  SILC_LOG_DEBUG(("Decoding success"));
+  printf("\n");
+
+#endif /* 1 */
   silc_asn1_free(asn1);
 
+  success = TRUE;
  out:
-  exit(success);
+  SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
+  fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
+
+  return success;
 }