1 /*
2  * v4l-test: Test environment for Video For Linux Two API
3  *
4  * 20 Apr 2009  0.6  Added string content validation
5  * 19 Apr 2009  0.5  Also check std field
6  * 18 Apr 2009  0.4  More strict check for strings
7  *  3 Apr 2009  0.3  Test case for NULL parameter reworked
8  * 28 Mar 2009  0.2  Clean up ret and errno variable names and dprintf() output
9  *  1 Jan 2009  0.1  First release
10  *
11  * Written by M�rton N�meth <nm127@freemail.hu>
12  * Released under GPL
13  */
14 
15 #include <stdio.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/ioctl.h>
21 #include <errno.h>
22 #include <string.h>
23 
24 #include <linux/videodev2.h>
25 #include <linux/errno.h>
26 
27 #include <CUnit/CUnit.h>
28 #include <CUnit/Basic.h>
29 
30 #include "v4l2_test.h"
31 #include "dev_video.h"
32 #include "video_limits.h"
33 #include "v4l2_validator.h"
34 
35 #include "test_VIDIOC_ENUMOUTPUT.h"
36 
test_VIDIOC_ENUMOUTPUT()37 void test_VIDIOC_ENUMOUTPUT()
38 {
39 	int ret_enum, errno_enum;
40 	struct v4l2_output output;
41 	struct v4l2_output output2;
42 	__u32 i;
43 
44 	i = 0;
45 	do {
46 		memset(&output, 0xff, sizeof(output));
47 		output.index = i;
48 		ret_enum = ioctl(get_video_fd(), VIDIOC_ENUMOUTPUT, &output);
49 		errno_enum = errno;
50 
51 		dprintf
52 		    ("\t%s:%u: VIDIOC_ENUMOUTPUT, ret_enum=%i, errno_enum=%i\n",
53 		     __FILE__, __LINE__, ret_enum, errno_enum);
54 
55 		if (ret_enum == 0) {
56 			CU_ASSERT_EQUAL(ret_enum, 0);
57 			CU_ASSERT_EQUAL(output.index, i);
58 
59 			CU_ASSERT(0 < strlen((char *)output.name));
60 			CU_ASSERT(valid_string
61 				  ((char *)output.name, sizeof(output.name)));
62 
63 			//CU_ASSERT_EQUAL(output.type, ?);
64 			//CU_ASSERT_EQUAL(output.audioset, ?);
65 			//CU_ASSERT_EQUAL(output.modulator, ?);
66 			CU_ASSERT(valid_v4l2_std_id(output.std));
67 			CU_ASSERT_EQUAL(output.reserved[0], 0);
68 			CU_ASSERT_EQUAL(output.reserved[1], 0);
69 			CU_ASSERT_EQUAL(output.reserved[2], 0);
70 			CU_ASSERT_EQUAL(output.reserved[3], 0);
71 
72 			/* Check if the unused bytes of the name string are
73 			 * also filled with zeros. Also check if there is any
74 			 * padding byte between any two fields then this
75 			 * padding byte is also filled with zeros.
76 			 */
77 			memset(&output2, 0, sizeof(output2));
78 			output2.index = output.index;
79 			strncpy((char *)output2.name, (char *)output.name,
80 				sizeof(output2.name));
81 			output2.type = output.type;
82 			output2.audioset = output.audioset;
83 			output2.modulator = output.modulator;
84 			output2.std = output.std;
85 			CU_ASSERT_EQUAL(memcmp
86 					(&output, &output2, sizeof(output)), 0);
87 
88 			dprintf("\toutput = {.index=%u, .name=\"%s\", "
89 				".type=0x%X, .audioset=0x%X, .modulator=0x%X, "
90 				".std=%llX, "
91 				".reserved[]={ 0x%X, 0x%X, 0x%X, 0x%X } }\n",
92 				output.index,
93 				output.name,
94 				output.type,
95 				output.audioset,
96 				output.modulator,
97 				output.std,
98 				output.reserved[0],
99 				output.reserved[1],
100 				output.reserved[2], output.reserved[3]
101 			    );
102 
103 		} else {
104 			CU_ASSERT_EQUAL(ret_enum, -1);
105 			CU_ASSERT_EQUAL(errno_enum, EINVAL);
106 
107 			memset(&output2, 0xff, sizeof(output2));
108 			output2.index = i;
109 			CU_ASSERT_EQUAL(memcmp
110 					(&output, &output2, sizeof(output)), 0);
111 
112 			dprintf("\terrno=%i\n", errno);
113 
114 		}
115 		i++;
116 	} while (ret_enum == 0);
117 
118 }
119 
test_VIDIOC_ENUMOUTPUT_S32_MAX()120 void test_VIDIOC_ENUMOUTPUT_S32_MAX()
121 {
122 	int ret_enum, errno_enum;
123 	struct v4l2_output output;
124 	struct v4l2_output output2;
125 
126 	memset(&output, 0xff, sizeof(output));
127 	output.index = (__u32) S32_MAX;
128 	ret_enum = ioctl(get_video_fd(), VIDIOC_ENUMOUTPUT, &output);
129 	errno_enum = errno;
130 
131 	CU_ASSERT_EQUAL(ret_enum, -1);
132 	CU_ASSERT_EQUAL(errno_enum, EINVAL);
133 
134 	memset(&output2, 0xff, sizeof(output2));
135 	output2.index = (__u32) S32_MAX;
136 	CU_ASSERT_EQUAL(memcmp(&output, &output2, sizeof(output)), 0);
137 }
138 
test_VIDIOC_ENUMOUTPUT_S32_MAX_1()139 void test_VIDIOC_ENUMOUTPUT_S32_MAX_1()
140 {
141 	int ret_enum, errno_enum;
142 	struct v4l2_output output;
143 	struct v4l2_output output2;
144 
145 	memset(&output, 0xff, sizeof(output));
146 	output.index = ((__u32) S32_MAX) + 1;
147 	ret_enum = ioctl(get_video_fd(), VIDIOC_ENUMOUTPUT, &output);
148 	errno_enum = errno;
149 
150 	CU_ASSERT_EQUAL(ret_enum, -1);
151 	CU_ASSERT_EQUAL(errno_enum, EINVAL);
152 
153 	memset(&output2, 0xff, sizeof(output2));
154 	output2.index = ((__u32) S32_MAX) + 1;
155 	CU_ASSERT_EQUAL(memcmp(&output, &output2, sizeof(output)), 0);
156 }
157 
test_VIDIOC_ENUMOUTPUT_U32_MAX()158 void test_VIDIOC_ENUMOUTPUT_U32_MAX()
159 {
160 	int ret_enum, errno_enum;
161 	struct v4l2_output output;
162 	struct v4l2_output output2;
163 
164 	memset(&output, 0xff, sizeof(output));
165 	output.index = U32_MAX;
166 	ret_enum = ioctl(get_video_fd(), VIDIOC_ENUMOUTPUT, &output);
167 	errno_enum = errno;
168 
169 	CU_ASSERT_EQUAL(ret_enum, -1);
170 	CU_ASSERT_EQUAL(errno_enum, EINVAL);
171 
172 	memset(&output2, 0xff, sizeof(output2));
173 	output2.index = U32_MAX;
174 	CU_ASSERT_EQUAL(memcmp(&output, &output2, sizeof(output)), 0);
175 }
176 
test_VIDIOC_ENUMOUTPUT_NULL()177 void test_VIDIOC_ENUMOUTPUT_NULL()
178 {
179 	int ret_enum, errno_enum;
180 	int ret_null, errno_null;
181 	struct v4l2_output output;
182 
183 	memset(&output, 0xff, sizeof(output));
184 	output.index = 0;
185 	ret_enum = ioctl(get_video_fd(), VIDIOC_ENUMOUTPUT, &output);
186 	errno_enum = errno;
187 
188 	dprintf("\t%s:%u: VIDIOC_ENUMOUTPUT, ret_enum=%i, errno_enum=%i\n",
189 		__FILE__, __LINE__, ret_enum, errno_enum);
190 
191 	ret_null = ioctl(get_video_fd(), VIDIOC_ENUMOUTPUT, NULL);
192 	errno_null = errno;
193 
194 	dprintf("\t%s:%u: VIDIOC_ENUMOUTPUT, ret_null=%i, errno_null=%i\n",
195 		__FILE__, __LINE__, ret_null, errno_null);
196 
197 	if (ret_enum == 0) {
198 		CU_ASSERT_EQUAL(ret_enum, 0);
199 		CU_ASSERT_EQUAL(ret_null, -1);
200 		CU_ASSERT_EQUAL(errno_null, EFAULT);
201 	} else {
202 		CU_ASSERT_EQUAL(ret_enum, -1);
203 		CU_ASSERT_EQUAL(errno_enum, EINVAL);
204 		CU_ASSERT_EQUAL(ret_null, -1);
205 		CU_ASSERT_EQUAL(errno_null, EINVAL);
206 	}
207 
208 }
209