#include "libhusb.h"
#include "libhusb_internal.h"
#include <libusb-1.0/libusb.h>
+#include "uref.h"
/* assertions and checks */
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;
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[] = {
*/
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),