/* * [StressTest.java] * * Summary: Tests the classes by exercising them on every file in the current directory. * * Copyright: (c) 2004-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.4 2006-03-05 reformat with IntelliJ and add Javadoc. */ package com.mindprod.transporter; import com.mindprod.common18.EIO; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import static java.lang.System.*; /** * Tests the classes by exercising them on every file in the current directory. * * @author Roedy Green, Canadian Mind Products * @version 1.4 2006-03-05 reformat with IntelliJ and add Javadoc. * @since 2004-06-09 */ public final class StressTest { /** * size of test in bytes */ private static int size; /** * start timestamp of test */ private static long start; /** * stop timestamp of test */ private static long stop; /** * ensure we got back to where we started * * @param original original array * @param reconstituted reconstituted array */ private static void mustBeSame( byte[] original, byte[] reconstituted ) { if ( !Tools.equals( original, reconstituted ) ) { throw new IllegalArgumentException( "original and reconstituted don't match" ); } } /** * report on how fast a test was * * @param title title for the test */ private static void report( String title ) { long elapsed = stop - start; out.print( title + " " + elapsed + " milliseconds" ); if ( elapsed != 0 ) { // cannot use TimeUnit in Java 1.2 out.print( " (" + ( size * 1000 / elapsed ) + " bytes per second)" ); } out.println(); out.println(); } /** * Exercise com.mindprod.transporter classes invoke with:
java com.mindprod.transporter.StressTest * * @param args not used */ public static void main( String[] args ) { try { final File dir = new File( "." ); String[] filenames = dir.list(); // load keys, previously prepared with: // java.exe com.mindprod.transporter.MakeKeys testSender 1024 // and: // java.exe com.mindprod.tranporter.MakeKeys testReceiver 1024 // DON'T USE THESE TEST KEYS IN YOUR OWN APPLICATIONS!! // THEY ARE ANYTHING BUT SECRET!! PrivateKey senderPrivateKey = new PrivateKey( new FileInputStream( "E:/com/mindprod/transporter/testSenderPrivateKey.ser" ) ); // test PrivateKey receiverPrivateKey = new PrivateKey( new FileInputStream( "E:/com/mindprod/transporter/testReceiverPrivateKey.ser" ) ); PublicKey senderPublicKey = new PublicKey( new FileInputStream( "E:/com/mindprod/transporter/testSenderPublicKey.ser" ) ); PublicKey receiverPublicKey = new PublicKey( new FileInputStream( "E:/com/mindprod/transporter/testReceiverPublicKey.ser" ) ); out.println( "Testing com.mindprod.transporter classes" ); out.println(); out.println( "Sender keySize " + senderPrivateKey.keySize ); out.println( "Receiver keySize " + receiverPrivateKey .keySize ); out.println(); // loop for each file in the test directory for ( final String filename : filenames ) { final File file = new File( dir, filename ); if ( file.isDirectory() ) { continue; } out.println( "<><>Stress testing file " + filename + "<><>" ); out.println(); size = ( int ) file.length(); FileInputStream fis = new FileInputStream( file ); // R E A D byte[] originalMessage = new byte[ size ]; int charsRead = fis.read( originalMessage ); if ( charsRead != size ) { throw new IOException( "error: problems reading file " + EIO.getCanOrAbsPath( file ) ); } // C L O S E fis.close(); { out.println( "test serialization " + size + " bytes." ); start = System.currentTimeMillis(); Wrap w = new Wrap(); byte[] transport = w.serialize( originalMessage ); stop = System.currentTimeMillis(); report( "serialise" ); out.println( transport.length + " bytes to send." ); start = System.currentTimeMillis(); Unwrap u = new Unwrap(); byte[] reconstitutedMessage = ( byte[] ) u .reconstitute( transport ); stop = System.currentTimeMillis(); mustBeSame( originalMessage, reconstitutedMessage ); report( "reconstitute" ); } { out.println( "test compression " + size + " bytes." ); start = System.currentTimeMillis(); Wrap w = new Wrap(); byte[] transport = w.compress( w .serialize( originalMessage ) ); stop = System.currentTimeMillis(); report( "compress" ); out.println( transport.length + " bytes to send." ); start = System.currentTimeMillis(); Unwrap u = new Unwrap(); byte[] reconstitutedMessage = ( byte[] ) u.reconstitute( u .decompress( transport ) ); stop = System.currentTimeMillis(); mustBeSame( originalMessage, reconstitutedMessage ); report( "decompress" ); } { out.println( "test Wrap/Unwrap no encryption or signing " + size + " bytes." ); out.println(); start = System.currentTimeMillis(); Wrap w = new Wrap(); // you can send any Serializable object, including Strings // and raw byte arrays. String transport = w.wrap( originalMessage ); stop = System.currentTimeMillis(); report( "wrap" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); Unwrap u = new Unwrap(); byte[] reconstitutedMessage = ( byte[] ) u.unwrap( transport ); stop = System.currentTimeMillis(); mustBeSame( originalMessage, reconstitutedMessage ); report( "unwrap" ); } { out.println( "test Encrypt/Decrypt, encryption but no signing " + size + " bytes." ); start = System.currentTimeMillis(); Encrypt e = new Encrypt( receiverPublicKey ); String transport = e.wrap( originalMessage ); stop = System.currentTimeMillis(); report( "encrypt" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); Decrypt d = new Decrypt( receiverPrivateKey ); byte[] reconstitutedMessage = ( byte[] ) d.unwrap( transport ); stop = System.currentTimeMillis(); mustBeSame( originalMessage, reconstitutedMessage ); report( "decrypt" ); } { out.println( "test Sign/Verify digital signature without encryption " + size + " bytes." ); start = System.currentTimeMillis(); Sign s = new Sign( senderPrivateKey ); String transport = s.wrap( originalMessage ); stop = System.currentTimeMillis(); report( "sign" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); Verify v = new Verify( senderPublicKey ); byte[] reconstitutedMessage = ( byte[] ) v.unwrap( transport ); stop = System.currentTimeMillis(); mustBeSame( originalMessage, reconstitutedMessage ); report( "verify" ); } { out.println( "test SignAndEncrypt/DecryptAndVerify " + size + " bytes." ); start = System.currentTimeMillis(); SignAndEncrypt s = new SignAndEncrypt( senderPrivateKey, receiverPublicKey ); String transport = s.wrap( originalMessage ); stop = System.currentTimeMillis(); report( "sign and encrypt" ); out.println( transport.length() + " chars to send." ); // transport String is sent to another machine... and then start = System.currentTimeMillis(); DecryptAndVerify v = new DecryptAndVerify( receiverPrivateKey, senderPublicKey ); byte[] reconstitutedMessage = ( byte[] ) v.unwrap( transport ); stop = System.currentTimeMillis(); mustBeSame( originalMessage, reconstitutedMessage ); report( "decrypt and verify" ); } } // end for } catch ( FileNotFoundException e ) { err.println(); err.println(); e.printStackTrace( System.err ); err.println( "can't find files" ); err.println(); } catch ( IOException e ) { e.printStackTrace( System.err ); err.println( "Some problem with I/O " ); err.println(); } } }