ronin-support 0.1.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (127) hide show
  1. data/.document +4 -0
  2. data/.rspec +1 -0
  3. data/.yardopts +1 -0
  4. data/COPYING.txt +504 -0
  5. data/ChangeLog.md +4 -0
  6. data/Gemfile +23 -0
  7. data/README.md +98 -0
  8. data/Rakefile +29 -0
  9. data/gemspec.yml +20 -0
  10. data/lib/ronin/extensions.rb +28 -0
  11. data/lib/ronin/extensions/file.rb +63 -0
  12. data/lib/ronin/extensions/ip_addr.rb +220 -0
  13. data/lib/ronin/extensions/kernel.rb +45 -0
  14. data/lib/ronin/extensions/meta.rb +22 -0
  15. data/lib/ronin/extensions/meta/object.rb +24 -0
  16. data/lib/ronin/extensions/string.rb +200 -0
  17. data/lib/ronin/formatting.rb +26 -0
  18. data/lib/ronin/formatting/binary.rb +22 -0
  19. data/lib/ronin/formatting/digest.rb +22 -0
  20. data/lib/ronin/formatting/extensions.rb +25 -0
  21. data/lib/ronin/formatting/extensions/binary.rb +24 -0
  22. data/lib/ronin/formatting/extensions/binary/file.rb +35 -0
  23. data/lib/ronin/formatting/extensions/binary/integer.rb +147 -0
  24. data/lib/ronin/formatting/extensions/binary/string.rb +363 -0
  25. data/lib/ronin/formatting/extensions/digest.rb +23 -0
  26. data/lib/ronin/formatting/extensions/digest/file.rb +117 -0
  27. data/lib/ronin/formatting/extensions/digest/string.rb +80 -0
  28. data/lib/ronin/formatting/extensions/http.rb +23 -0
  29. data/lib/ronin/formatting/extensions/http/integer.rb +57 -0
  30. data/lib/ronin/formatting/extensions/http/string.rb +102 -0
  31. data/lib/ronin/formatting/extensions/text.rb +23 -0
  32. data/lib/ronin/formatting/extensions/text/array.rb +125 -0
  33. data/lib/ronin/formatting/extensions/text/string.rb +206 -0
  34. data/lib/ronin/formatting/http.rb +22 -0
  35. data/lib/ronin/formatting/text.rb +22 -0
  36. data/lib/ronin/network.rb +29 -0
  37. data/lib/ronin/network/esmtp.rb +22 -0
  38. data/lib/ronin/network/extensions.rb +29 -0
  39. data/lib/ronin/network/extensions/esmtp.rb +22 -0
  40. data/lib/ronin/network/extensions/esmtp/net.rb +100 -0
  41. data/lib/ronin/network/extensions/http.rb +22 -0
  42. data/lib/ronin/network/extensions/http/net.rb +661 -0
  43. data/lib/ronin/network/extensions/imap.rb +22 -0
  44. data/lib/ronin/network/extensions/imap/net.rb +124 -0
  45. data/lib/ronin/network/extensions/pop3.rb +22 -0
  46. data/lib/ronin/network/extensions/pop3/net.rb +92 -0
  47. data/lib/ronin/network/extensions/smtp.rb +22 -0
  48. data/lib/ronin/network/extensions/smtp/net.rb +110 -0
  49. data/lib/ronin/network/extensions/ssl.rb +22 -0
  50. data/lib/ronin/network/extensions/ssl/net.rb +147 -0
  51. data/lib/ronin/network/extensions/tcp.rb +22 -0
  52. data/lib/ronin/network/extensions/tcp/net.rb +304 -0
  53. data/lib/ronin/network/extensions/telnet.rb +22 -0
  54. data/lib/ronin/network/extensions/telnet/net.rb +156 -0
  55. data/lib/ronin/network/extensions/udp.rb +22 -0
  56. data/lib/ronin/network/extensions/udp/net.rb +226 -0
  57. data/lib/ronin/network/http.rb +24 -0
  58. data/lib/ronin/network/http/exceptions.rb +22 -0
  59. data/lib/ronin/network/http/exceptions/unknown_request.rb +29 -0
  60. data/lib/ronin/network/http/http.rb +290 -0
  61. data/lib/ronin/network/http/proxy.rb +307 -0
  62. data/lib/ronin/network/imap.rb +49 -0
  63. data/lib/ronin/network/network.rb +41 -0
  64. data/lib/ronin/network/pop3.rb +49 -0
  65. data/lib/ronin/network/smtp.rb +24 -0
  66. data/lib/ronin/network/smtp/email.rb +143 -0
  67. data/lib/ronin/network/smtp/smtp.rb +68 -0
  68. data/lib/ronin/network/ssl.rb +47 -0
  69. data/lib/ronin/network/tcp.rb +22 -0
  70. data/lib/ronin/network/telnet.rb +109 -0
  71. data/lib/ronin/network/udp.rb +22 -0
  72. data/lib/ronin/path.rb +132 -0
  73. data/lib/ronin/support.rb +28 -0
  74. data/lib/ronin/support/inflector.rb +40 -0
  75. data/lib/ronin/support/version.rb +27 -0
  76. data/lib/ronin/templates.rb +23 -0
  77. data/lib/ronin/templates/erb.rb +75 -0
  78. data/lib/ronin/templates/template.rb +161 -0
  79. data/ronin-support.gemspec +10 -0
  80. data/spec/extensions/file_spec.rb +24 -0
  81. data/spec/extensions/ip_addr_spec.rb +171 -0
  82. data/spec/extensions/kernel_spec.rb +30 -0
  83. data/spec/extensions/string_spec.rb +177 -0
  84. data/spec/formatting/binary/helpers/hexdumps.rb +16 -0
  85. data/spec/formatting/binary/helpers/hexdumps/ascii.bin +0 -0
  86. data/spec/formatting/binary/helpers/hexdumps/hexdump_decimal_shorts.txt +17 -0
  87. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_bytes.txt +17 -0
  88. data/spec/formatting/binary/helpers/hexdumps/hexdump_hex_shorts.txt +17 -0
  89. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_bytes.txt +17 -0
  90. data/spec/formatting/binary/helpers/hexdumps/hexdump_octal_shorts.txt +17 -0
  91. data/spec/formatting/binary/helpers/hexdumps/hexdump_repeated.txt +6 -0
  92. data/spec/formatting/binary/helpers/hexdumps/od_decimal_bytes.txt +17 -0
  93. data/spec/formatting/binary/helpers/hexdumps/od_decimal_ints.txt +17 -0
  94. data/spec/formatting/binary/helpers/hexdumps/od_decimal_quads.txt +17 -0
  95. data/spec/formatting/binary/helpers/hexdumps/od_decimal_shorts.txt +17 -0
  96. data/spec/formatting/binary/helpers/hexdumps/od_hex_bytes.txt +17 -0
  97. data/spec/formatting/binary/helpers/hexdumps/od_hex_ints.txt +17 -0
  98. data/spec/formatting/binary/helpers/hexdumps/od_hex_quads.txt +17 -0
  99. data/spec/formatting/binary/helpers/hexdumps/od_hex_shorts.txt +17 -0
  100. data/spec/formatting/binary/helpers/hexdumps/od_octal_bytes.txt +17 -0
  101. data/spec/formatting/binary/helpers/hexdumps/od_octal_ints.txt +17 -0
  102. data/spec/formatting/binary/helpers/hexdumps/od_octal_quads.txt +17 -0
  103. data/spec/formatting/binary/helpers/hexdumps/od_octal_shorts.txt +17 -0
  104. data/spec/formatting/binary/helpers/hexdumps/od_repeated.txt +6 -0
  105. data/spec/formatting/binary/helpers/hexdumps/repeated.bin +1 -0
  106. data/spec/formatting/binary/integer_spec.rb +140 -0
  107. data/spec/formatting/binary/string_spec.rb +306 -0
  108. data/spec/formatting/digest/string_spec.rb +82 -0
  109. data/spec/formatting/http/integer_spec.rb +42 -0
  110. data/spec/formatting/http/string_spec.rb +76 -0
  111. data/spec/formatting/text/array_spec.rb +105 -0
  112. data/spec/formatting/text/string_spec.rb +180 -0
  113. data/spec/network/http/http_spec.rb +280 -0
  114. data/spec/network/http/proxy_spec.rb +150 -0
  115. data/spec/network/network_spec.rb +8 -0
  116. data/spec/network/ssl_spec.rb +14 -0
  117. data/spec/path_spec.rb +84 -0
  118. data/spec/spec_helper.rb +4 -0
  119. data/spec/support_spec.rb +8 -0
  120. data/spec/templates/classes/example_erb.rb +11 -0
  121. data/spec/templates/classes/example_template.rb +35 -0
  122. data/spec/templates/erb_spec.rb +21 -0
  123. data/spec/templates/helpers/data.rb +9 -0
  124. data/spec/templates/helpers/data/includes/_relative.erb +1 -0
  125. data/spec/templates/helpers/data/templates/example.erb +1 -0
  126. data/spec/templates/template_spec.rb +54 -0
  127. metadata +286 -0
@@ -0,0 +1,28 @@
1
+ #
2
+ # Ronin Support - A support library for Ronin.
3
+ #
4
+ # Copyright (c) 2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/extensions'
23
+ require 'ronin/formatting'
24
+ require 'ronin/network'
25
+ require 'ronin/path'
26
+ require 'ronin/templates'
27
+ require 'ronin/support/inflector'
28
+ require 'ronin/support/version'
@@ -0,0 +1,40 @@
1
+ #
2
+ # Ronin Support - A support library for Ronin.
3
+ #
4
+ # Copyright (c) 2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This program is free software; you can redistribute it and/or modify
7
+ # it under the terms of the GNU General Public License as published by
8
+ # the Free Software Foundation; either version 2 of the License, or
9
+ # (at your option) any later version.
10
+ #
11
+ # This program is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ # GNU General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU General Public License
17
+ # along with this program; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
+ #
20
+
21
+ begin
22
+ require 'active_support/inflector'
23
+ rescue LoadError
24
+ begin
25
+ require 'extlib/inflection'
26
+ rescue LoadError
27
+ raise(LoadError,"unable to load 'active_support/inflector' or 'extlib/inflection'",caller)
28
+ end
29
+ end
30
+
31
+ module Ronin
32
+ module Support
33
+ # The inflector that Ronin will use.
34
+ Inflector = if Object.const_defined?(:ActiveSupport)
35
+ ActiveSupport::Inflector
36
+ else
37
+ Extlib::Inflection
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # Ronin Support - A support library for Ronin.
3
+ #
4
+ # Copyright (c) 2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ module Ronin
23
+ module Support
24
+ # ronin-support version
25
+ VERSION = '0.1.0.pre1'
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/templates/template'
23
+ require 'ronin/templates/erb'
@@ -0,0 +1,75 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'ronin/templates/template'
23
+
24
+ require 'erb'
25
+
26
+ module Ronin
27
+ module Templates
28
+ #
29
+ # The {Erb} module uses the {Template} module to find and render
30
+ # Embedded Ruby (ERb) templates.
31
+ #
32
+ module Erb
33
+ include Template
34
+ #
35
+ # Renders the inline ERB template in the scope of the object.
36
+ #
37
+ # @param [String] template
38
+ # Source of the ERB template.
39
+ #
40
+ # @return [String]
41
+ # Result of the rendered template.
42
+ #
43
+ # @example
44
+ # @user = 'lolcats'
45
+ #
46
+ # erb %{
47
+ # USER: <%= @user %>
48
+ # PASSWORD: <%= @user.reverse %>
49
+ # }
50
+ # # => "\nUSER: lolcats\nPASSWORD: staclol\n"
51
+ #
52
+ def erb(template)
53
+ ERB.new(template).result(binding)
54
+ end
55
+
56
+ #
57
+ # Renders an ERB template file in the scope of the object.
58
+ #
59
+ # @param [String] template_path
60
+ # The relative path of the ERB template.
61
+ #
62
+ # @return [String]
63
+ # Result of the rendered template.
64
+ #
65
+ # @example
66
+ # erb_file 'path/to/template.erb'
67
+ #
68
+ def erb_file(template_path)
69
+ read_template(template_path) do |template|
70
+ erb(template)
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,161 @@
1
+ #
2
+ # Ronin - A Ruby platform for exploit development and security research.
3
+ #
4
+ # Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
+ # Boston, MA 02110-1301 USA
20
+ #
21
+
22
+ require 'data_paths/finders'
23
+
24
+ module Ronin
25
+ module Templates
26
+ #
27
+ # The {Template} module allows a class to find templates and manage
28
+ # multiple template directories. The {Template} module also can find
29
+ # templates within `data/` directories using `DataPaths::Finders`.
30
+ #
31
+ module Template
32
+ include DataPaths::Finders
33
+
34
+ protected
35
+
36
+ #
37
+ # A stack of directories to search for other templates within.
38
+ #
39
+ # @return [Array]
40
+ # The stack of directory paths.
41
+ #
42
+ def template_dirs
43
+ @template_dirs ||= []
44
+ end
45
+
46
+ #
47
+ # The first path in {#template_dirs}, that will be used to search for
48
+ # other templates in.
49
+ #
50
+ # @return [String, nil]
51
+ # Returns the first path in template_dirs, or `nil` if
52
+ # template_dirs is empty.
53
+ #
54
+ def template_dir
55
+ template_dirs.first
56
+ end
57
+
58
+ #
59
+ # Finds the template within the {#template_dir} or uses
60
+ # `DataPaths::Finders#find_data_file` to search through all
61
+ # `data/` directories for the template.
62
+ #
63
+ # @param [String] sub_path
64
+ # The relative path of the template to find.
65
+ #
66
+ # @return [String, nil]
67
+ # Returns the absolute path to the template, or `nil` if the
68
+ # template could not be found.
69
+ #
70
+ # @example
71
+ # find_template 'sub/path/template.erb'
72
+ #
73
+ def find_template(sub_path)
74
+ sub_path = sub_path.to_s
75
+
76
+ if template_dir
77
+ path = File.expand_path(File.join(template_dir,sub_path))
78
+
79
+ return path if File.file?(path)
80
+ end
81
+
82
+ return find_data_file(sub_path)
83
+ end
84
+
85
+ #
86
+ # Finds the template, pushing the directory that the template resides
87
+ # within to {#template_dirs}, calls the given block and then pops
88
+ # the directory off of {#template_dirs}.
89
+ #
90
+ # @param [String] sub_path
91
+ # The relative path of the template to find.
92
+ #
93
+ # @yield [path]
94
+ # The block to be called after the directory of the template has
95
+ # been pushed onto {#template_dirs}. After the block has returned,
96
+ # the directory will be popped off of {#template_dirs}.
97
+ #
98
+ # @yieldparam [String] path
99
+ # The absolute path of the template.
100
+ #
101
+ # @return [Object]
102
+ # Result of the given block.
103
+ #
104
+ # @raise [RuntimeError]
105
+ # The template was not found.
106
+ #
107
+ # @example
108
+ # enter_template('sub/path/template.erb') do |path|
109
+ # # do stuff with the full path
110
+ # end
111
+ #
112
+ def enter_template(sub_path)
113
+ sub_path = sub_path.to_s
114
+
115
+ unless (path = find_template(sub_path))
116
+ raise(RuntimeError,"could not find template #{sub_path.dump}")
117
+ end
118
+
119
+ template_dirs.unshift(File.dirname(path))
120
+
121
+ result = yield(path)
122
+
123
+ template_dirs.shift
124
+ return result
125
+ end
126
+
127
+ #
128
+ # Finds and reads the contents of a template.
129
+ #
130
+ # @param [String] template_path
131
+ # The relative path to the template.
132
+ #
133
+ # @yield [template]
134
+ # The given block will receive the contents of the template.
135
+ #
136
+ # @yieldparam [String] template
137
+ # The contents of the template.
138
+ #
139
+ # @example
140
+ # read_template 'path/to/_include.txt'
141
+ # # => "..."
142
+ #
143
+ # @example Calling read_template with a block
144
+ # read_template 'path/to/_include.txt' do |contents|
145
+ # # ...
146
+ # end
147
+ #
148
+ def read_template(template_path)
149
+ enter_template(template_path) do |path|
150
+ contents = File.read(path)
151
+
152
+ if block_given?
153
+ yield(contents)
154
+ else
155
+ contents
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ begin
4
+ Ore::Specification.new do |gemspec|
5
+ # custom logic here
6
+ end
7
+ rescue NameError
8
+ STDERR.puts "The 'ronin-support.gemspec' file requires Ore."
9
+ STDERR.puts "Run `gem install ore-core` to install Ore."
10
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ require 'ronin/extensions/file'
3
+
4
+ describe File do
5
+ subject { File }
6
+
7
+ it "should provide File.escape_path" do
8
+ subject.should respond_to(:escape_path)
9
+ end
10
+
11
+ describe "escape_path" do
12
+ it "should remove null-bytes" do
13
+ File.escape_path("hello\0world\0").should == "helloworld"
14
+ end
15
+
16
+ it "should escape home-dir expansions" do
17
+ File.escape_path("hello/~world").should == "hello/\\~world"
18
+ end
19
+
20
+ it "should remove '.' and '..' directories" do
21
+ File.escape_path("hello/./../world").should == "world"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,171 @@
1
+ require 'spec_helper'
2
+ require 'ronin/extensions/ip_addr'
3
+
4
+ describe IPAddr do
5
+ describe "extract" do
6
+ context "ipv4" do
7
+ it "should extract a single IPv4 address" do
8
+ addr = '127.0.0.1'
9
+
10
+ IPAddr.extract(addr,:ipv4).should == [addr]
11
+ end
12
+
13
+ it "should extract multiple IPv4 addresses" do
14
+ addrs = %w[127.0.0.1 192.168.0.1]
15
+ text = "#{addrs[0]} #{addrs[1]}"
16
+
17
+ IPAddr.extract(text,:ipv4).should == addrs
18
+ end
19
+
20
+ it "should extract IPv4 addresses from text" do
21
+ addr = '127.0.0.1'
22
+ text = "ip (#{addr})"
23
+
24
+ IPAddr.extract(text,:ipv4).should == [addr]
25
+ end
26
+
27
+ it "should ignore leading periods" do
28
+ addr = '127.0.0.1'
29
+ text = ".#{addr}"
30
+
31
+ IPAddr.extract(text,:ipv4).should == [addr]
32
+ end
33
+
34
+ it "should ignore tailing periods" do
35
+ addr = '127.0.0.1'
36
+ text = "#{addr}."
37
+
38
+ IPAddr.extract(text,:ipv4).should == [addr]
39
+ end
40
+
41
+ it "should ignore less than 3 octet IPv4 addresses" do
42
+ text = '127.0.0. 1'
43
+
44
+ IPAddr.extract(text,:ipv4).should be_empty
45
+ end
46
+
47
+ it "should ignore IPv4 addresses with more than 3 diget octets" do
48
+ text = '127.1111.0.1'
49
+
50
+ IPAddr.extract(text,:ipv4).should be_empty
51
+ end
52
+ end
53
+
54
+ context "ipv6" do
55
+ it "should extract a single IPv6 address" do
56
+ addr = 'fe80:0000:0000:0000:0204:61ff:fe9d:f156'
57
+
58
+ IPAddr.extract(addr,:ipv6).should == [addr]
59
+ end
60
+
61
+ it "should extract multiple IPv6 addresses" do
62
+ addrs = %w[::1 fe80:0000:0000:0000:0204:61ff:fe9d:f156]
63
+ text = "#{addrs[0]} #{addrs[1]}"
64
+
65
+ IPAddr.extract(text,:ipv6).should == addrs
66
+ end
67
+
68
+ it "should extract collapsed IPv6 addresses" do
69
+ addr = 'fe80::0204:61ff:fe9d:f156'
70
+ text = ":: #{addr} ::"
71
+
72
+ IPAddr.extract(text,:ipv6).should == [addr]
73
+ end
74
+
75
+ it "should extract IPv6 addresses from text" do
76
+ addr = 'fe80:0000:0000:0000:0204:61ff:fe9d:f156'
77
+ text = "hello #{addr} world"
78
+
79
+ IPAddr.extract(text,:ipv6).should == [addr]
80
+ end
81
+
82
+ it "should extract trailing IPv4 suffixes" do
83
+ addr = 'fe80:0000:0000:0000:0204:61ff:fe9d:f156:222.1.1.1'
84
+ text = "#{addr} 1.1.1.1"
85
+
86
+ IPAddr.extract(text,:ipv6).should == [addr]
87
+ end
88
+
89
+ it "should extract short-hand IPv6 addresses" do
90
+ addr = '::f0:0d'
91
+ text = "ipv6: #{addr}"
92
+
93
+ IPAddr.extract(text,:ipv6).should == [addr]
94
+ end
95
+ end
96
+
97
+ it "should extract both IPv4 and IPv6 addresses" do
98
+ ipv4 = '127.0.0.1'
99
+ ipv6 = '::1'
100
+ text = "ipv4: #{ipv4}, ipv6: #{ipv6}"
101
+
102
+ IPAddr.extract(text).should == [ipv4, ipv6]
103
+ end
104
+
105
+ it "should ignore non-IP addresses" do
106
+ text = 'one :: two.three.'
107
+
108
+ IPAddr.extract(text).should be_empty
109
+ end
110
+ end
111
+
112
+ describe "CIDR addresses" do
113
+ let(:fixed_addr) { IPAddr.new('10.1.1.2') }
114
+ let(:class_c) { IPAddr.new('10.1.1.2/24') }
115
+
116
+ it "should only iterate over one IP address for an address" do
117
+ addresses = fixed_addr.map { |ip| IPAddr.new(ip) }
118
+
119
+ addresses.length.should == 1
120
+ fixed_addr.should include(addresses.first)
121
+ end
122
+
123
+ it "should iterate over all IP addresses contained within the IP range" do
124
+ class_c.each do |ip|
125
+ class_c.should include(IPAddr.new(ip))
126
+ end
127
+ end
128
+
129
+ it "should return an Enumerator when no block is given" do
130
+ class_c.each.all? { |ip|
131
+ class_c.include?(IPAddr.new(ip))
132
+ }.should == true
133
+ end
134
+ end
135
+
136
+ describe "globbed addresses" do
137
+ let(:ipv4_range) { '10.1.1-5.1' }
138
+ let(:ipv6_range) { '::ff::02-0a::c3' }
139
+
140
+ it "should iterate over all IP addresses in an IPv4 range" do
141
+ IPAddr.each(ipv4_range) do |ip|
142
+ ip.should =~ /^10\.1\.[1-5]\.1$/
143
+ end
144
+ end
145
+
146
+ it "should iterate over all IP addresses in an IPv6 range" do
147
+ IPAddr.each(ipv6_range) do |ip|
148
+ ip.should =~ /^::ff::0[2-9a]::c3$/
149
+ end
150
+ end
151
+
152
+ it "should return an Enumerator when no block is given" do
153
+ ips = IPAddr.each(ipv4_range)
154
+
155
+ ips.all? { |ip| ip =~ /^10\.1\.[1-5]\.1$/ }.should == true
156
+ end
157
+ end
158
+
159
+ let(:ip) { IPAddr.new(Resolv.getaddress('www.example.com')) }
160
+ let(:bad_ip) { IPAddr.new('0.0.0.0') }
161
+
162
+ describe "lookup" do
163
+ it "should lookup the host-name for an IP" do
164
+ ip.lookup.should == ['www.example.com']
165
+ end
166
+
167
+ it "should return an empty Array for unknown IP addresses" do
168
+ bad_ip.lookup.should be_empty
169
+ end
170
+ end
171
+ end