Initial commit

This commit is contained in:
2024-04-07 19:41:57 -04:00
parent 2b3212cb44
commit 8f0d1c64bd
95 changed files with 6787 additions and 45 deletions

View File

@@ -0,0 +1,79 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class HillCipherControllerTest{
@InjectMocks
private HillCipherController hillCipherController;
private static final String HILL_INPUT_STRING = "Message to^encode";
private static final String HILL_OUTPUT_STRING = "Mgkeqge ul^ikhisp";
private static final int[][] KEY = {{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeHill() throws JsonProcessingException{
ObjectNode cipherParams = generateParams(HILL_INPUT_STRING);
ObjectNode returnedJson = hillCipherController.encodeHill(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.encodeHill(blankNode);
});
}
@Test
public void testDecodeHill() throws JsonProcessingException{
ObjectNode cipherParams = generateParams(HILL_OUTPUT_STRING);
ObjectNode returnedJson = hillCipherController.decodeHill(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.decodeHill(blankNode);
});
}
private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
JsonNode keyNode = objectMapper.valueToTree(KEY);
cipherParams.set(CipherParameterUtil.HILL_KEY, keyNode);
return cipherParams;
}
}