From: Michal Bloch Date: Mon, 26 Oct 2015 17:45:42 +0000 (+0100) Subject: tests: add libhusb_{get,free}_config_descriptor test X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1d9025197ab446e577f359ae0bed4ca61bb4be2c;p=platform%2Fcore%2Fapi%2Fusb-host.git tests: add libhusb_{get,free}_config_descriptor test Change-Id: I910e7d899ba69b5a284a005e2b114f3a3217fc1c Signed-off-by: Michal Bloch --- diff --git a/tests/libhusb-test.c b/tests/libhusb-test.c index aeddf66..3db9305 100644 --- a/tests/libhusb-test.c +++ b/tests/libhusb-test.c @@ -680,6 +680,27 @@ static void test_get_string_descriptor_ascii(void **state) assert_string_equal(data, testString); } +static void test_config_descriptor(void **state) +{ + libhusb_cfg_descriptor *config; + libhusb_device *dev; + uint8_t config_index; + int ret, interfaces; + + config_index = rand(); + interfaces = rand() % 6 + 1; // not too large, n^4 objects are created + dev = (libhusb_device *)(*state); + assert_non_null(dev); + + will_return(libusb_get_config_descriptor, interfaces); + + ret = libhusb_get_config_descriptor(dev, config_index, &config); + + assert_return_code(ret, 0); + + libhusb_free_config_descriptor(config); +} + /* Custom macro for defining test with given name and fixed teardown function */ #define HUSB_TEST(func, setup, teardown) \ cmocka_unit_test_setup_teardown(func, setup, teardown) @@ -726,6 +747,7 @@ static struct CMUnitTest tests[] = { HUSB_TEST_DEV_HANDLE(test_detach_kernel_driver), HUSB_TEST_DEV_HANDLE(test_attach_kernel_driver), HUSB_TEST_DEV_HANDLE(test_get_string_descriptor_ascii), + HUSB_TEST_DEVICE(test_config_descriptor), }; diff --git a/tests/libusb-wrap.c b/tests/libusb-wrap.c index 3352095..818a81c 100644 --- a/tests/libusb-wrap.c +++ b/tests/libusb-wrap.c @@ -123,9 +123,73 @@ int libusb_get_active_config_descriptor(libusb_device *dev, return 0; } +static int deallocate_config_descriptor (struct libusb_config_descriptor *c, + int i, int j, int temp) +{ + while (i > 0) + { + --i; + while (j > 0) + { + --j; + free (c->interface[i].altsetting[j].endpoint); + } + j = temp; + free (c->interface[i].altsetting); + } + free (c->interface); + free (c); + + return LIBUSB_ERROR_NO_MEM; +} + int libusb_get_config_descriptor(libusb_device *dev, uint8_t config_index, struct libusb_config_descriptor **config) { + int i, j, k, temp; + + struct libusb_interface *iface; + struct libusb_interface_descriptor *desc; + struct libusb_endpoint_descriptor *ep; + + struct libusb_config_descriptor *c = malloc (sizeof (*c)); + if (!c) return LIBUSB_ERROR_NO_MEM; + + temp = mock_type(int); + + c->bNumInterfaces = temp; + c->interface = malloc(temp * sizeof(*iface)); + if (!c->interface) + { + free (c); + return LIBUSB_ERROR_NO_MEM; + } + + c->extra_length = 0; + for (i = 0; i < temp; ++i) + { + iface = c->interface + i; + iface->num_altsetting = temp; + iface->altsetting = malloc(temp * sizeof(*desc)); + if (!iface->altsetting) return deallocate_config_descriptor (c, i, j, temp); + for (j = 0; j < temp; ++j) + { + desc = iface->altsetting + j; + desc->bNumEndpoints = temp; + desc->endpoint = malloc(temp * sizeof(*ep)); + if (!desc->endpoint) return deallocate_config_descriptor (c, i, j, temp); + desc->extra_length = 0; + for (k = 0; k < temp; ++k) + { + ep = desc->endpoint + k; + ep->extra_length = 0; + } + } + j = 0; + } + + *config = c; + return 0; }