Skip to content

Commit 5f8a1bd

Browse files
committed
[compat] Store#time= accepts numeric epoch seconds like CRuby
MRI time= uses NUM2LONG(rb_Integer(time)) which accepts Time, Integer, Float, or anything responding to #to_int
1 parent 2ed0a0c commit 5f8a1bd

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

src/main/java/org/jruby/ext/openssl/X509Store.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.jruby.RubyFixnum;
3838
import org.jruby.RubyModule;
3939
import org.jruby.RubyNumeric;
40+
import org.jruby.RubyTime;
4041
import org.jruby.RubyObject;
4142
import org.jruby.anno.JRubyMethod;
4243
import org.jruby.exceptions.RaiseException;
@@ -149,11 +150,20 @@ public IRubyObject set_trust(final IRubyObject arg) {
149150
}
150151

151152
@JRubyMethod(name = "time=")
152-
public IRubyObject set_time(final IRubyObject arg) {
153-
setInstanceVariable("@time", arg);
153+
public IRubyObject set_time(final ThreadContext context, final IRubyObject arg) {
154+
// match CRuby: NUM2LONG(rb_Integer(time)) — accepts Time, Integer, or
155+
// anything convertible via #to_int, and stores epoch seconds internally
156+
setInstanceVariable("@time", toTime(context, arg));
154157
return arg;
155158
}
156159

160+
static RubyTime toTime(final ThreadContext context, final IRubyObject arg) {
161+
if (arg instanceof RubyTime) return (RubyTime) arg;
162+
// numeric (epoch seconds) — convert via Time.at
163+
long epoch = RubyNumeric.num2long(arg.callMethod(context, "to_int"));
164+
return RubyTime.newTime(context.runtime, epoch * 1000);
165+
}
166+
157167
@JRubyMethod
158168
public IRubyObject add_path(final ThreadContext context, final IRubyObject arg) {
159169
warn(context, "WARNING: unimplemented method called: OpenSSL::X509::Store#add_path");

src/main/java/org/jruby/ext/openssl/X509StoreContext.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
144144
}
145145

146146
IRubyObject time = store.getInstanceVariables().getInstanceVariable("@time");
147-
if ( ! time.isNil() ) set_time(time);
147+
if ( ! time.isNil() ) set_time(context, time);
148148
IRubyObject verify_callback = store.verify_callback_internal();
149149
if (verify_callback != null) this.setInstanceVariable("@verify_callback", verify_callback);
150150
if (cert != null) this.setInstanceVariable("@cert", cert);
@@ -269,8 +269,9 @@ public IRubyObject set_trust(final ThreadContext context, final IRubyObject arg)
269269
}
270270

271271
@JRubyMethod(name = "time=")
272-
public IRubyObject set_time(IRubyObject arg) {
273-
storeContext.setTime( 0, ( (RubyTime) arg ).getJavaDate() );
272+
public IRubyObject set_time(final ThreadContext context, IRubyObject arg) {
273+
RubyTime time = X509Store.toTime(context, arg);
274+
storeContext.setTime( 0, time.getJavaDate() );
274275
return arg;
275276
}
276277

src/test/ruby/x509/test_x509store.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def test_add_file_raises_with_invalid_pem # GH-285
139139
assert_raise(OpenSSL::X509::StoreError) { store.add_file(invalid) }
140140
end
141141

142+
# CRuby's Store#time= accepts Time, Integer, and Float (epoch seconds).
143+
# Ported from CRuby's test/openssl/test_x509store.rb (time-based verification).
144+
def test_store_time_accepts_integer
145+
store = OpenSSL::X509::Store.new
146+
store.add_file @ca_cert
147+
# set time as integer epoch seconds ??? must not raise
148+
store.time = Time.now.to_i
149+
assert store.verify(@cert)
150+
end
151+
142152
# CRuby undefs initialize_copy, blocking both dup and clone
143153
def test_store_dup_clone_not_allowed
144154
store = OpenSSL::X509::Store.new

0 commit comments

Comments
 (0)