commit:     de3c19ed21ceb3695130ff81e94170b08a2bf457
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Wed Dec 28 22:10:43 2016 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Wed Dec 28 22:10:43 2016 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=de3c19ed

tests: add a test for copy_file

 tests/Makefile             |  2 +-
 tests/copy_file/.gitignore |  1 +
 tests/copy_file/Makefile   | 18 +++++++++++++
 tests/copy_file/dotest     | 12 +++++++++
 tests/copy_file/test.c     | 64 ++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/tests/Makefile b/tests/Makefile
index bbe0fe8..fcf5a8c 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,5 +1,5 @@
 TESTS = \
-       reinitialize atom_compare atom_explode mkdir rmspace \
+       reinitialize atom_compare atom_explode copy_file mkdir rmspace \
        qatom qcheck qdepends qfile qlist qlop qmerge qtbz2 quse qxpak \
        install profile source
 

diff --git a/tests/copy_file/.gitignore b/tests/copy_file/.gitignore
new file mode 100644
index 0000000..28ce6a8
--- /dev/null
+++ b/tests/copy_file/.gitignore
@@ -0,0 +1 @@
+m

diff --git a/tests/copy_file/Makefile b/tests/copy_file/Makefile
new file mode 100644
index 0000000..bf52c55
--- /dev/null
+++ b/tests/copy_file/Makefile
@@ -0,0 +1,18 @@
+thisdir = copy_file
+include ../subdir.mk
+
+all: $(b)/m
+
+$(b)/m: $(s)/test.c
+       mkdir -p $(b)
+       $(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@
+
+test check: dotest
+
+dotest: $(b)/m
+       $(Q)$(s)/dotest
+
+clean:
+       rm -f $(b)/m
+
+.PHONY: all basic dotest test check clean

diff --git a/tests/copy_file/dotest b/tests/copy_file/dotest
new file mode 100755
index 0000000..970a5f9
--- /dev/null
+++ b/tests/copy_file/dotest
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+. ../init.sh || exit 1
+
+set -e
+
+m=${ab}/m
+
+${m}
+: $(( tpassed += 1 ))
+
+end

diff --git a/tests/copy_file/test.c b/tests/copy_file/test.c
new file mode 100644
index 0000000..deff965
--- /dev/null
+++ b/tests/copy_file/test.c
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2005-2016 Gentoo Foundation
+ * Distributed under the terms of the GNU General Public License v2
+ */
+
+#include "tests/tests.h"
+
+#include "libq/copy_file.c"
+
+static int src_fd, dst_fd;
+
+static void testone(const char *src_buf, size_t len)
+{
+       int ret;
+       static char dst_buf[50 * 1024 * 1024];
+
+       assert(len <= sizeof(dst_buf));
+
+       ret = ftruncate(src_fd, 0);
+       assert(ret == 0);
+       ret = lseek(src_fd, 0, SEEK_SET);
+       assert(ret == 0);
+       ret = ftruncate(dst_fd, 0);
+       assert(ret == 0);
+       ret = lseek(dst_fd, 0, SEEK_SET);
+       assert(ret == 0);
+
+       ret = write(src_fd, src_buf, len);
+       assert(ret == len);
+       ret = lseek(src_fd, 0, SEEK_SET);
+       assert(ret == 0);
+       ret = copy_file_fd(src_fd, dst_fd);
+       assert(ret == 0);
+
+       ret = lseek(dst_fd, 0, SEEK_SET);
+       assert(ret == 0);
+       ret = read(dst_fd, dst_buf, len);
+       assert(ret == len);
+
+       assert(memcmp(dst_buf, src_buf, len) == 0);
+}
+
+int main(int argc, char *argv[])
+{
+       size_t len;
+       char *buf;
+       char src_path[] = "portage-utils.src.XXXXXX";
+       char dst_path[] = "portage-utils.dst.XXXXXX";
+
+       src_fd = mkstemp(src_path);
+       assert(src_fd != -1);
+       unlink(src_path);
+       dst_fd = mkstemp(dst_path);
+       assert(dst_fd != -1);
+       unlink(dst_path);
+
+       testone("foo", 4);
+
+       len = 10 * 1024 * 1024;
+       buf = malloc(len);
+       assert(buf != NULL);
+       memset(buf, 0xaf, len);
+       testone(buf, len);
+}

Reply via email to