427 lines
13 KiB
Java
427 lines
13 KiB
Java
package com.mattrixwv.cipherstream.utils;
|
|
|
|
|
|
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.junit.jupiter.MockitoExtension;
|
|
|
|
import tools.jackson.databind.ObjectMapper;
|
|
import tools.jackson.databind.node.ObjectNode;
|
|
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
|
|
|
|
|
|
@Tag("unit-test")
|
|
@ExtendWith(MockitoExtension.class)
|
|
public class CipherParameterUtilTest{
|
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
@Test
|
|
public void testVerifyUniversalParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Capitals
|
|
params.remove(CipherParameterUtil.PRESERVE_CAPITALS);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
//Integer Capitals
|
|
params.put(CipherParameterUtil.PRESERVE_CAPITALS, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
|
|
|
|
//Missing Whitespace
|
|
params.remove(CipherParameterUtil.PRESERVE_WHITESPACE);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
//Integer Whitespace
|
|
params.put(CipherParameterUtil.PRESERVE_WHITESPACE, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
|
|
|
|
//Missing Symbols
|
|
params.remove(CipherParameterUtil.PRESERVE_SYMBOLS);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
//Integer Symbols
|
|
params.put(CipherParameterUtil.PRESERVE_SYMBOLS, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
|
|
|
|
//Missing Input
|
|
params.remove(CipherParameterUtil.INPUT_STRING);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
//Integer Input
|
|
params.put(CipherParameterUtil.INPUT_STRING, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.INPUT_STRING, "Input String");
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyUniversalParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyParamsWithKeyword(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Keyword
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyParamsWithKeyword(params);
|
|
});
|
|
//Integer Keyword
|
|
params.put(CipherParameterUtil.KEYWORD, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyParamsWithKeyword(params);
|
|
});
|
|
|
|
//Full
|
|
params.put(CipherParameterUtil.KEYWORD, CipherParameterUtil.KEYWORD);
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyParamsWithKeyword(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyCaesarParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing shift
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyCaesarParams(params);
|
|
});
|
|
//String shift
|
|
params.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, "1");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyCaesarParams(params);
|
|
});
|
|
|
|
//Full
|
|
params.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, 1);
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyCaesarParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyAffineParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Key1
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyAffineParams(params);
|
|
});
|
|
//String Key1
|
|
params.put(CipherParameterUtil.AFFINE_KEY_1, "1");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyAffineParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.AFFINE_KEY_1, 1);
|
|
|
|
//Missing Key2
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyAffineParams(params);
|
|
});
|
|
//String Key2
|
|
params.put(CipherParameterUtil.AFFINE_KEY_2, "1");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyAffineParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.AFFINE_KEY_2, 1);
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyAffineParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyAtbashParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyAtbashParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyBaconianParams(){
|
|
ObjectNode params = objectMapper.createObjectNode();
|
|
|
|
//Missing Capitals
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaconianParams(params);
|
|
});
|
|
//Integer Capitals
|
|
params.put(CipherParameterUtil.PRESERVE_CAPITALS, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaconianParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
|
|
|
|
//Missing Input
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaconianParams(params);
|
|
});
|
|
//Integer Input
|
|
params.put(CipherParameterUtil.INPUT_STRING, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaconianParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.INPUT_STRING, "Input String");
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyBaconianParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyBaseXParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Input
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaseXParams(params);
|
|
});
|
|
//Integer Input
|
|
params.put(CipherParameterUtil.INPUT_STRING, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaseXParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.INPUT_STRING, CipherParameterUtil.INPUT_STRING);
|
|
|
|
//Missing BaseX
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaseXParams(params);
|
|
});
|
|
//String BaseX
|
|
params.put(CipherParameterUtil.BASE_X_BASE, "2");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyBaseXParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.BASE_X_BASE, 2);
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyBaseXParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifySquareKeyword(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Keyword
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifySquareKeyword(params);
|
|
});
|
|
|
|
//Integer Keyword
|
|
params.put(CipherParameterUtil.SQUARE_KEYWORD, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifySquareKeyword(params);
|
|
});
|
|
|
|
//Full
|
|
params.put(CipherParameterUtil.SQUARE_KEYWORD, "squareKeyword");
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifySquareKeyword(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyHillParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Key
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyHillParams(params);
|
|
});
|
|
|
|
//Integer Key
|
|
params.put(CipherParameterUtil.HILL_KEY, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyHillParams(params);
|
|
});
|
|
|
|
//1D Array Key
|
|
params.set(CipherParameterUtil.HILL_KEY, objectMapper.valueToTree(new int[]{1}));
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyHillParams(params);
|
|
});
|
|
|
|
//String Array Key
|
|
params.set(CipherParameterUtil.HILL_KEY, objectMapper.valueToTree(new String[][]{{"test"}}));
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyHillParams(params);
|
|
});
|
|
|
|
//Full
|
|
params.set(CipherParameterUtil.HILL_KEY, objectMapper.valueToTree(new int[][]{{1}}));
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyHillParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyMorseParams(){
|
|
ObjectNode params = objectMapper.createObjectNode();
|
|
|
|
//Missing Input
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyMorseParams(params);
|
|
});
|
|
//Integer Input
|
|
params.put(CipherParameterUtil.INPUT_STRING, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyMorseParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.INPUT_STRING, "Input String");
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyMorseParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyPolybiusParams(){
|
|
ObjectNode params = objectMapper.createObjectNode();
|
|
|
|
//Missing Whitespace
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
//String Whitespace
|
|
params.put(CipherParameterUtil.PRESERVE_WHITESPACE, "true");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
|
|
|
|
//Missing Symbols
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
//String Symbols
|
|
params.put(CipherParameterUtil.PRESERVE_SYMBOLS, "true");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
|
|
|
|
//Missing Input
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
//Integer Input
|
|
params.put(CipherParameterUtil.INPUT_STRING, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.INPUT_STRING, "");
|
|
|
|
//Missing Keyword
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
//Integer Keyword
|
|
params.put(CipherParameterUtil.KEYWORD, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.KEYWORD, CipherParameterUtil.KEYWORD);
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyPolybiusParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyRailFenceParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
|
|
//Missing Rails
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyRailFenceParams(params);
|
|
});
|
|
//String rails
|
|
params.put(CipherParameterUtil.RAIL_FENCE_RAILS, "1");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyRailFenceParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.RAIL_FENCE_RAILS, 1);
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyRailFenceParams(params);
|
|
});
|
|
}
|
|
|
|
@Test
|
|
public void testVerifyTrifidParams(){
|
|
ObjectNode params = setUniversalParams();
|
|
params.put(CipherParameterUtil.KEYWORD, CipherParameterUtil.KEYWORD);
|
|
|
|
//Missing fill
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyTrifidParams(params);
|
|
});
|
|
//Integer fill
|
|
params.put(CipherParameterUtil.TRIFID_FILL, 1);
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyTrifidParams(params);
|
|
});
|
|
//String fill
|
|
params.put(CipherParameterUtil.TRIFID_FILL, "Input");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyTrifidParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.TRIFID_FILL, "+");
|
|
|
|
//Missing length
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyTrifidParams(params);
|
|
});
|
|
//String length
|
|
params.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, "2");
|
|
assertThrows(InvalidCipherParameterException.class, () -> {
|
|
CipherParameterUtil.verifyTrifidParams(params);
|
|
});
|
|
params.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, 2);
|
|
|
|
//Full
|
|
assertDoesNotThrow(() -> {
|
|
CipherParameterUtil.verifyTrifidParams(params);
|
|
});
|
|
}
|
|
|
|
|
|
private ObjectNode setUniversalParams(){
|
|
ObjectNode universalParams = objectMapper.createObjectNode();
|
|
universalParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
|
|
universalParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
|
|
universalParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
|
|
universalParams.put(CipherParameterUtil.INPUT_STRING, "Input String");
|
|
return universalParams;
|
|
}
|
|
}
|