relevance_ipaddress 0.5.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.
@@ -0,0 +1,290 @@
1
+ require 'test_helper'
2
+
3
+ class IPv6Test < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @klass = IPAddress::IPv6
7
+
8
+ @compress_addr = {
9
+ "2001:db8:0000:0000:0008:0800:200c:417a" => "2001:db8::8:800:200c:417a",
10
+ "2001:db8:0:0:8:800:200c:417a" => "2001:db8::8:800:200c:417a",
11
+ "ff01:0:0:0:0:0:0:101" => "ff01::101",
12
+ "0:0:0:0:0:0:0:1" => "::1",
13
+ "0:0:0:0:0:0:0:0" => "::"}
14
+
15
+ @valid_ipv6 = { # Kindly taken from the python IPy library
16
+ "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210" => 338770000845734292534325025077361652240,
17
+ "1080:0000:0000:0000:0008:0800:200C:417A" => 21932261930451111902915077091070067066,
18
+ "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066,
19
+ "1080:0::8:800:200C:417A" => 21932261930451111902915077091070067066,
20
+ "1080::8:800:200C:417A" => 21932261930451111902915077091070067066,
21
+ "FF01:0:0:0:0:0:0:43" => 338958331222012082418099330867817087043,
22
+ "FF01:0:0::0:0:43" => 338958331222012082418099330867817087043,
23
+ "FF01::43" => 338958331222012082418099330867817087043,
24
+ "0:0:0:0:0:0:0:1" => 1,
25
+ "0:0:0::0:0:1" => 1,
26
+ "::1" => 1,
27
+ "0:0:0:0:0:0:0:0" => 0,
28
+ "0:0:0::0:0:0" => 0,
29
+ "::" => 0,
30
+ "1080:0:0:0:8:800:200C:417A" => 21932261930451111902915077091070067066,
31
+ "1080::8:800:200C:417A" => 21932261930451111902915077091070067066}
32
+
33
+ @invalid_ipv6 = [":1:2:3:4:5:6:7",
34
+ ":1:2:3:4:5:6:7"]
35
+
36
+ @ip = @klass.new "2001:db8::8:800:200c:417a/64"
37
+ @network = @klass.new "2001:db8:8:800::/64"
38
+ @arr = [8193,3512,0,0,8,2048,8204,16762]
39
+ @hex = "20010db80000000000080800200c417a"
40
+ end
41
+
42
+
43
+ def test_attribute_address
44
+ addr = "2001:0db8:0000:0000:0008:0800:200c:417a"
45
+ assert_equal addr, @ip.address
46
+ end
47
+
48
+ def test_initialize
49
+ assert_instance_of @klass, @ip
50
+ @valid_ipv6.keys.each do |ip|
51
+ assert_nothing_raised {@klass.new ip}
52
+ end
53
+ @invalid_ipv6.each do |ip|
54
+ assert_raise(ArgumentError) {@klass.new ip}
55
+ end
56
+ assert_equal 64, @ip.prefix
57
+ end
58
+
59
+ def test_attribute_groups
60
+ assert_equal @arr, @ip.groups
61
+ end
62
+
63
+ def test_method_hexs
64
+ arr = "2001:0db8:0000:0000:0008:0800:200c:417a".split(":")
65
+ assert_equal arr, @ip.hexs
66
+ end
67
+
68
+ def test_method_to_i
69
+ @valid_ipv6.each do |ip,num|
70
+ assert_equal num, @klass.new(ip).to_i
71
+ end
72
+ end
73
+
74
+ def test_method_bits
75
+ bits = "0010000000000001000011011011100000000000000000000" +
76
+ "000000000000000000000000000100000001000000000000010000" +
77
+ "0000011000100000101111010"
78
+ assert_equal bits, @ip.bits
79
+ end
80
+
81
+ def test_method_prefix=()
82
+ ip = @klass.new "2001:db8::8:800:200c:417a"
83
+ assert_equal 128, ip.prefix
84
+ ip.prefix = 64
85
+ assert_equal 64, ip.prefix
86
+ assert_equal "2001:db8::8:800:200c:417a/64", ip.to_s
87
+ end
88
+
89
+ def test_method_mapped?
90
+ assert_equal false, @ip.mapped?
91
+ end
92
+
93
+ def test_method_literal
94
+ str = "2001-0db8-0000-0000-0008-0800-200c-417a.ipv6-literal.net"
95
+ assert_equal str, @ip.literal
96
+ end
97
+
98
+ def test_method_group
99
+ @arr.each_with_index do |val,index|
100
+ assert_equal val, @ip[index]
101
+ end
102
+ end
103
+
104
+ def test_method_network?
105
+ assert_equal true, @network.network?
106
+ assert_equal false, @ip.network?
107
+ end
108
+
109
+ def test_method_to_hex
110
+ assert_equal @hex, @ip.to_hex
111
+ end
112
+
113
+ def test_method_to_s
114
+ assert_equal "2001:db8::8:800:200c:417a/64", @ip.to_s
115
+ end
116
+
117
+ def test_method_to_string
118
+ str = "2001:0db8:0000:0000:0008:0800:200c:417a/64"
119
+ assert_equal str, @ip.to_string
120
+ end
121
+
122
+ def test_method_data
123
+ str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
124
+ assert_equal str, @ip.data
125
+ end
126
+
127
+ def test_method_compressed
128
+ assert_equal "1:1:1::1", @klass.new("1:1:1:0:0:0:0:1").compressed
129
+ assert_equal "1:0:1::1", @klass.new("1:0:1:0:0:0:0:1").compressed
130
+ assert_equal "1:0:0:1::1", @klass.new("1:0:0:1:0:0:0:1").compressed
131
+ assert_equal "1::1:0:0:1", @klass.new("1:0:0:0:1:0:0:1").compressed
132
+ assert_equal "1::1", @klass.new("1:0:0:0:0:0:0:1").compressed
133
+ end
134
+
135
+ def test_method_unspecified?
136
+ assert_equal true, @klass.new("::").unspecified?
137
+ assert_equal false, @ip.unspecified?
138
+ end
139
+
140
+ def test_method_loopback?
141
+ assert_equal true, @klass.new("::1").loopback?
142
+ assert_equal false, @ip.loopback?
143
+ end
144
+
145
+ def test_classmethod_expand
146
+ compressed = "2001:db8:0:cd30::"
147
+ expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
148
+ assert_equal expanded, @klass.expand(compressed)
149
+ assert_not_equal expanded, @klass.expand("2001:0db8:0:cd3")
150
+ assert_not_equal expanded, @klass.expand("2001:0db8::cd30")
151
+ assert_not_equal expanded, @klass.expand("2001:0db8::cd3")
152
+ end
153
+
154
+ def test_classmethod_compress
155
+ compressed = "2001:db8:0:cd30::"
156
+ expanded = "2001:0db8:0000:cd30:0000:0000:0000:0000"
157
+ assert_equal compressed, @klass.compress(expanded)
158
+ assert_not_equal compressed, @klass.compress("2001:0db8:0:cd3")
159
+ assert_not_equal compressed, @klass.compress("2001:0db8::cd30")
160
+ assert_not_equal compressed, @klass.compress("2001:0db8::cd3")
161
+ end
162
+
163
+ # def test_classmethod_create_unpecified
164
+ # unspec = @klass.create_unspecified
165
+ # assert_equal "::", unspec.address
166
+ # assert_equal 128, unspec.prefix
167
+ # assert_equal true, unspec.unspecified?
168
+ # assert_instance_of @klass, unspec.class
169
+ # end
170
+
171
+ # def test_classmethod_create_loopback
172
+ # loopb = @klass.create_loopback
173
+ # assert_equal "::1", loopb.address
174
+ # assert_equal 128, loopb.prefix
175
+ # assert_equal true, loopb.loopback?
176
+ # assert_instance_of @klass, loopb.class
177
+ # end
178
+
179
+ def test_classmethod_parse_data
180
+ str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
181
+ ip = @klass.parse_data str
182
+ assert_instance_of @klass, ip
183
+ assert_equal "2001:0db8:0000:0000:0008:0800:200c:417a", ip.address
184
+ assert_equal "2001:db8::8:800:200c:417a/128", ip.to_s
185
+ end
186
+
187
+ def test_classhmethod_parse_u128
188
+ @valid_ipv6.each do |ip,num|
189
+ assert_equal @klass.new(ip).to_s, @klass.parse_u128(num).to_s
190
+ end
191
+ end
192
+
193
+ def test_classmethod_parse_hex
194
+ assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
195
+ end
196
+
197
+ end # class IPv4Test
198
+
199
+ class IPv6UnspecifiedTest < Test::Unit::TestCase
200
+
201
+ def setup
202
+ @klass = IPAddress::IPv6::Unspecified
203
+ @ip = @klass.new
204
+ @str = "::/128"
205
+ @string = "0000:0000:0000:0000:0000:0000:0000:0000/128"
206
+ @u128 = 0
207
+ @address = "::"
208
+ end
209
+
210
+ def test_initialize
211
+ assert_nothing_raised {@klass.new}
212
+ assert_instance_of @klass, @ip
213
+ end
214
+
215
+ def test_attributes
216
+ assert_equal @address, @ip.compressed
217
+ assert_equal 128, @ip.prefix
218
+ assert_equal true, @ip.unspecified?
219
+ assert_equal @str, @ip.to_s
220
+ assert_equal @string, @ip.to_string
221
+ assert_equal @u128, @ip.to_u128
222
+ end
223
+
224
+ end # class IPv6UnspecifiedTest
225
+
226
+
227
+ class IPv6LoopbackTest < Test::Unit::TestCase
228
+
229
+ def setup
230
+ @klass = IPAddress::IPv6::Loopback
231
+ @ip = @klass.new
232
+ @str = "::1/128"
233
+ @string = "0000:0000:0000:0000:0000:0000:0000:0001/128"
234
+ @u128 = 1
235
+ @address = "::1"
236
+ end
237
+
238
+ def test_initialize
239
+ assert_nothing_raised {@klass.new}
240
+ assert_instance_of @klass, @ip
241
+ end
242
+
243
+ def test_attributes
244
+ assert_equal @address, @ip.compressed
245
+ assert_equal 128, @ip.prefix
246
+ assert_equal true, @ip.loopback?
247
+ assert_equal @str, @ip.to_s
248
+ assert_equal @string, @ip.to_string
249
+ assert_equal @u128, @ip.to_u128
250
+ end
251
+
252
+ end # class IPv6LoopbackTest
253
+
254
+ class IPv6MappedTest < Test::Unit::TestCase
255
+
256
+ def setup
257
+ @klass = IPAddress::IPv6::Mapped
258
+ @ip = @klass.new("::172.16.10.1")
259
+ @str = "::ffff:172.16.10.1/128"
260
+ @string = "0000:0000:0000:0000:0000:ffff:ac10:0a01/128"
261
+ @u128 = 281473568475649
262
+ @address = "::ffff:ac10:a01"
263
+
264
+ @valid_mapped = {'::13.1.68.3' => 281470899930115,
265
+ '0:0:0:0:0:ffff:129.144.52.38' => 281472855454758,
266
+ '::ffff:129.144.52.38' => 281472855454758}
267
+ end
268
+
269
+ def test_initialize
270
+ assert_nothing_raised {@klass.new("::172.16.10.1")}
271
+ assert_instance_of @klass, @ip
272
+ @valid_mapped.each do |ip, u128|
273
+ assert_nothing_raised {@klass.new ip}
274
+ assert_equal u128, @klass.new(ip).to_u128
275
+ end
276
+ end
277
+
278
+ def test_attributes
279
+ assert_equal @address, @ip.compressed
280
+ assert_equal 128, @ip.prefix
281
+ assert_equal @str, @ip.to_s
282
+ assert_equal @string, @ip.to_string
283
+ assert_equal @u128, @ip.to_u128
284
+ end
285
+
286
+ def test_mapped?
287
+ assert_equal true, @ip.mapped?
288
+ end
289
+
290
+ end # class IPv6MappedTest
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+
3
+ class Prefix32Test < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @netmask8 = "255.0.0.0"
7
+ @netmask16 = "255.255.0.0"
8
+ @netmask24 = "255.255.255.0"
9
+ @netmask30 = "255.255.255.252"
10
+ @netmasks = [@netmask8,@netmask16,@netmask24,@netmask30]
11
+
12
+ @prefix_hash = {
13
+ "255.0.0.0" => 8,
14
+ "255.255.0.0" => 16,
15
+ "255.255.255.0" => 24,
16
+ "255.255.255.252" => 30}
17
+
18
+ @octets_hash = {
19
+ [255,0,0,0] => 8,
20
+ [255,255,0,0] => 16,
21
+ [255,255,255,0] => 24,
22
+ [255,255,255,252] => 30}
23
+
24
+ @u32_hash = {
25
+ 8 => 4278190080,
26
+ 16 => 4294901760,
27
+ 24 => 4294967040,
28
+ 30 => 4294967292}
29
+
30
+ @klass = IPAddress::Prefix32
31
+ end
32
+
33
+ def test_attributes
34
+ @prefix_hash.values.each do |num|
35
+ prefix = @klass.new(num)
36
+ assert_equal num, prefix.prefix
37
+ end
38
+ end
39
+
40
+ def test_parse_netmask
41
+ @prefix_hash.each do |netmask, num|
42
+ prefix = @klass.parse_netmask(netmask)
43
+ assert_equal num, prefix.prefix
44
+ end
45
+ end
46
+
47
+ def test_method_to_ip
48
+ @prefix_hash.each do |netmask, num|
49
+ prefix = @klass.new(num)
50
+ assert_equal netmask, prefix.to_ip
51
+ end
52
+ end
53
+
54
+ def test_method_to_s
55
+ prefix = @klass.new(8)
56
+ assert_equal "8", prefix.to_s
57
+ end
58
+
59
+ def test_method_bits
60
+ prefix = @klass.new(16)
61
+ str = "1"*16 + "0"*16
62
+ assert_equal str, prefix.bits
63
+ end
64
+
65
+ def test_method_to_u32
66
+ @u32_hash.each do |num,u32|
67
+ assert_equal u32, @klass.new(num).to_u32
68
+ end
69
+ end
70
+
71
+ def test_initialize
72
+ assert_raise (ArgumentError) do
73
+ @klass.new 33
74
+ end
75
+ assert_nothing_raised do
76
+ @klass.new 8
77
+ end
78
+ assert_instance_of @klass, @klass.new(8)
79
+ end
80
+
81
+ def test_method_octets
82
+ @octets_hash.each do |arr,pref|
83
+ prefix = @klass.new(pref)
84
+ assert_equal prefix.octets, arr
85
+ end
86
+ end
87
+
88
+ def test_method_brackets
89
+ @octets_hash.each do |arr,pref|
90
+ prefix = @klass.new(pref)
91
+ arr.each_with_index do |oct,index|
92
+ assert_equal prefix[index], oct
93
+ end
94
+ end
95
+ end
96
+
97
+ def test_method_hostmask
98
+ prefix = @klass.new(8)
99
+ assert_equal "0.255.255.255", prefix.hostmask
100
+ end
101
+
102
+ end # class Prefix32Test
103
+
104
+
105
+ class Prefix128Test < Test::Unit::TestCase
106
+
107
+ def setup
108
+ @u128_hash = {
109
+ 32 => 340282366841710300949110269838224261120,
110
+ 64 => 340282366920938463444927863358058659840,
111
+ 96 => 340282366920938463463374607427473244160,
112
+ 126 => 340282366920938463463374607431768211452}
113
+
114
+ @klass = IPAddress::Prefix128
115
+ end
116
+
117
+ def test_initialize
118
+ assert_raise (ArgumentError) do
119
+ @klass.new 129
120
+ end
121
+ assert_nothing_raised do
122
+ @klass.new 64
123
+ end
124
+ assert_instance_of @klass, @klass.new(64)
125
+ end
126
+
127
+ def test_method_bits
128
+ prefix = @klass.new(64)
129
+ str = "1"*64 + "0"*64
130
+ assert_equal str, prefix.bits
131
+ end
132
+
133
+ def test_method_to_u32
134
+ @u128_hash.each do |num,u128|
135
+ assert_equal u128, @klass.new(num).to_u128
136
+ end
137
+ end
138
+
139
+ end # class Prefix128Test
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class IPAddressTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @valid_ipv4 = "172.16.10.1/24"
7
+ @valid_ipv6 = "2001:db8::8:800:200c:417a/64"
8
+ @valid_mapped = "::13.1.68.3"
9
+
10
+ @invalid_ipv4 = "10.0.0.256"
11
+ @invalid_ipv6 = ":1:2:3:4:5:6:7"
12
+ @invalid_mapped = "::1:2.3.4"
13
+
14
+ @ipv4class = IPAddress::IPv4
15
+ @ipv6class = IPAddress::IPv6
16
+ @mappedclass = IPAddress::IPv6::Mapped
17
+
18
+ @method = Module.method("IPAddress")
19
+ end
20
+
21
+ def test_method_IPAddress
22
+ assert_nothing_raised {@method.call(@valid_ipv4)}
23
+ assert_nothing_raised {@method.call(@valid_ipv6)}
24
+ assert_nothing_raised {@method.call(@valid_mapped)}
25
+
26
+ assert_instance_of @ipv4class, @method.call(@valid_ipv4)
27
+ assert_instance_of @ipv6class, @method.call(@valid_ipv6)
28
+ assert_instance_of @mappedclass, @method.call(@valid_mapped)
29
+
30
+ assert_raise(ArgumentError) {@method.call(@invalid_ipv4)}
31
+ assert_raise(ArgumentError) {@method.call(@invalid_ipv6)}
32
+ assert_raise(ArgumentError) {@method.call(@invalid_mapped)}
33
+
34
+ end
35
+
36
+ end
37
+
38
+