unit-tests: Updated libimcv test suite
[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 <ietf/ietf_attr_pa_tnc_error.h>
23 #include <ita/ita_attr.h>
24 #include <ita/ita_attr_command.h>
25 #include <ita/ita_attr_dummy.h>
26 #include <tcg/seg/tcg_seg_attr_seg_env.h>
27
28 #include <tncif_pa_subtypes.h>
29
30 static struct {
31 uint32_t max_seg_size, next_segs, last_seg_size;
32 } seg_env_tests[] = {
33 { 0, 0, 0 },
34 { 11, 0, 0 },
35 { 12, 3, 12 },
36 { 13, 3, 9 },
37 { 15, 3, 3 },
38 { 16, 2, 16 },
39 { 17, 2, 14 },
40 { 23, 2, 2 },
41 { 24, 1, 24 },
42 { 25, 1, 23 },
43 { 47, 1, 1 },
44 { 48, 0, 0 },
45 };
46
47 static char command[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
48 static uint32_t id = 0x123456;
49
50 START_TEST(test_imcv_seg_env)
51 {
52 pa_tnc_attr_t *attr, *attr1, *base_attr, *base_attr1, *error;
53 tcg_seg_attr_seg_env_t *seg_env_attr;
54 ita_attr_command_t *ita_attr;
55 seg_env_t *seg_env, *seg_env1;
56 pen_type_t type;
57 uint32_t base_attr_id, max_seg_size, last_seg_size, seg_size, offset;
58 uint8_t flags;
59 bool last, last_seg;
60 chunk_t value, segment, seg;
61 int n;
62
63 libimcv_init(FALSE);
64 max_seg_size = seg_env_tests[_i].max_seg_size;
65 last_seg_size = seg_env_tests[_i].last_seg_size;
66 base_attr = ita_attr_command_create(command);
67 base_attr->build(base_attr);
68
69 seg_env = seg_env_create(id, base_attr, max_seg_size);
70 if (seg_env_tests[_i].next_segs == 0)
71 {
72 ck_assert(seg_env == NULL);
73 }
74 else
75 {
76 ck_assert(seg_env->get_base_attr_id(seg_env) == id);
77 base_attr1 = seg_env->get_base_attr(seg_env);
78 ck_assert(base_attr == base_attr1);
79 base_attr1->destroy(base_attr1);
80
81 for (n = 0; n <= seg_env_tests[_i].next_segs; n++)
82 {
83 last_seg = (n == seg_env_tests[_i].next_segs);
84 seg_size = (last_seg) ? last_seg_size : max_seg_size;
85 if (n == 0)
86 {
87 /* create first segment */
88 attr = seg_env->first_segment(seg_env);
89
90 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
91 segment = seg_env_attr->get_segment(seg_env_attr, &flags);
92 if (max_seg_size > 12)
93 {
94 seg = chunk_create(command, seg_size - 12);
95 ck_assert(chunk_equals(seg, chunk_skip(segment, 12)));
96 }
97 ck_assert(flags == (SEG_ENV_FLAG_MORE | SEG_ENV_FLAG_START));
98 }
99 else
100 {
101 /* create next segments */
102 attr = seg_env->next_segment(seg_env, &last);
103 ck_assert(last == last_seg);
104
105 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
106 segment = seg_env_attr->get_segment(seg_env_attr, &flags);
107 seg = chunk_create(command + n * max_seg_size - 12, seg_size);
108 ck_assert(chunk_equals(seg, segment));
109 ck_assert(flags == last_seg ? SEG_ENV_FLAG_NONE :
110 SEG_ENV_FLAG_MORE);
111 }
112
113 /* check built segment envelope attribute */
114 value = attr->get_value(attr);
115 ck_assert(value.len == 4 + seg_size);
116 ck_assert(segment.len == seg_size);
117 ck_assert(seg_env_attr->get_base_attr_id(seg_env_attr) == id);
118
119 /* create parse segment envelope attribute from data */
120 attr1 = tcg_seg_attr_seg_env_create_from_data(value.len, value);
121 ck_assert(attr1->process(attr1, &offset) == SUCCESS);
122 attr->destroy(attr);
123
124 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr1;
125 segment = seg_env_attr->get_segment(seg_env_attr, &flags);
126 base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
127 ck_assert(base_attr_id == id);
128
129 /* create and update seg_env object on the receiving side */
130 if (n == 0)
131 {
132 ck_assert(flags == (SEG_ENV_FLAG_MORE | SEG_ENV_FLAG_START));
133 seg_env1 = seg_env_create_from_data(base_attr_id, segment,
134 max_seg_size, &error);
135 }
136 else
137 {
138 ck_assert(flags == last_seg ? SEG_ENV_FLAG_NONE :
139 SEG_ENV_FLAG_MORE);
140 seg_env1->add_segment(seg_env1, segment, &error);
141 }
142 attr1->destroy(attr1);
143 }
144
145 /* check reconstructed base attribute */
146 base_attr1 = seg_env1->get_base_attr(seg_env1);
147 ck_assert(base_attr1);
148 type = base_attr1->get_type(base_attr1);
149 ck_assert(type.vendor_id == PEN_ITA);
150 ck_assert(type.type == ITA_ATTR_COMMAND);
151 ita_attr = (ita_attr_command_t*)base_attr1;
152 ck_assert(streq(ita_attr->get_command(ita_attr), command));
153
154 seg_env->destroy(seg_env);
155 seg_env1->destroy(seg_env1);
156 base_attr1->destroy(base_attr1);
157 }
158 base_attr->destroy(base_attr);
159 libimcv_deinit();
160 }
161 END_TEST
162
163 START_TEST(test_imcv_seg_env_special)
164 {
165 pa_tnc_attr_t *attr, *attr1, *base_attr;
166 tcg_seg_attr_seg_env_t *seg_env_attr;
167 pen_type_t type;
168 seg_env_t *seg_env;
169 chunk_t segment, value;
170 uint32_t max_seg_size = 47;
171 uint32_t last_seg_size = 1;
172 uint32_t offset = 12;
173
174 base_attr = ita_attr_command_create(command);
175 base_attr->build(base_attr);
176
177 /* set noskip flag in base attribute */
178 base_attr->set_noskip_flag(base_attr, TRUE);
179
180 seg_env = seg_env_create(id, base_attr, max_seg_size);
181 attr = seg_env->first_segment(seg_env);
182 attr->destroy(attr);
183
184 /* don't return last segment indicator */
185 attr = seg_env->next_segment(seg_env, NULL);
186
187 /* build attribute */
188 attr->build(attr);
189
190 /* don't return flags */
191 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
192 segment = seg_env_attr->get_segment(seg_env_attr, NULL);
193 ck_assert(segment.len == last_seg_size);
194
195 /* get segment envelope attribute reference and destroy it */
196 attr1 = attr->get_ref(attr);
197 attr1->destroy(attr1);
198
199 /* check some standard methods */
200 type = attr->get_type(attr);
201 ck_assert(type.vendor_id == PEN_TCG);
202 ck_assert(type.type == TCG_SEG_ATTR_SEG_ENV);
203 ck_assert(attr->get_noskip_flag(attr) == FALSE);
204 attr->set_noskip_flag(attr, TRUE);
205 ck_assert(attr->get_noskip_flag(attr) == TRUE);
206
207 /* request next segment which does not exist */
208 ck_assert(seg_env->next_segment(seg_env, NULL) == NULL);
209
210 /* create and parse a too short segment envelope attribute */
211 attr1 = tcg_seg_attr_seg_env_create_from_data(0, chunk_empty);
212 ck_assert(attr1->process(attr1, &offset) == FAILED);
213 ck_assert(offset == 0);
214 attr1->destroy(attr1);
215
216 /* create and parse correct segment envelope attribute */
217 value = attr->get_value(attr);
218 attr1 = tcg_seg_attr_seg_env_create_from_data(value.len, value);
219 ck_assert(attr1->process(attr1, &offset) == SUCCESS);
220 type = attr1->get_type(attr1);
221 ck_assert(type.vendor_id == PEN_TCG);
222 ck_assert(type.type == TCG_SEG_ATTR_SEG_ENV);
223 attr1->destroy(attr1);
224
225 /* cleanup */
226 attr->destroy(attr);
227 seg_env->destroy(seg_env);
228 base_attr->destroy(base_attr);
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, base_attr_r);
309
310 if (seg_env_tests[_i].next_segs == 0)
311 {
312 ck_assert(attr == NULL);
313 }
314 else
315 {
316 ck_assert(attr);
317 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
318 base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
319 ck_assert(base_attr_id == 1);
320 base_attr_i = contract_i->add_segment(contract_i, attr, &error, &more);
321 ck_assert(base_attr_i == NULL);
322 attr->destroy(attr);
323 ck_assert(more);
324 while (more)
325 {
326 attr = contract_r->next_segment(contract_r, base_attr_id);
327 ck_assert(attr);
328 seg_env_attr = (tcg_seg_attr_seg_env_t*)attr;
329 base_attr_id = seg_env_attr->get_base_attr_id(seg_env_attr);
330 ck_assert(base_attr_id == 1);
331 base_attr_i = contract_i->add_segment(contract_i, attr, &error,
332 &more);
333 attr->destroy(attr);
334 }
335 ck_assert(base_attr_i);
336 ck_assert(error == NULL);
337 type = base_attr_i->get_type(base_attr_i);
338 ck_assert(pen_type_equals(type, base_attr_r->get_type(base_attr_r)));
339 ita_attr = (ita_attr_command_t*)base_attr_i;
340 ck_assert(streq(ita_attr->get_command(ita_attr), command));
341 base_attr_i->destroy(base_attr_i);
342 }
343 contract_i->destroy(contract_i);
344 contract_r->destroy(contract_r);
345 base_attr_r->destroy(base_attr_r);
346 libimcv_deinit();
347 }
348 END_TEST
349
350 START_TEST(test_imcv_seg_contract_special)
351 {
352 seg_contract_t *contract_i, *contract_r;
353 tcg_seg_attr_seg_env_t *seg_env_attr1, *seg_env_attr2;
354 ita_attr_command_t *ita_attr;
355 pa_tnc_attr_t *base_attr1_i, *base_attr2_i, *base_attr1_r, *base_attr2_r;
356 pa_tnc_attr_t *attr1_f, *attr2_f, *attr1_n, *attr2_n, *attr3, *error;
357 pen_type_t type, msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
358 uint32_t max_seg_size, max_attr_size, issuer_id = 1;
359 uint32_t base_attr1_id, base_attr2_id;
360 char info[512];
361 bool oversize, more;
362
363 libimcv_init(FALSE);
364
365 /* create two base attributes to be segmented */
366 base_attr1_r = ita_attr_command_create(command);
367 base_attr2_r = ita_attr_dummy_create(129);
368 base_attr1_r->build(base_attr1_r);
369 base_attr2_r->build(base_attr2_r);
370
371 /* create an issuer contract*/
372 contract_i = seg_contract_create(msg_type, 1000, 47,
373 TRUE, issuer_id, FALSE);
374 ck_assert(pen_type_equals(contract_i->get_msg_type(contract_i), msg_type));
375 ck_assert(contract_i->is_issuer(contract_i));
376 ck_assert(!contract_i->is_null(contract_i));
377
378 /* set null contract */
379 contract_i->set_max_size(contract_i, SEG_CONTRACT_MAX_SIZE_VALUE,
380 SEG_CONTRACT_MAX_SIZE_VALUE);
381 ck_assert(contract_i->is_null(contract_i));
382
383 /* set and get maximum attribute and segment sizes */
384 contract_i->set_max_size(contract_i, 1000, 47);
385 contract_i->get_max_size(contract_i, NULL, NULL);
386 contract_i->get_max_size(contract_i, &max_attr_size, &max_seg_size);
387 contract_i->get_info_string(contract_i, info, sizeof(info), TRUE);
388 ck_assert(max_attr_size == 1000 && max_seg_size == 47);
389 ck_assert(!contract_i->is_null(contract_i));
390
391 /* create a null responder contract*/
392 contract_r = seg_contract_create(msg_type, SEG_CONTRACT_MAX_SIZE_VALUE,
393 SEG_CONTRACT_MAX_SIZE_VALUE,
394 FALSE, issuer_id, TRUE);
395 ck_assert(!contract_r->is_issuer(contract_r));
396 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
397 ck_assert(!oversize);
398
399 /* allow no fragmentation */
400 contract_r->set_max_size(contract_r, 1000, SEG_CONTRACT_MAX_SIZE_VALUE);
401 ck_assert(!contract_r->is_null(contract_r));
402 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
403 ck_assert(!oversize);
404
405 /* no maximum size limit and no fragmentation needed */
406 contract_r->set_max_size(contract_r, SEG_CONTRACT_MAX_SIZE_VALUE, 141);
407 ck_assert(!contract_r->is_null(contract_r));
408 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
409 ck_assert(!oversize);
410
411 /* oversize base attribute */
412 contract_r->set_max_size(contract_r, 140, 47);
413 ck_assert(!contract_r->is_null(contract_r));
414 ck_assert(!contract_r->check_size(contract_r, base_attr2_r, &oversize));
415 ck_assert(oversize);
416
417 /* set final maximum attribute and segment sizes */
418 contract_r->set_max_size(contract_r, 141, 47);
419 contract_r->get_info_string(contract_r, info, sizeof(info), TRUE);
420 ck_assert(contract_r->check_size(contract_r, base_attr2_r, &oversize));
421 ck_assert(!oversize);
422
423 /* get first segment of each base attribute */
424 attr1_f = contract_r->first_segment(contract_r, base_attr1_r);
425 attr2_f = contract_r->first_segment(contract_r, base_attr2_r);
426 ck_assert(attr1_f);
427 ck_assert(attr2_f);
428 seg_env_attr1 = (tcg_seg_attr_seg_env_t*)attr1_f;
429 seg_env_attr2 = (tcg_seg_attr_seg_env_t*)attr2_f;
430 base_attr1_id = seg_env_attr1->get_base_attr_id(seg_env_attr1);
431 base_attr2_id = seg_env_attr2->get_base_attr_id(seg_env_attr2);
432 ck_assert(base_attr1_id == 1);
433 ck_assert(base_attr2_id == 2);
434
435 /* get second segment of each base attribute */
436 attr1_n = contract_r->next_segment(contract_r, 1);
437 attr2_n = contract_r->next_segment(contract_r, 2);
438 ck_assert(attr1_n);
439 ck_assert(attr2_n);
440
441 /* process first segment of first base attribute */
442 base_attr1_i = contract_i->add_segment(contract_i, attr1_f, &error, &more);
443 ck_assert(base_attr1_i == NULL);
444 ck_assert(error == NULL);
445 ck_assert(more);
446
447 /* reapply first segment of first base attribute */
448 base_attr1_i = contract_i->add_segment(contract_i, attr1_f, &error, &more);
449 ck_assert(base_attr1_i == NULL);
450 ck_assert(error == NULL);
451 ck_assert(more);
452
453 /* process stray second segment of second attribute */
454 base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
455 ck_assert(base_attr2_i == NULL);
456 ck_assert(error == NULL);
457 ck_assert(more);
458
459 /* process first segment of second base attribute */
460 base_attr2_i = contract_i->add_segment(contract_i, attr2_f, &error, &more);
461 ck_assert(base_attr2_i == NULL);
462 ck_assert(error == NULL);
463 ck_assert(more);
464
465 /* try to get a segment of a non-existing base-attribute */
466 attr3 = contract_r->next_segment(contract_r, 3);
467 ck_assert(attr3 == NULL);
468
469 /* process second segment of first base attribute */
470 base_attr1_i = contract_i->add_segment(contract_i, attr1_n, &error, &more);
471 ck_assert(base_attr1_i);
472 ck_assert(error == NULL);
473 ck_assert(!more);
474
475 /* process second segment of second base attribute */
476 base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
477 ck_assert(base_attr2_i == NULL);
478 ck_assert(error == NULL);
479 ck_assert(more);
480
481 /* destroy first and second segments */
482 attr1_f->destroy(attr1_f);
483 attr2_f->destroy(attr2_f);
484 attr1_n->destroy(attr1_n);
485 attr2_n->destroy(attr2_n);
486
487 /* request surplus segment of first base attribute */
488 attr1_n = contract_r->next_segment(contract_r, 1);
489 ck_assert(attr1_n == NULL);
490
491 /* get last segment of second base attribute */
492 attr2_n = contract_r->next_segment(contract_r, 2);
493 ck_assert(attr2_n);
494
495 /* process last segment of second base attribute */
496 base_attr2_i = contract_i->add_segment(contract_i, attr2_n, &error, &more);
497 attr2_n->destroy(attr2_n);
498 ck_assert(base_attr2_i);
499 ck_assert(error == NULL);
500 ck_assert(!more);
501
502 /* request surplus segment of second base attribute */
503 attr2_n = contract_r->next_segment(contract_r, 2);
504 ck_assert(attr2_n == NULL);
505
506 /* compare original with reconstructed base attributes */
507 type = base_attr1_i->get_type(base_attr1_i);
508 ck_assert(pen_type_equals(type, base_attr1_r->get_type(base_attr1_r)));
509 ita_attr = (ita_attr_command_t*)base_attr1_i;
510 ck_assert(streq(ita_attr->get_command(ita_attr), command));
511
512 type = base_attr2_i->get_type(base_attr2_i);
513 ck_assert(pen_type_equals(type, base_attr2_r->get_type(base_attr2_r)));
514 ck_assert(chunk_equals(base_attr2_i->get_value(base_attr2_i),
515 base_attr2_r->get_value(base_attr2_r)));
516
517 /* cleanup */
518 base_attr1_r->destroy(base_attr1_r);
519 base_attr2_r->destroy(base_attr2_r);
520 base_attr1_i->destroy(base_attr1_i);
521 base_attr2_i->destroy(base_attr2_i);
522 contract_i->destroy(contract_i);
523 contract_r->destroy(contract_r);
524 libimcv_deinit();
525 }
526 END_TEST
527
528 static struct {
529 bool err_f;
530 chunk_t frag_f;
531 bool err_n;
532 bool base_attr;
533 chunk_t frag_n;
534 } contract_invalid_tests[] = {
535 { FALSE, chunk_from_chars(
536 0xc0, 0x00, 0x00, 0x01, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x01,
537 0x00, 0x00, 0x00, 0x0d),
538 FALSE, TRUE, chunk_from_chars(
539 0x00, 0x00, 0x00, 0x01, 0x01 )
540 },
541 { FALSE, chunk_from_chars(
542 0xc0, 0x00, 0x00, 0x02, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x01,
543 0x00, 0x00, 0x00, 0x0e),
544 TRUE, FALSE, chunk_from_chars(
545 0x00, 0x00, 0x00, 0x02, 0x01 )
546 },
547 { TRUE, chunk_from_chars(
548 0xc0, 0x00, 0x00, 0x03, 0x00, 0x00, 0x55, 0x97, 0x00, 0x00, 0x00, 0x23,
549 0x00, 0x00, 0x00, 0x0d),
550 FALSE, FALSE, chunk_from_chars(
551 0x00, 0x00, 0x00, 0x03, 0x01 )
552 },
553 { FALSE, chunk_from_chars(
554 0xc0, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
555 0x00, 0x00, 0x00, 0x14),
556 FALSE, FALSE, chunk_from_chars(
557 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 )
558 },
559 { FALSE, chunk_from_chars(
560 0xc0, 0x00, 0x00, 0x05, 0x00, 0x00, 0x90, 0x2a, 0x00, 0x00, 0x00, 0x03,
561 0x00, 0x00, 0x00, 0x0f),
562 TRUE, FALSE, chunk_from_chars(
563 0x00, 0x00, 0x00, 0x05, 0x00, 0x02, 0x01 )
564 },
565 { FALSE, chunk_from_chars(
566 0xc0, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
567 0x00, 0x00, 0x00, 0x11),
568 TRUE, FALSE, chunk_from_chars(
569 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0xff )
570 }
571 };
572
573 START_TEST(test_imcv_seg_contract_invalid)
574 {
575 uint32_t max_seg_size = 12, max_attr_size = 100, issuer_id = 1;
576 pen_type_t msg_type = { PEN_ITA, PA_SUBTYPE_ITA_TEST };
577 pa_tnc_attr_t *attr_f, *attr_n, *base_attr, *error;
578 chunk_t value_f, value_n;
579 seg_contract_t *contract;
580 uint32_t offset;
581 bool more;
582
583 libimcv_init(FALSE);
584 value_f = contract_invalid_tests[_i].frag_f;
585 value_n = contract_invalid_tests[_i].frag_n;
586 attr_f = tcg_seg_attr_seg_env_create_from_data(value_f.len, value_f);
587 attr_n = tcg_seg_attr_seg_env_create_from_data(value_n.len, value_n);
588 ck_assert(attr_f->process(attr_f, &offset) == SUCCESS);
589 ck_assert(attr_n->process(attr_n, &offset) == SUCCESS);
590
591 contract = seg_contract_create(msg_type, max_attr_size, max_seg_size,
592 TRUE, issuer_id, FALSE);
593 base_attr = contract->add_segment(contract, attr_f, &error, &more);
594 ck_assert(base_attr == NULL);
595
596 if (contract_invalid_tests[_i].err_f)
597 {
598 ck_assert(error);
599 error->destroy(error);
600 }
601 else
602 {
603 ck_assert(error == NULL);
604 ck_assert(more);
605 base_attr = contract->add_segment(contract, attr_n, &error, &more);
606 if (contract_invalid_tests[_i].err_n)
607 {
608 ck_assert(error);
609 error->destroy(error);
610 }
611 else
612 {
613 ck_assert(error == NULL);
614 }
615 if (contract_invalid_tests[_i].base_attr)
616 {
617 ck_assert(base_attr);
618 base_attr->destroy(base_attr);
619 }
620 }
621
622 /* cleanup */
623 attr_f->destroy(attr_f);
624 attr_n->destroy(attr_n);
625 contract->destroy(contract);
626 libimcv_deinit();
627 }
628 END_TEST
629
630 Suite *imcv_seg_suite_create()
631 {
632 Suite *s;
633 TCase *tc;
634
635 s = suite_create("imcv_seg");
636
637 tc = tcase_create("env");
638 tcase_add_loop_test(tc, test_imcv_seg_env, 0, countof(seg_env_tests));
639 suite_add_tcase(s, tc);
640
641 tc = tcase_create("env_special");
642 tcase_add_test(tc, test_imcv_seg_env_special);
643 suite_add_tcase(s, tc);
644
645 tc = tcase_create("env_invalid");
646 tcase_add_loop_test(tc, test_imcv_seg_env_invalid, 0,
647 countof(env_invalid_tests));
648 suite_add_tcase(s, tc);
649
650 tc = tcase_create("contract");
651 tcase_add_loop_test(tc, test_imcv_seg_contract, 0, countof(seg_env_tests));
652 suite_add_tcase(s, tc);
653
654 tc = tcase_create("contract_special");
655 tcase_add_test(tc, test_imcv_seg_contract_special);
656 suite_add_tcase(s, tc);
657
658 tc = tcase_create("contract_invalid");
659 tcase_add_loop_test(tc, test_imcv_seg_contract_invalid, 0,
660 countof(contract_invalid_tests));
661 suite_add_tcase(s, tc);
662
663 return s;
664 }