aboutsummaryrefslogtreecommitdiffstats
path: root/src/libstrongswan/plugins/bliss/tests/suites/test_bliss_bitpacker.c
blob: 6a728e280685adc24e4d862b8b5af5d0424e90d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2014 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "test_suite.h"

#include <bliss_bitpacker.h>

static uint32_t bits[] = { 0, 1, 2, 3, 4, 7, 1, 14, 2, 29, 3, 28, 67, 0x2fe3a9c1};

static chunk_t packed_bits = chunk_from_chars(0x6e, 0x71, 0xe1, 0x74,
											  0x37, 0x21, 0x97, 0xf1,
											  0xd4, 0xe0, 0x80);

START_TEST(test_bliss_sign_bitpacker_write)
{
	chunk_t buf;
	bliss_bitpacker_t *packer;
	int i;

	packer = bliss_bitpacker_create(81);

	for (i = 0; i < 13; i++)
	{
		ck_assert(packer->write_bits(packer, bits[i], 1 + i/2));
	}
	ck_assert(packer->write_bits(packer, bits[13], 32));

	buf = packer->extract_buf(packer);
	ck_assert_int_eq(packer->get_bits(packer), 81);
	ck_assert_chunk_eq(buf, packed_bits);

	packer->destroy(packer);
	free(buf.ptr);
}
END_TEST

START_TEST(test_bliss_sign_bitpacker_read)
{
	uint32_t value;
	bliss_bitpacker_t *packer;
	int i;

	packer = bliss_bitpacker_create_from_data(packed_bits);

	ck_assert(!packer->read_bits(packer, &value, 33));

	for (i = 0; i < 13; i++)
	{
		ck_assert(packer->read_bits(packer, &value, 1 + i/2));
		ck_assert_int_eq(value, bits[i]);
	}
	ck_assert(packer->read_bits(packer, &value, 32));
	ck_assert_int_eq(value, bits[13]);

	packer->destroy(packer);
}
END_TEST

START_TEST(test_bliss_sign_bitpacker_fail)
{
	bliss_bitpacker_t *packer;
	uint32_t value;

	packer = bliss_bitpacker_create(32);
	ck_assert( packer->write_bits(packer, 0xff, 0));
	ck_assert(!packer->write_bits(packer, 0, 33));
	ck_assert( packer->write_bits(packer, 0x7f2a3b01, 31));
	ck_assert(!packer->write_bits(packer, 3,  2));
	packer->destroy(packer);

	packer = bliss_bitpacker_create_from_data(
							chunk_from_chars(0x7f, 0x2a, 0x3b, 0x01));
	ck_assert(!packer->read_bits(packer, &value, 33));
	ck_assert( packer->read_bits(packer, &value, 31));
	ck_assert(!packer->read_bits(packer, &value,  2));
	packer->destroy(packer);
}
END_TEST

Suite *bliss_bitpacker_suite_create()
{
	Suite *s;
	TCase *tc;

	s = suite_create("bliss_bitpacker");

	tc = tcase_create("bitpacker_write");
	tcase_add_test(tc, test_bliss_sign_bitpacker_write);
	suite_add_tcase(s, tc);

	tc = tcase_create("bitpacker_read");
	tcase_add_test(tc, test_bliss_sign_bitpacker_read);
	suite_add_tcase(s, tc);

	tc = tcase_create("bitpacker_fail");
	tcase_add_test(tc, test_bliss_sign_bitpacker_fail);
	suite_add_tcase(s, tc);

	return s;
}