summaryrefslogtreecommitdiff
path: root/src/check.c
blob: 01b19fb46e5050749e69dbb305502e31bfb24890 (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

#include "config.h"

#include "p11-tests.h"

#include <ctype.h>

int
p11t_check_returns(const char *message, CK_RV have, CK_RV want)
{
	if(have != want)
	{
		p11t_msg_print("%s: expected %s but got %s", message,
		               p11t_msg_rv(want), p11t_msg_rv(have));
		return 0;
	}

	return 1;
}

int
p11t_check_padded_len(const char *message, const CK_UTF8CHAR_PTR padded,
                      CK_ULONG length)
{
	CK_ULONG i;

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

		if(!padded[i])
		{
			p11t_msg_print("%s: not blank padded", message);
			return 0;
		}
	}

	return 1;
}

int
p11t_check_ulong(const char *message, CK_ULONG have, CK_ULONG want)
{
	if(have != want)
	{
		p11t_msg_print("%s: expected 0x%08x but have 0x%08x", message,
		               want, have);
		return 0;
	}

	return 1;
}

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

	return 1;
}

int
p11t_check_string(const char *message, CK_UTF8CHAR_PTR value, CK_ULONG length)
{
	CK_ULONG i;

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

	return 1;
}

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

	return 1;
}

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

	return 1;
}

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

	return 1;
}