1 #ifndef EIDMW_TESTLIB_H
2 #define EIDMW_TESTLIB_H
3 
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdbool.h>
7 
8 #define TEST_RV_SKIP 77		// defined by automake
9 #define TEST_RV_FAIL 1
10 #define TEST_RV_OK 0
11 
12 extern int va_counter;
13 extern int fc_counter;
14 
15 #ifndef TEST_NO_ABORT
16 #define my_assert(a) assert(a)
17 #else
18 #define my_assert(a) do { if(!(a)) { printf("failed!\n"); return TEST_RV_FAIL; }} while(0)
19 #endif
20 
21 #define verbose_assert(a) { printf("assertion %d: \"%s\": ", va_counter++, #a); my_assert((a)); printf("ok\n"); }
22 
23 #ifdef __GNUC__
24 #define EIDT_LIKELY(expr) __builtin_expect((expr), 1)
25 #define EIDT_UNLIKELY(expr) __builtin_expect((expr), 0)
26 #define EIDT_UNUSED __attribute__((unused))
27 #else
28 #define EIDT_LIKELY(expr) (expr)
29 #define EIDT_UNLIKELY(expr) (expr)
30 #define EIDT_UNUSED
31 #endif
32 
33 typedef struct
34 {
35 	CK_RV rv;
36 	int retval;
37 } ckrv_mod;
38 
39 static const ckrv_mod m_p11_noinit[] = {
40 	{CKR_OK, TEST_RV_FAIL},
41 	{CKR_CRYPTOKI_NOT_INITIALIZED, TEST_RV_OK},
42 };
43 
44 static const ckrv_mod m_p11_badarg[] = {
45 	{CKR_OK, TEST_RV_FAIL},
46 	{CKR_ARGUMENTS_BAD, TEST_RV_OK},
47 };
48 
49 static const ckrv_mod m_p11_badslot[] = {
50 	{CKR_OK, TEST_RV_FAIL},
51 	{CKR_SLOT_ID_INVALID, TEST_RV_OK},
52 };
53 
54 static const ckrv_mod m_p11_nocard[] = {
55 	{CKR_OK, TEST_RV_FAIL},
56 	{CKR_DEVICE_ERROR, TEST_RV_OK},
57 	{CKR_DEVICE_REMOVED, TEST_RV_OK},
58 };
59 
60 typedef enum {
61 	NOTHING_FOUND,
62 	VELLEMAN_FOUND,
63 	SYSTEM_FOUND,
64 } reading_pos;
65 
66 #define CHECK_RV_DEALLOC
67 #define check_rv_late(func) { int retval = ckrv_decode(rv, func, 0, NULL); if(EIDT_UNLIKELY(retval != TEST_RV_OK)) { printf("not ok\n"); C_Finalize(NULL_PTR); my_assert(retval != TEST_RV_FAIL); CHECK_RV_DEALLOC; return retval; }}
68 #define check_rv_late_long(func, mods) { int c = sizeof(mods) / sizeof(ckrv_mod); int retval = ckrv_decode(rv, func, c, (mods)); if(EIDT_UNLIKELY(retval != TEST_RV_OK)) { printf("not ok\n"); C_Finalize(NULL_PTR); my_assert(retval != TEST_RV_FAIL); CHECK_RV_DEALLOC; return retval; }}
69 #define check_rv(call) check_rv_action(call, 0, NULL)
70 #define check_rv_action(call, count, mods) { CK_RV rv = call; int retval = ckrv_decode(rv, #call, count, mods); if(EIDT_UNLIKELY(retval != TEST_RV_OK)) { printf("not ok\n"); C_Finalize(NULL_PTR); my_assert(retval != TEST_RV_FAIL); CHECK_RV_DEALLOC; return retval; }}
71 #define check_rv_long(call, mods) { int c = sizeof(mods) / sizeof(ckrv_mod); check_rv_action(call, c, mods); }
72 
73 int ckrv_decode(CK_RV rv, char *fc, int count, const ckrv_mod *);
74 
75 char *ckm_to_charp(CK_MECHANISM_TYPE);
76 
77 #ifdef HAVE_CONFIG_H
78 #define TEST_FUNC(a) int main(void)
79 #else
80 #define TEST_FUNC(a) int a(void)
81 #endif
82 
83 /* Verifies that a string does not contain a NULL character */
84 int verify_null_func(CK_UTF8CHAR * string, size_t length, int nulls_expected,
85 		     char *msg);
86 
87 #define verify_null(s, l, e, m) { int retval = verify_null_func(s, l, e, m); if(EIDT_UNLIKELY(retval != TEST_RV_OK)) return retval; }
88 
89 /* Functions to work with card moving robots */
90 CK_BBOOL have_robot(void);
91 CK_BBOOL is_manual_robot(void);
92 CK_BBOOL can_confirm(void);
93 CK_BBOOL have_pin(void);
94 CK_BBOOL can_enter_pin(CK_SLOT_ID slot);
95 CK_BBOOL open_robot(char* envvar);
96 CK_BBOOL open_reader_robot(char* envvar);
97 void robot_cmd(char cmd, CK_BBOOL check_result);
98 void reader_cmd(char cmd, CK_BBOOL check_result);
99 void robot_remove_card(void);
100 void robot_remove_card_delayed(void);
101 void robot_insert_card(void);
102 void robot_insert_card_delayed(void);
103 void robot_remove_reader(void);
104 void robot_remove_reader_delayed(void);
105 void robot_insert_reader(void);
106 void robot_insert_reader_delayed(void);
107 void hex_dump(char *data, CK_ULONG length);
108 
109 /* Functions to work with reader robots */
110 CK_BBOOL have_reader_robot(void);
111 void robot_remove_reader(void);
112 void robot_remove_reader_delayed(void);
113 void robot_insert_reader(void);
114 void robot_remove_card_delayed(void);
115 
116 enum robot_type {
117 	ROBOT_NONE,
118 	ROBOT_MECHANICAL_TURK,
119 	ROBOT_AUTO,
120 	ROBOT_AUTO_2,
121 };
122 
123 enum dialogs_type {
124 	DIALOGS_AVOID,
125 	DIALOGS_NOPIN,
126 	DIALOGS_OK,
127 };
128 
129 extern enum robot_type robot_type;
130 extern enum dialogs_type dialogs_type;
131 
132 /* Helper functions to not have to repeat common operations all the time */
133 int find_slot(CK_BBOOL with_token, CK_SLOT_ID_PTR slot);
134 
135 /* function definitions for tests that exist */
136 int init_finalize(void);
137 int getinfo(void);
138 int funclist(void);
139 int slotlist(void);
140 int slotinfo(void);
141 int tkinfo(void);
142 int double_init(void);
143 int fork_init(void);
144 int slotevent(void);
145 int mechlist(void);
146 int mechinfo(void);
147 int sessions(void);
148 int sessions_nocard(void);
149 int sessioninfo(void);
150 int slogin(void);
151 int scardcom_common(void);
152 int nonsensible(void);
153 int objects(void);
154 int readdata(void);
155 int readdata_limited(void);
156 int readdata_sequence(void);
157 int digest(void);
158 int threads(void);
159 int sign(void);
160 int sign_state(void);
161 int decode_photo(void);
162 int ordering(void);
163 int wrong_init(void);
164 int login_state(void);
165 int eject(void);
166 
167 #endif
168