• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * v4l-test: Test environment for Video For Linux Two API
3  *
4  *  4 Apr 2009  0.3  Test case for NULL parameter reworked
5  * 27 Mar 2009  0.2  Correct VIDIOC_S_PRIORITY test cases
6  *                   Clean up ret and errno variable names and dprintf() output
7  *  2 Feb 2009  0.1  First release
8  *
9  * Written by M�rton N�meth <nm127@freemail.hu>
10  * Released under GPL
11  */
12 
13 #include <stdio.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/ioctl.h>
19 #include <errno.h>
20 #include <string.h>
21 
22 #include <linux/videodev2.h>
23 #include <linux/errno.h>
24 
25 #include <CUnit/CUnit.h>
26 
27 #include "v4l2_test.h"
28 #include "dev_video.h"
29 #include "video_limits.h"
30 
31 #include "test_VIDIOC_PRIORITY.h"
32 
valid_priority(enum v4l2_priority priority)33 int valid_priority(enum v4l2_priority priority)
34 {
35 	int valid = 0;
36 
37 	CU_ASSERT_EQUAL(V4L2_PRIORITY_DEFAULT, V4L2_PRIORITY_INTERACTIVE);
38 
39 	switch (priority) {
40 	case V4L2_PRIORITY_UNSET:
41 	case V4L2_PRIORITY_BACKGROUND:
42 	case V4L2_PRIORITY_INTERACTIVE:
43 	case V4L2_PRIORITY_RECORD:
44 		valid = 1;
45 		break;
46 	default:
47 		valid = 0;
48 	}
49 	return valid;
50 }
51 
do_set_priority(enum v4l2_priority priority)52 static void do_set_priority(enum v4l2_priority priority)
53 {
54 	int ret_set, errno_set;
55 	int ret_get, errno_get;
56 	enum v4l2_priority new_priority;
57 
58 	dprintf("\t%s:%u: set priority to %i\n", __FILE__, __LINE__, priority);
59 	ret_set = ioctl(get_video_fd(), VIDIOC_S_PRIORITY, &priority);
60 	errno_set = errno;
61 
62 	dprintf("\t%s:%u: VIDIOC_S_PRIORITY, ret_set=%i, errno_set=%i\n",
63 		__FILE__, __LINE__, ret_set, errno_set);
64 
65 	CU_ASSERT_EQUAL(ret_set, 0);
66 	if (ret_set == 0) {
67 		memset(&new_priority, 0xff, sizeof(new_priority));
68 		ret_get =
69 		    ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &new_priority);
70 		errno_get = errno;
71 
72 		CU_ASSERT_EQUAL(ret_get, 0);
73 		if (ret_get == 0) {
74 			CU_ASSERT_EQUAL(new_priority, priority);
75 		}
76 	}
77 }
78 
do_set_invalid_priority(enum v4l2_priority orig_priority,enum v4l2_priority priority)79 static void do_set_invalid_priority(enum v4l2_priority orig_priority,
80 				    enum v4l2_priority priority)
81 {
82 	int ret_set, errno_set;
83 	int ret_get, errno_get;
84 	enum v4l2_priority new_priority;
85 
86 	dprintf("\t%s:%u: try to set priority to %i\n", __FILE__, __LINE__,
87 		priority);
88 	ret_set = ioctl(get_video_fd(), VIDIOC_S_PRIORITY, &priority);
89 	errno_set = errno;
90 
91 	dprintf("\t%s:%u: VIDIOC_S_PRIORITY, ret_set=%i, errno_set=%i\n",
92 		__FILE__, __LINE__, ret_set, errno_set);
93 
94 	CU_ASSERT_EQUAL(ret_set, -1);
95 	CU_ASSERT_EQUAL(errno_set, EINVAL);
96 	if (ret_set == -1 && errno_set == EINVAL) {
97 		memset(&new_priority, 0xff, sizeof(new_priority));
98 		ret_get =
99 		    ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &new_priority);
100 		errno_get = errno;
101 
102 		CU_ASSERT_EQUAL(ret_get, 0);
103 		if (ret_get == 0) {
104 			CU_ASSERT_EQUAL(new_priority, orig_priority);
105 		}
106 	}
107 }
108 
test_VIDIOC_G_PRIORITY()109 void test_VIDIOC_G_PRIORITY()
110 {
111 	int ret_get, errno_get;
112 	enum v4l2_priority orig_priority;
113 
114 	memset(&orig_priority, 0xff, sizeof(orig_priority));
115 	ret_get = ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &orig_priority);
116 	errno_get = errno;
117 
118 	dprintf
119 	    ("\t%s:%u: VIDIOC_G_PRIORITY, ret_get=%i, errno_get=%i, orig_priority=%i\n",
120 	     __FILE__, __LINE__, ret_get, errno_get, orig_priority);
121 
122 	if (ret_get == 0) {
123 		CU_ASSERT_EQUAL(ret_get, 0);
124 		CU_ASSERT(valid_priority(orig_priority));
125 
126 	} else {
127 		CU_ASSERT_EQUAL(ret_get, -1);
128 		CU_ASSERT_EQUAL(errno_get, EINVAL);
129 
130 	}
131 
132 }
133 
test_VIDIOC_G_PRIORITY_NULL()134 void test_VIDIOC_G_PRIORITY_NULL()
135 {
136 	int ret_get, errno_get;
137 	int ret_null, errno_null;
138 	enum v4l2_priority priority;
139 
140 	memset(&priority, 0xff, sizeof(priority));
141 	ret_get = ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &priority);
142 	errno_get = errno;
143 
144 	dprintf("\t%s:%u: VIDIOC_G_PRIORITY: ret_get=%i, errno_get=%i\n",
145 		__FILE__, __LINE__, ret_get, errno_get);
146 
147 	ret_null = ioctl(get_video_fd(), VIDIOC_G_PRIORITY, NULL);
148 	errno_null = errno;
149 
150 	dprintf("\t%s:%u: VIDIOC_G_PRIORITY: ret_null=%i, errno_null=%i\n",
151 		__FILE__, __LINE__, ret_null, errno_null);
152 
153 	/* check if VIDIOC_G_PRIORITY is supported at all or not */
154 	if (ret_get == -1 && errno_get == EINVAL) {
155 		/* VIDIOC_G_PRIORITY not supported at all, the parameter should not be evaluated */
156 		CU_ASSERT_EQUAL(ret_null, -1);
157 		CU_ASSERT_EQUAL(errno_null, EINVAL);
158 
159 	} else {
160 		/* VIDIOC_G_PRIORITY is supported, the parameter should be checked */
161 		CU_ASSERT_EQUAL(ret_null, -1);
162 		CU_ASSERT_EQUAL(errno_null, EFAULT);
163 	}
164 }
165 
test_VIDIOC_S_PRIORITY()166 void test_VIDIOC_S_PRIORITY()
167 {
168 	int ret_get, errno_get;
169 	enum v4l2_priority orig_priority;
170 
171 	memset(&orig_priority, 0xff, sizeof(orig_priority));
172 	ret_get = ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &orig_priority);
173 	errno_get = errno;
174 
175 	dprintf("\t%s:%u: VIDIOC_G_PRIORITY, ret_get=%i, errno_get=%i\n",
176 		__FILE__, __LINE__, ret_get, errno_get);
177 
178 	if (ret_get == 0) {
179 		CU_ASSERT_EQUAL(ret_get, 0);
180 		CU_ASSERT(valid_priority(orig_priority));
181 
182 		dprintf("\torig_priority = %u\n", orig_priority);
183 
184 		do_set_priority(V4L2_PRIORITY_UNSET);
185 		do_set_priority(V4L2_PRIORITY_BACKGROUND);
186 		do_set_priority(V4L2_PRIORITY_INTERACTIVE);
187 		do_set_priority(V4L2_PRIORITY_RECORD);
188 
189 		CU_ASSERT_EQUAL(V4L2_PRIORITY_DEFAULT,
190 				V4L2_PRIORITY_INTERACTIVE);
191 
192 		do_set_priority(orig_priority);
193 
194 	} else {
195 		CU_ASSERT_EQUAL(ret_get, -1);
196 		CU_ASSERT_EQUAL(errno_get, EINVAL);
197 
198 	}
199 }
200 
test_VIDIOC_S_PRIORITY_invalid()201 void test_VIDIOC_S_PRIORITY_invalid()
202 {
203 	int ret_get, errno_get;
204 	enum v4l2_priority orig_priority;
205 
206 	memset(&orig_priority, 0xff, sizeof(orig_priority));
207 	ret_get = ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &orig_priority);
208 	errno_get = errno;
209 
210 	dprintf("\t%s:%u: VIDIOC_G_PRIORITY, ret_get=%i, errno_get=%i\n",
211 		__FILE__, __LINE__, ret_get, errno_get);
212 
213 	if (ret_get == 0) {
214 		CU_ASSERT_EQUAL(ret_get, 0);
215 		CU_ASSERT(valid_priority(orig_priority));
216 
217 		dprintf("\torig_priority = %u\n", orig_priority);
218 
219 		do_set_invalid_priority(orig_priority, 4);
220 		do_set_invalid_priority(orig_priority, S32_MAX);
221 		do_set_invalid_priority(orig_priority, ((__u32) S32_MAX) + 1);
222 		do_set_invalid_priority(orig_priority, U32_MAX);
223 
224 		do_set_priority(orig_priority);
225 
226 	} else {
227 		CU_ASSERT_EQUAL(ret_get, -1);
228 		CU_ASSERT_EQUAL(errno_get, EINVAL);
229 
230 	}
231 }
232 
test_VIDIOC_S_PRIORITY_NULL()233 void test_VIDIOC_S_PRIORITY_NULL()
234 {
235 	int ret_orig, errno_orig;
236 	int ret_set, errno_set;
237 	int ret_null, errno_null;
238 	enum v4l2_priority priority_orig;
239 	enum v4l2_priority priority;
240 
241 	memset(&priority_orig, 0, sizeof(priority_orig));
242 	ret_orig = ioctl(get_video_fd(), VIDIOC_G_PRIORITY, &priority_orig);
243 	errno_orig = errno;
244 
245 	dprintf("\t%s:%u: VIDIOC_G_PRIORITY, ret_orig=%i, errno_orig=%i\n",
246 		__FILE__, __LINE__, ret_orig, errno_orig);
247 
248 	if (ret_orig == 0) {
249 		priority = priority_orig;
250 	} else {
251 		priority = V4L2_PRIORITY_DEFAULT;
252 	}
253 
254 	ret_set = ioctl(get_video_fd(), VIDIOC_S_PRIORITY, &priority);
255 	errno_set = errno;
256 
257 	dprintf("\t%s:%u: VIDIOC_S_PRIORITY, ret_set=%d, errno_set=%i\n",
258 		__FILE__, __LINE__, ret_set, errno_set);
259 
260 	ret_null = ioctl(get_video_fd(), VIDIOC_S_PRIORITY, NULL);
261 	errno_null = errno;
262 
263 	dprintf("\t%s:%u: VIDIOC_S_PRIORITY, ret_null=%d, errno_null=%i\n",
264 		__FILE__, __LINE__, ret_null, errno_null);
265 
266 	/* check if VIDIOC_S_PRIORITY is supported at all or not */
267 	if (ret_set == 0) {
268 		/* VIDIOC_S_PRIORITY is supported, the parameter should be checked */
269 		CU_ASSERT_EQUAL(ret_set, 0);
270 		CU_ASSERT_EQUAL(ret_null, -1);
271 		CU_ASSERT_EQUAL(errno_null, EFAULT);
272 	} else {
273 		/* VIDIOC_S_PRIORITY not supported at all, the parameter should not be evaluated */
274 		CU_ASSERT_EQUAL(ret_set, -1);
275 		CU_ASSERT_EQUAL(errno_set, EINVAL);
276 		CU_ASSERT_EQUAL(ret_null, -1);
277 		CU_ASSERT_EQUAL(errno_null, EINVAL);
278 	}
279 
280 }
281