Annotation of embedaddon/strongswan/src/libstrongswan/tests/suites/test_asn1.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2013 Andreas Steffen
! 3: * HSR Hochschule fuer Technik Rapperswil
! 4: *
! 5: * This program is free software; you can redistribute it and/or modify it
! 6: * under the terms of the GNU General Public License as published by the
! 7: * Free Software Foundation; either version 2 of the License, or (at your
! 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
! 9: *
! 10: * This program is distributed in the hope that it will be useful, but
! 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
! 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
! 13: * for more details.
! 14: */
! 15:
! 16:
! 17: #include "test_suite.h"
! 18:
! 19: #include <asn1/asn1.h>
! 20: #include <asn1/oid.h>
! 21: #include <utils/chunk.h>
! 22:
! 23: /*******************************************************************************
! 24: * algorithm_identifier
! 25: */
! 26:
! 27: START_TEST(test_asn1_algorithmIdentifier)
! 28: {
! 29: typedef struct {
! 30: int n;
! 31: chunk_t algid;
! 32: } testdata_t;
! 33:
! 34: testdata_t test[] = {
! 35: { OID_ECDSA_WITH_SHA1, chunk_from_chars(0x30, 0x09, 0x06, 0x07,
! 36: 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
! 37: { OID_SHA1_WITH_RSA, chunk_from_chars(0x30, 0x0d, 0x06, 0x09,
! 38: 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05, 0x05, 0x00) },
! 39: };
! 40:
! 41: chunk_t algid;
! 42: int i;
! 43:
! 44: for (i = 0; i < countof(test); i++)
! 45: {
! 46: algid = asn1_algorithmIdentifier(test[i].n);
! 47: ck_assert(chunk_equals(algid, test[i].algid));
! 48: free(algid.ptr);
! 49: }
! 50: }
! 51: END_TEST
! 52:
! 53: /*******************************************************************************
! 54: * parse_algorithm_identifier
! 55: */
! 56:
! 57: START_TEST(test_asn1_parse_algorithmIdentifier)
! 58: {
! 59: typedef struct {
! 60: int alg;
! 61: bool empty;
! 62: chunk_t parameters;
! 63: } testdata_t;
! 64:
! 65: testdata_t test[] = {
! 66: { OID_ECDSA_WITH_SHA1, TRUE, chunk_empty },
! 67: { OID_SHA1_WITH_RSA, TRUE, chunk_from_chars(0x05, 0x00) },
! 68: { OID_3DES_EDE_CBC, FALSE, chunk_from_chars(0x04, 0x01, 0xaa) },
! 69: { OID_PBKDF2, FALSE, chunk_from_chars(0x30, 0x01, 0xaa) }
! 70: };
! 71:
! 72: chunk_t algid, parameters;
! 73: int i, alg;
! 74:
! 75: for (i = 0; i < countof(test); i++)
! 76: {
! 77: algid = asn1_wrap(ASN1_SEQUENCE, "mc",
! 78: asn1_build_known_oid(test[i].alg), test[i].parameters);
! 79: parameters = chunk_empty;
! 80: if (i == 2)
! 81: {
! 82: alg = asn1_parse_algorithmIdentifier(algid, 0, NULL);
! 83: }
! 84: else
! 85: {
! 86: alg = asn1_parse_algorithmIdentifier(algid, 0, ¶meters);
! 87: if (test[i].empty)
! 88: {
! 89: ck_assert(parameters.len == 0 && parameters.ptr == NULL);
! 90: }
! 91: else
! 92: {
! 93: ck_assert(chunk_equals(parameters, test[i].parameters));
! 94: }
! 95: }
! 96: ck_assert(alg == test[i].alg);
! 97: chunk_free(&algid);
! 98: }
! 99: }
! 100: END_TEST
! 101:
! 102: /*******************************************************************************
! 103: * known_oid
! 104: */
! 105:
! 106: START_TEST(test_asn1_known_oid)
! 107: {
! 108: typedef struct {
! 109: int n;
! 110: chunk_t oid;
! 111: } testdata_t;
! 112:
! 113: testdata_t test[] = {
! 114: { OID_UNKNOWN, chunk_empty },
! 115: { OID_UNKNOWN, chunk_from_chars(0x55, 0x04, 0x02) },
! 116: { OID_COUNTRY, chunk_from_chars(0x55, 0x04, 0x06) },
! 117: { OID_STRONGSWAN, chunk_from_chars(0x2b, 0x06, 0x01, 0x04, 0x01,
! 118: 0x82, 0xa0, 0x2a, 0x01) }
! 119: };
! 120:
! 121: int i;
! 122:
! 123: for (i = 0; i < countof(test); i++)
! 124: {
! 125: ck_assert(asn1_known_oid(test[i].oid) == test[i].n);
! 126: }
! 127: }
! 128: END_TEST
! 129:
! 130: /*******************************************************************************
! 131: * build_known_oid
! 132: */
! 133:
! 134: START_TEST(test_asn1_build_known_oid)
! 135: {
! 136: typedef struct {
! 137: int n;
! 138: chunk_t oid;
! 139: } testdata_t;
! 140:
! 141: testdata_t test[] = {
! 142: { OID_UNKNOWN, chunk_empty },
! 143: { OID_MAX, chunk_empty },
! 144: { OID_COUNTRY, chunk_from_chars(0x06, 0x03, 0x55, 0x04, 0x06) },
! 145: { OID_STRONGSWAN, chunk_from_chars(0x06, 0x09, 0x2b, 0x06, 0x01, 0x04,
! 146: 0x01, 0x82, 0xa0, 0x2a, 0x01) }
! 147: };
! 148:
! 149: int i;
! 150: chunk_t oid = chunk_empty;
! 151:
! 152: for (i = 0; i < countof(test); i++)
! 153: {
! 154: oid = asn1_build_known_oid(test[i].n);
! 155: if (test[i].oid.len == 0)
! 156: {
! 157: ck_assert(oid.len == 0 && oid.ptr == NULL);
! 158: }
! 159: else
! 160: {
! 161: ck_assert(chunk_equals(oid, test[i].oid));
! 162: chunk_free(&oid);
! 163: }
! 164: }
! 165: }
! 166: END_TEST
! 167:
! 168: /*******************************************************************************
! 169: * oid_from_string
! 170: */
! 171:
! 172: START_TEST(test_asn1_oid_from_string)
! 173: {
! 174: typedef struct {
! 175: char *string;
! 176: chunk_t oid;
! 177: } testdata_t;
! 178:
! 179: testdata_t test[] = {
! 180: { "", chunk_empty },
! 181: { " ", chunk_empty },
! 182: { "0.2.262.1", chunk_from_chars(
! 183: 0x02, 0x82, 0x06, 0x01) },
! 184: { "1.2.840.10045.4.1", chunk_from_chars(
! 185: 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
! 186: { "1.3.6.1.4.1.36906.1", chunk_from_chars(
! 187: 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
! 188: { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
! 189: 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
! 190: { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
! 191: chunk_from_chars(0x0a,0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
! 192: 0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
! 193: 0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
! 194: { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
! 195: "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
! 196: "2097153", chunk_from_chars(
! 197: 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
! 198: 0x81, 0x00, 0x81, 0x01, 0x81, 0x02, 0x81, 0x03, 0x81, 0x04,
! 199: 0x81, 0x05, 0x81, 0x06, 0x81, 0x07, 0x81, 0x08, 0x81, 0x09,
! 200: 0x82, 0x00, 0x82, 0x01, 0x82, 0x02, 0x82, 0x03, 0x82, 0x04,
! 201: 0x82, 0x05, 0x82, 0x06, 0x82, 0x07, 0x82, 0x08, 0x82, 0x09,
! 202: 0x83, 0x00, 0x83, 0x01, 0x83, 0x02, 0x83, 0x03, 0x83, 0x04,
! 203: 0x81, 0x80, 0x80, 0x01) },
! 204: { "0.1.2.3.4.5.6.7.8.9.10.128.129.130.131.132.133.134.135.136.137."
! 205: "256.257.258.259.260.261.262.263.264.265.384.385.386.387.388."
! 206: "1.2097153", chunk_empty },
! 207: { "1.a.2.b.3", chunk_empty }
! 208: };
! 209:
! 210: int i;
! 211: chunk_t oid = chunk_empty;
! 212:
! 213: for (i = 0; i < countof(test); i++)
! 214: {
! 215: oid = asn1_oid_from_string(test[i].string);
! 216: if (test[i].oid.len == 0)
! 217: {
! 218: ck_assert(oid.len == 0 && oid.ptr == NULL);
! 219: }
! 220: else
! 221: {
! 222: ck_assert(chunk_equals(oid, test[i].oid));
! 223: chunk_free(&oid);
! 224: }
! 225: }
! 226: }
! 227: END_TEST
! 228:
! 229: /*******************************************************************************
! 230: * oid_to_string
! 231: */
! 232:
! 233: START_TEST(test_asn1_oid_to_string)
! 234: {
! 235: typedef struct {
! 236: char *string;
! 237: chunk_t oid;
! 238: } testdata_t;
! 239:
! 240: testdata_t test[] = {
! 241: { NULL, chunk_empty },
! 242: { "0.2.262.1", chunk_from_chars(
! 243: 0x02, 0x82, 0x06, 0x01) },
! 244: { "1.2.840.10045.4.1", chunk_from_chars(
! 245: 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x01) },
! 246: { "1.3.6.1.4.1.36906.1", chunk_from_chars(
! 247: 0x2b, 0x06, 0x01, 0x04, 0x01, 0x82, 0xa0, 0x2a, 0x01) },
! 248: { "2.16.840.1.101.3.4.2.1", chunk_from_chars(
! 249: 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01) },
! 250: { "0.10.100.1000.10000.100000.1000000.10000000.100000000.268435455",
! 251: chunk_from_chars( 0x0a, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d,
! 252: 0x20, 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00,
! 253: 0xaf, 0xd7, 0xc2, 0x00, 0xff, 0xff, 0xff, 0x7f) },
! 254: { NULL, chunk_from_chars(
! 255: 0x0a, 0x02, 0x64, 0x87, 0x68, 0xce, 0x10, 0x86, 0x8d, 0x20,
! 256: 0xbd, 0x84, 0x40, 0x84, 0xe2, 0xad, 0x00, 0xaf, 0xd7, 0xc2, 0x00,
! 257: 0xff, 0xff, 0xff, 0x7f) },
! 258: { NULL, chunk_from_chars(0x0a, 0x87) }
! 259: };
! 260:
! 261: int i;
! 262: char *string = NULL;
! 263:
! 264: for (i = 0; i < countof(test); i++)
! 265: {
! 266: string = asn1_oid_to_string(test[i].oid);
! 267: if (test[i].string == NULL)
! 268: {
! 269: ck_assert(string == NULL);
! 270: }
! 271: else
! 272: {
! 273: ck_assert(streq(string, test[i].string));
! 274: free(string);
! 275: }
! 276: }
! 277: }
! 278: END_TEST
! 279:
! 280: /*******************************************************************************
! 281: * length
! 282: */
! 283:
! 284: START_TEST(test_asn1_length)
! 285: {
! 286: chunk_t a;
! 287:
! 288: a = chunk_empty;
! 289: ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
! 290:
! 291: a = chunk_from_chars(0x04);
! 292: ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
! 293:
! 294: a = chunk_from_chars(0x04, 0x00);
! 295: ck_assert(asn1_length(&a) == 0);
! 296:
! 297: a = chunk_from_chars(0x04, 0x01);
! 298: ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
! 299:
! 300: a = chunk_from_chars(0x04, 0x01, 0xaa);
! 301: ck_assert(asn1_length(&a) == 1);
! 302:
! 303: a = chunk_from_chars(0x04, 0x7f, 0xaa);
! 304: a.len = 2 + 127;
! 305: ck_assert(asn1_length(&a) == 127);
! 306:
! 307: a = chunk_from_chars(0x04, 0x80, 0xaa);
! 308: a.len = 2 + 128;
! 309: ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
! 310:
! 311: a = chunk_from_chars(0x04, 0x81);
! 312: ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
! 313:
! 314: a = chunk_from_chars(0x04, 0x81, 0x00);
! 315: ck_assert(asn1_length(&a) == 0);
! 316:
! 317: a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
! 318: ck_assert(asn1_length(&a) == ASN1_INVALID_LENGTH);
! 319:
! 320: a = chunk_from_chars(0x04, 0x81, 0x80, 0xaa);
! 321: a.len = 3 + 128;
! 322: ck_assert(asn1_length(&a) == 128);
! 323:
! 324: a = chunk_from_chars(0x04, 0x82, 0x01, 0x02, 0xaa);
! 325: a.len = 4 + 258;
! 326: ck_assert(asn1_length(&a) == 258);
! 327:
! 328: a = chunk_from_chars(0x04, 0x83, 0x01, 0x02, 0x03, 0xaa);
! 329: a.len = 5 + 66051;
! 330: ck_assert(asn1_length(&a) == 66051);
! 331:
! 332: a = chunk_from_chars(0x04, 0x84, 0x01, 0x02, 0x03, 0x04, 0xaa);
! 333: a.len = 6 + 16909060;
! 334: ck_assert(asn1_length(&a) == 16909060);
! 335:
! 336: /* largest chunk on 32 bit system */
! 337: a = chunk_from_chars(0x04, 0x84, 0xff, 0xff, 0xff, 0xf9, 0xaa);
! 338: a.len = 4294967295U;
! 339: ck_assert(asn1_length(&a) == 4294967289U);
! 340:
! 341: }
! 342: END_TEST
! 343:
! 344: /*******************************************************************************
! 345: * unwrap
! 346: */
! 347:
! 348: START_TEST(test_asn1_unwrap)
! 349: {
! 350: chunk_t c0 = chunk_from_chars(0x30);
! 351: chunk_t c1 = chunk_from_chars(0x30, 0x01, 0xaa);
! 352: chunk_t c2 = chunk_from_chars(0x30, 0x80);
! 353: chunk_t c3 = chunk_from_chars(0x30, 0x81);
! 354: chunk_t c4 = chunk_from_chars(0x30, 0x81, 0x01, 0xaa);
! 355: chunk_t c5 = chunk_from_chars(0x30, 0x81, 0x02, 0xaa);
! 356:
! 357: chunk_t inner;
! 358: chunk_t inner_ref = chunk_from_chars(0xaa);
! 359:
! 360: ck_assert(asn1_unwrap(&c0, &inner) == ASN1_INVALID);
! 361:
! 362: ck_assert(asn1_unwrap(&c1, &inner) == ASN1_SEQUENCE);
! 363:
! 364: ck_assert(chunk_equals(inner, inner_ref));
! 365:
! 366: ck_assert(asn1_unwrap(&c2, &inner) == ASN1_INVALID);
! 367:
! 368: ck_assert(asn1_unwrap(&c3, &inner) == ASN1_INVALID);
! 369:
! 370: ck_assert(asn1_unwrap(&c4, &inner) == ASN1_SEQUENCE);
! 371:
! 372: ck_assert(chunk_equals(inner, inner_ref));
! 373:
! 374: ck_assert(asn1_unwrap(&c5, &inner) == ASN1_INVALID);
! 375: }
! 376: END_TEST
! 377:
! 378: /*******************************************************************************
! 379: * is_asn1
! 380: */
! 381:
! 382: START_TEST(test_is_asn1)
! 383: {
! 384: typedef struct {
! 385: bool asn1;
! 386: chunk_t chunk;
! 387: } testdata_t;
! 388:
! 389: u_char buf[8];
! 390: chunk_t chunk_zero = { buf, 0 };
! 391: chunk_t chunk_mean = { 0, 1 };
! 392:
! 393: testdata_t test[] = {
! 394: { FALSE, chunk_zero },
! 395: { FALSE, chunk_empty },
! 396: { FALSE, chunk_mean },
! 397: { TRUE, chunk_from_chars(0x30, 0x00) },
! 398: { TRUE, chunk_from_chars(0x31, 0x00) },
! 399: { TRUE, chunk_from_chars(0x04, 0x00) },
! 400: { FALSE, chunk_from_chars(0x02, 0x00) },
! 401: { FALSE, chunk_from_chars(0x30, 0x01) },
! 402: { FALSE, chunk_from_chars(0x30, 0x80) },
! 403: { TRUE, chunk_from_chars(0x30, 0x01, 0xa1) },
! 404: { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2) },
! 405: { TRUE, chunk_from_chars(0x30, 0x01, 0xa1, 0x0a) },
! 406: { FALSE, chunk_from_chars(0x30, 0x01, 0xa1, 0xa2, 0x0a) },
! 407: };
! 408:
! 409: int i;
! 410:
! 411: for (i = 0; i < countof(test); i++)
! 412: {
! 413: ck_assert(is_asn1(test[i].chunk) == test[i].asn1);
! 414: }
! 415: }
! 416: END_TEST
! 417:
! 418: /*******************************************************************************
! 419: * is_printablestring
! 420: */
! 421:
! 422: START_TEST(test_asn1_is_printablestring)
! 423: {
! 424: typedef struct {
! 425: bool printable;
! 426: char *string;
! 427: } testdata_t;
! 428:
! 429:
! 430: testdata_t test[] = {
! 431: { TRUE, "" },
! 432: { TRUE, "Z" },
! 433: { FALSE, "Z#" },
! 434: { FALSE, "&Z" },
! 435: { FALSE, "Z@z" },
! 436: { FALSE, "!" }, { FALSE, "*" }, { FALSE, "$" }, { FALSE, "%" },
! 437: { FALSE, "[" }, { FALSE, "]" }, { FALSE, "{" }, { FALSE, "}" },
! 438: { FALSE, "|" }, { FALSE, "~" }, { FALSE, "^" }, { FALSE, "_" },
! 439: { FALSE, "\"" }, { FALSE, "\\" }, { FALSE, "ä" }, { FALSE, "à" },
! 440: { TRUE, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
! 441: "0123456789 '()+,-./:=?" },
! 442: };
! 443:
! 444: chunk_t chunk;
! 445: int i;
! 446:
! 447: ck_assert(asn1_is_printablestring(chunk_empty));
! 448:
! 449: for (i = 0; i < countof(test); i++)
! 450: {
! 451: chunk = chunk_from_str(test[i].string);
! 452: ck_assert(asn1_is_printablestring(chunk) == test[i].printable);
! 453: }
! 454: }
! 455: END_TEST
! 456:
! 457: /*******************************************************************************
! 458: * to_time
! 459: */
! 460:
! 461: START_TEST(test_asn1_to_time)
! 462: {
! 463: typedef struct {
! 464: time_t time;
! 465: uint8_t type;
! 466: char *string;
! 467: } testdata_t;
! 468:
! 469: testdata_t test[] = {
! 470: { 352980, 0x18, "197001050203Z" },
! 471: { 352984, 0x18, "19700105020304Z" },
! 472: { 352980, 0x17, "7001050203Z" },
! 473: { 347580, 0x17, "7001050203+0130" },
! 474: { 358380, 0x17, "7001050203-0130" },
! 475: { 352984, 0x17, "700105020304Z" },
! 476: { 347584, 0x17, "700105020304+0130" },
! 477: { 358384, 0x17, "700105020304-0130" },
! 478: { 0, 0x17, "700105020304+01" },
! 479: { 0, 0x17, "700105020304-01" },
! 480: { 0, 0x17, "700105020304" },
! 481: { 0, 0x17, "70010502Z" },
! 482: { 0, 0x17, "7001050203xxZ" },
! 483: { 0, 0x17, "7000050203Z" },
! 484: { 0, 0x17, "7013050203Z" },
! 485: { 0, 0x17, "7001004203Z" },
! 486: { 0, 0x17, "7001320203Z" },
! 487: { 0, 0x17, "700101-103Z" },
! 488: { 0, 0x17, "7001016003Z" },
! 489: { 0, 0x17, "70010102-1Z" },
! 490: { 0, 0x17, "7001010260Z" },
! 491: { 0, 0x17, "7001010203-1Z" },
! 492: { 0, 0x17, "700101020361Z" },
! 493: { -631152000, 0x17, "500101000000Z" }, /* UTCTime min */
! 494: { 59, 0x17, "691231235959-0001" },
! 495: { -1, 0x17, "691231235959Z" },
! 496: { 0, 0x17, "700101000000Z" },
! 497: { -60, 0x17, "700101000000+0001" },
! 498: { 2524607999UL, 0x17, "491231235959Z" }, /* UTCTime max */
! 499: { 5097600, 0x17, "7003010000Z" },
! 500: { 68256000, 0x17, "7203010000Z" },
! 501: { 951868800, 0x17, "0003010000Z" },
! 502: { 4107542400UL, 0x18, "210003010000Z" }
! 503: };
! 504:
! 505: int i;
! 506: chunk_t chunk;
! 507:
! 508: for (i = 0; i < countof(test); i++)
! 509: {
! 510: if (sizeof(time_t) == 4 && test[i].time < 0)
! 511: {
! 512: continue;
! 513: }
! 514: chunk = chunk_from_str(test[i].string);
! 515: ck_assert(asn1_to_time(&chunk, test[i].type) == test[i].time);
! 516: }
! 517: }
! 518: END_TEST
! 519:
! 520: /*******************************************************************************
! 521: * from_time
! 522: */
! 523:
! 524: START_TEST(test_asn1_from_time)
! 525: {
! 526: typedef struct {
! 527: time_t time;
! 528: uint8_t type;
! 529: chunk_t chunk;
! 530: } testdata_t;
! 531:
! 532: testdata_t test[] = {
! 533: { 352984, 0x18, chunk_from_chars(
! 534: 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
! 535: 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
! 536: { 352984, 0x17, chunk_from_chars(
! 537: 0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
! 538: 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
! 539: { 1078099200, 0x17, chunk_from_chars(
! 540: 0x17, 0x0d, 0x30, 0x34, 0x30, 0x33, 0x30, 0x31,
! 541: 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) },
! 542: { 4107542400UL, 0x18, chunk_from_chars(
! 543: 0x18, 0x0f, 0x32, 0x31, 0x30, 0x30, 0x30, 0x33, 0x30, 0x31,
! 544: 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x5a) }
! 545: };
! 546:
! 547: int i;
! 548: chunk_t chunk;
! 549:
! 550: for (i = 0; i < countof(test); i++)
! 551: {
! 552: if (sizeof(time_t) == 4 && test[i].time < 0)
! 553: {
! 554: continue;
! 555: }
! 556: chunk = asn1_from_time(&test[i].time, test[i].type);
! 557: ck_assert(chunk_equals(chunk, test[i].chunk));
! 558: free(chunk.ptr);
! 559: }
! 560: }
! 561: END_TEST
! 562:
! 563: /*******************************************************************************
! 564: * parse_time
! 565: */
! 566:
! 567: START_TEST(test_asn1_parse_time)
! 568: {
! 569: typedef struct {
! 570: time_t time;
! 571: chunk_t chunk;
! 572: } testdata_t;
! 573:
! 574: testdata_t test[] = {
! 575: { 352984, chunk_from_chars(
! 576: 0x18, 0x0f, 0x31, 0x39, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
! 577: 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
! 578: { 352984, chunk_from_chars(
! 579: 0x17, 0x0d, 0x37, 0x30, 0x30, 0x31, 0x30, 0x35,
! 580: 0x30, 0x32, 0x30, 0x33, 0x30, 0x34, 0x5a) },
! 581: { 0, chunk_from_chars(0x05, 0x00) }
! 582: };
! 583:
! 584: int i;
! 585:
! 586: for (i = 0; i < countof(test); i++)
! 587: {
! 588: ck_assert(asn1_parse_time(test[i].chunk, 0) == test[i].time);
! 589: }
! 590: }
! 591: END_TEST
! 592:
! 593: /*******************************************************************************
! 594: * build_object
! 595: */
! 596:
! 597: START_TEST(test_asn1_build_object)
! 598: {
! 599: typedef struct {
! 600: size_t len;
! 601: size_t size;
! 602: u_char *b;
! 603: } testdata_t;
! 604:
! 605: u_char b0[] = { 0x05, 0x00 };
! 606: u_char b1[] = { 0x04, 0x7f };
! 607: u_char b2[] = { 0x04, 0x81, 0x80 };
! 608: u_char b3[] = { 0x04, 0x81, 0xff };
! 609: u_char b4[] = { 0x04, 0x82, 0x01, 0x00 };
! 610: u_char b5[] = { 0x04, 0x82, 0xff, 0xff };
! 611: u_char b6[] = { 0x04, 0x83, 0x01, 0x00, 0x00 };
! 612:
! 613: testdata_t test[] = {
! 614: { 0, sizeof(b0), b0 },
! 615: { 127, sizeof(b1), b1 },
! 616: { 128, sizeof(b2), b2 },
! 617: { 255, sizeof(b3), b3 },
! 618: { 256, sizeof(b4), b4 },
! 619: { 65535, sizeof(b5), b5 },
! 620: { 65536, sizeof(b6), b6 }
! 621: };
! 622:
! 623: chunk_t a = chunk_empty;
! 624: u_char *pos;
! 625: int i;
! 626:
! 627: for (i = 0; i < countof(test); i++)
! 628: {
! 629: pos = asn1_build_object(&a, test[i].b[0], test[i].len);
! 630: ck_assert(pos == (a.ptr + test[i].size));
! 631: ck_assert(a.len == test[i].size + test[i].len);
! 632: ck_assert(memeq(a.ptr, test[i].b, test[i].size));
! 633: chunk_free(&a);
! 634: }
! 635: }
! 636: END_TEST
! 637:
! 638: /*******************************************************************************
! 639: * simple_object
! 640: */
! 641:
! 642: START_TEST(test_asn1_simple_object)
! 643: {
! 644: chunk_t a = chunk_empty;
! 645: chunk_t b = chunk_from_chars(0x04, 0x05, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
! 646: chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4, 0xa5);
! 647:
! 648: a = asn1_simple_object(0x04, c);
! 649: ck_assert(chunk_equals(a, b));
! 650: chunk_free(&a);
! 651: }
! 652: END_TEST
! 653:
! 654: /*******************************************************************************
! 655: * parse_simple_object
! 656: */
! 657:
! 658: START_TEST(test_asn1_parse_simple_object)
! 659: {
! 660: typedef struct {
! 661: bool res;
! 662: int type;
! 663: chunk_t chunk;
! 664: } testdata_t;
! 665:
! 666: testdata_t test[] = {
! 667: { FALSE, 0x04, chunk_from_chars(0x04) },
! 668: { FALSE, 0x04, chunk_from_chars(0x02, 0x01, 0x55) },
! 669: { FALSE, 0x04, chunk_from_chars(0x04, 0x01) },
! 670: { TRUE, 0x04, chunk_from_chars(0x04, 0x01, 0x55) },
! 671: { TRUE, 0x06, chunk_from_chars(0x06, 0x02, 0x55, 0x03) },
! 672: { TRUE, 0x06, chunk_from_chars(0x06, 0x00) },
! 673: { TRUE, 0x13, chunk_from_chars(0x13, 0x01, 0x55), }
! 674: };
! 675:
! 676: int i;
! 677: bool res;
! 678:
! 679: for (i = 0; i < countof(test); i++)
! 680: {
! 681: res = asn1_parse_simple_object(&test[i].chunk, test[i].type, 0, "test");
! 682: ck_assert(res == test[i].res);
! 683: if (res && test[i].chunk.len)
! 684: {
! 685: ck_assert(*test[i].chunk.ptr == 0x55);
! 686: }
! 687: }
! 688: }
! 689: END_TEST
! 690:
! 691: /*******************************************************************************
! 692: * bitstring
! 693: */
! 694:
! 695: START_TEST(test_asn1_bitstring)
! 696: {
! 697: chunk_t a = chunk_empty;
! 698: chunk_t b = chunk_from_chars(0x03, 0x05, 0x00, 0xa1, 0xa2, 0xa3, 0xa4);
! 699: chunk_t c = chunk_from_chars(0xa1, 0xa2, 0xa3, 0xa4);
! 700: chunk_t d = chunk_clone(c);
! 701:
! 702: a = asn1_bitstring("c", c);
! 703: ck_assert(chunk_equals(a, b));
! 704: chunk_free(&a);
! 705:
! 706: a = asn1_bitstring("m", d);
! 707: ck_assert(chunk_equals(a, b));
! 708: chunk_free(&a);
! 709: }
! 710: END_TEST
! 711:
! 712: /*******************************************************************************
! 713: * integer
! 714: */
! 715:
! 716: START_TEST(test_asn1_integer)
! 717: {
! 718: typedef struct {
! 719: chunk_t b;
! 720: chunk_t c;
! 721: } testdata_t;
! 722:
! 723: chunk_t b0 = chunk_from_chars(0x02, 0x01, 0x00);
! 724: chunk_t b1 = chunk_from_chars(0x02, 0x01, 0x7f);
! 725: chunk_t b2 = chunk_from_chars(0x02, 0x02, 0x00, 0x80);
! 726:
! 727: chunk_t c0 = chunk_empty;
! 728: chunk_t c1 = chunk_from_chars(0x7f);
! 729: chunk_t c2 = chunk_from_chars(0x80);
! 730: chunk_t c3 = chunk_from_chars(0x00, 0x80);
! 731:
! 732: testdata_t test[] = {
! 733: { b0, c0 },
! 734: { b1, c1 },
! 735: { b2, c2 },
! 736: { b2, c3 }
! 737: };
! 738:
! 739: chunk_t a = chunk_empty;
! 740: int i;
! 741:
! 742: for (i = 0; i < countof(test); i++)
! 743: {
! 744: a = asn1_integer("c", test[i].c);
! 745: ck_assert(chunk_equals(a, test[i].b));
! 746: chunk_free(&a);
! 747:
! 748: a = asn1_integer("m", chunk_clone(test[i].c));
! 749: ck_assert(chunk_equals(a, test[i].b));
! 750: chunk_free(&a);
! 751: }
! 752: }
! 753: END_TEST
! 754:
! 755: /*******************************************************************************
! 756: * parse_integer_uint64
! 757: */
! 758:
! 759: START_TEST(test_asn1_parse_integer_uint64)
! 760: {
! 761: struct {
! 762: uint64_t n;
! 763: chunk_t chunk;
! 764: } test[] = {
! 765: { 67305985ULL, chunk_from_chars(
! 766: 0x04, 0x03, 0x02, 0x01) },
! 767: { 578437695752307201ULL, chunk_from_chars(
! 768: 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01) },
! 769: { 18446744073709551615ULL, chunk_from_chars(
! 770: 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff) }
! 771: };
! 772:
! 773: int i;
! 774:
! 775: for (i = 0; i < countof(test); i++)
! 776: {
! 777: ck_assert(asn1_parse_integer_uint64(test[i].chunk) == test[i].n);
! 778: }
! 779: }
! 780: END_TEST
! 781:
! 782: /*******************************************************************************
! 783: * integer_from_uint64
! 784: */
! 785:
! 786: START_TEST(test_asn1_integer_from_uint64)
! 787: {
! 788: struct {
! 789: uint64_t n;
! 790: chunk_t chunk;
! 791: } test[] = {
! 792: { 0ULL, chunk_from_chars(0x00) },
! 793: { 255ULL, chunk_from_chars(0xff) },
! 794: { 256ULL, chunk_from_chars(0x01, 0x00) },
! 795: { 67305985ULL, chunk_from_chars(0x04, 0x03, 0x02, 0x01) },
! 796: { 578437695752307201ULL, chunk_from_chars(
! 797: 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01) },
! 798: { 18446744073709551615ULL, chunk_from_chars(
! 799: 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff) },
! 800: };
! 801: chunk_t asn;
! 802: int i;
! 803:
! 804: for (i = 0; i < countof(test); i++)
! 805: {
! 806: asn = asn1_integer_from_uint64(test[i].n);
! 807: ck_assert_chunk_eq(test[i].chunk, asn);
! 808: chunk_free(&asn);
! 809: }
! 810: }
! 811: END_TEST
! 812:
! 813: Suite *asn1_suite_create()
! 814: {
! 815: Suite *s;
! 816: TCase *tc;
! 817:
! 818: s = suite_create("asn1");
! 819:
! 820: tc = tcase_create("algorithmIdentifier");
! 821: tcase_add_test(tc, test_asn1_algorithmIdentifier);
! 822: suite_add_tcase(s, tc);
! 823:
! 824: tc = tcase_create("parse_algorithmIdentifier");
! 825: tcase_add_test(tc, test_asn1_parse_algorithmIdentifier);
! 826: suite_add_tcase(s, tc);
! 827:
! 828: tc = tcase_create("known_oid");
! 829: tcase_add_test(tc, test_asn1_known_oid);
! 830: suite_add_tcase(s, tc);
! 831:
! 832: tc = tcase_create("build_known_oid");
! 833: tcase_add_test(tc, test_asn1_build_known_oid);
! 834: suite_add_tcase(s, tc);
! 835:
! 836: tc = tcase_create("oid_from_string");
! 837: tcase_add_test(tc, test_asn1_oid_from_string);
! 838: suite_add_tcase(s, tc);
! 839:
! 840: tc = tcase_create("oid_to_string");
! 841: tcase_add_test(tc, test_asn1_oid_to_string);
! 842: suite_add_tcase(s, tc);
! 843:
! 844: tc = tcase_create("length");
! 845: tcase_add_test(tc, test_asn1_length);
! 846: suite_add_tcase(s, tc);
! 847:
! 848: tc = tcase_create("unwrap");
! 849: tcase_add_test(tc, test_asn1_unwrap);
! 850: suite_add_tcase(s, tc);
! 851:
! 852: tc = tcase_create("is_asn1");
! 853: tcase_add_test(tc, test_is_asn1);
! 854: suite_add_tcase(s, tc);
! 855:
! 856: tc = tcase_create("is_printablestring");
! 857: tcase_add_test(tc, test_asn1_is_printablestring);
! 858: suite_add_tcase(s, tc);
! 859:
! 860: tc = tcase_create("to_time");
! 861: tcase_add_test(tc, test_asn1_to_time);
! 862: suite_add_tcase(s, tc);
! 863:
! 864: tc = tcase_create("from_time");
! 865: tcase_add_test(tc, test_asn1_from_time);
! 866: suite_add_tcase(s, tc);
! 867:
! 868: tc = tcase_create("parse_time");
! 869: tcase_add_test(tc, test_asn1_parse_time);
! 870: suite_add_tcase(s, tc);
! 871:
! 872: tc = tcase_create("build_object");
! 873: tcase_add_test(tc, test_asn1_build_object);
! 874: suite_add_tcase(s, tc);
! 875:
! 876: tc = tcase_create("simple_object");
! 877: tcase_add_test(tc, test_asn1_simple_object);
! 878: suite_add_tcase(s, tc);
! 879:
! 880: tc = tcase_create("parse_simple_object");
! 881: tcase_add_test(tc, test_asn1_parse_simple_object);
! 882: suite_add_tcase(s, tc);
! 883:
! 884: tc = tcase_create("bitstring");
! 885: tcase_add_test(tc, test_asn1_bitstring);
! 886: suite_add_tcase(s, tc);
! 887:
! 888: tc = tcase_create("integer");
! 889: tcase_add_test(tc, test_asn1_integer);
! 890: suite_add_tcase(s, tc);
! 891:
! 892: tc = tcase_create("integer_uint64");
! 893: tcase_add_test(tc, test_asn1_parse_integer_uint64);
! 894: tcase_add_test(tc, test_asn1_integer_from_uint64);
! 895: suite_add_tcase(s, tc);
! 896:
! 897: return s;
! 898: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>