xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/status.c (revision 78801af7286cd73dbc996d470f789e75993cf15d)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2019, Joyent, Inc.
14  */
15 
16 /*
17  * Verify that we can issue various status ioctls regardless of whether or not
18  * we have exclusive access on our handle. Also, check some of the failure
19  * modes.
20  */
21 
22 #include <err.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <strings.h>
28 #include <unistd.h>
29 #include <sys/debug.h>
30 #include <errno.h>
31 #include <sys/mman.h>
32 #include <sys/param.h>
33 
34 #include <sys/usb/clients/ccid/uccid.h>
35 
36 int
37 main(int argc, char *argv[])
38 {
39 	int fd, efd, ret;
40 	uccid_cmd_status_t ucs;
41 	uccid_cmd_txn_begin_t begin;
42 	void *badaddr;
43 
44 	if (argc != 2) {
45 		errx(EXIT_FAILURE, "missing required ccid path");
46 	}
47 
48 	if ((fd = open(argv[1], O_RDWR)) < 0) {
49 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
50 	}
51 
52 	if ((efd = open(argv[1], O_RDWR)) < 0) {
53 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
54 	}
55 
56 	bzero(&begin, sizeof (begin));
57 	begin.uct_version = UCCID_CURRENT_VERSION;
58 	if (ioctl(efd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
59 		err(EXIT_FAILURE, "failed to issue begin ioctl");
60 	}
61 
62 	bzero(&ucs, sizeof (ucs));
63 	ucs.ucs_version = UCCID_CURRENT_VERSION;
64 
65 	ret = ioctl(fd, UCCID_CMD_STATUS, &ucs);
66 	VERIFY3S(ret, ==, 0);
67 
68 	ret = ioctl(efd, UCCID_CMD_STATUS, &ucs);
69 	VERIFY3S(ret, ==, 0);
70 
71 	ucs.ucs_version = UCCID_VERSION_ONE - 1;
72 	ret = ioctl(efd, UCCID_CMD_STATUS, &ucs);
73 	VERIFY3S(ret, ==, -1);
74 	VERIFY3S(errno, ==, EINVAL);
75 
76 	ucs.ucs_version = UCCID_VERSION_ONE + 1;
77 	ret = ioctl(efd, UCCID_CMD_STATUS, &ucs);
78 	VERIFY3S(ret, ==, -1);
79 	VERIFY3S(errno, ==, EINVAL);
80 
81 	ucs.ucs_version = UCCID_VERSION_ONE - 1;
82 	ret = ioctl(fd, UCCID_CMD_STATUS, &ucs);
83 	VERIFY3S(ret, ==, -1);
84 	VERIFY3S(errno, ==, EINVAL);
85 
86 	ucs.ucs_version = UCCID_VERSION_ONE + 1;
87 	ret = ioctl(fd, UCCID_CMD_STATUS, &ucs);
88 	VERIFY3S(ret, ==, -1);
89 	VERIFY3S(errno, ==, EINVAL);
90 
91 	badaddr = mmap(NULL, PAGESIZE, PROT_READ, MAP_PRIVATE | MAP_ANON, -1,
92 	    0);
93 	VERIFY3P(badaddr, !=, MAP_FAILED);
94 	VERIFY0(munmap(badaddr, PAGESIZE));
95 
96 	return (0);
97 }
98