ipv4_address 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +5 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +48 -0
- data/lib/ipv4_address.rb +144 -0
- data/spec/ipv4_address_spec.rb +504 -0
- data/spec/spec_helper.rb +9 -0
- metadata +73 -0
data/.document
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Marcin Nowicki
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= ipv4_address
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Marcin Nowicki. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ipv4_address"
|
8
|
+
gem.summary = %Q{Recognition of ipv4 addresses}
|
9
|
+
gem.description = %Q{Recognize ipv4 addresses in many ways}
|
10
|
+
gem.email = "pr0d1r2@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/Pr0d1r2/ipv4_address"
|
12
|
+
gem.authors = ["Marcin Nowicki"]
|
13
|
+
gem.add_development_dependency "rspec"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
36
|
+
require 'rake/rdoctask'
|
37
|
+
Rake::RDocTask.new do |rdoc|
|
38
|
+
if File.exist?('VERSION')
|
39
|
+
version = File.read('VERSION')
|
40
|
+
else
|
41
|
+
version = ""
|
42
|
+
end
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "ipv4_address #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/lib/ipv4_address.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'ipaddr'
|
2
|
+
|
3
|
+
class IPv4Address < IPAddr
|
4
|
+
|
5
|
+
# by IANA
|
6
|
+
RESERVED = ['128.0.0.0/16', '191.255.0.0/16', '192.0.0.0/24', '223.255.255.0/24']
|
7
|
+
PRIVATE = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16']
|
8
|
+
LOOPBACK = ['127.0.0.0/8']
|
9
|
+
LINK_LOCAL = ['169.254.0.0/16']
|
10
|
+
DOCUMENTATION = ['192.0.2.0/24']
|
11
|
+
IPV6_RELAY = ['192.88.99.0/24']
|
12
|
+
BENCHMARK = ['198.18.0.0/15']
|
13
|
+
MULTICAST = ['224.0.0.0/4']
|
14
|
+
|
15
|
+
def reserved?
|
16
|
+
exist_in_pool?(RESERVED)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.reserved?(addr)
|
20
|
+
begin
|
21
|
+
new(addr).reserved?
|
22
|
+
rescue
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def private?
|
28
|
+
exist_in_pool?(PRIVATE)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.private?(addr)
|
32
|
+
begin
|
33
|
+
new(addr).private?
|
34
|
+
rescue
|
35
|
+
false
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def loopback?
|
40
|
+
exist_in_pool?(LOOPBACK)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.loopback?(addr)
|
44
|
+
begin
|
45
|
+
new(addr).loopback?
|
46
|
+
rescue
|
47
|
+
false
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def link_local?
|
52
|
+
exist_in_pool?(LINK_LOCAL)
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.link_local?(addr)
|
56
|
+
begin
|
57
|
+
new(addr).link_local?
|
58
|
+
rescue
|
59
|
+
false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def documentation?
|
64
|
+
exist_in_pool?(DOCUMENTATION)
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.documentation?(addr)
|
68
|
+
begin
|
69
|
+
new(addr).documentation?
|
70
|
+
rescue
|
71
|
+
false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def ipv6_relay?
|
76
|
+
exist_in_pool?(IPV6_RELAY)
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.ipv6_relay?(addr)
|
80
|
+
begin
|
81
|
+
new(addr).ipv6_relay?
|
82
|
+
rescue
|
83
|
+
false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def benchmark?
|
88
|
+
exist_in_pool?(BENCHMARK)
|
89
|
+
end
|
90
|
+
|
91
|
+
def self.benchmark?(addr)
|
92
|
+
begin
|
93
|
+
new(addr).benchmark?
|
94
|
+
rescue
|
95
|
+
false
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def multicast?
|
100
|
+
exist_in_pool?(MULTICAST)
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.multicast?(addr)
|
104
|
+
begin
|
105
|
+
new(addr).multicast?
|
106
|
+
rescue
|
107
|
+
false
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def internet_routable?
|
112
|
+
!reserved? &&
|
113
|
+
!private? &&
|
114
|
+
!loopback? &&
|
115
|
+
!link_local? &&
|
116
|
+
!documentation? &&
|
117
|
+
!ipv6_relay? &&
|
118
|
+
!benchmark? &&
|
119
|
+
!multicast?
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.internet_routable?(addr)
|
123
|
+
is?(to_s) && new(addr).internet_routable?
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.is?(addr)
|
127
|
+
begin
|
128
|
+
IPAddr.new(addr)
|
129
|
+
true
|
130
|
+
rescue
|
131
|
+
false
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def exist_in_pool?(collection)
|
138
|
+
collection.collect do |address_scope|
|
139
|
+
return true if IPAddr.new(address_scope).include?(self)
|
140
|
+
end
|
141
|
+
false
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
@@ -0,0 +1,504 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe IPv4Address do
|
4
|
+
|
5
|
+
describe 'self.reserved?' do
|
6
|
+
it 'should be true when is in reserved pool' do
|
7
|
+
%w(
|
8
|
+
128.0.0.1
|
9
|
+
191.255.0.1
|
10
|
+
192.0.0.1
|
11
|
+
223.255.255.1
|
12
|
+
).each do |address|
|
13
|
+
IPv4Address.reserved?(address).should be_true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should be false when is not in reserved pool' do
|
18
|
+
%w(
|
19
|
+
74.125.127.83
|
20
|
+
0.0.0.0
|
21
|
+
10.0.0.1
|
22
|
+
14.0.0.1
|
23
|
+
127.0.0.1
|
24
|
+
169.254.0.1
|
25
|
+
172.16.0.1
|
26
|
+
192.0.2.1
|
27
|
+
192.88.99.1
|
28
|
+
192.168.0.1
|
29
|
+
198.18.0.1
|
30
|
+
224.0.0.1
|
31
|
+
240.0.0.1
|
32
|
+
255.255.255.255
|
33
|
+
256.0.0.0
|
34
|
+
255.256.0.0
|
35
|
+
255.255.256.0
|
36
|
+
255.255.255.256
|
37
|
+
999.999.999.999
|
38
|
+
string
|
39
|
+
).each do |address|
|
40
|
+
IPv4Address.reserved?(address).should be_false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'self.private?' do
|
46
|
+
it 'should be true when address is in private pool' do
|
47
|
+
%w(
|
48
|
+
10.0.0.1
|
49
|
+
172.16.0.1
|
50
|
+
192.168.0.1
|
51
|
+
).each do |address|
|
52
|
+
IPv4Address.private?(address).should be_true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should be false when is not in private pool' do
|
57
|
+
%w(
|
58
|
+
74.125.127.83
|
59
|
+
0.0.0.0
|
60
|
+
14.0.0.1
|
61
|
+
127.0.0.1
|
62
|
+
128.0.0.1
|
63
|
+
169.254.0.1
|
64
|
+
191.255.0.1
|
65
|
+
192.0.0.1
|
66
|
+
192.0.2.1
|
67
|
+
192.88.99.1
|
68
|
+
198.18.0.1
|
69
|
+
223.255.255.1
|
70
|
+
224.0.0.1
|
71
|
+
240.0.0.1
|
72
|
+
255.255.255.255
|
73
|
+
256.0.0.0
|
74
|
+
255.256.0.0
|
75
|
+
255.255.256.0
|
76
|
+
255.255.255.256
|
77
|
+
999.999.999.999
|
78
|
+
string
|
79
|
+
).each do |address|
|
80
|
+
IPv4Address.private?(address).should be_false
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe 'self.loopback?' do
|
86
|
+
it 'should be true when is not in loopback pool' do
|
87
|
+
%w(
|
88
|
+
127.0.0.1
|
89
|
+
).each do |address|
|
90
|
+
IPv4Address.loopback?(address).should be_true
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should be false when is not in loopback pool' do
|
95
|
+
%w(
|
96
|
+
74.125.127.83
|
97
|
+
0.0.0.0
|
98
|
+
10.0.0.1
|
99
|
+
14.0.0.1
|
100
|
+
128.0.0.1
|
101
|
+
169.254.0.1
|
102
|
+
172.16.0.1
|
103
|
+
191.255.0.1
|
104
|
+
192.0.0.1
|
105
|
+
192.0.2.1
|
106
|
+
192.88.99.1
|
107
|
+
192.168.0.1
|
108
|
+
198.18.0.1
|
109
|
+
223.255.255.1
|
110
|
+
224.0.0.1
|
111
|
+
240.0.0.1
|
112
|
+
255.255.255.255
|
113
|
+
256.0.0.0
|
114
|
+
255.256.0.0
|
115
|
+
255.255.256.0
|
116
|
+
255.255.255.256
|
117
|
+
999.999.999.999
|
118
|
+
string
|
119
|
+
).each do |address|
|
120
|
+
IPv4Address.loopback?(address).should be_false
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'self.link_local?' do
|
126
|
+
it 'should be true when is not in link_local pool' do
|
127
|
+
%w(
|
128
|
+
169.254.0.1
|
129
|
+
).each do |address|
|
130
|
+
IPv4Address.link_local?(address).should be_true
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'should be false when is not in link_local pool' do
|
135
|
+
%w(
|
136
|
+
74.125.127.83
|
137
|
+
0.0.0.0
|
138
|
+
10.0.0.1
|
139
|
+
14.0.0.1
|
140
|
+
127.0.0.1
|
141
|
+
128.0.0.1
|
142
|
+
172.16.0.1
|
143
|
+
191.255.0.1
|
144
|
+
192.0.0.1
|
145
|
+
192.0.2.1
|
146
|
+
192.88.99.1
|
147
|
+
192.168.0.1
|
148
|
+
198.18.0.1
|
149
|
+
223.255.255.1
|
150
|
+
224.0.0.1
|
151
|
+
240.0.0.1
|
152
|
+
255.255.255.255
|
153
|
+
256.0.0.0
|
154
|
+
255.256.0.0
|
155
|
+
255.255.256.0
|
156
|
+
255.255.255.256
|
157
|
+
999.999.999.999
|
158
|
+
string
|
159
|
+
).each do |address|
|
160
|
+
IPv4Address.link_local?(address).should be_false
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
describe 'self.documentation?' do
|
167
|
+
it 'should be true when is not in documentation pool' do
|
168
|
+
%w(
|
169
|
+
192.0.2.1
|
170
|
+
).each do |address|
|
171
|
+
IPv4Address.documentation?(address).should be_true
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'should be false when is not in documentation pool' do
|
176
|
+
%w(
|
177
|
+
74.125.127.83
|
178
|
+
0.0.0.0
|
179
|
+
10.0.0.1
|
180
|
+
14.0.0.1
|
181
|
+
127.0.0.1
|
182
|
+
128.0.0.1
|
183
|
+
169.254.0.1
|
184
|
+
172.16.0.1
|
185
|
+
191.255.0.1
|
186
|
+
192.0.0.1
|
187
|
+
192.88.99.1
|
188
|
+
192.168.0.1
|
189
|
+
198.18.0.1
|
190
|
+
223.255.255.1
|
191
|
+
224.0.0.1
|
192
|
+
240.0.0.1
|
193
|
+
255.255.255.255
|
194
|
+
256.0.0.0
|
195
|
+
255.256.0.0
|
196
|
+
255.255.256.0
|
197
|
+
255.255.255.256
|
198
|
+
999.999.999.999
|
199
|
+
string
|
200
|
+
).each do |address|
|
201
|
+
IPv4Address.documentation?(address).should be_false
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
206
|
+
describe 'self.ipv6_relay?' do
|
207
|
+
it 'should be true when is not in ipv6_relay pool' do
|
208
|
+
%w(
|
209
|
+
192.88.99.1
|
210
|
+
).each do |address|
|
211
|
+
IPv4Address.ipv6_relay?(address).should be_true
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
it 'should be false when is not in ipv6_relay pool' do
|
216
|
+
%w(
|
217
|
+
74.125.127.83
|
218
|
+
0.0.0.0
|
219
|
+
10.0.0.1
|
220
|
+
14.0.0.1
|
221
|
+
127.0.0.1
|
222
|
+
128.0.0.1
|
223
|
+
169.254.0.1
|
224
|
+
172.16.0.1
|
225
|
+
191.255.0.1
|
226
|
+
192.0.0.1
|
227
|
+
192.0.2.1
|
228
|
+
192.168.0.1
|
229
|
+
198.18.0.1
|
230
|
+
223.255.255.1
|
231
|
+
224.0.0.1
|
232
|
+
240.0.0.1
|
233
|
+
255.255.255.255
|
234
|
+
256.0.0.0
|
235
|
+
255.256.0.0
|
236
|
+
255.255.256.0
|
237
|
+
255.255.255.256
|
238
|
+
999.999.999.999
|
239
|
+
string
|
240
|
+
).each do |address|
|
241
|
+
IPv4Address.ipv6_relay?(address).should be_false
|
242
|
+
end
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
describe 'self.benchmark?' do
|
247
|
+
it 'should be true when is not in benchmark pool' do
|
248
|
+
%w(
|
249
|
+
198.18.0.1
|
250
|
+
).each do |address|
|
251
|
+
IPv4Address.benchmark?(address).should be_true
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'should be false when is not in benchmark pool' do
|
256
|
+
%w(
|
257
|
+
74.125.127.83
|
258
|
+
0.0.0.0
|
259
|
+
10.0.0.1
|
260
|
+
14.0.0.1
|
261
|
+
127.0.0.1
|
262
|
+
128.0.0.1
|
263
|
+
169.254.0.1
|
264
|
+
172.16.0.1
|
265
|
+
191.255.0.1
|
266
|
+
192.0.0.1
|
267
|
+
192.0.2.1
|
268
|
+
192.88.99.1
|
269
|
+
192.168.0.1
|
270
|
+
223.255.255.1
|
271
|
+
224.0.0.1
|
272
|
+
240.0.0.1
|
273
|
+
255.255.255.255
|
274
|
+
256.0.0.0
|
275
|
+
255.256.0.0
|
276
|
+
255.255.256.0
|
277
|
+
255.255.255.256
|
278
|
+
999.999.999.999
|
279
|
+
string
|
280
|
+
).each do |address|
|
281
|
+
IPv4Address.benchmark?(address).should be_false
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
describe 'self.multicast?' do
|
287
|
+
it 'should be true when is not in multicast pool' do
|
288
|
+
%w(
|
289
|
+
224.0.0.1
|
290
|
+
).each do |address|
|
291
|
+
IPv4Address.multicast?(address).should be_true
|
292
|
+
end
|
293
|
+
end
|
294
|
+
|
295
|
+
it 'should be false when is not in multicast pool' do
|
296
|
+
%w(
|
297
|
+
74.125.127.83
|
298
|
+
0.0.0.0
|
299
|
+
10.0.0.1
|
300
|
+
14.0.0.1
|
301
|
+
127.0.0.1
|
302
|
+
128.0.0.1
|
303
|
+
169.254.0.1
|
304
|
+
172.16.0.1
|
305
|
+
191.255.0.1
|
306
|
+
192.0.0.1
|
307
|
+
192.0.2.1
|
308
|
+
192.88.99.1
|
309
|
+
192.168.0.1
|
310
|
+
198.18.0.1
|
311
|
+
223.255.255.1
|
312
|
+
240.0.0.1
|
313
|
+
255.255.255.255
|
314
|
+
256.0.0.0
|
315
|
+
255.256.0.0
|
316
|
+
255.255.256.0
|
317
|
+
255.255.255.256
|
318
|
+
999.999.999.999
|
319
|
+
string
|
320
|
+
).each do |address|
|
321
|
+
IPv4Address.multicast?(address).should be_false
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
describe 'self.is?' do
|
327
|
+
it 'should be true when is an ipv4 address' do
|
328
|
+
%w(
|
329
|
+
74.125.127.83
|
330
|
+
0.0.0.0
|
331
|
+
10.0.0.1
|
332
|
+
14.0.0.1
|
333
|
+
127.0.0.1
|
334
|
+
128.0.0.1
|
335
|
+
169.254.0.1
|
336
|
+
172.16.0.1
|
337
|
+
191.255.0.1
|
338
|
+
192.0.0.1
|
339
|
+
192.0.2.1
|
340
|
+
192.88.99.1
|
341
|
+
192.168.0.1
|
342
|
+
198.18.0.1
|
343
|
+
223.255.255.1
|
344
|
+
224.0.0.1
|
345
|
+
240.0.0.1
|
346
|
+
255.255.255.255
|
347
|
+
).each do |address|
|
348
|
+
IPv4Address.is?(address).should be_true
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
it 'should be false when is not an ipv4 address' do
|
353
|
+
%w(
|
354
|
+
256.0.0.0
|
355
|
+
255.256.0.0
|
356
|
+
255.255.256.0
|
357
|
+
255.255.255.256
|
358
|
+
999.999.999.999
|
359
|
+
string
|
360
|
+
).each do |address|
|
361
|
+
IPv4Address.is?(address).should be_false
|
362
|
+
end
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
describe 'self.internet_routable?' do
|
367
|
+
before(:each) do
|
368
|
+
@ipv4_address = mock(IPv4Address, :internet_routable? => false)
|
369
|
+
end
|
370
|
+
|
371
|
+
it 'should be true when given string is an ip address and it is internet routable' do
|
372
|
+
IPv4Address.should_receive(:is?).with('IPv4Address').and_return(true)
|
373
|
+
@ipv4_address.should_receive(:internet_routable?).and_return(true)
|
374
|
+
IPv4Address.should_receive(:new).with(:addr).and_return(@ipv4_address)
|
375
|
+
IPv4Address.internet_routable?(:addr).should be_true
|
376
|
+
end
|
377
|
+
|
378
|
+
it 'should be false when string is an ip address but it is not internet routable' do
|
379
|
+
IPv4Address.should_receive(:is?).with('IPv4Address').and_return(true)
|
380
|
+
@ipv4_address.should_receive(:internet_routable?).and_return(false)
|
381
|
+
IPv4Address.should_receive(:new).with(:addr).and_return(@ipv4_address)
|
382
|
+
IPv4Address.internet_routable?(:addr).should be_false
|
383
|
+
end
|
384
|
+
|
385
|
+
it 'should be false when' do
|
386
|
+
IPv4Address.should_receive(:is?).with('IPv4Address').and_return(false)
|
387
|
+
IPv4Address.internet_routable?(:addr).should be_false
|
388
|
+
end
|
389
|
+
end
|
390
|
+
|
391
|
+
describe 'instance method' do
|
392
|
+
before(:each) do
|
393
|
+
@ipv4_address = IPv4Address.new
|
394
|
+
end
|
395
|
+
|
396
|
+
describe 'internet_routable?' do
|
397
|
+
before(:each) do
|
398
|
+
@ipv4_address.stub!(
|
399
|
+
:reserved? => false,
|
400
|
+
:private? => false,
|
401
|
+
:loopback? => false,
|
402
|
+
:link_local? => false,
|
403
|
+
:documentation? => false,
|
404
|
+
:ipv6_relay? => false,
|
405
|
+
:benchmark? => false,
|
406
|
+
:multicast? => false
|
407
|
+
)
|
408
|
+
end
|
409
|
+
|
410
|
+
it 'should be true when address is not in any pools' do
|
411
|
+
@ipv4_address.should_receive(:reserved?).and_return(false)
|
412
|
+
@ipv4_address.should_receive(:private?).and_return(false)
|
413
|
+
@ipv4_address.should_receive(:loopback?).and_return(false)
|
414
|
+
@ipv4_address.should_receive(:link_local?).and_return(false)
|
415
|
+
@ipv4_address.should_receive(:documentation?).and_return(false)
|
416
|
+
@ipv4_address.should_receive(:ipv6_relay?).and_return(false)
|
417
|
+
@ipv4_address.should_receive(:benchmark?).and_return(false)
|
418
|
+
@ipv4_address.should_receive(:multicast?).and_return(false)
|
419
|
+
@ipv4_address.internet_routable?.should be_true
|
420
|
+
end
|
421
|
+
|
422
|
+
it 'should be false when address is in reserved pool' do
|
423
|
+
@ipv4_address.should_receive(:reserved?).and_return(true)
|
424
|
+
@ipv4_address.internet_routable?.should be_false
|
425
|
+
end
|
426
|
+
|
427
|
+
it 'should be false when address is in pool' do
|
428
|
+
@ipv4_address.should_receive(:private?).and_return(true)
|
429
|
+
@ipv4_address.internet_routable?.should be_false
|
430
|
+
end
|
431
|
+
|
432
|
+
it 'should be false when address is in loopback pool' do
|
433
|
+
@ipv4_address.should_receive(:loopback?).and_return(true)
|
434
|
+
@ipv4_address.internet_routable?.should be_false
|
435
|
+
end
|
436
|
+
|
437
|
+
it 'should be false when address is in link_local pool' do
|
438
|
+
@ipv4_address.should_receive(:link_local?).and_return(true)
|
439
|
+
@ipv4_address.internet_routable?.should be_false
|
440
|
+
end
|
441
|
+
|
442
|
+
it 'should be false when address is in documentation pool' do
|
443
|
+
@ipv4_address.should_receive(:documentation?).and_return(true)
|
444
|
+
@ipv4_address.internet_routable?.should be_false
|
445
|
+
end
|
446
|
+
|
447
|
+
it 'should be false when address is in ipv6_relay pool' do
|
448
|
+
@ipv4_address.should_receive(:ipv6_relay?).and_return(true)
|
449
|
+
@ipv4_address.internet_routable?.should be_false
|
450
|
+
end
|
451
|
+
|
452
|
+
it 'should be false when address is in benchmark pool' do
|
453
|
+
@ipv4_address.should_receive(:benchmark?).and_return(true)
|
454
|
+
@ipv4_address.internet_routable?.should be_false
|
455
|
+
end
|
456
|
+
|
457
|
+
it 'should be false when address is in multicast pool' do
|
458
|
+
@ipv4_address.should_receive(:multicast?).and_return(true)
|
459
|
+
@ipv4_address.internet_routable?.should be_false
|
460
|
+
end
|
461
|
+
end
|
462
|
+
|
463
|
+
it 'reserved? should check_existance in proper pool' do
|
464
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::RESERVED)
|
465
|
+
@ipv4_address.reserved?
|
466
|
+
end
|
467
|
+
|
468
|
+
it 'private? should check_existance in proper pool' do
|
469
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::PRIVATE)
|
470
|
+
@ipv4_address.private?
|
471
|
+
end
|
472
|
+
|
473
|
+
it 'loopback? should check_existance in proper pool' do
|
474
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::LOOPBACK)
|
475
|
+
@ipv4_address.loopback?
|
476
|
+
end
|
477
|
+
|
478
|
+
it 'link_local? should check_existance in proper pool' do
|
479
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::LINK_LOCAL)
|
480
|
+
@ipv4_address.link_local?
|
481
|
+
end
|
482
|
+
|
483
|
+
it 'documentation? should check_existance in proper pool' do
|
484
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::DOCUMENTATION)
|
485
|
+
@ipv4_address.documentation?
|
486
|
+
end
|
487
|
+
|
488
|
+
it 'ipv6_relay? should check_existance in proper pool' do
|
489
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::IPV6_RELAY)
|
490
|
+
@ipv4_address.ipv6_relay?
|
491
|
+
end
|
492
|
+
|
493
|
+
it 'benchmark? should check_existance in proper pool' do
|
494
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::BENCHMARK)
|
495
|
+
@ipv4_address.benchmark?
|
496
|
+
end
|
497
|
+
|
498
|
+
it 'multicast? should check_existance in proper pool' do
|
499
|
+
@ipv4_address.should_receive(:exist_in_pool?).with(IPv4Address::MULTICAST)
|
500
|
+
@ipv4_address.multicast?
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ipv4_address
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marcin Nowicki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-05 00:00:00 +02:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: Recognize ipv4 addresses in many ways
|
26
|
+
email: pr0d1r2@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- lib/ipv4_address.rb
|
41
|
+
- spec/ipv4_address_spec.rb
|
42
|
+
- spec/spec_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/Pr0d1r2/ipv4_address
|
45
|
+
licenses: []
|
46
|
+
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options:
|
49
|
+
- --charset=UTF-8
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: "0"
|
57
|
+
version:
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.5
|
68
|
+
signing_key:
|
69
|
+
specification_version: 2
|
70
|
+
summary: Recognition of ipv4 addresses
|
71
|
+
test_files:
|
72
|
+
- spec/ipv4_address_spec.rb
|
73
|
+
- spec/spec_helper.rb
|