I have implemented a very simple class called Enigma
which has a symmetric key and two methods: byte[] encryptString(String strToEncrypt)
and String decryptBytes(byte[] arrayToDecrypt)
.
I am trying to write some tests for the methods. I have thought of testing that the encrypt and decrypt methods are the inverse of each other, but that says nothing about each of them individually. I wanted to use the methods as they are now to obtain a battery of input-outputs and set that as the tests (I know that is not ideal, but the purpose of these tests is to guarantee that the function behaves in the future as it does today, not that the encryption/decryption are good).
However, I do not know how to obtain a representation of the byte array output by byte[] encryptString(String strToEncrypt)
so that I can hardcode it in the test class.
Any ideas?
A few notes on how to test this ( personal opinion warning :-) )
My last point there brings me (finally ?) to a recommendation. Think about what's the responsibility of the class (hopefully, the single responsibility, see SRP). In this case, I believe that the responsibility of your class is two way encryption of strings.
Therefore, I would write the following tests:
@Test
public void testThatEncryptingStringResultsInExpectedBytes() {
byte[] encryption = enigma.encryptString(TEST_STRING);
assertArrayEquals(EXPECTED_ENCRYPTION, encryption);
}
@Test
public void testThatDecryptinEncryptionResultsInOriginalString() {
String decryption = enigma.decryptBytes(TEST_ENCRYPTION);
assertEquals(EXPECTED_ORIGINAL_STRING, decryption);
}
@Test
public void testThatDecriptionReversesEncryption() {
String decryption = enigma.decryptBytes(enigma.encryptString(TEST_STRING));
assertEquals(TEST_STRING, decryption);
}
@Test
public void testThatEncryptionReversesDecryption() {
byte[] encryption = enigma.encriptString(enigma.decryptBytes(TEST_ENCRYPTION));
assertEquals(TEST_ENCRYPTION, encryption);
}
Maybe add some more tests to check that trying to encrypt/decrypt invalid values throws exceptions, and other error cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With