diff --git a/jpos/src/main/java/org/jpos/security/jceadapter/JCEHandler.java b/jpos/src/main/java/org/jpos/security/jceadapter/JCEHandler.java index 856d6c5e01..65b7ddcd75 100644 --- a/jpos/src/main/java/org/jpos/security/jceadapter/JCEHandler.java +++ b/jpos/src/main/java/org/jpos/security/jceadapter/JCEHandler.java @@ -21,8 +21,6 @@ import java.security.InvalidKeyException; import java.security.Key; import java.security.NoSuchAlgorithmException; -import java.security.Provider; -import java.security.Security; import java.security.spec.AlgorithmParameterSpec; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -60,34 +58,6 @@ public class JCEHandler { /** * The JCE provider */ - Provider provider = null; - - /** - * Registers the JCE provider whose name is providerName and sets it to be the only provider to be used in this instance of the - * JCEHandler class. - * - * @param jceProviderClassName - * Name of the JCE provider (e.g. "com.sun.crypto.provider.SunJCE" for Sun's implementation, or - * "org.bouncycastle.jce.provider.BouncyCastleProvider" for bouncycastle.org implementation) - * @throws JCEHandlerException - */ - public JCEHandler(String jceProviderClassName) throws JCEHandlerException { - try { - provider = (Provider) Class.forName(jceProviderClassName).newInstance(); - Security.addProvider(provider); - } catch (Exception e) { - throw new JCEHandlerException(e); - } - } - - /** - * Uses the JCE provider specified - * - * @param provider - */ - public JCEHandler(Provider provider) { - this.provider = provider; - } /** * Generates a clear DES (DESede) key @@ -102,9 +72,9 @@ public Key generateDESKey(short keyLength) throws JCEHandlerException { try { KeyGenerator k1; if (keyLength > SMAdapter.LENGTH_DES) { - k1 = KeyGenerator.getInstance(ALG_TRIPLE_DES, provider.getName()); + k1 = KeyGenerator.getInstance(ALG_TRIPLE_DES); } else { - k1 = KeyGenerator.getInstance(ALG_DES, provider.getName()); + k1 = KeyGenerator.getInstance(ALG_DES); } generatedClearKey = k1.generateKey(); /* @@ -306,7 +276,7 @@ byte[] doCryptStuff(byte[] data, Key key, int direction } AlgorithmParameterSpec aps = null; try { - Cipher c1 = Cipher.getInstance(transformation, provider.getName()); + Cipher c1 = Cipher.getInstance(transformation); if (cipherMode != CipherMode.ECB) aps = new IvParameterSpec(iv); c1.init(direction, key, aps); @@ -361,7 +331,7 @@ Mac assignMACEngine(MacEngineKey engine) throws JCEHandlerException { // Initalize new MAC engine and store them in macEngines cache Mac mac = null; try { - mac = Mac.getInstance(engine.getMacAlgorithm(), provider); + mac = Mac.getInstance(engine.getMacAlgorithm()); mac.init(engine.getMacKey()); } catch (NoSuchAlgorithmException e) { throw new JCEHandlerException(e); diff --git a/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java b/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java index 333d04c9a9..dbd29662a2 100644 --- a/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java +++ b/jpos/src/main/java/org/jpos/security/jceadapter/JCESecurityModule.java @@ -25,7 +25,6 @@ import org.jpos.iso.ISODate; import org.jpos.iso.ISOException; import org.jpos.iso.ISOUtil; -import org.jpos.q2.QClassLoader; import org.jpos.security.*; import org.jpos.util.LogEvent; import org.jpos.util.Logger; @@ -39,7 +38,6 @@ import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.InvocationTargetException; import java.nio.ByteBuffer; import java.security.*; import java.util.*; @@ -1826,16 +1824,14 @@ private void init (String jceProviderClassName, String lmkFile, boolean lmkRebui keyTypeToLMKIndex.put(SMAdapter.TYPE_RSA_SK, 0x00C); keyTypeToLMKIndex.put(SMAdapter.TYPE_HMAC, 0x10C); keyTypeToLMKIndex.put(SMAdapter.TYPE_RSA_PK, 0x00D); - Provider provider; + Provider provider = null; LogEvent evt = new LogEvent(this, "jce-provider"); try { - if (jceProviderClassName == null || jceProviderClassName.isEmpty()) { - provider = Security.getProvider("SunJCE"); - } else { + if (jceProviderClassName != null && !jceProviderClassName.isEmpty()) { provider = (Provider) Class.forName(jceProviderClassName).getDeclaredConstructor().newInstance(); + Security.addProvider(provider); + evt.addMessage("name", provider.getName()); } - Security.addProvider(provider); - evt.addMessage("name", provider.getName()); } catch (Exception e) { evt.addMessage(e); throw new SMException("Unable to load jce provider whose class name is: " @@ -1843,7 +1839,7 @@ private void init (String jceProviderClassName, String lmkFile, boolean lmkRebui } finally { Logger.log(evt); } - jceHandler = new JCEHandler(provider); + jceHandler = new JCEHandler(); if (lmkRebuild) { // Creat new LMK file evt = new LogEvent(this, "local-master-keys"); diff --git a/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java b/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java index eb3db2acc0..8b67fce403 100644 --- a/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java +++ b/jpos/src/test/java/org/jpos/iso/BaseChannelTest.java @@ -88,46 +88,21 @@ public class BaseChannelTest { @Test public void testAcceptThrowsNullPointerException() throws Throwable { - BaseChannel xMLChannel = new XMLChannel(new PostPackager()); + BaseChannel xMLChannel = new XMLChannel(new XMLPackager()); try { xMLChannel.accept(null); - fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.net.ServerSocket.accept()\" because \"s\" is null", ex.getMessage(), "ex.getMessage()"); - } - assertNull(((XMLChannel) xMLChannel).logger, "(XMLChannel) xMLChannel.logger"); - assertNull(((XMLChannel) xMLChannel).originalRealm, "(XMLChannel) xMLChannel.originalRealm"); - assertNull(((XMLChannel) xMLChannel).serverIn, "(XMLChannel) xMLChannel.serverIn"); - assertNull(((XMLChannel) xMLChannel).serverOut, "(XMLChannel) xMLChannel.serverOut"); - assertNull(xMLChannel.getSocket(), "(XMLChannel) xMLChannel.getSocket()"); - assertEquals(3, ((XMLChannel) xMLChannel).cnt.length, "(XMLChannel) xMLChannel.cnt.length"); - assertNull(((XMLChannel) xMLChannel).realm, "(XMLChannel) xMLChannel.realm"); - assertFalse(((XMLChannel) xMLChannel).usable, "(XMLChannel) xMLChannel.usable"); - } + fail("Expected SocketException to be thrown"); + } catch (NullPointerException ignored) {} } @Test - public void testAcceptThrowsSocketException() throws Throwable { + public void testAcceptThrowsSocketException2() throws Throwable { BaseChannel gZIPChannel = new GZIPChannel(); ServerSocket s = new ServerSocket(); try { gZIPChannel.accept(s); fail("Expected SocketException to be thrown"); - } catch (SocketException ex) { - assertEquals(SocketException.class, ex.getClass(), "ex.getClass()"); - assertNull(((GZIPChannel) gZIPChannel).logger, "(GZIPChannel) gZIPChannel.logger"); - assertNull(((GZIPChannel) gZIPChannel).originalRealm, "(GZIPChannel) gZIPChannel.originalRealm"); - assertNull(((GZIPChannel) gZIPChannel).serverIn, "(GZIPChannel) gZIPChannel.serverIn"); - assertNull(((GZIPChannel) gZIPChannel).serverOut, "(GZIPChannel) gZIPChannel.serverOut"); - assertNull(gZIPChannel.getSocket(), "(GZIPChannel) gZIPChannel.getSocket()"); - assertEquals(3, ((GZIPChannel) gZIPChannel).cnt.length, "(GZIPChannel) gZIPChannel.cnt.length"); - assertNull(((GZIPChannel) gZIPChannel).realm, "(GZIPChannel) gZIPChannel.realm"); - assertFalse(((GZIPChannel) gZIPChannel).usable, "(GZIPChannel) gZIPChannel.usable"); - assertFalse(s.isClosed(), "s.isClosed()"); - } + } catch (SocketException ignored) {} } @Test @@ -288,55 +263,25 @@ public void testApplyTimeout() throws Throwable { } @Test - public void testConnectThrowsNullPointerException() throws Throwable { + public void testConnectThrowsSocketException() throws Throwable { BaseChannel bASE24TCPChannel = new BASE24TCPChannel(); Socket socket = new Socket(); bASE24TCPChannel.setTimeout(-1); try { bASE24TCPChannel.connect(socket); - fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.net.InetAddress.getHostAddress()\" because the return value of \"java.net.Socket.getInetAddress()\" is null", ex.getMessage(), "ex.getMessage()"); - } - assertNull(((BASE24TCPChannel) bASE24TCPChannel).serverIn, "(BASE24TCPChannel) bASE24TCPChannel.serverIn"); - assertNull(((BASE24TCPChannel) bASE24TCPChannel).serverOut, "(BASE24TCPChannel) bASE24TCPChannel.serverOut"); - assertSame(socket, bASE24TCPChannel.getSocket(), "(BASE24TCPChannel) bASE24TCPChannel.getSocket()"); - assertEquals(3, ((BASE24TCPChannel) bASE24TCPChannel).cnt.length, "(BASE24TCPChannel) bASE24TCPChannel.cnt.length"); - assertFalse(((BASE24TCPChannel) bASE24TCPChannel).usable, "(BASE24TCPChannel) bASE24TCPChannel.usable"); - assertNull(((BASE24TCPChannel) bASE24TCPChannel).logger, "(BASE24TCPChannel) bASE24TCPChannel.logger"); - assertNull(((BASE24TCPChannel) bASE24TCPChannel).originalRealm, "(BASE24TCPChannel) bASE24TCPChannel.originalRealm"); - assertNull(((BASE24TCPChannel) bASE24TCPChannel).realm, "(BASE24TCPChannel) bASE24TCPChannel.realm"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + fail("Expected SocketException to be thrown"); + } catch (SocketException ignored) {} } @Test - public void testConnectThrowsNullPointerException1() throws Throwable { + public void testConnectSocketException1() throws Throwable { Socket socket = new Socket(Proxy.NO_PROXY); BaseChannel gZIPChannel = new GZIPChannel(); gZIPChannel.setTimeout(1); try { gZIPChannel.connect(socket); - fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.net.InetAddress.getHostAddress()\" because the return value of \"java.net.Socket.getInetAddress()\" is null", ex.getMessage(), "ex.getMessage()"); - } - assertNull(((GZIPChannel) gZIPChannel).serverIn, "(GZIPChannel) gZIPChannel.serverIn"); - assertNull(((GZIPChannel) gZIPChannel).serverOut, "(GZIPChannel) gZIPChannel.serverOut"); - assertSame(socket, gZIPChannel.getSocket(), "(GZIPChannel) gZIPChannel.getSocket()"); - assertEquals(3, ((GZIPChannel) gZIPChannel).cnt.length, "(GZIPChannel) gZIPChannel.cnt.length"); - assertFalse(((GZIPChannel) gZIPChannel).usable, "(GZIPChannel) gZIPChannel.usable"); - assertNull(((GZIPChannel) gZIPChannel).logger, "(GZIPChannel) gZIPChannel.logger"); - assertNull(((GZIPChannel) gZIPChannel).originalRealm, "(GZIPChannel) gZIPChannel.originalRealm"); - assertNull(((GZIPChannel) gZIPChannel).realm, "(GZIPChannel) gZIPChannel.realm"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + fail("Expected SocketException to be thrown"); + } catch (SocketException ignored) {} } @Test diff --git a/jpos/src/test/java/org/jpos/iso/ISOServerTest.java b/jpos/src/test/java/org/jpos/iso/ISOServerTest.java index cbd744a201..ccf6137cbb 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOServerTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOServerTest.java @@ -77,53 +77,57 @@ public void testSimultaneousConnections() throws ISOException, InterruptedExcept int runs = 1000; Recording recording = new Recording(Configuration.getConfiguration("default")); - recording.setMaxAge(Duration.ofSeconds(120)); + recording.setMaxAge(Duration.ofSeconds(300)); // jfr print --stack-depth 64 --events jdk.VirtualThreadPinned build/reports/isoserver.jfr Path outputPath = Paths.get("build/reports/isoserver.jfr"); recording.setDestination(outputPath); recording.start(); CSChannel channel = new CSChannel(); - channel.setTimeout(10000); + channel.setTimeout(30000); channel.setPackager(new ISO87BPackager()); - ISOServer server = new ISOServer(9999, channel, 1000); + ISOServer server = new ISOServer(9999, channel, runs+10); SimpleConfiguration cfg = new SimpleConfiguration(); - cfg.put("backlog", Integer.toString(runs)); + cfg.put("backlog", "100"); + cfg.put("connect-timeout", "60000"); server.setConfiguration(cfg); Logger logger = new Logger(); - logger.addListener (new SimpleLogListener()); + // logger.addListener (new SimpleLogListener()); server.setLogger(logger, "ISOServerTest"); server.addISORequestListener(new AutoResponder()); ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); executor.submit(server); + ISOUtil.sleep (5000L); CountDownLatch latch = new CountDownLatch(runs); - for (int i=0; i { try { CSChannel c = new CSChannel("localhost", 9999, new ISO87BPackager()); - c.setTimeout(5000); + c.setTimeout(60000); c.setLogger(logger, "test-client"); + c.setConfiguration(cfg); // we want a connect-timeout c.connect(); ISOMsg m = new ISOMsg("0800"); m.set(11, ISOUtil.zeropad(j+1, 6)); c.send (m); - ISOMsg r = c.receive(); + c.receive(); ISOUtil.sleep(5000L); c.disconnect(); } catch (Throwable t) { + fail ("%d: could not receive (%s)".formatted(j, t.getMessage())); throw new RuntimeException(t); } finally { latch.countDown(); } }); - // LockSupport.parkNanos(Duration.ofMillis(5).toNanos()); + LockSupport.parkNanos(Duration.ofMillis(4).toNanos()); } - latch.await(60, TimeUnit.SECONDS); + latch.await(300, TimeUnit.SECONDS); + ISOUtil.sleep (1000L); // let JFR catch-up with latests messages recording.dump(outputPath); recording.stop(); recording.close(); diff --git a/jpos/src/test/java/org/jpos/iso/channel/LogChannelTest.java b/jpos/src/test/java/org/jpos/iso/channel/LogChannelTest.java index 369494bc3a..310623b898 100644 --- a/jpos/src/test/java/org/jpos/iso/channel/LogChannelTest.java +++ b/jpos/src/test/java/org/jpos/iso/channel/LogChannelTest.java @@ -49,7 +49,6 @@ public void testConnectThrowsSocketException() throws Throwable { assertEquals(SocketException.class, ex.getClass(), "ex.getClass()"); assertEquals("org.jpos.iso.channel.LogChannel", logChannel.getOriginalRealm(), "logChannel.getOriginalRealm()"); assertEquals(3, logChannel.getCounters().length, "logChannel.getCounters().length"); - assertNull(logChannel.getRealm(), "logChannel.getRealm()"); assertSame(socket, logChannel.getSocket(), "logChannel.getSocket()"); assertFalse(logChannel.isConnected(), "logChannel.isConnected()"); assertNull(logChannel.getLogger(), "logChannel.getLogger()"); diff --git a/jpos/src/test/java/org/jpos/iso/channel/X25ChannelTest.java b/jpos/src/test/java/org/jpos/iso/channel/X25ChannelTest.java index b1aa4e0a0c..6ae17489a5 100644 --- a/jpos/src/test/java/org/jpos/iso/channel/X25ChannelTest.java +++ b/jpos/src/test/java/org/jpos/iso/channel/X25ChannelTest.java @@ -38,49 +38,25 @@ public class X25ChannelTest { @Test - public void testConnectThrowsNullPointerException() throws Throwable { + public void testConnectThrowsSocketException() throws Throwable { X25Channel x25Channel = new X25Channel("testX25ChannelHost", 100, new ISO87APackagerBBitmap()); x25Channel.setTimeout(1); Socket socket = new Socket(Proxy.NO_PROXY); try { x25Channel.connect(socket); fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.net.InetAddress.getHostAddress()\" because the return value of \"java.net.Socket.getInetAddress()\" is null", ex.getMessage(), "ex.getMessage()"); - } - assertNull(x25Channel.reader, "x25Channel.reader"); - assertEquals("org.jpos.iso.channel.X25Channel", x25Channel.getOriginalRealm(), "x25Channel.getOriginalRealm()"); - assertEquals(3, x25Channel.getCounters().length, "x25Channel.getCounters().length"); - assertNull(x25Channel.getRealm(), "x25Channel.getRealm()"); - assertSame(socket, x25Channel.getSocket(), "x25Channel.getSocket()"); - assertFalse(x25Channel.isConnected(), "x25Channel.isConnected()"); - assertNull(x25Channel.getLogger(), "x25Channel.getLogger()"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + } catch (SocketException ignored) {} } @Test - public void testConnectThrowsSocketException() throws Throwable { + public void testConnectThrowsSocketException1() throws Throwable { Socket socket = new Socket(); X25Channel x25Channel = new X25Channel(); socket.close(); try { x25Channel.connect(socket); fail("Expected SocketException to be thrown"); - } catch (SocketException ex) { - assertEquals(SocketException.class, ex.getClass(), "ex.getClass()"); - assertNull(x25Channel.reader, "x25Channel.reader"); - assertEquals("org.jpos.iso.channel.X25Channel", x25Channel.getOriginalRealm(), "x25Channel.getOriginalRealm()"); - assertEquals(3, x25Channel.getCounters().length, "x25Channel.getCounters().length"); - assertNull(x25Channel.getRealm(), "x25Channel.getRealm()"); - assertSame(socket, x25Channel.getSocket(), "x25Channel.getSocket()"); - assertFalse(x25Channel.isConnected(), "x25Channel.isConnected()"); - assertNull(x25Channel.getLogger(), "x25Channel.getLogger()"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + } catch (SocketException ignored) {} } @Test diff --git a/jpos/src/test/java/org/jpos/iso/channel/XMLChannelTest.java b/jpos/src/test/java/org/jpos/iso/channel/XMLChannelTest.java index f9646ae19e..9a42d162d6 100644 --- a/jpos/src/test/java/org/jpos/iso/channel/XMLChannelTest.java +++ b/jpos/src/test/java/org/jpos/iso/channel/XMLChannelTest.java @@ -41,74 +41,36 @@ public class XMLChannelTest { @Test - public void testConnectThrowsNullPointerException() throws Throwable { + public void testConnectThrowsSocketException() throws Throwable { XMLChannel xMLChannel = new XMLChannel(); xMLChannel.setTimeout(1); Socket socket = new Socket(); try { xMLChannel.connect(socket); fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.net.InetAddress.getHostAddress()\" because the return value of \"java.net.Socket.getInetAddress()\" is null", ex.getMessage(), "ex.getMessage()"); - } - assertNull(xMLChannel.getLogger(), "xMLChannel.getLogger()"); - assertEquals("org.jpos.iso.channel.XMLChannel", xMLChannel.getOriginalRealm(), "xMLChannel.getOriginalRealm()"); - assertNull(xMLChannel.reader, "xMLChannel.reader"); - assertSame(socket, xMLChannel.getSocket(), "xMLChannel.getSocket()"); - assertEquals(3, xMLChannel.getCounters().length, "xMLChannel.getCounters().length"); - assertNull(xMLChannel.getRealm(), "xMLChannel.getRealm()"); - assertFalse(xMLChannel.isConnected(), "xMLChannel.isConnected()"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + } catch (SocketException ignored) {} } @Test - public void testConnectThrowsNullPointerException1() throws Throwable { + public void testConnectThrowsSocketException1() throws Throwable { XMLChannel xMLChannel = new XMLChannel(); xMLChannel.setTimeout(-1); Socket socket = new Socket(Proxy.NO_PROXY); try { xMLChannel.connect(socket); fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.net.InetAddress.getHostAddress()\" because the return value of \"java.net.Socket.getInetAddress()\" is null", ex.getMessage(), "ex.getMessage()"); - } - assertNull(xMLChannel.getLogger(), "xMLChannel.getLogger()"); - assertEquals("org.jpos.iso.channel.XMLChannel", xMLChannel.getOriginalRealm(), "xMLChannel.getOriginalRealm()"); - assertNull(xMLChannel.reader, "xMLChannel.reader"); - assertSame(socket, xMLChannel.getSocket(), "xMLChannel.getSocket()"); - assertEquals(3, xMLChannel.getCounters().length, "xMLChannel.getCounters().length"); - assertNull(xMLChannel.getRealm(), "xMLChannel.getRealm()"); - assertFalse(xMLChannel.isConnected(), "xMLChannel.isConnected()"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + } catch (SocketException ignored) {} } @Test - public void testConnectThrowsSocketException() throws Throwable { + public void testConnectThrowsSocketException2() throws Throwable { XMLChannel xMLChannel = new XMLChannel(); Socket socket = new Socket(Proxy.NO_PROXY); socket.close(); try { xMLChannel.connect(socket); fail("Expected SocketException to be thrown"); - } catch (SocketException ex) { - assertEquals(SocketException.class, ex.getClass(), "ex.getClass()"); - assertNull(xMLChannel.getLogger(), "xMLChannel.getLogger()"); - assertEquals("org.jpos.iso.channel.XMLChannel", xMLChannel.getOriginalRealm(), "xMLChannel.getOriginalRealm()"); - assertNull(xMLChannel.reader, "xMLChannel.reader"); - assertSame(socket, xMLChannel.getSocket(), "xMLChannel.getSocket()"); - assertEquals(3, xMLChannel.getCounters().length, "xMLChannel.getCounters().length"); - assertNull(xMLChannel.getRealm(), "xMLChannel.getRealm()"); - assertFalse(xMLChannel.isConnected(), "xMLChannel.isConnected()"); - assertNull(socket.getChannel(), "socket.getChannel()"); - } + } catch (SocketException ignored) {} } @Test diff --git a/jpos/src/test/java/org/jpos/security/jceadapter/DUKPTTest.java b/jpos/src/test/java/org/jpos/security/jceadapter/DUKPTTest.java index 54665de623..401bcbfa19 100644 --- a/jpos/src/test/java/org/jpos/security/jceadapter/DUKPTTest.java +++ b/jpos/src/test/java/org/jpos/security/jceadapter/DUKPTTest.java @@ -69,7 +69,7 @@ public void setUp() throws Exception initKS(); } - // @Test + @Test public void test_DUKPT() throws Exception { test_DUKPT ("test-bdk", new KeySerialNumber (ISOUtil.hex2byte("FFFF9876543210E00002")), ISOUtil.hex2byte ("B76997F83C1479DB"), PAN); @@ -124,7 +124,7 @@ public void test_DUKPT() throws Exception test_DUKPT ("test-bdk", new KeySerialNumber ("9876543210", "00004", "00002"), ISOUtil.hex2byte ("AEF0F261B1222EB1"), PAN,true); } - // @Test + @Test public void test_dataEncrypt() throws Exception { SecureDESKey bdk = ks.getKey("test-bdk"); byte[] original = "The quick brown fox jumps over the lazy dog".getBytes(); diff --git a/jpos/src/test/java/org/jpos/security/jceadapter/JCEHandlerTest.java b/jpos/src/test/java/org/jpos/security/jceadapter/JCEHandlerTest.java index 825d404dfa..b4df05b0f4 100644 --- a/jpos/src/test/java/org/jpos/security/jceadapter/JCEHandlerTest.java +++ b/jpos/src/test/java/org/jpos/security/jceadapter/JCEHandlerTest.java @@ -44,30 +44,19 @@ public class JCEHandlerTest { @BeforeEach public void onSetup() { provider = mock(Provider.class); - jCEHandler = new JCEHandler(provider); + jCEHandler = new JCEHandler(); } @Test public void testConstructor() throws Throwable { assertEquals("NoPadding", JCEHandler.DES_NO_PADDING, "jCEHandler.desPadding"); assertEquals("ECB", JCEHandler.DES_MODE_ECB, "jCEHandler.desMode"); - assertSame(provider, jCEHandler.provider, "jCEHandler.provider"); } - @Test - public void testConstructorThrowsJCEHandlerException() throws Throwable { - try { - new JCEHandler("testJCEHandlerJceProviderClassName"); - fail("Expected JCEHandlerException to be thrown"); - } catch (JCEHandlerException ex) { - assertEquals("java.lang.ClassNotFoundException: testJCEHandlerJceProviderClassName", ex.getMessage(), "ex.getMessage()"); - assertEquals("testJCEHandlerJceProviderClassName", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } - } @Test public void testDecryptDataThrowsJCEHandlerException() throws Throwable { - jCEHandler = new JCEHandler((Provider) null); + jCEHandler = new JCEHandler(); byte[] clearKeyBytes = new byte[1]; Key key = jCEHandler.formDESKey((short) 192, clearKeyBytes); byte[] encryptedData = new byte[0]; @@ -75,35 +64,15 @@ public void testDecryptDataThrowsJCEHandlerException() throws Throwable { jCEHandler.decryptData(encryptedData, key); fail("Expected JCEHandlerException to be thrown"); } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } assertEquals("DESede", key.getAlgorithm(), "(SecretKeySpec) key.getAlgorithm()"); } } - @Test - public void testDecryptDataThrowsNullPointerException() throws Throwable { - try { - new JCEHandler((Provider) null).decryptData("testString".getBytes(), null); - fail("Expected NullPointerException to be thrown"); - } catch (NullPointerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertNull(ex.getMessage(), "ex.getMessage()"); - } else { - assertEquals("Cannot invoke \"java.security.Key.getAlgorithm()\" because \"key\" is null", ex.getMessage(), "ex.getMessage()"); - } - } - } @Test public void testDecryptDESKeyThrowsJCEHandlerException() throws Throwable { - jCEHandler = new JCEHandler((Provider) null); - JCEHandler jCEHandler2 = new JCEHandler((Provider) null); + jCEHandler = new JCEHandler(); + JCEHandler jCEHandler2 = new JCEHandler(); byte[] clearKeyBytes = new byte[1]; Key encryptingKey = jCEHandler2.formDESKey((short) 192, clearKeyBytes); byte[] encryptedDESKey = new byte[3]; @@ -111,13 +80,6 @@ public void testDecryptDESKeyThrowsJCEHandlerException() throws Throwable { jCEHandler.decryptDESKey((short) 100, encryptedDESKey, encryptingKey, true); fail("Expected JCEHandlerException to be thrown"); } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } assertEquals("DESede", encryptingKey.getAlgorithm(), "(SecretKeySpec) encryptingKey.getAlgorithm()"); } } @@ -126,7 +88,7 @@ public void testDecryptDESKeyThrowsJCEHandlerException() throws Throwable { public void testDecryptDESKeyThrowsNullPointerException() throws Throwable { byte[] encryptedDESKey = new byte[2]; try { - new JCEHandler((Provider) null).decryptDESKey((short) 100, encryptedDESKey, null, true); + new JCEHandler().decryptDESKey((short) 100, encryptedDESKey, null, true); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { if (isJavaVersionAtMost(JAVA_14)) { @@ -139,7 +101,7 @@ public void testDecryptDESKeyThrowsNullPointerException() throws Throwable { @Test public void testDoCryptStuffThrowsJCEHandlerException() throws Throwable { - jCEHandler = new JCEHandler((Provider) null); + jCEHandler = new JCEHandler(); byte[] bytes = new byte[1]; Key key = new SecretKeySpec(bytes, "testJCEHandlerParam2"); byte[] data = new byte[3]; @@ -147,13 +109,6 @@ public void testDoCryptStuffThrowsJCEHandlerException() throws Throwable { jCEHandler.doCryptStuff(data, key, 100); fail("Expected JCEHandlerException to be thrown"); } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } assertEquals("testJCEHandlerParam2", key.getAlgorithm(), "(SecretKeySpec) key.getAlgorithm()"); } } @@ -162,19 +117,12 @@ public void testDoCryptStuffThrowsJCEHandlerException() throws Throwable { public void testDoCryptStuffThrowsJCEHandlerException1() throws Throwable { byte[] clearKeyBytes = new byte[1]; Key key = jCEHandler.formDESKey((short) 192, clearKeyBytes); - JCEHandler jCEHandler2 = new JCEHandler((Provider) null); + JCEHandler jCEHandler2 = new JCEHandler(); byte[] data = new byte[3]; try { jCEHandler2.doCryptStuff(data, key, 100); fail("Expected JCEHandlerException to be thrown"); } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } assertEquals("DESede", key.getAlgorithm(), "(SecretKeySpec) key.getAlgorithm()"); } } @@ -196,8 +144,8 @@ public void testDoCryptStuffThrowsNullPointerException() throws Throwable { @Test public void testEncryptDataThrowsJCEHandlerException() throws Throwable { - jCEHandler = new JCEHandler((Provider) null); - JCEHandler jCEHandler2 = new JCEHandler((Provider) null); + jCEHandler = new JCEHandler(); + JCEHandler jCEHandler2 = new JCEHandler(); byte[] clearKeyBytes = new byte[1]; Key key = jCEHandler2.formDESKey((short) 192, clearKeyBytes); byte[] data = new byte[1]; @@ -205,13 +153,6 @@ public void testEncryptDataThrowsJCEHandlerException() throws Throwable { jCEHandler.encryptData(data, key); fail("Expected JCEHandlerException to be thrown"); } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } assertEquals("DESede", key.getAlgorithm(), "(SecretKeySpec) key.getAlgorithm()"); } } @@ -220,7 +161,7 @@ public void testEncryptDataThrowsJCEHandlerException() throws Throwable { public void testEncryptDataThrowsNullPointerException() throws Throwable { byte[] data = new byte[1]; try { - new JCEHandler((Provider) null).encryptData(data, null); + new JCEHandler().encryptData(data, null); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { if (isJavaVersionAtMost(JAVA_14)) { @@ -238,7 +179,7 @@ public void testEncryptDESKeyThrowsArrayIndexOutOfBoundsException() throws Throw byte[] bytes2 = new byte[1]; Key encryptingKey = new SecretKeySpec(bytes2, "testJCEHandlerParam2"); try { - new JCEHandler((Provider) null).encryptDESKey((short) 64, clearDESKey, encryptingKey); + new JCEHandler().encryptDESKey((short) 64, clearDESKey, encryptingKey); fail("Expected ArrayIndexOutOfBoundsException to be thrown"); } catch (ArrayIndexOutOfBoundsException ex) { if (isJavaVersionAtMost(JAVA_10)) { @@ -257,7 +198,7 @@ public void testEncryptDESKeyThrowsJCEHandlerException() throws Throwable { byte[] clearKeyBytes = new byte[1]; Key clearDESKey = jCEHandler.formDESKey((short) 192, clearKeyBytes); try { - new JCEHandler((Provider) null).encryptDESKey((short) 100, clearDESKey, clearDESKey); + new JCEHandler().encryptDESKey((short) 100, clearDESKey, clearDESKey); fail("Expected JCEHandlerException to be thrown"); } catch (JCEHandlerException ex) { assertEquals("Unsupported key length: 100 bits", ex.getMessage(), "ex.getMessage()"); @@ -271,7 +212,7 @@ public void testEncryptDESKeyThrowsNullPointerException() throws Throwable { byte[] bytes = new byte[1]; Key encryptingKey = new SecretKeySpec(bytes, "testJCEHandlerParam2"); try { - new JCEHandler((Provider) null).encryptDESKey((short) 100, null, encryptingKey); + new JCEHandler().encryptDESKey((short) 100, null, encryptingKey); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { if (isJavaVersionAtMost(JAVA_14)) { @@ -288,7 +229,7 @@ public void testEncryptDESKeyThrowsNullPointerException() throws Throwable { public void testExtractDESKeyMaterialThrowsArrayIndexOutOfBoundsException() throws Throwable { byte[] clearKeyBytes = new byte[1]; Key clearDESKey = jCEHandler.formDESKey((short) 192, clearKeyBytes); - JCEHandler jCEHandler2 = new JCEHandler((Provider) null); + JCEHandler jCEHandler2 = new JCEHandler(); try { jCEHandler2.extractDESKeyMaterial((short) 128, clearDESKey); fail("Expected ArrayIndexOutOfBoundsException to be thrown"); @@ -304,7 +245,7 @@ public void testExtractDESKeyMaterialThrowsArrayIndexOutOfBoundsException() thro @Test public void testExtractDESKeyMaterialThrowsJCEHandlerException() throws Throwable { - JCEHandler jCEHandler2 = new JCEHandler((Provider) null); + JCEHandler jCEHandler2 = new JCEHandler(); byte[] clearKeyBytes = new byte[1]; Key clearDESKey = jCEHandler.formDESKey((short) 192, clearKeyBytes); try { @@ -412,51 +353,19 @@ public void testFormDESKeyThrowsJCEHandlerException() throws Throwable { @Test public void testGenerateDESKeyThrowsJCEHandlerException() throws Throwable { try { - new JCEHandler((Provider) null).generateDESKey((short) 63); + new JCEHandler().generateDESKey((short) 63); fail("Expected JCEHandlerException to be thrown"); - } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } - } + } catch (JCEHandlerException ignored) {} } @Test public void testGenerateDESKeyThrowsJCEHandlerException1() throws Throwable { try { - new JCEHandler((Provider) null).generateDESKey((short) 64); - fail("Expected JCEHandlerException to be thrown"); - } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } - } - } - - @Test - public void testGenerateDESKeyThrowsJCEHandlerException2() throws Throwable { - try { - new JCEHandler((Provider) null).generateDESKey((short) 65); + new JCEHandler().generateDESKey((short) 65); fail("Expected JCEHandlerException to be thrown"); - } catch (JCEHandlerException ex) { - if (isJavaVersionAtMost(JAVA_14)) { - assertEquals("java.lang.NullPointerException", ex.getMessage(), "ex.getMessage()"); - assertNull(ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } else { - assertEquals("java.lang.NullPointerException: Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getMessage(), "ex.getMessage()"); - assertEquals("Cannot invoke \"java.security.Provider.getName()\" because \"this.provider\" is null", ex.getNested().getMessage(), "ex.getNested().getMessage()"); - } - } + } catch (JCEHandlerException ignored) {} } - + @Test public void testGetBytesLength() throws Throwable { jCEHandler.getBytesLength((short) 128); diff --git a/jpos/src/test/java/org/jpos/security/jceadapter/JCESecurityModuleTest.java b/jpos/src/test/java/org/jpos/security/jceadapter/JCESecurityModuleTest.java index 70cdb0e6a6..f68f0f8617 100644 --- a/jpos/src/test/java/org/jpos/security/jceadapter/JCESecurityModuleTest.java +++ b/jpos/src/test/java/org/jpos/security/jceadapter/JCESecurityModuleTest.java @@ -22,7 +22,6 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.*; -import static org.apache.commons.lang3.JavaVersion.JAVA_13; import static org.apache.commons.lang3.JavaVersion.JAVA_14; import static org.apache.commons.lang3.SystemUtils.isJavaVersionAtMost; @@ -953,7 +952,7 @@ public void testVerifyDCVVImplException1() throws Throwable { String serviceCode = "226"; String dcvv = "562"; byte[] atc = ISOUtil.hex2byte("3210"); - assertThrows(SMException.class, () -> { + assertThrows(IllegalArgumentException.class, () -> { jcesecmod.verifydCVV(accountNo, imkac, dcvv, expDate ,serviceCode, atc, MKDMethod.OPTION_A); }); @@ -966,7 +965,7 @@ public void testVerifyDCVVImplException2() throws Throwable { String serviceCode = "226"; String dcvv = "562"; byte[] atc = ISOUtil.hex2byte("3210"); - assertThrows(SMException.class, () -> { + assertThrows(IllegalArgumentException.class, () -> { jcesecmod.verifydCVV(accountNo, imkac, dcvv, expDate ,serviceCode, atc, MKDMethod.OPTION_A); });