summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorasomers <asomers@FreeBSD.org>2017-12-11 20:47:26 +0000
committerasomers <asomers@FreeBSD.org>2017-12-11 20:47:26 +0000
commited93a2a00a931a97076f2d61ed4ae436098bd1f4 (patch)
tree9bd3efced31913f574be4cebef8d8346c0d0d2f3 /lib
parent19b4a1ede4b55166d36ace3dc7070b4b54a4a312 (diff)
downloadFreeBSD-src-ed93a2a00a931a97076f2d61ed4ae436098bd1f4.zip
FreeBSD-src-ed93a2a00a931a97076f2d61ed4ae436098bd1f4.tar.gz
MFC r304443, r326034, r326065
r304443 by imp: Improve the pattern matching so that internal *'s work, as well as [set] notation. This fixes pattern matching for recently added drives that would set the NCQ Trim being broken incorrectly. PR: 210686 Tested-by: Tomoaki AOKI r326034: Fix multiple bugs in cam_strmatch * Wrongly matches strings that are shorter than the pattern * Fails to match negative character sets * Fails to match character sets that aren't at the end of the pattern * Fails to match character ranges Reviewed by: imp Sponsored by: Spectra Logic Corp Differential Revision: https://reviews.freebsd.org/D13173 r326065: Fix uninitialized variable from 326034 Reported by: Coverity CID: 1382887 X-MFC-With: 326034 Sponsored by: Spectra Logic Corp
Diffstat (limited to 'lib')
-rw-r--r--lib/libcam/tests/Makefile1
-rw-r--r--lib/libcam/tests/cam_test.c111
-rw-r--r--lib/libcam/tests/libcam_test.c2
3 files changed, 114 insertions, 0 deletions
diff --git a/lib/libcam/tests/Makefile b/lib/libcam/tests/Makefile
index e088bf7..178c308 100644
--- a/lib/libcam/tests/Makefile
+++ b/lib/libcam/tests/Makefile
@@ -1,6 +1,7 @@
# $FreeBSD$
ATF_TESTS_C+= libcam_test
+ATF_TESTS_C+= cam_test
LIBADD+= cam
diff --git a/lib/libcam/tests/cam_test.c b/lib/libcam/tests/cam_test.c
new file mode 100644
index 0000000..0c62f0f
--- /dev/null
+++ b/lib/libcam/tests/cam_test.c
@@ -0,0 +1,111 @@
+/*-
+ * Copyright (c) 2017 Spectra Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+/* Tests functions in sys/cam/cam.c */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <camlib.h>
+
+#include <atf-c.h>
+
+#define ATF_CHECK_NE(x, y) ATF_CHECK((x) != (y))
+
+ATF_TC_WITHOUT_HEAD(cam_strmatch);
+ATF_TC_BODY(cam_strmatch, tc)
+{
+ /* Basic fixed patterns */
+ ATF_CHECK_EQ(0, cam_strmatch("foo", "foo", 3));
+ ATF_CHECK_NE(0, cam_strmatch("foo", "bar", 3));
+ ATF_CHECK_NE(0, cam_strmatch("foo", "foobar", 3));
+
+ /* The str is not necessarily null-terminated */
+ ATF_CHECK_EQ(0, cam_strmatch("fooxuehfxeuf", "foo", 3));
+ ATF_CHECK_NE(0, cam_strmatch("foo\0bar", "foo", 7));
+
+ /* Eat trailing spaces, which get added by SAT */
+ ATF_CHECK_EQ(0, cam_strmatch("foo ", "foo", 16));
+
+ /* '*' matches everything, like shell globbing */
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo*", 6));
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "*bar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foobar", "foo*x", 6));
+ ATF_CHECK_EQ(0, cam_strmatch("foobarbaz", "*bar*", 9));
+ /* Even NUL */
+ ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo*", 7));
+ /* Or nothing */
+ ATF_CHECK_EQ(0, cam_strmatch("foo", "foo*", 3));
+ /* But stuff after the * still must match */
+ ATF_CHECK_NE(0, cam_strmatch("foo", "foo*x", 3));
+
+ /* '?' matches exactly one single character */
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo?ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foo", "foo?", 3));
+ /* Even NUL */
+ ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo?bar", 7));
+
+ /* '[]' contains a set of characters */
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[abc]ar", 6));
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[b]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[ac]ar", 6));
+
+ /* '[]' can contain a range of characters, too */
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[a-c]ar", 6));
+ ATF_CHECK_EQ(0, cam_strmatch("fooxar", "foo[a-cx]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foodar", "foo[a-c]ar", 6));
+
+ /* Back-to-back '[]' character sets */
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "fo[a-z][abc]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foAbar", "fo[a-z][abc]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foodar", "fo[a-z][abc]ar", 6));
+
+ /* A '^' negates a set of characters */
+ ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^abc]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^b]ar", 6));
+ ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[^ac]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^a-c]ar", 6));
+ ATF_CHECK_NE(0, cam_strmatch("fooxar", "foo[^a-cx]ar", 6));
+ ATF_CHECK_EQ(0, cam_strmatch("foodar", "foo[^a-c]ar", 6));
+
+ /* Outside of '[]' a ']' is just an ordinary character */
+ ATF_CHECK_EQ(0, cam_strmatch("f]o", "f]o", 3));
+ ATF_CHECK_NE(0, cam_strmatch("foo", "f]o", 3));
+
+ /* Matching a literal '[' requires specifying a range */
+ ATF_CHECK_EQ(0, cam_strmatch("f[o", "f[[]o", 3));
+ ATF_CHECK_NE(0, cam_strmatch("foo", "f[[]o", 3));
+}
+
+ATF_TP_ADD_TCS(tp)
+{
+ ATF_TP_ADD_TC(tp, cam_strmatch);
+
+ return (atf_no_error());
+}
diff --git a/lib/libcam/tests/libcam_test.c b/lib/libcam/tests/libcam_test.c
index a31bdb7..34eb529 100644
--- a/lib/libcam/tests/libcam_test.c
+++ b/lib/libcam/tests/libcam_test.c
@@ -24,6 +24,8 @@
* SUCH DAMAGE.
*/
+/* Tests functions in lib/libcam/camlib.c */
+
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
OpenPOWER on IntegriCloud