tests: Add libhusb_close test
authorMichal Bloch <m.bloch@samsung.com>
Mon, 19 Oct 2015 13:49:41 +0000 (15:49 +0200)
committerStanislaw Wadas <s.wadas@samsung.com>
Wed, 2 Dec 2015 12:50:46 +0000 (13:50 +0100)
Change-Id: I2dbbcda98a01d053de80e39869d28e175205f6c2
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
tests/libhusb-test.c
tests/libusb-wrap.c

index d07e8b1ea57bbe0f49f5930b9ed70c2cbd7acf9d..231e6fc1d24a33483bedcff19555559475b0f07a 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;
@@ -544,6 +582,8 @@ static void test_interrupt_transfer_simple(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[] = {
 
@@ -553,6 +593,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 648220a3b5ff4999482e20ef878e0a9c0d04499a..eac127f146c949624cfc57cf8e605ebb372bb2d1 100644 (file)
@@ -191,6 +191,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)