ip 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Rakefile +74 -0
  2. data/lib/ip.rb +2 -0
  3. data/test/test_ip.rb +298 -0
  4. metadata +12 -10
  5. data/test/ip.rb +0 -603
@@ -0,0 +1,74 @@
1
+ #
2
+ # Please see the COPYING file in the source distribution for copyright information.
3
+ #
4
+
5
+ begin
6
+ require 'rubygems'
7
+ gem 'test-unit'
8
+ rescue LoadError
9
+ end
10
+
11
+ $:.unshift 'lib'
12
+ require 'rake/testtask'
13
+ require 'rake/packagetask'
14
+ require 'rake/gempackagetask'
15
+ require 'rake/rdoctask'
16
+ require 'ip'
17
+
18
+ task :default => [ :dist ]
19
+
20
+ #
21
+ # Tests
22
+ #
23
+
24
+ Rake::TestTask.new do |t|
25
+ t.libs << 'lib'
26
+ t.test_files = FileList['test/test*.rb']
27
+ t.verbose = true
28
+ end
29
+
30
+ #
31
+ # Distribution
32
+ #
33
+
34
+ task :dist => [:test, :repackage, :gem, :rdoc]
35
+ task :distclean => [:clobber_package, :clobber_rdoc]
36
+ task :clean => [:distclean]
37
+
38
+ #
39
+ # Documentation
40
+ #
41
+
42
+ Rake::RDocTask.new do |rd|
43
+ rd.rdoc_dir = "rdoc"
44
+ rd.main = "IP"
45
+ rd.rdoc_files.include("./lib/**/*.rb")
46
+ rd.options = %w(-ap)
47
+ end
48
+
49
+ #
50
+ # Packaging
51
+ #
52
+
53
+ spec = Gem::Specification.new do |s|
54
+ s.name = "ip"
55
+ s.version = IP::VERSION
56
+ s.author = "Erik Hollensbe"
57
+ s.email = "erik@hollensbe.org"
58
+ s.summary = "Ruby classes to work with IP address, ranges, and netmasks"
59
+ s.has_rdoc = true
60
+ s.files = Dir['Rakefile'] + Dir['examples/*.rb'] + Dir['lib/*.rb'] + Dir['test/*.rb'] + Dir['lib/ip/*.rb']
61
+ s.rubyforge_project = 'ip-address'
62
+ end
63
+
64
+ Rake::GemPackageTask.new(spec) do |s|
65
+ end
66
+
67
+ Rake::PackageTask.new(spec.name, spec.version) do |p|
68
+ p.need_tar_gz = true
69
+ p.need_zip = true
70
+ p.package_files.include("./Rakefile")
71
+ p.package_files.include("./examples/**/*.rb")
72
+ p.package_files.include("./lib/**/*.rb")
73
+ p.package_files.include("./test/**/*")
74
+ end
data/lib/ip.rb CHANGED
@@ -59,6 +59,8 @@
59
59
 
60
60
  class IP
61
61
 
62
+ VERSION = "0.2.2"
63
+
62
64
  #
63
65
  # A IP::AddressException is thrown when an IP address cannot be
64
66
  # parsed.
@@ -0,0 +1,298 @@
1
+ begin
2
+ require 'rubygems'
3
+ gem 'test-unit'
4
+ rescue LoadError => e
5
+ end
6
+
7
+ require 'test/unit'
8
+ load 'lib/ip.rb'
9
+
10
+ class CIDRTest < Test::Unit::TestCase
11
+
12
+ def name
13
+ return "IP::CIDR tests"
14
+ end
15
+
16
+ def test_init_generic
17
+ assert_raise(IP::AddressException) { IP::CIDR.new(Hash.new) }
18
+ assert_raise(IP::AddressException) { IP::CIDR.new("foomatic_wootmaster/32") }
19
+ end
20
+
21
+ def test_init_ipv6
22
+ assert_raise(IP::AddressException) { IP::CIDR.new("F00F:DEAD:BEEF::") }
23
+ assert_raise(IP::AddressException) { IP::CIDR.new("F00F:DEAD:BEEF::/") }
24
+ assert_raise(IP::AddressException) { IP::CIDR.new("F00F:DEAD:BEEF::/asdf/32") }
25
+ assert_raise(IP::AddressException) { IP::CIDR.new("F00F:DEAD:BEEF::/foomatic_wootmaster") }
26
+ assert_raise(IP::AddressException) { IP::CIDR.new("F00F:DEAD:BEEF::/255.255.255.255") }
27
+
28
+ cidr = nil
29
+ assert_nothing_raised() { cidr = IP::CIDR.new("F00F:DEAD:BEEF::0001/128") }
30
+
31
+ # this sort of indirectly tests the ipv6 address manipulation.
32
+ assert_equal("F00F:DEAD:BEEF::1", cidr.ip.short_address, "ipv6 object constructed clean")
33
+ assert_equal(128, cidr.mask, "mask is set properly")
34
+ assert_equal("F00F:DEAD:BEEF::0001/128", cidr.cidr, "original CIDR preserved")
35
+ end
36
+
37
+ def test_init_ipv4
38
+ assert_raise(IP::AddressException) { IP::CIDR.new("10.0.0.1") }
39
+ assert_raise(IP::AddressException) { IP::CIDR.new("10.0.0.1/") }
40
+ assert_raise(IP::AddressException) { IP::CIDR.new("10.0.0.1/asdf/32") }
41
+ assert_raise(IP::AddressException) { IP::CIDR.new("10.0.0.1/foomatic_wootmaster") }
42
+ assert_raise(IP::AddressException) { IP::CIDR.new("10.0.0.1/255") }
43
+
44
+ cidr = nil
45
+
46
+ assert_nothing_raised() { cidr = IP::CIDR.new("10.0.0.1/32") }
47
+
48
+ assert_equal("10.0.0.1", cidr.ip.ip_address, "ipv4 data integrity test #1")
49
+ assert_equal(32, cidr.mask, "ipv4 data integrity test #2")
50
+ assert_equal("10.0.0.1/32", cidr.cidr, "ipv4 data integrity test #3")
51
+
52
+ assert_nothing_raised() { cidr = IP::CIDR.new("10.0.0.1/255.255.255.255") }
53
+
54
+ assert_equal("10.0.0.1", cidr.ip.ip_address, "ipv4 data integrity test #4")
55
+ assert_equal(32, cidr.mask, "ipv4 data integrity test #5")
56
+ assert_equal("10.0.0.1/255.255.255.255", cidr.cidr, "ipv4 data integrity test #6")
57
+ end
58
+
59
+ def test_netmasks
60
+ cidr = nil
61
+ assert_nothing_raised() { cidr = IP::CIDR.new("10.0.0.1/32") }
62
+ assert_equal(32, cidr.short_netmask, "ipv4 netmask test #1")
63
+ assert_equal("255.255.255.255", cidr.long_netmask.ip_address, "ipv4 netmask test #2")
64
+
65
+ assert_nothing_raised() { cidr = IP::CIDR.new("10.0.0.1/255.255.255.248") }
66
+ assert_equal(29, cidr.short_netmask, "ipv4 netmask test #3")
67
+ assert_equal("255.255.255.248", cidr.long_netmask.ip_address, "ipv4 netmask test #4")
68
+
69
+ assert_nothing_raised() { cidr = IP::CIDR.new("F00F:DEAD::/16") }
70
+ assert_equal(16, cidr.short_netmask, "ipv6 has proper short netmask")
71
+ assert_raise(IP::AddressException) { cidr.long_netmask }
72
+ end
73
+
74
+ def test_first_last
75
+ cidr = nil
76
+ assert_nothing_raised() { cidr = IP::CIDR.new("10.0.0.2/24") }
77
+ assert_equal("10.0.0.0", cidr.first_ip.ip_address, "ipv4 first/last test #1")
78
+ assert_equal("10.0.0.255", cidr.last_ip.ip_address, "ipv4 first/last test #2")
79
+ end
80
+
81
+ def test_range
82
+ cidr = nil
83
+ assert_nothing_raised() { cidr = IP::CIDR.new("10.0.0.2/24") }
84
+ assert_equal(1, cidr.range.find_all { |x| x.ip_address == "10.0.0.1" }.length, "ipv4 range test #1")
85
+ assert_equal(0, cidr.range.find_all { |x| x.ip_address == "10.0.1.0" }.length, "ipv4 range test #2")
86
+
87
+ assert_nothing_raised() { cidr = IP::CIDR.new("::0001/120") }
88
+ assert_equal(1, cidr.range.find_all { |x| x.ip_address == "0:0:0:0:0:0:0:00FF" }.length, "ipv6 range test (included)")
89
+ assert_equal(0, cidr.range.find_all { |x| x.ip_address ==" 0:0:0:0:0:0:0:0F00" }.length, "ipv6 range test (not included)")
90
+ end
91
+
92
+ def test_overlaps
93
+ cidr, cidr2 = [nil, nil]
94
+
95
+ assert_nothing_raised() do
96
+ cidr = IP::CIDR.new("10.0.0.2/24")
97
+ cidr2 = IP::CIDR.new("10.0.0.1/29")
98
+ end
99
+
100
+ assert(cidr.overlaps?(cidr2), "ipv4 overlaps test #1")
101
+
102
+ assert_nothing_raised() { cidr2 = IP::CIDR.new("10.0.0.1/16") }
103
+
104
+ assert(cidr2.overlaps?(cidr), "ipv4 overlaps test #2")
105
+ assert(cidr.overlaps?(cidr2), "ipv4 overlaps test #3")
106
+
107
+ assert_nothing_raised() do
108
+ cidr = IP::CIDR.new("F00F:DEAD::/16")
109
+ cidr2 = IP::CIDR.new("F00F:BEEF::/16")
110
+ end
111
+
112
+ assert(cidr.overlaps?(cidr2), "ipv6 #overlaps? reports correctly #1")
113
+ assert(cidr2.overlaps?(cidr), "ipv6 #overlaps? reports correctly #2")
114
+ end
115
+
116
+ def test_includes
117
+ cidr, ip = [nil, nil]
118
+ assert_nothing_raised() do
119
+ cidr = IP::CIDR.new("10.0.0.2/24")
120
+ ip = IP::Address::IPv4.new("10.0.0.1")
121
+ end
122
+
123
+ assert(cidr.includes?(ip), "ipv4 #includes? reports correctly (included)")
124
+
125
+ assert_nothing_raised() { ip = IP::Address::IPv4.new("10.0.1.0") }
126
+
127
+ assert(!cidr.includes?(ip), "ipv4 #includes? reports correctly (not included)")
128
+
129
+ assert_nothing_raised() do
130
+ cidr = IP::CIDR.new("FF00::/16")
131
+ ip = IP::Address::IPv6.new("FF00::DEAD")
132
+ end
133
+ assert(cidr.includes?(ip), "ipv6 #includes? reports correctly (included)")
134
+
135
+ assert_nothing_raised() { ip = IP::Address::IPv6.new("F000::DEAD") }
136
+
137
+ assert(!cidr.includes?(ip), "ipv6 #includes? reports correctly (not included)")
138
+ end
139
+
140
+ end
141
+
142
+ class RangeTest < Test::Unit::TestCase
143
+ def name
144
+ return "IP::Range tests"
145
+ end
146
+
147
+ def test_range_generic
148
+ assert_raise(IP::AddressException) { IP::Range[Hash.new, ""] }
149
+ assert_raise(IP::AddressException) { IP::Range["", Hash.new] }
150
+ assert_raise(IP::AddressException) { IP::Range[IP::Address::IPv6.new("F00F::"), IP::Address::IPv4.new("10.0.0.1")] }
151
+ assert_raise(IP::AddressException) { IP::Range[IP::Address::IPv4.new("10.0.0.1"), IP::Address::IPv6.new("F00F::")] }
152
+ end
153
+
154
+ def test_range_ipv6
155
+ assert_nothing_raised() do
156
+ IP::Range["::0001", "::00F0"]
157
+ IP::Range[IP::Address::IPv6.new("::0001"), IP::Address::IPv6.new("::00F0")]
158
+ end
159
+
160
+ range = nil
161
+
162
+ assert_nothing_raised() { range = IP::Range["::0001", "::0010"] }
163
+
164
+ assert_equal(1, range.find_all { |x| x.short_address == "::1" }.length, "ipv6 range check #1")
165
+ assert_equal(1, range.find_all { |x| x.short_address == "::0010" }.length, "ipv6 range check #2")
166
+ assert_equal(1, range.find_all { |x| x.short_address == "::000A" }.length, "ipv6 range check #3")
167
+ assert_equal(0, range.find_all { |x| x.short_address == "::0011" }.length, "ipv6 range check #4")
168
+ end
169
+
170
+ def test_range_ipv4
171
+ assert_nothing_raised() do
172
+ IP::Range["10.0.0.1", "10.0.0.2"]
173
+ IP::Range[IP::Address::IPv4.new("10.0.0.1"), IP::Address::IPv4.new("10.0.0.2")]
174
+ end
175
+
176
+ range = nil
177
+ assert_nothing_raised() { range = IP::Range["10.0.0.1", "10.0.0.10"] }
178
+
179
+ assert_equal(1, range.find_all { |x| x.ip_address == "10.0.0.1" }.length, "ipv4 range check #1")
180
+ assert_equal(1, range.find_all { |x| x.ip_address == "10.0.0.10" }.length, "ipv4 range check #2")
181
+ assert_equal(1, range.find_all { |x| x.ip_address == "10.0.0.7" }.length, "ipv4 range check #3")
182
+ assert_equal(0, range.find_all { |x| x.ip_address == "10.0.0.11" }.length, "ipv4 range check #4")
183
+ end
184
+
185
+ end
186
+
187
+ class IPv6AddressTest < Test::Unit::TestCase
188
+ def name
189
+ return "IP::Address::IPv6 tests"
190
+ end
191
+
192
+ def test_init
193
+ assert_nothing_raised() do
194
+ IP::Address::IPv6.new("0000:0000:0000:0000:0000:0000:0000:0001")
195
+ IP::Address::IPv6.new("::0001")
196
+ IP::Address::IPv6.new("FF00::")
197
+ IP::Address::IPv6.new("FF00::0001")
198
+ IP::Address::IPv6.new("FF00:BEEF::0001")
199
+ IP::Address::IPv6.new("FF00::BEEF:0001")
200
+ IP::Address::IPv6.new("::1.2.3.4")
201
+ IP::Address::IPv6.new("FFFF::1.2.3.4")
202
+ IP::Address::IPv6.new("FFFF:0000:0000:0000:0000:0000:1.2.3.4")
203
+ end
204
+
205
+ # now, the tests that should fail
206
+
207
+ assert_raise(IP::AddressException) { IP::Address::IPv6.new("FF00:BEEF:") }
208
+ assert_raise(IP::AddressException) { IP::Address::IPv6.new("FF00::BEEF::") }
209
+ assert_raise(IP::AddressException) { IP::Address::IPv6.new("FF00::BEEF::DEAD") }
210
+ assert_raise(IP::AddressException) { IP::Address::IPv6.new("HF00::0001") }
211
+ assert_raise(IP::AddressException) { IP::Address::IPv6.new("1.2.3.4::0001") }
212
+ end
213
+
214
+ def test_accessors
215
+ ip = nil
216
+ assert_nothing_raised() { ip = IP::Address::IPv6.new("F00F::DEAD:BEEF") }
217
+
218
+ assert_equal(ip[0], ip.octet(0), "ip[0] eq ip.octet(0)")
219
+ assert_equal(61455, ip[0], "ip[0] is correct")
220
+ assert_equal("F00F", ip.octet_as_hex(0), "octet converts to hex properly")
221
+ assert_equal("F00F::DEAD:BEEF", ip.ip_address, '#ip_address preserves original address')
222
+ end
223
+
224
+ def test_address
225
+ ip = nil
226
+ assert_nothing_raised() { ip = IP::Address::IPv6.new("F00F::DEAD:BEEF") }
227
+ assert_equal("F00F::DEAD:BEEF", ip.short_address, 'wildcard left - #short_address returns a compressed version')
228
+ assert_equal("F00F:0:0:0:0:0:DEAD:BEEF", ip.long_address, 'wildcard left - #long_address returns the right thing')
229
+
230
+ assert_nothing_raised() { ip = IP::Address::IPv6.new("F00F:DEAD::BEEF") }
231
+ assert_equal("F00F:DEAD::BEEF", ip.short_address, 'wildcard right - #short_address returns a compressed version')
232
+ assert_equal("F00F:DEAD:0:0:0:0:0:BEEF", ip.long_address, 'wildcard right - #long_address returns the right thing')
233
+
234
+ assert_nothing_raised() { ip = IP::Address::IPv6.new("F00F:DEAD:0:0:0:0:0:BEEF") }
235
+ assert_equal("F00F:DEAD::BEEF", ip.short_address, 'no wildcard - #short_address returns a compressed version')
236
+ assert_equal("F00F:DEAD:0:0:0:0:0:BEEF", ip.long_address, 'no wildcard - #long_address returns the right thing')
237
+
238
+ assert_nothing_raised() { ip = IP::Address::IPv6.new("F00F::DEAD:BEEF:0:0") }
239
+ assert_equal("F00F:0:0:0:DEAD:BEEF::", ip.short_address, '#short_address returns a compressed version with wildcard @ right')
240
+ end
241
+
242
+ end
243
+
244
+
245
+ class IPv4AddressTest < Test::Unit::TestCase
246
+ def name
247
+ return "IP::Address::IPv4 tests"
248
+ end
249
+
250
+ def test_init
251
+ assert_raise(IP::AddressException) { IP::Address::IPv4.new(Hash.new) }
252
+ assert_raise(IP::AddressException) { IP::Address::IPv4.new("asdf") }
253
+ assert_raise(IP::AddressException) { IP::Address::IPv4.new("0.0.0") }
254
+ assert_raise(IP::AddressException) { IP::Address::IPv4.new("256.255.255.255") }
255
+ assert_raise(IP::AddressException) { IP::Address::IPv4.new("255.255.255.255aaaa") }
256
+ assert_raise(IP::AddressException) { IP::Address::IPv4.new("255.255.255.") }
257
+ end
258
+
259
+ def test_accessor
260
+ ip = nil
261
+ assert_nothing_raised() { ip = IP::Address::IPv4.new("10.1.2.3") }
262
+ assert_equal("10.1.2.3", ip.ip_address, "accessor test #1")
263
+ assert_equal(10, ip.octets[0], "accessor test #2")
264
+ assert_equal(3, ip.octets[3], "accessor test #3")
265
+ assert_equal(nil, ip.octets[4], "accessor test #4")
266
+
267
+ assert_equal(ip.octet(1), ip[1], "accessor test #5")
268
+ assert_equal(1, ip[1], "accessor test #5")
269
+
270
+ assert_raise(IP::BoundaryException) { ip[4] }
271
+ end
272
+
273
+ end
274
+
275
+ class UtilTest < Test::Unit::TestCase
276
+ def name
277
+ return "IP::Address::Util tests"
278
+ end
279
+
280
+ def test_pack_unpack
281
+ address = "10.0.0.1"
282
+ assert_equal("10.0.0.1", IP::Address::Util.unpack(IP::Address::Util.pack(IP::Address::IPv4.new(address))).ip_address, "pack/unpack test")
283
+ end
284
+
285
+ def test_short_netmask
286
+ ip = nil
287
+ assert_nothing_raised() { ip = IP::Address::IPv4.new("255.255.255.255") }
288
+ assert_equal(32, IP::Address::Util.short_netmask(ip), "Short Netmask Test #1")
289
+ assert_nothing_raised() { ip = IP::Address::IPv4.new("255.255.255.248") }
290
+ assert_equal(29, IP::Address::Util.short_netmask(ip), "Short Netmask Test #2")
291
+ end
292
+
293
+ def test_long_netmask
294
+ assert_equal("255.255.255.255", IP::Address::Util.long_netmask_ipv4(32).ip_address, "Long Netmask Test #1")
295
+ assert_equal("255.255.255.248", IP::Address::Util.long_netmask_ipv4(29).ip_address, "Long Netmask Test #2")
296
+ end
297
+
298
+ end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: ip
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2006-04-20 00:00:00 -07:00
6
+ version: 0.2.2
7
+ date: 2008-11-02 00:00:00 -07:00
8
8
  summary: Ruby classes to work with IP address, ranges, and netmasks
9
9
  require_paths:
10
10
  - lib
@@ -12,7 +12,7 @@ email: erik@hollensbe.org
12
12
  homepage:
13
13
  rubyforge_project: ip-address
14
14
  description:
15
- autorequire: ip
15
+ autorequire:
16
16
  default_executable:
17
17
  bindir: bin
18
18
  has_rdoc: true
@@ -25,20 +25,22 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
25
25
  platform: ruby
26
26
  signing_key:
27
27
  cert_chain:
28
+ post_install_message:
28
29
  authors:
29
30
  - Erik Hollensbe
30
31
  files:
32
+ - Rakefile
31
33
  - examples/check-included.rb
32
- - examples/check-overlap.rb
33
34
  - examples/generate-a-records.rb
35
+ - examples/check-overlap.rb
34
36
  - lib/ip.rb
35
- - test/ip.rb
36
- - lib/ip/address.rb
37
+ - test/test_ip.rb
37
38
  - lib/ip/cidr.rb
38
- - lib/ip/range.rb
39
39
  - lib/ip/util.rb
40
- test_files:
41
- - test/ip.rb
40
+ - lib/ip/address.rb
41
+ - lib/ip/range.rb
42
+ test_files: []
43
+
42
44
  rdoc_options: []
43
45
 
44
46
  extra_rdoc_files: []
data/test/ip.rb DELETED
@@ -1,603 +0,0 @@
1
- require 'test/unit'
2
- load 'lib/ip.rb'
3
-
4
- class CIDRTest < Test::Unit::TestCase
5
-
6
- def name
7
- return "IP::CIDR tests"
8
- end
9
-
10
- def test_init_generic
11
- a = nil
12
-
13
- begin
14
- IP::CIDR.new(Hash.new)
15
- a = false
16
- rescue IP::AddressException => e
17
- a = true
18
- end
19
-
20
- assert(a, "only accepts string")
21
-
22
- begin
23
- IP::CIDR.new("foomatic_wootmaster/32")
24
- a = false
25
- rescue IP::AddressException => e
26
- a = true
27
- end
28
-
29
- assert(a, "ensures we have a IP address")
30
- end
31
-
32
- def test_init_ipv6
33
- a = nil
34
-
35
- begin
36
- IP::CIDR.new("F00F:DEAD:BEEF::")
37
- a = false
38
- rescue IP::AddressException => e
39
- a = true
40
- end
41
-
42
- assert(a, "no subnet")
43
-
44
- begin
45
- IP::CIDR.new("F00F:DEAD:BEEF::/")
46
- a = false
47
- rescue IP::AddressException => e
48
- a = true
49
- end
50
-
51
- assert(a, "subnet marker, no integer")
52
-
53
- begin
54
- IP::CIDR.new("F00F:DEAD:BEEF::/asdf/32")
55
- a = false
56
- rescue IP::AddressException => e
57
- a = true
58
- end
59
-
60
- assert(a, "'two' subnets")
61
-
62
- begin
63
- IP::CIDR.new("F00F:DEAD:BEEF::/foomatic_wootmaster")
64
- a = false
65
- rescue IP::AddressException => e
66
- a = true
67
- end
68
-
69
- assert(a, "junk subnet")
70
-
71
- begin
72
- IP::CIDR.new("F00F:DEAD:BEEF::/255.255.255.255")
73
- a = false
74
- rescue IP::AddressException => e
75
- a = true
76
- end
77
-
78
- assert(a, "ipv4 long subnet on ipv6")
79
-
80
- cidr = IP::CIDR.new("F00F:DEAD:BEEF::0001/128")
81
-
82
- # this sort of indirectly tests the ipv6 address manipulation.
83
- assert(cidr.ip.short_address == "F00F:DEAD:BEEF::1", "ipv6 object constructed clean")
84
- assert(cidr.mask == 128, "mask is set properly")
85
- assert(cidr.cidr == "F00F:DEAD:BEEF::0001/128", "original CIDR preserved")
86
- end
87
-
88
- def test_init_ipv4
89
- a = nil
90
-
91
- begin
92
- IP::CIDR.new("10.0.0.1")
93
- a = false
94
- rescue IP::AddressException => e
95
- a = true
96
- end
97
-
98
- assert(a, "ipv4 data validation test #1")
99
-
100
- begin
101
- IP::CIDR.new("10.0.0.1/")
102
- a = false
103
- rescue IP::AddressException => e
104
- a = true
105
- end
106
-
107
- assert(a, "ipv4 data validation test #2")
108
-
109
- begin
110
- IP::CIDR.new("10.0.0.1/asdf/32")
111
- a = false
112
- rescue IP::AddressException => e
113
- a = true
114
- end
115
-
116
- assert(a, "ipv4 data validation test #3")
117
-
118
- begin
119
- IP::CIDR.new("10.0.0.1/foomatic_wootmaster")
120
- a = false
121
- rescue IP::AddressException => e
122
- a = true
123
- end
124
-
125
- assert(a, "ipv4 data validation test #4")
126
-
127
- begin
128
- IP::CIDR.new("10.0.0.1/255")
129
- a = false
130
- rescue IP::AddressException => e
131
- a = true
132
- end
133
-
134
- assert(a, "ipv4 data validation test #5")
135
-
136
- cidr = IP::CIDR.new("10.0.0.1/32")
137
-
138
- assert(cidr.ip.ip_address == "10.0.0.1", "ipv4 data integrity test #1")
139
- assert(cidr.mask == 32, "ipv4 data integrity test #2")
140
- assert(cidr.cidr == "10.0.0.1/32", "ipv4 data integrity test #3")
141
-
142
- cidr = IP::CIDR.new("10.0.0.1/255.255.255.255")
143
-
144
- assert(cidr.ip.ip_address == "10.0.0.1", "ipv4 data integrity test #4")
145
- assert(cidr.mask == 32, "ipv4 data integrity test #5")
146
- assert(cidr.cidr == "10.0.0.1/255.255.255.255", "ipv4 data integrity test #6")
147
- end
148
-
149
- def test_netmasks
150
- a = nil
151
-
152
- cidr = IP::CIDR.new("10.0.0.1/32")
153
- assert(cidr.short_netmask == 32, "ipv4 netmask test #1")
154
- assert(cidr.long_netmask.ip_address == "255.255.255.255", "ipv4 netmask test #2")
155
-
156
- cidr = IP::CIDR.new("10.0.0.1/255.255.255.248")
157
- assert(cidr.short_netmask == 29, "ipv4 netmask test #3")
158
- assert(cidr.long_netmask.ip_address == "255.255.255.248", "ipv4 netmask test #4")
159
-
160
- cidr = IP::CIDR.new("F00F:DEAD::/16")
161
- assert(cidr.short_netmask == 16, "ipv6 has proper short netmask")
162
-
163
- begin
164
- cidr.long_netmask
165
- a = false
166
- rescue IP::AddressException => e
167
- a = true
168
- end
169
-
170
- assert(a, "ipv6 cannot use a long netmask")
171
- end
172
-
173
- def test_first_last
174
- cidr = IP::CIDR.new("10.0.0.2/24")
175
- assert(cidr.first_ip.ip_address == "10.0.0.0", "ipv4 first/last test #1")
176
- assert(cidr.last_ip.ip_address == "10.0.0.255", "ipv4 first/last test #2")
177
- end
178
-
179
- def test_range
180
- cidr = IP::CIDR.new("10.0.0.2/24")
181
- assert(cidr.range.find_all { |x| x.ip_address == "10.0.0.1" }.length == 1, "ipv4 range test #1")
182
- assert(cidr.range.find_all { |x| x.ip_address == "10.0.1.0" }.length == 0, "ipv4 range test #2")
183
-
184
- cidr = IP::CIDR.new("::0001/120")
185
- assert(cidr.range.find_all { |x| x.ip_address == "0:0:0:0:0:0:0:00FF" }.length == 1, "ipv6 range test (included)")
186
- assert(cidr.range.find_all { |x| x.ip_address ==" 0:0:0:0:0:0:0:0F00" }.length == 0, "ipv6 range test (not included)")
187
- end
188
-
189
- def test_overlaps
190
- cidr = IP::CIDR.new("10.0.0.2/24")
191
- cidr2 = IP::CIDR.new("10.0.0.1/29")
192
-
193
- assert(cidr.overlaps?(cidr2), "ipv4 overlaps test #1")
194
-
195
- cidr2 = IP::CIDR.new("10.0.0.1/16")
196
-
197
- assert(cidr2.overlaps?(cidr), "ipv4 overlaps test #2")
198
- assert(cidr.overlaps?(cidr2), "ipv4 overlaps test #3")
199
-
200
- cidr = IP::CIDR.new("F00F:DEAD::/16")
201
- cidr2 = IP::CIDR.new("F00F:BEEF::/16")
202
-
203
- assert(cidr.overlaps?(cidr2), "ipv6 #overlaps? reports correctly #1")
204
- assert(cidr2.overlaps?(cidr), "ipv6 #overlaps? reports correctly #2")
205
- end
206
-
207
- def test_includes
208
- cidr = IP::CIDR.new("10.0.0.2/24")
209
- ip = IP::Address::IPv4.new("10.0.0.1")
210
-
211
- assert(cidr.includes?(ip), "ipv4 #includes? reports correctly (included)")
212
- ip = IP::Address::IPv4.new("10.0.1.0")
213
- assert(!cidr.includes?(ip), "ipv4 #includes? reports correctly (not included)")
214
-
215
- cidr = IP::CIDR.new("FF00::/16")
216
- ip = IP::Address::IPv6.new("FF00::DEAD")
217
- assert(cidr.includes?(ip), "ipv6 #includes? reports correctly (included)")
218
- ip = IP::Address::IPv6.new("F000::DEAD")
219
- assert(!cidr.includes?(ip), "ipv6 #includes? reports correctly (not included)")
220
- end
221
-
222
- end
223
-
224
- class RangeTest < Test::Unit::TestCase
225
- def name
226
- return "IP::Range tests"
227
- end
228
-
229
- def test_range_generic
230
- a = nil
231
- begin
232
- IP::Range[Hash.new, ""]
233
- a = false
234
- rescue Exception => e
235
- a = true
236
- end
237
-
238
- assert(a, "generic data types test #1")
239
-
240
- begin
241
- IP::Range["", Hash.new]
242
- a = false
243
- rescue Exception => e
244
- a = true
245
- end
246
-
247
- assert(a, "generic data types test #2")
248
-
249
- begin
250
- IP::Range[IP::Address::IPv6.new("F00F::"), IP::Address::IPv4.new("10.0.0.1")]
251
- a = false
252
- rescue Exception => e
253
- a = true
254
- end
255
-
256
- assert(a, "generic data types test #3")
257
-
258
- begin
259
- IP::Range[IP::Address::IPv4.new("10.0.0.1"), IP::Address::IPv6.new("F00F::")]
260
- a = false
261
- rescue Exception => e
262
- a = true
263
- end
264
-
265
- assert(a, "generic data types test #4")
266
- end
267
-
268
- def test_range_ipv6
269
- a = nil
270
-
271
- begin
272
- IP::Range["::0001", "::00F0"]
273
- a = true
274
- rescue Exception => e
275
- a = false
276
- end
277
-
278
- assert(a, "ipv6 data types test #1")
279
-
280
- begin
281
- IP::Range[IP::Address::IPv6.new("::0001"), IP::Address::IPv6.new("::00F0")]
282
- a = true
283
- rescue Exception => e
284
- a = false
285
- end
286
-
287
- assert(a, "ipv6 data types test #2")
288
-
289
- range = IP::Range["::0001", "::0010"]
290
-
291
- assert(range.find_all { |x| x.short_address == "::1" }.length == 1, "ipv6 range check #1")
292
- assert(range.find_all { |x| x.short_address == "::0010" }.length == 1, "ipv6 range check #2")
293
- assert(range.find_all { |x| x.short_address == "::000A" }.length == 1, "ipv6 range check #3")
294
- assert(range.find_all { |x| x.short_address == "::0011" }.length == 0, "ipv6 range check #4")
295
- end
296
-
297
- def test_range_ipv4
298
- a = nil
299
- begin
300
- IP::Range["10.0.0.1", "10.0.0.2"]
301
- a = true
302
- rescue Exception => e
303
- a = false
304
- end
305
-
306
- assert(a, "ipv4 data types test #1")
307
-
308
- begin
309
- IP::Range[IP::Address::IPv4.new("10.0.0.1"), IP::Address::IPv4.new("10.0.0.2")]
310
- a = true
311
- rescue Exception => e
312
- a = false
313
- end
314
-
315
- assert(a, "ipv4 data types test #2")
316
-
317
- range = IP::Range["10.0.0.1", "10.0.0.10"]
318
-
319
- assert(range.find_all { |x| x.ip_address == "10.0.0.1" }.length == 1, "ipv4 range check #1")
320
- assert(range.find_all { |x| x.ip_address == "10.0.0.10" }.length == 1, "ipv4 range check #2")
321
- assert(range.find_all { |x| x.ip_address == "10.0.0.7" }.length == 1, "ipv4 range check #3")
322
- assert(range.find_all { |x| x.ip_address == "10.0.0.11" }.length == 0, "ipv4 range check #4")
323
- end
324
-
325
- end
326
-
327
- class IPv6AddressTest < Test::Unit::TestCase
328
- def name
329
- return "IP::Address::IPv6 tests"
330
- end
331
-
332
- def test_init
333
- a = nil
334
-
335
- # test the good data first...
336
-
337
- begin
338
- IP::Address::IPv6.new("0000:0000:0000:0000:0000:0000:0000:0001")
339
- a = true
340
- rescue IP::AddressException => e
341
- a = false
342
- end
343
-
344
- assert(a, "full address")
345
-
346
- begin
347
- IP::Address::IPv6.new("::0001")
348
- a = true
349
- rescue IP::AddressException => e
350
- a = false
351
- end
352
-
353
- assert(a, "wildcard address, wildcard left")
354
-
355
- begin
356
- IP::Address::IPv6.new("FF00::")
357
- a = true
358
- rescue IP::AddressException => e
359
- a = false
360
- end
361
-
362
- assert(a, "wildcard address, wildcard right")
363
-
364
- begin
365
- IP::Address::IPv6.new("FF00::0001")
366
- a = true
367
- rescue IP::AddressException => e
368
- a = false
369
- end
370
-
371
- assert(a, "wildcard address, wildcard center")
372
-
373
- begin
374
- IP::Address::IPv6.new("FF00:BEEF::0001")
375
- a = true
376
- rescue IP::AddressException => e
377
- a = false
378
- end
379
-
380
- assert(a, "wildcard address, wildcard center with two leading")
381
-
382
- begin
383
- IP::Address::IPv6.new("FF00::BEEF:0001")
384
- a = true
385
- rescue IP::AddressException => e
386
- a = false
387
- end
388
-
389
- assert(a, "wildcard address, wildcard center with two trailing")
390
-
391
- begin
392
- IP::Address::IPv6.new("::1.2.3.4")
393
- a = true
394
- rescue IP::AddressException => e
395
- puts e
396
- a = false
397
- end
398
-
399
- assert(a, "IPv4 address in IPv6, wildcard left")
400
-
401
- begin
402
- IP::Address::IPv6.new("FFFF::1.2.3.4")
403
- a = true
404
- rescue IP::AddressException => e
405
- a = false
406
- end
407
-
408
- assert(a, "IPv4 in IPv6, data on wildcard @ left")
409
-
410
- begin
411
- IP::Address::IPv6.new("FFFF:0000:0000:0000:0000:0000:1.2.3.4")
412
- a = true
413
- rescue IP::AddressException => e
414
- a = false
415
- end
416
-
417
- assert(a, "IPv4 in IPv6, no wildcard")
418
-
419
- # now, the tests that should fail
420
-
421
- begin
422
- IP::Address::IPv6.new("FF00:BEEF:")
423
- a = false
424
- rescue IP::AddressException => e
425
- a = true
426
- end
427
-
428
- assert(a, "not enough octets")
429
-
430
- begin
431
- IP::Address::IPv6.new("FF00::BEEF::")
432
- a = false
433
- rescue IP::AddressException => e
434
- a = true
435
- end
436
-
437
- assert(a, "double wildcard no trailer")
438
-
439
- begin
440
- IP::Address::IPv6.new("FF00::BEEF::DEAD")
441
- a = false
442
- rescue IP::AddressException => e
443
- a = true
444
- end
445
-
446
- assert(a, "double wildcard w/ trailer")
447
-
448
- begin
449
- IP::Address::IPv6.new("HF00::0001")
450
- a = false
451
- rescue IP::AddressException => e
452
- a = true
453
- end
454
-
455
- assert(a, "invalid hexidecimal")
456
-
457
- begin
458
- IP::Address::IPv6.new("1.2.3.4::0001")
459
- a = false
460
- rescue IP::AddressException => e
461
- a = true
462
- end
463
-
464
- assert(a, "invalid IPv4 in IPv6")
465
-
466
- end
467
-
468
- def test_accessors
469
- ip = IP::Address::IPv6.new("F00F::DEAD:BEEF")
470
-
471
- assert(ip[0] == 61455 && ip.octet(0) == 61455, "#octet is integer representation, #[] is #octet")
472
- assert(ip.octet_as_hex(0) == "F00F", "octet converts to hex properly")
473
- assert(ip.ip_address == "F00F::DEAD:BEEF", '#ip_address preserves original address')
474
- end
475
-
476
- def test_address
477
- ip = IP::Address::IPv6.new("F00F::DEAD:BEEF")
478
- assert(ip.short_address == "F00F::DEAD:BEEF", 'wildcard left - #short_address returns a compressed version')
479
- assert(ip.long_address == "F00F:0:0:0:0:0:DEAD:BEEF", 'wildcard left - #long_address returns the right thing')
480
-
481
- ip = IP::Address::IPv6.new("F00F:DEAD::BEEF")
482
- assert(ip.short_address == "F00F:DEAD::BEEF", 'wildcard right - #short_address returns a compressed version')
483
- assert(ip.long_address == "F00F:DEAD:0:0:0:0:0:BEEF", 'wildcard right - #long_address returns the right thing')
484
-
485
- ip = IP::Address::IPv6.new("F00F:DEAD:0:0:0:0:0:BEEF")
486
- assert(ip.short_address == "F00F:DEAD::BEEF", 'no wildcard - #short_address returns a compressed version')
487
- assert(ip.long_address == "F00F:DEAD:0:0:0:0:0:BEEF", 'no wildcard - #long_address returns the right thing')
488
-
489
- ip = IP::Address::IPv6.new("F00F::DEAD:BEEF:0:0")
490
- assert(ip.short_address == "F00F:0:0:0:DEAD:BEEF::", '#short_address returns a compressed version with wildcard @ right')
491
- end
492
-
493
- end
494
-
495
-
496
- class IPv4AddressTest < Test::Unit::TestCase
497
- def name
498
- return "IP::Address::IPv4 tests"
499
- end
500
-
501
- def test_init
502
- ip = nil
503
- begin
504
- ip = IP::Address::IPv4.new(Hash.new)
505
- rescue IP::AddressException => e
506
- assert(true, "init test #1")
507
- end
508
-
509
- assert(false, "init test #2") if ip
510
-
511
- ip = nil
512
-
513
- begin
514
- ip = IP::Address::IPv4.new("asdf")
515
- rescue IP::AddressException => e
516
- assert(true, "init test #2")
517
- end
518
-
519
- assert(false, "init test #2") if ip
520
- ip = nil
521
-
522
- begin
523
- ip = IP::Address::IPv4.new("0.0.0")
524
- rescue IP::AddressException => e
525
- assert(true, "init test #3")
526
- end
527
-
528
- assert(false, "init test #3") if ip
529
- ip = nil
530
-
531
- begin
532
- ip = IP::Address::IPv4.new("256.255.255.255")
533
- rescue IP::AddressException => e
534
- assert(true, "init test #4")
535
- end
536
-
537
- assert(false, "init test #4") if ip
538
-
539
- ip = nil
540
- begin
541
- ip = IP::Address::IPv4.new("255.255.255.255aaaa")
542
- rescue IP::AddressException => e
543
- assert(true, "init test #5")
544
- end
545
-
546
- assert(false, "init test #5") if ip
547
-
548
- ip = nil
549
- begin
550
- ip = IP::Address::IPv4.new("255.255.255.")
551
- rescue IP::AddressException => e
552
- assert(true, "init test #6")
553
- end
554
-
555
- assert(false, "init test #6") if ip
556
-
557
- end
558
-
559
- def test_accessor
560
- ip = IP::Address::IPv4.new("10.1.2.3")
561
- assert(ip.ip_address == "10.1.2.3", "accessor test #1")
562
- assert(ip.octets[0] == 10, "accessor test #2")
563
- assert(ip.octets[3] == 3, "accessor test #3")
564
- assert(ip.octets[4] == nil, "accessor test #4")
565
-
566
- assert(ip.octet(1) == ip[1] && ip[1] == 1, "accessor test #5")
567
-
568
- oct = nil
569
-
570
- begin
571
- oct = ip[4]
572
- rescue IP::BoundaryException => e
573
- assert(true, "accessor test #6")
574
- end
575
-
576
- assert(false, "accessor test #6") if oct
577
- end
578
-
579
- end
580
-
581
- class UtilTest < Test::Unit::TestCase
582
- def name
583
- return "IP::Address::Util tests"
584
- end
585
-
586
- def test_pack_unpack
587
- address = "10.0.0.1"
588
- assert(IP::Address::Util.unpack(IP::Address::Util.pack(IP::Address::IPv4.new(address))).ip_address == address, "pack/unpack test")
589
- end
590
-
591
- def test_short_netmask
592
- ip = IP::Address::IPv4.new("255.255.255.255")
593
- assert(IP::Address::Util.short_netmask(ip) == 32, "Short Netmask Test #1")
594
- ip = IP::Address::IPv4.new("255.255.255.248")
595
- assert(IP::Address::Util.short_netmask(ip) == 29, "Short Netmask Test #2")
596
- end
597
-
598
- def test_long_netmask
599
- assert(IP::Address::Util.long_netmask_ipv4(32).ip_address == "255.255.255.255", "Long Netmask Test #1")
600
- assert(IP::Address::Util.long_netmask_ipv4(29).ip_address == "255.255.255.248", "Long Netmask Test #2")
601
- end
602
-
603
- end