You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by gk...@apache.org on 2019/09/13 14:03:36 UTC

svn commit: r1866909 - in /turbine/fulcrum/trunk/yaafi-crypto: pom.xml src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java

Author: gk
Date: Fri Sep 13 14:03:36 2019
New Revision: 1866909

URL: http://svn.apache.org/viewvc?rev=1866909&view=rev
Log:
- update tests using commons-io, add integration sample test
- fix mkdirs check

Modified:
    turbine/fulcrum/trunk/yaafi-crypto/pom.xml
    turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
    turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java

Modified: turbine/fulcrum/trunk/yaafi-crypto/pom.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/pom.xml?rev=1866909&r1=1866908&r2=1866909&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/pom.xml (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/pom.xml Fri Sep 13 14:03:36 2019
@@ -74,6 +74,12 @@
       <version>${turbine.log4j2.version}</version>
       <scope>test</scope><!-- change to provided ? -->
     </dependency>
+    <dependency>
+        <groupId>commons-io</groupId>
+        <artifactId>commons-io</artifactId>
+        <version>2.6</version>
+        <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>

Modified: turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java?rev=1866909&r1=1866908&r2=1866909&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/java/org/apache/fulcrum/jce/crypto/cli/CLI2.java Fri Sep 13 14:03:36 2019
@@ -161,7 +161,7 @@ public class CLI2
             targetFile = new File(args[4]);
             File parentFile = targetFile.getParentFile(); 
 
-            if (parentFile != null)
+            if (parentFile != null && (!parentFile.exists() || !parentFile.isDirectory()))
             {
                 boolean success = parentFile.mkdirs();
                 if ( !success )
@@ -179,7 +179,7 @@ public class CLI2
      * @param cipherMode the mode
      * @param password the password
      * @param sourceFile the file to process
-     * @param targetFile the targetf file
+     * @param targetFile the target file
      * @throws Exception the operation failed
      */
     public static void processFile(String cipherMode, char[] password, File sourceFile, File targetFile)
@@ -278,7 +278,7 @@ public class CLI2
     {
         String cipherMode = args[1];
         char[] password = args[2].toCharArray();
-        String value = args[3];        
+        String value = args[3];
         File targetFile = null;
         
         if (args.length == 5) 
@@ -286,13 +286,13 @@ public class CLI2
             targetFile = new File(args[4]);
             File parentFile = targetFile.getParentFile(); 
 
-            if(parentFile != null)
+            if (parentFile != null && (!parentFile.exists() || !parentFile.isDirectory()))
             {
                 boolean success = parentFile.mkdirs();
                 if ( !success )
                 {
-                	System.err.println("Error, could not create directory to write parent file");
-                }            	
+                  System.err.println("Error, could not create directory to write parent file");
+                }
                 
             }
         }
@@ -300,6 +300,33 @@ public class CLI2
         if (value != null && !value.equals("")) 
         {
         
+            String result = processString(cipherMode, password, value);
+            
+            if (targetFile != null) {
+       
+              try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(targetFile), Charset.forName("UTF-8").newEncoder() ) )
+              {
+                osw.write(result);
+              }
+            } else {
+               System.out.println( result );
+            }
+        }
+    }
+    
+    /**
+     * Decrypt and encrypt a string.
+     * 
+     * @param cipherMode \"dec|enc\" + @link{TYPES}
+     * @param password as char array
+     * @param value String to be en/decrypted
+     * @throws Exception the operation failed
+     */
+    public static String processString(String cipherMode, char[] password, String value)
+        throws Exception
+    {  
+        if (value != null && !value.equals("")) 
+        {
             CryptoUtilJ8 cryptoUtilJ8 = createCryptoUtil(cipherMode);
     
             String result = null;
@@ -311,16 +338,9 @@ public class CLI2
             {
                 result = cryptoUtilJ8.encryptString(value,password);
             }
-    
-            System.out.println( result );
-            
-            if (targetFile != null) {
-       
-            	try (OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(targetFile), Charset.forName("UTF-8").newEncoder() ) )
-            	{
-            		osw.write(result);
-            	}
-            }
+            return result;
+        } else {
+          return null;
         }
     }
     

Modified: turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java?rev=1866909&r1=1866908&r2=1866909&view=diff
==============================================================================
--- turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java (original)
+++ turbine/fulcrum/trunk/yaafi-crypto/src/test/org/apache/fulcrum/jce/crypto/extended/Main8Test.java Fri Sep 13 14:03:36 2019
@@ -1,13 +1,23 @@
 package org.apache.fulcrum.jce.crypto.extended;
 
+import static org.junit.Assert.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.io.Charsets;
+import org.apache.commons.io.FileUtils;
 import org.apache.fulcrum.jce.crypto.cli.CLI2;
 import org.apache.fulcrum.jce.crypto.extended.CryptoParametersJ8.TYPES;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
 import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
 import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
+
 import org.junit.jupiter.api.Test;
 
 /*
@@ -86,12 +96,20 @@ public class Main8Test
         String[] decryptionArgs = { "file", "dec", this.password, "./target/main8/plain.enc.txt", "./target/main8/plain.dec.txt" };
         CLI2.main(encryptionArgs);
         CLI2.main(decryptionArgs);
+        try {
+            assertTrue(
+                    FileUtils.contentEquals(new File("./src/test/data/plain.txt"), new File("./target/main8/plain.dec.txt"))
+                    );
+        } catch (IOException e) {
+            fail();
+        }
     }
     @Test
     /** Encrypt a text file in-place on the command line */
     public void testFileEncryption2()
     {
         String[] encryptionArgs = { "file", "enc", this.password, "./src/test/data/plain.txt", "./target/main8/plain.txt" };
+        // caution decrypting into source file!
         String[] decryptionArgs = { "file", "dec", this.password, "./target/main8/plain.txt" };
         CLI2.main(encryptionArgs);
         CLI2.main(decryptionArgs);
@@ -101,21 +119,95 @@ public class Main8Test
     public void testYetAnotherStringEncryption()
     {
         try {
-            String[] encryptionArgs = { "string", "enc"+TYPES.GCM, this.password, "mysecretpassword",  "./target/main8/another-plain.enc.txt"};
+            // assumptions
+            String topSecret= "mysecretpassword";
+            assertTrue(FileUtils.readFileToString(
+                    new File("./src/test/data/plain-simple.txt"), StandardCharsets.UTF_8).
+                    equals(topSecret));
+            // test
+            // encode from string = hexdecimal file
+            String[] encryptionArgs = { "string", "enc"+TYPES.GCM, this.password, topSecret, 
+                    "./target/main8/another-plain16chars.enc.txt"};
             CLI2.main(encryptionArgs);
-            String[] decryptionArgs = { "string", "dec"+TYPES.GCM, this.password, "c9fa3e7d3c49d379ee8ff2dff6e6effbafee264794a03d0ffd895caac2b3c9b4558087f5b12e72a92475f1ed638b7911389234b443d4ebcf351c86cb", "./target/main8/another-plain.dec.txt"};
+            // shows hex dec in stderr + file 
+   
+            String[] decryptionArgs = { "string", "dec"+TYPES.GCM, this.password, 
+                    "c9fa3e7d3c49d379ee8ff2dff6e6effbafee264794a03d0ffd895caac2b3c9b4558087f5b12e72a92475f1ed638b7911389234b443d4ebcf351c86cb", 
+                    "./target/main8/another-plain16chars.dec.txt"};
             CLI2.main(decryptionArgs);
-            String[] decryptionArgs2 = { "string", "dec"+TYPES.GCM, this.password, "605efd3009a7242a9c9cab23aa712d6d116e8686732194d3306416cda2a416df1e63aeffcdc1910af1e1100b382b24fc628d9c413ebf7e1b2885c0ec"};
+            //  shows clear password in stdout + file
+            
+            assertTrue(
+                    FileUtils.readFileToString(
+                            new File("./target/main8/another-plain16chars.dec.txt"), StandardCharsets.UTF_8).
+                    equals(topSecret));
+            
+            String[] decryptionArgs2 = { "string", "dec"+TYPES.GCM, this.password, 
+                    "605efd3009a7242a9c9cab23aa712d6d116e8686732194d3306416cda2a416df1e63aeffcdc1910af1e1100b382b24fc628d9c413ebf7e1b2885c0ec"};
             CLI2.main(decryptionArgs2);
+            //  shows clear password in stdout (offline decoded)
+            
+            // file mode commands do show nothing on stdout, except Decrypting / Encrypting
             
             // should not fail, if converted from hex
-            String[] decryptionArgs3 = { "file", "dec"+TYPES.GCM, this.password, "./target/main8/another-plain.enc.txt", "./target/main8/another-plain.dec.txt"};
+            String[] decryptionArgs3 = { "file", "dec"+TYPES.GCM, this.password, "./target/main8/another-plain16chars.enc.txt", "./target/main8/another-plain16chars2.dec.txt"};
             CLI2.main(decryptionArgs3);
+            assertTrue(
+                    FileUtils.readFileToString(
+                            new File("./target/main8/another-plain16chars2.dec.txt"), StandardCharsets.UTF_8).
+                    equals(topSecret));
             
-            String[] encryptionArgs4 = { "file", "enc"+TYPES.GCM, this.password, "./src/test/data/plain-simple.txt", "./target/main8/plain-simple.enc.txt" };
+            String[] encryptionArgs4 = { "file", "enc"+TYPES.GCM, this.password, 
+                    "./src/test/data/plain-simple.txt", "./target/main8/plain-simple.enc.txt" };
             CLI2.main(encryptionArgs4);
-            String[] decryptionArgs4 = { "file", "dec"+TYPES.GCM, this.password, "./target/main8/plain-simple.enc.txt", "./target/main8/plain-simple.dec.txt"};
+            
+            String[] decryptionArgs4 = { "file", "dec"+TYPES.GCM, this.password, 
+                    "./target/main8/plain-simple.enc.txt", "./target/main8/plain-simple.dec.txt"};
             CLI2.main(decryptionArgs4);
+            
+            try {
+                assertTrue(
+                        FileUtils.contentEquals(
+                                new File("./src/test/data/plain-simple.txt"), 
+                                new File("./target/main8/plain-simple.dec.txt"))
+                        );
+            } catch (IOException e) {
+                fail();
+            }
+  
+        } catch (Exception e) {
+            e.printStackTrace();
+            fail();
+        }
+    }
+    
+    @Test
+    public void testIntegratedStringEncryption()
+    {
+        try {
+            // assumptions
+            String topSecret= "myX!_secretp@ssword?~,2";
+            
+            Map<String,Object> conf = new HashMap<>();
+            conf.put("enc",TYPES.GCM);
+
+            // encode as string to stderr and  file in hexdecimal format
+            String[] encryptionArgs = { "string", "enc"+ conf.get("enc"), this.password, 
+                    topSecret, 
+                    "./target/main8/integrated-plain16chars.enc.txt"};
+            CLI2.main(encryptionArgs);
+            // shows encoded hexdec in stdout + file
+            
+            String encodedEncrypted = FileUtils.readFileToString(new File("./target/main8/integrated-plain16chars.enc.txt"), StandardCharsets.UTF_8);
+            
+            conf.put("pw", encodedEncrypted);
+
+            // this should be done without output to console
+            String result = CLI2.processString( "dec"+ conf.get("enc"), this.password.toCharArray(), (String) conf.get("pw"));
+            
+            assertTrue(
+                    result.equals(topSecret));
+
         } catch (Exception e) {
             e.printStackTrace();
             fail();