summaryrefslogtreecommitdiffstats
path: root/tests/libdnssec/test_shared_bignum.c
blob: 0e8f8d52cbef29909efce76ce4e0c17e5522724f (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 3 of the License, or
    (at your option) any later version.

    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.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <tap/basic.h>
#include <string.h>

#include "bignum.h"
#include "binary.h"

#define bin_init(array) { .data = array, .size = sizeof(array) }

#define test_size(value, usize, ssize, msg) \
	dnssec_binary_t __bin = bin_init(value); \
	is_int(usize, bignum_size_u(&__bin), "bignum_size_u, " msg); \
	is_int(ssize, bignum_size_s(&__bin), "bignum_size_s, " msg)

#define test_write(num, expect, msg) \
	uint8_t __buffer[sizeof(expect)]; \
	memset(__buffer, 0xaa, sizeof(__buffer)); \
	wire_ctx_t __ctx = wire_ctx_init(__buffer, sizeof(expect)); \
	dnssec_binary_t __num = bin_init(num); \
	dnssec_binary_t __exp = bin_init(expect); \
	bignum_write(&__ctx, sizeof(expect), &__num); \
	dnssec_binary_t __dst = bin_init(__buffer); \
	ok(dnssec_binary_cmp(&__dst, &__exp) == 0, "bignum_write, " msg)

int main(int argc, char *argv[])
{
	plan_lazy();

	{
	uint8_t num[] = { };
	test_size(num, 1, 1, "empty string");
	}

	{
	uint8_t num[] = { 0x00 };
	test_size(num, 1, 1, "zero");
	}

	{
	uint8_t num[] = { 0x00, 0x00, 0x00 };
	test_size(num, 1, 1, "long zero");
	}

	{
	uint8_t num[] = { 0x01, 0x02, 0x03 };
	test_size(num, 3, 3, "no MSB");
	}

	{
	uint8_t num[] = { 0x7f, 0xff, 0x00, 0x00, 0x00 };
	test_size(num, 5, 5, "no MSB but all other bits");
	}

	{
	uint8_t num[] = { 0x84, 0x42 };
	test_size(num, 2, 3, "MSB");
	}

	{
	uint8_t num[] = { 0x00, 0x84, 0x42 };
	test_size(num, 2, 3, "MSB and leading zero");
	}

	{
	uint8_t num[] = { 0x00, 0x00, 0x00, 0x00, 0xfc, 0xe1, 0xda };
	test_size(num, 3, 4, "MSB, many leading zeroes");
	}

	{
	uint8_t num[] = { 0x00, 0x00, 0x00, 0x01 };
	test_size(num, 1, 1, "no MSB, many leading zeroes");
	}

	// test writing

	{
	uint8_t num[] = { };
	uint8_t exp[] = { 0x00 };
	test_write(num, exp, "empty string");
	}

	{
	uint8_t num[] = { 0x00 };
	uint8_t exp[] = { 0x00 };
	test_write(num, exp, "zero");
	}

	{
	uint8_t num[] = { 0x11, 0x22, 0x33 };
	uint8_t exp[] = { 0x00, 0x00, 0x00, 0x11, 0x22, 0x33 };
	test_write(num, exp, "no MSB, right-aligned");
	}

	{
	uint8_t num[] = { 0xff, 0xee, 0xdd };
	uint8_t exp[] = { 0x00, 0x00, 0x00, 0xff, 0xee, 0xdd };
	test_write(num, exp, "MSB, right-aligned");
	}

	{
	uint8_t num[] = { 0x11, 0x22, 0x33 };
	uint8_t exp[] = { 0x11, 0x22, 0x33 };
	test_write(num, exp, "no MSB, fitting exactly");
	}

	{
	uint8_t num[] = { 0xff, 0xee, 0xdd };
	uint8_t exp[] = { 0xff, 0xee, 0xdd };
	test_write(num, exp, "MSB, fitting exactly");
	}

	return 0;
}