Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 20 May 2024 13:33:13 GMT
From:      Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= <des@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 02d98d1850f8 - main - libdiff: Add a test for the truncation issue.
Message-ID:  <202405201333.44KDXD6D045800@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=02d98d1850f870e535b93dccb7284dffe81d65d7

commit 02d98d1850f870e535b93dccb7284dffe81d65d7
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2024-05-20 13:26:42 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2024-05-20 13:26:42 +0000

    libdiff: Add a test for the truncation issue.
    
    Sponsored by:   Klara, Inc.
    Reviewed by:    allanjude
    Differential Revision:  https://reviews.freebsd.org/D45218
---
 etc/mtree/BSD.tests.dist         |  2 ++
 lib/libdiff/Makefile             |  5 ++++
 lib/libdiff/tests/Makefile       |  8 ++++++
 lib/libdiff/tests/libdiff_test.c | 61 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 76 insertions(+)

diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist
index d0451081022b..3f447f9ec25e 100644
--- a/etc/mtree/BSD.tests.dist
+++ b/etc/mtree/BSD.tests.dist
@@ -419,6 +419,8 @@
         ..
         libdevdctl
         ..
+        libdiff
+        ..
         libexecinfo
         ..
         libkvm
diff --git a/lib/libdiff/Makefile b/lib/libdiff/Makefile
index 3fa8e6b05d2d..8541ff424de2 100644
--- a/lib/libdiff/Makefile
+++ b/lib/libdiff/Makefile
@@ -1,3 +1,5 @@
+.include <src.opts.mk>
+
 LIB=	diff
 INTERNALLIB=	# API not published or supported.
 
@@ -12,4 +14,7 @@ WARNS=
 CFLAGS+= -I${SRCTOP}/contrib/libdiff/compat/include
 CFLAGS+= -I${SRCTOP}/contrib/libdiff/include
 
+HAS_TESTS=
+SUBDIR.${MK_TESTS}+= tests
+
 .include <bsd.lib.mk>
diff --git a/lib/libdiff/tests/Makefile b/lib/libdiff/tests/Makefile
new file mode 100644
index 000000000000..278ad7c95dcf
--- /dev/null
+++ b/lib/libdiff/tests/Makefile
@@ -0,0 +1,8 @@
+PACKAGE=	tests
+
+ATF_TESTS_C=	libdiff_test
+
+CFLAGS = -I${SRCTOP}/contrib/libdiff/lib -I${SRCTOP}/contrib/libdiff/include
+LIBADD = diff
+
+.include <bsd.test.mk>
diff --git a/lib/libdiff/tests/libdiff_test.c b/lib/libdiff/tests/libdiff_test.c
new file mode 100644
index 000000000000..f12717686721
--- /dev/null
+++ b/lib/libdiff/tests/libdiff_test.c
@@ -0,0 +1,61 @@
+/*-
+ * Copyright (c) 2024 Klara, Inc.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <arraylist.h>
+#include <diff_main.h>
+
+#include <atf-c.h>
+
+ATF_TC_WITH_CLEANUP(diff_atomize_truncated);
+ATF_TC_HEAD(diff_atomize_truncated, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Verify that the atomizer "
+	    "does not crash when an input file is truncated");
+}
+ATF_TC_BODY(diff_atomize_truncated, tc)
+{
+	char line[128];
+	struct diff_config cfg = { .atomize_func = diff_atomize_text_by_line };
+	struct diff_data d = { };
+	const char *fn = atf_tc_get_ident(tc);
+	FILE *f;
+	unsigned char *p;
+	size_t size = 65536;
+
+	ATF_REQUIRE((f = fopen(fn, "w+")) != NULL);
+	line[sizeof(line) - 1] = '\n';
+	for (unsigned int i = 0; i <= size / sizeof(line); i++) {
+		memset(line, 'a' + i % 26, sizeof(line) - 1);
+		ATF_REQUIRE(fwrite(line, sizeof(line), 1, f) == 1);
+	}
+	ATF_REQUIRE(fsync(fileno(f)) == 0);
+	rewind(f);
+	ATF_REQUIRE(truncate(fn, size / 2) == 0);
+	ATF_REQUIRE((p = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fileno(f), 0)) != MAP_FAILED);
+	ATF_REQUIRE(diff_atomize_file(&d, &cfg, f, p, size, 0) == 0);
+	ATF_REQUIRE((size_t)d.len <= size / 2);
+	ATF_REQUIRE((size_t)d.len >= size / 2 - sizeof(line));
+	ATF_REQUIRE(d.atomizer_flags & DIFF_ATOMIZER_FILE_TRUNCATED);
+}
+ATF_TC_CLEANUP(diff_atomize_truncated, tc)
+{
+	unlink(atf_tc_get_ident(tc));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+	ATF_TP_ADD_TC(tp, diff_atomize_truncated);
+	return atf_no_error();
+}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202405201333.44KDXD6D045800>