tests: Add basic tests
authorPawel Szewczyk <p.szewczyk@samsung.com>
Mon, 28 Sep 2015 11:57:34 +0000 (13:57 +0200)
committerPawel Szewczyk <p.szewczyk@samsung.com>
Thu, 1 Oct 2015 14:39:35 +0000 (16:39 +0200)
Change-Id: I410c5e796f3e7dfd095c1b877db2dd607714d2d5
Signed-off-by: Pawel Szewczyk <p.szewczyk@samsung.com>
tests/libhusb-test.c [new file with mode: 0644]

diff --git a/tests/libhusb-test.c b/tests/libhusb-test.c
new file mode 100644 (file)
index 0000000..2b3f17c
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * libhusb-test.c
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <cmocka.h>
+
+#include "libhusb.h"
+#include "libhusb_internal.h"
+#include <libusb-1.0/libusb.h>
+
+/* assertions and checks */
+
+/**
+ * @brief Initialize library context and check results
+ */
+static int setup_libhusb_context(void **state)
+{
+       int ret;
+       libhusb_context *ctx;
+       libusb_context *lusb_ctx;
+
+       expect_any(libusb_init, ctx);
+       will_return(libusb_init, lusb_ctx);
+       will_return(libusb_init, LIBUSB_SUCCESS);
+
+       ret = libhusb_init(&ctx);
+
+       assert_return_code(ret, 0);
+       assert_ptr_equal(ctx->lusb_ctx, lusb_ctx);
+
+       *state = (void *)ctx;
+
+       return 0;
+}
+
+static int teardown_libhusb_context(void **state)
+{
+       libhusb_context *ctx;
+
+       ctx = (libhusb_context *)(*state);
+       expect_any(libusb_exit, ctx);
+       libhusb_exit(ctx);
+
+       return 0;
+}
+
+/**
+ * @brief Test if initialization works fine
+ */
+static void test_init(void **state)
+{
+       libhusb_context *ctx;
+
+       ctx = (libhusb_context *)(*state);
+       assert_non_null(ctx);
+}
+
+/* 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)
+
+static struct CMUnitTest tests[] = {
+
+               /**
+                * @some_test
+                * TODO: documentation
+                */
+               HUSB_TEST_CTX(test_init),
+};
+
+
+int main(int argc, char **argv)
+{
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}