From 9b637112e7112180e3ddb6129a62b5e21953b469 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Wed, 16 Oct 2013 10:51:44 -0400 Subject: [PATCH] List regex alternates in reverse order to avoid matching single digits before the entire octet. Add a test for the bug fixed by the code change. Update existing tests and documentation for the new output. --- doc/man1/hath.1 | 14 +- hath.cabal | 10 +- src/Main.hs | 11 +- test/shell/manpage-barriers.test | 6 +- test/shell/manpage-regexed.test | 2 +- test/shell/match-class-c.test | 518 +++++++++++++++++++++++++++++++ 6 files changed, 543 insertions(+), 18 deletions(-) create mode 100644 test/shell/match-class-c.test diff --git a/doc/man1/hath.1 b/doc/man1/hath.1 index 3b24558..207e870 100644 --- a/doc/man1/hath.1 +++ b/doc/man1/hath.1 @@ -41,7 +41,7 @@ the input CIDR blocks. It's the default mode of operation. .P .nf .I $ hath <<< \(dq10.0.0.0/29 10.0.0.8/29\(dq -((10)\.(0)\.(0)\.(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15)) +((10)\.(0)\.(0)\.(15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0)) .fi .IP \(bu 2 \fBReduced\fR @@ -84,7 +84,7 @@ notation. List the IP addresses contained within the given CIDRs. .P .nf -.I $ hath listed <<< \(dq192.168.0.240/29\(dq +.I $ hath listed <<< 192.168.0.240/29 192.168.0.240 192.168.0.241 192.168.0.242 @@ -101,7 +101,7 @@ Perform reverse DNS (PTR) lookups on the IP addresses contained within the given CIDRs. .P .nf -.I $ hath reversed <<< \(dq198.41.0.4/30\(dq +.I $ hath reversed <<< 198.41.0.4/30 198.41.0.4: a.root-servers.net. 198.41.0.5: 198.41.0.6: rs.internic.net. @@ -114,7 +114,7 @@ runtime on the command line; for example, the following will perform 25 lookups in parallel: .P .nf -.I $ hath reversed +RTS -N25 <<< \(dq198.41.0.4/24\(dq +.I $ hath reversed +RTS -N25 <<< 198.41.0.4/24 198.41.0.4: a.root-servers.net. 198.41.0.5: 198.41.0.6: rs.internic.net. @@ -133,7 +133,7 @@ address, and this messes up e.g. \fIgrep -o\fR. Without \fB\-\-barriers\fR, you can match things you shouldn't: .nf -.I $ echo \(dq127.0.0.100\(dq | grep -P $(hath <<< \(dq127.0.0.1/32\(dq) +.I $ echo 127.0.0.100 | grep -P $(hath <<< 127.0.0.1/32) 127.0.0.100 .fi @@ -141,7 +141,7 @@ Without \fB\-\-barriers\fR, you can match things you shouldn't: Using \fB\-\-barriers\fR can prevent this: .nf -.I $ echo \(dq127.0.0.100\(dq | grep -P $(hath -b <<< \(dq127.0.0.1/32\(dq) +.I $ echo 127.0.0.100 | grep -P $(hath -b <<< 127.0.0.1/32) .I $ echo $? 1 .fi @@ -151,6 +151,6 @@ But, this may also cause the regex to match something that isn't an IP address: .nf -.I $ echo \(dqx127.0.0.1x\(dq | grep -Po $(hath -b <<< \(dq127.0.0.1/32\(dq) +.I $ echo x127.0.0.1x | grep -Po $(hath -b <<< 127.0.0.1/32) x127.0.0.1x .fi diff --git a/hath.cabal b/hath.cabal index 3d30395..1bdc6d6 100644 --- a/hath.cabal +++ b/hath.cabal @@ -1,5 +1,5 @@ name: hath -version: 0.1.1 +version: 0.1.2 cabal-version: >= 1.8 author: Michael Orlitzky maintainer: Michael Orlitzky @@ -64,7 +64,7 @@ description: . @ $ hath <<< \"10.0.0.0\/29 10.0.0.8\/29\" - ((10)\.(0)\.(0)\.(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15)) + ((10)\.(0)\.(0)\.(15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0)) @ . Combine two \/24s into a \/23: @@ -77,7 +77,7 @@ description: List the addresses in 192.168.0.240\/29: . @ - $ hath listed <<< \"192.168.0.240\/29\" + $ hath listed <<< 192.168.0.240\/29 192.168.0.240 192.168.0.241 192.168.0.242 @@ -91,7 +91,7 @@ description: Perform PTR lookups on all of 198.41.0.4\/30: . @ - $ hath reversed <<< \"198.41.0.4\/30\" + $ hath reversed <<< 198.41.0.4\/30 198.41.0.4: a.root-servers.net. 198.41.0.5: 198.41.0.6: rs.internic.net. @@ -104,7 +104,7 @@ description: perform 25 lookups in parallel: . @ - $ hath reversed +RTS -N25 <<< \"198.41.0.4\/24\" + $ hath reversed +RTS -N25 <<< 198.41.0.4\/24 198.41.0.4: a.root-servers.net. 198.41.0.5: 198.41.0.6: rs.internic.net. diff --git a/src/Main.hs b/src/Main.hs index 45705be..deb9022 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -49,7 +49,8 @@ add_barriers x = non_addr_char ++ x ++ non_addr_char -- 3. Generate a regex matching every value between those min and -- max values. -- 4. Join the regexes from step 3 with regexes matching periods. --- 5. Stick an address boundary on either side of the result. +-- 5. Stick an address boundary on either side of the result if +-- use_barriers is True. -- cidr_to_regex :: Bool -> Cidr.Cidr -> String cidr_to_regex use_barriers cidr = @@ -79,9 +80,15 @@ alternate terms = "(" ++ (intercalate "|" terms) ++ ")" -- | Take two Ints as parameters, and return a regex matching any -- integer between them (inclusive). +-- +-- IMPORTANT: we match from max to min so that if e.g. the last +-- octet is '255', we want '255' to match before '2' in the regex +-- (255|254|...|3|2|1) which does not happen if we use +-- (1|2|3|...|254|255). +-- numeric_range :: Int -> Int -> String numeric_range x y = - alternate (map show [lower..upper]) + alternate (map show $ reverse [lower..upper]) where lower = minimum [x,y] upper = maximum [x,y] diff --git a/test/shell/manpage-barriers.test b/test/shell/manpage-barriers.test index 4b8c165..d50564b 100644 --- a/test/shell/manpage-barriers.test +++ b/test/shell/manpage-barriers.test @@ -1,18 +1,18 @@ # Test the --barriers examples from the manpage. # The first one matches a line it probably shouldn't. -echo "127.0.0.100" | grep -P $(dist/build/hath/hath <<< "127.0.0.1/32") +grep -P --color=never $(dist/build/hath/hath <<< 127.0.0.1/32) <<< 127.0.0.100 >>> 127.0.0.100 >>>= 0 # This one uses --barriers, and doesn't match that same line. -echo "127.0.0.100" | grep -P $(dist/build/hath/hath -b <<< "127.0.0.1/32") +grep -P $(dist/build/hath/hath -b <<< 127.0.0.1/32) <<< 127.0.0.100 >>> >>>= 1 # But, using barriers makes the regexp match something it shouldn't. -echo "x127.0.0.1x" | grep -Po $(dist/build/hath/hath -b <<< "127.0.0.1/32") +grep -Po --color=never $(dist/build/hath/hath -b <<< 127.0.0.1/32) <<< x127.0.0.1x >>> x127.0.0.1x >>>= 0 diff --git a/test/shell/manpage-regexed.test b/test/shell/manpage-regexed.test index 9b09f11..4961eac 100644 --- a/test/shell/manpage-regexed.test +++ b/test/shell/manpage-regexed.test @@ -1,5 +1,5 @@ # Test the regexed example from the man page. dist/build/hath/hath <<< "10.0.0.0/29 10.0.0.8/29" >>> -((10)\.(0)\.(0)\.(0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15)) +((10)\.(0)\.(0)\.(15|14|13|12|11|10|9|8|7|6|5|4|3|2|1|0)) >>>= 0 diff --git a/test/shell/match-class-c.test b/test/shell/match-class-c.test new file mode 100644 index 0000000..0e0bef8 --- /dev/null +++ b/test/shell/match-class-c.test @@ -0,0 +1,518 @@ +# We should match every address in this class C. +# (input/output generated with `hath listed`) +grep -Po --color=never $(dist/build/hath/hath <<< "192.168.0.0/24") +<<< +192.168.0.0 +192.168.0.1 +192.168.0.2 +192.168.0.3 +192.168.0.4 +192.168.0.5 +192.168.0.6 +192.168.0.7 +192.168.0.8 +192.168.0.9 +192.168.0.10 +192.168.0.11 +192.168.0.12 +192.168.0.13 +192.168.0.14 +192.168.0.15 +192.168.0.16 +192.168.0.17 +192.168.0.18 +192.168.0.19 +192.168.0.20 +192.168.0.21 +192.168.0.22 +192.168.0.23 +192.168.0.24 +192.168.0.25 +192.168.0.26 +192.168.0.27 +192.168.0.28 +192.168.0.29 +192.168.0.30 +192.168.0.31 +192.168.0.32 +192.168.0.33 +192.168.0.34 +192.168.0.35 +192.168.0.36 +192.168.0.37 +192.168.0.38 +192.168.0.39 +192.168.0.40 +192.168.0.41 +192.168.0.42 +192.168.0.43 +192.168.0.44 +192.168.0.45 +192.168.0.46 +192.168.0.47 +192.168.0.48 +192.168.0.49 +192.168.0.50 +192.168.0.51 +192.168.0.52 +192.168.0.53 +192.168.0.54 +192.168.0.55 +192.168.0.56 +192.168.0.57 +192.168.0.58 +192.168.0.59 +192.168.0.60 +192.168.0.61 +192.168.0.62 +192.168.0.63 +192.168.0.64 +192.168.0.65 +192.168.0.66 +192.168.0.67 +192.168.0.68 +192.168.0.69 +192.168.0.70 +192.168.0.71 +192.168.0.72 +192.168.0.73 +192.168.0.74 +192.168.0.75 +192.168.0.76 +192.168.0.77 +192.168.0.78 +192.168.0.79 +192.168.0.80 +192.168.0.81 +192.168.0.82 +192.168.0.83 +192.168.0.84 +192.168.0.85 +192.168.0.86 +192.168.0.87 +192.168.0.88 +192.168.0.89 +192.168.0.90 +192.168.0.91 +192.168.0.92 +192.168.0.93 +192.168.0.94 +192.168.0.95 +192.168.0.96 +192.168.0.97 +192.168.0.98 +192.168.0.99 +192.168.0.100 +192.168.0.101 +192.168.0.102 +192.168.0.103 +192.168.0.104 +192.168.0.105 +192.168.0.106 +192.168.0.107 +192.168.0.108 +192.168.0.109 +192.168.0.110 +192.168.0.111 +192.168.0.112 +192.168.0.113 +192.168.0.114 +192.168.0.115 +192.168.0.116 +192.168.0.117 +192.168.0.118 +192.168.0.119 +192.168.0.120 +192.168.0.121 +192.168.0.122 +192.168.0.123 +192.168.0.124 +192.168.0.125 +192.168.0.126 +192.168.0.127 +192.168.0.128 +192.168.0.129 +192.168.0.130 +192.168.0.131 +192.168.0.132 +192.168.0.133 +192.168.0.134 +192.168.0.135 +192.168.0.136 +192.168.0.137 +192.168.0.138 +192.168.0.139 +192.168.0.140 +192.168.0.141 +192.168.0.142 +192.168.0.143 +192.168.0.144 +192.168.0.145 +192.168.0.146 +192.168.0.147 +192.168.0.148 +192.168.0.149 +192.168.0.150 +192.168.0.151 +192.168.0.152 +192.168.0.153 +192.168.0.154 +192.168.0.155 +192.168.0.156 +192.168.0.157 +192.168.0.158 +192.168.0.159 +192.168.0.160 +192.168.0.161 +192.168.0.162 +192.168.0.163 +192.168.0.164 +192.168.0.165 +192.168.0.166 +192.168.0.167 +192.168.0.168 +192.168.0.169 +192.168.0.170 +192.168.0.171 +192.168.0.172 +192.168.0.173 +192.168.0.174 +192.168.0.175 +192.168.0.176 +192.168.0.177 +192.168.0.178 +192.168.0.179 +192.168.0.180 +192.168.0.181 +192.168.0.182 +192.168.0.183 +192.168.0.184 +192.168.0.185 +192.168.0.186 +192.168.0.187 +192.168.0.188 +192.168.0.189 +192.168.0.190 +192.168.0.191 +192.168.0.192 +192.168.0.193 +192.168.0.194 +192.168.0.195 +192.168.0.196 +192.168.0.197 +192.168.0.198 +192.168.0.199 +192.168.0.200 +192.168.0.201 +192.168.0.202 +192.168.0.203 +192.168.0.204 +192.168.0.205 +192.168.0.206 +192.168.0.207 +192.168.0.208 +192.168.0.209 +192.168.0.210 +192.168.0.211 +192.168.0.212 +192.168.0.213 +192.168.0.214 +192.168.0.215 +192.168.0.216 +192.168.0.217 +192.168.0.218 +192.168.0.219 +192.168.0.220 +192.168.0.221 +192.168.0.222 +192.168.0.223 +192.168.0.224 +192.168.0.225 +192.168.0.226 +192.168.0.227 +192.168.0.228 +192.168.0.229 +192.168.0.230 +192.168.0.231 +192.168.0.232 +192.168.0.233 +192.168.0.234 +192.168.0.235 +192.168.0.236 +192.168.0.237 +192.168.0.238 +192.168.0.239 +192.168.0.240 +192.168.0.241 +192.168.0.242 +192.168.0.243 +192.168.0.244 +192.168.0.245 +192.168.0.246 +192.168.0.247 +192.168.0.248 +192.168.0.249 +192.168.0.250 +192.168.0.251 +192.168.0.252 +192.168.0.253 +192.168.0.254 +192.168.0.255 +>>> +192.168.0.0 +192.168.0.1 +192.168.0.2 +192.168.0.3 +192.168.0.4 +192.168.0.5 +192.168.0.6 +192.168.0.7 +192.168.0.8 +192.168.0.9 +192.168.0.10 +192.168.0.11 +192.168.0.12 +192.168.0.13 +192.168.0.14 +192.168.0.15 +192.168.0.16 +192.168.0.17 +192.168.0.18 +192.168.0.19 +192.168.0.20 +192.168.0.21 +192.168.0.22 +192.168.0.23 +192.168.0.24 +192.168.0.25 +192.168.0.26 +192.168.0.27 +192.168.0.28 +192.168.0.29 +192.168.0.30 +192.168.0.31 +192.168.0.32 +192.168.0.33 +192.168.0.34 +192.168.0.35 +192.168.0.36 +192.168.0.37 +192.168.0.38 +192.168.0.39 +192.168.0.40 +192.168.0.41 +192.168.0.42 +192.168.0.43 +192.168.0.44 +192.168.0.45 +192.168.0.46 +192.168.0.47 +192.168.0.48 +192.168.0.49 +192.168.0.50 +192.168.0.51 +192.168.0.52 +192.168.0.53 +192.168.0.54 +192.168.0.55 +192.168.0.56 +192.168.0.57 +192.168.0.58 +192.168.0.59 +192.168.0.60 +192.168.0.61 +192.168.0.62 +192.168.0.63 +192.168.0.64 +192.168.0.65 +192.168.0.66 +192.168.0.67 +192.168.0.68 +192.168.0.69 +192.168.0.70 +192.168.0.71 +192.168.0.72 +192.168.0.73 +192.168.0.74 +192.168.0.75 +192.168.0.76 +192.168.0.77 +192.168.0.78 +192.168.0.79 +192.168.0.80 +192.168.0.81 +192.168.0.82 +192.168.0.83 +192.168.0.84 +192.168.0.85 +192.168.0.86 +192.168.0.87 +192.168.0.88 +192.168.0.89 +192.168.0.90 +192.168.0.91 +192.168.0.92 +192.168.0.93 +192.168.0.94 +192.168.0.95 +192.168.0.96 +192.168.0.97 +192.168.0.98 +192.168.0.99 +192.168.0.100 +192.168.0.101 +192.168.0.102 +192.168.0.103 +192.168.0.104 +192.168.0.105 +192.168.0.106 +192.168.0.107 +192.168.0.108 +192.168.0.109 +192.168.0.110 +192.168.0.111 +192.168.0.112 +192.168.0.113 +192.168.0.114 +192.168.0.115 +192.168.0.116 +192.168.0.117 +192.168.0.118 +192.168.0.119 +192.168.0.120 +192.168.0.121 +192.168.0.122 +192.168.0.123 +192.168.0.124 +192.168.0.125 +192.168.0.126 +192.168.0.127 +192.168.0.128 +192.168.0.129 +192.168.0.130 +192.168.0.131 +192.168.0.132 +192.168.0.133 +192.168.0.134 +192.168.0.135 +192.168.0.136 +192.168.0.137 +192.168.0.138 +192.168.0.139 +192.168.0.140 +192.168.0.141 +192.168.0.142 +192.168.0.143 +192.168.0.144 +192.168.0.145 +192.168.0.146 +192.168.0.147 +192.168.0.148 +192.168.0.149 +192.168.0.150 +192.168.0.151 +192.168.0.152 +192.168.0.153 +192.168.0.154 +192.168.0.155 +192.168.0.156 +192.168.0.157 +192.168.0.158 +192.168.0.159 +192.168.0.160 +192.168.0.161 +192.168.0.162 +192.168.0.163 +192.168.0.164 +192.168.0.165 +192.168.0.166 +192.168.0.167 +192.168.0.168 +192.168.0.169 +192.168.0.170 +192.168.0.171 +192.168.0.172 +192.168.0.173 +192.168.0.174 +192.168.0.175 +192.168.0.176 +192.168.0.177 +192.168.0.178 +192.168.0.179 +192.168.0.180 +192.168.0.181 +192.168.0.182 +192.168.0.183 +192.168.0.184 +192.168.0.185 +192.168.0.186 +192.168.0.187 +192.168.0.188 +192.168.0.189 +192.168.0.190 +192.168.0.191 +192.168.0.192 +192.168.0.193 +192.168.0.194 +192.168.0.195 +192.168.0.196 +192.168.0.197 +192.168.0.198 +192.168.0.199 +192.168.0.200 +192.168.0.201 +192.168.0.202 +192.168.0.203 +192.168.0.204 +192.168.0.205 +192.168.0.206 +192.168.0.207 +192.168.0.208 +192.168.0.209 +192.168.0.210 +192.168.0.211 +192.168.0.212 +192.168.0.213 +192.168.0.214 +192.168.0.215 +192.168.0.216 +192.168.0.217 +192.168.0.218 +192.168.0.219 +192.168.0.220 +192.168.0.221 +192.168.0.222 +192.168.0.223 +192.168.0.224 +192.168.0.225 +192.168.0.226 +192.168.0.227 +192.168.0.228 +192.168.0.229 +192.168.0.230 +192.168.0.231 +192.168.0.232 +192.168.0.233 +192.168.0.234 +192.168.0.235 +192.168.0.236 +192.168.0.237 +192.168.0.238 +192.168.0.239 +192.168.0.240 +192.168.0.241 +192.168.0.242 +192.168.0.243 +192.168.0.244 +192.168.0.245 +192.168.0.246 +192.168.0.247 +192.168.0.248 +192.168.0.249 +192.168.0.250 +192.168.0.251 +192.168.0.252 +192.168.0.253 +192.168.0.254 +192.168.0.255 +>>>= 0 -- 2.44.2