Package: libyang2
Version: 2.0.112-6
Severity: normal

Dear Maintainer,

libyang2 has a test suite that can run at build time, but it's
disabled by default in release builds. From upstream's README[1]:
```

The tests are by default built in the Debug build mode by running

$ make

In case of the Release mode, the tests are not built by default (it
requires additional dependency), but they can be enabled via cmake
option:

$ cmake -DENABLE_TESTS=ON ..
```

This is the diff for debian/rules to enable the tests with a release build:

--- a/debian/rules
+++ b/debian/rules
@@ -9,4 +9,5 @@ include /usr/share/dpkg/default.mk

 override_dh_auto_configure:
        dh_auto_configure -- \
-               -DCMAKE_BUILD_TYPE:String="Release"
+               -DCMAKE_BUILD_TYPE:String="Release" \
+               -DENABLE_TESTS=ON

Unfortunately, at least on Ubuntu, there were a couple of test
failures[2], which upstream promptly fixed[3]. I'm attaching the patch
I'm using in Ubuntu for that.

Thanks!



1. https://github.com/CESNET/libyang/blob/master/README.md
2. https://github.com/CESNET/libyang/issues/1773
3. 
https://github.com/CESNET/libyang/commit/de2e8b272b8238258f36c42386f3f2c9806026fd
Description: use test files instead of __FILE__
Author: Michal Vasko <mva...@cesnet.cz>
Origin: backport, https://github.com/CESNET/libyang/commit/de2e8b272b8238258f36c42386f3f2c9806026fd
Bug: https://github.com/CESNET/libyang/issues/1773
Bug-Ubuntu: https://bugs.launchpad.net/ubuntu/+source/libyang2/+bug/1958385
Last-Update: 2022-01-20
---
This patch header follows DEP-3: http://dep.debian.net/deps/dep3/
--- a/tests/utests/basic/test_context.c
+++ b/tests/utests/basic/test_context.c
@@ -38,9 +38,6 @@
     /* readable and executable, but not a directory */
     assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, TESTS_BIN "/utest_context"));
     CHECK_LOG_CTX("Given search directory \""TESTS_BIN "/utest_context\" is not a directory.", NULL);
-    /* not executable */
-    assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, __FILE__));
-    CHECK_LOG_CTX("Unable to fully access search directory \""__FILE__ "\" (Permission denied).", NULL);
     /* not existing */
     assert_int_equal(LY_EINVAL, ly_ctx_set_searchdir(UTEST_LYCTX, "/nonexistingfile"));
     CHECK_LOG_CTX("Unable to use search directory \"/nonexistingfile\" (No such file or directory).", NULL);
--- a/tests/utests/basic/test_inout.c
+++ b/tests/utests/basic/test_inout.c
@@ -26,6 +26,59 @@
 #include "log.h"
 #include "out.h"
 
+
+#define TEST_INPUT_FILE TESTS_BIN "/libyang_test_input"
+#define TEST_OUTPUT_FILE TESTS_BIN "/libyang_test_output"
+#define TEST_OUTPUT_FILE2 TESTS_BIN "/libyang_test_output2"
+
+static int
+setup_files(void **state)
+{
+    int fd;
+
+    UTEST_SETUP;
+
+    /* create input */
+    fd = open(TEST_INPUT_FILE, O_CREAT | O_WRONLY, 00600);
+    if (fd == -1) {
+        return 1;
+    }
+
+    /* write something */
+    if (write(fd, "data", 4) != 4) {
+        return 1;
+    }
+    close(fd);
+
+    /* create output */
+    fd = open(TEST_OUTPUT_FILE, O_CREAT | O_RDONLY, 00600);
+    if (fd == -1) {
+        return 1;
+    }
+    close(fd);
+
+    /* create output2 */
+    fd = open(TEST_OUTPUT_FILE2, O_CREAT | O_RDONLY, 00600);
+    if (fd == -1) {
+        return 1;
+    }
+    close(fd);
+
+    return 0;
+}
+
+static int
+teardown_files(void **state)
+{
+    unlink(TEST_INPUT_FILE);
+    unlink(TEST_OUTPUT_FILE);
+    unlink(TEST_OUTPUT_FILE2);
+
+    UTEST_TEARDOWN;
+    return 0;
+}
+
+
 static void
 test_input_mem(void **UNUSED(state))
 {
@@ -54,8 +107,8 @@
     assert_int_equal(LY_EINVAL, ly_in_new_fd(-1, NULL));
     assert_int_equal(-1, ly_in_fd(NULL, -1));
 
-    assert_int_not_equal(-1, fd1 = open(__FILE__, O_RDONLY));
-    assert_int_not_equal(-1, fd2 = open(__FILE__, O_RDONLY));
+    assert_int_not_equal(-1, fd1 = open(TEST_INPUT_FILE, O_RDONLY));
+    assert_int_not_equal(-1, fd2 = open(TEST_INPUT_FILE, O_RDONLY));
 
     assert_int_equal(LY_EINVAL, ly_in_new_fd(fd1, NULL));
 
@@ -83,8 +136,8 @@
     assert_int_equal(LY_EINVAL, ly_in_new_file(NULL, NULL));
     assert_null(ly_in_file(NULL, NULL));
 
-    assert_int_not_equal(-1, f1 = fopen(__FILE__, "r"));
-    assert_int_not_equal(-1, f2 = fopen(__FILE__, "r"));
+    assert_non_null(f1 = fopen(TEST_INPUT_FILE, "r"));
+    assert_non_null(f2 = fopen(TEST_INPUT_FILE, "r"));
 
     assert_int_equal(LY_EINVAL, ly_in_new_file(f1, NULL));
 
@@ -104,7 +157,7 @@
 test_input_filepath(void **UNUSED(state))
 {
     struct ly_in *in = NULL;
-    const char *path1 = __FILE__, *path2 = __FILE__;
+    const char *path1 = TEST_INPUT_FILE, *path2 = TEST_INPUT_FILE;
 
     assert_int_equal(LY_EINVAL, ly_in_new_filepath(NULL, 0, NULL));
     assert_int_equal(LY_EINVAL, ly_in_new_filepath(path1, 0, NULL));
@@ -154,10 +207,9 @@
     struct ly_out *out = NULL;
     int fd1, fd2;
     char buf[31] = {0};
-    const char *filepath = "/tmp/libyang_test_output";
 
-    assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
-    assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
 
     /* manipulate with the handler */
     assert_int_equal(LY_SUCCESS, ly_out_new_fd(fd1, &out));
@@ -171,8 +223,8 @@
     ly_out_free(out, NULL, 1);
 
     /* writing data */
-    assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
-    assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
     /* truncate file to start with no data */
     assert_int_equal(0, ftruncate(fd1, 0));
 
@@ -201,10 +253,9 @@
     struct ly_out *out = NULL;
     FILE *f1, *f2;
     char buf[31] = {0};
-    const char *filepath = "/tmp/libyang_test_output";
 
-    assert_int_not_equal(-1, f1 = fopen(filepath, "w"));
-    assert_int_not_equal(-1, f2 = fopen(filepath, "w"));
+    assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "w"));
+    assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "w"));
 
     /* manipulate with the handler */
     assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
@@ -218,8 +269,8 @@
     ly_out_free(out, NULL, 1);
 
     /* writing data */
-    assert_int_not_equal(-1, f1 = fopen(filepath, "w"));
-    assert_int_not_equal(-1, f2 = fopen(filepath, "r"));
+    assert_non_null(f1 = fopen(TEST_OUTPUT_FILE, "w"));
+    assert_non_null(f2 = fopen(TEST_OUTPUT_FILE, "r"));
 
     assert_int_equal(LY_SUCCESS, ly_out_new_file(f1, &out));
     assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
@@ -246,8 +297,8 @@
     struct ly_out *out = NULL;
     FILE *f1;
     char buf[31] = {0};
-    const char *fp1 = "/tmp/libyang_test_output";
-    const char *fp2 = "/tmp/libyang_test_output2";
+    const char *fp1 = TEST_OUTPUT_FILE;
+    const char *fp2 = TEST_OUTPUT_FILE2;
 
     /* manipulate with the handler */
     assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
@@ -260,7 +311,7 @@
     ly_out_free(out, NULL, 1);
 
     /* writing data */
-    assert_int_not_equal(-1, f1 = fopen(fp1, "r"));
+    assert_non_null(f1 = fopen(fp1, "r"));
 
     assert_int_equal(LY_SUCCESS, ly_out_new_filepath(fp1, &out));
     assert_int_equal(LY_SUCCESS, ly_print(out, "test %s", "print"));
@@ -299,10 +350,9 @@
     struct ly_out *out = NULL;
     int fd1, fd2;
     char buf[31] = {0};
-    const char *filepath = "/tmp/libyang_test_output";
 
-    assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
-    assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
 
     /* manipulate with the handler */
     assert_int_equal(LY_SUCCESS, ly_out_new_clb(write_clb, (void *)(intptr_t)fd1, &out));
@@ -317,8 +367,8 @@
     ly_out_free(out, close_clb, 0);
 
     /* writing data */
-    assert_int_not_equal(-1, fd1 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
-    assert_int_not_equal(-1, fd2 = open(filepath, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd1 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
+    assert_int_not_equal(-1, fd2 = open(TEST_OUTPUT_FILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR));
     /* truncate file to start with no data */
     assert_int_equal(0, ftruncate(fd1, 0));
 
@@ -337,14 +387,14 @@
 {
     const struct CMUnitTest tests[] = {
         UTEST(test_input_mem),
-        UTEST(test_input_fd),
-        UTEST(test_input_file),
-        UTEST(test_input_filepath),
+        UTEST(test_input_fd, setup_files, teardown_files),
+        UTEST(test_input_file, setup_files, teardown_files),
+        UTEST(test_input_filepath, setup_files, teardown_files),
         UTEST(test_output_mem),
-        UTEST(test_output_fd),
-        UTEST(test_output_file),
-        UTEST(test_output_filepath),
-        UTEST(test_output_clb),
+        UTEST(test_output_fd, setup_files, teardown_files),
+        UTEST(test_output_file, setup_files, teardown_files),
+        UTEST(test_output_filepath, setup_files, teardown_files),
+        UTEST(test_output_clb, setup_files, teardown_files),
     };
 
     return cmocka_run_group_tests(tests, NULL, NULL);

Reply via email to