Signed-off-by: Frediano Ziglio <[email protected]>
---
Changes since v1:
- move file to a new tests/ directory;
- add missing .gitignore changes;
- improve test to catch errors in open() other than EEXISTS.
---
 .gitignore              | 17 +++++----
 Makefile.am             | 22 ++++++++++++
 tests/test-file-xfers.c | 77 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 107 insertions(+), 9 deletions(-)
 create mode 100644 tests/test-file-xfers.c

diff --git a/.gitignore b/.gitignore
index ae47a90..76d4081 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,19 +1,16 @@
 *~
+*.o
+.deps
+.dirstamp
 data/spice-vdagent*.1
 src/config.h
 src/config.h.in
 src/spice-vdagent
 src/spice-vdagentd
 src/stamp-h1
-src/*.o
-src/.deps
-src/.dirstamp
-src/vdagent/*.o
-src/vdagent/.deps
-src/vdagent/.dirstamp
-src/vdagentd/*.o
-src/vdagentd/.deps
-src/vdagentd/.dirstamp
+tests/test-*.log
+tests/test-*.trs
+tests/test-file-xfers
 config.log
 config.status
 aclocal.m4
@@ -25,3 +22,5 @@ install-sh
 Makefile.in
 Makefile
 missing
+test-driver
+test-suite.log
diff --git a/Makefile.am b/Makefile.am
index 3e405bc..97b8bf0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,6 +3,8 @@ NULL =
 
 bin_PROGRAMS = src/spice-vdagent
 sbin_PROGRAMS = src/spice-vdagentd
+check_PROGRAMS = tests/test-file-xfers
+TESTS = $(check_PROGRAMS)
 
 common_sources =                               \
        src/udscs.c                             \
@@ -44,6 +46,26 @@ src_spice_vdagent_SOURCES =                  \
        src/vdagent/vdagent.c                   \
        $(NULL)
 
+tests_test_file_xfers_CFLAGS =                 \
+       $(SPICE_CFLAGS)                         \
+       $(GLIB2_CFLAGS)                         \
+       -I$(srcdir)/src                         \
+       -I$(srcdir)/src/vdagent                 \
+       -DUDSCS_NO_SERVER                       \
+       $(NULL)
+
+tests_test_file_xfers_LDADD =                  \
+       $(SPICE_LIBS)                           \
+       $(GLIB2_LIBS)                           \
+       $(NULL)
+
+tests_test_file_xfers_SOURCES =                        \
+       $(common_sources)                       \
+       src/vdagent/file-xfers.c                \
+       src/vdagent/file-xfers.h                \
+       tests/test-file-xfers.c                 \
+       $(NULL)
+
 src_spice_vdagentd_CFLAGS =                    \
        $(DBUS_CFLAGS)                          \
        $(LIBSYSTEMD_DAEMON_CFLAGS)             \
diff --git a/tests/test-file-xfers.c b/tests/test-file-xfers.c
new file mode 100644
index 0000000..e40a89b
--- /dev/null
+++ b/tests/test-file-xfers.c
@@ -0,0 +1,77 @@
+/*  test-file-xfers.c  - test file transfer
+
+    Copyright 2019 Red Hat, Inc.
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+#include <config.h>
+
+#undef NDEBUG
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <glib.h>
+
+#include <spice/vd_agent.h>
+
+#include "file-xfers.h"
+
+static void test_file(const char *file_name, const char *out)
+{
+    char *fn = g_strdup(file_name);
+    int fd = vdagent_file_xfers_create_file("./test-dir", &fn);
+    if (out) {
+        g_assert_cmpint(fd, !=, -1);
+        g_assert_cmpstr(fn, ==, out);
+        close(fd);
+        g_assert_cmpint(access(out, W_OK), ==, 0);
+    } else {
+        g_assert_cmpint(fd, ==, -1);
+    }
+    g_free(fn);
+}
+
+int main(int argc, char *argv[])
+{
+    assert(system("rm -rf test-dir && mkdir test-dir") == 0);
+
+    // create a file
+    test_file("test.txt", "./test-dir/test.txt");
+
+    // create a file with an existing name
+    for (int i = 1; i < 64; ++i) {
+        char out_name[64];
+        sprintf(out_name, "./test-dir/test (%d).txt", i);
+        test_file("test.txt", out_name);
+    }
+
+    // check too much files with the same name
+    test_file("test.txt", NULL);
+
+    // create a file in a subdirectory not existing
+    test_file("subdir/test.txt", "./test-dir/subdir/test.txt");
+
+    // create a file in a directory with no permissions
+    assert(system("chmod 555 test-dir/subdir") == 0);
+    test_file("subdir/test2.txt", NULL);
+
+    // try to create a file with a path where there's a file (should fail)
+    test_file("test.txt/out", NULL);
+
+    assert(system("chmod 755 test-dir/subdir && rm -rf test-dir") == 0);
+
+    return 0;
+}
-- 
2.20.1

_______________________________________________
Spice-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/spice-devel

Reply via email to