X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcasn1%2Ftests%2Ftest_silcasn1.c;h=c6266e1e2e311eb1d23f207a54b683d6aed507f1;hb=e7b6c157b80152bf9fb9266e6bdd93f9fb0db776;hp=3e2bd3ce72e16560ba873e69a6a7215fe649ee09;hpb=4a2208546ba22e3da99dc1a2adcf2102246d6c6f;p=silc.git diff --git a/lib/silcasn1/tests/test_silcasn1.c b/lib/silcasn1/tests/test_silcasn1.c index 3e2bd3ce..c6266e1e 100644 --- a/lib/silcasn1/tests/test_silcasn1.c +++ b/lib/silcasn1/tests/test_silcasn1.c @@ -48,11 +48,14 @@ int main(int argc, char **argv) SilcBufferStruct node, node2; SilcAsn1 asn1; SilcBool success = FALSE; - SilcBool val = TRUE; + SilcBool val = TRUE, nval; int i; - unsigned char *str; - SilcUInt32 str_len; + unsigned char *str, buf[512]; + SilcUInt32 str_len, tmpint, choice; char tmp[32]; + SilcRng rng; + SilcMPInt mpint, mpint2; + SilcStack stack; memset(&node, 0, sizeof(node)); memset(&node2, 0, sizeof(node2)); @@ -60,11 +63,17 @@ int main(int argc, char **argv) if (argc > 1 && !strcmp(argv[1], "-d")) { silc_log_debug(TRUE); silc_log_debug_hexdump(TRUE); - silc_log_set_debug_string("*asn1*,*ber*"); + silc_log_set_debug_string("*asn1*,*ber*,*stack*"); } + silc_hash_register_default(); + rng = silc_rng_alloc(); + silc_rng_init(rng); + + stack = silc_stack_alloc(0, NULL); + SILC_LOG_DEBUG(("Allocating ASN.1 context")); - asn1 = silc_asn1_alloc(); + asn1 = silc_asn1_alloc(stack); if (!asn1) goto out; @@ -174,7 +183,10 @@ int main(int argc, char **argv) SILC_ASN1_SEQUENCE_T(SILC_ASN1_EXPLICIT, 9), SILC_ASN1_SEQUENCE_T(SILC_ASN1_INDEFINITE, 0), SILC_ASN1_BOOLEAN_T(0, 4, &val), - SILC_ASN1_BOOLEAN(&val), + SILC_ASN1_CHOICE(&choice), + SILC_ASN1_SHORT_INT(&tmpint), + SILC_ASN1_BOOLEAN(&val), + SILC_ASN1_END, SILC_ASN1_END, SILC_ASN1_END, SILC_ASN1_END, SILC_ASN1_END); @@ -184,6 +196,9 @@ int main(int argc, char **argv) } SILC_LOG_DEBUG(("Decoding success")); SILC_LOG_DEBUG(("Boolean val %d", val)); + SILC_LOG_DEBUG(("Choice index %d", choice)); + if (choice != 2) + goto out; printf("\n"); @@ -583,6 +598,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 = @@ -597,7 +613,7 @@ int main(int argc, char **argv) SILC_ASN1_BOOLEAN_T(0, 100, val), SILC_ASN1_END, SILC_ASN1_SEQUENCE, - SILC_ASN1_NULL, + SILC_ASN1_NULL(TRUE), SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 0, val), SILC_ASN1_OCTET_STRING("foobar", 6), SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE, 43, val), @@ -624,7 +640,7 @@ int main(int argc, char **argv) SILC_ASN1_BOOLEAN_T(0, 100, &val), SILC_ASN1_END, SILC_ASN1_SEQUENCE, - SILC_ASN1_NULL, + SILC_ASN1_NULL(&nval), SILC_ASN1_BOOLEAN_T(SILC_ASN1_EXPLICIT, 0, &val), SILC_ASN1_OCTET_STRING(&str, &str_len), SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE, 43, &val), @@ -638,12 +654,114 @@ int main(int argc, char **argv) } SILC_LOG_DEBUG(("Decoding success")); SILC_LOG_DEBUG(("Boolean val %d", val)); + SILC_LOG_DEBUG(("NULL is present %s", nval ? "yes" : "no")); 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)")); + silc_rng_get_rn_data(rng, 256, buf, sizeof(buf)); + silc_mp_init(&mpint); + silc_mp_init(&mpint2); + silc_mp_bin2mp(buf, 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); + silc_rng_free(rng); + silc_hash_unregister_all(); + silc_stack_free(stack); + 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; }