Skip to content

Commit

Permalink
Fix binary tests in database reader/writer
Browse files Browse the repository at this point in the history
  • Loading branch information
cternes committed Oct 25, 2017
1 parent a63672b commit aeb10ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package de.slackspace.openkeepass.api;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import javax.xml.bind.DatatypeConverter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -13,6 +14,9 @@
import java.util.List;
import java.util.UUID;

import org.junit.Assert;
import org.junit.Test;

import de.slackspace.openkeepass.KeePassDatabase;
import de.slackspace.openkeepass.domain.Attachment;
import de.slackspace.openkeepass.domain.CompressionAlgorithm;
Expand All @@ -25,8 +29,7 @@
import de.slackspace.openkeepass.exception.KeePassDatabaseUnreadableException;
import de.slackspace.openkeepass.util.ByteUtils;
import de.slackspace.openkeepass.util.ResourceUtils;
import org.junit.Assert;
import org.junit.Test;
import de.slackspace.openkeepass.util.StreamUtils;

public class KeepassDatabaseReaderTest {

Expand Down Expand Up @@ -337,18 +340,24 @@ public void whenGettingColorsShouldReturnColors() throws FileNotFoundException {
}

@Test
public void whenGettingAttachmentShouldReturnAttachment() throws FileNotFoundException {
public void whenGettingAttachmentShouldReturnAttachment() throws IOException {
FileInputStream file = new FileInputStream(ResourceUtils.getResource("DatabaseWithAttachments.kdbx"));

KeePassDatabase reader = KeePassDatabase.getInstance(file);
KeePassFile database = reader.openDatabase("abcdefg");

Entry entry = database.getEntryByTitle("Sample Entry");

Assert.assertEquals(1, entry.getAttachments().size());
Attachment attachment = entry.getAttachments().get(0);
Assert.assertEquals(0, attachment.getRef());
Assert.assertEquals("attachment.txt", attachment.getKey());
Assert.assertEquals("H4sIAAAAAAAAAwtJrSgBAPkIuZsEAAAA", DatatypeConverter.printBase64Binary(attachment.getData()));
Assert.assertEquals(2, entry.getAttachments().size());
List<Attachment> attachments = entry.getAttachments();

Attachment image = attachments.get(0);
assertThat(image.getKey(), is("0.png"));
assertThat(image.getRef(), is(0));

FileInputStream originalImage = new FileInputStream(ResourceUtils.getResource("0.png"));
byte[] originalByteArray = StreamUtils.toByteArray(originalImage);

assertThat(image.getData(), equalTo(originalByteArray));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import javax.xml.bind.DatatypeConverter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -13,6 +12,13 @@
import java.util.List;
import java.util.UUID;

import javax.xml.bind.DatatypeConverter;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import de.slackspace.openkeepass.KeePassDatabase;
import de.slackspace.openkeepass.crypto.Salsa20;
import de.slackspace.openkeepass.domain.Attachment;
Expand Down Expand Up @@ -45,10 +51,7 @@
import de.slackspace.openkeepass.util.ByteUtils;
import de.slackspace.openkeepass.util.CalendarHandler;
import de.slackspace.openkeepass.util.ResourceUtils;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import de.slackspace.openkeepass.util.StreamUtils;

public class KeepassDatabaseWriterTest {

Expand Down Expand Up @@ -218,13 +221,13 @@ public void shouldWriteDatabaseWithCustomIcon() throws IOException {

@Test
public void shouldWriteDatabaseWithAttachment() throws IOException {
String base64Attachment = "H4sIAAAAAAAAAwtJrSgBAPkIuZsEAAAA";
FileInputStream image = new FileInputStream(ResourceUtils.getResource("0.png"));
byte[] imageData = StreamUtils.toByteArray(image);

String attachmentKey = "text.txt";
String attachmentKey = "dummy.png";
int attachmentId = 0;
byte[] data = DatatypeConverter.parseBase64Binary(base64Attachment);

Binary binary = new BinaryBuilder().id(attachmentId).isCompressed(true).data(data).build();
Binary binary = new BinaryBuilder().id(attachmentId).isCompressed(true).data(imageData).build();
Binaries binaries = new BinariesBuilder().addBinary(binary).build();

Meta meta = new MetaBuilder("attachmentTest").binaries(binaries).build();
Expand All @@ -241,10 +244,11 @@ public void shouldWriteDatabaseWithAttachment() throws IOException {
KeePassFile readDb = KeePassDatabase.getInstance(dbFilename).openDatabase("abcdefg");
Assert.assertEquals("attachmentTest", readDb.getMeta().getDatabaseName());
List<Attachment> attachments = readDb.getEntryByTitle("1").getAttachments();
Assert.assertEquals(1, attachments.size());
Assert.assertEquals(attachmentId, attachments.get(0).getRef());
Assert.assertEquals(attachmentKey, attachments.get(0).getKey());
Assert.assertEquals(base64Attachment, DatatypeConverter.printBase64Binary(attachments.get(0).getData()));

assertThat(attachments.size(), is(1));
assertThat(attachments.get(0).getRef(), is(attachmentId));
assertThat(attachments.get(0).getKey(), is(attachmentKey));
assertThat(attachments.get(0).getData(), is(imageData));
}

@Test
Expand Down

0 comments on commit aeb10ae

Please sign in to comment.