Commit 1e45a9bb by Facundo Javier Gelatti

Use presenter in EmbeddingAndExtractingTest

1 parent 784a7684
package org.stegosuite;
import org.eclipse.swt.graphics.ImageData;
import org.junit.After;
import org.junit.Test;
import org.stegosuite.image.embedding.EmbeddingProgress;
import org.stegosuite.image.embedding.Visualizer;
import org.stegosuite.image.format.ImageFormat;
import org.stegosuite.model.exception.SteganoEmbedException;
import org.stegosuite.model.exception.SteganoExtractException;
import org.stegosuite.model.exception.SteganoImageException;
import org.stegosuite.model.exception.SteganoKeyException;
import org.stegosuite.model.payload.Payload;
import org.stegosuite.model.payload.block.MessageBlock;
import org.stegosuite.ui.gui.embedding.Embedding;
import org.stegosuite.ui.gui.embedding.EmbeddingFactory;
import org.stegosuite.util.FileUtils;
import org.stegosuite.ui.gui.StegosuitePresenter;
import org.stegosuite.ui.gui.StegosuiteUI;
import java.io.File;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
......@@ -25,7 +24,9 @@ public class EmbeddingAndExtractingTest {
private static final String THE_PASSWORD = "a password";
private static final String INCORRECT_PASSWORD = "an incorrect password";
private final StegosuiteUI ui = new UIStub();
private String outputPath;
private String extractedMessage;
@After
public void tearDown() throws Exception {
......@@ -50,93 +51,79 @@ public class EmbeddingAndExtractingTest {
}
@Test(expected = SteganoKeyException.class)
public void testIncorrectPasswordFromGifFile() throws Exception {
public void testIncorrectPasswordFromGifFile() throws Throwable {
testExtractingWhenPasswordIsIncorrect("sunflower_embed_ok.gif");
}
@Test(expected = SteganoKeyException.class)
public void testIncorrectPasswordFromBmpFile() throws Exception {
public void testIncorrectPasswordFromBmpFile() throws Throwable {
testExtractingWhenPasswordIsIncorrect("snow_embed_ok.bmp");
}
@Test(expected = SteganoKeyException.class)
public void testIncorrectPasswordFromJpgFile() throws Exception {
public void testIncorrectPasswordFromJpgFile() throws Throwable {
testExtractingWhenPasswordIsIncorrect("landscape_embed_ok.jpg");
}
private void testEmbeddingAndThenExtractingOk(String imageName) throws SteganoImageException, SteganoEmbedException, InterruptedException, java.util.concurrent.ExecutionException, SteganoExtractException {
// Embed
Payload payloadToEmbed = new Payload();
setPassword(payloadToEmbed, THE_PASSWORD);
addMessage(payloadToEmbed, THE_MESSAGE);
outputPath = embedPayload(imageName, payloadToEmbed);
embedPayload(imageName, THE_PASSWORD, THE_MESSAGE);
assertTrue(new File(outputPath).exists());
// Extract
Payload extractedPayload = extractPayload(outputPath, THE_PASSWORD);
assertEquals(THE_MESSAGE, getMessageFrom(extractedPayload));
extractPayload(outputPath, THE_PASSWORD);
assertEquals(THE_MESSAGE, extractedMessage);
}
private void testExtractingWhenPasswordIsIncorrect(String imageName) throws SteganoImageException, SteganoExtractException, InterruptedException, java.util.concurrent.ExecutionException {
String imagePath = pathOf(imageName);
extractPayload(imagePath, INCORRECT_PASSWORD);
private void testExtractingWhenPasswordIsIncorrect(String imageName) throws Throwable {
try {
extractPayload(pathOf(imageName), INCORRECT_PASSWORD);
} catch (Exception e) {
throw e.getCause();
}
}
private String embedPayload(String imageName, Payload embedPayload) throws SteganoImageException, SteganoEmbedException {
AtomicReference<String> outputPathToReturn = new AtomicReference<>();
Embedding embedding = getEmbeddingFor(pathOf(imageName));
embedding.embed(embedPayload, (embeddingMethod, embeddedImage) -> {
try {
String outputPath = getOutputPathFor(embeddedImage);
embeddedImage.save(new File(outputPath));
outputPathToReturn.set(outputPath);
} catch (SteganoImageException e) {
e.printStackTrace();
}
});
return outputPathToReturn.get();
private void embedPayload(String imageName, String password, String message) throws SteganoImageException, SteganoEmbedException {
String imagePath = pathOf(imageName);
StegosuitePresenter presenter = getPresenterFor(imagePath);
presenter.embedNotifying(new EmbeddingProgress(), message, password);
}
private String getOutputPathFor(ImageFormat embeddedImage) {
return FileUtils.addFileNameSuffix(embeddedImage.getFile().getAbsolutePath(), "_embed");
private void extractPayload(String imagePath, String password) throws SteganoImageException, SteganoExtractException {
StegosuitePresenter presenter = getPresenterFor(imagePath);
presenter.extractNotifying(new EmbeddingProgress(), password);
}
private Payload extractPayload(String imagePath, String password) throws SteganoImageException, SteganoExtractException {
Embedding extractEmbedding = getEmbeddingFor(imagePath);
Payload extractPayload = new Payload();
setPassword(extractPayload, password);
extractEmbedding.extract(extractPayload, embeddingMethod -> {});
return extractPayload;
private String pathOf(String imageName) {
return getClass().getClassLoader().getResource(imageName).getPath();
}
private Embedding getEmbeddingFor(String imagePath) throws SteganoImageException {
private StegosuitePresenter getPresenterFor(String imagePath) throws SteganoImageException {
ImageFormat image = ImageFormat.getImageFormat(imagePath);
Embedding embedding = EmbeddingFactory.getEmbedding(image);
embedding.setPointFilter(1);
return embedding;
return new StegosuitePresenter(image, ui);
}
private String pathOf(String imageName) {
return getClass().getClassLoader().getResource(imageName).getPath();
}
private class UIStub implements StegosuiteUI {
@Override
public void showEmbeddingError(SteganoEmbedException e) {
throw new RuntimeException(e);
}
private void setPassword(Payload payload, String password) {
payload.setPassword(password);
}
@Override
public void showExtractingError(SteganoExtractException e) {
throw new RuntimeException(e);
}
private void addMessage(Payload payload, String theMessage) {
payload.addBlock(new MessageBlock(theMessage));
}
@Override
public void extractingCompleted(String extractedMessage, String statusMessage, Visualizer visualizer, ImageData imageData) {
EmbeddingAndExtractingTest.this.extractedMessage = extractedMessage;
}
@Override
public void embeddingCompleted(ImageFormat embeddedImage, String outputPath, Visualizer visualizer) {
EmbeddingAndExtractingTest.this.outputPath = outputPath;
}
private String getMessageFrom(Payload payload) {
return ((MessageBlock) payload.getBlock(0)).getMessage();
@Override
public void addPayloadFile(String filename, String extension, long fileSize) {
}
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!