xref: /freebsd/lib/libcam/tests/cam_test.c (revision a0b9e2e854027e6ff61fb075a1309dbc71c42b54)
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 __FBSDID("$FreeBSD$");
31 
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <camlib.h>
36 
37 #include <atf-c.h>
38 
39 #define ATF_CHECK_NE(x, y) ATF_CHECK((x) != (y))
40 
41 ATF_TC_WITHOUT_HEAD(cam_strmatch);
42 ATF_TC_BODY(cam_strmatch, tc)
43 {
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));
48 
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));
52 
53 	/* Eat trailing spaces, which get added by SAT */
54 	ATF_CHECK_EQ(0, cam_strmatch("foo             ", "foo", 16));
55 
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));
61 	/* Even NUL */
62 	ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo*", 7));
63 	/* Or nothing */
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));
67 
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));
71 	/* Even NUL */
72 	ATF_CHECK_EQ(0, cam_strmatch("foo\0bar", "foo?bar", 7));
73 
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));
78 
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));
83 
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));
88 
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));
96 
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));
100 
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));
104 }
105 
106 ATF_TP_ADD_TCS(tp)
107 {
108 	ATF_TP_ADD_TC(tp, cam_strmatch);
109 
110 	return (atf_no_error());
111 }
112