1/*
2 * Check decoding of SCSI ioctl commands.
3 *
4 * Copyright (c) 2017 Dmitry V. Levin <ldv@altlinux.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 *    derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include "tests.h"
31
32#ifdef HAVE_SCSI_SG_H
33
34# include <stdio.h>
35# include <sys/ioctl.h>
36# include <scsi/sg.h>
37# include "xlat/scsi_sg_commands.h"
38
39# define TEST_NO_ARG(cmd)							\
40	do {									\
41		ioctl(-1, cmd, 0xdeadbeef);					\
42		printf("ioctl(-1, %s) = -1 EBADF (%m)\n", #cmd);		\
43	} while (0)
44
45# define TEST_NULL_ARG(cmd)							\
46	do {									\
47		ioctl(-1, cmd, 0);						\
48		printf("ioctl(-1, %s, NULL) = -1 EBADF (%m)\n", #cmd);		\
49	} while (0)
50
51# define TEST_TAKES_INT_BY_VAL(cmd, val)					\
52	do {									\
53		ioctl(-1, cmd, val);						\
54		printf("ioctl(-1, %s, %#x) = -1 EBADF (%m)\n", #cmd, val);	\
55	} while (0)
56
57# define TEST_TAKES_INT_BY_PTR(cmd, pint)					\
58	do {									\
59		ioctl(-1, cmd, pint);						\
60		printf("ioctl(-1, %s, [%d]) = -1 EBADF (%m)\n", #cmd, *(pint));	\
61	} while (0)
62
63# define TEST_RETURNS_INT_BY_PTR(cmd, pint)					\
64	do {									\
65		ioctl(-1, cmd, pint);						\
66		printf("ioctl(-1, %s, %p) = -1 EBADF (%m)\n", #cmd, pint);	\
67	} while (0)
68
69int
70main(void)
71{
72	int *const pint = tail_alloc(sizeof(*pint));
73	*pint = (int) 0xfacefeed;
74
75	TEST_NO_ARG(SG_GET_TIMEOUT);
76
77	TEST_NULL_ARG(SG_SET_TIMEOUT);
78	TEST_NULL_ARG(SG_EMULATED_HOST);
79	TEST_NULL_ARG(SG_GET_TRANSFORM);
80	TEST_NULL_ARG(SG_GET_COMMAND_Q);
81	TEST_NULL_ARG(SG_SET_COMMAND_Q);
82	TEST_NULL_ARG(SG_GET_RESERVED_SIZE);
83	TEST_NULL_ARG(SG_SET_RESERVED_SIZE);
84	TEST_NULL_ARG(SG_GET_SCSI_ID);
85	TEST_NULL_ARG(SG_SET_FORCE_LOW_DMA);
86	TEST_NULL_ARG(SG_GET_LOW_DMA);
87	TEST_NULL_ARG(SG_SET_FORCE_PACK_ID);
88	TEST_NULL_ARG(SG_GET_PACK_ID);
89	TEST_NULL_ARG(SG_GET_NUM_WAITING);
90	TEST_NULL_ARG(SG_SET_DEBUG);
91	TEST_NULL_ARG(SG_GET_SG_TABLESIZE);
92	TEST_NULL_ARG(SG_GET_VERSION_NUM);
93	TEST_NULL_ARG(SG_NEXT_CMD_LEN);
94	TEST_NULL_ARG(SG_SCSI_RESET);
95	TEST_NULL_ARG(SG_IO);
96	TEST_NULL_ARG(SG_GET_REQUEST_TABLE);
97	TEST_NULL_ARG(SG_SET_KEEP_ORPHAN);
98	TEST_NULL_ARG(SG_GET_KEEP_ORPHAN);
99	TEST_NULL_ARG(SG_GET_ACCESS_COUNT);
100
101	TEST_TAKES_INT_BY_VAL(SG_SET_TRANSFORM, 0);
102	TEST_TAKES_INT_BY_VAL(SG_SET_TRANSFORM, *pint);
103
104	TEST_TAKES_INT_BY_PTR(SG_NEXT_CMD_LEN, pint);
105	TEST_TAKES_INT_BY_PTR(SG_SET_COMMAND_Q, pint);
106	TEST_TAKES_INT_BY_PTR(SG_SET_DEBUG, pint);
107	TEST_TAKES_INT_BY_PTR(SG_SET_FORCE_LOW_DMA, pint);
108	TEST_TAKES_INT_BY_PTR(SG_SET_FORCE_PACK_ID, pint);
109	TEST_TAKES_INT_BY_PTR(SG_SET_KEEP_ORPHAN, pint);
110	TEST_TAKES_INT_BY_PTR(SG_SET_RESERVED_SIZE, pint);
111	TEST_TAKES_INT_BY_PTR(SG_SET_TIMEOUT, pint);
112
113	TEST_RETURNS_INT_BY_PTR(SG_EMULATED_HOST, pint);
114	TEST_RETURNS_INT_BY_PTR(SG_GET_ACCESS_COUNT, pint);
115	TEST_RETURNS_INT_BY_PTR(SG_GET_COMMAND_Q, pint);
116	TEST_RETURNS_INT_BY_PTR(SG_GET_KEEP_ORPHAN, pint);
117	TEST_RETURNS_INT_BY_PTR(SG_GET_LOW_DMA, pint);
118	TEST_RETURNS_INT_BY_PTR(SG_GET_NUM_WAITING, pint);
119	TEST_RETURNS_INT_BY_PTR(SG_GET_PACK_ID, pint);
120	TEST_RETURNS_INT_BY_PTR(SG_GET_RESERVED_SIZE, pint);
121	TEST_RETURNS_INT_BY_PTR(SG_GET_SG_TABLESIZE, pint);
122	TEST_RETURNS_INT_BY_PTR(SG_GET_TRANSFORM, pint);
123	TEST_RETURNS_INT_BY_PTR(SG_GET_VERSION_NUM, pint);
124
125	ioctl(-1, SG_SCSI_RESET, pint);
126	printf("ioctl(-1, %s, [%#x /* %s_??? */]) = -1 EBADF (%m)\n",
127	       "SG_SCSI_RESET", *pint, "SG_SCSI_RESET");
128
129	*pint = 0x100;
130	ioctl(-1, SG_SCSI_RESET, pint);
131	printf("ioctl(-1, %s, [SG_SCSI_RESET_NO_ESCALATE|SG_SCSI_RESET_NOTHING])"
132	       " = -1 EBADF (%m)\n", "SG_SCSI_RESET");
133
134	*pint = 1;
135	ioctl(-1, SG_SCSI_RESET, pint);
136	printf("ioctl(-1, %s, [SG_SCSI_RESET_DEVICE]) = -1 EBADF (%m)\n",
137	       "SG_SCSI_RESET");
138
139	ioctl(-1, 0x22ff, 0);
140	printf("ioctl(-1, _IOC(0, 0x22, 0xff, 0), 0) = -1 EBADF (%m)\n");
141
142	static const unsigned long magic =
143		(unsigned long) 0xdeadbeeffacefeedULL;
144	ioctl(-1, 0x22ff, magic);
145	printf("ioctl(-1, _IOC(0, 0x22, 0xff, 0), %#lx) = -1 EBADF (%m)\n",
146	       magic);
147
148	puts("+++ exited with 0 +++");
149	return 0;
150}
151
152#else
153
154SKIP_MAIN_UNDEFINED("HAVE_SCSI_SG_H")
155
156#endif
157