tests: Add libhusb_close test
authorMichal Bloch <m.bloch@samsung.com>
Mon, 19 Oct 2015 13:49:41 +0000 (15:49 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Tue, 20 Oct 2015 13:58:00 +0000 (15:58 +0200)
Change-Id: I2dbbcda98a01d053de80e39869d28e175205f6c2
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
tests/libhusb-test.c
tests/libusb-wrap.c

index c5488eb06280e3ddb8b378289f71d9e0e29f978b..e5cb1106278b2a58f3e68ec5cd701b745d1df77c 100644 (file)
@@ -26,6 +26,7 @@
 #include "libhusb.h"
 #include "libhusb_internal.h"
 #include <libusb-1.0/libusb.h>
+#include "uref.h"
 
 /* assertions and checks */
 
@@ -130,6 +131,43 @@ static void test_open(void **state)
        assert_ptr_equal(handle->lusb_dev_handle, lusb_handle);
 }
 
+/*
+ * A pair of functions required for uref,
+ * to which libhusb_close relegates some work
+ * and expects to find inside a libhusb_device,
+ * so we cannot use a mock instance.
+ *
+ * No libhusb function returns a libhusb_device,
+ * so we need to manually instantiate one.
+ */
+static inline struct libhusb_device *to_libhusb_device(struct uref *_uref)
+{
+       return container_of(_uref, struct libhusb_device, ref);
+}
+
+static void free_device(struct uref *uref)
+{
+       struct libhusb_device *dev = to_libhusb_device(uref);
+
+       libusb_unref_device(dev->lusb_dev);
+       free(dev);
+}
+
+static void test_close(void **state)
+{
+       libhusb_device_handle *handle;
+
+       handle = (libhusb_device_handle *) (*state);
+       assert_non_null(handle);
+
+       handle->device = malloc (sizeof (libhusb_device));
+       uref_init(&handle->device->ref, free_device);
+
+       expect_value (libusb_close, dev_handle, handle->lusb_dev_handle);
+
+       libhusb_close (handle);
+}
+
 static void test_get_device(void **state)
 {
        libhusb_device_handle handle;
@@ -363,6 +401,8 @@ static void test_control_transfer(void **state)
        cmocka_unit_test_setup_teardown(func, setup_libhusb_dev_handle, teardown_free)
 #define HUSB_TEST_NOSETUP(func) \
        cmocka_unit_test(func)
+#define HUSB_TEST_NO_TEARDOWN(func, setup) \
+       cmocka_unit_test_setup (func, setup)
 
 static struct CMUnitTest tests[] = {
 
@@ -372,6 +412,7 @@ static struct CMUnitTest tests[] = {
                 */
                HUSB_TEST_CTX(test_init),
                HUSB_TEST_CTX(test_open),
+               HUSB_TEST_NO_TEARDOWN(test_close, setup_libhusb_dev_handle), // the tested function does the teardown
                HUSB_TEST_NOSETUP(test_get_device),
                HUSB_TEST_CTX(test_get_devices),
                HUSB_TEST_DEVICE(test_get_max_packet_size),
index fd720b7f487dabb2829f1aca0172f6270eaac146..da01a3ad734974f88358fe846aa92adabb738469 100644 (file)
@@ -181,6 +181,7 @@ int libusb_open(libusb_device *dev, libusb_device_handle **handle)
 
 void libusb_close(libusb_device_handle *dev_handle)
 {
+       check_expected (dev_handle);
 }
 
 libusb_device * libusb_get_device(libusb_device_handle *dev_handle)