Skip to content

Commit 493c28f

Browse files
committed
[test] struggling with JCE security restrictions once again due 8u112
1 parent d84bf06 commit 493c28f

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/test/ruby/test_helper.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,46 @@ def assert_nothing_raised
9595

9696
def self.disable_security_restrictions!; end # do nothing on MRI
9797

98+
@@security_restrictions = nil
99+
98100
def self.disable_security_restrictions!
99-
security_class = java.lang.Class.for_name('javax.crypto.JceSecurity')
100-
restricted_field = security_class.get_declared_field('isRestricted')
101+
jce_security_class = java.lang.Class.for_name('javax.crypto.JceSecurity')
102+
restricted_field = jce_security_class.getDeclaredField('isRestricted')
101103
restricted_field.accessible = true
102-
restricted_field.set nil, false; return true
104+
@@security_restrictions = restricted_field.getBoolean(nil)
105+
return false unless @@security_restrictions
106+
107+
if java.lang.reflect.Modifier.isFinal restricted_field.modifiers
108+
field_class = java.lang.Class.for_name('java.lang.reflect.Field')
109+
# NOTE: this no longer works since 8u111 as it's using unsafe :
110+
# Can not set static final boolean field javax.crypto.JceSecurity.isRestricted to (boolean)false
111+
# sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(sun/reflect/UnsafeFieldAccessorImpl.java:76)
112+
# sun.reflect.UnsafeFieldAccessorImpl.throwFinalFieldIllegalAccessException(sun/reflect/UnsafeFieldAccessorImpl.java:84)
113+
# sun.reflect.UnsafeQualifiedStaticBooleanFieldAccessorImpl.setBoolean(sun/reflect/UnsafeQualifiedStaticBooleanFieldAccessorImpl.java:93)
114+
# java.lang.reflect.Field.setBoolean(java/lang/reflect/Field.java:801)
115+
mods_field = field_class.getDeclaredField('modifiers')
116+
mods_field.accessible = true
117+
118+
# restricted_field = jce_security_class.getDeclaredField('isRestricted')
119+
# restricted_field.accessible = true
120+
mods_field.setInt restricted_field, (~java.lang.reflect.Modifier::FINAL & restricted_field.modifiers)
121+
end
122+
restricted_field.setBoolean nil, false; return true
103123
rescue Java::JavaLang::ClassNotFoundException => e
104124
warn "failed to disable JCE security restrictions: #{e.inspect}"; nil
105125
rescue Java::JavaLang::NoSuchFieldException => e # Java 6
106126
warn "failed to disable JCE security restrictions: #{e.inspect}"; nil
127+
rescue Java::JavaLang::IllegalAccessException => e
128+
warn "failed to disable JCE security restrictions: #{e.inspect}"; nil
107129
rescue NameError => e
108130
warn "failed to disable JCE security restrictions: #{e.inspect}"; nil
109131
end if defined? JRUBY_VERSION
110132

133+
def self.security_restrictions?
134+
disable_security_restrictions! if @@security_restrictions.nil?
135+
@@security_restrictions
136+
end
137+
111138
def self.java6?; java_version.last.to_i == 6 end
112139
def self.java7?; java_version.last.to_i == 7 end
113140
def self.java8?; java_version.last.to_i == 8 end

0 commit comments

Comments
 (0)