summaryrefslogtreecommitdiff
path: root/src/p11-tests.c
blob: 4d8b9ff1bdf978610f906941809d3131162e569a (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

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

#include <stdio.h>
#include <stdlib.h>

#ifdef _WIN32

#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT 0x400
#include <windows.h>

static HMODULE module = NULL;

#else /* !_WIN32 */

#include <dlfcn.h>
#include <pthread.h>

static void *module = NULL;

#endif /* !_WIN32 */

int p11t_test_unexpected = 1;
int p11t_test_write_session = 0;

static void
usage()
{
	fprintf(stderr, "usage: p11-tests [-f config] module\n");
	exit(2);
}

void
fatal (const char *message, ...)
{
	va_list va;
	va_start (va, message);
	fprintf (stderr, message, va);
	va_end (va);
	exit (1);
}

CK_FUNCTION_LIST_PTR
module_load(const char *filename)
{
	CK_FUNCTION_LIST_PTR list = NULL;
	CK_C_GetFunctionList get_function_list;
	CK_RV rv;

#ifdef _WIN32

	module = LoadLibrary (filename);
	if (!module)
		fatal ("couldn't load library: %s: %s", filename, p11t_msg_os ());

	/* Lookup the appropriate function in the library */
	get_function_list = (CK_C_GetFunctionList)GetProcAddress (module, "C_GetFunctionList");
	if (!get_function_list)
		fatal ("couldn't find function 'C_GetFunctionList' in library: %s: %s",
		       filename, p11t_msg_os ());

#else /* !_WIN32 */

	module = dlopen (filename, RTLD_NOW);
	if (!module)
		fatal ("couldn't open library: %s: %s", filename, dlerror ());

	/* Lookup the appropriate function in library */
	get_function_list = (CK_C_GetFunctionList)dlsym (module, "C_GetFunctionList");
	if (!get_function_list)
		fatal ("couldn't find function 'C_GetFunctionList' in library: %s: %s",
		       filename, dlerror ());

#endif /* !_WIN32 */

	/* Get the function list */
	rv = (get_function_list) (&list);
	if(rv != CKR_OK || !list)
		fatal ("couldn't get function list: %d", (int)rv);

	return list;
}

void
module_unload (void)
{
	if (module) {
#ifdef _WIN32
		FreeLibrary (module);
#else
		dlclose (module);
#endif
	}
	module = NULL;
}

int
main(int argc, char* argv[])
{
	CK_FUNCTION_LIST_PTR module;
	int ch;

	while((ch = getopt(argc, argv, "f:svz")) != -1)
	{
		switch(ch)
		{
		case 'f':
			if (p11_tests_load_config (optarg) < 0)
				exit (2);
			break;
		case 's':
			p11_tests_set_write_session (1);
			break;
		case 'v':
			p11_tests_set_verbose (1);
			break;
		case 'z':
			p11_tests_set_unexpected (0);
			break;
		case '?':
		default:
			usage();
			break;
		}
	}

	argc -= optind;
	argv += optind;

	if(argc != 1)
		usage();

	module = module_load (argv[0]);
	p11_tests_perform (module);
	module_unload ();

	/* TODO: Change return value based on tests passed, failed */
	return 0;
}