xref: /illumos-gate/usr/src/test/os-tests/tests/uccid/excl-badread.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
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 various bad read conditions fail successfully.
18  */
19 
20 #include <err.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <strings.h>
26 #include <unistd.h>
27 #include <errno.h>
28 
29 #include <sys/usb/clients/ccid/uccid.h>
30 
31 int
32 main(int argc, char *argv[])
33 {
34 	int fd;
35 	uccid_cmd_txn_begin_t begin;
36 	ssize_t ret;
37 	char buf[500];
38 
39 	if (argc != 2) {
40 		errx(EXIT_FAILURE, "missing required ccid path");
41 	}
42 
43 	if ((fd = open(argv[1], O_RDWR)) < 0) {
44 		err(EXIT_FAILURE, "failed to open %s", argv[1]);
45 	}
46 
47 	bzero(&begin, sizeof (begin));
48 	begin.uct_version = UCCID_CURRENT_VERSION;
49 
50 	/*
51 	 * Read without having a transaction
52 	 */
53 	ret = read(fd, buf, sizeof (buf));
54 	if (ret != -1) {
55 		errx(EXIT_FAILURE, "read succeeded when it should have failed "
56 		    "(EACCES case), returned %ld", ret);
57 	}
58 
59 	if (errno != EACCES) {
60 		errx(EXIT_FAILURE, "found wrong value for errno. Expected "
61 		    "%d, received %d", EACCES, errno);
62 	}
63 
64 	if (ioctl(fd, UCCID_CMD_TXN_BEGIN, &begin) != 0) {
65 		err(EXIT_FAILURE, "failed to issue begin ioctl");
66 	}
67 
68 	ret = read(fd, buf, sizeof (buf));
69 	if (ret != -1) {
70 		errx(EXIT_FAILURE, "read succeeded when it should have failed "
71 		    "(ENODATA case), returned %ld", ret);
72 	}
73 
74 	if (errno != ENODATA) {
75 		errx(EXIT_FAILURE, "found wrong value for errno. Expected "
76 		    "%d, received %d", ENODATA, errno);
77 	}
78 
79 	return (0);
80 }
81