tests: Add test of getting device list
authorPawel Szewczyk <p.szewczyk@samsung.com>
Thu, 24 Sep 2015 13:41:30 +0000 (15:41 +0200)
committerStanislaw Wadas <s.wadas@samsung.com>
Wed, 2 Dec 2015 12:50:46 +0000 (13:50 +0100)
Change-Id: Ie84a227e75093c78ab7b561a3ff0674e18bf10f4
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
tests/libhusb-test.c
tests/libusb-wrap.c

index f8f0ff4988df399c04a41cbc2f938a5af118fc23..a1539175a4eed38988ed6afc242eed55ead8ef5d 100644 (file)
@@ -104,6 +104,31 @@ static void test_get_device(void **state)
        assert_ptr_equal(handle.device, device);
 }
 
+static void test_get_devices(void **state)
+{
+       int n = 7, i, ret;
+       libhusb_context *ctx;
+       libhusb_device **devs;
+
+       /* TODO init this with some (?) values */
+       libusb_device *lusb_devs[7];
+
+       ctx = (libhusb_context *)(*state);
+       assert_non_null(ctx);
+
+       will_return(libusb_get_device_list, n);
+       for (i = 0; i < n; ++i)
+               will_return(libusb_get_device_list, lusb_devs[i]);
+
+       ret = libhusb_get_devices(ctx, &devs);
+
+       assert_int_equal(ret, n);
+       for (i = 0; i < n; ++i)
+               assert_ptr_equal(devs[i]->lusb_dev, lusb_devs[i]);
+
+       libhusb_free_devices(devs, 0);
+}
+
 /* Custom macro for defining test with given name and fixed teardown function */
 #define HUSB_TEST_CTX(func) \
        cmocka_unit_test_setup_teardown(func, setup_libhusb_context, teardown_libhusb_context)
@@ -119,6 +144,7 @@ static struct CMUnitTest tests[] = {
                HUSB_TEST_CTX(test_init),
                HUSB_TEST_CTX(test_open),
                HUSB_TEST_NOSETUP(test_get_device),
+               HUSB_TEST_CTX(test_get_devices),
 };
 
 
index f1fb68b9f4023d7c5a4226eada996ae3f5488971..4e9286439860a2b03b7ab092911a7ea0af158289 100644 (file)
@@ -72,12 +72,23 @@ const char * libusb_error_name(int errcode)
 ssize_t libusb_get_device_list(libusb_context *ctx,
        libusb_device ***list)
 {
-       return 0;
+       int n, i;
+       libusb_device **rlist;
+
+       n = mock_type(int);
+       rlist = calloc(sizeof(libusb_device *), n);
+       for (i = 0; i < n; ++i) {
+               rlist[i] = mock_ptr_type(libusb_device *);
+       }
+
+       *list = rlist;
+       return n;
 }
 
 void libusb_free_device_list(libusb_device **list,
        int unref_devices)
 {
+       free(list);
 }
 
 libusb_device * libusb_ref_device(libusb_device *dev)