bterlson-httpclient 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_ssl.rb ADDED
@@ -0,0 +1,199 @@
1
+ require 'test/unit'
2
+ require 'httpclient'
3
+
4
+
5
+ class TestSSL < Test::Unit::TestCase
6
+ PORT = 17171
7
+ DIR = File.dirname(File.expand_path(__FILE__))
8
+ require 'rbconfig'
9
+ RUBY = File.join(
10
+ Config::CONFIG["bindir"],
11
+ Config::CONFIG["ruby_install_name"] + Config::CONFIG["EXEEXT"]
12
+ )
13
+
14
+ def setup
15
+ @url = "https://localhost:#{PORT}/hello"
16
+ @serverpid = @client = nil
17
+ @verify_callback_called = false
18
+ setup_server
19
+ setup_client
20
+ end
21
+
22
+ def teardown
23
+ teardown_client
24
+ teardown_server
25
+ end
26
+
27
+ def path(filename)
28
+ File.expand_path(filename, DIR)
29
+ end
30
+
31
+ def test_options
32
+ cfg = @client.ssl_config
33
+ assert_nil(cfg.client_cert)
34
+ assert_nil(cfg.client_key)
35
+ assert_nil(cfg.client_ca)
36
+ assert_equal(OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT, cfg.verify_mode)
37
+ assert_nil(cfg.verify_callback)
38
+ assert_nil(cfg.timeout)
39
+ assert_equal(OpenSSL::SSL::OP_ALL | OpenSSL::SSL::OP_NO_SSLv2, cfg.options)
40
+ assert_equal("ALL:!ADH:!LOW:!EXP:!MD5:+SSLv2:@STRENGTH", cfg.ciphers)
41
+ assert_instance_of(OpenSSL::X509::Store, cfg.cert_store)
42
+ end
43
+
44
+ def test_sync
45
+ cfg = @client.ssl_config
46
+ cfg.set_client_cert_file(path('client.cert'), path('client.key'))
47
+ cfg.set_trust_ca(path('ca.cert'))
48
+ cfg.set_trust_ca(path('subca.cert'))
49
+ assert_equal("hello", @client.get_content(@url))
50
+
51
+ @client.socket_sync = false
52
+ @client.reset_all
53
+ assert_equal("hello", @client.get_content(@url))
54
+ end
55
+
56
+ def test_debug_dev
57
+ str = @client.debug_dev = ''
58
+ cfg = @client.ssl_config
59
+ cfg.client_cert = cert("client.cert")
60
+ cfg.client_key = key("client.key")
61
+ cfg.set_trust_ca(path('ca.cert'))
62
+ cfg.set_trust_ca(path('subca.cert'))
63
+ assert_equal("hello", @client.get_content(@url))
64
+ assert(str.scan(/^hello$/)[0])
65
+ end
66
+
67
+ def test_verification
68
+ cfg = @client.ssl_config
69
+ cfg.verify_callback = method(:verify_callback).to_proc
70
+ begin
71
+ @verify_callback_called = false
72
+ @client.get(@url)
73
+ assert(false)
74
+ rescue OpenSSL::SSL::SSLError => ssle
75
+ assert_match(/certificate verify failed/, ssle.message)
76
+ assert(@verify_callback_called)
77
+ end
78
+ #
79
+ cfg.client_cert = cert("client.cert")
80
+ cfg.client_key = key("client.key")
81
+ @verify_callback_called = false
82
+ begin
83
+ @client.get(@url)
84
+ assert(false)
85
+ rescue OpenSSL::SSL::SSLError => ssle
86
+ assert_match(/certificate verify failed/, ssle.message)
87
+ assert(@verify_callback_called)
88
+ end
89
+ #
90
+ cfg.set_trust_ca(path('ca.cert'))
91
+ @verify_callback_called = false
92
+ begin
93
+ @client.get(@url)
94
+ assert(false)
95
+ rescue OpenSSL::SSL::SSLError => ssle
96
+ assert_match(/certificate verify failed/, ssle.message)
97
+ assert(@verify_callback_called)
98
+ end
99
+ #
100
+ cfg.set_trust_ca(path('subca.cert'))
101
+ @verify_callback_called = false
102
+ assert_equal("hello", @client.get_content(@url))
103
+ assert(@verify_callback_called)
104
+ #
105
+ cfg.verify_depth = 1
106
+ @verify_callback_called = false
107
+ begin
108
+ @client.get(@url)
109
+ assert(false)
110
+ rescue OpenSSL::SSL::SSLError => ssle
111
+ assert_match(/certificate verify failed/, ssle.message)
112
+ assert(@verify_callback_called)
113
+ end
114
+ #
115
+ cfg.verify_depth = nil
116
+ cfg.cert_store = OpenSSL::X509::Store.new
117
+ cfg.verify_mode = OpenSSL::SSL::VERIFY_PEER
118
+ begin
119
+ @client.get_content(@url)
120
+ assert(false)
121
+ rescue OpenSSL::SSL::SSLError => ssle
122
+ assert_match(/certificate verify failed/, ssle.message)
123
+ end
124
+ #
125
+ cfg.verify_mode = nil
126
+ assert_equal("hello", @client.get_content(@url))
127
+ end
128
+
129
+ def test_ciphers
130
+ cfg = @client.ssl_config
131
+ cfg.set_client_cert_file(path('client.cert'), path('client.key'))
132
+ cfg.set_trust_ca(path('ca.cert'))
133
+ cfg.set_trust_ca(path('subca.cert'))
134
+ cfg.timeout = 123
135
+ assert_equal("hello", @client.get_content(@url))
136
+ #
137
+ cfg.ciphers = "!ALL"
138
+ begin
139
+ @client.get(@url)
140
+ assert(false)
141
+ rescue OpenSSL::SSL::SSLError => ssle
142
+ assert_equal("SSL_CTX_set_cipher_list:: no cipher match", ssle.message)
143
+ end
144
+ #
145
+ cfg.ciphers = "ALL"
146
+ assert_equal("hello", @client.get_content(@url))
147
+ #
148
+ cfg.ciphers = "DEFAULT"
149
+ assert_equal("hello", @client.get_content(@url))
150
+ end
151
+
152
+ private
153
+
154
+ def cert(filename)
155
+ OpenSSL::X509::Certificate.new(File.open(File.join(DIR, filename)) { |f|
156
+ f.read
157
+ })
158
+ end
159
+
160
+ def key(filename)
161
+ OpenSSL::PKey::RSA.new(File.open(File.join(DIR, filename)) { |f|
162
+ f.read
163
+ })
164
+ end
165
+
166
+ def q(str)
167
+ %Q["#{str}"]
168
+ end
169
+
170
+ def setup_server
171
+ svrcmd = "#{q(RUBY)} "
172
+ svrcmd << "-d " if $DEBUG
173
+ svrcmd << File.join(DIR, "sslsvr.rb")
174
+ svrout = IO.popen(svrcmd)
175
+ @serverpid = Integer(svrout.gets.chomp)
176
+ end
177
+
178
+ def setup_client
179
+ @client = HTTPClient.new
180
+ @client.debug_dev = STDOUT if $DEBUG
181
+ end
182
+
183
+ def teardown_server
184
+ if @serverpid
185
+ Process.kill('INT', @serverpid)
186
+ Process.waitpid(@serverpid) rescue nil
187
+ end
188
+ end
189
+
190
+ def teardown_client
191
+ @client.reset_all if @client
192
+ end
193
+
194
+ def verify_callback(ok, cert)
195
+ @verify_callback_called = true
196
+ p ["client", ok, cert] if $DEBUG
197
+ ok
198
+ end
199
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bterlson-httpclient
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.4
5
+ platform: ruby
6
+ authors:
7
+ - NAKAMURA, Hiroshi
8
+ - Brian Terlson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-03-27 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: nahi@ruby-lang.org
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.txt
25
+ files:
26
+ - README.txt
27
+ - VERSION.yml
28
+ - lib/http-access2
29
+ - lib/http-access2/cookie.rb
30
+ - lib/http-access2/http.rb
31
+ - lib/http-access2.rb
32
+ - lib/httpclient
33
+ - lib/httpclient/auth.rb
34
+ - lib/httpclient/cacert.p7s
35
+ - lib/httpclient/connection.rb
36
+ - lib/httpclient/cookie.rb
37
+ - lib/httpclient/http.rb
38
+ - lib/httpclient/session.rb
39
+ - lib/httpclient/ssl_config.rb
40
+ - lib/httpclient/timeout.rb
41
+ - lib/httpclient/util.rb
42
+ - lib/httpclient.rb
43
+ - test/ca.cert
44
+ - test/client.cert
45
+ - test/client.key
46
+ - test/htdigest
47
+ - test/htpasswd
48
+ - test/runner.rb
49
+ - test/server.cert
50
+ - test/server.key
51
+ - test/sslsvr.rb
52
+ - test/subca.cert
53
+ - test/test_auth.rb
54
+ - test/test_cookie.rb
55
+ - test/test_http-access2.rb
56
+ - test/test_httpclient.rb
57
+ - test/test_ssl.rb
58
+ has_rdoc: true
59
+ homepage: http://dev.ctor.org/httpclient
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --inline-source
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ version:
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.2.0
82
+ signing_key:
83
+ specification_version: 2
84
+ summary: gives something like the functionality of libwww-perl (LWP) in Ruby
85
+ test_files: []
86
+