Signed-off-by: Frediano Ziglio <fzig...@redhat.com>
---
 Makefile.am                 | 22 +++++++++++
 src/tests/test-file-xfers.c | 73 +++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+)
 create mode 100644 src/tests/test-file-xfers.c

diff --git a/Makefile.am b/Makefile.am
index 3e405bc..2aee440 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,6 +3,8 @@ NULL =
 
 bin_PROGRAMS = src/spice-vdagent
 sbin_PROGRAMS = src/spice-vdagentd
+check_PROGRAMS = src/test-file-xfers
+TESTS = $(check_PROGRAMS)
 
 common_sources =                               \
        src/udscs.c                             \
@@ -44,6 +46,26 @@ src_spice_vdagent_SOURCES =                  \
        src/vdagent/vdagent.c                   \
        $(NULL)
 
+src_test_file_xfers_CFLAGS =                   \
+       $(SPICE_CFLAGS)                         \
+       $(GLIB2_CFLAGS)                         \
+       -I$(srcdir)/src                         \
+       -I$(srcdir)/src/vdagent                 \
+       -DUDSCS_NO_SERVER                       \
+       $(NULL)
+
+src_test_file_xfers_LDADD =                    \
+       $(SPICE_LIBS)                           \
+       $(GLIB2_LIBS)                           \
+       $(NULL)
+
+src_test_file_xfers_SOURCES =                  \
+       $(common_sources)                       \
+       src/vdagent/file-xfers.c                \
+       src/vdagent/file-xfers.h                \
+       src/tests/test-file-xfers.c             \
+       $(NULL)
+
 src_spice_vdagentd_CFLAGS =                    \
        $(DBUS_CFLAGS)                          \
        $(LIBSYSTEMD_DAEMON_CFLAGS)             \
diff --git a/src/tests/test-file-xfers.c b/src/tests/test-file-xfers.c
new file mode 100644
index 0000000..494adc9
--- /dev/null
+++ b/src/tests/test-file-xfers.c
@@ -0,0 +1,73 @@
+/*  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");
+
+    // try to create a file with a path where there's a file (should fail)
+    test_file("test.txt/out", NULL);
+
+    assert(system("rm -rf test-dir") == 0);
+
+    return 0;
+}
-- 
2.20.1

_______________________________________________
Spice-devel mailing list
Spice-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/spice-devel

Reply via email to