forked from jruby/jruby-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_helper.rb
More file actions
235 lines (201 loc) · 7.01 KB
/
test_helper.rb
File metadata and controls
235 lines (201 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
require 'java' if defined? JRUBY_VERSION
if bc_version = ENV['BC_VERSION'] # && respond_to?(:require_jar)
require 'jar-dependencies'
require_jar 'org.bouncycastle', 'bcpkix-jdk15on', bc_version
require_jar 'org.bouncycastle', 'bcprov-jdk15on', bc_version
Jars.freeze_loading if defined? Jars.freeze_loading
puts Java::OrgBouncycastleJceProvider::BouncyCastleProvider.new.info if $VERBOSE
else
base_dir = File.expand_path('../../..', File.dirname(__FILE__))
jar = File.join(base_dir, 'lib/jopenssl.jar')
raise "jopenssl.jar jar not found" unless jar; $CLASSPATH << jar
jar = Dir[File.join(base_dir, 'lib/org/bouncycastle/**/bcprov-*.jar')].first
raise "bcprov jar not found" unless jar; $CLASSPATH << jar
jar = Dir[File.join(base_dir, 'lib/org/bouncycastle/**/bcpkix-*.jar')].first
raise "bcpkix jar not found" unless jar; $CLASSPATH << jar
end if defined? JRUBY_VERSION
begin
gem 'test-unit'
rescue LoadError
warn "gem 'test-unit' not available, will load built-in 'test/unit'"
end
begin
gem 'minitest'
require 'minitest/autorun'
# NOTE: deal with maven plugin 1.0.10 setting output (gone on minitest 5) :
if Minitest.const_defined?(:Unit) && ! defined?(Minitest::Unit.output)
Minitest::Unit.module_eval do
@@report_path = nil
def self.report_path; @@report_path end
def self.output=(report_path) # called by the runit-maven-plugin
Minitest.extensions << 'output' # add a plugin (MiniT calls #plugin_output_init)
@@report_path = report_path
end
end
Minitest.module_eval do
def self.plugin_output_init(options)
summary = self.reporter.reporters.find { |rr| rr.is_a?(Minitest::SummaryReporter) }
if summary && Minitest::Unit.report_path
summary = summary.dup
summary.io = Minitest::Unit.report_path
self.reporter.reporters << summary
end
end
end
end
rescue LoadError => e
warn "gem 'minitest' failed to load: #{e.inspect}"
end unless (Test::Unit::AutoRunner.respond_to?(:setup_option)) rescue true # runit rules
# @see https://github.com/jruby/jruby-maven-plugins/blob/master/runit-maven-plugin/src/main/java/de/saumya/mojo/runit/RunitMavenTestScriptFactory.java
if defined? Minitest::Test
TestCase = Minitest::Test
else
require 'test/unit'
TestCase = Test::Unit::TestCase
end
puts "#{__FILE__} using #{TestCase}" if $VERBOSE || defined?(REPORT_PATH)
class TestCase
def setup; require 'openssl' end
alias assert_raise assert_raises unless method_defined?(:assert_raise)
def assert_pkey_error(&block)
assert_raise_kind_of(OpenSSL::PKey::PKeyError, &block)
end
unless method_defined?(:skip)
if method_defined?(:omit)
alias skip omit
else
def skip(msg = nil)
warn "Skipped: #{caller[0]} #{msg}"
end
end
end
unless method_defined?(:assert_not_equal)
def assert_not_equal(expected, actual)
assert expected != actual, "expected: #{expected} to not equal: #{actual} but did"
end
end
unless method_defined?(:assert_nothing_raised)
def assert_nothing_raised
begin
yield
rescue => e
assert false, "unexpected error raised: #{e.inspect}"
end
end
end
unless method_defined?(:assert_not_same)
def assert_not_same(expected, actual)
assert ! expected.equal?(actual), "expected: #{expected} to be same as: #{actual} but did"
end
end
def self.java8?; java_version.last.to_i == 8 end
def self.java_version
return [] unless defined? JRUBY_VERSION
ENV_JAVA[ 'java.specification.version' ].split('.')
end
def self.jruby?; !! defined?(JRUBY_VERSION) end
def jruby?; self.class.jruby? end
private
def debug(msg); puts msg if $VERBOSE end
def issue_cert(dn, key, serial, extensions, issuer, issuer_key, not_before: nil, not_after: nil, digest: 'sha256')
cert = OpenSSL::X509::Certificate.new
issuer = cert unless issuer
issuer_key = key unless issuer_key
cert.version = 2
cert.serial = serial
cert.subject = dn
cert.issuer = issuer.subject
cert.public_key = key
now = Time.now
cert.not_before = not_before || now - 3600
cert.not_after = not_after || now + 3600
ef = OpenSSL::X509::ExtensionFactory.new
ef.subject_certificate = cert
ef.issuer_certificate = issuer
extensions.each do |oid, value, critical|
cert.add_extension ef.create_extension(oid, value, critical)
end
cert.sign(issuer_key, digest)
cert
end
def issue_crl(revoke_info, serial, lastup, nextup, extensions, issuer, issuer_key, digest)
crl = OpenSSL::X509::CRL.new
crl.issuer = issuer.subject
crl.version = 1
crl.last_update = lastup
crl.next_update = nextup
revoke_info.each{|rserial, time, reason_code|
revoked = OpenSSL::X509::Revoked.new
revoked.serial = rserial
revoked.time = time
enum = OpenSSL::ASN1::Enumerated(reason_code)
ext = OpenSSL::X509::Extension.new("CRLReason", enum)
revoked.add_extension(ext)
crl.add_revoked(revoked)
}
ef = OpenSSL::X509::ExtensionFactory.new
ef.issuer_certificate = issuer
ef.crl = crl
crlnum = OpenSSL::ASN1::Integer(serial)
crl.add_extension(OpenSSL::X509::Extension.new("crlNumber", crlnum))
extensions.each do |oid, value, critical|
crl.add_extension ef.create_extension(oid, value, critical)
end
crl.sign(issuer_key, digest)
crl
end
def get_subject_key_id(cert)
asn1_cert = OpenSSL::ASN1.decode(cert)
tbscert = asn1_cert.value[0]
pkinfo = tbscert.value[6]
publickey = pkinfo.value[1]
pkvalue = publickey.value
OpenSSL::Digest::SHA1.hexdigest(pkvalue).scan(/../).join(":").upcase
end
module Fixtures
module_function
def pkey(name)
OpenSSL::PKey.read(read_file("pkey", name))
end
def pkey_dh(name)
# DH parameters can be read by OpenSSL::PKey.read atm
OpenSSL::PKey::DH.new(read_file("pkey", name))
end
@@__fixtures_cache = {}
def read_file(category, name)
@@__fixtures_cache[ [category, name] ] ||=
File.read(File.join(File.dirname(__FILE__), "fixtures", category, name))
end
end
end
begin
gem 'mocha'
rescue LoadError => e
warn "#{e} to run all tests please `gem install mocha'"
else
begin
if defined? MiniTest
require 'mocha/mini_test'
else
require 'mocha/test_unit'
end
rescue LoadError => e
warn "current mocha version might not work (try `gem install mocha'): #{e}"
end
end
if defined? JRUBY_VERSION # make sure our OpenSSL lib gets used not JRuby's
unless ENV['BC_VERSION']
$LOAD_PATH.unshift(File.expand_path('../../../lib', File.dirname(__FILE__)))
end
end
if ENV['OPENSSL_TEST_SUITE'].to_s == 'true'
require 'jopenssl/load' if defined? JRUBY_VERSION
base = File.expand_path('../ossl', File.dirname(__FILE__))
if ( sub = RUBY_VERSION[0, 3] ) == '2.0'
sub = '1.9'
end
puts "loading (MRI) OpenSSL suite from: #{File.join(base, sub)}"
Dir.glob("#{File.join(base, sub)}/**/test_*.rb").each do |test|
require test
end
end