]> git.ipfire.org Git - people/ms/strongswan.git/blob - src/libstrongswan/plugins/bliss/suites/test_bliss_bitpacker.c
Created bliss_bitpacker class to encode BLISS signatures
[people/ms/strongswan.git] / src / libstrongswan / plugins / bliss / suites / test_bliss_bitpacker.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 <bliss_bitpacker.h>
19
20 static uint16_t bits[] = { 0, 1, 2, 3, 4, 7, 1, 14, 2, 29, 3, 28, 67};
21
22 static chunk_t packed_bits = chunk_from_chars(0x6e, 0x71, 0xe1, 0x74,
23 0x37, 0x21, 0x80);
24
25 START_TEST(test_bliss_sign_bitpacker_write)
26 {
27 chunk_t buf;
28 bliss_bitpacker_t *packer;
29 int i;
30
31 packer = bliss_bitpacker_create(49);
32
33 ck_assert(!packer->write_bits(packer, 0, 17));
34
35 for (i = 0; i < countof(bits); i++)
36 {
37 ck_assert(packer->write_bits(packer, bits[i], 1 + i/2));
38 }
39 buf = packer->extract_buf(packer);
40 ck_assert_int_eq(packer->get_bits(packer), 49);
41 ck_assert_chunk_eq(buf, packed_bits);
42
43 packer->destroy(packer);
44 free(buf.ptr);
45 }
46 END_TEST
47
48 START_TEST(test_bliss_sign_bitpacker_read)
49 {
50 uint16_t value;
51 bliss_bitpacker_t *packer;
52 int i;
53
54 packer = bliss_bitpacker_create_from_data(packed_bits);
55
56 ck_assert(!packer->read_bits(packer, &value, 17));
57
58 for (i = 0; i < countof(bits); i++)
59 {
60 ck_assert(packer->read_bits(packer, &value, 1 + i/2));
61 ck_assert_int_eq(value, bits[i]);
62 }
63 ck_assert(!packer->read_bits(packer, &value, 16));
64
65 packer->destroy(packer);
66 }
67 END_TEST
68
69 START_TEST(test_bliss_sign_bitpacker_fail)
70 {
71 bliss_bitpacker_t *packer;
72 uint16_t value;
73
74 packer = bliss_bitpacker_create(16);
75 ck_assert(!packer->write_bits(packer, 0, 17));
76 ck_assert( packer->write_bits(packer, 0x7f01, 15));
77 ck_assert(!packer->write_bits(packer, 3, 2));
78 packer->destroy(packer);
79
80 packer = bliss_bitpacker_create_from_data(chunk_from_chars(0x7f, 0x01));
81 ck_assert(!packer->read_bits(packer, &value, 17));
82 ck_assert( packer->read_bits(packer, &value, 15));
83 ck_assert(!packer->read_bits(packer, &value, 2));
84 packer->destroy(packer);
85 }
86 END_TEST
87
88 Suite *bliss_bitpacker_suite_create()
89 {
90 Suite *s;
91 TCase *tc;
92
93 s = suite_create("bliss_bitpacker");
94
95 tc = tcase_create("bitpacker_write");
96 tcase_add_test(tc, test_bliss_sign_bitpacker_write);
97 suite_add_tcase(s, tc);
98
99 tc = tcase_create("bitpacker_read");
100 tcase_add_test(tc, test_bliss_sign_bitpacker_read);
101 suite_add_tcase(s, tc);
102
103 tc = tcase_create("bitpacker_fail");
104 tcase_add_test(tc, test_bliss_sign_bitpacker_fail);
105 suite_add_tcase(s, tc);
106
107 return s;
108 }