From: Michal Bloch Date: Mon, 19 Oct 2015 13:49:41 +0000 (+0200) Subject: tests: Add libhusb_close test X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1ea433a146ed62cf934afddda4b75f35cfa4d67b;p=platform%2Fcore%2Fapi%2Fusb-host.git tests: Add libhusb_close test Change-Id: I2dbbcda98a01d053de80e39869d28e175205f6c2 Signed-off-by: Michal Bloch --- diff --git a/tests/libhusb-test.c b/tests/libhusb-test.c index c5488eb..e5cb110 100644 --- a/tests/libhusb-test.c +++ b/tests/libhusb-test.c @@ -26,6 +26,7 @@ #include "libhusb.h" #include "libhusb_internal.h" #include +#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), diff --git a/tests/libusb-wrap.c b/tests/libusb-wrap.c index fd720b7..da01a3a 100644 --- a/tests/libusb-wrap.c +++ b/tests/libusb-wrap.c @@ -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)