Updated more tests

This commit is contained in:
Matthew Ellison
2024-04-08 16:44:38 -04:00
parent 1a93a30b42
commit ff11cd72c3
52 changed files with 1813 additions and 294 deletions

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Bifid; import com.mattrixwv.cipherstream.polysubstitution.Bifid;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/bifid") @RequestMapping("/cipherStream/bifid")
public class BifidCipherController{ public class BifidCipherController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.BIFID_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.BIFID_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.BIFID_CIPHER_NAME, CipherInfoUtil.BIFID_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodeBifid(@RequestBody ObjectNode cipherParams){ public ObjectNode encodeBifid(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Bifid"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.BIFID_CIPHER_NAME);
log.info("Encoding Bifid"); log.info("Encoding {}", CipherInfoUtil.BIFID_CIPHER_NAME);
CipherParameterUtil.verifyParamsWithKeyword(cipherParams); CipherParameterUtil.verifyParamsWithKeyword(cipherParams);
@@ -44,8 +54,8 @@ public class BifidCipherController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodeBifid(@RequestBody ObjectNode cipherParams){ public ObjectNode decodeBifid(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Bifid"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.BIFID_CIPHER_NAME);
log.info("Decoding Bifid"); log.info("Decoding {}", CipherInfoUtil.BIFID_CIPHER_NAME);
CipherParameterUtil.verifyParamsWithKeyword(cipherParams); CipherParameterUtil.verifyParamsWithKeyword(cipherParams);

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Columnar; import com.mattrixwv.cipherstream.polysubstitution.Columnar;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/columnar") @RequestMapping("/cipherStream/columnar")
public class ColumnarCipherController{ public class ColumnarCipherController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.COLUMNAR_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.COLUMNAR_CIPHER_NAME, CipherInfoUtil.COLUMNAR_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodeColumnar(@RequestBody ObjectNode cipherParams){ public ObjectNode encodeColumnar(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Columnar"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.COLUMNAR_CIPHER_NAME);
log.info("Encoding Columnar"); log.info("Encoding {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
CipherParameterUtil.verifyParamsWithKeyword(cipherParams); CipherParameterUtil.verifyParamsWithKeyword(cipherParams);
@@ -44,8 +54,8 @@ public class ColumnarCipherController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodeColumnar(@RequestBody ObjectNode cipherParams){ public ObjectNode decodeColumnar(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Columnar"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.COLUMNAR_CIPHER_NAME);
log.info("Decoding Columnar"); log.info("Decoding {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
CipherParameterUtil.verifyParamsWithKeyword(cipherParams); CipherParameterUtil.verifyParamsWithKeyword(cipherParams);

View File

@@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Hill; import com.mattrixwv.cipherstream.polysubstitution.Hill;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -21,10 +22,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/hill") @RequestMapping("/cipherStream/hill")
public class HillCipherController{ public class HillCipherController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.HILL_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.HILL_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.HILL_CIPHER_NAME, CipherInfoUtil.HILL_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{ public ObjectNode encodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Hill"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.HILL_CIPHER_NAME);
log.info("Encoding Hill"); log.info("Encoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
CipherParameterUtil.verifyHillParams(cipherParams); CipherParameterUtil.verifyHillParams(cipherParams);
@@ -46,8 +56,8 @@ public class HillCipherController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{ public ObjectNode decodeHill(@RequestBody ObjectNode cipherParams) throws JsonProcessingException{
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Hill"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.HILL_CIPHER_NAME);
log.info("Decoding Hill"); log.info("Decoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
CipherParameterUtil.verifyHillParams(cipherParams); CipherParameterUtil.verifyHillParams(cipherParams);

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Morse; import com.mattrixwv.cipherstream.polysubstitution.Morse;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/morse") @RequestMapping("/cipherStream/morse")
public class MorseCodeController{ public class MorseCodeController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.MORSE_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.MORSE_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.MORSE_CIPHER_NAME, CipherInfoUtil.MORSE_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodeMorse(@RequestBody ObjectNode cipherParams){ public ObjectNode encodeMorse(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Morse"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.MORSE_CIPHER_NAME);
log.info("Encoding Morse"); log.info("Encoding {}", CipherInfoUtil.MORSE_CIPHER_NAME);
CipherParameterUtil.verifyMorseParams(cipherParams); CipherParameterUtil.verifyMorseParams(cipherParams);
@@ -40,8 +50,8 @@ public class MorseCodeController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodeMorse(@RequestBody ObjectNode cipherParams){ public ObjectNode decodeMorse(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Morse"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.MORSE_CIPHER_NAME);
log.info("Decoding Morse"); log.info("Decoding {}", CipherInfoUtil.MORSE_CIPHER_NAME);
CipherParameterUtil.verifyMorseParams(cipherParams); CipherParameterUtil.verifyMorseParams(cipherParams);

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Playfair; import com.mattrixwv.cipherstream.polysubstitution.Playfair;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/playfair") @RequestMapping("/cipherStream/playfair")
public class PlayfairCipherController{ public class PlayfairCipherController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.PLAYFAIR_CIPHER_NAME, CipherInfoUtil.PLAYFAIR_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodePlayfair(@RequestBody ObjectNode cipherParams){ public ObjectNode encodePlayfair(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Playfair"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
log.info("Encoding Playfair"); log.info("Encoding {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
CipherParameterUtil.verifyParamsWithKeyword(cipherParams); CipherParameterUtil.verifyParamsWithKeyword(cipherParams);
@@ -44,8 +54,8 @@ public class PlayfairCipherController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodePlayfair(@RequestBody ObjectNode cipherParams){ public ObjectNode decodePlayfair(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Playfair"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
log.info("Decoding Playfair"); log.info("Decoding {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
CipherParameterUtil.verifyParamsWithKeyword(cipherParams); CipherParameterUtil.verifyParamsWithKeyword(cipherParams);

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare; import com.mattrixwv.cipherstream.polysubstitution.PolybiusSquare;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/polybius") @RequestMapping("/cipherStream/polybius")
public class PolybiusSquareController{ public class PolybiusSquareController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME, CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodePolybius(@RequestBody ObjectNode cipherParams){ public ObjectNode encodePolybius(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Polybius Square"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
log.info("Encoding Polybius"); log.info("Encoding {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
CipherParameterUtil.verifyPolybiusParams(cipherParams); CipherParameterUtil.verifyPolybiusParams(cipherParams);
@@ -43,8 +53,8 @@ public class PolybiusSquareController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodePolybius(@RequestBody ObjectNode cipherParams){ public ObjectNode decodePolybius(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Polybius Square"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
log.info("Decoding Polybius"); log.info("Decoding {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
CipherParameterUtil.verifyPolybiusParams(cipherParams); CipherParameterUtil.verifyPolybiusParams(cipherParams);

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.RailFence; import com.mattrixwv.cipherstream.polysubstitution.RailFence;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/railFence") @RequestMapping("/cipherStream/railFence")
public class RailFenceController{ public class RailFenceController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.RAIL_FENCE_CIPHER_NAME, CipherInfoUtil.RAIL_FENCE_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodeRailFence(@RequestBody ObjectNode cipherParams){ public ObjectNode encodeRailFence(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Rail Fence"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
log.info("Encoding Rail Fence"); log.info("Encoding {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
CipherParameterUtil.verifyRailFenceParams(cipherParams); CipherParameterUtil.verifyRailFenceParams(cipherParams);
@@ -44,8 +54,8 @@ public class RailFenceController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodeRailFence(@RequestBody ObjectNode cipherParams){ public ObjectNode decodeRailFence(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Rail Fence"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
log.info("Decoding Rail Fence"); log.info("Decoding {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
CipherParameterUtil.verifyRailFenceParams(cipherParams); CipherParameterUtil.verifyRailFenceParams(cipherParams);

View File

@@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.polysubstitution.Trifid; import com.mattrixwv.cipherstream.polysubstitution.Trifid;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -19,10 +20,19 @@ import lombok.extern.slf4j.Slf4j;
@RestController @RestController
@RequestMapping("/cipherStream/trifid") @RequestMapping("/cipherStream/trifid")
public class TrifidCipherController{ public class TrifidCipherController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.TRIFID_CIPHER_NAME, CipherInfoUtil.TRIFID_CIPHER_DESCRIPTION);
}
@GetMapping("/encode") @GetMapping("/encode")
public ObjectNode encodeTrifid(@RequestBody ObjectNode cipherParams){ public ObjectNode encodeTrifid(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Trifid"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME);
log.info("Encoding Trifid"); log.info("Encoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
CipherParameterUtil.verifyTrifidParams(cipherParams); CipherParameterUtil.verifyTrifidParams(cipherParams);
@@ -46,8 +56,8 @@ public class TrifidCipherController{
@GetMapping("/decode") @GetMapping("/decode")
public ObjectNode decodeTrifid(@RequestBody ObjectNode cipherParams){ public ObjectNode decodeTrifid(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, "Trifid"); MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.TRIFID_CIPHER_NAME);
log.info("Decoding Trifid"); log.info("Decoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
CipherParameterUtil.verifyTrifidParams(cipherParams); CipherParameterUtil.verifyTrifidParams(cipherParams);

View File

@@ -16,9 +16,12 @@ public class CipherInfoUtil{
public static final String CIPHER_NAME = "name"; public static final String CIPHER_NAME = "name";
public static final String CIPHER_DESCRIPTION = "description"; public static final String CIPHER_DESCRIPTION = "description";
//Cipher Names //Cipher Names
//Combination
public static final String ADFGVX_CIPHER_NAME = "ADFGVX"; public static final String ADFGVX_CIPHER_NAME = "ADFGVX";
public static final String ADFGX_CIPHER_NAME = "ADFGX"; public static final String ADFGX_CIPHER_NAME = "ADFGX";
//Mono-Substitution
public static final String AFFINE_CIPHER_NAME = "Affine"; public static final String AFFINE_CIPHER_NAME = "Affine";
public static final String ATBASH_CIPHER_NAME = "Atbash"; public static final String ATBASH_CIPHER_NAME = "Atbash";
public static final String AUTOKEY_CIPHER_NAME = "Autokey"; public static final String AUTOKEY_CIPHER_NAME = "Autokey";
@@ -30,10 +33,22 @@ public class CipherInfoUtil{
public static final String PORTA_CIPHER_NAME = "Porta"; public static final String PORTA_CIPHER_NAME = "Porta";
public static final String SUBSTITUTION_CIPHER_NAME = "Substitution"; public static final String SUBSTITUTION_CIPHER_NAME = "Substitution";
public static final String VIGENERE_CIPHER_NAME = "Vigenere"; public static final String VIGENERE_CIPHER_NAME = "Vigenere";
//Poly-Substitution
public static final String BIFID_CIPHER_NAME = "Bifid";
public static final String COLUMNAR_CIPHER_NAME = "Columnar";
public static final String HILL_CIPHER_NAME = "Hill";
public static final String MORSE_CIPHER_NAME = "Morse";
public static final String PLAYFAIR_CIPHER_NAME = "Playfair";
public static final String POLYBIUS_SQUARE_CIPHER_NAME = "Polybius Square";
public static final String RAIL_FENCE_CIPHER_NAME = "Rail Fence";
public static final String TRIFID_CIPHER_NAME = "Trifid Cipher";
//TODO: Cipher descriptions //TODO: Cipher descriptions
//Combintation
public static final String ADFGVX_CIPHER_DESCRIPTION = "ADFGVX Cipher"; public static final String ADFGVX_CIPHER_DESCRIPTION = "ADFGVX Cipher";
public static final String ADFGX_CIPHER_DESCRIPTION = "ADFGX Cipher"; public static final String ADFGX_CIPHER_DESCRIPTION = "ADFGX Cipher";
//Mono-Substitution
public static final String AFFINE_CIPHER_DESCRIPTION = "Affine Cipher"; public static final String AFFINE_CIPHER_DESCRIPTION = "Affine Cipher";
public static final String ATBASH_CIPHER_DESCRIPTION = "Atbash Cipher"; public static final String ATBASH_CIPHER_DESCRIPTION = "Atbash Cipher";
public static final String AUTOKEY_CIPHER_DESCRIPTION = "Autokey Cipher"; public static final String AUTOKEY_CIPHER_DESCRIPTION = "Autokey Cipher";
@@ -45,6 +60,15 @@ public class CipherInfoUtil{
public static final String PORTA_CIPHER_DESCRIPTION = "Porta Cipher"; public static final String PORTA_CIPHER_DESCRIPTION = "Porta Cipher";
public static final String SUBSTITUTION_CIPHER_DESCRIPTION = "Substitution Cipher"; public static final String SUBSTITUTION_CIPHER_DESCRIPTION = "Substitution Cipher";
public static final String VIGENERE_CIPHER_DESCRIPTION = "Vigenere Cipher"; public static final String VIGENERE_CIPHER_DESCRIPTION = "Vigenere Cipher";
//Poly-Substitution
public static final String BIFID_CIPHER_DESCRIPTION = "Bifid Cipher";
public static final String COLUMNAR_CIPHER_DESCRIPTION = "Columnar Cipher";
public static final String HILL_CIPHER_DESCRIPTION = "Hill Cipher";
public static final String MORSE_CIPHER_DESCRIPTION = "Morse Code";
public static final String PLAYFAIR_CIPHER_DESCRIPTION = "Playfair Cipher";
public static final String POLYBIUS_SQUARE_CIPHER_DESCRIPTION = "Polybius Square Cipher";
public static final String RAIL_FENCE_CIPHER_DESCRIPTION = "Rail Fence Cipher";
public static final String TRIFID_CIPHER_DESCRIPTION = "Trifid Cipher";
public static ObjectNode buildInfoNode(String name, String description){ public static ObjectNode buildInfoNode(String name, String description){

View File

@@ -38,53 +38,6 @@ public class CipherStreamControllerIntegrationTestBase{
@Mock(name = "com.mattrixwv.cipherstream.controller.CipherStreamController") @Mock(name = "com.mattrixwv.cipherstream.controller.CipherStreamController")
protected Logger baseLogger; protected Logger baseLogger;
//Combination
@Mock(name = "com.mattrixwv.cipherstream.controller.combination.AdfgvxCipherController")
protected Logger adfgvxLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.combination.AdfgxCipherController")
protected Logger adfgxLogger;
//Monosubstitution
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AffineCipherController")
protected Logger affineLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AtbashCipherController")
protected Logger atbashLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AutokeyCipherController")
protected Logger autokeyLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaconianCipherController")
protected Logger baconianLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaseXCipherController")
protected Logger baseXLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BeaufortCipherController")
protected Logger beaufortLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.CaesarCipherController")
protected Logger caesarLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.OneTimePadCipherController")
protected Logger oneTimePadLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.PortaCipherController")
protected Logger portaLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.SubstitutionCipherController")
protected Logger substitutionLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.VigenereCipherController")
protected Logger vigenereLogger;
//Polysubstitution
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.BifidCipherController")
protected Logger bifidLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.ColumnarCipherController")
protected Logger columnarLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.HillCipherController")
protected Logger hillLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.MorseCodeController")
protected Logger morseLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PlayfairCipherController")
protected Logger playfiarLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PolybiusSquareController")
protected Logger polybiusLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.RailFenceController")
protected Logger railFenceLogger;
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.TrifidCipherController")
protected Logger trifidLogger;
//Misc //Misc
@Mock(name = "com.mattrixwv.cipherstream.config.FullFilter") @Mock(name = "com.mattrixwv.cipherstream.config.FullFilter")

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,12 +26,16 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.combination.AdfgvxCipherController")
private Logger adfgvxLogger;
//Fields //Fields
private String url = "/adfgvx"; private String url = "/adfgvx";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda"; private String encodedString = "AXgvdavfxgagfa afag^aaxdxfgdagda";
private String keyword = "keyword"; private String keyword = "keyword";
private String squareKeyword = "SquareKeyword"; private String squareKeyword = "SquareKeyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -60,7 +66,7 @@ public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.ADFGVX_CIPHER_NAME)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.ADFGVX_CIPHER_NAME))
@@ -78,7 +84,7 @@ public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeAdfgvx() throws Exception{ public void testEncodeAdfgvx() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -93,11 +99,30 @@ public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(adfgvxLogger, times(1)).info("Encoding {}", CipherInfoUtil.ADFGVX_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeAdfgvx() throws Exception{ public void testDecodeAdfgvx() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -111,4 +136,23 @@ public class AdfgvxCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(adfgvxLogger, times(1)).info("Decoding {}", CipherInfoUtil.ADFGVX_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,13 +21,26 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AdfgvxCipherControllerTest{ public class AdfgvxCipherControllerTest{
@InjectMocks @InjectMocks
private AdfgvxCipherController adfgvxCipherController; private AdfgvxCipherController adfgvxCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String KEYWORD = CipherParameterUtil.KEYWORD; private static final String KEYWORD = CipherParameterUtil.KEYWORD;
private static final String SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD; private static final String SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD;
private static final String INPUT_STRING = "Message to-encode"; private static final String INPUT_STRING = "Message to-encode";
private static final String OUTPUT_STRING = "AXgvdavfxgagfa afag-aaxdxfgdagda"; private static final String OUTPUT_STRING = "AXgvdavfxgagfa afag-aaxdxfgdagda";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.ADFGVX_CIPHER_NAME, CipherInfoUtil.ADFGVX_CIPHER_DESCRIPTION);
ObjectNode returnedJson = adfgvxCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeAdfgvx(){ public void testEncodeAdfgvx(){
ObjectNode cipherParams = generateParams(KEYWORD, SQUARE_KEYWORD, INPUT_STRING); ObjectNode cipherParams = generateParams(KEYWORD, SQUARE_KEYWORD, INPUT_STRING);
@@ -37,9 +51,10 @@ public class AdfgvxCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeAdfgvx_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
adfgvxCipherController.encodeAdfgvx(blankNode); adfgvxCipherController.encodeAdfgvx(blankNode);
}); });
@@ -55,22 +70,26 @@ public class AdfgvxCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
@Test
public void testDecodeAdfgvx_invalidParameters(){
//Verify invalid params are caught //Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
adfgvxCipherController.decodeAdfgvx(blankNode); adfgvxCipherController.decodeAdfgvx(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String squareKeyword, String inputString){ private ObjectNode generateParams(String keyword, String squareKeyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); cipherParams.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,12 +26,16 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AdfgxCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class AdfgxCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.combination.AdfgxCipherController")
private Logger adfgxLogger;
//Fields //Fields
private String url = "/adfgx"; private String url = "/adfgx";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "AAgagadfagaxxd axdx^adafafxddgdf"; private String encodedString = "AAgagadfagaxxd axdx^adafafxddgdf";
private String keyword = "keyword"; private String keyword = "keyword";
private String squareKeyword = "SquareKeyword"; private String squareKeyword = "SquareKeyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -60,7 +66,7 @@ public class AdfgxCipherControllerIntegrationTest extends CipherStreamController
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.ADFGX_CIPHER_NAME)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.ADFGX_CIPHER_NAME))
@@ -78,7 +84,7 @@ public class AdfgxCipherControllerIntegrationTest extends CipherStreamController
public void testEncodeAdfgx() throws Exception{ public void testEncodeAdfgx() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -93,11 +99,30 @@ public class AdfgxCipherControllerIntegrationTest extends CipherStreamController
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(adfgxLogger, times(1)).info("Encoding {}", CipherInfoUtil.ADFGX_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeAdfgx() throws Exception{ public void testDecodeAdfgx() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -111,4 +136,23 @@ public class AdfgxCipherControllerIntegrationTest extends CipherStreamController
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(adfgxLogger, times(1)).info("Decoding {}", CipherInfoUtil.ADFGX_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,11 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AdfgxCipherControllerTest{ public class AdfgxCipherControllerTest{
@InjectMocks @InjectMocks
private AdfgxCipherController adfgxCipherController; private AdfgxCipherController adfgxCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String ADFGX_KEYWORD = CipherParameterUtil.KEYWORD; private static final String ADFGX_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String ADFGX_SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD; private static final String ADFGX_SQUARE_KEYWORD = CipherParameterUtil.SQUARE_KEYWORD;
private static final String ADFGX_INPUT_STRING = "Message to^encode"; private static final String ADFGX_INPUT_STRING = "Message to^encode";
private static final String ADFGX_OUTPUT_STRING = "AAgagadfagaxxd axdx^adafafxddgdf"; private static final String ADFGX_OUTPUT_STRING = "AAgagadfagaxxd axdx^adafafxddgdf";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.ADFGX_CIPHER_NAME, CipherInfoUtil.ADFGX_CIPHER_DESCRIPTION);
ObjectNode returnedJson = adfgxCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeAdfgx(){ public void testEncodeAdfgx(){
@@ -36,9 +51,10 @@ public class AdfgxCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(ADFGX_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(ADFGX_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeAdfgx_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
adfgxCipherController.encodeAdfgx(blankNode); adfgxCipherController.encodeAdfgx(blankNode);
}); });
@@ -54,22 +70,25 @@ public class AdfgxCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(ADFGX_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(ADFGX_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeAdfgx_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
adfgxCipherController.decodeAdfgx(blankNode); adfgxCipherController.decodeAdfgx(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String squareKeyword, String inputString){ private ObjectNode generateParams(String keyword, String squareKeyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword); cipherParams.put(CipherParameterUtil.SQUARE_KEYWORD, squareKeyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,12 +26,16 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class AffineCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AffineCipherController")
private Logger affineLogger;
//Fields //Fields
private String url = "/cipherStream/affine"; private String url = "/cipherStream/affine";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Pbtthlb yz^burzwb"; private String encodedString = "Pbtthlb yz^burzwb";
private int key1 = 5; private int key1 = 5;
private int key2 = 7; private int key2 = 7;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -60,7 +66,7 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.AFFINE_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.AFFINE_CIPHER_DESCRIPTION))
@@ -78,7 +84,7 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeAffine() throws Exception{ public void testEncodeAffine() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -93,11 +99,30 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAffine_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(affineLogger, times(1)).info("Encoding {}", CipherInfoUtil.AFFINE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeAffine() throws Exception{ public void testDecodeAffine() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -111,4 +136,23 @@ public class AffineCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAffine_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(affineLogger, times(1)).info("Decoding {}", CipherInfoUtil.AFFINE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,13 +21,26 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AffineCipherControllerTest{ public class AffineCipherControllerTest{
@InjectMocks @InjectMocks
private AffineCipherController affineCipherController; private AffineCipherController affineCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String AFFINE_INPUT_STRING = "Message to^encode"; private static final String AFFINE_INPUT_STRING = "Message to^encode";
private static final String AFFINE_OUTPUT_STRING = "Pbtthlb yz^burzwb"; private static final String AFFINE_OUTPUT_STRING = "Pbtthlb yz^burzwb";
private static final int AFFINE_KEY_1 = 5; private static final int AFFINE_KEY_1 = 5;
private static final int AFFINE_KEY_2 = 7; private static final int AFFINE_KEY_2 = 7;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.AFFINE_CIPHER_NAME, CipherInfoUtil.AFFINE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = affineCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeAffine(){ public void testEncodeAffine(){
ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_INPUT_STRING); ObjectNode cipherParams = generateParams(AFFINE_KEY_1, AFFINE_KEY_2, AFFINE_INPUT_STRING);
@@ -37,9 +51,9 @@ public class AffineCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(AFFINE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeAffine_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.encodeAffine(blankNode); affineCipherController.encodeAffine(blankNode);
}); });
@@ -55,22 +69,24 @@ public class AffineCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(AFFINE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(AFFINE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeAffine_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
affineCipherController.decodeAffine(blankNode); affineCipherController.decodeAffine(blankNode);
}); });
} }
private ObjectNode generateParams(int key1, int key2, String inputString){ private ObjectNode generateParams(int key1, int key2, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.AFFINE_KEY_1, key1); cipherParams.put(CipherParameterUtil.AFFINE_KEY_1, key1);
cipherParams.put(CipherParameterUtil.AFFINE_KEY_2, key2); cipherParams.put(CipherParameterUtil.AFFINE_KEY_2, key2);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,10 +26,14 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AtbashCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class AtbashCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AtbashCipherController")
protected Logger atbashLogger;
//Fields //Fields
private String url = "/cipherStream/atbash"; private String url = "/cipherStream/atbash";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Nvhhztv gl^vmxlwv"; private String encodedString = "Nvhhztv gl^vmxlwv";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -54,7 +60,7 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.ATBASH_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.ATBASH_CIPHER_DESCRIPTION))
@@ -72,7 +78,7 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeAtbash() throws Exception{ public void testEncodeAtbash() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -87,11 +93,30 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAtbash_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(atbashLogger, times(1)).info("Encoding {}", CipherInfoUtil.ATBASH_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeAtbash() throws Exception{ public void testDecodeAtbash() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -105,4 +130,23 @@ public class AtbashCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAtbash_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(atbashLogger, times(1)).info("Decoding {}", CipherInfoUtil.ATBASH_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -20,9 +20,11 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AtbashCipherControllerTest{ public class AtbashCipherControllerTest{
@InjectMocks @InjectMocks
private AtbashCipherController atbashCipherController; private AtbashCipherController atbashCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String ATBASH_INPUT_STRING = "Message to^encode"; private static final String ATBASH_INPUT_STRING = "Message to^encode";
private static final String ATBASH_OUTPUT_STRING = "Nvhhztv gl^vmxlwv"; private static final String ATBASH_OUTPUT_STRING = "Nvhhztv gl^vmxlwv";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test @Test
@@ -33,9 +35,9 @@ public class AtbashCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(ATBASH_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeAtbash_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.encodeAtbash(blankNode); atbashCipherController.encodeAtbash(blankNode);
}); });
@@ -49,20 +51,22 @@ public class AtbashCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(ATBASH_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(ATBASH_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeAtbash_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
atbashCipherController.decodeAtbash(blankNode); atbashCipherController.decodeAtbash(blankNode);
}); });
} }
private ObjectNode generateParams(String inputString){ private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,16 +26,19 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AutokeyCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.AutokeyCipherController")
protected Logger autokeyLogger;
//Fields //Fields
private String url = "/cipherStream/autokey"; private String url = "/cipherStream/autokey";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh fs^wfcuhx"; private String encodedString = "Wiqooxh fs^wfcuhx";
private String keyword = "keyword"; private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
public void setup(){ public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode = mapper.createObjectNode(); decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true); decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
@@ -58,7 +63,7 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.AUTOKEY_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.AUTOKEY_CIPHER_DESCRIPTION))
@@ -76,7 +81,7 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
public void testEncodeAutokey() throws Exception{ public void testEncodeAutokey() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -91,11 +96,30 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAutokey_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(autokeyLogger, times(1)).info("Encoding {}", CipherInfoUtil.AUTOKEY_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeAutokey() throws Exception{ public void testDecodeAutokey() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -109,4 +133,23 @@ public class AutokeyCipherControllerIntegrationTest extends CipherStreamControll
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAutokey_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(autokeyLogger, times(1)).info("Decoding {}", CipherInfoUtil.AUTOKEY_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class AutokeyCipherControllerTest{ public class AutokeyCipherControllerTest{
@InjectMocks @InjectMocks
private AutokeyCipherController autokeyCipherController; private AutokeyCipherController autokeyCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String AUTOKEY_INPUT_STRING = "Message to^encode"; private static final String AUTOKEY_INPUT_STRING = "Message to^encode";
private static final String AUTOKEY_OUTPUT_STRING = "Wiqooxh fs^wfcuhx"; private static final String AUTOKEY_OUTPUT_STRING = "Wiqooxh fs^wfcuhx";
private static final String AUTOKEY_KEYWORD = CipherParameterUtil.KEYWORD; private static final String AUTOKEY_KEYWORD = CipherParameterUtil.KEYWORD;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void testGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.AUTOKEY_CIPHER_NAME, CipherInfoUtil.AUTOKEY_CIPHER_DESCRIPTION);
ObjectNode returnedJson = autokeyCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeAutokey(){ public void testEncodeAutokey(){
ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING); ObjectNode cipherParams = generateParams(AUTOKEY_KEYWORD, AUTOKEY_INPUT_STRING);
@@ -36,9 +50,10 @@ public class AutokeyCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(AUTOKEY_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeAutokey_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.encodeAutokey(blankNode); autokeyCipherController.encodeAutokey(blankNode);
}); });
@@ -54,21 +69,24 @@ public class AutokeyCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(AUTOKEY_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeAutokey_invapidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
autokeyCipherController.decodeAutokey(blankNode); autokeyCipherController.decodeAutokey(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,10 +26,14 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaconianCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class BaconianCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaconianCipherController")
protected Logger baconianLogger;
//Fields //Fields
private String url = "/cipherStream/baconian"; private String url = "/cipherStream/baconian";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; private String encodedString = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -54,7 +60,7 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BACONIAN_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BACONIAN_CIPHER_DESCRIPTION))
@@ -72,7 +78,7 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
public void testEncodeBaconian() throws Exception{ public void testEncodeBaconian() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -87,11 +93,30 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeBaconian_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(baconianLogger, times(1)).info("Encoding {}", CipherInfoUtil.BACONIAN_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeBaconian() throws Exception{ public void testDecodeBaconian() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -105,4 +130,23 @@ public class BaconianCipherControllerIntegrationTest extends CipherStreamControl
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeBaconian_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(baconianLogger, times(1)).info("Decoding {}", CipherInfoUtil.BACONIAN_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaconianCipherControllerTest{ public class BaconianCipherControllerTest{
@InjectMocks @InjectMocks
private BaconianCipherController baconianCipherController; private BaconianCipherController baconianCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String BACONIAN_INPUT_STRING = "Message to-encode"; private static final String BACONIAN_INPUT_STRING = "Message to-encode";
private static final String BACONIAN_OUTPUT_STRING = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa"; private static final String BACONIAN_OUTPUT_STRING = "ABABB aabaa baaab baaab aaaaa aabba aabaa baaba abbab aabaa abbaa aaaba abbab aaabb aabaa";
private static final String BACONIAN_DECODED_STRING = "Messagetoencode"; private static final String BACONIAN_DECODED_STRING = "Messagetoencode";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BACONIAN_CIPHER_NAME, CipherInfoUtil.BACONIAN_CIPHER_DESCRIPTION);
ObjectNode returnedJson = baconianCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeBaconian(){ public void testEncodeBaconian(){
ObjectNode cipherParams = generateParams(BACONIAN_INPUT_STRING); ObjectNode cipherParams = generateParams(BACONIAN_INPUT_STRING);
@@ -36,9 +50,10 @@ public class BaconianCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BACONIAN_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BACONIAN_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeBaconian_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
baconianCipherController.encodeBaconian(blankNode); baconianCipherController.encodeBaconian(blankNode);
}); });
@@ -54,18 +69,21 @@ public class BaconianCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BACONIAN_DECODED_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BACONIAN_DECODED_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeBaconian_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
baconianCipherController.decodeBaconian(blankNode); baconianCipherController.decodeBaconian(blankNode);
}); });
} }
private ObjectNode generateParams(String inputString){ private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaseXCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class BaseXCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BaseXCipherController")
protected Logger baseXLogger;
//Fields //Fields
private String url = "/cipherStream/basex"; private String url = "/cipherStream/basex";
private String decodedString = "A+B@C d\te\nf"; private String decodedString = "A+B@C d\te\nf";
private String encodedString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; private String encodedString = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
private int base = 2; private int base = 2;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -57,7 +63,7 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BASE_X_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BASE_X_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
public void testEncodeBaseXEncode() throws Exception{ public void testEncodeBaseXEncode() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeBaseX_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(baseXLogger, times(1)).info("Encoding {}", CipherInfoUtil.BASE_X_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeBaseXDecode() throws Exception{ public void testDecodeBaseXDecode() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class BaseXCipherControllerIntegrationTest extends CipherStreamController
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeBaseX_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(baseXLogger, times(1)).info("Decoding {}", CipherInfoUtil.BASE_X_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BaseXCipherControllerTest{ public class BaseXCipherControllerTest{
@InjectMocks @InjectMocks
private BaseXCipherController baseXCipherController; private BaseXCipherController baseXCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final int BASE_X_BASE = 2; private static final int BASE_X_BASE = 2;
private static final String BASE_X_INPUT_STRING = "A+B@C d\te\nf"; private static final String BASE_X_INPUT_STRING = "A+B@C d\te\nf";
private static final String BASE_X_OUTPUT_STRING = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110"; private static final String BASE_X_OUTPUT_STRING = "1000001 101011 1000010 1000000 1000011 100000 1100100 1001 1100101 1010 1100110";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BASE_X_CIPHER_NAME, CipherInfoUtil.BASE_X_CIPHER_DESCRIPTION);
ObjectNode returnedJson = baseXCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeBaseX(){ public void testEncodeBaseX(){
ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_INPUT_STRING); ObjectNode cipherParams = generateParams(BASE_X_BASE, BASE_X_INPUT_STRING);
@@ -36,9 +50,10 @@ public class BaseXCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BASE_X_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BASE_X_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeBaseX_invalidParameter(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
baseXCipherController.encodeBaseX(blankNode); baseXCipherController.encodeBaseX(blankNode);
}); });
@@ -54,18 +69,21 @@ public class BaseXCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BASE_X_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BASE_X_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeBaseX_invalidParameter(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
baseXCipherController.decodeBaseX(blankNode); baseXCipherController.decodeBaseX(blankNode);
}); });
} }
private ObjectNode generateParams(int base, String inputString){ private ObjectNode generateParams(int base, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
cipherParams.put(CipherParameterUtil.BASE_X_BASE, base); cipherParams.put(CipherParameterUtil.BASE_X_BASE, base);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BeaufortCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.BeaufortCipherController")
protected Logger beaufortLogger;
//Fields //Fields
private String url = "/cipherStream/beaufort"; private String url = "/cipherStream/beaufort";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Yageolz rq^ujmdag"; private String encodedString = "Yageolz rq^ujmdag";
private String keyword = "Ke*y word"; private String keyword = "Ke*y word";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -57,7 +63,7 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BEAUFORT_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BEAUFORT_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
public void testEncodeBeaufort() throws Exception{ public void testEncodeBeaufort() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(beaufortLogger, times(1)).info("Encoding {}", CipherInfoUtil.BEAUFORT_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeBeaufort() throws Exception{ public void testDecodeBeaufort() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class BeaufortCipherControllerIntegrationTest extends CipherStreamControl
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(beaufortLogger, times(1)).info("Decoding {}", CipherInfoUtil.BEAUFORT_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,14 +21,27 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BeaufortCipherControllerTest{ public class BeaufortCipherControllerTest{
@InjectMocks @InjectMocks
private BeaufortCipherController beaufortCipherController; private BeaufortCipherController beaufortCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String BEAUFORT_KEYWORD = CipherParameterUtil.KEYWORD; private static final String BEAUFORT_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String BEAUFORT_INPUT_STRING = "Message to^encode"; private static final String BEAUFORT_INPUT_STRING = "Message to^encode";
private static final String BEAUFORT_OUTPUT_STRING = "Yageolz rq^ujmdag"; private static final String BEAUFORT_OUTPUT_STRING = "Yageolz rq^ujmdag";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test @Test
public void encodeBeaufort(){ public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BEAUFORT_CIPHER_NAME, CipherInfoUtil.BEAUFORT_CIPHER_DESCRIPTION);
ObjectNode returnedJson = beaufortCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test
public void testEncodeBeaufort(){
ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_INPUT_STRING); ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_INPUT_STRING);
@@ -36,16 +50,17 @@ public class BeaufortCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BEAUFORT_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BEAUFORT_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeBeaufort_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
beaufortCipherController.encodeBeaufort(blankNode); beaufortCipherController.encodeBeaufort(blankNode);
}); });
} }
@Test @Test
public void decodeBeaufort(){ public void testDecodeBeaufort(){
ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_OUTPUT_STRING); ObjectNode cipherParams = generateParams(BEAUFORT_KEYWORD, BEAUFORT_OUTPUT_STRING);
@@ -54,21 +69,24 @@ public class BeaufortCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BEAUFORT_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BEAUFORT_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeBeaufort_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
beaufortCipherController.decodeBeaufort(blankNode); beaufortCipherController.decodeBeaufort(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class CaesarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class CaesarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.CaesarCipherController")
protected Logger caesarLogger;
//Fields //Fields
private String url = "/cipherStream/caesar"; private String url = "/cipherStream/caesar";
private String decodedString = "The quick brown fox jumps over - the lazy dog"; private String decodedString = "The quick brown fox jumps over - the lazy dog";
private String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; private String encodedString = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
private int shiftAmount = 23; private int shiftAmount = 23;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -57,7 +63,7 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.CAESAR_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.CAESAR_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
public void testEncodeCaesar() throws Exception{ public void testEncodeCaesar() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(caesarLogger, times(1)).info("Encoding {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeCaesar() throws Exception{ public void testDecodeCaesar() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class CaesarCipherControllerIntegrationTest extends CipherStreamControlle
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(caesarLogger, times(1)).info("Decoding {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class CaesarCipherControllerTest{ public class CaesarCipherControllerTest{
@InjectMocks @InjectMocks
private CaesarCipherController caesarCipherController; private CaesarCipherController caesarCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String CAESAR_INPUT_STRING = "The quick brown fox jumps over - the lazy dog"; private static final String CAESAR_INPUT_STRING = "The quick brown fox jumps over - the lazy dog";
private static final String CAESAR_OUTPUT_STRING = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald"; private static final String CAESAR_OUTPUT_STRING = "Qeb nrfzh yoltk clu grjmp lsbo - qeb ixwv ald";
private static final int CAESAR_SHIFT_AMOUNT = 23; private static final int CAESAR_SHIFT_AMOUNT = 23;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.CAESAR_CIPHER_NAME, CipherInfoUtil.CAESAR_CIPHER_DESCRIPTION);
ObjectNode returnedJson = caesarCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeCaesar(){ public void testEncodeCaesar(){
ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_INPUT_STRING); ObjectNode cipherParams = generateParams(CAESAR_SHIFT_AMOUNT, CAESAR_INPUT_STRING);
@@ -36,11 +50,12 @@ public class CaesarCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(CAESAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(CAESAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testEncodeCaesar_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
caesarCipherController.encodeCaesar(blankParams); caesarCipherController.encodeCaesar(blankNode);
}); });
} }
@@ -54,21 +69,24 @@ public class CaesarCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(CAESAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(CAESAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testDecodeCaesar_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
caesarCipherController.decodeCaesar(blankParams); caesarCipherController.decodeCaesar(blankNode);
}); });
} }
private ObjectNode generateParams(int shiftAmount, String inputString){ private ObjectNode generateParams(int shiftAmount, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount); cipherParams.put(CipherParameterUtil.CAESAR_SHIFT_AMOUNT, shiftAmount);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class OneTimePadCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.OneTimePadCipherController")
protected Logger oneTimePadLogger;
//Fields //Fields
private String url = "/cipherStream/oneTimePad"; private String url = "/cipherStream/oneTimePad";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh mv^egkgws"; private String encodedString = "Wiqooxh mv^egkgws";
private String keyword = "keywordThatIsTotallyRandom"; private String keyword = "keywordThatIsTotallyRandom";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -57,7 +63,7 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.ONE_TIME_PAD_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.ONE_TIME_PAD_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
public void testEncodeOneTimePad() throws Exception{ public void testEncodeOneTimePad() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(oneTimePadLogger, times(1)).info("Encoding {}", CipherInfoUtil.ONE_TIME_PAD_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeOneTimePad() throws Exception{ public void testDecodeOneTimePad() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class OneTimePadCipherControllerIntegrationTest extends CipherStreamContr
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(oneTimePadLogger, times(1)).info("Decoding {}", CipherInfoUtil.ONE_TIME_PAD_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,10 +21,24 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class OneTimePadCipherControllerTest{ public class OneTimePadCipherControllerTest{
@InjectMocks @InjectMocks
private OneTimePadCipherController oneTimePadCipherController; private OneTimePadCipherController oneTimePadCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String ONE_TIME_PAD_KEYWORD = "keywordThatIsTotallyRandom"; private static final String ONE_TIME_PAD_KEYWORD = "keywordThatIsTotallyRandom";
private static final String ONE_TIME_PAD_INPUT_STRING = "Message to^encode"; private static final String ONE_TIME_PAD_INPUT_STRING = "Message to^encode";
private static final String ONE_TIME_PAD_OUTPUT_STRING = "Wiqooxh mv^egkgws"; private static final String ONE_TIME_PAD_OUTPUT_STRING = "Wiqooxh mv^egkgws";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.ONE_TIME_PAD_CIPHER_NAME, CipherInfoUtil.ONE_TIME_PAD_CIPHER_DESCRIPTION);
ObjectNode returnedJson = oneTimePadCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeOneTimePad(){ public void testEncodeOneTimePad(){
@@ -35,9 +50,10 @@ public class OneTimePadCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(ONE_TIME_PAD_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(ONE_TIME_PAD_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeOneTimePad_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
oneTimePadCipherController.encodeOneTimePad(blankNode); oneTimePadCipherController.encodeOneTimePad(blankNode);
}); });
@@ -53,21 +69,24 @@ public class OneTimePadCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(ONE_TIME_PAD_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(ONE_TIME_PAD_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeOneTimePad_invalidParameter(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
oneTimePadCipherController.decodeOneTimePad(blankNode); oneTimePadCipherController.decodeOneTimePad(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -9,6 +9,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -25,11 +27,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PortaCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class PortaCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.PortaCipherController")
protected Logger portaLogger;
//Fields //Fields
private String url = "/cipherStream/porta"; private String url = "/cipherStream/porta";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Rtghuos bm^qcwgrw"; private String encodedString = "Rtghuos bm^qcwgrw";
private String keyword = "keyword"; private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -58,7 +64,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.PORTA_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.PORTA_CIPHER_DESCRIPTION))
@@ -76,7 +82,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
public void testEncodePorta() throws Exception{ public void testEncodePorta() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -86,7 +92,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
//Filter //Filter
verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString()); verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString());
verify(mdc, times(1)).put("requestId", requestId); verify(mdc, times(1)).put("requestId", requestId);
verify(mdc, times(1)).put("ip", "192.168.1.1"); verify(mdc, times(1)).put("ip", ipAddress);
verify(mdc, times(1)).put("url", url + "/encode"); verify(mdc, times(1)).put("url", url + "/encode");
verify(mdc, times(1)).clear(); verify(mdc, times(1)).clear();
//Controller //Controller
@@ -95,11 +101,30 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(portaLogger, times(1)).info("Encoding {}", CipherInfoUtil.PORTA_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodePorta() throws Exception{ public void testDecodePorta() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -109,7 +134,7 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
//Filter //Filter
verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString()); verify(filterLogger, never()).info(eq("Request parameters: {}"), anyString());
verify(mdc, times(1)).put("requestId", requestId); verify(mdc, times(1)).put("requestId", requestId);
verify(mdc, times(1)).put("ip", "192.168.1.1"); verify(mdc, times(1)).put("ip", ipAddress);
verify(mdc, times(1)).put("url", url + "/decode"); verify(mdc, times(1)).put("url", url + "/decode");
verify(mdc, times(1)).clear(); verify(mdc, times(1)).clear();
//Controller //Controller
@@ -117,4 +142,23 @@ public class PortaCipherControllerIntegrationTest extends CipherStreamController
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(portaLogger, times(1)).info("Decoding {}", CipherInfoUtil.PORTA_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PortaCipherControllerTest{ public class PortaCipherControllerTest{
@InjectMocks @InjectMocks
private PortaCipherController portaCipherController; private PortaCipherController portaCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String PORTA_KEYWORD = CipherParameterUtil.KEYWORD; private static final String PORTA_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String PORTA_INPUT_STRING = "Message to^encode"; private static final String PORTA_INPUT_STRING = "Message to^encode";
private static final String PORTA_OUTPUT_STRING = "Rtghuos bm^qcwgrw"; private static final String PORTA_OUTPUT_STRING = "Rtghuos bm^qcwgrw";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.PORTA_CIPHER_NAME, CipherInfoUtil.PORTA_CIPHER_DESCRIPTION);
ObjectNode returnedJson = portaCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodePorta(){ public void testEncodePorta(){
ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_INPUT_STRING); ObjectNode cipherParams = generateParams(PORTA_KEYWORD, PORTA_INPUT_STRING);
@@ -36,11 +50,12 @@ public class PortaCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(PORTA_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(PORTA_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testEncodePorta_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
portaCipherController.encodePorta(blankParams); portaCipherController.encodePorta(blankNode);
}); });
} }
@@ -54,21 +69,24 @@ public class PortaCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(PORTA_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(PORTA_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testDecodePorta_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
portaCipherController.decodePorta(blankParams); portaCipherController.decodePorta(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class SubstitutionCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.SubstitutionCipherController")
protected Logger substitutionLogger;
//Fields //Fields
private String url = "/cipherStream/substitution"; private String url = "/cipherStream/substitution";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Oguucig vq^gpeqfg"; private String encodedString = "Oguucig vq^gpeqfg";
private String keyword = "cdefghijklmnopqrstuvwxyzab"; private String keyword = "cdefghijklmnopqrstuvwxyzab";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -57,7 +63,7 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.SUBSTITUTION_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.SUBSTITUTION_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
public void testEncodeSubstitution() throws Exception{ public void testEncodeSubstitution() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(substitutionLogger, times(1)).info("Encoding {}", CipherInfoUtil.SUBSTITUTION_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeSubstitution() throws Exception{ public void testDecodeSubstitution() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class SubstitutionCipherControllerIntegrationTest extends CipherStreamCon
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(substitutionLogger, times(1)).info("Decoding {}", CipherInfoUtil.SUBSTITUTION_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class SubstitutionCipherControllerTest{ public class SubstitutionCipherControllerTest{
@InjectMocks @InjectMocks
private SubstitutionCipherController substitutionCipherController; private SubstitutionCipherController substitutionCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String SUBSTITUTION_KEYWORD = "cdefghijklmnopqrstuvwxyzab9876543210"; private static final String SUBSTITUTION_KEYWORD = "cdefghijklmnopqrstuvwxyzab9876543210";
private static final String SUBSTITUTION_INPUT_STRING = "Message to&encode 123"; private static final String SUBSTITUTION_INPUT_STRING = "Message to&encode 123";
private static final String SUBSTITUTION_OUTPUT_STRING = "Oguucig vq&gpeqfg 876"; private static final String SUBSTITUTION_OUTPUT_STRING = "Oguucig vq&gpeqfg 876";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.SUBSTITUTION_CIPHER_NAME, CipherInfoUtil.SUBSTITUTION_CIPHER_DESCRIPTION);
ObjectNode returnedJson = substitutionCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeSubstitution(){ public void testEncodeSubstitution(){
ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_INPUT_STRING); ObjectNode cipherParams = generateParams(SUBSTITUTION_KEYWORD, SUBSTITUTION_INPUT_STRING);
@@ -36,11 +50,12 @@ public class SubstitutionCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(SUBSTITUTION_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(SUBSTITUTION_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testEncodeSubstitution_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
substitutionCipherController.encodeSubstitution(blankParams); substitutionCipherController.encodeSubstitution(blankNode);
}); });
} }
@@ -54,21 +69,24 @@ public class SubstitutionCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(SUBSTITUTION_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(SUBSTITUTION_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testDecodeSubstitution_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
substitutionCipherController.decodeSubstitution(blankParams); substitutionCipherController.decodeSubstitution(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,6 +8,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@@ -24,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class VigenereCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class VigenereCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.monosubstitution.VigenereCipherController")
protected Logger vigenereLogger;
//Fields //Fields
private String url = "/cipherStream/vigenere"; private String url = "/cipherStream/vigenere";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Wiqooxh ds^cjqfgo"; private String encodedString = "Wiqooxh ds^cjqfgo";
private String keyword = "keyword"; private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -57,7 +63,7 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
mockMvc.perform(get(url) mockMvc.perform(get(url)
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")) .header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.VIGENERE_CIPHER_DESCRIPTION)) .andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.VIGENERE_CIPHER_DESCRIPTION))
@@ -75,7 +81,7 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
public void testEncodeVigenere() throws Exception{ public void testEncodeVigenere() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -90,11 +96,30 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(vigenereLogger, times(1)).info("Encoding {}", CipherInfoUtil.VIGENERE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeVigenere() throws Exception{ public void testDecodeVigenere() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -108,4 +133,23 @@ public class VigenereCipherControllerIntegrationTest extends CipherStreamControl
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(vigenereLogger, times(1)).info("Decoding {}", CipherInfoUtil.VIGENERE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class VigenereCipherControllerTest{ public class VigenereCipherControllerTest{
@InjectMocks @InjectMocks
private VigenereCipherController vigenereCipherController; private VigenereCipherController vigenereCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String VIGENERE_KEYWORD = CipherParameterUtil.KEYWORD; private static final String VIGENERE_KEYWORD = CipherParameterUtil.KEYWORD;
private static final String VIGENERE_INPUT_STRING = "Message to^encode"; private static final String VIGENERE_INPUT_STRING = "Message to^encode";
private static final String VIGENERE_OUTPUT_STRING = "Wiqooxh ds^cjqfgo"; private static final String VIGENERE_OUTPUT_STRING = "Wiqooxh ds^cjqfgo";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.VIGENERE_CIPHER_NAME, CipherInfoUtil.VIGENERE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = vigenereCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeVigenere(){ public void testEncodeVigenere(){
ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_INPUT_STRING); ObjectNode cipherParams = generateParams(VIGENERE_KEYWORD, VIGENERE_INPUT_STRING);
@@ -36,11 +50,12 @@ public class VigenereCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(VIGENERE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(VIGENERE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testEncodeVigenere_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
vigenereCipherController.encodeVigenere(blankParams); vigenereCipherController.encodeVigenere(blankNode);
}); });
} }
@@ -54,21 +69,24 @@ public class VigenereCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(VIGENERE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(VIGENERE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankParams = objectMapper.createObjectNode(); public void testDecodeVigenere_invalidparameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
vigenereCipherController.decodeVigenere(blankParams); vigenereCipherController.decodeVigenere(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class BifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.BifidCipherController")
protected Logger bifidLogger;
//Fields //Fields
private String url = "/cipherStream/bifid"; private String url = "/cipherStream/bifid";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Mqaokne kc^vdodzd"; private String encodedString = "Mqaokne kc^vdodzd";
private String keyword = "keyword"; private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -49,11 +57,31 @@ public class BifidCipherControllerIntegrationTest extends CipherStreamController
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BIFID_CIPHER_NAME, CipherInfoUtil.BIFID_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.BIFID_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.BIFID_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(bifidLogger, times(1)).info("Getting info for {}", CipherInfoUtil.BIFID_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodeBifid() throws Exception{ public void testEncodeBifid() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -63,16 +91,35 @@ public class BifidCipherControllerIntegrationTest extends CipherStreamController
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(bifidLogger, times(1)).info("Encoding Bifid"); verify(bifidLogger, times(1)).info("Encoding {}", CipherInfoUtil.BIFID_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(bifidLogger, times(1)).info("Encoding {}", CipherInfoUtil.BIFID_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeBifid() throws Exception{ public void testDecodeBifid() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -82,8 +129,27 @@ public class BifidCipherControllerIntegrationTest extends CipherStreamController
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(bifidLogger, times(1)).info("Decoding Bifid"); verify(bifidLogger, times(1)).info("Decoding {}", CipherInfoUtil.BIFID_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(bifidLogger, times(1)).info("Decoding {}", CipherInfoUtil.BIFID_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class BifidCipherControllerTest{ public class BifidCipherControllerTest{
@InjectMocks @InjectMocks
private BifidCipherController bifidCipherController; private BifidCipherController bifidCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String BIFID_INPUT_STRING = "Message to^encode"; private static final String BIFID_INPUT_STRING = "Message to^encode";
private static final String BIFID_OUTPUT_STRING = "Mqaokne kc^vdodzd"; private static final String BIFID_OUTPUT_STRING = "Mqaokne kc^vdodzd";
private static final String BIFID_KEYWORD = CipherParameterUtil.KEYWORD; private static final String BIFID_KEYWORD = CipherParameterUtil.KEYWORD;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.BIFID_CIPHER_NAME, CipherInfoUtil.BIFID_CIPHER_DESCRIPTION);
ObjectNode returnedJson = bifidCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeBifid(){ public void testEncodeBifid(){
ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_INPUT_STRING); ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_INPUT_STRING);
@@ -36,16 +50,17 @@ public class BifidCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeBifid_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
bifidCipherController.encodeBifid(blankNode); bifidCipherController.encodeBifid(blankNode);
}); });
} }
@Test @Test
public void testDecodeAutokey(){ public void testDecodeBifid(){
ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_OUTPUT_STRING); ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_OUTPUT_STRING);
@@ -54,21 +69,24 @@ public class BifidCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(BIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(BIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeBifid_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
bifidCipherController.decodeBifid(blankNode); bifidCipherController.decodeBifid(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class ColumnarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class ColumnarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.ColumnarCipherController")
protected Logger columnarLogger;
//Fields //Fields
private String url = "/cipherStream/columnar"; private String url = "/cipherStream/columnar";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Edeomte ac^gosnse"; private String encodedString = "Edeomte ac^gosnse";
private String keyword = "keyword"; private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -49,11 +57,31 @@ public class ColumnarCipherControllerIntegrationTest extends CipherStreamControl
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.COLUMNAR_CIPHER_NAME, CipherInfoUtil.COLUMNAR_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.COLUMNAR_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.COLUMNAR_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(columnarLogger, times(1)).info("Getting info for {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodeColumnar() throws Exception{ public void testEncodeColumnar() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -63,16 +91,35 @@ public class ColumnarCipherControllerIntegrationTest extends CipherStreamControl
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(columnarLogger, times(1)).info("Encoding Columnar"); verify(columnarLogger, times(1)).info("Encoding {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(columnarLogger, times(1)).info("Encoding {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeColumnar() throws Exception{ public void testDecodeColumnar() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -82,8 +129,27 @@ public class ColumnarCipherControllerIntegrationTest extends CipherStreamControl
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(columnarLogger, times(1)).info("Decoding Columnar"); verify(columnarLogger, times(1)).info("Decoding {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(columnarLogger, times(1)).info("Decoding {}", CipherInfoUtil.COLUMNAR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class ColumnarCipherControllerTest{ public class ColumnarCipherControllerTest{
@InjectMocks @InjectMocks
private ColumnarCipherController columnarCipherController; private ColumnarCipherController columnarCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String COLUMNAR_INPUT_STRING = "Message to*encode"; private static final String COLUMNAR_INPUT_STRING = "Message to*encode";
private static final String COLUMNAR_OUTPUT_STRING = "Edeomte ac*gosnse"; private static final String COLUMNAR_OUTPUT_STRING = "Edeomte ac*gosnse";
private static final String COLUMNAR_KEYWORD = CipherParameterUtil.KEYWORD; private static final String COLUMNAR_KEYWORD = CipherParameterUtil.KEYWORD;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.COLUMNAR_CIPHER_NAME, CipherInfoUtil.COLUMNAR_CIPHER_DESCRIPTION);
ObjectNode returnedJson = columnarCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeColumnar(){ public void testEncodeColumnar(){
ObjectNode cipherParams = generateParams(COLUMNAR_KEYWORD, COLUMNAR_INPUT_STRING); ObjectNode cipherParams = generateParams(COLUMNAR_KEYWORD, COLUMNAR_INPUT_STRING);
@@ -36,9 +50,10 @@ public class ColumnarCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(COLUMNAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(COLUMNAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeColumnar_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
columnarCipherController.encodeColumnar(blankNode); columnarCipherController.encodeColumnar(blankNode);
}); });
@@ -54,21 +69,24 @@ public class ColumnarCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(COLUMNAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(COLUMNAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeColumnar_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
columnarCipherController.decodeColumnar(blankNode); columnarCipherController.decodeColumnar(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class HillCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class HillCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.HillCipherController")
protected Logger hillLogger;
//Fields //Fields
private String url = "/cipherStream/hill"; private String url = "/cipherStream/hill";
private String decodedString = "Message to^encoded"; private String decodedString = "Message to^encoded";
private String encodedString = "Mgkeqge ul^ikhisplrd"; private String encodedString = "Mgkeqge ul^ikhisplrd";
private int[][] keyArray = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}}; private int[][] keyArray = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -49,11 +57,31 @@ public class HillCipherControllerIntegrationTest extends CipherStreamControllerI
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.HILL_CIPHER_NAME, CipherInfoUtil.HILL_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.HILL_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.HILL_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(hillLogger, times(1)).info("Getting info for {}", CipherInfoUtil.HILL_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodeHill() throws Exception{ public void testEncodeHill() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -63,16 +91,35 @@ public class HillCipherControllerIntegrationTest extends CipherStreamControllerI
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(hillLogger, times(1)).info("Encoding Hill"); verify(hillLogger, times(1)).info("Encoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(hillLogger, times(1)).info("Encoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeHill() throws Exception{ public void testDecodeHill() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -82,8 +129,27 @@ public class HillCipherControllerIntegrationTest extends CipherStreamControllerI
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(hillLogger, times(1)).info("Decoding Hill"); verify(hillLogger, times(1)).info("Decoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(hillLogger, times(1)).info("Decoding {}", CipherInfoUtil.HILL_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -14,6 +14,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,12 +23,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class HillCipherControllerTest{ public class HillCipherControllerTest{
@InjectMocks @InjectMocks
private HillCipherController hillCipherController; private HillCipherController hillCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String HILL_INPUT_STRING = "Message to^encode"; private static final String HILL_INPUT_STRING = "Message to^encode";
private static final String HILL_OUTPUT_STRING = "Mgkeqge ul^ikhisp"; 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 static final int[][] KEY = {{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.HILL_CIPHER_NAME, CipherInfoUtil.HILL_CIPHER_DESCRIPTION);
ObjectNode returnedJson = hillCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeHill() throws JsonProcessingException{ public void testEncodeHill() throws JsonProcessingException{
ObjectNode cipherParams = generateParams(HILL_INPUT_STRING); ObjectNode cipherParams = generateParams(HILL_INPUT_STRING);
@@ -38,9 +52,10 @@ public class HillCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(HILL_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.encodeHill(blankNode); hillCipherController.encodeHill(blankNode);
}); });
@@ -56,22 +71,24 @@ public class HillCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(HILL_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeHill_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
hillCipherController.decodeHill(blankNode); hillCipherController.decodeHill(blankNode);
}); });
} }
private ObjectNode generateParams(String inputString){ private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
JsonNode keyNode = objectMapper.valueToTree(KEY); JsonNode keyNode = mapper.valueToTree(KEY);
cipherParams.set(CipherParameterUtil.HILL_KEY, keyNode); cipherParams.set(CipherParameterUtil.HILL_KEY, keyNode);
return cipherParams; return cipherParams;

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,10 +26,14 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.MorseCodeController")
protected Logger morseLogger;
//Fields //Fields
private String url = "/cipherStream/morse"; private String url = "/cipherStream/morse";
private String decodedString = "Message to^encode123"; private String decodedString = "Message to^encode123";
private String encodedString = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--"; private String encodedString = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -39,12 +47,31 @@ public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIn
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.toUpperCase().replaceAll("[^A-Z0-9]", "")); encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""));
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.MORSE_CIPHER_NAME, CipherInfoUtil.MORSE_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.MORSE_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.MORSE_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(morseLogger, times(1)).info("Getting info for {}", CipherInfoUtil.MORSE_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodeMorse() throws Exception{ public void testEncodeMorse() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -54,16 +81,35 @@ public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIn
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(morseLogger, times(1)).info("Encoding Morse"); verify(morseLogger, times(1)).info("Encoding {}", CipherInfoUtil.MORSE_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(morseLogger, times(1)).info("Encoding {}", CipherInfoUtil.MORSE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeMorse() throws Exception{ public void testDecodeMorse() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -73,8 +119,27 @@ public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIn
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(morseLogger, times(1)).info("Decoding Morse"); verify(morseLogger, times(1)).info("Decoding {}", CipherInfoUtil.MORSE_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.INPUT_STRING + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(morseLogger, times(1)).info("Decoding {}", CipherInfoUtil.MORSE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,11 +21,24 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class MorseCodeControllerTest{ public class MorseCodeControllerTest{
@InjectMocks @InjectMocks
private MorseCodeController morseCodeController; private MorseCodeController morseCodeController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String MORSE_INPUT_STRING = "SOS"; private static final String MORSE_INPUT_STRING = "SOS";
private static final String MORSE_OUTPUT_STRING = "... --- ..."; private static final String MORSE_OUTPUT_STRING = "... --- ...";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.MORSE_CIPHER_NAME, CipherInfoUtil.MORSE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = morseCodeController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeMorse(){ public void testEncodeMorse(){
ObjectNode cipherParams = generateParams(MORSE_INPUT_STRING); ObjectNode cipherParams = generateParams(MORSE_INPUT_STRING);
@@ -35,9 +49,10 @@ public class MorseCodeControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(MORSE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(MORSE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncoderMorse_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
morseCodeController.encodeMorse(blankNode); morseCodeController.encodeMorse(blankNode);
}); });
@@ -53,17 +68,20 @@ public class MorseCodeControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(MORSE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(MORSE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeMorse_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
morseCodeController.decodeMorse(blankNode); morseCodeController.decodeMorse(blankNode);
}); });
} }
private ObjectNode generateParams(String inputString){ private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,12 +26,16 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PlayfairCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class PlayfairCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PlayfairCipherController")
protected Logger playfairLogger;
//Fields //Fields
private String url = "/cipherStream/playfair"; private String url = "/cipherStream/playfair";
private String decodedString = "Hide the gold in - the@tree+stump"; private String decodedString = "Hide the gold in - the@tree+stump";
private String decodedStringPadded = "Hide the gold in - the@trexe+stump"; private String decodedStringPadded = "Hide the gold in - the@trexe+stump";
private String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif"; private String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
private String keyword = "Play-fair@Exam ple"; private String keyword = "Play-fair@Exam ple";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -50,11 +58,31 @@ public class PlayfairCipherControllerIntegrationTest extends CipherStreamControl
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.PLAYFAIR_CIPHER_NAME, CipherInfoUtil.PLAYFAIR_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.PLAYFAIR_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.PLAYFAIR_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(playfairLogger, times(1)).info("Getting info for {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodePlayfair() throws Exception{ public void testEncodePlayfair() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -64,16 +92,35 @@ public class PlayfairCipherControllerIntegrationTest extends CipherStreamControl
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(playfiarLogger, times(1)).info("Encoding Playfair"); verify(playfairLogger, times(1)).info("Encoding {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(playfairLogger, times(1)).info("Encoding {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodePlayfair() throws Exception{ public void testDecodePlayfair() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -83,8 +130,27 @@ public class PlayfairCipherControllerIntegrationTest extends CipherStreamControl
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(playfiarLogger, times(1)).info("Decoding Playfair"); verify(playfairLogger, times(1)).info("Decoding {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(playfairLogger, times(1)).info("Decoding {}", CipherInfoUtil.PLAYFAIR_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,13 +21,26 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PlayfairCipherControllerTest{ public class PlayfairCipherControllerTest{
@InjectMocks @InjectMocks
private PlayfairCipherController playfairCipherController; private PlayfairCipherController playfairCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String PLAYFAIR_INPUT_STRING = "Hide the gold in - the@tree+stump"; private static final String PLAYFAIR_INPUT_STRING = "Hide the gold in - the@tree+stump";
private static final String DECODED_INPUT_STRING = "Hide the gold in - the@trexe+stump"; private static final String DECODED_INPUT_STRING = "Hide the gold in - the@trexe+stump";
private static final String PLAYFAIR_OUTPUT_STRING = "Bmod zbx dnab ek - udm@uixmm+ouvif"; private static final String PLAYFAIR_OUTPUT_STRING = "Bmod zbx dnab ek - udm@uixmm+ouvif";
private static final String PLAYFAIR_KEYWORD = "Play-fair@Exam ple"; private static final String PLAYFAIR_KEYWORD = "Play-fair@Exam ple";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.PLAYFAIR_CIPHER_NAME, CipherInfoUtil.PLAYFAIR_CIPHER_DESCRIPTION);
ObjectNode returnedJson = playfairCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodePlayfair(){ public void testEncodePlayfair(){
ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_INPUT_STRING); ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_INPUT_STRING);
@@ -37,9 +51,10 @@ public class PlayfairCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(PLAYFAIR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(PLAYFAIR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodePlayfair_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
playfairCipherController.encodePlayfair(blankNode); playfairCipherController.encodePlayfair(blankNode);
}); });
@@ -55,21 +70,24 @@ public class PlayfairCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(DECODED_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(DECODED_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodePlayfair_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
playfairCipherController.decodePlayfair(blankNode); playfairCipherController.decodePlayfair(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PolybiusSquareControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class PolybiusSquareControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.PolybiusSquareController")
protected Logger polybiusLogger;
//Fields //Fields
private String url = "/cipherStream/polybius"; private String url = "/cipherStream/polybius";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "41124545233212 5115^124225152212"; private String encodedString = "41124545233212 5115^124225152212";
private String keyword = "keyword"; private String keyword = "keyword";
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -49,11 +57,31 @@ public class PolybiusSquareControllerIntegrationTest extends CipherStreamControl
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME, CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(polybiusLogger, times(1)).info("Getting info for {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodePolybius() throws Exception{ public void testEncodePolybius() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -63,16 +91,35 @@ public class PolybiusSquareControllerIntegrationTest extends CipherStreamControl
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(polybiusLogger, times(1)).info("Encoding Polybius"); verify(polybiusLogger, times(1)).info("Encoding {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_WHITESPACE + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(polybiusLogger, times(1)).info("Encoding {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodePolybius() throws Exception{ public void testDecodePolybius() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -82,8 +129,27 @@ public class PolybiusSquareControllerIntegrationTest extends CipherStreamControl
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(polybiusLogger, times(1)).info("Decoding Polybius"); verify(polybiusLogger, times(1)).info("Decoding {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_WHITESPACE + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(polybiusLogger, times(1)).info("Decoding {}", CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class PolybiusSquareControllerTest{ public class PolybiusSquareControllerTest{
@InjectMocks @InjectMocks
private PolybiusSquareController polybiusSquareController; private PolybiusSquareController polybiusSquareController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String POLYBIUS_INPUT_STRING = "B A-T"; private static final String POLYBIUS_INPUT_STRING = "B A-T";
private static final String POLYBIUS_OUTPUT_STRING = "15 14-52"; private static final String POLYBIUS_OUTPUT_STRING = "15 14-52";
private static final String POLYBIUS_KEYWORD = "Z Y+ X-"; private static final String POLYBIUS_KEYWORD = "Z Y+ X-";
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_NAME, CipherInfoUtil.POLYBIUS_SQUARE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = polybiusSquareController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodePolybius(){ public void testEncodePolybius(){
ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_INPUT_STRING); ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_INPUT_STRING);
@@ -36,9 +50,10 @@ public class PolybiusSquareControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(POLYBIUS_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(POLYBIUS_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodePolybius_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
polybiusSquareController.encodePolybius(blankNode); polybiusSquareController.encodePolybius(blankNode);
}); });
@@ -54,21 +69,24 @@ public class PolybiusSquareControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(POLYBIUS_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(POLYBIUS_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodePolybius_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
polybiusSquareController.decodePolybius(blankNode); polybiusSquareController.decodePolybius(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, String inputString){ private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword); cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,11 +26,15 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class RailFenceControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class RailFenceControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.RailFenceController")
protected Logger railFenceLogger;
//Fields //Fields
private String url = "/cipherStream/railFence"; private String url = "/cipherStream/railFence";
private String decodedString = "Message to^encode"; private String decodedString = "Message to^encode";
private String encodedString = "Maooesg te^cdsene"; private String encodedString = "Maooesg te^cdsene";
private int rails = 3; private int rails = 3;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -49,11 +57,31 @@ public class RailFenceControllerIntegrationTest extends CipherStreamControllerIn
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.RAIL_FENCE_CIPHER_NAME, CipherInfoUtil.RAIL_FENCE_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.RAIL_FENCE_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.RAIL_FENCE_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(railFenceLogger, times(1)).info("Getting info for {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodeRailFence() throws Exception{ public void testEncodeRailFence() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -63,16 +91,35 @@ public class RailFenceControllerIntegrationTest extends CipherStreamControllerIn
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(railFenceLogger, times(1)).info("Encoding Rail Fence"); verify(railFenceLogger, times(1)).info("Encoding {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(railFenceLogger, times(1)).info("Encoding {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeRailFence() throws Exception{ public void testDecodeRailFence() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -82,8 +129,27 @@ public class RailFenceControllerIntegrationTest extends CipherStreamControllerIn
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(railFenceLogger, times(1)).info("Decoding Rail Fence"); verify(railFenceLogger, times(1)).info("Decoding {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(railFenceLogger, times(1)).info("Decoding {}", CipherInfoUtil.RAIL_FENCE_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,12 +21,25 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class RailFenceControllerTest{ public class RailFenceControllerTest{
@InjectMocks @InjectMocks
private RailFenceController railFenceController; private RailFenceController railFenceController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String RAIL_FENCE_INPUT_STRING = "Message to^encode"; private static final String RAIL_FENCE_INPUT_STRING = "Message to^encode";
private static final String RAIL_FENCE_OUTPUT_STRING = "Moetese ne^sgcdao"; private static final String RAIL_FENCE_OUTPUT_STRING = "Moetese ne^sgcdao";
private static final int RAIL_FENCE_RAILS = 5; private static final int RAIL_FENCE_RAILS = 5;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.RAIL_FENCE_CIPHER_NAME, CipherInfoUtil.RAIL_FENCE_CIPHER_DESCRIPTION);
ObjectNode returnedJson = railFenceController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeRailFence(){ public void testEncodeRailFence(){
ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_INPUT_STRING); ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_INPUT_STRING);
@@ -36,9 +50,10 @@ public class RailFenceControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(RAIL_FENCE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(RAIL_FENCE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify inavlid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeRailFence_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
railFenceController.encodeRailFence(blankNode); railFenceController.encodeRailFence(blankNode);
}); });
@@ -54,21 +69,24 @@ public class RailFenceControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(RAIL_FENCE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(RAIL_FENCE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify inavlid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeRailFence_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
railFenceController.encodeRailFence(blankNode); railFenceController.encodeRailFence(blankNode);
}); });
} }
private ObjectNode generateParams(int rails, String inputString){ private ObjectNode generateParams(int rails, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails); cipherParams.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }

View File

@@ -8,12 +8,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase; import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -22,6 +26,9 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class TrifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{ public class TrifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired @Autowired
private MockMvc mockMvc; private MockMvc mockMvc;
//Loggers
@Mock(name = "com.mattrixwv.cipherstream.controller.polysubstitution.TrifidCipherController")
protected Logger trifidLogger;
//Fields //Fields
private String url = "/cipherStream/trifid"; private String url = "/cipherStream/trifid";
private String decodedString = "Message to^encode+"; private String decodedString = "Message to^encode+";
@@ -29,6 +36,7 @@ public class TrifidCipherControllerIntegrationTest extends CipherStreamControlle
private String keyword = "keyword"; private String keyword = "keyword";
private String trifidFill = "="; private String trifidFill = "=";
private int grouplength = Integer.MAX_VALUE; private int grouplength = Integer.MAX_VALUE;
private static final ObjectNode blankNode = mapper.createObjectNode();
@BeforeEach @BeforeEach
@@ -55,11 +63,31 @@ public class TrifidCipherControllerIntegrationTest extends CipherStreamControlle
} }
@Test
public void testGetCipherInfo() throws Exception{
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.TRIFID_CIPHER_NAME, CipherInfoUtil.TRIFID_CIPHER_DESCRIPTION);
mockMvc.perform(get(url)
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_NAME).value(CipherInfoUtil.TRIFID_CIPHER_NAME))
.andExpect(jsonPath(CipherInfoUtil.CIPHER_DESCRIPTION).value(CipherInfoUtil.TRIFID_CIPHER_DESCRIPTION));
//Filter
super.verifyFilter(url);
//Controller
verify(trifidLogger, times(1)).info("Getting info for {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", infoNode);
}
@Test @Test
public void testEncodeTrifid() throws Exception{ public void testEncodeTrifid() throws Exception{
mockMvc.perform(get(url + "/encode") mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString())) .content(decodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -69,16 +97,35 @@ public class TrifidCipherControllerIntegrationTest extends CipherStreamControlle
//Filter //Filter
super.verifyFilter(url + "/encode"); super.verifyFilter(url + "/encode");
//Controller //Controller
verify(trifidLogger, times(1)).info("Encoding Trifid"); verify(trifidLogger, times(1)).info("Encoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
} }
@Test
public void testEncodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(trifidLogger, times(1)).info("Encoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
@Test @Test
public void testDecodeTrifid() throws Exception{ public void testDecodeTrifid() throws Exception{
mockMvc.perform(get(url + "/decode") mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId) .header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1") .header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString())) .content(encodedNode.toString()))
.andExpect(status().isOk()) .andExpect(status().isOk())
@@ -88,8 +135,27 @@ public class TrifidCipherControllerIntegrationTest extends CipherStreamControlle
//Filter //Filter
super.verifyFilter(url + "/decode"); super.verifyFilter(url + "/decode");
//Controller //Controller
verify(trifidLogger, times(1)).info("Decoding Trifid"); verify(trifidLogger, times(1)).info("Decoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
//Cipher Aspect //Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode); verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
} }
@Test
public void testDecodeAdfgvx_error() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", ipAddress)
.contentType(MediaType.APPLICATION_JSON)
.content(blankNode.toString()))
.andExpect(status().isBadRequest())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("message").value(CipherParameterUtil.PRESERVE_CAPITALS + CipherParameterUtil.PRESENT_MESSAGE));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(trifidLogger, times(1)).info("Decoding {}", CipherInfoUtil.TRIFID_CIPHER_NAME);
//Cipher Aspect
verifyNoInteractions(aspectLogger);
}
} }

View File

@@ -12,6 +12,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException; import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@@ -20,14 +21,27 @@ import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
public class TrifidCipherControllerTest{ public class TrifidCipherControllerTest{
@InjectMocks @InjectMocks
private TrifidCipherController trifidCipherController; private TrifidCipherController trifidCipherController;
//Fields
private static final ObjectMapper mapper = new ObjectMapper();
private static final String TRIFID_INPUT_STRING = "Message to^encode"; private static final String TRIFID_INPUT_STRING = "Message to^encode";
private static final String TRIFID_OUTPUT_STRING = "Gpjqdvd of^odlklf"; private static final String TRIFID_OUTPUT_STRING = "Gpjqdvd of^odlklf";
private static final String TRIFID_KEYWORD = CipherParameterUtil.KEYWORD; private static final String TRIFID_KEYWORD = CipherParameterUtil.KEYWORD;
private static final char TRIFID_FILL_ID = '+'; private static final char TRIFID_FILL_ID = '+';
private static final int TRIFID_GROUP_LENGTH = 3; private static final int TRIFID_GROUP_LENGTH = 3;
private ObjectMapper objectMapper = new ObjectMapper(); private static final ObjectNode blankNode = mapper.createObjectNode();
@Test
public void tetGetCipherInfo(){
ObjectNode infoNode = CipherInfoUtil.buildInfoNode(CipherInfoUtil.TRIFID_CIPHER_NAME, CipherInfoUtil.TRIFID_CIPHER_DESCRIPTION);
ObjectNode returnedJson = trifidCipherController.getCipherInfo();
assertEquals(infoNode, returnedJson);
}
@Test @Test
public void testEncodeTrifid(){ public void testEncodeTrifid(){
ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_INPUT_STRING); ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_INPUT_STRING);
@@ -38,9 +52,10 @@ public class TrifidCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(TRIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(TRIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testEncodeTrifid_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
trifidCipherController.encodeTrifid(blankNode); trifidCipherController.encodeTrifid(blankNode);
}); });
@@ -56,16 +71,18 @@ public class TrifidCipherControllerTest{
assertEquals(cipherParams, returnedJson); assertEquals(cipherParams, returnedJson);
assertEquals(TRIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText()); assertEquals(TRIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
}
//Verify invalid params are caught @Test
final ObjectNode blankNode = objectMapper.createObjectNode(); public void testDecodeTrifid_invalidParameters(){
assertThrows(InvalidCipherParameterException.class, () -> { assertThrows(InvalidCipherParameterException.class, () -> {
trifidCipherController.decodeTrifid(blankNode); trifidCipherController.decodeTrifid(blankNode);
}); });
} }
private ObjectNode generateParams(String keyword, char fill, int groupLength, String inputString){ private ObjectNode generateParams(String keyword, char fill, int groupLength, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode(); ObjectNode cipherParams = mapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true); cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true); cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true); cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
@@ -73,6 +90,7 @@ public class TrifidCipherControllerTest{
cipherParams.put(CipherParameterUtil.TRIFID_FILL, String.valueOf(fill)); cipherParams.put(CipherParameterUtil.TRIFID_FILL, String.valueOf(fill));
cipherParams.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, groupLength); cipherParams.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, groupLength);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString); cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams; return cipherParams;
} }
} }