unit-tests: Base attributes get adopted by seg-env/seg-contract
[strongswan.git] / src / libimcv / suites / test_imcv_seg.c
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);
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_seg_size = 47;
172 uint32_t last_seg_size = 1;
173 uint32_t offset = 12;
174
175 base_attr = ita_attr_command_create(command);
176 base_attr->build(base_attr);
177
178 /* set noskip flag in base attribute */
179 base_attr->set_noskip_flag(base_attr, TRUE);
180
181 seg_env = seg_env_create(id, base_attr, max_seg_size);
182 attr = seg_env->first_segment(seg_env);
183 attr->destroy(attr);
184
185 /* don't return last segment indicator */
186 attr = seg_env->next_segment(seg_env, NULL);
187
188 /* build attribute */
189 attr->build(attr);
190
191 /* don't return flags */
192 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
193 segment = seg_env_attr->get_segment(seg_env_attr, NULL);
194 ck_assert(segment.len == last_seg_size);
195
196 /* get segment envelope attribute reference and destroy it */
197 attr1 = attr->get_ref(attr);
198 attr1->destroy(attr1);
199
200 /* check some standard methods */
201 type = attr->get_type(attr);
202 ck_assert(type.vendor_id == PEN_TCG);
203 ck_assert(type.type == TCG_SEG_ATTR_SEG_ENV);
204 ck_assert(attr->get_noskip_flag(attr) == FALSE);
205 attr->set_noskip_flag(attr, TRUE);
206 ck_assert(attr->get_noskip_flag(attr) == TRUE);
207
208 /* request next segment which does not exist */
209 ck_assert(seg_env->next_segment(seg_env, NULL) == NULL);
210
211 /* create and parse a too short segment envelope attribute */
212 attr1 = tcg_seg_attr_seg_env_create_from_data(0, chunk_empty);
213 ck_assert(attr1->process(attr1, &offset) == FAILED);
214 ck_assert(offset == 0);
215 attr1->destroy(attr1);
216
217 /* create and parse correct segment envelope attribute */
218 value = attr->get_value(attr);
219 attr1 = tcg_seg_attr_seg_env_create_from_data(value.len, value);
220 ck_assert(attr1->process(attr1, &offset) == SUCCESS);
221 type = attr1->get_type(attr1);
222 ck_assert(type.vendor_id == PEN_TCG);
223 ck_assert(type.type == TCG_SEG_ATTR_SEG_ENV);
224 attr1->destroy(attr1);
225
226 /* cleanup */
227 attr->destroy(attr);
228 seg_env->destroy(seg_env);
229 }
230 END_TEST
231
232 static struct {
233 pa_tnc_error_code_t error_code;
234 chunk_t segment;
235 } env_invalid_tests[] = {
236 { PA_ERROR_INVALID_PARAMETER, { NULL, 0 } },
237 { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
238 0x00, 0xff, 0xff, 0xf0, 0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x0a)
239 },
240 { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
241 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0c)
242 },
243 { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
244 0x00, 0x00, 0x90, 0x2a, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x0c)
245 },
246 { PA_ERROR_ATTR_TYPE_NOT_SUPPORTED, chunk_from_chars(
247 0x80, 0x00, 0x90, 0x2a, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x0c)
248 },
249 { PA_ERROR_RESERVED, chunk_from_chars(
250 0x00, 0x00, 0x90, 0x2a, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x0c)
251 },
252 { PA_ERROR_RESERVED, chunk_from_chars(
253 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x0c)
254 },
255 { PA_ERROR_INVALID_PARAMETER, chunk_from_chars(
256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x0c)
257 }
258 };
259
260 START_TEST(test_imcv_seg_env_invalid)
261 {
262 seg_env_t *seg_env;
263 pen_type_t error_code;
264 pa_tnc_attr_t*error;
265 ietf_attr_pa_tnc_error_t *error_attr;
266
267 libimcv_init(FALSE);
268 seg_env = seg_env_create_from_data(id, env_invalid_tests[_i].segment, 20,
269 &error);
270 ck_assert(seg_env == NULL);
271 if (env_invalid_tests[_i].error_code == PA_ERROR_RESERVED)
272 {
273 ck_assert(error == NULL);
274 }
275 else
276 {
277 ck_assert(error);
278 error->build(error);
279 error_attr = (ietf_attr_pa_tnc_error_t*)error;
280 error_code = error_attr->get_error_code(error_attr);
281 ck_assert(error_code.vendor_id == PEN_IETF);
282 ck_assert(error_code.type == env_invalid_tests[_i].error_code);
283 error->destroy(error);
284 }
285 libimcv_deinit();
286 }
287 END_TEST
288
289 START_TEST(test_imcv_seg_contract)
290 {
291 seg_contract_t *contract_i, *contract_r;
292 tcg_seg_attr_seg_env_t *seg_env_attr;
293 ita_attr_command_t *ita_attr;
294 pa_tnc_attr_t *attr, *base_attr_i, *base_attr_r, *error;
295 pen_type_t type, msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
296 uint32_t max_seg_size, max_attr_size = 1000, issuer_id = 1;
297 uint32_t base_attr_id;
298 bool more;
299
300 libimcv_init(FALSE);
301 max_seg_size = seg_env_tests[_i].max_seg_size;
302 base_attr_r = ita_attr_command_create(command);
303 base_attr_r->build(base_attr_r);
304 contract_i = seg_contract_create(msg_type, max_attr_size, max_seg_size,
305 TRUE, issuer_id, FALSE);
306 contract_r = seg_contract_create(msg_type, max_attr_size, max_seg_size,
307 FALSE, issuer_id, TRUE);
308 attr = contract_r->first_segment(contract_r,
309 base_attr_r->get_ref(base_attr_r));
310
311 if (seg_env_tests[_i].next_segs == 0)
312 {
313 ck_assert(attr == NULL);
314 }
315 else
316 {
317 ck_assert(attr);
318 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
319 base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
320 ck_assert(base_attr_id == 1);
321 base_attr_i = contract_i->add_segment(contract_i, attr, &error, &more);
322 ck_assert(base_attr_i == NULL);
323 attr->destroy(attr);
324 ck_assert(more);
325 while (more)
326 {
327 attr = contract_r->next_segment(contract_r, base_attr_id);
328 ck_assert(attr);
329 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
330 base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
331 ck_assert(base_attr_id == 1);
332 base_attr_i = contract_i->add_segment(contract_i, attr, &error,
333 &more);
334 attr->destroy(attr);
335 }
336 ck_assert(base_attr_i);
337 ck_assert(error == NULL);
338 type = base_attr_i->get_type(base_attr_i);
339 ck_assert(pen_type_equals(type, base_attr_r->get_type(base_attr_r)));
340 ita_attr = (ita_attr_command_t*)base_attr_i;
341 ck_assert(streq(ita_attr->get_command(ita_attr), command));
342 base_attr_i->destroy(base_attr_i);
343 }
344 contract_i->destroy(contract_i);
345 contract_r->destroy(contract_r);
346 base_attr_r->destroy(base_attr_r);
347 libimcv_deinit();
348 }
349 END_TEST
350
351 START_TEST(test_imcv_seg_contract_special)
352 {
353 seg_contract_t *contract_i, *contract_r;
354 tcg_seg_attr_seg_env_t *seg_env_attr1, *seg_env_attr2;
355 ita_attr_command_t *ita_attr;
356 pa_tnc_attr_t *base_attr1_i, *base_attr2_i, *base_attr1_r, *base_attr2_r;
357 pa_tnc_attr_t *attr1_f, *attr2_f, *attr1_n, *attr2_n, *attr3, *error;
358 pen_type_t type, msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
359 uint32_t max_seg_size, max_attr_size, issuer_id = 1;
360 uint32_t base_attr1_id, base_attr2_id;
361 char info[512];
362 bool oversize, more;
363
364 libimcv_init(FALSE);
365
366 /* create two base attributes to be segmented */
367 base_attr1_r = ita_attr_command_create(command);
368 base_attr2_r = ita_attr_dummy_create(129);
369 base_attr1_r->build(base_attr1_r);
370 base_attr2_r->build(base_attr2_r);
371
372 /* create an issuer contract*/
373 contract_i = seg_contract_create(msg_type, 1000, 47,
374 TRUE, issuer_id, FALSE);
375 ck_assert(pen_type_equals(contract_i->get_msg_type(contract_i), msg_type));
376 ck_assert(contract_i->is_issuer(contract_i));
377 ck_assert(!contract_i->is_null(contract_i));
378
379 /* set null contract */
380 contract_i->set_max_size(contract_i, SEG_CONTRACT_MAX_SIZE_VALUE,
381 SEG_CONTRACT_MAX_SIZE_VALUE);
382 ck_assert(contract_i->is_null(contract_i));
383
384 /* set and get maximum attribute and segment sizes */
385 contract_i->set_max_size(contract_i, 1000, 47);
386 contract_i->get_max_size(contract_i, NULL, NULL);
387 contract_i->get_max_size(contract_i, &max_attr_size, &max_seg_size);
388 contract_i->get_info_string(contract_i, info, sizeof(info), TRUE);
389 ck_assert(max_attr_size == 1000 && max_seg_size == 47);
390 ck_assert(!contract_i->is_null(contract_i));
391
392 /* create a null responder contract*/
393 contract_r = seg_contract_create(msg_type, SEG_CONTRACT_MAX_SIZE_VALUE,
394 SEG_CONTRACT_MAX_SIZE_VALUE,
395 FALSE, issuer_id, TRUE);
396 ck_assert(!contract_r->is_issuer(contract_r));
397 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
398 ck_assert(!oversize);
399
400 /* allow no fragmentation */
401 contract_r->set_max_size(contract_r, 1000, SEG_CONTRACT_MAX_SIZE_VALUE);
402 ck_assert(!contract_r->is_null(contract_r));
403 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
404 ck_assert(!oversize);
405
406 /* no maximum size limit and no fragmentation needed */
407 contract_r->set_max_size(contract_r, SEG_CONTRACT_MAX_SIZE_VALUE, 141);
408 ck_assert(!contract_r->is_null(contract_r));
409 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
410 ck_assert(!oversize);
411
412 /* oversize base attribute */
413 contract_r->set_max_size(contract_r, 140, 47);
414 ck_assert(!contract_r->is_null(contract_r));
415 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
416 ck_assert(oversize);
417
418 /* set final maximum attribute and segment sizes */
419 contract_r->set_max_size(contract_r, 141, 47);
420 contract_r->get_info_string(contract_r, info, sizeof(info), TRUE);
421 ck_assert(contract_r->check_size(contract_r, base_attr2_r, &oversize));
422 ck_assert(!oversize);
423
424 /* get first segment of each base attribute */
425 attr1_f = contract_r->first_segment(contract_r, base_attr1_r->get_ref(base_attr1_r));
426 attr2_f = contract_r->first_segment(contract_r, base_attr2_r->get_ref(base_attr2_r));
427 ck_assert(attr1_f);
428 ck_assert(attr2_f);
429 seg_env_attr1 = (tcg_seg_attr_seg_env_t*)attr1_f;
430 seg_env_attr2 = (tcg_seg_attr_seg_env_t*)attr2_f;
431 base_attr1_id = seg_env_attr1->get_base_attr_id(seg_env_attr1);
432 base_attr2_id = seg_env_attr2->get_base_attr_id(seg_env_attr2);
433 ck_assert(base_attr1_id == 1);
434 ck_assert(base_attr2_id == 2);
435
436 /* get second segment of each base attribute */
437 attr1_n = contract_r->next_segment(contract_r, 1);
438 attr2_n = contract_r->next_segment(contract_r, 2);
439 ck_assert(attr1_n);
440 ck_assert(attr2_n);
441
442 /* process first segment of first base attribute */
443 base_attr1_i = contract_i->add_segment(contract_i, attr1_f, &error, &more);
444 ck_assert(base_attr1_i == NULL);
445 ck_assert(error == NULL);
446 ck_assert(more);
447
448 /* reapply first segment of first base attribute */
449 base_attr1_i = contract_i->add_segment(contract_i, attr1_f, &error, &more);
450 ck_assert(base_attr1_i == NULL);
451 ck_assert(error == NULL);
452 ck_assert(more);
453
454 /* process stray second segment of second attribute */
455 base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
456 ck_assert(base_attr2_i == NULL);
457 ck_assert(error == NULL);
458 ck_assert(more);
459
460 /* process first segment of second base attribute */
461 base_attr2_i = contract_i->add_segment(contract_i, attr2_f, &error, &more);
462 ck_assert(base_attr2_i == NULL);
463 ck_assert(error == NULL);
464 ck_assert(more);
465
466 /* try to get a segment of a non-existing base-attribute */
467 attr3 = contract_r->next_segment(contract_r, 3);
468 ck_assert(attr3 == NULL);
469
470 /* process second segment of first base attribute */
471 base_attr1_i = contract_i->add_segment(contract_i, attr1_n, &error, &more);
472 ck_assert(base_attr1_i);
473 ck_assert(error == NULL);
474 ck_assert(!more);
475
476 /* process second segment of second base attribute */
477 base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
478 ck_assert(base_attr2_i == NULL);
479 ck_assert(error == NULL);
480 ck_assert(more);
481
482 /* destroy first and second segments */
483 attr1_f->destroy(attr1_f);
484 attr2_f->destroy(attr2_f);
485 attr1_n->destroy(attr1_n);
486 attr2_n->destroy(attr2_n);
487
488 /* request surplus segment of first base attribute */
489 attr1_n = contract_r->next_segment(contract_r, 1);
490 ck_assert(attr1_n == NULL);
491
492 /* get last segment of second base attribute */
493 attr2_n = contract_r->next_segment(contract_r, 2);
494 ck_assert(attr2_n);
495
496 /* process last segment of second base attribute */
497 base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
498 attr2_n->destroy(attr2_n);
499 ck_assert(base_attr2_i);
500 ck_assert(error == NULL);
501 ck_assert(!more);
502
503 /* request surplus segment of second base attribute */
504 attr2_n = contract_r->next_segment(contract_r, 2);
505 ck_assert(attr2_n == NULL);
506
507 /* compare original with reconstructed base attributes */
508 type = base_attr1_i->get_type(base_attr1_i);
509 ck_assert(pen_type_equals(type, base_attr1_r->get_type(base_attr1_r)));
510 ita_attr = (ita_attr_command_t*)base_attr1_i;
511 ck_assert(streq(ita_attr->get_command(ita_attr), command));
512
513 type = base_attr2_i->get_type(base_attr2_i);
514 ck_assert(pen_type_equals(type, base_attr2_r->get_type(base_attr2_r)));
515 ck_assert(chunk_equals(base_attr2_i->get_value(base_attr2_i),
516 base_attr2_r->get_value(base_attr2_r)));
517
518 /* cleanup */
519 base_attr1_r->destroy(base_attr1_r);
520 base_attr2_r->destroy(base_attr2_r);
521 base_attr1_i->destroy(base_attr1_i);
522 base_attr2_i->destroy(base_attr2_i);
523 contract_i->destroy(contract_i);
524 contract_r->destroy(contract_r);
525 libimcv_deinit();
526 }
527 END_TEST
528
529 static struct {
530 bool err_f;
531 chunk_t frag_f;
532 bool err_n;
533 bool base_attr;
534 chunk_t frag_n;
535 } contract_invalid_tests[] = {
536 { FALSE, chunk_from_chars(
537 0xc0, 0x00, 0x00, 0x01, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x01,
538 0x00, 0x00, 0x00, 0x0d),
539 FALSE, TRUE, chunk_from_chars(
540 0x00, 0x00, 0x00, 0x01, 0x01 )
541 },
542 { FALSE, chunk_from_chars(
543 0xc0, 0x00, 0x00, 0x02, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x01,
544 0x00, 0x00, 0x00, 0x0e),
545 TRUE, FALSE, chunk_from_chars(
546 0x00, 0x00, 0x00, 0x02, 0x01 )
547 },
548 { TRUE, chunk_from_chars(
549 0xc0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x55, 0x97, 0x00, 0x00, 0x00, 0x23,
550 0x00, 0x00, 0x00, 0x0d),
551 FALSE, FALSE, chunk_from_chars(
552 0x00, 0x00, 0x00, 0x03, 0x01 )
553 },
554 { FALSE, chunk_from_chars(
555 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
556 0x00, 0x00, 0x00, 0x14),
557 FALSE, FALSE, chunk_from_chars(
558 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 )
559 },
560 { FALSE, chunk_from_chars(
561 0xc0, 0x00, 0x00, 0x05, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x03,
562 0x00, 0x00, 0x00, 0x0f),
563 TRUE, FALSE, chunk_from_chars(
564 0x00, 0x00, 0x00, 0x05, 0x00, 0x02, 0x01 )
565 },
566 { FALSE, chunk_from_chars(
567 0xc0, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
568 0x00, 0x00, 0x00, 0x11),
569 TRUE, FALSE, chunk_from_chars(
570 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0xff )
571 }
572 };
573
574 START_TEST(test_imcv_seg_contract_invalid)
575 {
576 uint32_t max_seg_size = 12, max_attr_size = 100, issuer_id = 1;
577 pen_type_t msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
578 pa_tnc_attr_t *attr_f, *attr_n, *base_attr, *error;
579 chunk_t value_f, value_n;
580 seg_contract_t *contract;
581 uint32_t offset;
582 bool more;
583
584 libimcv_init(FALSE);
585 value_f = contract_invalid_tests[_i].frag_f;
586 value_n = contract_invalid_tests[_i].frag_n;
587 attr_f = tcg_seg_attr_seg_env_create_from_data(value_f.len, value_f);
588 attr_n = tcg_seg_attr_seg_env_create_from_data(value_n.len, value_n);
589 ck_assert(attr_f->process(attr_f, &offset) == SUCCESS);
590 ck_assert(attr_n->process(attr_n, &offset) == SUCCESS);
591
592 contract = seg_contract_create(msg_type, max_attr_size, max_seg_size,
593 TRUE, issuer_id, FALSE);
594 base_attr = contract->add_segment(contract, attr_f, &error, &more);
595 ck_assert(base_attr == NULL);
596
597 if (contract_invalid_tests[_i].err_f)
598 {
599 ck_assert(error);
600 error->destroy(error);
601 }
602 else
603 {
604 ck_assert(error == NULL);
605 ck_assert(more);
606 base_attr = contract->add_segment(contract, attr_n, &error, &more);
607 if (contract_invalid_tests[_i].err_n)
608 {
609 ck_assert(error);
610 error->destroy(error);
611 }
612 else
613 {
614 ck_assert(error == NULL);
615 }
616 if (contract_invalid_tests[_i].base_attr)
617 {
618 ck_assert(base_attr);
619 base_attr->destroy(base_attr);
620 }
621 }
622
623 /* cleanup */
624 attr_f->destroy(attr_f);
625 attr_n->destroy(attr_n);
626 contract->destroy(contract);
627 libimcv_deinit();
628 }
629 END_TEST
630
631 START_TEST(test_imcv_seg_contract_mgr)
632 {
633 char buf[BUF_LEN];
634 uint32_t max_seg_size = 12, max_attr_size = 100;
635 pen_type_t msg_type1 = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
636 pen_type_t msg_type2 = { PEN_IETF, PA_SUBTYPE_IETF_OPERATING_SYSTEM };
637 seg_contract_manager_t *contracts;
638 seg_contract_t *cx, *c1, *c2, *c3, *c4;
639
640 contracts = seg_contract_manager_create();
641
642 /* add contract template as issuer */
643 c1 = seg_contract_create(msg_type1, max_attr_size, max_seg_size,
644 TRUE, 1, FALSE);
645 c1->get_info_string(c1, buf, BUF_LEN, TRUE);
646
647 contracts->add_contract(contracts, c1);
648
649 /* received contract request for msg_type1 as responder */
650 cx = contracts->get_contract(contracts, msg_type1, FALSE, 2);
651 ck_assert(cx == NULL);
652
653 /* add directed contract as responder */
654 c2 = seg_contract_create(msg_type1, max_attr_size, max_seg_size,
655 FALSE, 2, FALSE);
656 c2->set_responder(c2, 1);
657 c2->get_info_string(c2, buf, BUF_LEN, TRUE);
658 contracts->add_contract(contracts, c2);
659
660 /* retrieve this contract */
661 cx = contracts->get_contract(contracts, msg_type1, FALSE, 2);
662 ck_assert(cx == c2);
663
664 /* received directed contract response as issuer */
665 cx = contracts->get_contract(contracts, msg_type1, TRUE, 3);
666 ck_assert(cx == NULL);
667
668 /* get contract template */
669 cx = contracts->get_contract(contracts, msg_type1, TRUE, TNC_IMCID_ANY);
670 ck_assert(cx == c1);
671
672 /* clone the contract template and as it as a directed contract */
673 c3 = cx->clone(cx);
674 c3->set_responder(c3, 3);
675 c3->get_info_string(c3, buf, BUF_LEN, FALSE);
676 contracts->add_contract(contracts, c3);
677
678 /* retrieve this contract */
679 cx = contracts->get_contract(contracts, msg_type1, TRUE, 3);
680 ck_assert(cx == c3);
681
682 /* received contract request for msg_type2 as responder */
683 cx = contracts->get_contract(contracts, msg_type2, FALSE, 2);
684 ck_assert(cx == NULL);
685
686 /* add directed contract as responder */
687 c4 = seg_contract_create(msg_type2, max_attr_size, max_seg_size,
688 FALSE, 2, FALSE);
689 c4->set_responder(c4, 1);
690 contracts->add_contract(contracts, c4);
691
692 /* retrieve this contract */
693 cx = contracts->get_contract(contracts, msg_type2, FALSE, 2);
694 ck_assert(cx == c4);
695
696 contracts->destroy(contracts);
697 }
698 END_TEST
699
700 Suite *imcv_seg_suite_create()
701 {
702 Suite *s;
703 TCase *tc;
704
705 s = suite_create("imcv_seg");
706
707 tc = tcase_create("env");
708 tcase_add_loop_test(tc, test_imcv_seg_env, 0, countof(seg_env_tests));
709 suite_add_tcase(s, tc);
710
711 tc = tcase_create("env_special");
712 tcase_add_test(tc, test_imcv_seg_env_special);
713 suite_add_tcase(s, tc);
714
715 tc = tcase_create("env_invalid");
716 tcase_add_loop_test(tc, test_imcv_seg_env_invalid, 0,
717 countof(env_invalid_tests));
718 suite_add_tcase(s, tc);
719
720 tc = tcase_create("contract");
721 tcase_add_loop_test(tc, test_imcv_seg_contract, 0, countof(seg_env_tests));
722 suite_add_tcase(s, tc);
723
724 tc = tcase_create("contract_special");
725 tcase_add_test(tc, test_imcv_seg_contract_special);
726 suite_add_tcase(s, tc);
727
728 tc = tcase_create("contract_invalid");
729 tcase_add_loop_test(tc, test_imcv_seg_contract_invalid, 0,
730 countof(contract_invalid_tests));
731 suite_add_tcase(s, tc);
732
733 tc = tcase_create("contract_mgr");
734 tcase_add_test(tc, test_imcv_seg_contract_mgr);
735 suite_add_tcase(s, tc);
736
737 return s;
738 }