2 * Copyright (c) 2017 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 /* Tests functions in sys/cam/cam.c */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
39 #define ATF_CHECK_NE(x, y) ATF_CHECK((x) != (y))
41 ATF_TC_WITHOUT_HEAD(cam_strmatch);
42 ATF_TC_BODY(cam_strmatch, tc)
44 /* Basic fixed patterns */
45 ATF_CHECK_EQ(0, cam_strmatch("foo", "foo", 3));
46 ATF_CHECK_NE(0, cam_strmatch("foo", "bar", 3));
47 ATF_CHECK_NE(0, cam_strmatch("foo", "foobar", 3));
49 /* The str is not necessarily null-terminated */
50 ATF_CHECK_EQ(0, cam_strmatch("fooxuehfxeuf", "foo", 3));
51 ATF_CHECK_NE(0, cam_strmatch("foo\0bar", "foo", 7));
53 /* Eat trailing spaces, which get added by SAT */
54 ATF_CHECK_EQ(0, cam_strmatch("foo ", "foo", 16));
56 /* '*' matches everything, like shell globbing */
57 ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo*", 6));
58 ATF_CHECK_EQ(0, cam_strmatch("foobar", "*bar", 6));
59 ATF_CHECK_NE(0, cam_strmatch("foobar", "foo*x", 6));
60 ATF_CHECK_EQ(0, cam_strmatch("foobarbaz", "*bar*", 9));
62 ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo*", 7));
64 ATF_CHECK_EQ(0, cam_strmatch("foo", "foo*", 3));
65 /* But stuff after the * still must match */
66 ATF_CHECK_NE(0, cam_strmatch("foo", "foo*x", 3));
68 /* '?' matches exactly one single character */
69 ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo?ar", 6));
70 ATF_CHECK_NE(0, cam_strmatch("foo", "foo?", 3));
72 ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo?bar", 7));
74 /* '[]' contains a set of characters */
75 ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[abc]ar", 6));
76 ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[b]ar", 6));
77 ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[ac]ar", 6));
79 /* '[]' can contain a range of characters, too */
80 ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[a-c]ar", 6));
81 ATF_CHECK_EQ(0, cam_strmatch("fooxar", "foo[a-cx]ar", 6));
82 ATF_CHECK_NE(0, cam_strmatch("foodar", "foo[a-c]ar", 6));
84 /* Back-to-back '[]' character sets */
85 ATF_CHECK_EQ(0, cam_strmatch("foobar", "fo[a-z][abc]ar", 6));
86 ATF_CHECK_NE(0, cam_strmatch("foAbar", "fo[a-z][abc]ar", 6));
87 ATF_CHECK_NE(0, cam_strmatch("foodar", "fo[a-z][abc]ar", 6));
89 /* A '^' negates a set of characters */
90 ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^abc]ar", 6));
91 ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^b]ar", 6));
92 ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[^ac]ar", 6));
93 ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^a-c]ar", 6));
94 ATF_CHECK_NE(0, cam_strmatch("fooxar", "foo[^a-cx]ar", 6));
95 ATF_CHECK_EQ(0, cam_strmatch("foodar", "foo[^a-c]ar", 6));
97 /* Outside of '[]' a ']' is just an ordinary character */
98 ATF_CHECK_EQ(0, cam_strmatch("f]o", "f]o", 3));
99 ATF_CHECK_NE(0, cam_strmatch("foo", "f]o", 3));
101 /* Matching a literal '[' requires specifying a range */
102 ATF_CHECK_EQ(0, cam_strmatch("f[o", "f[[]o", 3));
103 ATF_CHECK_NE(0, cam_strmatch("foo", "f[[]o", 3));
108 ATF_TP_ADD_TC(tp, cam_strmatch);
110 return (atf_no_error());