xref: /netbsd-src/external/bsd/libfido2/dist/regress/dev.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*
2  * Copyright (c) 2019 Yubico AB. All rights reserved.
3  * Use of this source code is governed by a BSD-style
4  * license that can be found in the LICENSE file.
5  */
6 
7 #include <assert.h>
8 #include <fido.h>
9 #include <string.h>
10 
11 #define FAKE_DEV_HANDLE	((void *)0xdeadbeef)
12 #define REPORT_LEN	(64 + 1)
13 
14 static void *
15 dummy_open(const char *path)
16 {
17 	(void)path;
18 
19 	return (FAKE_DEV_HANDLE);
20 }
21 
22 static void
23 dummy_close(void *handle)
24 {
25 	assert(handle == FAKE_DEV_HANDLE);
26 }
27 
28 static int
29 dummy_read(void *handle, unsigned char *ptr, size_t len, int ms)
30 {
31 	(void)ptr;
32 	(void)len;
33 	(void)ms;
34 
35 	assert(handle == FAKE_DEV_HANDLE);
36 
37 	return (-1);
38 }
39 
40 static int
41 dummy_write(void *handle, const unsigned char *ptr, size_t len)
42 {
43 	assert(handle == FAKE_DEV_HANDLE);
44 	assert(ptr != NULL);
45 	assert(len == REPORT_LEN);
46 
47 	return ((int)len);
48 }
49 
50 /* gh#56 */
51 static void
52 open_iff_ok(void)
53 {
54 	fido_dev_t	*dev = NULL;
55 	fido_dev_io_t	 io;
56 
57 	memset(&io, 0, sizeof(io));
58 
59 	io.open = dummy_open;
60 	io.close = dummy_close;
61 	io.read = dummy_read;
62 	io.write = dummy_write;
63 
64 	assert((dev = fido_dev_new()) != NULL);
65 	assert(fido_dev_set_io_functions(dev, &io) == FIDO_OK);
66 	assert(fido_dev_open(dev, "dummy") == FIDO_ERR_RX);
67 	assert(fido_dev_close(dev) == FIDO_ERR_INVALID_ARGUMENT);
68 
69 	fido_dev_free(&dev);
70 }
71 
72 int
73 main(void)
74 {
75 	fido_init(0);
76 
77 	open_iff_ok();
78 
79 	exit(0);
80 }
81