From 77ed20565d7089c3efeaef51afc4e087ea65a192 Mon Sep 17 00:00:00 2001 From: IanHowell Date: Tue, 17 Apr 2018 14:28:16 -0500 Subject: Cleanup JUnits * Moved JUnits into proper packages so as to match other packages * Suppressed most of the noisy output from JUnits in cadi-aaf * Prepared SecurityInfo for JUnit testing Issue-ID: AAF-128 Change-Id: Ib34658ca6da71a692336c51f1489ed167b531f77 Signed-off-by: IanHowell --- .../org/onap/aaf/cadi/config/SecurityInfo.java | 257 +++++++------- .../java/org/onap/aaf/cadi/config/test/JU_Get.java | 116 +++++++ .../onap/aaf/cadi/config/test/JU_GetAccess.java | 107 ++++++ .../org/onap/aaf/cadi/config/test/JU_MultiGet.java | 68 ++++ .../aaf/cadi/config/test/JU_SecurityInfoC.java | 109 ++++++ .../onap/aaf/cadi/config/test/JU_UsersDump.java | 138 ++++++++ .../onap/aaf/cadi/filter/test/JU_FCGetTest.java | 103 ++++++ .../onap/aaf/cadi/lur/test/JU_ConfigPrincipal.java | 79 +++++ .../org/onap/aaf/cadi/lur/test/JU_LocalLur.java | 103 ++++++ .../onap/aaf/cadi/lur/test/JU_LocalPermission.java | 71 ++++ .../org/onap/aaf/cadi/lur/test/JU_NullLur.java | 80 +++++ .../aaf/cadi/principal/test/JU_BasicPrincipal.java | 123 +++++++ .../principal/test/JU_CachedBasicPrincipal.java | 125 +++++++ .../org/onap/aaf/cadi/principal/test/JU_Kind.java | 70 ++++ .../principal/test/JU_OAuth2FormPrincipal.java | 56 ++++ .../cadi/principal/test/JU_StringTagLookup.java | 40 +++ .../cadi/principal/test/JU_TaggedPrincipal.java | 68 ++++ .../aaf/cadi/principal/test/JU_TrustPrincipal.java | 91 +++++ .../cadi/principal/test/JU_UnAuthPrincipal.java | 41 +++ .../aaf/cadi/principal/test/JU_X509Principal.java | 140 ++++++++ .../cadi/taf/dos/test/JU_DenialOfServiceTaf.java | 369 +++++++++++++++++++++ .../java/org/onap/aaf/cadi/taf/test/JU_EpiTaf.java | 104 ++++++ .../org/onap/aaf/cadi/taf/test/JU_NullTaf.java | 65 ++++ .../org/onap/aaf/cadi/taf/test/JU_PuntTafResp.java | 54 +++ .../onap/aaf/cadi/taf/test/JU_TrustNotTafResp.java | 72 ++++ .../onap/aaf/cadi/taf/test/JU_TrustTafResp.java | 82 +++++ .../java/org/onap/aaf/cadi/test/config/JU_Get.java | 116 ------- .../onap/aaf/cadi/test/config/JU_GetAccess.java | 107 ------ .../org/onap/aaf/cadi/test/config/JU_MultiGet.java | 68 ---- .../aaf/cadi/test/config/JU_SecurityInfoC.java | 109 ------ .../onap/aaf/cadi/test/config/JU_UsersDump.java | 140 -------- .../onap/aaf/cadi/test/filter/JU_FCGetTest.java | 103 ------ .../onap/aaf/cadi/test/lur/JU_ConfigPrincipal.java | 79 ----- .../org/onap/aaf/cadi/test/lur/JU_LocalLur.java | 103 ------ .../onap/aaf/cadi/test/lur/JU_LocalPermission.java | 71 ---- .../org/onap/aaf/cadi/test/lur/JU_NullLur.java | 80 ----- .../aaf/cadi/test/principal/JU_BasicPrincipal.java | 123 ------- .../test/principal/JU_CachedBasicPrincipal.java | 125 ------- .../org/onap/aaf/cadi/test/principal/JU_Kind.java | 70 ---- .../test/principal/JU_OAuth2FormPrincipal.java | 56 ---- .../cadi/test/principal/JU_StringTagLookup.java | 40 --- .../cadi/test/principal/JU_TaggedPrincipal.java | 68 ---- .../aaf/cadi/test/principal/JU_TrustPrincipal.java | 91 ----- .../cadi/test/principal/JU_UnAuthPrincipal.java | 41 --- .../aaf/cadi/test/principal/JU_X509Principal.java | 140 -------- .../java/org/onap/aaf/cadi/test/taf/JU_EpiTaf.java | 104 ------ .../org/onap/aaf/cadi/test/taf/JU_NullTaf.java | 65 ---- .../org/onap/aaf/cadi/test/taf/JU_PuntTafResp.java | 54 --- .../onap/aaf/cadi/test/taf/JU_TrustNotTafResp.java | 72 ---- .../onap/aaf/cadi/test/taf/JU_TrustTafResp.java | 82 ----- .../cadi/test/taf/dos/JU_DenialOfServiceTaf.java | 369 --------------------- .../java/org/onap/aaf/cadi/test/util/JU_Chmod.java | 75 ----- .../java/org/onap/aaf/cadi/test/util/JU_FQI.java | 44 --- .../aaf/cadi/test/util/JU_JsonOutputStream.java | 93 ------ .../aaf/cadi/test/util/JU_MaskFormatException.java | 44 --- .../org/onap/aaf/cadi/test/util/JU_NetMask.java | 65 ---- .../java/org/onap/aaf/cadi/test/util/JU_Pool.java | 159 --------- .../java/org/onap/aaf/cadi/test/util/JU_Split.java | 113 ------- .../aaf/cadi/test/util/JU_SubStandardConsole.java | 124 ------- .../org/onap/aaf/cadi/test/util/JU_TheConsole.java | 34 -- .../onap/aaf/cadi/test/util/JU_UserChainManip.java | 67 ---- .../java/org/onap/aaf/cadi/test/util/JU_Vars.java | 150 --------- .../org/onap/aaf/cadi/test/wsse/JU_WSSE_Read.java | 189 ----------- .../org/onap/aaf/cadi/test/wsse/JU_XReader.java | 162 --------- .../java/org/onap/aaf/cadi/util/test/JU_Chmod.java | 75 +++++ .../java/org/onap/aaf/cadi/util/test/JU_FQI.java | 44 +++ .../aaf/cadi/util/test/JU_JsonOutputStream.java | 93 ++++++ .../aaf/cadi/util/test/JU_MaskFormatException.java | 44 +++ .../org/onap/aaf/cadi/util/test/JU_NetMask.java | 65 ++++ .../java/org/onap/aaf/cadi/util/test/JU_Pool.java | 159 +++++++++ .../java/org/onap/aaf/cadi/util/test/JU_Split.java | 113 +++++++ .../aaf/cadi/util/test/JU_SubStandardConsole.java | 124 +++++++ .../org/onap/aaf/cadi/util/test/JU_TheConsole.java | 34 ++ .../onap/aaf/cadi/util/test/JU_UserChainManip.java | 67 ++++ .../java/org/onap/aaf/cadi/util/test/JU_Vars.java | 150 +++++++++ .../org/onap/aaf/cadi/wsse/test/JU_WSSE_Read.java | 189 +++++++++++ .../org/onap/aaf/cadi/wsse/test/JU_XReader.java | 162 +++++++++ 77 files changed, 3932 insertions(+), 3913 deletions(-) create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_Get.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_GetAccess.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_MultiGet.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_SecurityInfoC.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_UsersDump.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/filter/test/JU_FCGetTest.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_ConfigPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalPermission.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_NullLur.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_BasicPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_CachedBasicPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_Kind.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_OAuth2FormPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_StringTagLookup.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TaggedPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TrustPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_UnAuthPrincipal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/taf/dos/test/JU_DenialOfServiceTaf.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_EpiTaf.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_NullTaf.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_PuntTafResp.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustNotTafResp.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustTafResp.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_Get.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_GetAccess.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_UsersDump.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/filter/JU_FCGetTest.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_ConfigPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalLur.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalPermission.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_NullLur.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_BasicPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_CachedBasicPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_Kind.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_OAuth2FormPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_StringTagLookup.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TaggedPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TrustPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_UnAuthPrincipal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_X509Principal.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_EpiTaf.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_NullTaf.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_PuntTafResp.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustNotTafResp.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustTafResp.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/dos/JU_DenialOfServiceTaf.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Chmod.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_FQI.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_JsonOutputStream.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_MaskFormatException.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_NetMask.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Pool.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Split.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_SubStandardConsole.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_TheConsole.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Vars.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/wsse/JU_WSSE_Read.java delete mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/test/wsse/JU_XReader.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Chmod.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_FQI.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_JsonOutputStream.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_MaskFormatException.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_NetMask.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Split.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_SubStandardConsole.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_TheConsole.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_UserChainManip.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Vars.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/wsse/test/JU_WSSE_Read.java create mode 100644 cadi/core/src/test/java/org/onap/aaf/cadi/wsse/test/JU_XReader.java (limited to 'cadi/core/src') diff --git a/cadi/core/src/main/java/org/onap/aaf/cadi/config/SecurityInfo.java b/cadi/core/src/main/java/org/onap/aaf/cadi/config/SecurityInfo.java index 30496d78..2d252ea9 100644 --- a/cadi/core/src/main/java/org/onap/aaf/cadi/config/SecurityInfo.java +++ b/cadi/core/src/main/java/org/onap/aaf/cadi/config/SecurityInfo.java @@ -86,128 +86,24 @@ public class SecurityInfo { try { this.access = access; // reuse DME2 Properties for convenience if specific Properties don't exist - String keyStore = access.getProperty(Config.CADI_KEYSTORE,null); - if(keyStore!=null && !new File(keyStore).exists()) { - throw new CadiException(keyStore + " does not exist"); - } - String keyStorePasswd = access.getProperty(Config.CADI_KEYSTORE_PASSWORD, null); - keyStorePasswd = keyStorePasswd==null?null:access.decrypt(keyStorePasswd,false); - String trustStore = access.getProperty(Config.CADI_TRUSTSTORE, null); - if(trustStore!=null && !new File(trustStore).exists()) { - throw new CadiException(trustStore + " does not exist"); - } - String trustStorePasswd = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD, null); - trustStorePasswd = trustStorePasswd==null?"changeit"/*defacto Java Trust Pass*/:access.decrypt(trustStorePasswd,false); - default_alias = access.getProperty(Config.CADI_ALIAS,null); - String keyPasswd = access.getProperty(Config.CADI_KEY_PASSWORD,null); - keyPasswd = keyPasswd==null?keyStorePasswd:access.decrypt(keyPasswd,false); - String tips=access.getProperty(Config.CADI_TRUST_MASKS, null); - if(tips!=null) { - access.log(Level.INIT,"Explicitly accepting valid X509s from",tips); - String[] ipsplit = tips.split(REGEX_COMMA); - trustMasks = new NetMask[ipsplit.length]; - for(int i=0;i kmal = new ArrayList(); - for(String ksname : keyStore.split(REGEX_COMMA)) { - file = new File(ksname); - String keystoreFormat; - if(ksname.endsWith(".p12") || ksname.endsWith(".pkcs12")) { - keystoreFormat = "PKCS12"; - } else { - keystoreFormat = "JKS"; - } - if(file.exists()) { - FileInputStream fis = new FileInputStream(file); - try { - KeyStore ks = KeyStore.getInstance(keystoreFormat); - ks.load(fis, keyStorePasswd.toCharArray()); - kmf.init(ks, keyPasswd.toCharArray()); - } finally { - fis.close(); - } - } - } - for(KeyManager km : kmf.getKeyManagers()) { - if(km instanceof X509KeyManager) { - kmal.add((X509KeyManager)km); - } - } - km = new X509KeyManager[kmal.size()]; - kmal.toArray(km); - } - - TrustManagerFactory tmf = TrustManagerFactory.getInstance(SslKeyManagerFactoryAlgorithm); - if(trustStore!=null) { - for(String tsname : trustStore.split(REGEX_COMMA)) { - file = new File(tsname); - if(file.exists()) { - FileInputStream fis = new FileInputStream(file); - try { - KeyStore ts = KeyStore.getInstance("JKS"); - ts.load(fis, trustStorePasswd.toCharArray()); - tmf.init(ts); - } finally { - fis.close(); - } - } - } - TrustManager tms[] = tmf.getTrustManagers(); - if(tms!=null) { - tm = new X509TrustManager[tms==null?0:tms.length]; - for(int i=0;i kmal = new ArrayList(); + File file; + for(String ksname : keyStore.split(REGEX_COMMA)) { + file = new File(ksname); + String keystoreFormat; + if(ksname.endsWith(".p12") || ksname.endsWith(".pkcs12")) { + keystoreFormat = "PKCS12"; + } else { + keystoreFormat = "JKS"; + } + if(file.exists()) { + FileInputStream fis = new FileInputStream(file); + try { + KeyStore ks = KeyStore.getInstance(keystoreFormat); + ks.load(fis, keyStorePasswd.toCharArray()); + kmf.init(ks, keyPasswd.toCharArray()); + } finally { + fis.close(); + } + } + } + for(KeyManager km : kmf.getKeyManagers()) { + if(km instanceof X509KeyManager) { + kmal.add((X509KeyManager)km); + } + } + km = new X509KeyManager[kmal.size()]; + kmal.toArray(km); + } + } + + protected void initializeTrustManager() throws NoSuchAlgorithmException, CertificateException, IOException, KeyStoreException, CadiException { + String trustStore = access.getProperty(Config.CADI_TRUSTSTORE, null); + if(trustStore != null && !new File(trustStore).exists()) { + throw new CadiException(trustStore + " does not exist"); + } + + String trustStorePasswd = access.getProperty(Config.CADI_TRUSTSTORE_PASSWORD, null); + trustStorePasswd = (trustStorePasswd == null) ? "changeit"/*defacto Java Trust Pass*/ : access.decrypt(trustStorePasswd, false); + TrustManagerFactory tmf = TrustManagerFactory.getInstance(SslKeyManagerFactoryAlgorithm); + if(trustStore != null) { + File file; + for(String tsname : trustStore.split(REGEX_COMMA)) { + file = new File(tsname); + if(file.exists()) { + FileInputStream fis = new FileInputStream(file); + try { + KeyStore ts = KeyStore.getInstance("JKS"); + ts.load(fis, trustStorePasswd.toCharArray()); + tmf.init(ts); + } finally { + fis.close(); + } + } + } + + TrustManager tms[] = tmf.getTrustManagers(); + if(tms != null) { + tm = new X509TrustManager[(tms == null) ? 0 : tms.length]; + for(int i = 0; i < tms.length; ++i) { + try { + tm[i] = (X509TrustManager)tms[i]; + } catch (ClassCastException e) { + access.log(Level.WARN, "Non X509 TrustManager", tm[i].getClass().getName(), "skipped in SecurityInfo"); + } + } + } + } + + } + + protected void initializeTrustMasks() throws AccessException { + String tips = access.getProperty(Config.CADI_TRUST_MASKS, null); + if(tips != null) { + access.log(Level.INIT, "Explicitly accepting valid X509s from", tips); + String[] ipsplit = tips.split(REGEX_COMMA); + trustMasks = new NetMask[ipsplit.length]; + for(int i = 0; i < ipsplit.length; ++i) { + try { + trustMasks[i] = new NetMask(ipsplit[i]); + } catch (MaskFormatException e) { + throw new AccessException("Invalid IP Mask in " + Config.CADI_TRUST_MASKS, e); + } + } + } + + if(trustMasks != null) { + final HostnameVerifier origHV = HttpsURLConnection.getDefaultHostnameVerifier(); + HttpsURLConnection.setDefaultHostnameVerifier(maskHV = new HostnameVerifier() { + @Override + public boolean verify(final String urlHostName, final SSLSession session) { + try { + // This will pick up /etc/host entries as well as DNS + InetAddress ia = InetAddress.getByName(session.getPeerHost()); + for(NetMask tmask : trustMasks) { + if(tmask.isInNet(ia.getHostAddress())) { + return true; + } + } + } catch (UnknownHostException e) { + // It's ok. do normal Verify + } + return origHV.verify(urlHostName, session); + }; + }); + } + } + } diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_Get.java b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_Get.java new file mode 100644 index 00000000..586c50c7 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_Get.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.config.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Get; + +public class JU_Get { + + private String defaultVal = "some default value"; + + private ByteArrayOutputStream outStream; + + private TestBean tb; + + @Before + public void setup() { + outStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outStream)); + } + + @After + public void tearDown() { + System.setOut(System.out); + } + + @Test + public void beanTest() { + tb = new TestBean(); + tb.setProperty1("prop1"); + + Get.Bean testBean = new Get.Bean(tb); + assertThat(testBean.get("property1", defaultVal, true), is("prop1")); + assertThat(testBean.get("property2", defaultVal, true), is(defaultVal)); + assertThat(testBean.get("thrower", defaultVal, true), is(defaultVal)); + } + + @Test + public void nullTest() { + assertThat(Get.NULL.get("name", defaultVal, true), is(defaultVal)); + } + + @Test + public void accessTest() { + String output; + + PropAccess access = new PropAccess(); + access.setProperty("tag", "value"); + Get.AccessGet accessGet = new Get.AccessGet(access); + + assertThat(accessGet.get("tag", defaultVal, true), is("value")); + output = outStream.toString().split(" ", 2)[1]; + assertThat(output, is("INIT [cadi] tag is set to value\n")); + + outStream.reset(); + + assertThat(accessGet.get("not a real tag", defaultVal, true), is(defaultVal)); + output = outStream.toString().split(" ", 2)[1]; + assertThat(output, is("INIT [cadi] not a real tag is set to " + defaultVal + "\n")); + + outStream.reset(); + + assertThat(accessGet.get("not a real tag", null, true), is(nullValue())); + output = outStream.toString().split(" ", 2)[1]; + assertThat(output, is("INIT [cadi] not a real tag is not set\n")); + + outStream.reset(); + + assertThat(accessGet.get("tag", defaultVal, false), is("value")); + assertThat(outStream.toString(), is("")); + } + + public class TestBean implements java.io.Serializable { + + private static final long serialVersionUID = 1L; + private String property1 = null; + private String property2 = null; + @SuppressWarnings("unused") + private String thrower = null; + + public TestBean() { } + public String getProperty1() { return property1; } + public void setProperty1(final String value) { this.property1 = value; } + public String getProperty2() { return property2; } + public void setProperty2(final String value) { this.property2 = value; } + public String getThrower() throws Exception { throw new Exception(); } + public void setThrower(final String value) { this.thrower = value; } + + } +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_GetAccess.java b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_GetAccess.java new file mode 100644 index 00000000..36da3073 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_GetAccess.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.config.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Get; +import org.onap.aaf.cadi.config.GetAccess; + +public class JU_GetAccess { + + private String defaultVal = "some default value"; + + private ByteArrayOutputStream outStream; + + private PropAccess access; + private Get.AccessGet accessGet; + private File file; + private String filePath; + + @Before + public void setup() throws IOException { + outStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outStream)); + + file = File.createTempFile("GetAccess_test", ""); + filePath = file.getAbsolutePath(); + + access = new PropAccess(); + access.setProperty("cadi_prop_files", filePath); + accessGet = new Get.AccessGet(access); + + } + + @After + public void tearDown() { + System.setOut(System.out); + + file.delete(); + } + + @Test + public void constructorTest() { + String output; + + @SuppressWarnings("unused") + GetAccess getAccess = new GetAccess(accessGet); + String[] lines = outStream.toString().split("\n"); + assertThat(lines.length, is(2)); + output = lines[0].split(" ", 2)[1]; + assertThat(output, is("INIT [cadi] cadi_prop_files is set to " + filePath)); + output = lines[1].split(" ", 2)[1]; + assertThat(output, is("INIT [cadi] Loading CADI Properties from " + filePath)); + } + + @Test + public void getPropertyTest1() { + GetAccess getAccess = new GetAccess(accessGet); + + getAccess.setProperty("tag", "value"); + assertThat(getAccess.getProperty("tag", defaultVal), is("value")); + assertThat(getAccess.getProperty("not_a_tag", defaultVal), is(defaultVal)); + } + + @Test + public void getPropertyTest2() { + GetAccess getAccess = new GetAccess(accessGet); + + getAccess.setProperty("tag", "value"); + assertThat(getAccess.getProperty("tag"), is("value")); + assertThat(getAccess.getProperty("not_a_tag"), is(nullValue())); + } + + @Test + public void getTest() { + GetAccess getAccess = new GetAccess(accessGet); + assertThat((Get.AccessGet)getAccess.get(), is(accessGet)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_MultiGet.java b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_MultiGet.java new file mode 100644 index 00000000..6510bdcd --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_MultiGet.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.config.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; + +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.Get; +import org.onap.aaf.cadi.config.MultiGet; + +public class JU_MultiGet { + + private String defaultVal = "some default value"; + + private ByteArrayOutputStream outStream; + + private MultiGet multiGet; + private Get.AccessGet accessGet; + private PropAccess access; + + @Before + public void setup() throws IOException { + outStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outStream)); + + access = new PropAccess(); + access.setProperty("tag", "value"); + accessGet = new Get.AccessGet(access); + multiGet = new MultiGet(accessGet, Get.NULL); + } + + @After + public void tearDown() { + System.setOut(System.out); + } + + @Test + public void getTest() { + assertThat(multiGet.get("tag", defaultVal, false), is("value")); + assertThat(multiGet.get("not_a_tag", defaultVal, false), is(defaultVal)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_SecurityInfoC.java b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_SecurityInfoC.java new file mode 100644 index 00000000..27014b9a --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_SecurityInfoC.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.config.test; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +import org.junit.*; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.SecuritySetter; +import org.onap.aaf.cadi.config.SecurityInfoC; + +public class JU_SecurityInfoC { + + ByteArrayOutputStream outStream; + ByteArrayOutputStream errStream; + + @Before + public void setup() { + outStream = new ByteArrayOutputStream(); + errStream = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(outStream)); + System.setErr(new PrintStream(errStream)); + } + + @After + public void tearDown() { + System.setOut(System.out); + System.setErr(System.err); + } + + @Test + public void instanceTest() throws CadiException, MalformedURLException { + SecurityInfoC si = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); + assertThat(si.defSS.getID(), is(SecurityInfoC.DEF_ID)); + try { + si.defSS.setSecurity(new HttpURLConnectionStub()); + fail("Should have thrown an exception"); + } catch (CadiException e) { + assertTrue(e instanceof CadiException); + assertThat(e.getMessage(), is("No Client Credentials set.")); + } + assertThat(si.defSS.setLastResponse(0), is(0)); + + // Try it again for coverage + SecurityInfoC siClone = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); + assertThat(siClone, is(si)); + } + + @Test + public void setTest() throws MalformedURLException, CadiException { + SecurityInfoC si = SecurityInfoC.instance(new PropAccess(), HttpURLConnectionStub.class); + SecuritySetter ss = new SecuritySetterStub(); + assertThat(si.set(ss), is(si)); + assertThat(si.defSS.getID(), is("Example ID")); + try { + si.defSS.setSecurity(new HttpURLConnectionStub()); + fail("Should have thrown an exception"); + } catch (CadiException e) { + assertTrue(e instanceof CadiException); + assertThat(e.getMessage(), is("Example exception")); + } + assertThat(si.defSS.setLastResponse(0), is(0)); + assertThat(si.defSS.setLastResponse(1), is(1)); + assertThat(si.defSS.setLastResponse(-1), is(-1)); + } + + private class HttpURLConnectionStub extends HttpURLConnection { + public HttpURLConnectionStub() throws MalformedURLException { super(new URL("http://www.example.com")); } + @Override public void disconnect() { } + @Override public boolean usingProxy() { return false; } + @Override public void connect() throws IOException { } + } + + private class SecuritySetterStub implements SecuritySetter { + public String getID() { return "Example ID"; } + public void setSecurity(CT client) throws CadiException { throw new CadiException("Example exception"); } + public int setLastResponse(int respCode) { return respCode; } + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_UsersDump.java b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_UsersDump.java new file mode 100644 index 00000000..63b8cf2d --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/config/test/JU_UsersDump.java @@ -0,0 +1,138 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.config.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; + +import org.onap.aaf.cadi.AbsUserCache; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.config.UsersDump; +import org.onap.aaf.cadi.lur.LocalLur; +import org.onap.aaf.cadi.lur.LocalPermission; +import org.onap.aaf.cadi.util.Split; + +public class JU_UsersDump { + + private ByteArrayOutputStream outStream; + private ByteArrayOutputStream stdoutSuppressor; + + private static final String expected = "\n" + + "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "\n"; + + private final static String groups = "myname:groupA,groupB"; + private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234"; + + private AbsUserCache lur; + + @Before + public void setup() throws IOException { + outStream = new ByteArrayOutputStream(); + stdoutSuppressor = new ByteArrayOutputStream(); + + System.setOut(new PrintStream(stdoutSuppressor)); + + lur = new LocalLur(new PropAccess(), groups, names); + } + + @After + public void tearDown() { + System.setOut(System.out); + } + + @Test + public void writeTest() throws IOException { + UsersDump.write(outStream, lur); + String[] actualLines = Split.splitTrim('\n', outStream.toString()); + String[] expectedLines = Split.splitTrim('\n', expected); + + assertThat(actualLines.length, is(expectedLines.length)); + + // Check that the output starts with an XML tag + assertThat(actualLines[0], is(expectedLines[0])); + // Check that lines 2-4 are a comment + assertThat(actualLines[1], is(expectedLines[1])); + assertThat(actualLines[3], is(expectedLines[3])); + + // Check that the rest of the output matches the expected output + for (int i = 4; i < actualLines.length; i++) { + assertThat(actualLines[i], is(expectedLines[i])); + } + + // Run the test again with outStream as a PrintStream (for coverage) + outStream.reset(); + UsersDump.write(new PrintStream(outStream), lur); + actualLines = Split.splitTrim('\n', outStream.toString()); + + assertThat(actualLines.length, is(expectedLines.length)); + + // Check that the output starts with an XML tag + assertThat(actualLines[0], is(expectedLines[0])); + // Check that lines 2-4 are a comment + assertThat(actualLines[1], is(expectedLines[1])); + assertThat(actualLines[3], is(expectedLines[3])); + + // Check that the rest of the output matches the expected output + for (int i = 4; i < actualLines.length; i++) { + assertThat(actualLines[i], is(expectedLines[i])); + } + } + + @Test + public void updateUsersTest() { + String output; + File outputFile = new File("src/test/resources/userdump.xml"); + assertThat(outputFile.exists(), is(false)); + + output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur); + assertThat(output, is(nullValue())); + assertThat(outputFile.exists(), is(true)); + + output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur); + assertThat(output, is(nullValue())); + assertThat(outputFile.exists(), is(true)); + + outputFile.delete(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/filter/test/JU_FCGetTest.java b/cadi/core/src/test/java/org/onap/aaf/cadi/filter/test/JU_FCGetTest.java new file mode 100644 index 00000000..694c59e7 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/filter/test/JU_FCGetTest.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.filter.test; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.when; + +import javax.servlet.FilterConfig; +import javax.servlet.ServletContext; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.aaf.cadi.PropAccess; + +public class JU_FCGetTest { + + @Test + public void netYetTested() { + //fail("Tests not yet implemented"); + } + +// @Mock +// private ServletContext context; + +// @Mock +// private FilterConfig config; + +// @Mock +// private PropAccess access = new PropAccess(); + +// @Before +// public void setUp() { +// MockitoAnnotations.initMocks(this); +// } + +// @Test +// public void testGetStringFromDef() { +// PropAccess access = new PropAccess(); + +// FCGet fcGet = new FCGet(access, context, config); + +// String user = fcGet.get("user", "DefaultUser", true); + +// assertEquals(user, "DefaultUser"); +// } + +// @Test +// public void testGetStringFromContext() { +// PropAccess access = new PropAccess(); +// when(context.getInitParameter("user")).thenReturn("ContextUser"); + +// FCGet fcGet = new FCGet(access, context, null); + +// String user = fcGet.get("user", "DefaultUser", true); + +// assertEquals(user,"ContextUser"); +// } + +// @Test +// public void testGetStringFromFilter() { +// PropAccess access = new PropAccess(); +// when(config.getInitParameter("user")).thenReturn("FilterUser"); + +// FCGet fcGet = new FCGet(access, null, config); + +// String user = fcGet.get("user", "DefaultUser", true); + +// assertEquals(user,"FilterUser"); +// } + +// @Test +// public void testGetStringWithNullContextFilter() { + +// when(access.getProperty("user", "DefaultUser")).thenReturn(null); + +// FCGet fcGet = new FCGet(access, null, null); + +// String user = fcGet.get("user", "DefaultUser", true); + +// assertEquals(user,"DefaultUser"); +// } +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_ConfigPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_ConfigPrincipal.java new file mode 100644 index 00000000..9853f88e --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_ConfigPrincipal.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.lur.test; + +import org.junit.*; +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import java.lang.reflect.Field; + +import java.io.IOException; + +import org.onap.aaf.cadi.lur.ConfigPrincipal; + +public class JU_ConfigPrincipal { + + private final String name = "User"; + private final String pass = "pass"; + + // Expected output of base64("User:pass") + private final String b64encoded = "VXNlcjpwYXNz"; + + private Field content_field; + + @Before + public void setup() throws NoSuchFieldException { + content_field = ConfigPrincipal.class.getDeclaredField("content"); + content_field.setAccessible(true); + } + + @Test + public void testConfigPrincipalStringString() throws IOException, IllegalArgumentException, IllegalAccessException { + ConfigPrincipal p = new ConfigPrincipal(name, pass); + + assertThat(p.getName(), is(name)); + assertThat(p.toString(), is(name)); + assertThat(p.getCred(), is(pass.getBytes())); + assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); + content_field.set(p, "pass"); + assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); + + // One more time for coverage purposes + assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); + } + + @Test + public void testConfigPrincipalStringByteArray() throws IOException, IllegalArgumentException, IllegalAccessException { + ConfigPrincipal p = new ConfigPrincipal(name, pass.getBytes()); + + assertThat(p.getName(), is(name)); + assertThat(p.toString(), is(name)); + assertThat(p.getCred(), is(pass.getBytes())); + assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); + content_field.set(p, "pass"); + assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); + + // One more time for coverage purposes + assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java new file mode 100644 index 00000000..fd8e3575 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalLur.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.lur.test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Test; +import org.onap.aaf.cadi.Lur; +import org.onap.aaf.cadi.Permission; +import org.onap.aaf.cadi.PropAccess; +import org.onap.aaf.cadi.Symm; +import org.onap.aaf.cadi.CredVal.Type; +import org.onap.aaf.cadi.config.UsersDump; +import org.onap.aaf.cadi.lur.LocalLur; +import org.onap.aaf.cadi.lur.LocalPermission; + +public class JU_LocalLur { + + @Test + public void test() throws IOException { + Symm symmetric = Symm.baseCrypt().obtain(); + LocalLur up; + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + baos.write(Symm.ENC.getBytes()); + symmetric.enpass("", baos); + PropAccess ta = new PropAccess(); + Lur ml = up = new LocalLur(ta,"myname:groupA,groupB","admin:myname,yourname;suser:hisname,hername,m1234%"+baos.toString()); + + +// Permission admin = new LocalPermission("admin"); +// Permission suser = new LocalPermission("suser"); +// +// // Check User fish +// assertTrue(ml.fish(new JUPrincipal("myname"),admin)); +// assertTrue(ml.fish(new JUPrincipal("hisname"),admin)); +// assertFalse(ml.fish(new JUPrincipal("noname"),admin)); +// assertTrue(ml.fish(new JUPrincipal("itsname"),suser)); +// assertTrue(ml.fish(new JUPrincipal("hername"),suser)); +// assertFalse(ml.fish(new JUPrincipal("myname"),suser)); +// +// // Check validate password +// assertTrue(up.validate("m1234",Type.PASSWORD, "".getBytes())); +// assertFalse(up.validate("m1234",Type.PASSWORD, "badPass".getBytes())); +// + // Check fishAll + Set set = new TreeSet(); + List perms = new ArrayList(); + ml.fishAll(new JUPrincipal("myname"), perms); + for(Permission p : perms) { + set.add(p.getKey()); + } +// assertEquals("[admin, groupA, groupB]",set.toString()); + UsersDump.write(System.out, up); + System.out.flush(); + + } + + // Simplistic Principal for testing purposes + private static class JUPrincipal implements Principal { + private String name; + public JUPrincipal(String name) { + this.name = name; + } +// @Override + public String getName() { + return name; + } + } + + + + + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalPermission.java b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalPermission.java new file mode 100644 index 00000000..f4b051a8 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_LocalPermission.java @@ -0,0 +1,71 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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,Z + * * 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.lur.test; + +import static org.junit.Assert.*; + +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import static org.mockito.Mockito.*; + +import org.onap.aaf.cadi.lur.LocalPermission; +import org.onap.aaf.cadi.Permission; + +public class JU_LocalPermission { + + @Mock + Permission perm; + + private LocalPermission localPerm; + private String role = "Fake Role"; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + when(perm.getKey()).thenReturn(role); + + localPerm = new LocalPermission(role); + } + + @Test + public void getKeyTest() { + assertThat(localPerm.getKey(), is(role)); + } + + @Test + public void toStringTest() { + assertThat(localPerm.toString(), is(role)); + } + + @Test + public void matchTest() { + assertTrue(localPerm.match(perm)); + } + + @Test + public void permTypeTest() { + assertThat(localPerm.permType(), is("LOCAL")); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_NullLur.java b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_NullLur.java new file mode 100644 index 00000000..1a7293d3 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/lur/test/JU_NullLur.java @@ -0,0 +1,80 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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,Z + * * 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.lur.test; + +import java.security.Principal; +import java.util.List; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import org.junit.*; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import java.lang.reflect.*; + +import org.onap.aaf.cadi.Permission; +import org.onap.aaf.cadi.lur.NullLur; + +public class JU_NullLur { + + @Mock + Principal p; + + @Mock + Permission perm; + + @Mock + List perms; + + private NullLur nullLur; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + + nullLur = new NullLur(); + } + + @Test + public void coverageTests() throws Exception { + + Field nullClass = NullLur.class.getDeclaredField("NULL"); + nullClass.setAccessible(true); + assertThat(((Permission) nullClass.get(NullLur.class)).permType(), is("")); + assertThat(((Permission) nullClass.get(NullLur.class)).getKey(), is("")); + assertFalse(((Permission) nullClass.get(NullLur.class)).match(perm)); + + nullLur.fishAll(p, perms); + nullLur.destroy(); + + assertFalse(nullLur.fish(p, perm)); + assertFalse(nullLur.handlesExclusively(perm)); + assertFalse(nullLur.handles(p)); + assertThat(nullLur.createPerm(""), is(nullClass.get(NullLur.class))); + + StringBuilder sb = new StringBuilder(); + nullLur.clear(p, sb); + assertThat(sb.toString(), is("NullLur\n")); + assertThat(nullLur.toString(), is("NullLur\n")); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_BasicPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_BasicPrincipal.java new file mode 100644 index 00000000..dee7fc21 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_BasicPrincipal.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; + +import java.io.IOException; +import java.util.Date; + +import org.onap.aaf.cadi.BasicCred; +import org.onap.aaf.cadi.Symm; +import org.onap.aaf.cadi.principal.BasicPrincipal; +import org.onap.aaf.cadi.principal.StringTagLookup; +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup; + +public class JU_BasicPrincipal { + + @Test + public void Constructor1Test() throws Exception { + // Test that everything works when the content doesn't contain "Basic" + BasicPrincipal bp = new BasicPrincipal("content", "domain"); + assertThat(bp.getName(), is("content")); + assertThat(bp.getCred(), is(nullValue())); + + // Test sending a user without an implicit domain + String name = "User"; + String password = "password"; + String content = name + ":" + password; + String domain = "exampledomain.com"; + String encrypted = new String(Symm.base64.encode(content.getBytes())); + bp = new BasicPrincipal("Basic " + encrypted, domain); + assertThat(bp.getShortName(), is(name)); + assertThat(bp.getName(), is(name + "@" + domain)); + assertThat(bp.getCred(), is(password.getBytes())); + + // Test sending a user with an implicit domain + String longName = name + "@" + domain + ":" + password; + encrypted = new String(Symm.base64.encode(longName.getBytes())); + bp = new BasicPrincipal("Basic " + encrypted, domain); + assertThat(bp.getShortName(), is(name)); + assertThat(bp.getName(), is(name + "@" + domain)); + assertThat(bp.getCred(), is(password.getBytes())); + + // Check that an exception is throw if no name is given in the content + try { + bp = new BasicPrincipal("Basic " + new String(Symm.base64.encode("no name".getBytes())), ""); + fail("Should have thrown an exception"); + } catch (IOException e) { + assertThat(e.getMessage(), is("Invalid Coding")); + } + } + + @Test + public void Constructor2Test() { + String name = "User"; + String password = "password"; + BasicCred bc = mock(BasicCred.class); + when(bc.getUser()).thenReturn(name); + when(bc.getCred()).thenReturn(password.getBytes()); + + BasicPrincipal bp = new BasicPrincipal(bc, "domain"); + assertThat(bp.getName(), is(name)); + assertThat(bp.getCred(), is(password.getBytes())); + } + + @Test + public void accessorsTest() throws IOException { + String name = "User"; + String password = "password"; + String content = name + ":" + password; + String domain = "exampledomain.com"; + String encrypted = new String(Symm.base64.encode(content.getBytes())); + String bearer = "bearer"; + long created = System.currentTimeMillis(); + BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, domain); + bp.setBearer(bearer); + + String expected = "Basic Authorization for " + name + "@" + domain + " evaluated on " + new Date(bp.created()).toString(); + assertTrue(Math.abs(bp.created() - created) < 10); + assertThat(bp.toString(), is(expected)); + assertThat(bp.tag(), is("BAth")); + assertThat(bp.personalName(), is(nullValue())); + + // This test hits the abstract class BearerPrincipal + assertThat(bp.getBearer(), is(bearer)); + } + + + @Test + public void coverageTest() throws IOException { + String name = "User"; + String password = "password:with:colons"; + String content = name + ":" + password; + String encrypted = new String(Symm.base64.encode(content.getBytes())); + @SuppressWarnings("unused") + BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, "domain"); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_CachedBasicPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_CachedBasicPrincipal.java new file mode 100644 index 00000000..aa9a01a5 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_CachedBasicPrincipal.java @@ -0,0 +1,125 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.Date; + +import org.onap.aaf.cadi.BasicCred; +import org.onap.aaf.cadi.CachedPrincipal; +import org.onap.aaf.cadi.CachedPrincipal.Resp; +import org.onap.aaf.cadi.Symm; +import org.onap.aaf.cadi.principal.CachedBasicPrincipal; +import org.onap.aaf.cadi.principal.StringTagLookup; +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup; +import org.onap.aaf.cadi.taf.HttpTaf; + +public class JU_CachedBasicPrincipal { + private Field creatorField; + private Field timeToLiveField; + + @Mock + private HttpTaf creator; + + private CachedPrincipal.Resp resp; + + @Before + public void setup() throws NoSuchFieldException, SecurityException { + MockitoAnnotations.initMocks(this); + + creatorField = CachedBasicPrincipal.class.getDeclaredField("creator"); + timeToLiveField = CachedBasicPrincipal.class.getDeclaredField("timeToLive"); + + creatorField.setAccessible(true); + timeToLiveField.setAccessible(true); + } + + @Test + public void Constructor1Test() throws IllegalArgumentException, IllegalAccessException { + String name = "User"; + String password = "password"; + BasicCred bc = mock(BasicCred.class); + when(bc.getUser()).thenReturn(name); + when(bc.getCred()).thenReturn(password.getBytes()); + + long timeToLive = 10000L; + long expires = System.currentTimeMillis() + timeToLive; + CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, bc, "domain", timeToLive); + + assertThat((HttpTaf)creatorField.get(cbp), is(creator)); + assertThat((Long)timeToLiveField.get(cbp), is(timeToLive)); + assertTrue(Math.abs(cbp.expires() - expires) < 10); + } + + @Test + public void Constructor2Test() throws Exception { + String name = "User"; + String password = "password"; + String content = name + ":" + password; + long timeToLive = 10000L; + long expires = System.currentTimeMillis() + timeToLive; + CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive); + + assertThat((HttpTaf)creatorField.get(cbp), is(creator)); + assertThat((Long)timeToLiveField.get(cbp), is(timeToLive)); + assertTrue(Math.abs(cbp.expires() - expires) < 10); + } + + @Test + public void revalidateTest() throws IOException, IllegalArgumentException, IllegalAccessException, InterruptedException { + resp = CachedPrincipal.Resp.REVALIDATED; + when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp); + + String name = "User"; + String password = "password"; + String content = name + ":" + password; + long timeToLive = 10000L; + long expires = System.currentTimeMillis() + timeToLive; + CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive); + + assertTrue(Math.abs(cbp.expires() - expires) < 10); + + Thread.sleep(1); + expires = System.currentTimeMillis() + timeToLive; + assertThat(cbp.revalidate(new Object()), is(resp)); + assertTrue(Math.abs(cbp.expires() - expires) < 10); + + resp = CachedPrincipal.Resp.UNVALIDATED; + when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp); + expires = System.currentTimeMillis() + timeToLive; + cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive); + + assertThat(cbp.revalidate(new Object()), is(resp)); + assertTrue(Math.abs(cbp.expires() - expires) < 10); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_Kind.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_Kind.java new file mode 100644 index 00000000..e9bd799c --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_Kind.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import org.onap.aaf.cadi.principal.BasicPrincipal; +import org.onap.aaf.cadi.principal.Kind; +import org.onap.aaf.cadi.principal.OAuth2FormPrincipal; +import org.onap.aaf.cadi.principal.TrustPrincipal; +import org.onap.aaf.cadi.principal.X509Principal; + +public class JU_Kind { + + @Mock + private TrustPrincipal trust; + + @Mock + private X509Principal x509; + + @Mock + private OAuth2FormPrincipal oauth; + + @Mock + private BasicPrincipal basic; + + @Before + public void setup() throws SecurityException { + MockitoAnnotations.initMocks(this); + } + + @Test + public void getKind() { + assertThat(Kind.getKind(trust), is('U')); + assertThat(Kind.getKind(x509), is('X')); + assertThat(Kind.getKind(oauth), is('O')); + assertThat(Kind.getKind(basic), is('B')); + } + + @Test + public void coverageTest() { + @SuppressWarnings("unused") + Kind kind = new Kind(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_OAuth2FormPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_OAuth2FormPrincipal.java new file mode 100644 index 00000000..c0b1c2af --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_OAuth2FormPrincipal.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.principal.OAuth2FormPrincipal; + +public class JU_OAuth2FormPrincipal { + + private String username = "user"; + private String id = "id"; + + @Test + public void accessorsTest() { + OAuth2FormPrincipal oauth = new OAuth2FormPrincipal(id, username); + assertThat(oauth.getName(), is(username)); + assertThat(oauth.client_id(), is(id)); + assertThat(oauth.tag(), is("OAuth")); + } + + @Test + public void personalNameTest() { + OAuth2FormPrincipal oauth = new OAuth2FormPrincipal(id, username); + assertThat(oauth.personalName(), is(username + "|" + id)); + + oauth = new OAuth2FormPrincipal(id, null); + assertThat(oauth.personalName(), is(id)); + + oauth = new OAuth2FormPrincipal(id, id); + assertThat(oauth.personalName(), is(id)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_StringTagLookup.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_StringTagLookup.java new file mode 100644 index 00000000..cce86255 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_StringTagLookup.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.principal.StringTagLookup; + +public class JU_StringTagLookup { + + @Test + public void accessorsTest() throws Exception { + String tag = "tag"; + StringTagLookup stl = new StringTagLookup(tag); + assertThat(stl.lookup(), is(tag)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TaggedPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TaggedPrincipal.java new file mode 100644 index 00000000..2bbfee23 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TaggedPrincipal.java @@ -0,0 +1,68 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.principal.StringTagLookup; + +public class JU_TaggedPrincipal { + + private final String name = "stubbedName"; + private final String tag = "tag"; + + private class TaggedPrincipalStub extends TaggedPrincipal { + public TaggedPrincipalStub() { super(); } + public TaggedPrincipalStub(final TagLookup tl) { super(tl); } + @Override public String getName() { return name; } + @Override public String tag() { return null; } + } + + private class WhinyTagLookup implements TagLookup { + public WhinyTagLookup(final String tag) { } + @Override + public String lookup() throws CadiException { + throw new CadiException(); + } + } + + @Test + public void personalNameTest() { + TaggedPrincipal tp = new TaggedPrincipalStub(); + assertThat(tp.personalName(), is(name)); + + StringTagLookup stl = new StringTagLookup(tag); + tp = new TaggedPrincipalStub(stl); + assertThat(tp.personalName(), is(tag)); + + WhinyTagLookup wtl = new WhinyTagLookup(tag); + tp.setTagLookup(wtl); + assertThat(tp.personalName(), is(name)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TrustPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TrustPrincipal.java new file mode 100644 index 00000000..12b4bc9f --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_TrustPrincipal.java @@ -0,0 +1,91 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.security.Principal; + +import org.onap.aaf.cadi.UserChain; +import org.onap.aaf.cadi.principal.TaggedPrincipal; +import org.onap.aaf.cadi.principal.TrustPrincipal; + +public class JU_TrustPrincipal { + + private final String ucName = "UserChain"; + private final String uc = "This is a UserChain"; + private final String taggedName = "TaggedPrincipal"; + private final String tag = "tag"; + private final String pName = "Principal"; + + private class UserChainPrincipalStub implements Principal, UserChain { + @Override public String userChain() { return uc; } + @Override public String getName() { return ucName; } + } + + private class TaggedPrincipalStub extends TaggedPrincipal { + public TaggedPrincipalStub() { super(); } + @Override public String getName() { return taggedName; } + @Override public String tag() { return tag; } + } + + private class PrincipalStub implements Principal { + @Override public String getName() { return pName; } + } + + @Test + public void userChainConstructorTest() { + UserChainPrincipalStub ucps = new UserChainPrincipalStub(); + TrustPrincipal tp = new TrustPrincipal(ucps, taggedName); + assertThat(tp.getName(), is(taggedName)); + assertThat(tp.userChain(), is(uc)); + assertSame(tp.original(), ucps); + assertThat(tp.tag(), is(uc)); + assertThat(tp.personalName(), is(ucName + '[' + uc + ']')); + } + + @Test + public void taggedPrincipalConstructorTest() { + TaggedPrincipal tagged = new TaggedPrincipalStub(); + TrustPrincipal tp = new TrustPrincipal(tagged, taggedName); + assertThat(tp.getName(), is(taggedName)); + assertThat(tp.userChain(), is(tag)); + assertSame(tp.original(), tagged); + assertThat(tp.tag(), is(tag)); + assertThat(tp.personalName(), is(taggedName + '[' + tag + ']')); + } + + @Test + public void principalConstructorTest() { + Principal principal = new PrincipalStub(); + TrustPrincipal tp = new TrustPrincipal(principal, pName); + assertThat(tp.getName(), is(pName)); + assertThat(tp.userChain(), is(principal.getClass().getSimpleName())); + assertSame(tp.original(), principal); + assertThat(tp.tag(), is(principal.getClass().getSimpleName())); + assertThat(tp.personalName(), is(pName + '[' + principal.getClass().getSimpleName() + ']')); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_UnAuthPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_UnAuthPrincipal.java new file mode 100644 index 00000000..c0095131 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_UnAuthPrincipal.java @@ -0,0 +1,41 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.principal.UnAuthPrincipal; + +public class JU_UnAuthPrincipal { + + private final String name = "name"; + + @Test + public void accessorsTest() { + UnAuthPrincipal up = new UnAuthPrincipal(name); + assertThat(up.getName(), is(name)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java new file mode 100644 index 00000000..e62dda4f --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/principal/test/JU_X509Principal.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.principal.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.IOException; +import java.security.Principal; +import java.security.cert.CertificateEncodingException; +import java.security.cert.X509Certificate; + +import org.onap.aaf.cadi.principal.X509Principal; + +public class JU_X509Principal { + + private final String name = "x509 name"; + private final byte[] cred = "super duper secret password".getBytes(); + + @Mock + X509Certificate cert; + + @Mock + Principal subject; + + @Before + public void setup() throws CertificateEncodingException { + MockitoAnnotations.initMocks(this); + when(cert.getEncoded()).thenReturn(cred); + } + + @Test + public void constructor1Test() throws IOException { + X509Principal x509 = new X509Principal(name, cert); + // Call twice to hit both branches + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.toString(), is("X509 Authentication for " + name)); + assertTrue(x509.getCred().equals(cred)); + assertThat(x509.getName(), is(name)); + assertThat(x509.tag(), is("x509")); + } + + @Test + public void constructor2Test() throws IOException { + X509Principal x509 = new X509Principal(name, cert, cred); + // Call twice to hit both branches + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.toString(), is("X509 Authentication for " + name)); + assertTrue(x509.getCred().equals(cred)); + assertThat(x509.getName(), is(name)); + assertThat(x509.tag(), is("x509")); + } + + @Test + public void constructor3Test() throws IOException { + final String longName = "name@domain"; + when(subject.getName()).thenReturn("OU=" + longName + ",extra"); + when(cert.getSubjectDN()).thenReturn(subject); + X509Principal x509 = new X509Principal(cert, cred); + // Call twice to hit both branches + assertThat(x509.getAsHeader(), is("X509 " + cred)); + assertThat(x509.toString(), is("X509 Authentication for " + longName)); + assertTrue(x509.getCred().equals(cred)); + assertThat(x509.getName(), is(longName)); + + when(subject.getName()).thenReturn(longName + ",extra"); + when(cert.getSubjectDN()).thenReturn(subject); + try { + x509 = new X509Principal(cert, cred); + fail("Should have thrown an Exception"); + } catch(IOException e) { + assertThat(e.getMessage(), is("X509 does not have Identity as CN")); + } + + when(subject.getName()).thenReturn("OU=" + longName); + when(cert.getSubjectDN()).thenReturn(subject); + try { + x509 = new X509Principal(cert, cred); + fail("Should have thrown an Exception"); + } catch(IOException e) { + assertThat(e.getMessage(), is("X509 does not have Identity as CN")); + } + + when(subject.getName()).thenReturn("OU=" + name + ",exta"); + when(cert.getSubjectDN()).thenReturn(subject); + try { + x509 = new X509Principal(cert, cred); + fail("Should have thrown an Exception"); + } catch(IOException e) { + assertThat(e.getMessage(), is("X509 does not have Identity as CN")); + } + + } + + @Test + public void throwsTest() throws CertificateEncodingException { + when(cert.getEncoded()).thenThrow(new CertificateEncodingException()); + X509Principal x509 = new X509Principal(name, cert); + assertThat(x509.getCred(), is(nullValue())); + try { + x509.getAsHeader(); + fail("Should have thrown an Exception"); + } catch (IOException e) { + } + } + + @Test + public void getCredTest() { + X509Principal x509 = new X509Principal(name, cert); + // Call twice to hit both branches + assertTrue(x509.getCred().equals(cred)); + assertTrue(x509.getCred().equals(cred)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/taf/dos/test/JU_DenialOfServiceTaf.java b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/dos/test/JU_DenialOfServiceTaf.java new file mode 100644 index 00000000..ce49654b --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/dos/test/JU_DenialOfServiceTaf.java @@ -0,0 +1,369 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.taf.dos.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; +import org.mockito.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CachedPrincipal.Resp; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.config.Config; +import org.onap.aaf.cadi.taf.TafResp; +import org.onap.aaf.cadi.Taf.LifeForm; +import org.onap.aaf.cadi.taf.dos.DenialOfServiceTaf; +import org.onap.aaf.cadi.taf.dos.DenialOfServiceTaf.Counter; + +public class JU_DenialOfServiceTaf { + + @Mock + HttpServletResponse respMock; + + @Mock + HttpServletRequest reqMock1; + + @Mock + HttpServletRequest reqMock2; + + @Mock + HttpServletRequest reqMock3; + + @Mock + Access accessMock; + + private File dosIPFile; + private File dosIDFile; + private File dosDir; + private final String dosDirName = "test"; + + private final String id1 = "id1"; + private final String id2 = "id2"; + + private final String ip1 = "111.111.111.111"; + private final String ip2 = "222.222.222.222"; + + @Before + public void setup() throws IOException { + MockitoAnnotations.initMocks(this); + + dosDir = new File(dosDirName); + dosDir.mkdirs(); + dosIPFile = new File(dosDirName, "/dosIP"); + dosIDFile = new File(dosDirName, "/dosID"); + dosIPFile.delete(); + dosIDFile.delete(); + + when(accessMock.getProperty(Config.AAF_DATA_DIR, null)).thenReturn(dosDirName); + when(reqMock1.getRemoteAddr()).thenReturn(ip1); + when(reqMock2.getRemoteAddr()).thenReturn(ip2); + + setPrivateField(DenialOfServiceTaf.class, "deniedIP", null); + setPrivateField(DenialOfServiceTaf.class, "deniedID", null); + setPrivateField(DenialOfServiceTaf.class, "dosIP", null); + setPrivateField(DenialOfServiceTaf.class, "dosID", null); + } + + @After + public void tearDown() { + dosIPFile = new File(dosDirName, "/dosIP"); + dosIDFile = new File(dosDirName, "/dosID"); + dosIPFile.delete(); + dosIDFile.delete(); + } + + @Test + public void constructorTest() throws CadiException { + @SuppressWarnings("unused") + DenialOfServiceTaf dost; + + // coverage... + when(accessMock.getProperty(Config.AAF_DATA_DIR, null)).thenReturn(null); + dost = new DenialOfServiceTaf(accessMock); + + when(accessMock.getProperty(Config.AAF_DATA_DIR, null)).thenReturn(dosDirName); + dost = new DenialOfServiceTaf(accessMock); + + // more coverage... + dost = new DenialOfServiceTaf(accessMock); + + // more coverage... + setPrivateField(DenialOfServiceTaf.class, "dosID", null); + dost = new DenialOfServiceTaf(accessMock); + } + + @Test + public void validateTest() throws CadiException { + DenialOfServiceTaf dost; + TafResp tafResp; + + dost = new DenialOfServiceTaf(accessMock); + tafResp = dost.validate(LifeForm.SBLF, reqMock1, respMock); + + assertThat(tafResp.desc(), is("DenialOfServiceTaf is not processing this transaction: This Transaction is not denied")); + + assertThat(DenialOfServiceTaf.denyIP(ip1), is(true)); + + tafResp = dost.validate(LifeForm.SBLF, reqMock1, respMock); + assertThat(tafResp.desc(), is(ip1 + " is on the IP Denial list")); + + tafResp = dost.validate(LifeForm.SBLF, reqMock2, respMock); + assertThat(tafResp.desc(), is("DenialOfServiceTaf is not processing this transaction: This Transaction is not denied")); + } + + @Test + public void revalidateTest() throws CadiException { + DenialOfServiceTaf dost = new DenialOfServiceTaf(accessMock); + Resp resp = dost.revalidate(null, null); + assertThat(resp, is(Resp.NOT_MINE)); + } + + @Test + public void denyIPTest() throws CadiException { + assertThat(DenialOfServiceTaf.isDeniedIP(ip1), is(nullValue())); + assertThat(DenialOfServiceTaf.denyIP(ip1), is(true)); // true because it's been added + assertThat(DenialOfServiceTaf.denyIP(ip2), is(true)); // true because it's been added + assertThat(DenialOfServiceTaf.denyIP(ip1), is(false)); // false because it's already been added + assertThat(DenialOfServiceTaf.denyIP(ip2), is(false)); // false because it's already been added + + Counter counter; + counter = DenialOfServiceTaf.isDeniedIP(ip1); + assertThat(counter.getName(), is(ip1)); + assertThat(counter.getCount(), is(0)); + assertThat(counter.getLast(), is(0L)); + assertThat(counter.toString(), is(ip1 + " is on the denied list, but has not attempted Access" )); + + DenialOfServiceTaf dost = new DenialOfServiceTaf(accessMock); + dost.validate(LifeForm.SBLF, reqMock1, respMock); + long approxTime = System.currentTimeMillis(); + + counter = DenialOfServiceTaf.isDeniedIP(ip1); + assertThat(counter.getName(), is(ip1)); + assertThat(counter.getCount(), is(1)); + assertThat((Math.abs(approxTime - counter.getLast()) < 10), is(true)); + assertThat(counter.toString().contains(ip1), is(true)); + assertThat(counter.toString().contains(" has been denied 1 times since "), is(true)); + assertThat(counter.toString().contains(". Last denial was "), is(true)); + + // coverage... + dost.validate(LifeForm.SBLF, reqMock1, respMock); + + assertThat(DenialOfServiceTaf.removeDenyIP(ip1), is(true)); + assertThat(DenialOfServiceTaf.removeDenyIP(ip1), is(false)); + assertThat(DenialOfServiceTaf.removeDenyIP(ip2), is(true)); + assertThat(DenialOfServiceTaf.removeDenyIP(ip2), is(false)); + } + + @Test + public void denyIDTest() throws CadiException { + assertThat(DenialOfServiceTaf.isDeniedID(id1), is(nullValue())); + assertThat(DenialOfServiceTaf.denyID(id1), is(true)); // true because it's been added + assertThat(DenialOfServiceTaf.denyID(id2), is(true)); // true because it's been added + assertThat(DenialOfServiceTaf.denyID(id1), is(false)); // false because it's already been added + assertThat(DenialOfServiceTaf.denyID(id2), is(false)); // false because it's already been added + + Counter counter; + counter = DenialOfServiceTaf.isDeniedID(id1); + assertThat(counter.getName(), is(id1)); + assertThat(counter.getCount(), is(0)); + assertThat(counter.getLast(), is(0L)); + + assertThat(DenialOfServiceTaf.removeDenyID(id1), is(true)); + assertThat(DenialOfServiceTaf.removeDenyID(id1), is(false)); + assertThat(DenialOfServiceTaf.removeDenyID(id2), is(true)); + assertThat(DenialOfServiceTaf.removeDenyID(id2), is(false)); + } + + @Test + public void reportTest() throws CadiException { + DenialOfServiceTaf dost = new DenialOfServiceTaf(accessMock); + List denials = dost.report(); + assertThat(denials.size(), is(0)); + + DenialOfServiceTaf.denyID(id1); + DenialOfServiceTaf.denyID(id2); + + DenialOfServiceTaf.denyIP(ip1); + DenialOfServiceTaf.denyIP(ip2); + + denials = dost.report(); + assertThat(denials.size(), is(4)); + for (String denied : denials) { + switch (denied.split(" ", 2)[0]) { + case ip1: + case ip2: + case id1: + case id2: + break; + default: + fail("The line: [" + denied + "] shouldn't be in the report"); + } + } + } + + @Test + public void respDenyIDTest() { + TafResp tafResp = DenialOfServiceTaf.respDenyID(accessMock, id1); + assertThat(tafResp.desc(), is(id1 + " is on the Identity Denial list")); + } + + @Test + public void ipFileIOTest() throws CadiException, IOException { + @SuppressWarnings("unused") + DenialOfServiceTaf dost; + + dosIPFile.createNewFile(); + + // coverage... + DenialOfServiceTaf.denyIP(ip1); + DenialOfServiceTaf.removeDenyIP(ip1); + + dost = new DenialOfServiceTaf(accessMock); + DenialOfServiceTaf.denyIP(ip1); + DenialOfServiceTaf.denyIP(ip2); + // coverage... + DenialOfServiceTaf.denyIP(ip2); + + String contents = readContentsFromFile(dosIPFile); + assertThat(contents.contains(ip1), is(true)); + assertThat(contents.contains(ip2), is(true)); + + // Removing all ips should delete the file + assertThat(dosIPFile.exists(), is(true)); + DenialOfServiceTaf.removeDenyIP(ip1); + DenialOfServiceTaf.removeDenyIP(ip2); + assertThat(dosIPFile.exists(), is(false)); + + dosIPFile.createNewFile(); + + DenialOfServiceTaf.denyIP(ip1); + DenialOfServiceTaf.denyIP(ip2); + + setPrivateField(DenialOfServiceTaf.class, "dosIP", null); + dost = new DenialOfServiceTaf(accessMock); + + contents = readContentsFromFile(dosIPFile); + assertThat(contents.contains(ip1), is(true)); + assertThat(contents.contains(ip2), is(true)); + + dosIPFile.delete(); + + // coverage... + setPrivateField(DenialOfServiceTaf.class, "deniedIP", null); + DenialOfServiceTaf.denyIP(ip1); + dosIPFile.delete(); + DenialOfServiceTaf.removeDenyIP(ip1); + + // coverage... + dosIPFile.delete(); + setPrivateField(DenialOfServiceTaf.class, "dosIP", null); + dost = new DenialOfServiceTaf(accessMock); + } + + @Test + public void idFileIOTest() throws CadiException, IOException { + @SuppressWarnings("unused") + DenialOfServiceTaf dost; + + dosIDFile.createNewFile(); + + // coverage... + DenialOfServiceTaf.denyID(id1); + DenialOfServiceTaf.removeDenyID(id1); + + dost = new DenialOfServiceTaf(accessMock); + DenialOfServiceTaf.denyID(id1); + DenialOfServiceTaf.denyID(id2); + // coverage... + DenialOfServiceTaf.denyID(id2); + + String contents = readContentsFromFile(dosIDFile); + assertThat(contents.contains(id1), is(true)); + assertThat(contents.contains(id2), is(true)); + + // Removing all ids should delete the file + assertThat(dosIDFile.exists(), is(true)); + DenialOfServiceTaf.removeDenyID(id1); + DenialOfServiceTaf.removeDenyID(id2); + assertThat(dosIDFile.exists(), is(false)); + + dosIDFile.createNewFile(); + + DenialOfServiceTaf.denyID(id1); + DenialOfServiceTaf.denyID(id2); + + setPrivateField(DenialOfServiceTaf.class, "dosID", null); + dost = new DenialOfServiceTaf(accessMock); + + contents = readContentsFromFile(dosIDFile); + assertThat(contents.contains(id1), is(true)); + assertThat(contents.contains(id2), is(true)); + + dosIDFile.delete(); + + // coverage... + setPrivateField(DenialOfServiceTaf.class, "deniedID", null); + DenialOfServiceTaf.denyID(id1); + dosIDFile.delete(); + DenialOfServiceTaf.removeDenyID(id1); + + // coverage... + dosIDFile.delete(); + setPrivateField(DenialOfServiceTaf.class, "dosID", null); + dost = new DenialOfServiceTaf(accessMock); + } + + private void setPrivateField(Class clazz, String fieldName, Object value) { + try { + Field field = clazz.getDeclaredField(fieldName); + field.setAccessible(true); + field.set(null, value); + field.setAccessible(false); + } catch(Exception e) { + System.err.println("Could not set field [" + fieldName + "] to " + value); + } + } + + private String readContentsFromFile(File file) throws IOException { + BufferedReader br = new BufferedReader(new FileReader(file)); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = br.readLine()) != null) { + sb.append(line); + } + br.close(); + return sb.toString(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_EpiTaf.java b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_EpiTaf.java new file mode 100644 index 00000000..a1190590 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_EpiTaf.java @@ -0,0 +1,104 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.taf.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.IOException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.Taf; +import org.onap.aaf.cadi.taf.TafResp; +import org.onap.aaf.cadi.taf.TafResp.RESP; + +import org.onap.aaf.cadi.taf.EpiTaf; +import org.onap.aaf.cadi.taf.NullTaf; +import org.onap.aaf.cadi.Taf.LifeForm; +import org.onap.aaf.cadi.principal.TaggedPrincipal; + +public class JU_EpiTaf { + + @Test(expected = CadiException.class) + @SuppressWarnings("unused") + public void constructorTest() throws CadiException { + EpiTaf et = new EpiTaf(); + } + + @Test + public void validateTryAnotherTest() throws CadiException { + EpiTaf et = new EpiTaf(new TryAnotherTaf()); + TafResp output = et.validate(LifeForm.CBLF); + assertThat(output.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); + } + + @Test + public void validateTryAuthenticatingTest() throws CadiException { + EpiTaf et = new EpiTaf(new TryAuthenticatingTaf(), new TryAuthenticatingTaf()); + TafResp output = et.validate(LifeForm.CBLF); + assertThat(output.isAuthenticated(), is(RESP.TRY_AUTHENTICATING)); + output = et.validate(LifeForm.CBLF); + assertThat(output.isAuthenticated(), is(RESP.TRY_AUTHENTICATING)); + } + + @Test + public void validateDefaultCaseTest() throws CadiException { + EpiTaf et = new EpiTaf(new NullTaf()); + TafResp output = et.validate(LifeForm.CBLF); + assertThat(output.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); + } + + class TryAnotherTafResp implements TafResp { + @Override public boolean isValid() { return false; } + @Override public String desc() { return null; } + @Override public RESP isAuthenticated() { return RESP.TRY_ANOTHER_TAF; } + @Override public RESP authenticate() throws IOException { return null; } + @Override public TaggedPrincipal getPrincipal() { return null; } + @Override public Access getAccess() { return null; } + @Override public boolean isFailedAttempt() { return false; } + } + + class TryAnotherTaf implements Taf { + @Override public TafResp validate(LifeForm reading, String ... info) { return new TryAnotherTafResp(); } + } + + class TryAuthenticatingResp implements TafResp { + @Override public boolean isValid() { return false; } + @Override public String desc() { return null; } + @Override public RESP isAuthenticated() { return RESP.TRY_AUTHENTICATING; } + @Override public RESP authenticate() throws IOException { return null; } + @Override public TaggedPrincipal getPrincipal() { return null; } + @Override public Access getAccess() { return null; } + @Override public boolean isFailedAttempt() { return false; } + } + + class TryAuthenticatingTaf implements Taf { + @Override public TafResp validate(LifeForm reading, String ... info) { return new TryAuthenticatingResp(); } + } + + class EpiTafStub extends EpiTaf { + public EpiTafStub() throws CadiException { } + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_NullTaf.java b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_NullTaf.java new file mode 100644 index 00000000..f42184df --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_NullTaf.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.taf.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.IOException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.CachedPrincipal.Resp; +import org.onap.aaf.cadi.taf.TafResp; +import org.onap.aaf.cadi.taf.TafResp.RESP; + +import org.onap.aaf.cadi.taf.NullTaf; + +public class JU_NullTaf { + + @Test + public void test() throws IOException { + NullTaf nt = new NullTaf(); + TafResp singleton1 = nt.validate(null); + TafResp singleton2 = nt.validate(null, null, null); + Resp singleton3 = nt.revalidate(null, null); + + assertThat(singleton1, is(singleton2)); + + assertFalse(singleton1.isValid()); + + assertThat(singleton1.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); + + assertThat(singleton1.desc(), is("All Authentication denied")); + + assertThat(singleton1.authenticate(), is(RESP.NO_FURTHER_PROCESSING)); + + assertThat(singleton1.getPrincipal(), is(nullValue())); + + assertThat(singleton1.getAccess(), is(Access.NULL)); + + assertTrue(singleton1.isFailedAttempt()); + + assertThat(singleton3, is(Resp.NOT_MINE)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_PuntTafResp.java b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_PuntTafResp.java new file mode 100644 index 00000000..516f4044 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_PuntTafResp.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.taf.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import java.io.IOException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.taf.TafResp.RESP; + +import org.onap.aaf.cadi.taf.PuntTafResp; + + +public class JU_PuntTafResp { + + @Test + public void test() throws IOException { + String name = "name"; + String explanation = "example explanation"; + + PuntTafResp punt = new PuntTafResp(name, explanation); + + assertFalse(punt.isValid()); + assertThat(punt.isAuthenticated(), is(RESP.TRY_ANOTHER_TAF)); + assertThat(punt.desc(), is(name + " is not processing this transaction: " + explanation)); + assertThat(punt.authenticate(), is(RESP.TRY_ANOTHER_TAF)); + assertThat(punt.getPrincipal(), is(nullValue())); + assertThat(punt.getAccess(), is(Access.NULL)); + assertFalse(punt.isFailedAttempt()); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustNotTafResp.java b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustNotTafResp.java new file mode 100644 index 00000000..b032c020 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustNotTafResp.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.taf.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; +import org.mockito.*; + +import java.io.IOException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.taf.TafResp; +import org.onap.aaf.cadi.taf.TafResp.RESP; +import org.onap.aaf.cadi.taf.TrustNotTafResp; +import org.onap.aaf.cadi.principal.TaggedPrincipal; + +public class JU_TrustNotTafResp { + + @Mock + TafResp delegateMock; + + @Mock + TaggedPrincipal principalMock; + + @Mock + Access accessMock; + + private final String description = "Example Description"; + + @Before + public void setup() throws IOException { + MockitoAnnotations.initMocks(this); + + when(delegateMock.getPrincipal()).thenReturn(principalMock); + when(delegateMock.getAccess()).thenReturn(accessMock); + } + + @Test + public void test() throws IOException { + TrustNotTafResp ttr = new TrustNotTafResp(delegateMock, description); + assertThat(ttr.isValid(), is(false)); + assertThat(ttr.desc(), is(description)); + assertThat(ttr.authenticate(), is(RESP.NO_FURTHER_PROCESSING)); + assertThat(ttr.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); + assertThat(ttr.getPrincipal(), is(principalMock)); + assertThat(ttr.getAccess(), is(accessMock)); + assertThat(ttr.isFailedAttempt(), is(true)); + assertThat(ttr.toString(), is(description)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustTafResp.java b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustTafResp.java new file mode 100644 index 00000000..10b5f146 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/taf/test/JU_TrustTafResp.java @@ -0,0 +1,82 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.taf.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; +import org.mockito.*; + +import java.io.IOException; + +import org.onap.aaf.cadi.Access; +import org.onap.aaf.cadi.taf.TafResp; +import org.onap.aaf.cadi.taf.TafResp.RESP; +import org.onap.aaf.cadi.taf.TrustTafResp; +import org.onap.aaf.cadi.principal.TaggedPrincipal; + +public class JU_TrustTafResp { + + @Mock + TafResp delegateMock; + + @Mock + TaggedPrincipal principalMock; + + @Mock + Access accessMock; + + private final String description = "Example Description"; + private final String anotherDescription = "Another Description"; + private final String name = "name"; + + private final RESP resp = RESP.IS_AUTHENTICATED; + + @Before + public void setup() throws IOException { + MockitoAnnotations.initMocks(this); + + when(delegateMock.desc()).thenReturn(anotherDescription); + when(delegateMock.isValid()).thenReturn(true); + when(delegateMock.isAuthenticated()).thenReturn(resp); + when(delegateMock.authenticate()).thenReturn(resp); + when(delegateMock.getAccess()).thenReturn(accessMock); + when(delegateMock.isFailedAttempt()).thenReturn(true); + + when(principalMock.getName()).thenReturn(name); + } + + @Test + public void test() throws IOException { + TrustTafResp ttr = new TrustTafResp(delegateMock, principalMock, description); + assertThat(ttr.isValid(), is(true)); + assertThat(ttr.desc(), is(description + ' ' + anotherDescription)); + assertThat(ttr.authenticate(), is(resp)); + assertThat(ttr.isAuthenticated(), is(resp)); + assertThat(ttr.getPrincipal(), is(principalMock)); + assertThat(ttr.getAccess(), is(accessMock)); + assertThat(ttr.isFailedAttempt(), is(true)); + assertThat(ttr.toString(), is(name + " by trust of " + description + ' ' + anotherDescription)); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_Get.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_Get.java deleted file mode 100644 index 5f89060e..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_Get.java +++ /dev/null @@ -1,116 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.config; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import org.onap.aaf.cadi.PropAccess; -import org.onap.aaf.cadi.config.Get; - -public class JU_Get { - - private String defaultVal = "some default value"; - - private ByteArrayOutputStream outStream; - - private TestBean tb; - - @Before - public void setup() { - outStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outStream)); - } - - @After - public void tearDown() { - System.setOut(System.out); - } - - @Test - public void beanTest() { - tb = new TestBean(); - tb.setProperty1("prop1"); - - Get.Bean testBean = new Get.Bean(tb); - assertThat(testBean.get("property1", defaultVal, true), is("prop1")); - assertThat(testBean.get("property2", defaultVal, true), is(defaultVal)); - assertThat(testBean.get("thrower", defaultVal, true), is(defaultVal)); - } - - @Test - public void nullTest() { - assertThat(Get.NULL.get("name", defaultVal, true), is(defaultVal)); - } - - @Test - public void accessTest() { - String output; - - PropAccess access = new PropAccess(); - access.setProperty("tag", "value"); - Get.AccessGet accessGet = new Get.AccessGet(access); - - assertThat(accessGet.get("tag", defaultVal, true), is("value")); - output = outStream.toString().split(" ", 2)[1]; - assertThat(output, is("INIT [cadi] tag is set to value\n")); - - outStream.reset(); - - assertThat(accessGet.get("not a real tag", defaultVal, true), is(defaultVal)); - output = outStream.toString().split(" ", 2)[1]; - assertThat(output, is("INIT [cadi] not a real tag is set to " + defaultVal + "\n")); - - outStream.reset(); - - assertThat(accessGet.get("not a real tag", null, true), is(nullValue())); - output = outStream.toString().split(" ", 2)[1]; - assertThat(output, is("INIT [cadi] not a real tag is not set\n")); - - outStream.reset(); - - assertThat(accessGet.get("tag", defaultVal, false), is("value")); - assertThat(outStream.toString(), is("")); - } - - public class TestBean implements java.io.Serializable { - - private static final long serialVersionUID = 1L; - private String property1 = null; - private String property2 = null; - @SuppressWarnings("unused") - private String thrower = null; - - public TestBean() { } - public String getProperty1() { return property1; } - public void setProperty1(final String value) { this.property1 = value; } - public String getProperty2() { return property2; } - public void setProperty2(final String value) { this.property2 = value; } - public String getThrower() throws Exception { throw new Exception(); } - public void setThrower(final String value) { this.thrower = value; } - - } -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_GetAccess.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_GetAccess.java deleted file mode 100644 index 5acd39f9..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_GetAccess.java +++ /dev/null @@ -1,107 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.config; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; - -import org.onap.aaf.cadi.PropAccess; -import org.onap.aaf.cadi.config.Get; -import org.onap.aaf.cadi.config.GetAccess; - -public class JU_GetAccess { - - private String defaultVal = "some default value"; - - private ByteArrayOutputStream outStream; - - private PropAccess access; - private Get.AccessGet accessGet; - private File file; - private String filePath; - - @Before - public void setup() throws IOException { - outStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outStream)); - - file = File.createTempFile("GetAccess_test", ""); - filePath = file.getAbsolutePath(); - - access = new PropAccess(); - access.setProperty("cadi_prop_files", filePath); - accessGet = new Get.AccessGet(access); - - } - - @After - public void tearDown() { - System.setOut(System.out); - - file.delete(); - } - - @Test - public void constructorTest() { - String output; - - @SuppressWarnings("unused") - GetAccess getAccess = new GetAccess(accessGet); - String[] lines = outStream.toString().split("\n"); - assertThat(lines.length, is(2)); - output = lines[0].split(" ", 2)[1]; - assertThat(output, is("INIT [cadi] cadi_prop_files is set to " + filePath)); - output = lines[1].split(" ", 2)[1]; - assertThat(output, is("INIT [cadi] Loading CADI Properties from " + filePath)); - } - - @Test - public void getPropertyTest1() { - GetAccess getAccess = new GetAccess(accessGet); - - getAccess.setProperty("tag", "value"); - assertThat(getAccess.getProperty("tag", defaultVal), is("value")); - assertThat(getAccess.getProperty("not_a_tag", defaultVal), is(defaultVal)); - } - - @Test - public void getPropertyTest2() { - GetAccess getAccess = new GetAccess(accessGet); - - getAccess.setProperty("tag", "value"); - assertThat(getAccess.getProperty("tag"), is("value")); - assertThat(getAccess.getProperty("not_a_tag"), is(nullValue())); - } - - @Test - public void getTest() { - GetAccess getAccess = new GetAccess(accessGet); - assertThat((Get.AccessGet)getAccess.get(), is(accessGet)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java deleted file mode 100644 index e678421c..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_MultiGet.java +++ /dev/null @@ -1,68 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.config; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; - -import org.onap.aaf.cadi.PropAccess; -import org.onap.aaf.cadi.config.Get; -import org.onap.aaf.cadi.config.MultiGet; - -public class JU_MultiGet { - - private String defaultVal = "some default value"; - - private ByteArrayOutputStream outStream; - - private MultiGet multiGet; - private Get.AccessGet accessGet; - private PropAccess access; - - @Before - public void setup() throws IOException { - outStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outStream)); - - access = new PropAccess(); - access.setProperty("tag", "value"); - accessGet = new Get.AccessGet(access); - multiGet = new MultiGet(accessGet, Get.NULL); - } - - @After - public void tearDown() { - System.setOut(System.out); - } - - @Test - public void getTest() { - assertThat(multiGet.get("tag", defaultVal, false), is("value")); - assertThat(multiGet.get("not_a_tag", defaultVal, false), is(defaultVal)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java deleted file mode 100644 index ecd514fd..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_SecurityInfoC.java +++ /dev/null @@ -1,109 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.config; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; - -import org.junit.*; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.PropAccess; -import org.onap.aaf.cadi.SecuritySetter; -import org.onap.aaf.cadi.config.SecurityInfoC; - -public class JU_SecurityInfoC { - - ByteArrayOutputStream outStream; - ByteArrayOutputStream errStream; - - @Before - public void setup() { - outStream = new ByteArrayOutputStream(); - errStream = new ByteArrayOutputStream(); - - System.setOut(new PrintStream(outStream)); - System.setErr(new PrintStream(errStream)); - } - - @After - public void tearDown() { - System.setOut(System.out); - System.setErr(System.err); - } - - @Test - public void instanceTest() throws CadiException, MalformedURLException { - SecurityInfoC si = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); - assertThat(si.defSS.getID(), is(SecurityInfoC.DEF_ID)); - try { - si.defSS.setSecurity(new HttpURLConnectionStub()); - fail("Should have thrown an exception"); - } catch (CadiException e) { - assertTrue(e instanceof CadiException); - assertThat(e.getMessage(), is("No Client Credentials set.")); - } - assertThat(si.defSS.setLastResponse(0), is(0)); - - // Try it again for coverage - SecurityInfoC siClone = SecurityInfoC.instance(new PropAccess(), HttpURLConnection.class); - assertThat(siClone, is(si)); - } - - @Test - public void setTest() throws MalformedURLException, CadiException { - SecurityInfoC si = SecurityInfoC.instance(new PropAccess(), HttpURLConnectionStub.class); - SecuritySetter ss = new SecuritySetterStub(); - assertThat(si.set(ss), is(si)); - assertThat(si.defSS.getID(), is("Example ID")); - try { - si.defSS.setSecurity(new HttpURLConnectionStub()); - fail("Should have thrown an exception"); - } catch (CadiException e) { - assertTrue(e instanceof CadiException); - assertThat(e.getMessage(), is("Example exception")); - } - assertThat(si.defSS.setLastResponse(0), is(0)); - assertThat(si.defSS.setLastResponse(1), is(1)); - assertThat(si.defSS.setLastResponse(-1), is(-1)); - } - - private class HttpURLConnectionStub extends HttpURLConnection { - public HttpURLConnectionStub() throws MalformedURLException { super(new URL("http://www.example.com")); } - @Override public void disconnect() { } - @Override public boolean usingProxy() { return false; } - @Override public void connect() throws IOException { } - } - - private class SecuritySetterStub implements SecuritySetter { - public String getID() { return "Example ID"; } - public void setSecurity(CT client) throws CadiException { throw new CadiException("Example exception"); } - public int setLastResponse(int respCode) { return respCode; } - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_UsersDump.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_UsersDump.java deleted file mode 100644 index f6e366a7..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/config/JU_UsersDump.java +++ /dev/null @@ -1,140 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.config; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.*; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; - -import org.onap.aaf.cadi.AbsUserCache; -import org.onap.aaf.cadi.PropAccess; -import org.onap.aaf.cadi.config.UsersDump; -import org.onap.aaf.cadi.lur.LocalLur; -import org.onap.aaf.cadi.lur.LocalPermission; -import org.onap.aaf.cadi.util.Split; - -public class JU_UsersDump { - - private ByteArrayOutputStream outStream; - private ByteArrayOutputStream stdoutSuppressor; - - private static final String expected = "\n" + - "\n" + - "\n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - " \n" + - "\n"; - - private final static String groups = "myname:groupA,groupB"; - private final static String names = "admin:myname,yourname;suser:hisname,hername,m1234"; - - private AbsUserCache lur; - - @Before - public void setup() throws IOException { - outStream = new ByteArrayOutputStream(); - stdoutSuppressor = new ByteArrayOutputStream(); - - System.setOut(new PrintStream(stdoutSuppressor)); - - lur = new LocalLur(new PropAccess(), groups, names); - } - - @After - public void tearDown() { - System.setOut(System.out); - } - - @Test - public void writeTest() throws IOException { - UsersDump.write(outStream, lur); - String[] actualLines = Split.splitTrim('\n', outStream.toString()); - String[] expectedLines = Split.splitTrim('\n', expected); - - assertThat(actualLines.length, is(expectedLines.length)); - - // Check that the output starts with an XML tag - assertThat(actualLines[0], is(expectedLines[0])); - // Check that lines 2-4 are a comment - assertThat(actualLines[1], is(expectedLines[1])); - assertThat(actualLines[3], is(expectedLines[3])); - - // Check that the rest of the output matches the expected output - for (int i = 4; i < actualLines.length; i++) { - assertThat(actualLines[i], is(expectedLines[i])); - } - - // Run the test again with outStream as a PrintStream (for coverage) - outStream.reset(); - UsersDump.write(new PrintStream(outStream), lur); - actualLines = Split.splitTrim('\n', outStream.toString()); - - assertThat(actualLines.length, is(expectedLines.length)); - - // Check that the output starts with an XML tag - assertThat(actualLines[0], is(expectedLines[0])); - // Check that lines 2-4 are a comment - assertThat(actualLines[1], is(expectedLines[1])); - assertThat(actualLines[3], is(expectedLines[3])); - - // Check that the rest of the output matches the expected output - for (int i = 4; i < actualLines.length; i++) { - assertThat(actualLines[i], is(expectedLines[i])); - } - } - - @Test - public void updateUsersTest() { - String output; - File outputFile = new File("src/test/resources/userdump.xml"); - assertThat(outputFile.exists(), is(false)); - - output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur); - assertThat(output, is(nullValue())); - assertThat(outputFile.exists(), is(true)); - - output = UsersDump.updateUsers("src/test/resources/userdump.xml", (LocalLur) lur); - assertThat(output, is(nullValue())); - assertThat(outputFile.exists(), is(true)); - - outputFile.delete(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/filter/JU_FCGetTest.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/filter/JU_FCGetTest.java deleted file mode 100644 index 967334ac..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/filter/JU_FCGetTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.filter; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.when; - -import javax.servlet.FilterConfig; -import javax.servlet.ServletContext; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.onap.aaf.cadi.PropAccess; - -public class JU_FCGetTest { - - @Test - public void netYetTested() { - //fail("Tests not yet implemented"); - } - -// @Mock -// private ServletContext context; - -// @Mock -// private FilterConfig config; - -// @Mock -// private PropAccess access = new PropAccess(); - -// @Before -// public void setUp() { -// MockitoAnnotations.initMocks(this); -// } - -// @Test -// public void testGetStringFromDef() { -// PropAccess access = new PropAccess(); - -// FCGet fcGet = new FCGet(access, context, config); - -// String user = fcGet.get("user", "DefaultUser", true); - -// assertEquals(user, "DefaultUser"); -// } - -// @Test -// public void testGetStringFromContext() { -// PropAccess access = new PropAccess(); -// when(context.getInitParameter("user")).thenReturn("ContextUser"); - -// FCGet fcGet = new FCGet(access, context, null); - -// String user = fcGet.get("user", "DefaultUser", true); - -// assertEquals(user,"ContextUser"); -// } - -// @Test -// public void testGetStringFromFilter() { -// PropAccess access = new PropAccess(); -// when(config.getInitParameter("user")).thenReturn("FilterUser"); - -// FCGet fcGet = new FCGet(access, null, config); - -// String user = fcGet.get("user", "DefaultUser", true); - -// assertEquals(user,"FilterUser"); -// } - -// @Test -// public void testGetStringWithNullContextFilter() { - -// when(access.getProperty("user", "DefaultUser")).thenReturn(null); - -// FCGet fcGet = new FCGet(access, null, null); - -// String user = fcGet.get("user", "DefaultUser", true); - -// assertEquals(user,"DefaultUser"); -// } -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_ConfigPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_ConfigPrincipal.java deleted file mode 100644 index 897744ec..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_ConfigPrincipal.java +++ /dev/null @@ -1,79 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.lur; - -import org.junit.*; -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import java.lang.reflect.Field; - -import java.io.IOException; - -import org.onap.aaf.cadi.lur.ConfigPrincipal; - -public class JU_ConfigPrincipal { - - private final String name = "User"; - private final String pass = "pass"; - - // Expected output of base64("User:pass") - private final String b64encoded = "VXNlcjpwYXNz"; - - private Field content_field; - - @Before - public void setup() throws NoSuchFieldException { - content_field = ConfigPrincipal.class.getDeclaredField("content"); - content_field.setAccessible(true); - } - - @Test - public void testConfigPrincipalStringString() throws IOException, IllegalArgumentException, IllegalAccessException { - ConfigPrincipal p = new ConfigPrincipal(name, pass); - - assertThat(p.getName(), is(name)); - assertThat(p.toString(), is(name)); - assertThat(p.getCred(), is(pass.getBytes())); - assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); - content_field.set(p, "pass"); - assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); - - // One more time for coverage purposes - assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); - } - - @Test - public void testConfigPrincipalStringByteArray() throws IOException, IllegalArgumentException, IllegalAccessException { - ConfigPrincipal p = new ConfigPrincipal(name, pass.getBytes()); - - assertThat(p.getName(), is(name)); - assertThat(p.toString(), is(name)); - assertThat(p.getCred(), is(pass.getBytes())); - assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); - content_field.set(p, "pass"); - assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); - - // One more time for coverage purposes - assertThat(p.getAsBasicAuthHeader(), is("Basic " + b64encoded)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalLur.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalLur.java deleted file mode 100644 index 8388fa5f..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalLur.java +++ /dev/null @@ -1,103 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.lur; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertFalse; -import static junit.framework.Assert.assertTrue; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.security.Principal; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.TreeSet; - -import org.junit.Test; -import org.onap.aaf.cadi.Lur; -import org.onap.aaf.cadi.Permission; -import org.onap.aaf.cadi.PropAccess; -import org.onap.aaf.cadi.Symm; -import org.onap.aaf.cadi.CredVal.Type; -import org.onap.aaf.cadi.config.UsersDump; -import org.onap.aaf.cadi.lur.LocalLur; -import org.onap.aaf.cadi.lur.LocalPermission; - -public class JU_LocalLur { - - @Test - public void test() throws IOException { - Symm symmetric = Symm.baseCrypt().obtain(); - LocalLur up; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - baos.write(Symm.ENC.getBytes()); - symmetric.enpass("", baos); - PropAccess ta = new PropAccess(); - Lur ml = up = new LocalLur(ta,"myname:groupA,groupB","admin:myname,yourname;suser:hisname,hername,m1234%"+baos.toString()); - - -// Permission admin = new LocalPermission("admin"); -// Permission suser = new LocalPermission("suser"); -// -// // Check User fish -// assertTrue(ml.fish(new JUPrincipal("myname"),admin)); -// assertTrue(ml.fish(new JUPrincipal("hisname"),admin)); -// assertFalse(ml.fish(new JUPrincipal("noname"),admin)); -// assertTrue(ml.fish(new JUPrincipal("itsname"),suser)); -// assertTrue(ml.fish(new JUPrincipal("hername"),suser)); -// assertFalse(ml.fish(new JUPrincipal("myname"),suser)); -// -// // Check validate password -// assertTrue(up.validate("m1234",Type.PASSWORD, "".getBytes())); -// assertFalse(up.validate("m1234",Type.PASSWORD, "badPass".getBytes())); -// - // Check fishAll - Set set = new TreeSet(); - List perms = new ArrayList(); - ml.fishAll(new JUPrincipal("myname"), perms); - for(Permission p : perms) { - set.add(p.getKey()); - } -// assertEquals("[admin, groupA, groupB]",set.toString()); - UsersDump.write(System.out, up); - System.out.flush(); - - } - - // Simplistic Principal for testing purposes - private static class JUPrincipal implements Principal { - private String name; - public JUPrincipal(String name) { - this.name = name; - } -// @Override - public String getName() { - return name; - } - } - - - - - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalPermission.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalPermission.java deleted file mode 100644 index abc178a8..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_LocalPermission.java +++ /dev/null @@ -1,71 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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,Z - * * 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.lur; - -import static org.junit.Assert.*; - -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import static org.mockito.Mockito.*; - -import org.onap.aaf.cadi.lur.LocalPermission; -import org.onap.aaf.cadi.Permission; - -public class JU_LocalPermission { - - @Mock - Permission perm; - - private LocalPermission localPerm; - private String role = "Fake Role"; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - when(perm.getKey()).thenReturn(role); - - localPerm = new LocalPermission(role); - } - - @Test - public void getKeyTest() { - assertThat(localPerm.getKey(), is(role)); - } - - @Test - public void toStringTest() { - assertThat(localPerm.toString(), is(role)); - } - - @Test - public void matchTest() { - assertTrue(localPerm.match(perm)); - } - - @Test - public void permTypeTest() { - assertThat(localPerm.permType(), is("LOCAL")); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_NullLur.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_NullLur.java deleted file mode 100644 index d5740b3a..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/lur/JU_NullLur.java +++ /dev/null @@ -1,80 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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,Z - * * 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.lur; - -import java.security.Principal; -import java.util.List; - -import static org.hamcrest.CoreMatchers.*; -import static org.junit.Assert.*; -import org.junit.*; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import java.lang.reflect.*; - -import org.onap.aaf.cadi.Permission; -import org.onap.aaf.cadi.lur.NullLur; - -public class JU_NullLur { - - @Mock - Principal p; - - @Mock - Permission perm; - - @Mock - List perms; - - private NullLur nullLur; - - @Before - public void setup() { - MockitoAnnotations.initMocks(this); - - nullLur = new NullLur(); - } - - @Test - public void coverageTests() throws Exception { - - Field nullClass = NullLur.class.getDeclaredField("NULL"); - nullClass.setAccessible(true); - assertThat(((Permission) nullClass.get(NullLur.class)).permType(), is("")); - assertThat(((Permission) nullClass.get(NullLur.class)).getKey(), is("")); - assertFalse(((Permission) nullClass.get(NullLur.class)).match(perm)); - - nullLur.fishAll(p, perms); - nullLur.destroy(); - - assertFalse(nullLur.fish(p, perm)); - assertFalse(nullLur.handlesExclusively(perm)); - assertFalse(nullLur.handles(p)); - assertThat(nullLur.createPerm(""), is(nullClass.get(NullLur.class))); - - StringBuilder sb = new StringBuilder(); - nullLur.clear(p, sb); - assertThat(sb.toString(), is("NullLur\n")); - assertThat(nullLur.toString(), is("NullLur\n")); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_BasicPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_BasicPrincipal.java deleted file mode 100644 index 0faaca1a..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_BasicPrincipal.java +++ /dev/null @@ -1,123 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; - -import java.io.IOException; -import java.util.Date; - -import org.onap.aaf.cadi.BasicCred; -import org.onap.aaf.cadi.Symm; -import org.onap.aaf.cadi.principal.BasicPrincipal; -import org.onap.aaf.cadi.principal.StringTagLookup; -import org.onap.aaf.cadi.principal.TaggedPrincipal; -import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup; - -public class JU_BasicPrincipal { - - @Test - public void Constructor1Test() throws Exception { - // Test that everything works when the content doesn't contain "Basic" - BasicPrincipal bp = new BasicPrincipal("content", "domain"); - assertThat(bp.getName(), is("content")); - assertThat(bp.getCred(), is(nullValue())); - - // Test sending a user without an implicit domain - String name = "User"; - String password = "password"; - String content = name + ":" + password; - String domain = "exampledomain.com"; - String encrypted = new String(Symm.base64.encode(content.getBytes())); - bp = new BasicPrincipal("Basic " + encrypted, domain); - assertThat(bp.getShortName(), is(name)); - assertThat(bp.getName(), is(name + "@" + domain)); - assertThat(bp.getCred(), is(password.getBytes())); - - // Test sending a user with an implicit domain - String longName = name + "@" + domain + ":" + password; - encrypted = new String(Symm.base64.encode(longName.getBytes())); - bp = new BasicPrincipal("Basic " + encrypted, domain); - assertThat(bp.getShortName(), is(name)); - assertThat(bp.getName(), is(name + "@" + domain)); - assertThat(bp.getCred(), is(password.getBytes())); - - // Check that an exception is throw if no name is given in the content - try { - bp = new BasicPrincipal("Basic " + new String(Symm.base64.encode("no name".getBytes())), ""); - fail("Should have thrown an exception"); - } catch (IOException e) { - assertThat(e.getMessage(), is("Invalid Coding")); - } - } - - @Test - public void Constructor2Test() { - String name = "User"; - String password = "password"; - BasicCred bc = mock(BasicCred.class); - when(bc.getUser()).thenReturn(name); - when(bc.getCred()).thenReturn(password.getBytes()); - - BasicPrincipal bp = new BasicPrincipal(bc, "domain"); - assertThat(bp.getName(), is(name)); - assertThat(bp.getCred(), is(password.getBytes())); - } - - @Test - public void accessorsTest() throws IOException { - String name = "User"; - String password = "password"; - String content = name + ":" + password; - String domain = "exampledomain.com"; - String encrypted = new String(Symm.base64.encode(content.getBytes())); - String bearer = "bearer"; - long created = System.currentTimeMillis(); - BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, domain); - bp.setBearer(bearer); - - String expected = "Basic Authorization for " + name + "@" + domain + " evaluated on " + new Date(bp.created()).toString(); - assertTrue(Math.abs(bp.created() - created) < 10); - assertThat(bp.toString(), is(expected)); - assertThat(bp.tag(), is("BAth")); - assertThat(bp.personalName(), is(nullValue())); - - // This test hits the abstract class BearerPrincipal - assertThat(bp.getBearer(), is(bearer)); - } - - - @Test - public void coverageTest() throws IOException { - String name = "User"; - String password = "password:with:colons"; - String content = name + ":" + password; - String encrypted = new String(Symm.base64.encode(content.getBytes())); - @SuppressWarnings("unused") - BasicPrincipal bp = new BasicPrincipal("Basic " + encrypted, "domain"); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_CachedBasicPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_CachedBasicPrincipal.java deleted file mode 100644 index 39da6c09..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_CachedBasicPrincipal.java +++ /dev/null @@ -1,125 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.Date; - -import org.onap.aaf.cadi.BasicCred; -import org.onap.aaf.cadi.CachedPrincipal; -import org.onap.aaf.cadi.CachedPrincipal.Resp; -import org.onap.aaf.cadi.Symm; -import org.onap.aaf.cadi.principal.CachedBasicPrincipal; -import org.onap.aaf.cadi.principal.StringTagLookup; -import org.onap.aaf.cadi.principal.TaggedPrincipal; -import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup; -import org.onap.aaf.cadi.taf.HttpTaf; - -public class JU_CachedBasicPrincipal { - private Field creatorField; - private Field timeToLiveField; - - @Mock - private HttpTaf creator; - - private CachedPrincipal.Resp resp; - - @Before - public void setup() throws NoSuchFieldException, SecurityException { - MockitoAnnotations.initMocks(this); - - creatorField = CachedBasicPrincipal.class.getDeclaredField("creator"); - timeToLiveField = CachedBasicPrincipal.class.getDeclaredField("timeToLive"); - - creatorField.setAccessible(true); - timeToLiveField.setAccessible(true); - } - - @Test - public void Constructor1Test() throws IllegalArgumentException, IllegalAccessException { - String name = "User"; - String password = "password"; - BasicCred bc = mock(BasicCred.class); - when(bc.getUser()).thenReturn(name); - when(bc.getCred()).thenReturn(password.getBytes()); - - long timeToLive = 10000L; - long expires = System.currentTimeMillis() + timeToLive; - CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, bc, "domain", timeToLive); - - assertThat((HttpTaf)creatorField.get(cbp), is(creator)); - assertThat((Long)timeToLiveField.get(cbp), is(timeToLive)); - assertTrue(Math.abs(cbp.expires() - expires) < 10); - } - - @Test - public void Constructor2Test() throws Exception { - String name = "User"; - String password = "password"; - String content = name + ":" + password; - long timeToLive = 10000L; - long expires = System.currentTimeMillis() + timeToLive; - CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive); - - assertThat((HttpTaf)creatorField.get(cbp), is(creator)); - assertThat((Long)timeToLiveField.get(cbp), is(timeToLive)); - assertTrue(Math.abs(cbp.expires() - expires) < 10); - } - - @Test - public void revalidateTest() throws IOException, IllegalArgumentException, IllegalAccessException, InterruptedException { - resp = CachedPrincipal.Resp.REVALIDATED; - when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp); - - String name = "User"; - String password = "password"; - String content = name + ":" + password; - long timeToLive = 10000L; - long expires = System.currentTimeMillis() + timeToLive; - CachedBasicPrincipal cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive); - - assertTrue(Math.abs(cbp.expires() - expires) < 10); - - Thread.sleep(1); - expires = System.currentTimeMillis() + timeToLive; - assertThat(cbp.revalidate(new Object()), is(resp)); - assertTrue(Math.abs(cbp.expires() - expires) < 10); - - resp = CachedPrincipal.Resp.UNVALIDATED; - when(creator.revalidate((CachedPrincipal)any(), any())).thenReturn(resp); - expires = System.currentTimeMillis() + timeToLive; - cbp = new CachedBasicPrincipal(creator, content, "domain", timeToLive); - - assertThat(cbp.revalidate(new Object()), is(resp)); - assertTrue(Math.abs(cbp.expires() - expires) < 10); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_Kind.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_Kind.java deleted file mode 100644 index c66539c4..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_Kind.java +++ /dev/null @@ -1,70 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import org.onap.aaf.cadi.principal.BasicPrincipal; -import org.onap.aaf.cadi.principal.Kind; -import org.onap.aaf.cadi.principal.OAuth2FormPrincipal; -import org.onap.aaf.cadi.principal.TrustPrincipal; -import org.onap.aaf.cadi.principal.X509Principal; - -public class JU_Kind { - - @Mock - private TrustPrincipal trust; - - @Mock - private X509Principal x509; - - @Mock - private OAuth2FormPrincipal oauth; - - @Mock - private BasicPrincipal basic; - - @Before - public void setup() throws SecurityException { - MockitoAnnotations.initMocks(this); - } - - @Test - public void getKind() { - assertThat(Kind.getKind(trust), is('U')); - assertThat(Kind.getKind(x509), is('X')); - assertThat(Kind.getKind(oauth), is('O')); - assertThat(Kind.getKind(basic), is('B')); - } - - @Test - public void coverageTest() { - @SuppressWarnings("unused") - Kind kind = new Kind(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_OAuth2FormPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_OAuth2FormPrincipal.java deleted file mode 100644 index a7c14aa8..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_OAuth2FormPrincipal.java +++ /dev/null @@ -1,56 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.principal.OAuth2FormPrincipal; - -public class JU_OAuth2FormPrincipal { - - private String username = "user"; - private String id = "id"; - - @Test - public void accessorsTest() { - OAuth2FormPrincipal oauth = new OAuth2FormPrincipal(id, username); - assertThat(oauth.getName(), is(username)); - assertThat(oauth.client_id(), is(id)); - assertThat(oauth.tag(), is("OAuth")); - } - - @Test - public void personalNameTest() { - OAuth2FormPrincipal oauth = new OAuth2FormPrincipal(id, username); - assertThat(oauth.personalName(), is(username + "|" + id)); - - oauth = new OAuth2FormPrincipal(id, null); - assertThat(oauth.personalName(), is(id)); - - oauth = new OAuth2FormPrincipal(id, id); - assertThat(oauth.personalName(), is(id)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_StringTagLookup.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_StringTagLookup.java deleted file mode 100644 index ff1f3f8a..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_StringTagLookup.java +++ /dev/null @@ -1,40 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.principal.StringTagLookup; - -public class JU_StringTagLookup { - - @Test - public void accessorsTest() throws Exception { - String tag = "tag"; - StringTagLookup stl = new StringTagLookup(tag); - assertThat(stl.lookup(), is(tag)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TaggedPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TaggedPrincipal.java deleted file mode 100644 index 4674db70..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TaggedPrincipal.java +++ /dev/null @@ -1,68 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.principal.TaggedPrincipal; -import org.onap.aaf.cadi.principal.TaggedPrincipal.TagLookup; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.principal.StringTagLookup; - -public class JU_TaggedPrincipal { - - private final String name = "stubbedName"; - private final String tag = "tag"; - - private class TaggedPrincipalStub extends TaggedPrincipal { - public TaggedPrincipalStub() { super(); } - public TaggedPrincipalStub(final TagLookup tl) { super(tl); } - @Override public String getName() { return name; } - @Override public String tag() { return null; } - } - - private class WhinyTagLookup implements TagLookup { - public WhinyTagLookup(final String tag) { } - @Override - public String lookup() throws CadiException { - throw new CadiException(); - } - } - - @Test - public void personalNameTest() { - TaggedPrincipal tp = new TaggedPrincipalStub(); - assertThat(tp.personalName(), is(name)); - - StringTagLookup stl = new StringTagLookup(tag); - tp = new TaggedPrincipalStub(stl); - assertThat(tp.personalName(), is(tag)); - - WhinyTagLookup wtl = new WhinyTagLookup(tag); - tp.setTagLookup(wtl); - assertThat(tp.personalName(), is(name)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TrustPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TrustPrincipal.java deleted file mode 100644 index 4a7333e2..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_TrustPrincipal.java +++ /dev/null @@ -1,91 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.security.Principal; - -import org.onap.aaf.cadi.UserChain; -import org.onap.aaf.cadi.principal.TaggedPrincipal; -import org.onap.aaf.cadi.principal.TrustPrincipal; - -public class JU_TrustPrincipal { - - private final String ucName = "UserChain"; - private final String uc = "This is a UserChain"; - private final String taggedName = "TaggedPrincipal"; - private final String tag = "tag"; - private final String pName = "Principal"; - - private class UserChainPrincipalStub implements Principal, UserChain { - @Override public String userChain() { return uc; } - @Override public String getName() { return ucName; } - } - - private class TaggedPrincipalStub extends TaggedPrincipal { - public TaggedPrincipalStub() { super(); } - @Override public String getName() { return taggedName; } - @Override public String tag() { return tag; } - } - - private class PrincipalStub implements Principal { - @Override public String getName() { return pName; } - } - - @Test - public void userChainConstructorTest() { - UserChainPrincipalStub ucps = new UserChainPrincipalStub(); - TrustPrincipal tp = new TrustPrincipal(ucps, taggedName); - assertThat(tp.getName(), is(taggedName)); - assertThat(tp.userChain(), is(uc)); - assertSame(tp.original(), ucps); - assertThat(tp.tag(), is(uc)); - assertThat(tp.personalName(), is(ucName + '[' + uc + ']')); - } - - @Test - public void taggedPrincipalConstructorTest() { - TaggedPrincipal tagged = new TaggedPrincipalStub(); - TrustPrincipal tp = new TrustPrincipal(tagged, taggedName); - assertThat(tp.getName(), is(taggedName)); - assertThat(tp.userChain(), is(tag)); - assertSame(tp.original(), tagged); - assertThat(tp.tag(), is(tag)); - assertThat(tp.personalName(), is(taggedName + '[' + tag + ']')); - } - - @Test - public void principalConstructorTest() { - Principal principal = new PrincipalStub(); - TrustPrincipal tp = new TrustPrincipal(principal, pName); - assertThat(tp.getName(), is(pName)); - assertThat(tp.userChain(), is(principal.getClass().getSimpleName())); - assertSame(tp.original(), principal); - assertThat(tp.tag(), is(principal.getClass().getSimpleName())); - assertThat(tp.personalName(), is(pName + '[' + principal.getClass().getSimpleName() + ']')); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_UnAuthPrincipal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_UnAuthPrincipal.java deleted file mode 100644 index 9d694a53..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_UnAuthPrincipal.java +++ /dev/null @@ -1,41 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.principal.UnAuthPrincipal; - -public class JU_UnAuthPrincipal { - - private final String name = "name"; - - @Test - public void accessorsTest() { - UnAuthPrincipal up = new UnAuthPrincipal(name); - assertThat(up.getName(), is(name)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_X509Principal.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_X509Principal.java deleted file mode 100644 index a45ce24e..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/principal/JU_X509Principal.java +++ /dev/null @@ -1,140 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.principal; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - -import java.io.IOException; -import java.security.Principal; -import java.security.cert.CertificateEncodingException; -import java.security.cert.X509Certificate; - -import org.onap.aaf.cadi.principal.X509Principal; - -public class JU_X509Principal { - - private final String name = "x509 name"; - private final byte[] cred = "super duper secret password".getBytes(); - - @Mock - X509Certificate cert; - - @Mock - Principal subject; - - @Before - public void setup() throws CertificateEncodingException { - MockitoAnnotations.initMocks(this); - when(cert.getEncoded()).thenReturn(cred); - } - - @Test - public void constructor1Test() throws IOException { - X509Principal x509 = new X509Principal(name, cert); - // Call twice to hit both branches - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.toString(), is("X509 Authentication for " + name)); - assertTrue(x509.getCred().equals(cred)); - assertThat(x509.getName(), is(name)); - assertThat(x509.tag(), is("x509")); - } - - @Test - public void constructor2Test() throws IOException { - X509Principal x509 = new X509Principal(name, cert, cred); - // Call twice to hit both branches - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.toString(), is("X509 Authentication for " + name)); - assertTrue(x509.getCred().equals(cred)); - assertThat(x509.getName(), is(name)); - assertThat(x509.tag(), is("x509")); - } - - @Test - public void constructor3Test() throws IOException { - final String longName = "name@domain"; - when(subject.getName()).thenReturn("OU=" + longName + ",extra"); - when(cert.getSubjectDN()).thenReturn(subject); - X509Principal x509 = new X509Principal(cert, cred); - // Call twice to hit both branches - assertThat(x509.getAsHeader(), is("X509 " + cred)); - assertThat(x509.toString(), is("X509 Authentication for " + longName)); - assertTrue(x509.getCred().equals(cred)); - assertThat(x509.getName(), is(longName)); - - when(subject.getName()).thenReturn(longName + ",extra"); - when(cert.getSubjectDN()).thenReturn(subject); - try { - x509 = new X509Principal(cert, cred); - fail("Should have thrown an Exception"); - } catch(IOException e) { - assertThat(e.getMessage(), is("X509 does not have Identity as CN")); - } - - when(subject.getName()).thenReturn("OU=" + longName); - when(cert.getSubjectDN()).thenReturn(subject); - try { - x509 = new X509Principal(cert, cred); - fail("Should have thrown an Exception"); - } catch(IOException e) { - assertThat(e.getMessage(), is("X509 does not have Identity as CN")); - } - - when(subject.getName()).thenReturn("OU=" + name + ",exta"); - when(cert.getSubjectDN()).thenReturn(subject); - try { - x509 = new X509Principal(cert, cred); - fail("Should have thrown an Exception"); - } catch(IOException e) { - assertThat(e.getMessage(), is("X509 does not have Identity as CN")); - } - - } - - @Test - public void throwsTest() throws CertificateEncodingException { - when(cert.getEncoded()).thenThrow(new CertificateEncodingException()); - X509Principal x509 = new X509Principal(name, cert); - assertThat(x509.getCred(), is(nullValue())); - try { - x509.getAsHeader(); - fail("Should have thrown an Exception"); - } catch (IOException e) { - } - } - - @Test - public void getCredTest() { - X509Principal x509 = new X509Principal(name, cert); - // Call twice to hit both branches - assertTrue(x509.getCred().equals(cred)); - assertTrue(x509.getCred().equals(cred)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_EpiTaf.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_EpiTaf.java deleted file mode 100644 index baab0b6a..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_EpiTaf.java +++ /dev/null @@ -1,104 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.taf; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.io.IOException; - -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.Taf; -import org.onap.aaf.cadi.taf.TafResp; -import org.onap.aaf.cadi.taf.TafResp.RESP; - -import org.onap.aaf.cadi.taf.EpiTaf; -import org.onap.aaf.cadi.taf.NullTaf; -import org.onap.aaf.cadi.Taf.LifeForm; -import org.onap.aaf.cadi.principal.TaggedPrincipal; - -public class JU_EpiTaf { - - @Test(expected = CadiException.class) - @SuppressWarnings("unused") - public void constructorTest() throws CadiException { - EpiTaf et = new EpiTaf(); - } - - @Test - public void validateTryAnotherTest() throws CadiException { - EpiTaf et = new EpiTaf(new TryAnotherTaf()); - TafResp output = et.validate(LifeForm.CBLF); - assertThat(output.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); - } - - @Test - public void validateTryAuthenticatingTest() throws CadiException { - EpiTaf et = new EpiTaf(new TryAuthenticatingTaf(), new TryAuthenticatingTaf()); - TafResp output = et.validate(LifeForm.CBLF); - assertThat(output.isAuthenticated(), is(RESP.TRY_AUTHENTICATING)); - output = et.validate(LifeForm.CBLF); - assertThat(output.isAuthenticated(), is(RESP.TRY_AUTHENTICATING)); - } - - @Test - public void validateDefaultCaseTest() throws CadiException { - EpiTaf et = new EpiTaf(new NullTaf()); - TafResp output = et.validate(LifeForm.CBLF); - assertThat(output.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); - } - - class TryAnotherTafResp implements TafResp { - @Override public boolean isValid() { return false; } - @Override public String desc() { return null; } - @Override public RESP isAuthenticated() { return RESP.TRY_ANOTHER_TAF; } - @Override public RESP authenticate() throws IOException { return null; } - @Override public TaggedPrincipal getPrincipal() { return null; } - @Override public Access getAccess() { return null; } - @Override public boolean isFailedAttempt() { return false; } - } - - class TryAnotherTaf implements Taf { - @Override public TafResp validate(LifeForm reading, String ... info) { return new TryAnotherTafResp(); } - } - - class TryAuthenticatingResp implements TafResp { - @Override public boolean isValid() { return false; } - @Override public String desc() { return null; } - @Override public RESP isAuthenticated() { return RESP.TRY_AUTHENTICATING; } - @Override public RESP authenticate() throws IOException { return null; } - @Override public TaggedPrincipal getPrincipal() { return null; } - @Override public Access getAccess() { return null; } - @Override public boolean isFailedAttempt() { return false; } - } - - class TryAuthenticatingTaf implements Taf { - @Override public TafResp validate(LifeForm reading, String ... info) { return new TryAuthenticatingResp(); } - } - - class EpiTafStub extends EpiTaf { - public EpiTafStub() throws CadiException { } - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_NullTaf.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_NullTaf.java deleted file mode 100644 index 9131172e..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_NullTaf.java +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.taf; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.io.IOException; - -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.CachedPrincipal.Resp; -import org.onap.aaf.cadi.taf.TafResp; -import org.onap.aaf.cadi.taf.TafResp.RESP; - -import org.onap.aaf.cadi.taf.NullTaf; - -public class JU_NullTaf { - - @Test - public void test() throws IOException { - NullTaf nt = new NullTaf(); - TafResp singleton1 = nt.validate(null); - TafResp singleton2 = nt.validate(null, null, null); - Resp singleton3 = nt.revalidate(null, null); - - assertThat(singleton1, is(singleton2)); - - assertFalse(singleton1.isValid()); - - assertThat(singleton1.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); - - assertThat(singleton1.desc(), is("All Authentication denied")); - - assertThat(singleton1.authenticate(), is(RESP.NO_FURTHER_PROCESSING)); - - assertThat(singleton1.getPrincipal(), is(nullValue())); - - assertThat(singleton1.getAccess(), is(Access.NULL)); - - assertTrue(singleton1.isFailedAttempt()); - - assertThat(singleton3, is(Resp.NOT_MINE)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_PuntTafResp.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_PuntTafResp.java deleted file mode 100644 index 8c5397dc..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_PuntTafResp.java +++ /dev/null @@ -1,54 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.taf; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import java.io.IOException; - -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.taf.TafResp.RESP; - -import org.onap.aaf.cadi.taf.PuntTafResp; - - -public class JU_PuntTafResp { - - @Test - public void test() throws IOException { - String name = "name"; - String explanation = "example explanation"; - - PuntTafResp punt = new PuntTafResp(name, explanation); - - assertFalse(punt.isValid()); - assertThat(punt.isAuthenticated(), is(RESP.TRY_ANOTHER_TAF)); - assertThat(punt.desc(), is(name + " is not processing this transaction: " + explanation)); - assertThat(punt.authenticate(), is(RESP.TRY_ANOTHER_TAF)); - assertThat(punt.getPrincipal(), is(nullValue())); - assertThat(punt.getAccess(), is(Access.NULL)); - assertFalse(punt.isFailedAttempt()); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustNotTafResp.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustNotTafResp.java deleted file mode 100644 index 798348e8..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustNotTafResp.java +++ /dev/null @@ -1,72 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.taf; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.*; - -import java.io.IOException; - -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.taf.TafResp; -import org.onap.aaf.cadi.taf.TafResp.RESP; -import org.onap.aaf.cadi.taf.TrustNotTafResp; -import org.onap.aaf.cadi.principal.TaggedPrincipal; - -public class JU_TrustNotTafResp { - - @Mock - TafResp delegateMock; - - @Mock - TaggedPrincipal principalMock; - - @Mock - Access accessMock; - - private final String description = "Example Description"; - - @Before - public void setup() throws IOException { - MockitoAnnotations.initMocks(this); - - when(delegateMock.getPrincipal()).thenReturn(principalMock); - when(delegateMock.getAccess()).thenReturn(accessMock); - } - - @Test - public void test() throws IOException { - TrustNotTafResp ttr = new TrustNotTafResp(delegateMock, description); - assertThat(ttr.isValid(), is(false)); - assertThat(ttr.desc(), is(description)); - assertThat(ttr.authenticate(), is(RESP.NO_FURTHER_PROCESSING)); - assertThat(ttr.isAuthenticated(), is(RESP.NO_FURTHER_PROCESSING)); - assertThat(ttr.getPrincipal(), is(principalMock)); - assertThat(ttr.getAccess(), is(accessMock)); - assertThat(ttr.isFailedAttempt(), is(true)); - assertThat(ttr.toString(), is(description)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustTafResp.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustTafResp.java deleted file mode 100644 index 04e3a1fc..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/JU_TrustTafResp.java +++ /dev/null @@ -1,82 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.taf; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.*; - -import java.io.IOException; - -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.taf.TafResp; -import org.onap.aaf.cadi.taf.TafResp.RESP; -import org.onap.aaf.cadi.taf.TrustTafResp; -import org.onap.aaf.cadi.principal.TaggedPrincipal; - -public class JU_TrustTafResp { - - @Mock - TafResp delegateMock; - - @Mock - TaggedPrincipal principalMock; - - @Mock - Access accessMock; - - private final String description = "Example Description"; - private final String anotherDescription = "Another Description"; - private final String name = "name"; - - private final RESP resp = RESP.IS_AUTHENTICATED; - - @Before - public void setup() throws IOException { - MockitoAnnotations.initMocks(this); - - when(delegateMock.desc()).thenReturn(anotherDescription); - when(delegateMock.isValid()).thenReturn(true); - when(delegateMock.isAuthenticated()).thenReturn(resp); - when(delegateMock.authenticate()).thenReturn(resp); - when(delegateMock.getAccess()).thenReturn(accessMock); - when(delegateMock.isFailedAttempt()).thenReturn(true); - - when(principalMock.getName()).thenReturn(name); - } - - @Test - public void test() throws IOException { - TrustTafResp ttr = new TrustTafResp(delegateMock, principalMock, description); - assertThat(ttr.isValid(), is(true)); - assertThat(ttr.desc(), is(description + ' ' + anotherDescription)); - assertThat(ttr.authenticate(), is(resp)); - assertThat(ttr.isAuthenticated(), is(resp)); - assertThat(ttr.getPrincipal(), is(principalMock)); - assertThat(ttr.getAccess(), is(accessMock)); - assertThat(ttr.isFailedAttempt(), is(true)); - assertThat(ttr.toString(), is(name + " by trust of " + description + ' ' + anotherDescription)); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/dos/JU_DenialOfServiceTaf.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/dos/JU_DenialOfServiceTaf.java deleted file mode 100644 index 40933b41..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/taf/dos/JU_DenialOfServiceTaf.java +++ /dev/null @@ -1,369 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.taf.dos; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; -import org.mockito.*; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.List; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.CachedPrincipal.Resp; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.config.Config; -import org.onap.aaf.cadi.taf.TafResp; -import org.onap.aaf.cadi.Taf.LifeForm; -import org.onap.aaf.cadi.taf.dos.DenialOfServiceTaf; -import org.onap.aaf.cadi.taf.dos.DenialOfServiceTaf.Counter; - -public class JU_DenialOfServiceTaf { - - @Mock - HttpServletResponse respMock; - - @Mock - HttpServletRequest reqMock1; - - @Mock - HttpServletRequest reqMock2; - - @Mock - HttpServletRequest reqMock3; - - @Mock - Access accessMock; - - private File dosIPFile; - private File dosIDFile; - private File dosDir; - private final String dosDirName = "test"; - - private final String id1 = "id1"; - private final String id2 = "id2"; - - private final String ip1 = "111.111.111.111"; - private final String ip2 = "222.222.222.222"; - - @Before - public void setup() throws IOException { - MockitoAnnotations.initMocks(this); - - dosDir = new File(dosDirName); - dosDir.mkdirs(); - dosIPFile = new File(dosDirName, "/dosIP"); - dosIDFile = new File(dosDirName, "/dosID"); - dosIPFile.delete(); - dosIDFile.delete(); - - when(accessMock.getProperty(Config.AAF_DATA_DIR, null)).thenReturn(dosDirName); - when(reqMock1.getRemoteAddr()).thenReturn(ip1); - when(reqMock2.getRemoteAddr()).thenReturn(ip2); - - setPrivateField(DenialOfServiceTaf.class, "deniedIP", null); - setPrivateField(DenialOfServiceTaf.class, "deniedID", null); - setPrivateField(DenialOfServiceTaf.class, "dosIP", null); - setPrivateField(DenialOfServiceTaf.class, "dosID", null); - } - - @After - public void tearDown() { - dosIPFile = new File(dosDirName, "/dosIP"); - dosIDFile = new File(dosDirName, "/dosID"); - dosIPFile.delete(); - dosIDFile.delete(); - } - - @Test - public void constructorTest() throws CadiException { - @SuppressWarnings("unused") - DenialOfServiceTaf dost; - - // coverage... - when(accessMock.getProperty(Config.AAF_DATA_DIR, null)).thenReturn(null); - dost = new DenialOfServiceTaf(accessMock); - - when(accessMock.getProperty(Config.AAF_DATA_DIR, null)).thenReturn(dosDirName); - dost = new DenialOfServiceTaf(accessMock); - - // more coverage... - dost = new DenialOfServiceTaf(accessMock); - - // more coverage... - setPrivateField(DenialOfServiceTaf.class, "dosID", null); - dost = new DenialOfServiceTaf(accessMock); - } - - @Test - public void validateTest() throws CadiException { - DenialOfServiceTaf dost; - TafResp tafResp; - - dost = new DenialOfServiceTaf(accessMock); - tafResp = dost.validate(LifeForm.SBLF, reqMock1, respMock); - - assertThat(tafResp.desc(), is("DenialOfServiceTaf is not processing this transaction: This Transaction is not denied")); - - assertThat(DenialOfServiceTaf.denyIP(ip1), is(true)); - - tafResp = dost.validate(LifeForm.SBLF, reqMock1, respMock); - assertThat(tafResp.desc(), is(ip1 + " is on the IP Denial list")); - - tafResp = dost.validate(LifeForm.SBLF, reqMock2, respMock); - assertThat(tafResp.desc(), is("DenialOfServiceTaf is not processing this transaction: This Transaction is not denied")); - } - - @Test - public void revalidateTest() throws CadiException { - DenialOfServiceTaf dost = new DenialOfServiceTaf(accessMock); - Resp resp = dost.revalidate(null, null); - assertThat(resp, is(Resp.NOT_MINE)); - } - - @Test - public void denyIPTest() throws CadiException { - assertThat(DenialOfServiceTaf.isDeniedIP(ip1), is(nullValue())); - assertThat(DenialOfServiceTaf.denyIP(ip1), is(true)); // true because it's been added - assertThat(DenialOfServiceTaf.denyIP(ip2), is(true)); // true because it's been added - assertThat(DenialOfServiceTaf.denyIP(ip1), is(false)); // false because it's already been added - assertThat(DenialOfServiceTaf.denyIP(ip2), is(false)); // false because it's already been added - - Counter counter; - counter = DenialOfServiceTaf.isDeniedIP(ip1); - assertThat(counter.getName(), is(ip1)); - assertThat(counter.getCount(), is(0)); - assertThat(counter.getLast(), is(0L)); - assertThat(counter.toString(), is(ip1 + " is on the denied list, but has not attempted Access" )); - - DenialOfServiceTaf dost = new DenialOfServiceTaf(accessMock); - dost.validate(LifeForm.SBLF, reqMock1, respMock); - long approxTime = System.currentTimeMillis(); - - counter = DenialOfServiceTaf.isDeniedIP(ip1); - assertThat(counter.getName(), is(ip1)); - assertThat(counter.getCount(), is(1)); - assertThat((Math.abs(approxTime - counter.getLast()) < 10), is(true)); - assertThat(counter.toString().contains(ip1), is(true)); - assertThat(counter.toString().contains(" has been denied 1 times since "), is(true)); - assertThat(counter.toString().contains(". Last denial was "), is(true)); - - // coverage... - dost.validate(LifeForm.SBLF, reqMock1, respMock); - - assertThat(DenialOfServiceTaf.removeDenyIP(ip1), is(true)); - assertThat(DenialOfServiceTaf.removeDenyIP(ip1), is(false)); - assertThat(DenialOfServiceTaf.removeDenyIP(ip2), is(true)); - assertThat(DenialOfServiceTaf.removeDenyIP(ip2), is(false)); - } - - @Test - public void denyIDTest() throws CadiException { - assertThat(DenialOfServiceTaf.isDeniedID(id1), is(nullValue())); - assertThat(DenialOfServiceTaf.denyID(id1), is(true)); // true because it's been added - assertThat(DenialOfServiceTaf.denyID(id2), is(true)); // true because it's been added - assertThat(DenialOfServiceTaf.denyID(id1), is(false)); // false because it's already been added - assertThat(DenialOfServiceTaf.denyID(id2), is(false)); // false because it's already been added - - Counter counter; - counter = DenialOfServiceTaf.isDeniedID(id1); - assertThat(counter.getName(), is(id1)); - assertThat(counter.getCount(), is(0)); - assertThat(counter.getLast(), is(0L)); - - assertThat(DenialOfServiceTaf.removeDenyID(id1), is(true)); - assertThat(DenialOfServiceTaf.removeDenyID(id1), is(false)); - assertThat(DenialOfServiceTaf.removeDenyID(id2), is(true)); - assertThat(DenialOfServiceTaf.removeDenyID(id2), is(false)); - } - - @Test - public void reportTest() throws CadiException { - DenialOfServiceTaf dost = new DenialOfServiceTaf(accessMock); - List denials = dost.report(); - assertThat(denials.size(), is(0)); - - DenialOfServiceTaf.denyID(id1); - DenialOfServiceTaf.denyID(id2); - - DenialOfServiceTaf.denyIP(ip1); - DenialOfServiceTaf.denyIP(ip2); - - denials = dost.report(); - assertThat(denials.size(), is(4)); - for (String denied : denials) { - switch (denied.split(" ", 2)[0]) { - case ip1: - case ip2: - case id1: - case id2: - break; - default: - fail("The line: [" + denied + "] shouldn't be in the report"); - } - } - } - - @Test - public void respDenyIDTest() { - TafResp tafResp = DenialOfServiceTaf.respDenyID(accessMock, id1); - assertThat(tafResp.desc(), is(id1 + " is on the Identity Denial list")); - } - - @Test - public void ipFileIOTest() throws CadiException, IOException { - @SuppressWarnings("unused") - DenialOfServiceTaf dost; - - dosIPFile.createNewFile(); - - // coverage... - DenialOfServiceTaf.denyIP(ip1); - DenialOfServiceTaf.removeDenyIP(ip1); - - dost = new DenialOfServiceTaf(accessMock); - DenialOfServiceTaf.denyIP(ip1); - DenialOfServiceTaf.denyIP(ip2); - // coverage... - DenialOfServiceTaf.denyIP(ip2); - - String contents = readContentsFromFile(dosIPFile); - assertThat(contents.contains(ip1), is(true)); - assertThat(contents.contains(ip2), is(true)); - - // Removing all ips should delete the file - assertThat(dosIPFile.exists(), is(true)); - DenialOfServiceTaf.removeDenyIP(ip1); - DenialOfServiceTaf.removeDenyIP(ip2); - assertThat(dosIPFile.exists(), is(false)); - - dosIPFile.createNewFile(); - - DenialOfServiceTaf.denyIP(ip1); - DenialOfServiceTaf.denyIP(ip2); - - setPrivateField(DenialOfServiceTaf.class, "dosIP", null); - dost = new DenialOfServiceTaf(accessMock); - - contents = readContentsFromFile(dosIPFile); - assertThat(contents.contains(ip1), is(true)); - assertThat(contents.contains(ip2), is(true)); - - dosIPFile.delete(); - - // coverage... - setPrivateField(DenialOfServiceTaf.class, "deniedIP", null); - DenialOfServiceTaf.denyIP(ip1); - dosIPFile.delete(); - DenialOfServiceTaf.removeDenyIP(ip1); - - // coverage... - dosIPFile.delete(); - setPrivateField(DenialOfServiceTaf.class, "dosIP", null); - dost = new DenialOfServiceTaf(accessMock); - } - - @Test - public void idFileIOTest() throws CadiException, IOException { - @SuppressWarnings("unused") - DenialOfServiceTaf dost; - - dosIDFile.createNewFile(); - - // coverage... - DenialOfServiceTaf.denyID(id1); - DenialOfServiceTaf.removeDenyID(id1); - - dost = new DenialOfServiceTaf(accessMock); - DenialOfServiceTaf.denyID(id1); - DenialOfServiceTaf.denyID(id2); - // coverage... - DenialOfServiceTaf.denyID(id2); - - String contents = readContentsFromFile(dosIDFile); - assertThat(contents.contains(id1), is(true)); - assertThat(contents.contains(id2), is(true)); - - // Removing all ids should delete the file - assertThat(dosIDFile.exists(), is(true)); - DenialOfServiceTaf.removeDenyID(id1); - DenialOfServiceTaf.removeDenyID(id2); - assertThat(dosIDFile.exists(), is(false)); - - dosIDFile.createNewFile(); - - DenialOfServiceTaf.denyID(id1); - DenialOfServiceTaf.denyID(id2); - - setPrivateField(DenialOfServiceTaf.class, "dosID", null); - dost = new DenialOfServiceTaf(accessMock); - - contents = readContentsFromFile(dosIDFile); - assertThat(contents.contains(id1), is(true)); - assertThat(contents.contains(id2), is(true)); - - dosIDFile.delete(); - - // coverage... - setPrivateField(DenialOfServiceTaf.class, "deniedID", null); - DenialOfServiceTaf.denyID(id1); - dosIDFile.delete(); - DenialOfServiceTaf.removeDenyID(id1); - - // coverage... - dosIDFile.delete(); - setPrivateField(DenialOfServiceTaf.class, "dosID", null); - dost = new DenialOfServiceTaf(accessMock); - } - - private void setPrivateField(Class clazz, String fieldName, Object value) { - try { - Field field = clazz.getDeclaredField(fieldName); - field.setAccessible(true); - field.set(null, value); - field.setAccessible(false); - } catch(Exception e) { - System.err.println("Could not set field [" + fieldName + "] to " + value); - } - } - - private String readContentsFromFile(File file) throws IOException { - BufferedReader br = new BufferedReader(new FileReader(file)); - StringBuilder sb = new StringBuilder(); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - br.close(); - return sb.toString(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Chmod.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Chmod.java deleted file mode 100644 index 215980ff..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Chmod.java +++ /dev/null @@ -1,75 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.attribute.PosixFilePermission; -import java.nio.file.attribute.PosixFilePermissions; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.util.Chmod; - -public class JU_Chmod { - - private File file; - private String filePath; - - @Before - public void setup() throws IOException { - file = File.createTempFile("chmod_test", ""); - filePath = file.getAbsolutePath(); - } - - @After - public void tearDown() { - file.delete(); - } - - @Test - public void to755Test() throws IOException { - Chmod.to755.chmod(file); - Set set = Files.getPosixFilePermissions(Paths.get(filePath)); - assertThat(PosixFilePermissions.toString(set), is("rwxr-xr-x")); - } - - @Test - public void to644Test() throws IOException { - Chmod.to644.chmod(file); - Set set = Files.getPosixFilePermissions(Paths.get(filePath)); - assertThat(PosixFilePermissions.toString(set), is("rw-r--r--")); - } - - @Test - public void to400Test() throws IOException { - Chmod.to400.chmod(file); - Set set = Files.getPosixFilePermissions(Paths.get(filePath)); - assertThat(PosixFilePermissions.toString(set), is("r--------")); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_FQI.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_FQI.java deleted file mode 100644 index 57177839..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_FQI.java +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.util.FQI; - -public class JU_FQI { - - @Test - public void reverseDomainTest() { - assertThat(FQI.reverseDomain("user@att.com"), is("com.att")); - } - - @Test - public void coverageTest() { - @SuppressWarnings("unused") - FQI fqi = new FQI(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_JsonOutputStream.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_JsonOutputStream.java deleted file mode 100644 index d07652a1..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_JsonOutputStream.java +++ /dev/null @@ -1,93 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; - -import java.io.ByteArrayOutputStream; - -import org.junit.*; - -import java.io.IOException; -import java.lang.reflect.Field; - -import org.onap.aaf.cadi.util.JsonOutputStream; - -public class JU_JsonOutputStream { - - private JsonOutputStream jos; - - @Before - public void setup() { - jos = new JsonOutputStream(new ByteArrayOutputStream()); - } - - @Test - public void constructorTest() { - jos = new JsonOutputStream(System.out); - jos = new JsonOutputStream(System.err); - } - - @Test - public void writeTest() throws IOException { - byte[] json = ("{" + - "name: user," + - "password: pass," + - "contact: {" + - "email: user@att.com," + - "phone: 555-5555" + - "}," + - "list: [" + - "item1," + - "item2" + - "],[],{}," + - "list:" + - "[" + - "item1," + - "item2" + - "]" + - "}").getBytes(); - jos.write(json); - } - - @Test - public void resetIndentTest() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { - Field indentField = JsonOutputStream.class.getDeclaredField("indent"); - indentField.setAccessible(true); - - assertThat((int)indentField.get(jos), is(0)); - jos.resetIndent(); - assertThat((int)indentField.get(jos), is(1)); - } - - @Test - public void coverageTest() throws IOException { - jos.flush(); - jos.close(); - - jos = new JsonOutputStream(System.out); - jos.close(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_MaskFormatException.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_MaskFormatException.java deleted file mode 100644 index 6a60df68..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_MaskFormatException.java +++ /dev/null @@ -1,44 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ - -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.util.MaskFormatException; - -public class JU_MaskFormatException { - - @Test - public void throwsTest() { - String errorMessage = "This is a MaskFormatException"; - try { - throw new MaskFormatException(errorMessage); - } catch (Exception e) { - assertThat(e.getMessage(), is(errorMessage)); - assertTrue(e instanceof MaskFormatException); - } - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_NetMask.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_NetMask.java deleted file mode 100644 index be3b1b32..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_NetMask.java +++ /dev/null @@ -1,65 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.onap.aaf.cadi.util.NetMask; - -public class JU_NetMask { - - @Test - public void deriveTest() { - String test = "test"; - assertEquals(NetMask.derive(test.getBytes()), 0); - } - - @Test - public void deriveTest2() { - String test = "1.2.3.4"; - assertEquals(NetMask.derive(test.getBytes()), 0); - } - - @Test - public void deriveTest3() { - String test = "1.2.4"; - assertEquals(NetMask.derive(test.getBytes()), 0); - } - - @Test - public void deriveTest4() { - String test = "1.3.4"; - assertEquals(NetMask.derive(test.getBytes()), 0); - } - - @Test - public void deriveTest5() { - String test = "2.3.4"; - assertEquals(NetMask.derive(test.getBytes()), 0); - } - - @Test - public void deriveTest6() { - String test = "3.4"; - assertEquals(NetMask.derive(test.getBytes()), 0); - } -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Pool.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Pool.java deleted file mode 100644 index ef4bc687..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Pool.java +++ /dev/null @@ -1,159 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; - -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; -import org.onap.aaf.cadi.CadiException; -import org.onap.aaf.cadi.util.Pool; -import org.onap.aaf.cadi.util.Pool.*; - -public class JU_Pool { - - private StringBuilder sb = new StringBuilder(); - - private class IntegerCreator implements Creator { - private int current = 0; - - @Override - public Integer create() { - return current++; - } - - @Override - public void destroy(Integer t) { - t = 0; - } - - @Override - public boolean isValid(Integer t) { - return (t & 0x1) == 0; - } - - @Override - public void reuse(Integer t) { - } - } - - private class CustomLogger implements Log { - @Override - public void log(Object... o) { - for (Object item : o) { - sb.append(item.toString()); - } - } - } - - @Test - public void getTest() throws CadiException { - Pool intPool = new Pool(new IntegerCreator()); - - List> gotten = new ArrayList>(); - for (int i = 0; i < 10; i++) { - gotten.add(intPool.get()); - assertThat(gotten.get(i).content, is(i)); - } - - gotten.get(9).done(); - gotten.set(9, intPool.get()); - assertThat(gotten.get(9).content, is(9)); - - for (int i = 0; i < 10; i++) { - gotten.get(i).done(); - } - - for (int i = 0; i < 10; i++) { - gotten.set(i, intPool.get()); - if (i < 5) { - assertThat(gotten.get(i).content, is(i)); - } else { - assertThat(gotten.get(i).content, is(i + 5)); - } - } - - for (int i = 0; i < 10; i++) { - gotten.get(i).toss(); - // Coverage calls - gotten.get(i).toss(); - gotten.get(i).done(); - - // only set some objects to null -> this is for the finalize coverage test - if (i < 5) { - gotten.set(i, null); - } - } - - // Coverage of finalize() - System.gc(); - } - - @Test - public void bulkTest() throws CadiException { - Pool intPool = new Pool(new IntegerCreator()); - - intPool.prime(10); - // Remove all of the invalid items (in this case, odd numbers) - assertFalse(intPool.validate()); - - // Make sure we got them all - assertTrue(intPool.validate()); - - // Get an item from the pool - Pooled gotten = intPool.get(); - assertThat(gotten.content, is(0)); - - // finalize that item, then check the next one to make sure we actually purged - // the odd numbers - gotten = intPool.get(); - assertThat(gotten.content, is(2)); - - intPool.drain(); - - } - - @Test - public void setMaxTest() { - Pool intPool = new Pool(new IntegerCreator()); - intPool.setMaxRange(10); - assertThat(intPool.getMaxRange(), is(10)); - intPool.setMaxRange(-10); - assertThat(intPool.getMaxRange(), is(0)); - } - - @Test - public void loggingTest() { - Pool intPool = new Pool(new IntegerCreator()); - - // Log to Log.NULL for coverage - intPool.log("Test log output"); - - intPool.setLogger(new CustomLogger()); - intPool.log("Test log output"); - - assertThat(sb.toString(), is("Test log output")); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Split.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Split.java deleted file mode 100644 index e375f6c0..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Split.java +++ /dev/null @@ -1,113 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.util.Split; - -public class JU_Split { - - @Test - public void splitTest() { - String[] output = Split.split('c', "ctestctc", 0, "ctestctc".length()); - assertThat(output.length, is(4)); - assertThat(output[0], is("")); - assertThat(output[1], is("test")); - assertThat(output[2], is("t")); - assertThat(output[3], is("")); - - output = Split.split('c', "ctestctc", 0, 4); - assertThat(output.length, is(2)); - assertThat(output[0], is("")); - assertThat(output[1], is("tes")); - - output = Split.split('c', "test", 0, "test".length()); - assertThat(output.length, is(1)); - assertThat(output[0], is("test")); - - assertThat(Split.split('c', null, 0, 0).length, is(0)); - - // Test with fewer arguments - output = Split.split('c', "ctestctc"); - assertThat(output.length, is(4)); - assertThat(output[0], is("")); - assertThat(output[1], is("test")); - assertThat(output[2], is("t")); - assertThat(output[3], is("")); - } - - @Test - public void splitTrimTest() { - String[] output = Split.splitTrim('c', " cte stc ctc ", 0, " cte stc ctc ".length()); - assertThat(output.length, is(5)); - assertThat(output[0], is("")); - assertThat(output[1], is("te st")); - assertThat(output[2], is("")); - assertThat(output[3], is("t")); - assertThat(output[4], is("")); - - output = Split.splitTrim('c', " cte stc ctc ", 0, 5); - assertThat(output.length, is(2)); - assertThat(output[0], is("")); - assertThat(output[1], is("te")); - - assertThat(Split.splitTrim('c', " te st ", 0, " te st ".length())[0], is("te st")); - - assertThat(Split.splitTrim('c', null, 0, 0).length, is(0)); - - // Test with 2 arguments - output = Split.splitTrim('c', " cte stc ctc "); - assertThat(output.length, is(5)); - assertThat(output[0], is("")); - assertThat(output[1], is("te st")); - assertThat(output[2], is("")); - assertThat(output[3], is("t")); - assertThat(output[4], is("")); - - // Tests with 1 argument - output = Split.splitTrim('c', " cte stc ctc ", 1); - assertThat(output.length, is(1)); - assertThat(output[0], is("cte stc ctc")); - - output = Split.splitTrim('c', "testctest2", 2); - assertThat(output.length, is(2)); - assertThat(output[0], is("test")); - assertThat(output[1], is("test2")); - - output = Split.splitTrim('c', " cte stc ctc ", 4); - assertThat(output.length, is(4)); - assertThat(output[0], is("")); - assertThat(output[1], is("te st")); - assertThat(output[2], is("")); - - assertThat(Split.splitTrim('c', null, 0).length, is(0)); - } - - @Test - public void coverageTest() { - @SuppressWarnings("unused") - Split split = new Split(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_SubStandardConsole.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_SubStandardConsole.java deleted file mode 100644 index 9e6a175c..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_SubStandardConsole.java +++ /dev/null @@ -1,124 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import static org.mockito.Mockito.*; -import org.junit.*; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintStream; -import java.lang.reflect.Field; - -import org.onap.aaf.cadi.util.SubStandardConsole; - -public class JU_SubStandardConsole { - - private String inputString = "An input string"; - private ByteArrayOutputStream outStream; - private ByteArrayOutputStream errStream; - - @Before - public void setup() { - outStream = new ByteArrayOutputStream(); - errStream = new ByteArrayOutputStream(); - System.setOut(new PrintStream(outStream)); - System.setErr(new PrintStream(errStream)); - } - - @After - public void tearDown() { - System.setOut(System.out); - System.setErr(System.err); - } - - @Test - public void readLineTest() { - byte[] input = inputString.getBytes(); - System.setIn(new ByteArrayInputStream(input)); - SubStandardConsole ssc = new SubStandardConsole(); - String output = ssc.readLine("%s\n", ">>> "); - assertThat(output, is(inputString)); - assertThat(outStream.toString(), is(">>> \n")); - } - - @Test - public void readLineTest2() { - byte[] input = inputString.getBytes(); - System.setIn(new ByteArrayInputStream(input)); - SubStandardConsole ssc = new SubStandardConsole(); - String output = ssc.readLine("%s %s\n", ">>> ", "Another argument for coverage"); - assertThat(output, is(inputString)); - } - - @Test - public void readLineTest3() { - byte[] input = "\n".getBytes(); - System.setIn(new ByteArrayInputStream(input)); - SubStandardConsole ssc = new SubStandardConsole(); - String output = ssc.readLine("%s\n", ">>> "); - assertThat(output, is(">>> ")); - assertThat(outStream.toString(), is(">>> \n")); - } - - @Test - public void readPasswordTest() { - byte[] input = inputString.getBytes(); - System.setIn(new ByteArrayInputStream(input)); - SubStandardConsole ssc = new SubStandardConsole(); - char[] output = ssc.readPassword("%s\n", ">>> "); - System.out.println(output); - assertThat(output, is(inputString.toCharArray())); - assertThat(outStream.toString(), is(">>> \nAn input string\n")); - } - - @Test - public void printfTest() { - byte[] input = inputString.getBytes(); - System.setIn(new ByteArrayInputStream(input)); - SubStandardConsole ssc = new SubStandardConsole(); - ssc.printf("%s", "A format specifier"); - assertThat(outStream.toString(), is("A format specifier")); - } - - @Test - public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - BufferedReader brMock = mock(BufferedReader.class); - when(brMock.readLine()).thenThrow(new IOException()); - - SubStandardConsole ssc = new SubStandardConsole(); - - Field brField = SubStandardConsole.class.getDeclaredField("br"); - brField.setAccessible(true); - brField.set(ssc, brMock); - - assertThat(ssc.readLine(""), is("")); - assertThat(errStream.toString(), is("uh oh...\n")); - errStream.reset(); - assertThat(ssc.readPassword("").length, is(0)); - assertThat(errStream.toString(), is("uh oh...\n")); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_TheConsole.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_TheConsole.java deleted file mode 100644 index f168c7f4..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_TheConsole.java +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.assertEquals; - -import org.junit.Test; -import org.onap.aaf.cadi.util.TheConsole; - -public class JU_TheConsole { - - @Test - public void implemented(){ - assertEquals(TheConsole.implemented(),false); - } -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java deleted file mode 100644 index 949d034b..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_UserChainManip.java +++ /dev/null @@ -1,67 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.*; -import static org.hamcrest.CoreMatchers.*; -import org.junit.*; - -import org.onap.aaf.cadi.UserChain; -import org.onap.aaf.cadi.util.UserChainManip; - -public class JU_UserChainManip { - - @Test - public void build(){ - UserChain.Protocol baseAuth=UserChain.Protocol.BasicAuth; - StringBuilder sb = UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, true); - assertThat(sb.toString(), is("app:id:BasicAuth:AS")); - - // for coverage - sb = UserChainManip.build(sb, "app", "id", baseAuth, true); - assertThat(sb.toString(), is("app:id:BasicAuth:AS,app:id:BasicAuth")); - - sb = UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, false); - assertThat(sb.toString(), is("app:id:BasicAuth")); - } - - @Test - public void idToNSTEST() { - assertThat(UserChainManip.idToNS(null), is("")); - assertThat(UserChainManip.idToNS(""), is("")); - assertThat(UserChainManip.idToNS("something"), is("")); - assertThat(UserChainManip.idToNS("something@@"), is("")); - assertThat(UserChainManip.idToNS("something@@."), is("")); - assertThat(UserChainManip.idToNS("something@com"), is("com")); - assertThat(UserChainManip.idToNS("something@random.com"), is("com.random")); - assertThat(UserChainManip.idToNS("@random.com"), is("com.random")); - assertThat(UserChainManip.idToNS("something@random.com."), is("com.random")); - assertThat(UserChainManip.idToNS("something@..random...com..."), is("com.random")); - assertThat(UserChainManip.idToNS("something@this.random.com"), is("com.random.this")); - } - - @Test - public void coverageTest() { - @SuppressWarnings("unused") - UserChainManip ucm = new UserChainManip(); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Vars.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Vars.java deleted file mode 100644 index 345c8ca2..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/util/JU_Vars.java +++ /dev/null @@ -1,150 +0,0 @@ -/******************************************************************************* - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.util; - -import static org.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.Test; -import org.onap.aaf.cadi.util.Vars; - -public class JU_Vars { - - @Test - public void coverage() { - @SuppressWarnings("unused") - Vars my_nonstatic_object_for_coverage = new Vars(); - } - - @Test - public void convert() { - String test = "test"; - List list = new ArrayList(); - list.add("method"); - assertEquals(Vars.convert(test, list), test); - } - - @Test - public void convertTest1() { - String test = "te%t"; - List list = new ArrayList(); - list.add("method"); - assertEquals(Vars.convert("test", list), "test"); - } - - @Test - public void convertTest2() { - String test = "te%s%t"; - List list = new ArrayList(); - list.add("method"); - assertEquals(Vars.convert("test", list), "test"); - } - - @Test - public void test() { - StringBuilder holder = new StringBuilder(); - String str,bstr; - assertEquals(str = "set %1 to %2",Vars.convert(holder,str, "a","b")); - assertEquals("set a to b",holder.toString()); - assertEquals(str,Vars.convert(null,str, "a","b")); - holder.setLength(0); - assertEquals(str,Vars.convert(holder,bstr="set %s to %s", "a","b")); - assertEquals("set a to b",holder.toString()); - assertEquals(str,Vars.convert(null,bstr, "a","b")); - - holder.setLength(0); - assertEquals(str = "%1=%2",Vars.convert(holder,str, "a","b")); - assertEquals("a=b",holder.toString()); - assertEquals(str,Vars.convert(null,str, "a","b")); - holder.setLength(0); - assertEquals(str,Vars.convert(holder,bstr="%s=%s", "a","b")); - assertEquals("a=b",holder.toString()); - assertEquals(str,Vars.convert(null,bstr, "a","b")); - - holder.setLength(0); - assertEquals(str = "%1%2",Vars.convert(holder,str, "a","b")); - assertEquals("ab",holder.toString()); - assertEquals(str ,Vars.convert(null,str, "a","b")); - holder.setLength(0); - assertEquals(str,Vars.convert(holder,bstr="%s%s", "a","b")); - assertEquals("ab",holder.toString()); - assertEquals(str ,Vars.convert(null,bstr, "a","b")); - - - holder.setLength(0); - assertEquals(str = " %1=%2 ",Vars.convert(holder,str, "a","b")); - assertEquals(" a=b ",holder.toString()); - assertEquals(str ,Vars.convert(null,str, "a","b")); - holder.setLength(0); - assertEquals(str,Vars.convert(holder,bstr = " %s=%s ", "a","b")); - assertEquals(" a=b ",holder.toString()); - assertEquals(str ,Vars.convert(null,bstr, "a","b")); - - holder.setLength(0); - assertEquals(str = " %1%2%10 ",Vars.convert(holder,str, "a","b","c","d","e","f","g","h","i","j")); - assertEquals(" abj ",holder.toString()); - assertEquals(str,Vars.convert(null,str, "a","b","c","d","e","f","g","h","i","j")); - holder.setLength(0); - assertEquals(str=" %1%2%3 ",Vars.convert(holder,bstr = " %s%s%s ", "a","b","c","d","e","f","g","h","i","j")); - assertEquals(" abc ",holder.toString()); - assertEquals(str,Vars.convert(null,bstr, "a","b","c","d","e","f","g","h","i","j")); - - - holder.setLength(0); - assertEquals(str = "set %1 to %2",Vars.convert(holder,str, "Something much","larger")); - assertEquals("set Something much to larger",holder.toString()); - assertEquals(str,Vars.convert(null,str,"Something much","larger")); - holder.setLength(0); - assertEquals(str,Vars.convert(holder,bstr="set %s to %s", "Something much","larger")); - assertEquals("set Something much to larger",holder.toString()); - assertEquals(str,Vars.convert(null,bstr, "Something much","larger")); - - holder.setLength(0); - assertEquals(str = "Text without Vars",Vars.convert(holder,str)); - assertEquals(str,holder.toString()); - assertEquals(str = "Text without Vars",Vars.convert(null,str)); - - - holder.setLength(0); - assertEquals(str = "Not %1 Enough %2 Vars %3",Vars.convert(holder,str, "a","b")); - assertEquals("Not a Enough b Vars ",holder.toString()); - assertEquals(str ,Vars.convert(null,str, "a","b")); - holder.setLength(0); - assertEquals(str,Vars.convert(holder,bstr="Not %s Enough %s Vars %s", "a","b")); - assertEquals("Not a Enough b Vars ",holder.toString()); - assertEquals(str ,Vars.convert(null,bstr, "a","b")); - - holder.setLength(0); - assertEquals(str = "!@#$%^*()-+?/,:;.",Vars.convert(holder,str, "a","b")); - assertEquals(str,holder.toString()); - assertEquals(str ,Vars.convert(null,str, "a","b")); - - holder.setLength(0); - bstr = "%s !@#$%^*()-+?/,:;."; - str = "%1 !@#$%^*()-+?/,:;."; - assertEquals(str,Vars.convert(holder,bstr, "Not Acceptable")); - assertEquals("Not Acceptable !@#$%^*()-+?/,:;.",holder.toString()); - assertEquals(str ,Vars.convert(null,bstr, "Not Acceptable")); - } - -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/test/wsse/JU_WSSE_Read.java b/cadi/core/src/test/java/org/onap/aaf/cadi/test/wsse/JU_WSSE_Read.java deleted file mode 100644 index 26dd43b1..00000000 --- a/cadi/core/src/test/java/org/onap/aaf/cadi/test/wsse/JU_WSSE_Read.java +++ /dev/null @@ -1,189 +0,0 @@ -/******************************************************************************* - * ============LICENSE_START==================================================== - * * org.onap.aaf - * * =========================================================================== - * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. - * * =========================================================================== - * * Licensed 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. - * * ============LICENSE_END==================================================== - * * - * * - ******************************************************************************/ -package org.onap.aaf.cadi.test.wsse; - -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; -import static junit.framework.Assert.assertNull; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; - -import javax.xml.stream.XMLStreamException; - -import org.junit.Test; -import org.onap.aaf.cadi.BasicCred; -import org.onap.aaf.cadi.BufferedServletInputStream; -import org.onap.aaf.cadi.wsse.WSSEParser; - -public class JU_WSSE_Read { - -// @Test -// public void test() { -// try { -// final BasicCred bc = new BasicCred() { - -// private String user; -// private byte[] password; - -// public void setUser(String user) { -// this.user = user; -// } - -// public void setCred(byte[] passwd) { -// this.password = passwd; -// } - -// public String getUser() { -// return user; -// } - -// public byte[] getCred() { -// return password; -// } -// }; - -// WSSEParser wp = new WSSEParser(); - -// FileInputStream fis; -// fis = new FileInputStream("test/example.xml"); -// BufferedServletInputStream is = new BufferedServletInputStream(fis); -// try { -// is.mark(1536); -// try { -// assertNull(wp.parse(bc, is)); -// } finally { -// is.reset(); -// assertEquals(814,is.buffered()); -// } -// String password = new String(bc.getCred()); -// System.out.println("CadiWrap credentials are: " + bc.getUser() + ", " + password); -// assertEquals("some_user", bc.getUser()); -// assertEquals("some_password", password); - -// } finally { -// fis.close(); -// } - -// // CBUS (larger) -// fis = new FileInputStream("test/CBUSevent.xml"); -// is = new BufferedServletInputStream(fis); -// try { -// is.mark(1536); -// try { -// assertNull(wp.parse(bc, is)); -// } finally { -// is.reset(); -// assertEquals(667,is.buffered()); -// } -// String password = new String(bc.getCred()); -// System.out.println("CadiWrap credentials are: " + bc.getUser() + ", " + password); -// assertEquals("none", bc.getUser()); -// assertEquals("none", password); - -// } finally { -// fis.close(); -// } - -// // Closed Stream -// fis = new FileInputStream("test/example.xml"); -// fis.close(); -// bc.setCred(null); -// bc.setUser(null); -// XMLStreamException ex = wp.parse(bc, fis); -// assertNotNull(ex); -// assertNull(bc.getUser()); -// assertNull(bc.getCred()); - - -// fis = new FileInputStream("test/exampleNoSecurity.xml"); -// try { -// bc.setCred(null); -// bc.setUser(null); -// assertNull(wp.parse(bc, fis)); -// assertNull(bc.getUser()); -// assertNull(bc.getCred()); -// } finally { -// fis.close(); -// } - -// fis = new FileInputStream("test/exampleBad1.xml"); -// try { -// bc.setCred(null); -// bc.setUser(null); -// assertNull(wp.parse(bc, fis)); -// assertNull(bc.getUser()); -// assertNull(bc.getCred()); -// } finally { -// fis.close(); -// } - -// XMLStreamException e = wp.parse(bc, new ByteArrayInputStream("Not XML".getBytes())); // empty -// assertNotNull(e); - -// e = wp.parse(bc, new ByteArrayInputStream("".getBytes())); // empty -// assertNotNull(e); - - -// long start, count = 0L; -// int iter = 30000; -// File f = new File("test/CBUSevent.xml"); -// fis = new FileInputStream(f); -// is = new BufferedServletInputStream(fis); -// is.mark(0); -// try { -// while(is.read()>=0); -// } finally { -// fis.close(); -// } - -// for(int i=0;i"); - writer.println(""); - writer.println(""); - writer.println("<" + OUTER_TAG + ">"); - writer.println(" <" + INNER_TAG + ">"); - writer.println(" <" + DATA_TAG + ">" + DATA + ""); - writer.println(" <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />"); - writer.println(" <" + PREFIX + ":" + SUFFIX + "/>"); - writer.println(" "); - writer.println(""); - writer.flush(); - writer.close(); - return xmlFile; - } -} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Chmod.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Chmod.java new file mode 100644 index 00000000..62114189 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Chmod.java @@ -0,0 +1,75 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.attribute.PosixFilePermission; +import java.nio.file.attribute.PosixFilePermissions; +import java.util.Set; + +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.util.Chmod; + +public class JU_Chmod { + + private File file; + private String filePath; + + @Before + public void setup() throws IOException { + file = File.createTempFile("chmod_test", ""); + filePath = file.getAbsolutePath(); + } + + @After + public void tearDown() { + file.delete(); + } + + @Test + public void to755Test() throws IOException { + Chmod.to755.chmod(file); + Set set = Files.getPosixFilePermissions(Paths.get(filePath)); + assertThat(PosixFilePermissions.toString(set), is("rwxr-xr-x")); + } + + @Test + public void to644Test() throws IOException { + Chmod.to644.chmod(file); + Set set = Files.getPosixFilePermissions(Paths.get(filePath)); + assertThat(PosixFilePermissions.toString(set), is("rw-r--r--")); + } + + @Test + public void to400Test() throws IOException { + Chmod.to400.chmod(file); + Set set = Files.getPosixFilePermissions(Paths.get(filePath)); + assertThat(PosixFilePermissions.toString(set), is("r--------")); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_FQI.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_FQI.java new file mode 100644 index 00000000..bcd2f776 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_FQI.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.util.FQI; + +public class JU_FQI { + + @Test + public void reverseDomainTest() { + assertThat(FQI.reverseDomain("user@att.com"), is("com.att")); + } + + @Test + public void coverageTest() { + @SuppressWarnings("unused") + FQI fqi = new FQI(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_JsonOutputStream.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_JsonOutputStream.java new file mode 100644 index 00000000..da3557cb --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_JsonOutputStream.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; + +import java.io.ByteArrayOutputStream; + +import org.junit.*; + +import java.io.IOException; +import java.lang.reflect.Field; + +import org.onap.aaf.cadi.util.JsonOutputStream; + +public class JU_JsonOutputStream { + + private JsonOutputStream jos; + + @Before + public void setup() { + jos = new JsonOutputStream(new ByteArrayOutputStream()); + } + + @Test + public void constructorTest() { + jos = new JsonOutputStream(System.out); + jos = new JsonOutputStream(System.err); + } + + @Test + public void writeTest() throws IOException { + byte[] json = ("{" + + "name: user," + + "password: pass," + + "contact: {" + + "email: user@att.com," + + "phone: 555-5555" + + "}," + + "list: [" + + "item1," + + "item2" + + "],[],{}," + + "list:" + + "[" + + "item1," + + "item2" + + "]" + + "}").getBytes(); + jos.write(json); + } + + @Test + public void resetIndentTest() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException { + Field indentField = JsonOutputStream.class.getDeclaredField("indent"); + indentField.setAccessible(true); + + assertThat((int)indentField.get(jos), is(0)); + jos.resetIndent(); + assertThat((int)indentField.get(jos), is(1)); + } + + @Test + public void coverageTest() throws IOException { + jos.flush(); + jos.close(); + + jos = new JsonOutputStream(System.out); + jos.close(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_MaskFormatException.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_MaskFormatException.java new file mode 100644 index 00000000..ea743430 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_MaskFormatException.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ + +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.util.MaskFormatException; + +public class JU_MaskFormatException { + + @Test + public void throwsTest() { + String errorMessage = "This is a MaskFormatException"; + try { + throw new MaskFormatException(errorMessage); + } catch (Exception e) { + assertThat(e.getMessage(), is(errorMessage)); + assertTrue(e instanceof MaskFormatException); + } + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_NetMask.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_NetMask.java new file mode 100644 index 00000000..403f1f1e --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_NetMask.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.aaf.cadi.util.NetMask; + +public class JU_NetMask { + + @Test + public void deriveTest() { + String test = "test"; + assertEquals(NetMask.derive(test.getBytes()), 0); + } + + @Test + public void deriveTest2() { + String test = "1.2.3.4"; + assertEquals(NetMask.derive(test.getBytes()), 0); + } + + @Test + public void deriveTest3() { + String test = "1.2.4"; + assertEquals(NetMask.derive(test.getBytes()), 0); + } + + @Test + public void deriveTest4() { + String test = "1.3.4"; + assertEquals(NetMask.derive(test.getBytes()), 0); + } + + @Test + public void deriveTest5() { + String test = "2.3.4"; + assertEquals(NetMask.derive(test.getBytes()), 0); + } + + @Test + public void deriveTest6() { + String test = "3.4"; + assertEquals(NetMask.derive(test.getBytes()), 0); + } +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java new file mode 100644 index 00000000..79209322 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Pool.java @@ -0,0 +1,159 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; +import org.onap.aaf.cadi.CadiException; +import org.onap.aaf.cadi.util.Pool; +import org.onap.aaf.cadi.util.Pool.*; + +public class JU_Pool { + + private StringBuilder sb = new StringBuilder(); + + private class IntegerCreator implements Creator { + private int current = 0; + + @Override + public Integer create() { + return current++; + } + + @Override + public void destroy(Integer t) { + t = 0; + } + + @Override + public boolean isValid(Integer t) { + return (t & 0x1) == 0; + } + + @Override + public void reuse(Integer t) { + } + } + + private class CustomLogger implements Log { + @Override + public void log(Object... o) { + for (Object item : o) { + sb.append(item.toString()); + } + } + } + + @Test + public void getTest() throws CadiException { + Pool intPool = new Pool(new IntegerCreator()); + + List> gotten = new ArrayList>(); + for (int i = 0; i < 10; i++) { + gotten.add(intPool.get()); + assertThat(gotten.get(i).content, is(i)); + } + + gotten.get(9).done(); + gotten.set(9, intPool.get()); + assertThat(gotten.get(9).content, is(9)); + + for (int i = 0; i < 10; i++) { + gotten.get(i).done(); + } + + for (int i = 0; i < 10; i++) { + gotten.set(i, intPool.get()); + if (i < 5) { + assertThat(gotten.get(i).content, is(i)); + } else { + assertThat(gotten.get(i).content, is(i + 5)); + } + } + + for (int i = 0; i < 10; i++) { + gotten.get(i).toss(); + // Coverage calls + gotten.get(i).toss(); + gotten.get(i).done(); + + // only set some objects to null -> this is for the finalize coverage test + if (i < 5) { + gotten.set(i, null); + } + } + + // Coverage of finalize() + System.gc(); + } + + @Test + public void bulkTest() throws CadiException { + Pool intPool = new Pool(new IntegerCreator()); + + intPool.prime(10); + // Remove all of the invalid items (in this case, odd numbers) + assertFalse(intPool.validate()); + + // Make sure we got them all + assertTrue(intPool.validate()); + + // Get an item from the pool + Pooled gotten = intPool.get(); + assertThat(gotten.content, is(0)); + + // finalize that item, then check the next one to make sure we actually purged + // the odd numbers + gotten = intPool.get(); + assertThat(gotten.content, is(2)); + + intPool.drain(); + + } + + @Test + public void setMaxTest() { + Pool intPool = new Pool(new IntegerCreator()); + intPool.setMaxRange(10); + assertThat(intPool.getMaxRange(), is(10)); + intPool.setMaxRange(-10); + assertThat(intPool.getMaxRange(), is(0)); + } + + @Test + public void loggingTest() { + Pool intPool = new Pool(new IntegerCreator()); + + // Log to Log.NULL for coverage + intPool.log("Test log output"); + + intPool.setLogger(new CustomLogger()); + intPool.log("Test log output"); + + assertThat(sb.toString(), is("Test log output")); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Split.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Split.java new file mode 100644 index 00000000..12be4e15 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Split.java @@ -0,0 +1,113 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.util.Split; + +public class JU_Split { + + @Test + public void splitTest() { + String[] output = Split.split('c', "ctestctc", 0, "ctestctc".length()); + assertThat(output.length, is(4)); + assertThat(output[0], is("")); + assertThat(output[1], is("test")); + assertThat(output[2], is("t")); + assertThat(output[3], is("")); + + output = Split.split('c', "ctestctc", 0, 4); + assertThat(output.length, is(2)); + assertThat(output[0], is("")); + assertThat(output[1], is("tes")); + + output = Split.split('c', "test", 0, "test".length()); + assertThat(output.length, is(1)); + assertThat(output[0], is("test")); + + assertThat(Split.split('c', null, 0, 0).length, is(0)); + + // Test with fewer arguments + output = Split.split('c', "ctestctc"); + assertThat(output.length, is(4)); + assertThat(output[0], is("")); + assertThat(output[1], is("test")); + assertThat(output[2], is("t")); + assertThat(output[3], is("")); + } + + @Test + public void splitTrimTest() { + String[] output = Split.splitTrim('c', " cte stc ctc ", 0, " cte stc ctc ".length()); + assertThat(output.length, is(5)); + assertThat(output[0], is("")); + assertThat(output[1], is("te st")); + assertThat(output[2], is("")); + assertThat(output[3], is("t")); + assertThat(output[4], is("")); + + output = Split.splitTrim('c', " cte stc ctc ", 0, 5); + assertThat(output.length, is(2)); + assertThat(output[0], is("")); + assertThat(output[1], is("te")); + + assertThat(Split.splitTrim('c', " te st ", 0, " te st ".length())[0], is("te st")); + + assertThat(Split.splitTrim('c', null, 0, 0).length, is(0)); + + // Test with 2 arguments + output = Split.splitTrim('c', " cte stc ctc "); + assertThat(output.length, is(5)); + assertThat(output[0], is("")); + assertThat(output[1], is("te st")); + assertThat(output[2], is("")); + assertThat(output[3], is("t")); + assertThat(output[4], is("")); + + // Tests with 1 argument + output = Split.splitTrim('c', " cte stc ctc ", 1); + assertThat(output.length, is(1)); + assertThat(output[0], is("cte stc ctc")); + + output = Split.splitTrim('c', "testctest2", 2); + assertThat(output.length, is(2)); + assertThat(output[0], is("test")); + assertThat(output[1], is("test2")); + + output = Split.splitTrim('c', " cte stc ctc ", 4); + assertThat(output.length, is(4)); + assertThat(output[0], is("")); + assertThat(output[1], is("te st")); + assertThat(output[2], is("")); + + assertThat(Split.splitTrim('c', null, 0).length, is(0)); + } + + @Test + public void coverageTest() { + @SuppressWarnings("unused") + Split split = new Split(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_SubStandardConsole.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_SubStandardConsole.java new file mode 100644 index 00000000..4d8e8f84 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_SubStandardConsole.java @@ -0,0 +1,124 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import static org.mockito.Mockito.*; +import org.junit.*; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.lang.reflect.Field; + +import org.onap.aaf.cadi.util.SubStandardConsole; + +public class JU_SubStandardConsole { + + private String inputString = "An input string"; + private ByteArrayOutputStream outStream; + private ByteArrayOutputStream errStream; + + @Before + public void setup() { + outStream = new ByteArrayOutputStream(); + errStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outStream)); + System.setErr(new PrintStream(errStream)); + } + + @After + public void tearDown() { + System.setOut(System.out); + System.setErr(System.err); + } + + @Test + public void readLineTest() { + byte[] input = inputString.getBytes(); + System.setIn(new ByteArrayInputStream(input)); + SubStandardConsole ssc = new SubStandardConsole(); + String output = ssc.readLine("%s\n", ">>> "); + assertThat(output, is(inputString)); + assertThat(outStream.toString(), is(">>> \n")); + } + + @Test + public void readLineTest2() { + byte[] input = inputString.getBytes(); + System.setIn(new ByteArrayInputStream(input)); + SubStandardConsole ssc = new SubStandardConsole(); + String output = ssc.readLine("%s %s\n", ">>> ", "Another argument for coverage"); + assertThat(output, is(inputString)); + } + + @Test + public void readLineTest3() { + byte[] input = "\n".getBytes(); + System.setIn(new ByteArrayInputStream(input)); + SubStandardConsole ssc = new SubStandardConsole(); + String output = ssc.readLine("%s\n", ">>> "); + assertThat(output, is(">>> ")); + assertThat(outStream.toString(), is(">>> \n")); + } + + @Test + public void readPasswordTest() { + byte[] input = inputString.getBytes(); + System.setIn(new ByteArrayInputStream(input)); + SubStandardConsole ssc = new SubStandardConsole(); + char[] output = ssc.readPassword("%s\n", ">>> "); + System.out.println(output); + assertThat(output, is(inputString.toCharArray())); + assertThat(outStream.toString(), is(">>> \nAn input string\n")); + } + + @Test + public void printfTest() { + byte[] input = inputString.getBytes(); + System.setIn(new ByteArrayInputStream(input)); + SubStandardConsole ssc = new SubStandardConsole(); + ssc.printf("%s", "A format specifier"); + assertThat(outStream.toString(), is("A format specifier")); + } + + @Test + public void throwsTest() throws IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { + BufferedReader brMock = mock(BufferedReader.class); + when(brMock.readLine()).thenThrow(new IOException()); + + SubStandardConsole ssc = new SubStandardConsole(); + + Field brField = SubStandardConsole.class.getDeclaredField("br"); + brField.setAccessible(true); + brField.set(ssc, brMock); + + assertThat(ssc.readLine(""), is("")); + assertThat(errStream.toString(), is("uh oh...\n")); + errStream.reset(); + assertThat(ssc.readPassword("").length, is(0)); + assertThat(errStream.toString(), is("uh oh...\n")); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_TheConsole.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_TheConsole.java new file mode 100644 index 00000000..40f88a3a --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_TheConsole.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.onap.aaf.cadi.util.TheConsole; + +public class JU_TheConsole { + + @Test + public void implemented(){ + assertEquals(TheConsole.implemented(),false); + } +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_UserChainManip.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_UserChainManip.java new file mode 100644 index 00000000..21f8c21b --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_UserChainManip.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; +import org.junit.*; + +import org.onap.aaf.cadi.UserChain; +import org.onap.aaf.cadi.util.UserChainManip; + +public class JU_UserChainManip { + + @Test + public void build(){ + UserChain.Protocol baseAuth=UserChain.Protocol.BasicAuth; + StringBuilder sb = UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, true); + assertThat(sb.toString(), is("app:id:BasicAuth:AS")); + + // for coverage + sb = UserChainManip.build(sb, "app", "id", baseAuth, true); + assertThat(sb.toString(), is("app:id:BasicAuth:AS,app:id:BasicAuth")); + + sb = UserChainManip.build(new StringBuilder(""), "app", "id", baseAuth, false); + assertThat(sb.toString(), is("app:id:BasicAuth")); + } + + @Test + public void idToNSTEST() { + assertThat(UserChainManip.idToNS(null), is("")); + assertThat(UserChainManip.idToNS(""), is("")); + assertThat(UserChainManip.idToNS("something"), is("")); + assertThat(UserChainManip.idToNS("something@@"), is("")); + assertThat(UserChainManip.idToNS("something@@."), is("")); + assertThat(UserChainManip.idToNS("something@com"), is("com")); + assertThat(UserChainManip.idToNS("something@random.com"), is("com.random")); + assertThat(UserChainManip.idToNS("@random.com"), is("com.random")); + assertThat(UserChainManip.idToNS("something@random.com."), is("com.random")); + assertThat(UserChainManip.idToNS("something@..random...com..."), is("com.random")); + assertThat(UserChainManip.idToNS("something@this.random.com"), is("com.random.this")); + } + + @Test + public void coverageTest() { + @SuppressWarnings("unused") + UserChainManip ucm = new UserChainManip(); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Vars.java b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Vars.java new file mode 100644 index 00000000..32ca8f84 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/util/test/JU_Vars.java @@ -0,0 +1,150 @@ +/******************************************************************************* + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.util.test; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.aaf.cadi.util.Vars; + +public class JU_Vars { + + @Test + public void coverage() { + @SuppressWarnings("unused") + Vars my_nonstatic_object_for_coverage = new Vars(); + } + + @Test + public void convert() { + String test = "test"; + List list = new ArrayList(); + list.add("method"); + assertEquals(Vars.convert(test, list), test); + } + + @Test + public void convertTest1() { + String test = "te%t"; + List list = new ArrayList(); + list.add("method"); + assertEquals(Vars.convert("test", list), "test"); + } + + @Test + public void convertTest2() { + String test = "te%s%t"; + List list = new ArrayList(); + list.add("method"); + assertEquals(Vars.convert("test", list), "test"); + } + + @Test + public void test() { + StringBuilder holder = new StringBuilder(); + String str,bstr; + assertEquals(str = "set %1 to %2",Vars.convert(holder,str, "a","b")); + assertEquals("set a to b",holder.toString()); + assertEquals(str,Vars.convert(null,str, "a","b")); + holder.setLength(0); + assertEquals(str,Vars.convert(holder,bstr="set %s to %s", "a","b")); + assertEquals("set a to b",holder.toString()); + assertEquals(str,Vars.convert(null,bstr, "a","b")); + + holder.setLength(0); + assertEquals(str = "%1=%2",Vars.convert(holder,str, "a","b")); + assertEquals("a=b",holder.toString()); + assertEquals(str,Vars.convert(null,str, "a","b")); + holder.setLength(0); + assertEquals(str,Vars.convert(holder,bstr="%s=%s", "a","b")); + assertEquals("a=b",holder.toString()); + assertEquals(str,Vars.convert(null,bstr, "a","b")); + + holder.setLength(0); + assertEquals(str = "%1%2",Vars.convert(holder,str, "a","b")); + assertEquals("ab",holder.toString()); + assertEquals(str ,Vars.convert(null,str, "a","b")); + holder.setLength(0); + assertEquals(str,Vars.convert(holder,bstr="%s%s", "a","b")); + assertEquals("ab",holder.toString()); + assertEquals(str ,Vars.convert(null,bstr, "a","b")); + + + holder.setLength(0); + assertEquals(str = " %1=%2 ",Vars.convert(holder,str, "a","b")); + assertEquals(" a=b ",holder.toString()); + assertEquals(str ,Vars.convert(null,str, "a","b")); + holder.setLength(0); + assertEquals(str,Vars.convert(holder,bstr = " %s=%s ", "a","b")); + assertEquals(" a=b ",holder.toString()); + assertEquals(str ,Vars.convert(null,bstr, "a","b")); + + holder.setLength(0); + assertEquals(str = " %1%2%10 ",Vars.convert(holder,str, "a","b","c","d","e","f","g","h","i","j")); + assertEquals(" abj ",holder.toString()); + assertEquals(str,Vars.convert(null,str, "a","b","c","d","e","f","g","h","i","j")); + holder.setLength(0); + assertEquals(str=" %1%2%3 ",Vars.convert(holder,bstr = " %s%s%s ", "a","b","c","d","e","f","g","h","i","j")); + assertEquals(" abc ",holder.toString()); + assertEquals(str,Vars.convert(null,bstr, "a","b","c","d","e","f","g","h","i","j")); + + + holder.setLength(0); + assertEquals(str = "set %1 to %2",Vars.convert(holder,str, "Something much","larger")); + assertEquals("set Something much to larger",holder.toString()); + assertEquals(str,Vars.convert(null,str,"Something much","larger")); + holder.setLength(0); + assertEquals(str,Vars.convert(holder,bstr="set %s to %s", "Something much","larger")); + assertEquals("set Something much to larger",holder.toString()); + assertEquals(str,Vars.convert(null,bstr, "Something much","larger")); + + holder.setLength(0); + assertEquals(str = "Text without Vars",Vars.convert(holder,str)); + assertEquals(str,holder.toString()); + assertEquals(str = "Text without Vars",Vars.convert(null,str)); + + + holder.setLength(0); + assertEquals(str = "Not %1 Enough %2 Vars %3",Vars.convert(holder,str, "a","b")); + assertEquals("Not a Enough b Vars ",holder.toString()); + assertEquals(str ,Vars.convert(null,str, "a","b")); + holder.setLength(0); + assertEquals(str,Vars.convert(holder,bstr="Not %s Enough %s Vars %s", "a","b")); + assertEquals("Not a Enough b Vars ",holder.toString()); + assertEquals(str ,Vars.convert(null,bstr, "a","b")); + + holder.setLength(0); + assertEquals(str = "!@#$%^*()-+?/,:;.",Vars.convert(holder,str, "a","b")); + assertEquals(str,holder.toString()); + assertEquals(str ,Vars.convert(null,str, "a","b")); + + holder.setLength(0); + bstr = "%s !@#$%^*()-+?/,:;."; + str = "%1 !@#$%^*()-+?/,:;."; + assertEquals(str,Vars.convert(holder,bstr, "Not Acceptable")); + assertEquals("Not Acceptable !@#$%^*()-+?/,:;.",holder.toString()); + assertEquals(str ,Vars.convert(null,bstr, "Not Acceptable")); + } + +} diff --git a/cadi/core/src/test/java/org/onap/aaf/cadi/wsse/test/JU_WSSE_Read.java b/cadi/core/src/test/java/org/onap/aaf/cadi/wsse/test/JU_WSSE_Read.java new file mode 100644 index 00000000..599987d7 --- /dev/null +++ b/cadi/core/src/test/java/org/onap/aaf/cadi/wsse/test/JU_WSSE_Read.java @@ -0,0 +1,189 @@ +/******************************************************************************* + * ============LICENSE_START==================================================== + * * org.onap.aaf + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * Licensed 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. + * * ============LICENSE_END==================================================== + * * + * * + ******************************************************************************/ +package org.onap.aaf.cadi.wsse.test; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertNull; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; + +import javax.xml.stream.XMLStreamException; + +import org.junit.Test; +import org.onap.aaf.cadi.BasicCred; +import org.onap.aaf.cadi.BufferedServletInputStream; +import org.onap.aaf.cadi.wsse.WSSEParser; + +public class JU_WSSE_Read { + +// @Test +// public void test() { +// try { +// final BasicCred bc = new BasicCred() { + +// private String user; +// private byte[] password; + +// public void setUser(String user) { +// this.user = user; +// } + +// public void setCred(byte[] passwd) { +// this.password = passwd; +// } + +// public String getUser() { +// return user; +// } + +// public byte[] getCred() { +// return password; +// } +// }; + +// WSSEParser wp = new WSSEParser(); + +// FileInputStream fis; +// fis = new FileInputStream("test/example.xml"); +// BufferedServletInputStream is = new BufferedServletInputStream(fis); +// try { +// is.mark(1536); +// try { +// assertNull(wp.parse(bc, is)); +// } finally { +// is.reset(); +// assertEquals(814,is.buffered()); +// } +// String password = new String(bc.getCred()); +// System.out.println("CadiWrap credentials are: " + bc.getUser() + ", " + password); +// assertEquals("some_user", bc.getUser()); +// assertEquals("some_password", password); + +// } finally { +// fis.close(); +// } + +// // CBUS (larger) +// fis = new FileInputStream("test/CBUSevent.xml"); +// is = new BufferedServletInputStream(fis); +// try { +// is.mark(1536); +// try { +// assertNull(wp.parse(bc, is)); +// } finally { +// is.reset(); +// assertEquals(667,is.buffered()); +// } +// String password = new String(bc.getCred()); +// System.out.println("CadiWrap credentials are: " + bc.getUser() + ", " + password); +// assertEquals("none", bc.getUser()); +// assertEquals("none", password); + +// } finally { +// fis.close(); +// } + +// // Closed Stream +// fis = new FileInputStream("test/example.xml"); +// fis.close(); +// bc.setCred(null); +// bc.setUser(null); +// XMLStreamException ex = wp.parse(bc, fis); +// assertNotNull(ex); +// assertNull(bc.getUser()); +// assertNull(bc.getCred()); + + +// fis = new FileInputStream("test/exampleNoSecurity.xml"); +// try { +// bc.setCred(null); +// bc.setUser(null); +// assertNull(wp.parse(bc, fis)); +// assertNull(bc.getUser()); +// assertNull(bc.getCred()); +// } finally { +// fis.close(); +// } + +// fis = new FileInputStream("test/exampleBad1.xml"); +// try { +// bc.setCred(null); +// bc.setUser(null); +// assertNull(wp.parse(bc, fis)); +// assertNull(bc.getUser()); +// assertNull(bc.getCred()); +// } finally { +// fis.close(); +// } + +// XMLStreamException e = wp.parse(bc, new ByteArrayInputStream("Not XML".getBytes())); // empty +// assertNotNull(e); + +// e = wp.parse(bc, new ByteArrayInputStream("".getBytes())); // empty +// assertNotNull(e); + + +// long start, count = 0L; +// int iter = 30000; +// File f = new File("test/CBUSevent.xml"); +// fis = new FileInputStream(f); +// is = new BufferedServletInputStream(fis); +// is.mark(0); +// try { +// while(is.read()>=0); +// } finally { +// fis.close(); +// } + +// for(int i=0;i"); + writer.println(""); + writer.println(""); + writer.println("<" + OUTER_TAG + ">"); + writer.println(" <" + INNER_TAG + ">"); + writer.println(" <" + DATA_TAG + ">" + DATA + ""); + writer.println(" <" + SELF_CLOSING_TAG + " withAnAttribute=\"That has nested \\\" marks\" />"); + writer.println(" <" + PREFIX + ":" + SUFFIX + "/>"); + writer.println(" "); + writer.println(""); + writer.flush(); + writer.close(); + return xmlFile; + } +} -- cgit 1.2.3-korg