Initial commit

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

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = BifidCipherController.class)
public class BifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/bifid";
private String decodedString = "Message to^encode";
private String encodedString = "Mqaokne kc^vdodzd";
private String keyword = "keyword";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeBifid() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(bifidLogger, times(1)).info("Encoding Bifid");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeBifid() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(bifidLogger, times(1)).info("Decoding Bifid");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class BifidCipherControllerTest{
@InjectMocks
private BifidCipherController bifidCipherController;
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_KEYWORD = CipherParameterUtil.KEYWORD;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeBifid(){
ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_INPUT_STRING);
ObjectNode returnedJson = bifidCipherController.encodeBifid(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
bifidCipherController.encodeBifid(blankNode);
});
}
@Test
public void testDecodeAutokey(){
ObjectNode cipherParams = generateParams(BIFID_KEYWORD, BIFID_OUTPUT_STRING);
ObjectNode returnedJson = bifidCipherController.decodeBifid(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(BIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
bifidCipherController.decodeBifid(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = ColumnarCipherController.class)
public class ColumnarCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/columnar";
private String decodedString = "Message to^encode";
private String encodedString = "Edeomte ac^gosnse";
private String keyword = "keyword";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeColumnar() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(columnarLogger, times(1)).info("Encoding Columnar");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeColumnar() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(columnarLogger, times(1)).info("Decoding Columnar");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class ColumnarCipherControllerTest{
@InjectMocks
private ColumnarCipherController columnarCipherController;
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_KEYWORD = CipherParameterUtil.KEYWORD;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeColumnar(){
ObjectNode cipherParams = generateParams(COLUMNAR_KEYWORD, COLUMNAR_INPUT_STRING);
ObjectNode returnedJson = columnarCipherController.encodeColumnar(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(COLUMNAR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
columnarCipherController.encodeColumnar(blankNode);
});
}
@Test
public void testDecodeColumnar(){
ObjectNode cipherParams = generateParams(COLUMNAR_KEYWORD, COLUMNAR_OUTPUT_STRING);
ObjectNode returnedJson = columnarCipherController.decodeColumnar(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(COLUMNAR_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
columnarCipherController.decodeColumnar(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = HillCipherController.class)
public class HillCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/hill";
private String decodedString = "Message to^encoded";
private String encodedString = "Mgkeqge ul^ikhisplrd";
private int[][] keyArray = new int[][]{{1, 4, 2}, {2, 4, 1}, {4, 1, 2}};
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.set(CipherParameterUtil.HILL_KEY, mapper.valueToTree(keyArray));
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.set(CipherParameterUtil.HILL_KEY, mapper.valueToTree(keyArray));
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString + "xx");
}
@Test
public void testEncodeHill() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(hillLogger, times(1)).info("Encoding Hill");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeHill() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString + "xx"));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(hillLogger, times(1)).info("Decoding Hill");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

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

View File

@@ -0,0 +1,80 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = MorseCodeController.class)
public class MorseCodeControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/morse";
private String decodedString = "Message to^encode123";
private String encodedString = "-- . ... ... .- --. . - --- . -. -.-. --- -.. . .---- ..--- ...--";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.toUpperCase().replaceAll("[^A-Z0-9]", ""));
}
@Test
public void testEncodeMorse() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(morseLogger, times(1)).info("Encoding Morse");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeMorse() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString.toUpperCase().replaceAll("[^A-Z0-9]", "")));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(morseLogger, times(1)).info("Decoding Morse");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,69 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class MorseCodeControllerTest{
@InjectMocks
private MorseCodeController morseCodeController;
private static final String MORSE_INPUT_STRING = "SOS";
private static final String MORSE_OUTPUT_STRING = "... --- ...";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeMorse(){
ObjectNode cipherParams = generateParams(MORSE_INPUT_STRING);
ObjectNode returnedJson = morseCodeController.encodeMorse(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(MORSE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
morseCodeController.encodeMorse(blankNode);
});
}
@Test
public void testDecodeMorse(){
ObjectNode cipherParams = generateParams(MORSE_OUTPUT_STRING);
ObjectNode returnedJson = morseCodeController.decodeMorse(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(MORSE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
morseCodeController.decodeMorse(blankNode);
});
}
private ObjectNode generateParams(String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,90 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = PlayfairCipherController.class)
public class PlayfairCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/playfair";
private String decodedString = "Hide the gold in - the@tree+stump";
private String decodedStringPadded = "Hide the gold in - the@trexe+stump";
private String encodedString = "Bmod zbx dnab ek - udm@uixmm+ouvif";
private String keyword = "Play-fair@Exam ple";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedStringPadded);
}
@Test
public void testEncodePlayfair() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(playfiarLogger, times(1)).info("Encoding Playfair");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodePlayfair() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedStringPadded));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(playfiarLogger, times(1)).info("Decoding Playfair");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,75 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class PlayfairCipherControllerTest{
@InjectMocks
private PlayfairCipherController playfairCipherController;
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 PLAYFAIR_OUTPUT_STRING = "Bmod zbx dnab ek - udm@uixmm+ouvif";
private static final String PLAYFAIR_KEYWORD = "Play-fair@Exam ple";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodePlayfair(){
ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_INPUT_STRING);
ObjectNode returnedJson = playfairCipherController.encodePlayfair(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(PLAYFAIR_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
playfairCipherController.encodePlayfair(blankNode);
});
}
@Test
public void testDecodePlayfair(){
ObjectNode cipherParams = generateParams(PLAYFAIR_KEYWORD, PLAYFAIR_OUTPUT_STRING);
ObjectNode returnedJson = playfairCipherController.decodePlayfair(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(DECODED_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
playfairCipherController.decodePlayfair(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = PolybiusSquareController.class)
public class PolybiusSquareControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/polybius";
private String decodedString = "Message to^encode";
private String encodedString = "41124545233212 5115^124225152212";
private String keyword = "keyword";
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString.toUpperCase());
}
@Test
public void testEncodePolybius() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(polybiusLogger, times(1)).info("Encoding Polybius");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodePolybius() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString.toUpperCase()));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(polybiusLogger, times(1)).info("Decoding Polybius");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class PolybiusSquareControllerTest{
@InjectMocks
private PolybiusSquareController polybiusSquareController;
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_KEYWORD = "Z Y+ X-";
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodePolybius(){
ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_INPUT_STRING);
ObjectNode returnedJson = polybiusSquareController.encodePolybius(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(POLYBIUS_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
polybiusSquareController.encodePolybius(blankNode);
});
}
@Test
public void testDecodePolybius(){
ObjectNode cipherParams = generateParams(POLYBIUS_KEYWORD, POLYBIUS_OUTPUT_STRING);
ObjectNode returnedJson = polybiusSquareController.decodePolybius(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(POLYBIUS_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
polybiusSquareController.decodePolybius(blankNode);
});
}
private ObjectNode generateParams(String keyword, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,89 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = RailFenceController.class)
public class RailFenceControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/railFence";
private String decodedString = "Message to^encode";
private String encodedString = "Maooesg te^cdsene";
private int rails = 3;
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeRailFence() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(railFenceLogger, times(1)).info("Encoding Rail Fence");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeRailFence() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(railFenceLogger, times(1)).info("Decoding Rail Fence");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,74 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class RailFenceControllerTest{
@InjectMocks
private RailFenceController railFenceController;
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 int RAIL_FENCE_RAILS = 5;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeRailFence(){
ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_INPUT_STRING);
ObjectNode returnedJson = railFenceController.encodeRailFence(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(RAIL_FENCE_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify inavlid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
railFenceController.encodeRailFence(blankNode);
});
}
@Test
public void testDecodeRailFence(){
ObjectNode cipherParams = generateParams(RAIL_FENCE_RAILS, RAIL_FENCE_OUTPUT_STRING);
ObjectNode returnedJson = railFenceController.decodeRailFence(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(RAIL_FENCE_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify inavlid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
railFenceController.encodeRailFence(blankNode);
});
}
private ObjectNode generateParams(int rails, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.RAIL_FENCE_RAILS, rails);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}

View File

@@ -0,0 +1,95 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import com.mattrixwv.cipherstream.controller.CipherStreamControllerIntegrationTestBase;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("integration-test")
@WebMvcTest(controllers = TrifidCipherController.class)
public class TrifidCipherControllerIntegrationTest extends CipherStreamControllerIntegrationTestBase{
@Autowired
private MockMvc mockMvc;
//Fields
private String url = "/cipherStream/trifid";
private String decodedString = "Message to^encode+";
private String encodedString = "Gqdokpd od^ljvflf+";
private String keyword = "keyword";
private String trifidFill = "=";
private int grouplength = Integer.MAX_VALUE;
@BeforeEach
public void setup(){
decodedNode = mapper.createObjectNode();
decodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
decodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
decodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
decodedNode.put(CipherParameterUtil.KEYWORD, keyword);
decodedNode.put(CipherParameterUtil.TRIFID_FILL, trifidFill);
decodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, grouplength);
decodedNode.put(CipherParameterUtil.INPUT_STRING, decodedString);
decodedNode.put(CipherParameterUtil.OUTPUT_STRING, encodedString);
encodedNode = mapper.createObjectNode();
encodedNode.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
encodedNode.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
encodedNode.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
encodedNode.put(CipherParameterUtil.KEYWORD, keyword);
encodedNode.put(CipherParameterUtil.TRIFID_FILL, trifidFill);
encodedNode.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, grouplength);
encodedNode.put(CipherParameterUtil.INPUT_STRING, encodedString);
encodedNode.put(CipherParameterUtil.OUTPUT_STRING, decodedString);
}
@Test
public void testEncodeTrifid() throws Exception{
mockMvc.perform(get(url + "/encode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(decodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(encodedString));
//Filter
super.verifyFilter(url + "/encode");
//Controller
verify(trifidLogger, times(1)).info("Encoding Trifid");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", decodedNode);
}
@Test
public void testDecodeTrifid() throws Exception{
mockMvc.perform(get(url + "/decode")
.header("X-Request-Id", requestId)
.header("X-Forwarded-For", "192.168.1.1")
.contentType(MediaType.APPLICATION_JSON)
.content(encodedNode.toString()))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(jsonPath(CipherParameterUtil.OUTPUT_STRING).value(decodedString));
//Filter
super.verifyFilter(url + "/decode");
//Controller
verify(trifidLogger, times(1)).info("Decoding Trifid");
//Cipher Aspect
verify(aspectLogger, times(1)).info("CipherStream log {}", encodedNode);
}
}

View File

@@ -0,0 +1,78 @@
package com.mattrixwv.cipherstream.controller.polysubstitution;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.exception.InvalidCipherParameterException;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
@Tag("unit-test")
@ExtendWith(MockitoExtension.class)
public class TrifidCipherControllerTest{
@InjectMocks
private TrifidCipherController trifidCipherController;
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_KEYWORD = CipherParameterUtil.KEYWORD;
private static final char TRIFID_FILL_ID = '+';
private static final int TRIFID_GROUP_LENGTH = 3;
private ObjectMapper objectMapper = new ObjectMapper();
@Test
public void testEncodeTrifid(){
ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_INPUT_STRING);
ObjectNode returnedJson = trifidCipherController.encodeTrifid(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(TRIFID_OUTPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
trifidCipherController.encodeTrifid(blankNode);
});
}
@Test
public void testDecodeTrifid(){
ObjectNode cipherParams = generateParams(TRIFID_KEYWORD, TRIFID_FILL_ID, TRIFID_GROUP_LENGTH, TRIFID_OUTPUT_STRING);
ObjectNode returnedJson = trifidCipherController.decodeTrifid(cipherParams);
assertEquals(cipherParams, returnedJson);
assertEquals(TRIFID_INPUT_STRING, returnedJson.get(CipherParameterUtil.OUTPUT_STRING).asText());
//Verify invalid params are caught
final ObjectNode blankNode = objectMapper.createObjectNode();
assertThrows(InvalidCipherParameterException.class, () -> {
trifidCipherController.decodeTrifid(blankNode);
});
}
private ObjectNode generateParams(String keyword, char fill, int groupLength, String inputString){
ObjectNode cipherParams = objectMapper.createObjectNode();
cipherParams.put(CipherParameterUtil.PRESERVE_CAPITALS, true);
cipherParams.put(CipherParameterUtil.PRESERVE_WHITESPACE, true);
cipherParams.put(CipherParameterUtil.PRESERVE_SYMBOLS, true);
cipherParams.put(CipherParameterUtil.KEYWORD, keyword);
cipherParams.put(CipherParameterUtil.TRIFID_FILL, String.valueOf(fill));
cipherParams.put(CipherParameterUtil.TRIFID_GROUP_LENGTH, groupLength);
cipherParams.put(CipherParameterUtil.INPUT_STRING, inputString);
return cipherParams;
}
}