Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1078
  • Last Modified:

How can i write a Junit test case to test encyption and decryption methods of a class

How can i write a Junit test case to test encyption and decryption methods of a class where the methods take and return binary array of data. please let me know if you need any further information. Thanks in advance.
encrypt method
public static byte[] encrypt(byte[] str) { }
decrypt method 
 public static byte[] decrypt(byte[] str) { }

Open in new window

0
karpra
Asked:
karpra
  • 3
  • 2
3 Solutions
 
Kevin CrossChief Technology OfficerCommented:
Hello karpra,

Here is a nice article on the subject: http://articles.techrepublic.com.com/5100-10878_11-1027676.html#.

Regards,

mwvisa1
0
 
objectsCommented:
basically same way you write any unit test

pass it a byte array that you know the expected result and then compare the returned byte aarray with the expected result array.
We often us a file in these cases to hold the test data, one file for input and another for the expected result. Load the files into a byte array and run the test.
http://helpdesk.objects.com.au/java/how-do-i-read-the-contents-of-a-file-into-a-byte-array

You could also use something like a base64 encoded string in your code instead of a file. Using that you would first convert the string into a byte array before passing it to your method
http://helpdesk.objects.com.au/java/how-do-i-decode-a-base64-encoded-string-using-java
http://helpdesk.objects.com.au/java/how-to-create-a-base64-encoded-string-using-java

As there is some symettry in those two methods you can also pass the result from one into the other and check that what you get back is what you started with.

let me know if you need anymore information.

0
 
Kevin CrossChief Technology OfficerCommented:
Here is an example using JUnit 4.x.  EncryptDecrypt should be replaced with the actual name of your class with encrypt and decrypt methods.
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*; 
public class EncryptDecryptTest { 
    public EncryptDecryptTest() {
    } 
    @BeforeClass
    public static void setUpClass() throws Exception {
    } 
    @AfterClass
    public static void tearDownClass() throws Exception {
    } 
    @Before
    public void setUp() {
    } 
    @After
    public void tearDown() {
    } 
    /**
     * Test of encrypt method, of class EncryptDecrypt.
     */
    @Test
    public void testEncrypt() {
        System.out.println("encrypt");
        byte[] str = null;
        byte[] expResult = null;
        byte[] result = EncryptDecrypt.encrypt(str);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    } 
    /**
     * Test of decrypt method, of class EncryptDecrypt.
     */
    @Test
    public void testDecrypt() {
        System.out.println("decrypt");
        byte[] str = null;
        byte[] expResult = null;
        byte[] result = EncryptDecrypt.decrypt(str);
        assertEquals(expResult, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    } 
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Kevin CrossChief Technology OfficerCommented:
With objects suggestion, you could do in one test.  See objects links for good testing implementation using the files also.
public void testEncryptAndDecrypt() {
        System.out.println("encrypt");
        byte[] str = null;
        byte[] result = EncryptDecrypt.decrypt(EncryptDecrypt.encrypt(str));
        assertEquals(str, result);
        // TODO review the generated test code and remove the default call to fail.
        fail("The test case is a prototype.");
    } 

Open in new window

0
 
karpraAuthor Commented:
Thanks guru ,
sorry i was on vacation could not respond.
I have tested for encryption and got the below message but when i compare with binary data size its true and test succeeds. but when compared with actual binary array it fails.

Assert Failes with byte array -- assertEquals(testencyptBytes,enc); below is the error message
AssertEquals succeeds assertEquals(testencyptBytes.length,enc.length);
is the content does not match or is there any other reason?
Thanks in advance.

junit.framework.AssertionFailedError: expected:<[B@e39a3e> but was:<[B@a39137>
      at junit.framework.Assert.fail(Assert.java:47)
      at junit.framework.Assert.failNotEquals(Assert.java:282)
      at junit.framework.Assert.assertEquals(Assert.java:64)
      at junit.framework.Assert.assertEquals(Assert.java:71)
      at com.sjm.merlinrx.util.common.EncyptDecryptTest.testEncrypt(EncyptDecryptTest.java:37)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at junit.framework.TestCase.runTest(TestCase.java:154)
      at junit.framework.TestCase.runBare(TestCase.java:127)
      at junit.framework.TestResult$1.protect(TestResult.java:106)
      at junit.framework.TestResult.runProtected(TestResult.java:124)
      at junit.framework.TestResult.run(TestResult.java:109)
      at junit.framework.TestCase.run(TestCase.java:118)
      at junit.framework.TestSuite.runTest(TestSuite.java:208)
      at junit.framework.TestSuite.run(TestSuite.java:203)
      at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

0
 
objectsCommented:
you can't use assertEquals on the array, you need to instead compare each element in the array individually in a loop

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now