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