Annotation of embedaddon/strongswan/src/libimcv/suites/test_imcv_seg.c, revision 1.1
1.1 ! misho 1: /*
! 2: * Copyright (C) 2014 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: #include "test_suite.h"
! 17:
! 18: #include <imcv.h>
! 19: #include <pa_tnc/pa_tnc_attr.h>
! 20: #include <seg/seg_env.h>
! 21: #include <seg/seg_contract.h>
! 22: #include <seg/seg_contract_manager.h>
! 23: #include <ietf/ietf_attr_pa_tnc_error.h>
! 24: #include <ita/ita_attr.h>
! 25: #include <ita/ita_attr_command.h>
! 26: #include <ita/ita_attr_dummy.h>
! 27: #include <tcg/seg/tcg_seg_attr_seg_env.h>
! 28:
! 29: #include <tncif_pa_subtypes.h>
! 30:
! 31: static struct {
! 32: uint32_t max_seg_size, next_segs, last_seg_size;
! 33: } seg_env_tests[] = {
! 34: { 0, 0, 0 },
! 35: { 11, 0, 0 },
! 36: { 12, 3, 12 },
! 37: { 13, 3, 9 },
! 38: { 15, 3, 3 },
! 39: { 16, 2, 16 },
! 40: { 17, 2, 14 },
! 41: { 23, 2, 2 },
! 42: { 24, 1, 24 },
! 43: { 25, 1, 23 },
! 44: { 47, 1, 1 },
! 45: { 48, 0, 0 },
! 46: };
! 47:
! 48: static char command[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
! 49: static uint32_t id = 0x123456;
! 50:
! 51: START_TEST(test_imcv_seg_env)
! 52: {
! 53: pa_tnc_attr_t *attr, *attr1, *base_attr, *base_attr1, *error;
! 54: tcg_seg_attr_seg_env_t *seg_env_attr;
! 55: ita_attr_command_t *ita_attr;
! 56: seg_env_t *seg_env, *seg_env1;
! 57: pen_type_t type;
! 58: uint32_t base_attr_id, max_seg_size, last_seg_size, seg_size, offset;
! 59: uint8_t flags;
! 60: bool last, last_seg;
! 61: chunk_t value, segment, seg;
! 62: int n;
! 63:
! 64: libimcv_init(FALSE);
! 65: max_seg_size = seg_env_tests[_i].max_seg_size;
! 66: last_seg_size = seg_env_tests[_i].last_seg_size;
! 67:
! 68: base_attr = ita_attr_command_create(command);
! 69: base_attr->build(base_attr);
! 70: seg_env = seg_env_create(id, base_attr, max_seg_size);
! 71:
! 72: if (seg_env_tests[_i].next_segs == 0)
! 73: {
! 74: ck_assert(seg_env == NULL);
! 75: }
! 76: else
! 77: {
! 78: ck_assert(seg_env->get_base_attr_id(seg_env) == id);
! 79: base_attr1 = seg_env->get_base_attr(seg_env);
! 80: ck_assert(base_attr == base_attr1);
! 81: base_attr1->destroy(base_attr1);
! 82:
! 83: for (n = 0; n <= seg_env_tests[_i].next_segs; n++)
! 84: {
! 85: last_seg = (n == seg_env_tests[_i].next_segs);
! 86: seg_size = (last_seg) ? last_seg_size : max_seg_size;
! 87: if (n == 0)
! 88: {
! 89: /* create first segment */
! 90: attr = seg_env->first_segment(seg_env, 0);
! 91:
! 92: seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
! 93: segment = seg_env_attr->get_segment(seg_env_attr, &flags);
! 94: if (max_seg_size > 12)
! 95: {
! 96: seg = chunk_create(command, seg_size - 12);
! 97: ck_assert(chunk_equals(seg, chunk_skip(segment, 12)));
! 98: }
! 99: ck_assert(flags == (SEG_ENV_FLAG_MORE | SEG_ENV_FLAG_START));
! 100: }
! 101: else
! 102: {
! 103: /* create next segments */
! 104: attr = seg_env->next_segment(seg_env, &last);
! 105: ck_assert(last == last_seg);
! 106:
! 107: seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
! 108: segment = seg_env_attr->get_segment(seg_env_attr, &flags);
! 109: seg = chunk_create(command + n * max_seg_size - 12, seg_size);
! 110: ck_assert(chunk_equals(seg, segment));
! 111: ck_assert(flags == (last_seg ? SEG_ENV_FLAG_NONE :
! 112: SEG_ENV_FLAG_MORE));
! 113: }
! 114:
! 115: /* check built segment envelope attribute */
! 116: value = attr->get_value(attr);
! 117: ck_assert(value.len == 4 + seg_size);
! 118: ck_assert(segment.len == seg_size);
! 119: ck_assert(seg_env_attr->get_base_attr_id(seg_env_attr) == id);
! 120:
! 121: /* create parse segment envelope attribute from data */
! 122: attr1 = tcg_seg_attr_seg_env_create_from_data(value.len, value);
! 123: ck_assert(attr1->process(attr1, &offset) == SUCCESS);
! 124: attr->destroy(attr);
! 125:
! 126: seg_env_attr = (tcg_seg_attr_seg_env_t*)attr1;
! 127: segment = seg_env_attr->get_segment(seg_env_attr, &flags);
! 128: base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
! 129: ck_assert(base_attr_id == id);
! 130:
! 131: /* create and update seg_env object on the receiving side */
! 132: if (n == 0)
! 133: {
! 134: ck_assert(flags == (SEG_ENV_FLAG_MORE | SEG_ENV_FLAG_START));
! 135: seg_env1 = seg_env_create_from_data(base_attr_id, segment,
! 136: max_seg_size, &error);
! 137: }
! 138: else
! 139: {
! 140: ck_assert(flags == (last_seg ? SEG_ENV_FLAG_NONE :
! 141: SEG_ENV_FLAG_MORE));
! 142: seg_env1->add_segment(seg_env1, segment, &error);
! 143: }
! 144: attr1->destroy(attr1);
! 145: }
! 146:
! 147: /* check reconstructed base attribute */
! 148: base_attr1 = seg_env1->get_base_attr(seg_env1);
! 149: ck_assert(base_attr1);
! 150: type = base_attr1->get_type(base_attr1);
! 151: ck_assert(type.vendor_id == PEN_ITA);
! 152: ck_assert(type.type == ITA_ATTR_COMMAND);
! 153: ita_attr = (ita_attr_command_t*)base_attr1;
! 154: ck_assert(streq(ita_attr->get_command(ita_attr), command));
! 155:
! 156: seg_env->destroy(seg_env);
! 157: seg_env1->destroy(seg_env1);
! 158: base_attr1->destroy(base_attr1);
! 159: }
! 160: libimcv_deinit();
! 161: }
! 162: END_TEST
! 163:
! 164: START_TEST(test_imcv_seg_env_special)
! 165: {
! 166: pa_tnc_attr_t *attr, *attr1, *base_attr;
! 167: tcg_seg_attr_seg_env_t *seg_env_attr;
! 168: pen_type_t type;
! 169: seg_env_t *seg_env;
! 170: chunk_t segment, value;
! 171: uint32_t max_attr_len = 60;
! 172: uint32_t max_seg_size = 47;
! 173: uint32_t last_seg_size = 4;
! 174: uint32_t offset = 12;
! 175:
! 176: base_attr = ita_attr_command_create(command);
! 177: base_attr->build(base_attr);
! 178:
! 179: /* set noskip flag in base attribute */
! 180: base_attr->set_noskip_flag(base_attr, TRUE);
! 181:
! 182: seg_env = seg_env_create(id, base_attr, max_seg_size);
! 183: attr = seg_env->first_segment(seg_env, max_attr_len);
! 184: attr->destroy(attr);
! 185:
! 186: /* don't return last segment indicator */
! 187: attr = seg_env->next_segment(seg_env, NULL);
! 188:
! 189: /* build attribute */
! 190: attr->build(attr);
! 191:
! 192: /* don't return flags */
! 193: seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
! 194: segment = seg_env_attr->get_segment(seg_env_attr, NULL);
! 195: ck_assert(segment.len == last_seg_size);
! 196:
! 197: /* get segment envelope attribute reference and destroy it */
! 198: attr1 = attr->get_ref(attr);
! 199: attr1->destroy(attr1);
! 200:
! 201: /* check some standard methods */
! 202: type = attr->get_type(attr);
! 203: ck_assert(type.vendor_id == PEN_TCG);
! 204: ck_assert(type.type == TCG_SEG_ATTR_SEG_ENV);
! 205: ck_assert(attr->get_noskip_flag(attr) == FALSE);
! 206: attr->set_noskip_flag(attr, TRUE);
! 207: ck_assert(attr->get_noskip_flag(attr) == TRUE);
! 208:
! 209: /* request next segment which does not exist */
! 210: ck_assert(seg_env->next_segment(seg_env, NULL) == NULL);
! 211:
! 212: /* create and parse a too short segment envelope attribute */
! 213: attr1 = tcg_seg_attr_seg_env_create_from_data(0, chunk_empty);
! 214: ck_assert(attr1->process(attr1, &offset) == FAILED);
! 215: ck_assert(offset == 0);
! 216: attr1->destroy(attr1);
! 217:
! 218: /* create and parse correct segment envelope attribute */
! 219: value = attr->get_value(attr);
! 220: attr1 = tcg_seg_attr_seg_env_create_from_data(value.len, value);
! 221: ck_assert(attr1->process(attr1, &offset) == SUCCESS);
! 222: type = attr1->get_type(attr1);
! 223: ck_assert(type.vendor_id == PEN_TCG);
! 224: ck_assert(type.type == TCG_SEG_ATTR_SEG_ENV);
! 225: attr1->destroy(attr1);
! 226:
! 227: /* cleanup */
! 228: attr->destroy(attr);
! 229: seg_env->destroy(seg_env);
! 230: }
! 231: END_TEST
! 232:
! 233: static struct {
! 234: pa_tnc_error_code_t error_code;
! 235: chunk_t segment;
! 236: } env_invalid_tests[] = {
! 237: { PA_ERROR_INVALID_PARAMETER, { NULL, 0 } },
! 238: { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
! 239: 0x00, 0xff, 0xff, 0xf0, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x0a)
! 240: },
! 241: { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
! 242: 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c)
! 243: },
! 244: { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
! 245: 0x00, 0x00, 0x90, 0x2a, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x0c)
! 246: },
! 247: { PA_ERROR_ATTR_TYPE_NOT_SUPPORTED, chunk_from_chars(
! 248: 0x80, 0x00, 0x90, 0x2a, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x0c)
! 249: },
! 250: { PA_ERROR_RESERVED, chunk_from_chars(
! 251: 0x00, 0x00, 0x90, 0x2a, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x0c)
! 252: },
! 253: { PA_ERROR_RESERVED, chunk_from_chars(
! 254: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0c)
! 255: },
! 256: { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
! 257: 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c)
! 258: }
! 259: };
! 260:
! 261: START_TEST(test_imcv_seg_env_invalid)
! 262: {
! 263: seg_env_t *seg_env;
! 264: pen_type_t error_code;
! 265: pa_tnc_attr_t*error;
! 266: ietf_attr_pa_tnc_error_t *error_attr;
! 267:
! 268: libimcv_init(FALSE);
! 269: seg_env = seg_env_create_from_data(id, env_invalid_tests[_i].segment, 20,
! 270: &error);
! 271: ck_assert(seg_env == NULL);
! 272: if (env_invalid_tests[_i].error_code == PA_ERROR_RESERVED)
! 273: {
! 274: ck_assert(error == NULL);
! 275: }
! 276: else
! 277: {
! 278: ck_assert(error);
! 279: error->build(error);
! 280: error_attr = (ietf_attr_pa_tnc_error_t*)error;
! 281: error_code = error_attr->get_error_code(error_attr);
! 282: ck_assert(error_code.vendor_id == PEN_IETF);
! 283: ck_assert(error_code.type == env_invalid_tests[_i].error_code);
! 284: error->destroy(error);
! 285: }
! 286: libimcv_deinit();
! 287: }
! 288: END_TEST
! 289:
! 290: START_TEST(test_imcv_seg_contract)
! 291: {
! 292: seg_contract_t *contract_i, *contract_r;
! 293: tcg_seg_attr_seg_env_t *seg_env_attr;
! 294: ita_attr_command_t *ita_attr;
! 295: pa_tnc_attr_t *attr, *base_attr_i, *base_attr_r, *error;
! 296: pen_type_t type, msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
! 297: uint32_t max_seg_size, max_attr_size = 1000, issuer_id = 1;
! 298: uint32_t base_attr_id;
! 299: bool more;
! 300:
! 301: libimcv_init(FALSE);
! 302: max_seg_size = seg_env_tests[_i].max_seg_size;
! 303: base_attr_r = ita_attr_command_create(command);
! 304: base_attr_r->build(base_attr_r);
! 305: contract_i = seg_contract_create(msg_type, max_attr_size, max_seg_size,
! 306: TRUE, issuer_id, FALSE);
! 307: contract_r = seg_contract_create(msg_type, max_attr_size, max_seg_size,
! 308: FALSE, issuer_id, TRUE);
! 309: attr = contract_r->first_segment(contract_r,
! 310: base_attr_r->get_ref(base_attr_r), 0);
! 311:
! 312: if (seg_env_tests[_i].next_segs == 0)
! 313: {
! 314: ck_assert(attr == NULL);
! 315: }
! 316: else
! 317: {
! 318: ck_assert(attr);
! 319: seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
! 320: base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
! 321: ck_assert(base_attr_id == 1);
! 322: base_attr_i = contract_i->add_segment(contract_i, attr, &error, &more);
! 323: ck_assert(base_attr_i == NULL);
! 324: attr->destroy(attr);
! 325: ck_assert(more);
! 326: while (more)
! 327: {
! 328: attr = contract_r->next_segment(contract_r, base_attr_id);
! 329: ck_assert(attr);
! 330: seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
! 331: base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
! 332: ck_assert(base_attr_id == 1);
! 333: base_attr_i = contract_i->add_segment(contract_i, attr, &error,
! 334: &more);
! 335: attr->destroy(attr);
! 336: }
! 337: ck_assert(base_attr_i);
! 338: ck_assert(error == NULL);
! 339: type = base_attr_i->get_type(base_attr_i);
! 340: ck_assert(pen_type_equals(type, base_attr_r->get_type(base_attr_r)));
! 341: ita_attr = (ita_attr_command_t*)base_attr_i;
! 342: ck_assert(streq(ita_attr->get_command(ita_attr), command));
! 343: base_attr_i->destroy(base_attr_i);
! 344: }
! 345: contract_i->destroy(contract_i);
! 346: contract_r->destroy(contract_r);
! 347: base_attr_r->destroy(base_attr_r);
! 348: libimcv_deinit();
! 349: }
! 350: END_TEST
! 351:
! 352: START_TEST(test_imcv_seg_contract_special)
! 353: {
! 354: seg_contract_t *contract_i, *contract_r;
! 355: tcg_seg_attr_seg_env_t *seg_env_attr1, *seg_env_attr2;
! 356: ita_attr_command_t *ita_attr;
! 357: pa_tnc_attr_t *base_attr1_i, *base_attr2_i, *base_attr1_r, *base_attr2_r;
! 358: pa_tnc_attr_t *attr1_f, *attr2_f, *attr1_n, *attr2_n, *attr3, *error;
! 359: pen_type_t type, msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
! 360: uint32_t max_seg_size, max_attr_size, issuer_id = 1;
! 361: uint32_t base_attr1_id, base_attr2_id;
! 362: char info[512];
! 363: bool oversize, more;
! 364:
! 365: libimcv_init(FALSE);
! 366:
! 367: /* create two base attributes to be segmented */
! 368: base_attr1_r = ita_attr_command_create(command);
! 369: base_attr2_r = ita_attr_dummy_create(129);
! 370: base_attr1_r->build(base_attr1_r);
! 371: base_attr2_r->build(base_attr2_r);
! 372:
! 373: /* create an issuer contract*/
! 374: contract_i = seg_contract_create(msg_type, 1000, 47,
! 375: TRUE, issuer_id, FALSE);
! 376: ck_assert(pen_type_equals(contract_i->get_msg_type(contract_i), msg_type));
! 377: ck_assert(contract_i->is_issuer(contract_i));
! 378: ck_assert(!contract_i->is_null(contract_i));
! 379:
! 380: /* set null contract */
! 381: contract_i->set_max_size(contract_i, SEG_CONTRACT_MAX_SIZE_VALUE,
! 382: SEG_CONTRACT_MAX_SIZE_VALUE);
! 383: ck_assert(contract_i->is_null(contract_i));
! 384:
! 385: /* set and get maximum attribute and segment sizes */
! 386: contract_i->set_max_size(contract_i, 1000, 47);
! 387: contract_i->get_max_size(contract_i, NULL, NULL);
! 388: contract_i->get_max_size(contract_i, &max_attr_size, &max_seg_size);
! 389: contract_i->get_info_string(contract_i, info, sizeof(info), TRUE);
! 390: ck_assert(max_attr_size == 1000 && max_seg_size == 47);
! 391: ck_assert(!contract_i->is_null(contract_i));
! 392:
! 393: /* create a null responder contract*/
! 394: contract_r = seg_contract_create(msg_type, SEG_CONTRACT_MAX_SIZE_VALUE,
! 395: SEG_CONTRACT_MAX_SIZE_VALUE,
! 396: FALSE, issuer_id, TRUE);
! 397: ck_assert(!contract_r->is_issuer(contract_r));
! 398: ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
! 399: ck_assert(!oversize);
! 400:
! 401: /* allow no fragmentation */
! 402: contract_r->set_max_size(contract_r, 1000, SEG_CONTRACT_MAX_SIZE_VALUE);
! 403: ck_assert(!contract_r->is_null(contract_r));
! 404: ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
! 405: ck_assert(!oversize);
! 406:
! 407: /* no maximum size limit and no fragmentation needed */
! 408: contract_r->set_max_size(contract_r, SEG_CONTRACT_MAX_SIZE_VALUE, 141);
! 409: ck_assert(!contract_r->is_null(contract_r));
! 410: ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
! 411: ck_assert(!oversize);
! 412:
! 413: /* oversize base attribute */
! 414: contract_r->set_max_size(contract_r, 140, 47);
! 415: ck_assert(!contract_r->is_null(contract_r));
! 416: ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
! 417: ck_assert(oversize);
! 418:
! 419: /* set final maximum attribute and segment sizes */
! 420: contract_r->set_max_size(contract_r, 141, 47);
! 421: contract_r->get_info_string(contract_r, info, sizeof(info), TRUE);
! 422: ck_assert(contract_r->check_size(contract_r, base_attr2_r, &oversize));
! 423: ck_assert(!oversize);
! 424:
! 425: /* get first segment of each base attribute */
! 426: attr1_f = contract_r->first_segment(contract_r, base_attr1_r->get_ref(base_attr1_r), 0);
! 427: attr2_f = contract_r->first_segment(contract_r, base_attr2_r->get_ref(base_attr2_r), 0);
! 428: ck_assert(attr1_f);
! 429: ck_assert(attr2_f);
! 430: seg_env_attr1 = (tcg_seg_attr_seg_env_t*)attr1_f;
! 431: seg_env_attr2 = (tcg_seg_attr_seg_env_t*)attr2_f;
! 432: base_attr1_id = seg_env_attr1->get_base_attr_id(seg_env_attr1);
! 433: base_attr2_id = seg_env_attr2->get_base_attr_id(seg_env_attr2);
! 434: ck_assert(base_attr1_id == 1);
! 435: ck_assert(base_attr2_id == 2);
! 436:
! 437: /* get second segment of each base attribute */
! 438: attr1_n = contract_r->next_segment(contract_r, 1);
! 439: attr2_n = contract_r->next_segment(contract_r, 2);
! 440: ck_assert(attr1_n);
! 441: ck_assert(attr2_n);
! 442:
! 443: /* process first segment of first base attribute */
! 444: base_attr1_i = contract_i->add_segment(contract_i, attr1_f, &error, &more);
! 445: ck_assert(base_attr1_i == NULL);
! 446: ck_assert(error == NULL);
! 447: ck_assert(more);
! 448:
! 449: /* reapply first segment of first base attribute */
! 450: base_attr1_i = contract_i->add_segment(contract_i, attr1_f, &error, &more);
! 451: ck_assert(base_attr1_i == NULL);
! 452: ck_assert(error == NULL);
! 453: ck_assert(more);
! 454:
! 455: /* process stray second segment of second attribute */
! 456: base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
! 457: ck_assert(base_attr2_i == NULL);
! 458: ck_assert(error == NULL);
! 459: ck_assert(more);
! 460:
! 461: /* process first segment of second base attribute */
! 462: base_attr2_i = contract_i->add_segment(contract_i, attr2_f, &error, &more);
! 463: ck_assert(base_attr2_i == NULL);
! 464: ck_assert(error == NULL);
! 465: ck_assert(more);
! 466:
! 467: /* try to get a segment of a non-existing base-attribute */
! 468: attr3 = contract_r->next_segment(contract_r, 3);
! 469: ck_assert(attr3 == NULL);
! 470:
! 471: /* process second segment of first base attribute */
! 472: base_attr1_i = contract_i->add_segment(contract_i, attr1_n, &error, &more);
! 473: ck_assert(base_attr1_i);
! 474: ck_assert(error == NULL);
! 475: ck_assert(!more);
! 476:
! 477: /* process second segment of second base attribute */
! 478: base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
! 479: ck_assert(base_attr2_i == NULL);
! 480: ck_assert(error == NULL);
! 481: ck_assert(more);
! 482:
! 483: /* destroy first and second segments */
! 484: attr1_f->destroy(attr1_f);
! 485: attr2_f->destroy(attr2_f);
! 486: attr1_n->destroy(attr1_n);
! 487: attr2_n->destroy(attr2_n);
! 488:
! 489: /* request surplus segment of first base attribute */
! 490: attr1_n = contract_r->next_segment(contract_r, 1);
! 491: ck_assert(attr1_n == NULL);
! 492:
! 493: /* get last segment of second base attribute */
! 494: attr2_n = contract_r->next_segment(contract_r, 2);
! 495: ck_assert(attr2_n);
! 496:
! 497: /* process last segment of second base attribute */
! 498: base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
! 499: attr2_n->destroy(attr2_n);
! 500: ck_assert(base_attr2_i);
! 501: ck_assert(error == NULL);
! 502: ck_assert(!more);
! 503:
! 504: /* request surplus segment of second base attribute */
! 505: attr2_n = contract_r->next_segment(contract_r, 2);
! 506: ck_assert(attr2_n == NULL);
! 507:
! 508: /* compare original with reconstructed base attributes */
! 509: type = base_attr1_i->get_type(base_attr1_i);
! 510: ck_assert(pen_type_equals(type, base_attr1_r->get_type(base_attr1_r)));
! 511: ita_attr = (ita_attr_command_t*)base_attr1_i;
! 512: ck_assert(streq(ita_attr->get_command(ita_attr), command));
! 513:
! 514: type = base_attr2_i->get_type(base_attr2_i);
! 515: ck_assert(pen_type_equals(type, base_attr2_r->get_type(base_attr2_r)));
! 516: ck_assert(chunk_equals(base_attr2_i->get_value(base_attr2_i),
! 517: base_attr2_r->get_value(base_attr2_r)));
! 518:
! 519: /* cleanup */
! 520: base_attr1_r->destroy(base_attr1_r);
! 521: base_attr2_r->destroy(base_attr2_r);
! 522: base_attr1_i->destroy(base_attr1_i);
! 523: base_attr2_i->destroy(base_attr2_i);
! 524: contract_i->destroy(contract_i);
! 525: contract_r->destroy(contract_r);
! 526: libimcv_deinit();
! 527: }
! 528: END_TEST
! 529:
! 530: static struct {
! 531: bool err_f;
! 532: chunk_t frag_f;
! 533: bool err_n;
! 534: bool base_attr;
! 535: chunk_t frag_n;
! 536: } contract_invalid_tests[] = {
! 537: { FALSE, chunk_from_chars(
! 538: 0xc0, 0x00, 0x00, 0x01, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x01,
! 539: 0x00, 0x00, 0x00, 0x0d),
! 540: FALSE, TRUE, chunk_from_chars(
! 541: 0x00, 0x00, 0x00, 0x01, 0x01 )
! 542: },
! 543: { FALSE, chunk_from_chars(
! 544: 0xc0, 0x00, 0x00, 0x02, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x01,
! 545: 0x00, 0x00, 0x00, 0x0e),
! 546: TRUE, FALSE, chunk_from_chars(
! 547: 0x00, 0x00, 0x00, 0x02, 0x01 )
! 548: },
! 549: { TRUE, chunk_from_chars(
! 550: 0xc0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x55, 0x97, 0x00, 0x00, 0x00, 0x23,
! 551: 0x00, 0x00, 0x00, 0x0d),
! 552: FALSE, FALSE, chunk_from_chars(
! 553: 0x00, 0x00, 0x00, 0x03, 0x01 )
! 554: },
! 555: { FALSE, chunk_from_chars(
! 556: 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
! 557: 0x00, 0x00, 0x00, 0x14),
! 558: FALSE, FALSE, chunk_from_chars(
! 559: 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 )
! 560: },
! 561: { FALSE, chunk_from_chars(
! 562: 0xc0, 0x00, 0x00, 0x05, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x03,
! 563: 0x00, 0x00, 0x00, 0x0f),
! 564: TRUE, FALSE, chunk_from_chars(
! 565: 0x00, 0x00, 0x00, 0x05, 0x00, 0x02, 0x01 )
! 566: },
! 567: { FALSE, chunk_from_chars(
! 568: 0xc0, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
! 569: 0x00, 0x00, 0x00, 0x11),
! 570: TRUE, FALSE, chunk_from_chars(
! 571: 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0xff )
! 572: }
! 573: };
! 574:
! 575: START_TEST(test_imcv_seg_contract_invalid)
! 576: {
! 577: uint32_t max_seg_size = 12, max_attr_size = 100, issuer_id = 1;
! 578: pen_type_t msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
! 579: pa_tnc_attr_t *attr_f, *attr_n, *base_attr, *error;
! 580: chunk_t value_f, value_n;
! 581: seg_contract_t *contract;
! 582: uint32_t offset;
! 583: bool more;
! 584:
! 585: libimcv_init(FALSE);
! 586: value_f = contract_invalid_tests[_i].frag_f;
! 587: value_n = contract_invalid_tests[_i].frag_n;
! 588: attr_f = tcg_seg_attr_seg_env_create_from_data(value_f.len, value_f);
! 589: attr_n = tcg_seg_attr_seg_env_create_from_data(value_n.len, value_n);
! 590: ck_assert(attr_f->process(attr_f, &offset) == SUCCESS);
! 591: ck_assert(attr_n->process(attr_n, &offset) == SUCCESS);
! 592:
! 593: contract = seg_contract_create(msg_type, max_attr_size, max_seg_size,
! 594: TRUE, issuer_id, FALSE);
! 595: base_attr = contract->add_segment(contract, attr_f, &error, &more);
! 596: ck_assert(base_attr == NULL);
! 597:
! 598: if (contract_invalid_tests[_i].err_f)
! 599: {
! 600: ck_assert(error);
! 601: error->destroy(error);
! 602: }
! 603: else
! 604: {
! 605: ck_assert(error == NULL);
! 606: ck_assert(more);
! 607: base_attr = contract->add_segment(contract, attr_n, &error, &more);
! 608: if (contract_invalid_tests[_i].err_n)
! 609: {
! 610: ck_assert(error);
! 611: error->destroy(error);
! 612: }
! 613: else
! 614: {
! 615: ck_assert(error == NULL);
! 616: }
! 617: if (contract_invalid_tests[_i].base_attr)
! 618: {
! 619: ck_assert(base_attr);
! 620: base_attr->destroy(base_attr);
! 621: }
! 622: }
! 623:
! 624: /* cleanup */
! 625: attr_f->destroy(attr_f);
! 626: attr_n->destroy(attr_n);
! 627: contract->destroy(contract);
! 628: libimcv_deinit();
! 629: }
! 630: END_TEST
! 631:
! 632: START_TEST(test_imcv_seg_contract_mgr)
! 633: {
! 634: char buf[BUF_LEN];
! 635: uint32_t max_seg_size = 12, max_attr_size = 100;
! 636: pen_type_t msg_type1 = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
! 637: pen_type_t msg_type2 = { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM };
! 638: seg_contract_manager_t *contracts;
! 639: seg_contract_t *cx, *c1, *c2, *c3, *c4;
! 640:
! 641: contracts = seg_contract_manager_create();
! 642:
! 643: /* add contract template as issuer */
! 644: c1 = seg_contract_create(msg_type1, max_attr_size, max_seg_size,
! 645: TRUE, 1, FALSE);
! 646: c1->get_info_string(c1, buf, BUF_LEN, TRUE);
! 647:
! 648: contracts->add_contract(contracts, c1);
! 649:
! 650: /* received contract request for msg_type1 as responder */
! 651: cx = contracts->get_contract(contracts, msg_type1, FALSE, 2);
! 652: ck_assert(cx == NULL);
! 653:
! 654: /* add directed contract as responder */
! 655: c2 = seg_contract_create(msg_type1, max_attr_size, max_seg_size,
! 656: FALSE, 2, FALSE);
! 657: c2->set_responder(c2, 1);
! 658: c2->get_info_string(c2, buf, BUF_LEN, TRUE);
! 659: contracts->add_contract(contracts, c2);
! 660:
! 661: /* retrieve this contract */
! 662: cx = contracts->get_contract(contracts, msg_type1, FALSE, 2);
! 663: ck_assert(cx == c2);
! 664:
! 665: /* received directed contract response as issuer */
! 666: cx = contracts->get_contract(contracts, msg_type1, TRUE, 3);
! 667: ck_assert(cx == NULL);
! 668:
! 669: /* get contract template */
! 670: cx = contracts->get_contract(contracts, msg_type1, TRUE, TNC_IMCID_ANY);
! 671: ck_assert(cx == c1);
! 672:
! 673: /* clone the contract template and as it as a directed contract */
! 674: c3 = cx->clone(cx);
! 675: c3->set_responder(c3, 3);
! 676: c3->get_info_string(c3, buf, BUF_LEN, FALSE);
! 677: contracts->add_contract(contracts, c3);
! 678:
! 679: /* retrieve this contract */
! 680: cx = contracts->get_contract(contracts, msg_type1, TRUE, 3);
! 681: ck_assert(cx == c3);
! 682:
! 683: /* received contract request for msg_type2 as responder */
! 684: cx = contracts->get_contract(contracts, msg_type2, FALSE, 2);
! 685: ck_assert(cx == NULL);
! 686:
! 687: /* add directed contract as responder */
! 688: c4 = seg_contract_create(msg_type2, max_attr_size, max_seg_size,
! 689: FALSE, 2, FALSE);
! 690: c4->set_responder(c4, 1);
! 691: contracts->add_contract(contracts, c4);
! 692:
! 693: /* retrieve this contract */
! 694: cx = contracts->get_contract(contracts, msg_type2, FALSE, 2);
! 695: ck_assert(cx == c4);
! 696:
! 697: contracts->destroy(contracts);
! 698: }
! 699: END_TEST
! 700:
! 701: Suite *imcv_seg_suite_create()
! 702: {
! 703: Suite *s;
! 704: TCase *tc;
! 705:
! 706: s = suite_create("imcv_seg");
! 707:
! 708: tc = tcase_create("env");
! 709: tcase_add_loop_test(tc, test_imcv_seg_env, 0, countof(seg_env_tests));
! 710: suite_add_tcase(s, tc);
! 711:
! 712: tc = tcase_create("env_special");
! 713: tcase_add_test(tc, test_imcv_seg_env_special);
! 714: suite_add_tcase(s, tc);
! 715:
! 716: tc = tcase_create("env_invalid");
! 717: tcase_add_loop_test(tc, test_imcv_seg_env_invalid, 0,
! 718: countof(env_invalid_tests));
! 719: suite_add_tcase(s, tc);
! 720:
! 721: tc = tcase_create("contract");
! 722: tcase_add_loop_test(tc, test_imcv_seg_contract, 0, countof(seg_env_tests));
! 723: suite_add_tcase(s, tc);
! 724:
! 725: tc = tcase_create("contract_special");
! 726: tcase_add_test(tc, test_imcv_seg_contract_special);
! 727: suite_add_tcase(s, tc);
! 728:
! 729: tc = tcase_create("contract_invalid");
! 730: tcase_add_loop_test(tc, test_imcv_seg_contract_invalid, 0,
! 731: countof(contract_invalid_tests));
! 732: suite_add_tcase(s, tc);
! 733:
! 734: tc = tcase_create("contract_mgr");
! 735: tcase_add_test(tc, test_imcv_seg_contract_mgr);
! 736: suite_add_tcase(s, tc);
! 737:
! 738: return s;
! 739: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>