|
1 | 1 | package org.jruby.ext.openssl; |
2 | 2 |
|
3 | | -import java.io.IOException; |
4 | | -import java.io.ByteArrayOutputStream; |
5 | | -import java.io.InputStream; |
6 | 3 | import java.nio.ByteBuffer; |
7 | | -import java.nio.charset.StandardCharsets; |
8 | 4 |
|
9 | | -import org.jruby.Ruby; |
10 | 5 | import org.jruby.RubyArray; |
11 | 6 | import org.jruby.RubyFixnum; |
12 | 7 | import org.jruby.RubyInteger; |
13 | 8 | import org.jruby.RubyString; |
14 | 9 | import org.jruby.exceptions.RaiseException; |
15 | | -import org.jruby.runtime.ThreadContext; |
16 | 10 | import org.jruby.runtime.builtin.IRubyObject; |
17 | 11 |
|
18 | 12 | import org.junit.After; |
19 | 13 | import org.junit.Before; |
20 | 14 | import org.junit.Test; |
21 | 15 | import static org.junit.Assert.*; |
22 | 16 |
|
23 | | -public class SSLSocketTest { |
24 | | - |
25 | | - private Ruby runtime; |
| 17 | +public class SSLSocketTest extends OpenSSLHelper { |
26 | 18 |
|
27 | 19 | /** Loads the ssl_pair.rb script that creates a connected SSL socket pair. */ |
28 | 20 | private String start_ssl_server_rb() { return readResource("/start_ssl_server.rb"); } |
29 | 21 |
|
30 | 22 | @Before |
31 | | - public void setUp() { |
32 | | - runtime = Ruby.newInstance(); |
33 | | - // prepend lib/ so openssl.rb + jopenssl/ are loaded instead of the ones bundled in jruby-stdlib |
34 | | - String libDir = new java.io.File("lib").getAbsolutePath(); |
35 | | - runtime.evalScriptlet("$LOAD_PATH.unshift '" + libDir + "'"); |
36 | | - runtime.evalScriptlet("require 'openssl'"); |
| 23 | + public void setUp() throws Exception { |
| 24 | + setUpRuntime(); |
37 | 25 | } |
38 | 26 |
|
39 | 27 | @After |
40 | 28 | public void tearDown() { |
41 | | - if (runtime != null) { |
42 | | - runtime.tearDown(false); |
43 | | - runtime = null; |
44 | | - } |
| 29 | + tearDownRuntime(); |
45 | 30 | } |
46 | 31 |
|
47 | 32 | /** |
@@ -178,28 +163,10 @@ public void syswriteNonblockNetWriteDataConsistency() { |
178 | 163 | } |
179 | 164 | } |
180 | 165 |
|
181 | | - private ThreadContext currentContext() { |
182 | | - return runtime.getCurrentContext(); |
183 | | - } |
184 | | - |
185 | 166 | private void closeQuietly(final RubyArray sslPair) { |
186 | 167 | for (int i = 0; i < sslPair.getLength(); i++) { |
187 | 168 | try { sslPair.entry(i).callMethod(currentContext(), "close"); } |
188 | 169 | catch (RaiseException e) { /* already closed */ } |
189 | 170 | } |
190 | 171 | } |
191 | | - |
192 | | - static String readResource(final String resource) { |
193 | | - int n; |
194 | | - try (InputStream in = SSLSocketTest.class.getResourceAsStream(resource)) { |
195 | | - if (in == null) throw new IllegalArgumentException(resource + " not found on classpath"); |
196 | | - |
197 | | - ByteArrayOutputStream out = new ByteArrayOutputStream(); |
198 | | - byte[] buf = new byte[8192]; |
199 | | - while ((n = in.read(buf)) != -1) out.write(buf, 0, n); |
200 | | - return new String(out.toByteArray(), StandardCharsets.UTF_8); |
201 | | - } catch (IOException e) { |
202 | | - throw new IllegalStateException("failed to load" + resource, e); |
203 | | - } |
204 | | - } |
205 | 172 | } |
0 commit comments