Home | History | Annotate | Download | only in user_space
      1 /*
      2  * v4l-test: Test environment for Video For Linux Two API
      3  *
      4  * 20 Apr 2009  0.7  Added string content validation
      5  * 18 Apr 2009  0.6  More strict check for strings
      6  * 29 Mar 2009  0.5  Clean up test case for NULL parameter
      7  * 25 Mar 2009  0.4  Cleaned up dprintf() outputs and ret and errno names
      8  *  9 Feb 2009  0.3  Typo corrected; added some debug messages
      9  *  7 Feb 2009  0.2  Test case test_VIDIOC_G_AUDIO_ignore_index added
     10  *  3 Feb 2009  0.1  First release
     11  *
     12  * Written by Mrton Nmeth <nm127 (at) freemail.hu>
     13  * Released under GPL
     14  *
     15  */
     16 
     17 #include <stdio.h>
     18 #include <sys/types.h>
     19 #include <sys/stat.h>
     20 #include <fcntl.h>
     21 #include <unistd.h>
     22 #include <sys/ioctl.h>
     23 #include <errno.h>
     24 #include <string.h>
     25 
     26 #include <linux/videodev2.h>
     27 #include <linux/errno.h>
     28 
     29 #include <CUnit/CUnit.h>
     30 
     31 #include "v4l2_test.h"
     32 #include "dev_video.h"
     33 #include "video_limits.h"
     34 #include "v4l2_validator.h"
     35 
     36 #include "test_VIDIOC_ENUMAUDIO.h"
     37 
     38 int valid_audio_capability(__u32 capability)
     39 {
     40 	int valid = 0;
     41 
     42 	if ((capability & ~(V4L2_AUDCAP_STEREO | V4L2_AUDCAP_AVL))
     43 	    == 0) {
     44 		valid = 1;
     45 	} else {
     46 		valid = 0;
     47 	}
     48 	return valid;
     49 }
     50 
     51 int valid_audio_mode(__u32 mode)
     52 {
     53 	int valid = 0;
     54 
     55 	if ((mode & ~(V4L2_AUDMODE_AVL))
     56 	    == 0) {
     57 		valid = 1;
     58 	} else {
     59 		valid = 0;
     60 	}
     61 	return valid;
     62 }
     63 
     64 void test_VIDIOC_G_AUDIO()
     65 {
     66 	int ret_get, errno_get;
     67 	struct v4l2_audio audio;
     68 	struct v4l2_audio audio2;
     69 
     70 	memset(&audio, 0xff, sizeof(audio));
     71 	ret_get = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio);
     72 	errno_get = errno;
     73 
     74 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_get=%i, errno_get=%i\n",
     75 		__FILE__, __LINE__, ret_get, errno_get);
     76 
     77 	if (ret_get == 0) {
     78 		CU_ASSERT_EQUAL(ret_get, 0);
     79 
     80 		//CU_ASSERT_EQUAL(audio.index, ?);
     81 
     82 		CU_ASSERT(0 < strlen((char *)audio.name));
     83 		CU_ASSERT(valid_string((char *)audio.name, sizeof(audio.name)));
     84 
     85 		CU_ASSERT(valid_audio_capability(audio.capability));
     86 		CU_ASSERT(valid_audio_mode(audio.mode));
     87 
     88 		CU_ASSERT_EQUAL(audio.reserved[0], 0);
     89 		CU_ASSERT_EQUAL(audio.reserved[1], 0);
     90 
     91 		/* Check if the unused bytes of the name string are
     92 		 * also filled with zeros. Also check if there is any
     93 		 * padding byte between any two fields then this
     94 		 * padding byte is also filled with zeros.
     95 		 */
     96 		memset(&audio2, 0, sizeof(audio2));
     97 		audio2.index = audio.index;
     98 		strncpy((char *)audio2.name, (char *)audio.name,
     99 			sizeof(audio2.name));
    100 		audio2.capability = audio.capability;
    101 		audio2.mode = audio.mode;
    102 		CU_ASSERT_EQUAL(memcmp(&audio, &audio2, sizeof(audio)), 0);
    103 
    104 		dprintf("\taudio = {.index=%u, .name=\"%s\", "
    105 			".capability=0x%X, .mode=0x%X, "
    106 			".reserved[]={ 0x%X, 0x%X } }\n",
    107 			audio.index,
    108 			audio.name,
    109 			audio.capability,
    110 			audio.mode, audio.reserved[0], audio.reserved[1]
    111 		    );
    112 
    113 	} else {
    114 		CU_ASSERT_EQUAL(ret_get, -1);
    115 		CU_ASSERT_EQUAL(errno_get, EINVAL);
    116 
    117 		/* check if the audio structure is untouched */
    118 		memset(&audio2, 0xff, sizeof(audio2));
    119 		CU_ASSERT_EQUAL(memcmp(&audio, &audio2, sizeof(audio)), 0);
    120 
    121 	}
    122 
    123 }
    124 
    125 void test_VIDIOC_G_AUDIO_ignore_index()
    126 {
    127 	int ret_get, errno_get;
    128 	int ret2, errno2;
    129 	struct v4l2_audio audio;
    130 	struct v4l2_audio audio2;
    131 
    132 	/* check whether the "index" field is ignored by VIDIOC_G_AUDIO */
    133 
    134 	memset(&audio, 0, sizeof(audio));
    135 	dprintf("\t%s:%u: audio.index=%u\n", __FILE__, __LINE__, audio.index);
    136 	ret_get = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio);
    137 	errno_get = errno;
    138 
    139 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_get=%i, errno_get=%i\n",
    140 		__FILE__, __LINE__, ret_get, errno_get);
    141 
    142 	memset(&audio2, 0, sizeof(audio2));
    143 	audio2.index = U32_MAX;
    144 	ret2 = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio2);
    145 	errno2 = errno;
    146 
    147 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret2=%i, errno2=%i\n",
    148 		__FILE__, __LINE__, ret2, errno2);
    149 
    150 	if (ret_get == 0) {
    151 		CU_ASSERT_EQUAL(ret2, 0);
    152 		CU_ASSERT_EQUAL(memcmp(&audio, &audio2, sizeof(audio)), 0);
    153 	} else {
    154 		CU_ASSERT_EQUAL(ret_get, -1);
    155 		CU_ASSERT_EQUAL(errno_get, EINVAL);
    156 		CU_ASSERT_EQUAL(ret2, -1);
    157 		CU_ASSERT_EQUAL(errno2, EINVAL);
    158 	}
    159 
    160 }
    161 
    162 void test_VIDIOC_G_AUDIO_NULL()
    163 {
    164 	int ret_get, errno_get;
    165 	int ret_null, errno_null;
    166 	struct v4l2_audio audio;
    167 
    168 	memset(&audio, 0xff, sizeof(audio));
    169 	ret_get = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio);
    170 	errno_get = errno;
    171 
    172 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_get=%i, errno_get=%i\n",
    173 		__FILE__, __LINE__, ret_get, errno_get);
    174 
    175 	ret_null = ioctl(get_video_fd(), VIDIOC_G_AUDIO, NULL);
    176 	errno_null = errno;
    177 
    178 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_null=%i, errno_null=%i\n",
    179 		__FILE__, __LINE__, ret_null, errno_null);
    180 
    181 	/* check if VIDIOC_G_AUDIO is supported at all or not */
    182 	if (ret_get == 0) {
    183 		CU_ASSERT_EQUAL(ret_get, 0);
    184 		/* VIDIOC_G_AUDIO is supported, the parameter should be checked */
    185 		CU_ASSERT_EQUAL(ret_null, -1);
    186 		CU_ASSERT_EQUAL(errno_null, EFAULT);
    187 	} else {
    188 		CU_ASSERT_EQUAL(ret_get, -1);
    189 		CU_ASSERT_EQUAL(errno_get, EINVAL);
    190 		/* VIDIOC_G_AUDIO not supported at all, the parameter should not be evaluated */
    191 		CU_ASSERT_EQUAL(ret_null, -1);
    192 		CU_ASSERT_EQUAL(errno_null, EINVAL);
    193 	}
    194 
    195 }
    196 
    197 void test_VIDIOC_S_AUDIO()
    198 {
    199 	int ret_orig, errno_orig;
    200 	int ret_set, errno_set;
    201 	int ret_enum, errno_enum;
    202 	__u32 index;
    203 	__u32 i;
    204 	struct v4l2_audio audio_orig;
    205 	struct v4l2_audio audio_enum;
    206 	struct v4l2_audio audio_set;
    207 
    208 	/* This testcase tries to find out the relations between the following
    209 	 * commands:
    210 	 *  - VIDIOC_ENUMAUDIO
    211 	 *  - VIDIOC_G_AUDIO
    212 	 *  - VIDIOC_S_AUDIO
    213 	 */
    214 
    215 	/* remember the original settings */
    216 	memset(&audio_orig, 0, sizeof(audio_orig));
    217 	ret_orig = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio_orig);
    218 	errno_orig = errno;
    219 
    220 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_orig=%i, errno_orig=%i\n",
    221 		__FILE__, __LINE__, ret_orig, errno_orig);
    222 
    223 	if (ret_orig == 0) {
    224 		CU_ASSERT_EQUAL(ret_orig, 0);
    225 	} else {
    226 		CU_ASSERT_EQUAL(ret_orig, -1);
    227 		CU_ASSERT_EQUAL(errno_orig, EINVAL);
    228 	}
    229 
    230 	/* try to continue even if VIDIOC_G_AUDIO seems to be not supported */
    231 
    232 	index = 0;
    233 	do {
    234 		memset(&audio_enum, 0, sizeof(audio_enum));
    235 		audio_enum.index = index;
    236 		ret_enum = ioctl(get_video_fd(), VIDIOC_ENUMAUDIO, &audio_enum);
    237 		errno_enum = errno;
    238 
    239 		if (ret_enum == 0) {
    240 			memset(&audio_set, 0xff, sizeof(audio_set));
    241 			audio_set.index = index;
    242 			ret_set =
    243 			    ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    244 			errno_set = errno;
    245 
    246 			/* It shall be always possible to set the audio input to the
    247 			 * enumerated values.
    248 			 */
    249 			CU_ASSERT_EQUAL(ret_enum, 0);
    250 
    251 			index++;
    252 		}
    253 
    254 	} while (ret_enum == 0);
    255 	CU_ASSERT_EQUAL(ret_enum, -1);
    256 	CU_ASSERT_EQUAL(errno_enum, EINVAL);
    257 
    258 	/* try to set audio input to beyond the enumerated values */
    259 	for (i = 0; i <= 32; i++) {
    260 		memset(&audio_set, 0xff, sizeof(audio_set));
    261 		audio_set.index = index;
    262 		ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    263 		errno_set = errno;
    264 
    265 		CU_ASSERT_EQUAL(ret_set, -1);
    266 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    267 
    268 		index++;
    269 	}
    270 
    271 	/* restore the original audio input settings */
    272 	memset(&audio_set, 0, sizeof(audio_set));
    273 	audio_set.index = audio_orig.index;
    274 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    275 	errno_set = errno;
    276 
    277 	if (ret_orig == 0) {
    278 		/* If it was possible at the beginning to get the audio input then
    279 		 * it shall be possible to set it again.
    280 		 */
    281 		CU_ASSERT_EQUAL(ret_set, 0);
    282 	} else {
    283 		/* In case we could not fetch the audio input value at the start
    284 		 * of this test case: the VIDIOC_S_AUDIO shall also fail.
    285 		 */
    286 		CU_ASSERT_EQUAL(ret_set, -1);
    287 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    288 	}
    289 
    290 }
    291 
    292 void test_VIDIOC_S_AUDIO_S32_MAX()
    293 {
    294 	int ret_set, errno_set;
    295 	int ret_orig, errno_orig;
    296 	struct v4l2_audio audio;
    297 	struct v4l2_audio audio2;
    298 	struct v4l2_audio audio_orig;
    299 	struct v4l2_audio audio_set;
    300 
    301 	/* remember the original settings */
    302 	memset(&audio_orig, 0, sizeof(audio_orig));
    303 	ret_orig = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio_orig);
    304 	errno_orig = errno;
    305 
    306 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_orig=%i, errno_orig=%i\n",
    307 		__FILE__, __LINE__, ret_orig, errno_orig);
    308 
    309 	/* test invalid index */
    310 	memset(&audio, 0xff, sizeof(audio));
    311 	audio.index = (__u32) S32_MAX;
    312 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio);
    313 	errno_set = errno;
    314 
    315 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    316 		__FILE__, __LINE__, ret_set, errno_set);
    317 
    318 	CU_ASSERT_EQUAL(ret_set, -1);
    319 	CU_ASSERT_EQUAL(errno_set, EINVAL);
    320 
    321 	/* Check whether the original audio struct is untouched */
    322 	memset(&audio2, 0xff, sizeof(audio2));
    323 	audio2.index = (__u32) S32_MAX;
    324 	CU_ASSERT_EQUAL(memcmp(&audio, &audio2, sizeof(audio)), 0);
    325 
    326 	/* restore the original audio input settings */
    327 	memset(&audio_set, 0, sizeof(audio_set));
    328 	audio_set.index = audio_orig.index;
    329 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    330 	errno_set = errno;
    331 
    332 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    333 		__FILE__, __LINE__, ret_set, errno_set);
    334 
    335 	if (ret_orig == 0) {
    336 		/* If it was possible at the beginning to get the audio input then
    337 		 * it shall be possible to set it again.
    338 		 */
    339 		CU_ASSERT_EQUAL(ret_set, 0);
    340 	} else {
    341 		/* In case we could not fetch the audio input value at the start
    342 		 * of this test case: the VIDIOC_S_AUDIO shall also fail.
    343 		 */
    344 		CU_ASSERT_EQUAL(ret_set, -1);
    345 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    346 	}
    347 }
    348 
    349 void test_VIDIOC_S_AUDIO_S32_MAX_1()
    350 {
    351 	int ret_set, errno_set;
    352 	int ret_orig, errno_orig;
    353 	struct v4l2_audio audio;
    354 	struct v4l2_audio audio2;
    355 	struct v4l2_audio audio_orig;
    356 	struct v4l2_audio audio_set;
    357 
    358 	/* remember the original settings */
    359 	memset(&audio_orig, 0, sizeof(audio_orig));
    360 	ret_orig = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio_orig);
    361 	errno_orig = errno;
    362 
    363 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_orig=%i, errno_orig=%i\n",
    364 		__FILE__, __LINE__, ret_orig, errno_orig);
    365 
    366 	/* test invalid index */
    367 	memset(&audio, 0xff, sizeof(audio));
    368 	audio.index = ((__u32) S32_MAX) + 1;
    369 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio);
    370 	errno_set = errno;
    371 
    372 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    373 		__FILE__, __LINE__, ret_set, errno_set);
    374 
    375 	CU_ASSERT_EQUAL(ret_set, -1);
    376 	CU_ASSERT_EQUAL(errno_set, EINVAL);
    377 
    378 	/* Check whether the original audio struct is untouched */
    379 	memset(&audio2, 0xff, sizeof(audio2));
    380 	audio2.index = ((__u32) S32_MAX) + 1;
    381 	CU_ASSERT_EQUAL(memcmp(&audio, &audio2, sizeof(audio)), 0);
    382 
    383 	/* restore the original audio input settings */
    384 	memset(&audio_set, 0, sizeof(audio_set));
    385 	audio_set.index = audio_orig.index;
    386 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    387 	errno_set = errno;
    388 
    389 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    390 		__FILE__, __LINE__, ret_set, errno_set);
    391 
    392 	if (ret_orig == 0) {
    393 		/* If it was possible at the beginning to get the audio input then
    394 		 * it shall be possible to set it again.
    395 		 */
    396 		CU_ASSERT_EQUAL(ret_set, 0);
    397 	} else {
    398 		/* In case we could not fetch the audio input value at the start
    399 		 * of this test case: the VIDIOC_S_AUDIO shall also fail.
    400 		 */
    401 		CU_ASSERT_EQUAL(ret_set, -1);
    402 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    403 	}
    404 }
    405 
    406 void test_VIDIOC_S_AUDIO_U32_MAX()
    407 {
    408 	int ret_orig, errno_orig;
    409 	int ret_set, errno_set;
    410 	struct v4l2_audio audio;
    411 	struct v4l2_audio audio2;
    412 	struct v4l2_audio audio_orig;
    413 	struct v4l2_audio audio_set;
    414 
    415 	/* remember the original settings */
    416 	memset(&audio_orig, 0, sizeof(audio_orig));
    417 	ret_orig = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio_orig);
    418 	errno_orig = errno;
    419 
    420 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_orig=%i, errno_orig=%i\n",
    421 		__FILE__, __LINE__, ret_orig, errno_orig);
    422 	/* test invalid index */
    423 	memset(&audio, 0xff, sizeof(audio));
    424 	audio.index = U32_MAX;
    425 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio);
    426 	errno_set = errno;
    427 
    428 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    429 		__FILE__, __LINE__, ret_set, errno_set);
    430 
    431 	CU_ASSERT_EQUAL(ret_set, -1);
    432 	CU_ASSERT_EQUAL(errno_set, EINVAL);
    433 
    434 	/* Check whether the original audio struct is untouched */
    435 	memset(&audio2, 0xff, sizeof(audio2));
    436 	audio2.index = U32_MAX;
    437 	CU_ASSERT_EQUAL(memcmp(&audio, &audio2, sizeof(audio)), 0);
    438 
    439 	/* restore the original audio input settings */
    440 	memset(&audio_set, 0, sizeof(audio_set));
    441 	audio_set.index = audio_orig.index;
    442 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    443 	errno_set = errno;
    444 
    445 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    446 		__FILE__, __LINE__, ret_set, errno_set);
    447 
    448 	if (ret_orig == 0) {
    449 		/* If it was possible at the beginning to get the audio input then
    450 		 * it shall be possible to set it again.
    451 		 */
    452 		CU_ASSERT_EQUAL(ret_set, 0);
    453 	} else {
    454 		/* In case we could not fetch the audio input value at the start
    455 		 * of this test case: the VIDIOC_S_AUDIO shall also fail.
    456 		 */
    457 		CU_ASSERT_EQUAL(ret_set, -1);
    458 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    459 	}
    460 }
    461 
    462 void test_VIDIOC_S_AUDIO_NULL()
    463 {
    464 	int ret_orig, errno_orig;
    465 	int ret_set, errno_set;
    466 	int ret_null, errno_null;
    467 	struct v4l2_audio audio_orig;
    468 	struct v4l2_audio audio_set;
    469 
    470 	/* remember the original settings */
    471 	memset(&audio_orig, 0, sizeof(audio_orig));
    472 	ret_orig = ioctl(get_video_fd(), VIDIOC_G_AUDIO, &audio_orig);
    473 	errno_orig = errno;
    474 
    475 	dprintf("\t%s:%u: VIDIOC_G_AUDIO, ret_orig=%i, errno_orig=%i\n",
    476 		__FILE__, __LINE__, ret_orig, errno_orig);
    477 
    478 	memset(&audio_set, 0, sizeof(audio_set));
    479 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    480 	errno_set = errno;
    481 
    482 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    483 		__FILE__, __LINE__, ret_set, errno_set);
    484 
    485 	ret_null = ioctl(get_video_fd(), VIDIOC_S_AUDIO, NULL);
    486 	errno_null = errno;
    487 
    488 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_null=%i, errno_null=%i\n",
    489 		__FILE__, __LINE__, ret_null, errno_null);
    490 
    491 	if (ret_set == 0) {
    492 		CU_ASSERT_EQUAL(ret_set, 0);
    493 		CU_ASSERT_EQUAL(ret_null, -1);
    494 		CU_ASSERT_EQUAL(errno_null, EFAULT);
    495 	} else {
    496 		CU_ASSERT_EQUAL(ret_set, -1);
    497 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    498 		CU_ASSERT_EQUAL(ret_null, -1);
    499 		CU_ASSERT_EQUAL(errno_null, EINVAL);
    500 	}
    501 
    502 	/* restore the original audio input settings */
    503 	memset(&audio_set, 0, sizeof(audio_set));
    504 	audio_set.index = audio_orig.index;
    505 	ret_set = ioctl(get_video_fd(), VIDIOC_S_AUDIO, &audio_set);
    506 	errno_set = errno;
    507 
    508 	dprintf("\t%s:%u: VIDIOC_S_AUDIO, ret_set=%i, errno_set=%i\n",
    509 		__FILE__, __LINE__, ret_set, errno_set);
    510 
    511 	if (ret_orig == 0) {
    512 		/* If it was possible at the beginning to get the audio input then
    513 		 * it shall be possible to set it again.
    514 		 */
    515 		CU_ASSERT_EQUAL(ret_orig, 0);
    516 		CU_ASSERT_EQUAL(ret_set, 0);
    517 	} else {
    518 		/* In case we could not fetch the audio input value at the start
    519 		 * of this test case: the VIDIOC_S_AUDIO shall also fail.
    520 		 */
    521 		CU_ASSERT_EQUAL(ret_orig, -1);
    522 		CU_ASSERT_EQUAL(errno_orig, EINVAL);
    523 		CU_ASSERT_EQUAL(ret_set, -1);
    524 		CU_ASSERT_EQUAL(errno_set, EINVAL);
    525 	}
    526 }
    527