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)
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),
};
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;
}