tests: add libhusb_{get,free}_config_descriptor test
authorMichal Bloch <m.bloch@samsung.com>
Mon, 26 Oct 2015 17:45:42 +0000 (18:45 +0100)
committerStanislaw Wadas <s.wadas@samsung.com>
Wed, 2 Dec 2015 12:50:47 +0000 (13:50 +0100)
Change-Id: I910e7d899ba69b5a284a005e2b114f3a3217fc1c
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
tests/libhusb-test.c
tests/libusb-wrap.c

index 02f16322403f4038f5b572cb9bd0cc11b39d419b..7abf9defd602d403f49126e36ba1bc212081b9ab 100644 (file)
@@ -722,6 +722,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)
@@ -771,6 +792,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),
 };
 
 
index 6543a4798ccf4fc960ea01b440ebfb7d2c9ddfb4..79e602632502bf34935fd188847c3f63a6986a7b 100644 (file)
@@ -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;
 }