Skip to content

Commit e57f1b4

Browse files
karesclaude
andcommitted
[compat] Store#time= accepts numeric epoch seconds like CRuby
CRuby's time= uses NUM2LONG(rb_Integer(time)) which accepts Time, Integer, Float, or anything responding to #to_int. JRuby was storing the value as-is but StoreContext cast it to RubyTime, crashing on non-Time values. Now both Store#time= and StoreContext#time= convert via a shared toTime helper. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 5fa1868 commit e57f1b4

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;
@@ -148,11 +149,20 @@ public IRubyObject set_trust(final IRubyObject arg) {
148149
}
149150

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

159+
static RubyTime toTime(final ThreadContext context, final IRubyObject arg) {
160+
if (arg instanceof RubyTime) return (RubyTime) arg;
161+
// numeric (epoch seconds) — convert via Time.at
162+
long epoch = RubyNumeric.num2long(arg.callMethod(context, "to_int"));
163+
return RubyTime.newTime(context.runtime, epoch * 1000);
164+
}
165+
156166
@JRubyMethod
157167
public IRubyObject add_path(final ThreadContext context, final IRubyObject arg) {
158168
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
@@ -143,7 +143,7 @@ public IRubyObject initialize(final ThreadContext context, final IRubyObject[] a
143143
}
144144

145145
IRubyObject time = store.getInstanceVariables().getInstanceVariable("@time");
146-
if ( ! time.isNil() ) set_time(time);
146+
if ( ! time.isNil() ) set_time(context, time);
147147
IRubyObject verify_callback = store.verify_callback_internal();
148148
if (verify_callback != null) this.setInstanceVariable("@verify_callback", verify_callback);
149149
if (cert != null) this.setInstanceVariable("@cert", cert);
@@ -268,8 +268,9 @@ public IRubyObject set_trust(final ThreadContext context, final IRubyObject arg)
268268
}
269269

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

src/test/ruby/x509/test_x509store.rb

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

104+
# CRuby's Store#time= accepts Time, Integer, and Float (epoch seconds).
105+
# Ported from CRuby's test/openssl/test_x509store.rb (time-based verification).
106+
def test_store_time_accepts_integer
107+
store = OpenSSL::X509::Store.new
108+
store.add_file @ca_cert
109+
# set time as integer epoch seconds — must not raise
110+
store.time = Time.now.to_i
111+
assert store.verify(@cert)
112+
end
113+
104114
def test_use_non_existing_cert_file
105115
ENV['SSL_CERT_FILE'] = 'non-existing-file.crt'
106116
store = OpenSSL::X509::Store.new

0 commit comments

Comments
 (0)