summaryrefslogtreecommitdiff
path: root/src/check.c
blob: 4284e70d71fb08154bd65dd3d572de640d65915a (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

#include "p11-tests.h"
#include "p11-tests-lib.h"

#include <ctype.h>
#include <stdarg.h>

int p11t_check_verbose = 0;

int
p11t_check_fail(const char *message, ...)
{
	va_list va;
	va_start(va, message);
	p11t_msg_va (P11_TESTS_FAIL, message, va);
	va_end(va);
	return STOP;
}

int
p11t_check_info(const char *message, ...)
{
	va_list va;
	if(!p11t_check_verbose)
		return CONTINUE;
	va_start(va, message);
	p11t_msg_va (P11_TESTS_INFO, message, va);
	va_end(va);
	return CONTINUE;
}

int
p11t_check_warn(const char *message, ...)
{
	va_list va;
	va_start(va, message);
	p11t_msg_va (P11_TESTS_WARN, message, va);
	va_end(va);
	return CONTINUE;
}

int
_p11t_check_rv(const char *what, CK_RV have, CK_RV want)
{
	if(have != want)
	{
		if(have == CKR_FUNCTION_NOT_SUPPORTED)
			p11t_check_info("Not supported");
		else
			p11t_check_fail("%s: expected %s but got %s", what,
			                p11t_msg_rv(want), p11t_msg_rv(have));
		return STOP;
	}

	return CONTINUE;
}

int
_p11t_check_padded(const char *what, const CK_UTF8CHAR_PTR padded,
                   CK_ULONG length)
{
	CK_ULONG i;

	for(i = 0; i < length; ++i)
	{
		if(!isprint(padded[i]))
		{
			p11t_check_fail("%s: non printable character: %d", what, padded[i]);
			return STOP;
		}

		if(!padded[i])
		{
			p11t_check_fail("%s: not blank padded", what);
			return STOP;
		}
	}

	return CONTINUE;
}

int
_p11t_check_ulong(const char *what, CK_ULONG have, CK_ULONG want)
{
	if(have != want)
	{
		p11t_check_fail("%s: expected 0x%08x but have 0x%08x", what,
		                want, have);
		return STOP;
	}

	return CONTINUE;
}

int
_p11t_check_bool(const char *what, CK_BBOOL value)
{
	if(value != CK_FALSE && value != CK_TRUE)
	{
		p11t_check_fail("%s: expected CK_TRUE or CK_FALSE but got 0x%02x",
		                what, (int)value);
		return STOP;
	}

	return CONTINUE;
}

static int
atoin (const char *p, int digits)
{
	int ret = 0, base = 1;
	while(--digits >= 0) {
		if (p[digits] < '0' || p[digits] > '9')
			return -1;
		ret += (p[digits] - '0') * base;
		base *= 10;
	}
	return ret;
}

int
_p11t_check_date(const char *what, CK_DATE* value)
{
	int year, month, day;

	year = atoin((const char*)value->year, 4);
	month = atoin((const char*)value->month, 2);
	day = atoin((const char*)value->day, 2);

	if(year < 0 || year > 9999 ||
	   month < 1 || month > 12 ||
	   day < 1 || day > 31)
	{
		p11t_check_fail("%s: invalid date", what);
		return STOP;
	}

	return CONTINUE;
}

int
_p11t_check_string(const char *what, CK_UTF8CHAR_PTR value, CK_ULONG length)
{
	CK_ULONG i;

	for(i = 0; i < length; ++i)
	{
		if(!value[i])
		{
			p11t_check_fail("%s: is null terminated", what);
			return STOP;
		}
	}

	return CONTINUE;
}

int
_p11t_check_mask(const char *what, CK_ULONG flags, CK_ULONG mask)
{
	if((flags & ~mask) != 0)
	{
		p11t_check_fail("%s: extra flags outside of mask: 0x%08x",
		                what, (flags & ~mask));
		return STOP;
	}

	return CONTINUE;
}

int
_p11t_check_flag(const char *what, CK_ULONG flags, CK_ULONG flag)
{
	if((flags & flag) != flag)
	{
		p11t_check_fail("%s: flag 0x%08x should be set in: 0x%08x",
		                what, flag, flags);
		return STOP;
	}

	return CONTINUE;
}

int
_p11t_check_nflag(const char *what, CK_ULONG flags, CK_ULONG nflag)
{
	if((flags & nflag) != 0)
	{
		p11t_check_fail("%s: flag 0x%08x should not be set in: 0x%08x",
		                what, nflag, flags);
		return STOP;
	}

	return CONTINUE;
}