You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by co...@apache.org on 2020/09/01 10:45:12 UTC

[ws-wss4j] branch master updated: Switching to use Junit's TempDir annotation in a few other places

This is an automated email from the ASF dual-hosted git repository.

coheigea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git


The following commit(s) were added to refs/heads/master by this push:
     new cdb6762  Switching to use Junit's TempDir annotation in a few other places
cdb6762 is described below

commit cdb67628499e7c83b3f74bbafbeae15ca60ed535
Author: Colm O hEigeartaigh <co...@apache.org>
AuthorDate: Tue Sep 1 11:44:45 2020 +0100

    Switching to use Junit's TempDir annotation in a few other places
---
 .../apache/wss4j/dom/common/SecurityTestUtil.java  | 42 ----------------------
 .../org/apache/wss4j/dom/message/ReplayTest.java   | 23 ++++++++----
 .../org/apache/wss4j/stax/test/ReplayTest.java     | 12 +++----
 .../apache/wss4j/stax/test/UsernameTokenTest.java  | 14 ++++----
 4 files changed, 28 insertions(+), 63 deletions(-)

diff --git a/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java b/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
deleted file mode 100644
index 1867781..0000000
--- a/ws-security-dom/src/test/java/org/apache/wss4j/dom/common/SecurityTestUtil.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.wss4j.dom.common;
-
-import java.io.File;
-import java.util.Random;
-
-import org.apache.wss4j.common.cache.EHCacheReplayCache;
-import org.apache.wss4j.common.cache.ReplayCache;
-import org.apache.wss4j.common.ext.WSSecurityException;
-
-/**
- * A utility class for security tests
- */
-public final class SecurityTestUtil {
-
-    private SecurityTestUtil() {
-        // complete
-    }
-
-    public static ReplayCache createCache(String key) throws WSSecurityException {
-        String diskKey = key + "-" + Math.abs(new Random().nextInt());
-        File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
-        return new EHCacheReplayCache(key, diskstore.toPath());
-    }
-}
diff --git a/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java b/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java
index 7342b7e..de74a7f 100644
--- a/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java
+++ b/ws-security-dom/src/test/java/org/apache/wss4j/dom/message/ReplayTest.java
@@ -19,16 +19,17 @@
 
 package org.apache.wss4j.dom.message;
 
+import java.nio.file.Path;
 import java.util.List;
 
 import javax.security.auth.callback.CallbackHandler;
 
+import org.apache.wss4j.common.cache.EHCacheReplayCache;
 import org.apache.wss4j.dom.WSConstants;
 import org.apache.wss4j.dom.common.KeystoreCallbackHandler;
 import org.apache.wss4j.dom.common.SAML2CallbackHandler;
 import org.apache.wss4j.dom.common.SOAPUtil;
 
-import org.apache.wss4j.dom.common.SecurityTestUtil;
 import org.apache.wss4j.dom.common.UsernamePasswordCallbackHandler;
 import org.apache.wss4j.dom.engine.WSSConfig;
 import org.apache.wss4j.dom.engine.WSSecurityEngine;
@@ -50,6 +51,7 @@ import org.apache.wss4j.dom.util.WSSecurityUtil;
 import org.apache.wss4j.dom.validate.SamlAssertionValidator;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
@@ -66,10 +68,17 @@ public class ReplayTest {
     private CallbackHandler callbackHandler = new KeystoreCallbackHandler();
     private Crypto crypto;
 
+    @TempDir
+    Path tempDir;
+
     public ReplayTest() throws Exception {
         crypto = CryptoFactory.getInstance();
     }
 
+    private ReplayCache createCache(String key) throws WSSecurityException {
+        return new EHCacheReplayCache(key, tempDir);
+    }
+
     @Test
     public void testReplayedTimestamp() throws Exception {
 
@@ -158,7 +167,7 @@ public class ReplayTest {
         RequestData data = new RequestData();
         data.setWssConfig(wssConfig);
         data.setCallbackHandler(callbackHandler);
-        ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.timestamp.cache-");
+        ReplayCache replayCache = createCache("wss4j.timestamp.cache-");
         data.setTimestampReplayCache(replayCache);
 
         // Successfully verify timestamp
@@ -253,7 +262,7 @@ public class ReplayTest {
         RequestData data = new RequestData();
         data.setWssConfig(wssConfig);
         data.setCallbackHandler(callbackHandler);
-        ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.timestamp.cache-");
+        ReplayCache replayCache = createCache("wss4j.timestamp.cache-");
         data.setTimestampReplayCache(replayCache);
 
         // Successfully verify timestamp
@@ -358,7 +367,7 @@ public class ReplayTest {
         RequestData data = new RequestData();
         data.setWssConfig(wssConfig);
         data.setCallbackHandler(callbackHandler);
-        ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.timestamp.cache-");
+        ReplayCache replayCache = createCache("wss4j.timestamp.cache-");
         data.setTimestampReplayCache(replayCache);
 
         // Successfully verify timestamp
@@ -431,7 +440,7 @@ public class ReplayTest {
         RequestData data = new RequestData();
         data.setCallbackHandler(new UsernamePasswordCallbackHandler());
         data.setWssConfig(wssConfig);
-        ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.nonce.cache-");
+        ReplayCache replayCache = createCache("wss4j.nonce.cache-");
         data.setNonceReplayCache(replayCache);
 
         // Successfully verify UsernameToken
@@ -492,7 +501,7 @@ public class ReplayTest {
         RequestData data = new RequestData();
         data.setWssConfig(wssConfig);
         data.setCallbackHandler(callbackHandler);
-        ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.saml.one.time.use.cache-");
+        ReplayCache replayCache = createCache("wss4j.saml.one.time.use.cache-");
         data.setSamlOneTimeUseReplayCache(replayCache);
 
         // Successfully verify SAML Token
@@ -549,7 +558,7 @@ public class ReplayTest {
         RequestData data = new RequestData();
         data.setWssConfig(wssConfig);
         data.setCallbackHandler(callbackHandler);
-        ReplayCache replayCache = SecurityTestUtil.createCache("wss4j.saml.one.time.use.cache-");
+        ReplayCache replayCache = createCache("wss4j.saml.one.time.use.cache-");
         data.setSamlOneTimeUseReplayCache(replayCache);
 
         // Successfully verify SAML Token
diff --git a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
index 672842c..df62e2a 100644
--- a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
+++ b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/ReplayTest.java
@@ -20,10 +20,9 @@ package org.apache.wss4j.stax.test;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.InputStream;
+import java.nio.file.Path;
 import java.util.Properties;
-import java.util.Random;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -46,6 +45,7 @@ import org.apache.wss4j.stax.test.utils.StAX2DOM;
 import org.apache.wss4j.stax.validate.SamlTokenValidatorImpl;
 import org.apache.xml.security.exceptions.XMLSecurityException;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 import org.w3c.dom.Document;
 import org.w3c.dom.NodeList;
 
@@ -56,12 +56,12 @@ import static org.junit.jupiter.api.Assertions.fail;
 
 public class ReplayTest extends AbstractTestBase {
 
+    @TempDir
+    Path tempDir;
+
     private ReplayCache createCache(String key) throws WSSecurityException {
         try {
-            String diskKey = key + "-" + Math.abs(new Random().nextInt());
-            File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
-
-            return new EHCacheReplayCache(key, diskstore.toPath());
+            return new EHCacheReplayCache(key, tempDir);
         } catch (XMLSecurityException e) {
             throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e);
         }
diff --git a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
index 98b6611..253e1e0 100644
--- a/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
+++ b/ws-security-stax/src/test/java/org/apache/wss4j/stax/test/UsernameTokenTest.java
@@ -20,9 +20,9 @@ package org.apache.wss4j.stax.test;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.File;
 import java.io.InputStream;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Path;
 import java.time.ZoneOffset;
 import java.time.ZonedDateTime;
 import java.util.ArrayList;
@@ -30,7 +30,6 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Properties;
-import java.util.Random;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
@@ -61,6 +60,7 @@ import org.apache.xml.security.stax.securityEvent.SecurityEvent;
 import org.apache.xml.security.stax.securityEvent.SecurityEventListener;
 import org.apache.xml.security.utils.XMLUtils;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
 import org.w3c.dom.Document;
@@ -249,7 +249,7 @@ public class UsernameTokenTest extends AbstractTestBase {
     */
 
     @Test
-    public void testReusedNonce() throws Exception {
+    public void testReusedNonce(@TempDir Path tempDir) throws Exception {
 
         ZonedDateTime created = ZonedDateTime.now(ZoneOffset.UTC);
         String createdString = DateUtil.getDateTimeFormatter(true).format(created);
@@ -276,7 +276,7 @@ public class UsernameTokenTest extends AbstractTestBase {
 
         WSSSecurityProperties securityProperties = new WSSSecurityProperties();
         securityProperties.setCallbackHandler(new CallbackHandlerImpl());
-        ReplayCache replayCache = createCache("wss4j.nonce.cache-");
+        ReplayCache replayCache = createCache("wss4j.nonce.cache", tempDir);
         securityProperties.setNonceReplayCache(replayCache);
         InboundWSSec wsSecIn = WSSec.getInboundWSSec(securityProperties, false, true);
         XMLStreamReader xmlStreamReader = wsSecIn.processInMessage(xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(req.getBytes())));
@@ -868,11 +868,9 @@ public class UsernameTokenTest extends AbstractTestBase {
         }
     }
 
-    private ReplayCache createCache(String key) throws WSSecurityException {
+    private ReplayCache createCache(String key, Path tempDir) throws WSSecurityException {
         try {
-            String diskKey = key + "-" + Math.abs(new Random().nextInt());
-            File diskstore = new File(System.getProperty("java.io.tmpdir"), diskKey);
-            return new EHCacheReplayCache(key, diskstore.toPath());
+            return new EHCacheReplayCache(key, tempDir);
         } catch (XMLSecurityException e) {
             throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, e);
         }