Skip to content

Commit

Permalink
Merge branch 'feature-entry-colors'
Browse files Browse the repository at this point in the history
  • Loading branch information
cternes committed Oct 10, 2017
2 parents 4fc003a + 6da1c52 commit 22b6212
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 13 deletions.
19 changes: 18 additions & 1 deletion src/main/java/de/slackspace/openkeepass/domain/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
*
*/
@Root(strict = false, name = "Entry")
@Order(elements = {"UUID", "IconID", "CustomIconUUID", "Tags", "String", "Times", "History"})
@Order(elements = {"UUID", "IconID", "CustomIconUUID", "ForegroundColor",
"BackgroundColor", "Tags", "String", "Times", "History"})
public class Entry implements KeePassFileElement {

private static final String USER_NAME = "UserName";
Expand Down Expand Up @@ -57,6 +58,12 @@ public class Entry implements KeePassFileElement {
@Element(name = "Tags", required = false)
private String tags;

@Element(name = "ForegroundColor", required = false)
private String foregroundColor;

@Element(name = "BackgroundColor", required = false)
private String backgroundColor;

private TagParser tagParser = new TagParser();

Entry() {
Expand All @@ -71,6 +78,8 @@ public Entry(EntryContract entryContract) {
this.customIconUUID = entryContract.getCustomIconUUID();
this.times = entryContract.getTimes();
this.tags = tagParser.toTagString(entryContract.getTags());
this.foregroundColor = entryContract.getForegroundColor();
this.backgroundColor = entryContract.getBackgroundColor();

setValue(false, NOTES, entryContract.getNotes());
setValue(true, PASSWORD, entryContract.getPassword());
Expand Down Expand Up @@ -209,6 +218,14 @@ public List<String> getTags() {
return null;
}

public String getForegroundColor() {
return foregroundColor;
}

public String getBackgroundColor() {
return backgroundColor;
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/de/slackspace/openkeepass/domain/EntryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public class EntryBuilder implements EntryContract {

private List<String> tags = new ArrayList<String>();

private String foregroundColor;

private String backgroundColor;

private List<Property> customPropertyList = new ArrayList<Property>();

/**
Expand Down Expand Up @@ -87,6 +91,8 @@ public EntryBuilder(Entry entry) {
this.customPropertyList.addAll(entry.getCustomProperties());
this.times = entry.getTimes();
this.tags = entry.getTags();
this.foregroundColor = entry.getForegroundColor();
this.backgroundColor = entry.getBackgroundColor();
}

public EntryBuilder title(String title) {
Expand Down Expand Up @@ -159,6 +165,16 @@ public EntryBuilder addTag(String tag) {
return this;
}

public EntryBuilder foregroundColor(String foregroundColor) {
this.foregroundColor = foregroundColor;
return this;
}

public EntryBuilder backgroundColor(String backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}

/**
* Builds a new entry with the values from the builder.
*
Expand Down Expand Up @@ -251,4 +267,14 @@ public Times getTimes() {
public List<String> getTags() {
return tags;
}

@Override
public String getForegroundColor() {
return foregroundColor;
}

@Override
public String getBackgroundColor() {
return backgroundColor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public interface EntryContract {
Times getTimes();

List<String> getTags();

String getForegroundColor();

String getBackgroundColor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,17 @@ public void whenGettingTagsShouldReturnTags() throws FileNotFoundException {

assertThat(tags, hasItems("tag1", "tag2", "tag3"));
}

@Test
public void whenGettingColorsShouldReturnColors() throws FileNotFoundException {
FileInputStream file = new FileInputStream(ResourceUtils.getResource("DatabaseWithColors.kdbx"));

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

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

Assert.assertEquals("#0080FF", entry.getForegroundColor());
Assert.assertEquals("#FF0000", entry.getBackgroundColor());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 @@ -12,13 +13,6 @@
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.CompressionAlgorithm;
Expand Down Expand Up @@ -46,6 +40,10 @@
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;

public class KeepassDatabaseWriterTest {

Expand Down Expand Up @@ -268,4 +266,26 @@ public void shouldWriteDatabaseWithTags() throws IOException {
assertThat(tags, hasItems("x", "y"));
}

@Test
public void shouldWriteDatabaseWithColors() throws IOException {
// build database
Meta meta = new MetaBuilder("colorTest").build();
Entry entry1 = new EntryBuilder("1").foregroundColor("#FFFFFF").backgroundColor("#000000").build();
Group groupA = new GroupBuilder("A").addEntry(entry1).build();

KeePassFile keePassFile = new KeePassFileBuilder(meta).addTopGroups(groupA).build();

// write
String dbFilename = tempFolder.newFile("dbWithColors.kdbx").getPath();
KeePassDatabase.write(keePassFile, "abcdefg", dbFilename);

// read and assert
KeePassFile readDb = KeePassDatabase.getInstance(dbFilename).openDatabase("abcdefg");
assertThat(readDb.getMeta().getDatabaseName(), is("colorTest"));

Entry entry = readDb.getEntryByTitle("1");
Assert.assertEquals("#FFFFFF", entry.getForegroundColor());
Assert.assertEquals("#000000", entry.getBackgroundColor());
}

}
10 changes: 8 additions & 2 deletions src/test/java/de/slackspace/openkeepass/domain/EntryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ public void shouldMarshallObjectToXml() throws Exception {
.times(times)
.addTag("one")
.addTag("two")
.foregroundColor("#FFFFFF")
.backgroundColor("#000000")
.build();

ByteArrayOutputStream bos = new SimpleXmlParser().toXml(entry);

String xml = XmlStringCleaner.cleanXmlString(new String(bos.toByteArray()));
Assert.assertEquals(
"<Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><Tags>one;two</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times></Entry>",
"<Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><ForegroundColor>#FFFFFF</ForegroundColor><BackgroundColor>#000000</BackgroundColor><Tags>one;two</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times></Entry>",
xml);
}

Expand Down Expand Up @@ -84,10 +86,12 @@ public void shouldUnmarshallXmlToObject() throws Exception {
.times(times)
.addTag("one")
.addTag("two")
.foregroundColor("#FFFFFF")
.backgroundColor("#000000")
.build();

String xml =
"<Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><Tags>one;two</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times></Entry>";
"<Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><ForegroundColor>#FFFFFF</ForegroundColor><BackgroundColor>#000000</BackgroundColor><Tags>one;two</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times></Entry>";
ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
Entry entryUnmarshalled = new SimpleXmlParser().fromXml(inputStream, Entry.class);

Expand All @@ -102,5 +106,7 @@ public void shouldUnmarshallXmlToObject() throws Exception {
Assert.assertEquals(entry.getTags().size(), entryUnmarshalled.getTags().size());
Assert.assertEquals(entry.getTags().get(0), entryUnmarshalled.getTags().get(0));
Assert.assertEquals(entry.getTags().get(1), entryUnmarshalled.getTags().get(1));
Assert.assertEquals(entry.getForegroundColor(), entryUnmarshalled.getForegroundColor());
Assert.assertEquals(entry.getBackgroundColor(), entryUnmarshalled.getBackgroundColor());
}
}
8 changes: 6 additions & 2 deletions src/test/java/de/slackspace/openkeepass/domain/GroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public void shouldMarshallObjectToXml() throws Exception {
.iconId(23)
.iconData(new byte[1])
.addTag("tag")
.foregroundColor("#FFFFFF")
.backgroundColor("#000000")
.build();

Group group = new GroupBuilder(UUID.fromString("87d4f441-a5ec-4ce0-8ca9-82a5079d28ef"))
Expand All @@ -62,7 +64,7 @@ public void shouldMarshallObjectToXml() throws Exception {

String xml = XmlStringCleaner.cleanXmlString(new String(bos.toByteArray()));
Assert.assertEquals(
"<Group><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><Name>Test</Name><IconID>49</IconID><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times><IsExpanded>False</IsExpanded><Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><Tags>tag</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String></Entry></Group>",
"<Group><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><Name>Test</Name><IconID>49</IconID><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times><IsExpanded>False</IsExpanded><Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><ForegroundColor>#FFFFFF</ForegroundColor><BackgroundColor>#000000</BackgroundColor><Tags>tag</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String></Entry></Group>",
xml);
}

Expand All @@ -88,6 +90,8 @@ public void shouldUnmarshallXmlToObject() throws Exception {
.iconId(23)
.iconData(new byte[1])
.addTag("tag")
.foregroundColor("#FFFFFF")
.backgroundColor("#000000")
.build();

Group group = new GroupBuilder(UUID.fromString("87d4f441-a5ec-4ce0-8ca9-82a5079d28ef"))
Expand All @@ -96,7 +100,7 @@ public void shouldUnmarshallXmlToObject() throws Exception {
.build();

String xml =
"<Group><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><Name>Test</Name><IconID>49</IconID><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times><IsExpanded>False</IsExpanded><Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><Tags>tag</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String></Entry></Group>";
"<Group><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><Name>Test</Name><IconID>49</IconID><Times><LastModificationTime>2016-01-18T00:00:00</LastModificationTime><CreationTime>2016-01-15T00:00:00</CreationTime><LastAccessTime>2016-01-17T00:00:00</LastAccessTime><ExpiryTime>2016-01-16T00:00:00</ExpiryTime><Expires>True</Expires><UsageCount>23</UsageCount><LocationChanged>2016-01-19T00:00:00</LocationChanged></Times><IsExpanded>False</IsExpanded><Entry><UUID>h9T0QaXsTOCMqYKlB50o7w==</UUID><IconID>23</IconID><CustomIconUUID>h9T0QaXsTOCMqYKlB50o7w==</CustomIconUUID><ForegroundColor>#FFFFFF</ForegroundColor><BackgroundColor>#000000</BackgroundColor><Tags>tag</Tags><String><Key>Notes</Key><Value Protected='False'>MyNote</Value></String><String><Key>Password</Key><Value Protected='True'>MyPasswd</Value></String><String><Key>Title</Key><Value Protected='False'>SomeTitle</Value></String><String><Key>UserName</Key><Value Protected='False'>MyUser</Value></String><String><Key>URL</Key><Value Protected='False'>http://test.com</Value></String></Entry></Group>";
ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
Group groupUnmarshalled = new SimpleXmlParser().fromXml(inputStream, Group.class);

Expand Down
Loading

0 comments on commit 22b6212

Please sign in to comment.