xref: /freebsd/lib/libcam/tests/cam_test.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
1 /*-
2  * Copyright (c) 2017 Spectra Logic Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /* Tests functions in sys/cam/cam.c */
28 
29 #include <sys/cdefs.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <camlib.h>
34 
35 #include <atf-c.h>
36 
37 #define ATF_CHECK_NE(x, y) ATF_CHECK((x) != (y))
38 
39 ATF_TC_WITHOUT_HEAD(cam_strmatch);
40 ATF_TC_BODY(cam_strmatch, tc)
41 {
42 	/* Basic fixed patterns */
43 	ATF_CHECK_EQ(0, cam_strmatch("foo", "foo", 3));
44 	ATF_CHECK_NE(0, cam_strmatch("foo", "bar", 3));
45 	ATF_CHECK_NE(0, cam_strmatch("foo", "foobar", 3));
46 
47 	/* The str is not necessarily null-terminated */
48 	ATF_CHECK_EQ(0, cam_strmatch("fooxuehfxeuf", "foo", 3));
49 	ATF_CHECK_NE(0, cam_strmatch("foo\0bar", "foo", 7));
50 
51 	/* Eat trailing spaces, which get added by SAT */
52 	ATF_CHECK_EQ(0, cam_strmatch("foo             ", "foo", 16));
53 
54 	/* '*' matches everything, like shell globbing */
55 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo*", 6));
56 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "*bar", 6));
57 	ATF_CHECK_NE(0, cam_strmatch("foobar", "foo*x", 6));
58 	ATF_CHECK_EQ(0, cam_strmatch("foobarbaz", "*bar*", 9));
59 	/* Even NUL */
60 	ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo*", 7));
61 	/* Or nothing */
62 	ATF_CHECK_EQ(0, cam_strmatch("foo", "foo*", 3));
63 	/* But stuff after the * still must match */
64 	ATF_CHECK_NE(0, cam_strmatch("foo", "foo*x", 3));
65 
66 	/* '?' matches exactly one single character */
67 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo?ar", 6));
68 	ATF_CHECK_NE(0, cam_strmatch("foo", "foo?", 3));
69 	/* Even NUL */
70 	ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo?bar", 7));
71 
72 	/* '[]' contains a set of characters */
73 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[abc]ar", 6));
74 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[b]ar", 6));
75 	ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[ac]ar", 6));
76 
77 	/* '[]' can contain a range of characters, too */
78 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[a-c]ar", 6));
79 	ATF_CHECK_EQ(0, cam_strmatch("fooxar", "foo[a-cx]ar", 6));
80 	ATF_CHECK_NE(0, cam_strmatch("foodar", "foo[a-c]ar", 6));
81 
82 	/* Back-to-back '[]' character sets */
83 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "fo[a-z][abc]ar", 6));
84 	ATF_CHECK_NE(0, cam_strmatch("foAbar", "fo[a-z][abc]ar", 6));
85 	ATF_CHECK_NE(0, cam_strmatch("foodar", "fo[a-z][abc]ar", 6));
86 
87 	/* A '^' negates a set of characters */
88 	ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^abc]ar", 6));
89 	ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^b]ar", 6));
90 	ATF_CHECK_EQ(0, cam_strmatch("foobar", "foo[^ac]ar", 6));
91 	ATF_CHECK_NE(0, cam_strmatch("foobar", "foo[^a-c]ar", 6));
92 	ATF_CHECK_NE(0, cam_strmatch("fooxar", "foo[^a-cx]ar", 6));
93 	ATF_CHECK_EQ(0, cam_strmatch("foodar", "foo[^a-c]ar", 6));
94 
95 	/* Outside of '[]' a ']' is just an ordinary character */
96 	ATF_CHECK_EQ(0, cam_strmatch("f]o", "f]o", 3));
97 	ATF_CHECK_NE(0, cam_strmatch("foo", "f]o", 3));
98 
99 	/* Matching a literal '[' requires specifying a range */
100 	ATF_CHECK_EQ(0, cam_strmatch("f[o", "f[[]o", 3));
101 	ATF_CHECK_NE(0, cam_strmatch("foo", "f[[]o", 3));
102 }
103 
104 ATF_TP_ADD_TCS(tp)
105 {
106 	ATF_TP_ADD_TC(tp, cam_strmatch);
107 
108 	return (atf_no_error());
109 }
110