Skip to content

Commit

Permalink
Fixes for #70, Refactor for #71
Browse files Browse the repository at this point in the history
  • Loading branch information
jorabin committed Nov 22, 2024
1 parent 0f12b81 commit 0fddc53
Show file tree
Hide file tree
Showing 82 changed files with 1,016 additions and 906 deletions.
11 changes: 9 additions & 2 deletions database/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

<dependencies>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>util</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -25,5 +26,11 @@
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk18on</artifactId>
</dependency>
<!-- pull in junit as a compile time dependency, but don't deploy as run time -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.linguafranca.util;
package org.linguafranca.pwdb.util;

import org.linguafranca.pwdb.PropertyValue;

Expand Down Expand Up @@ -29,7 +29,7 @@ public static void genericTest(PropertyValue.Factory<?> factory, boolean testStr


// test input byte[]
testValue = factory.of(THIS_IS_A_SECRET.getBytes());
testValue = factory.of(THIS_IS_A_SECRET.getBytes(StandardCharsets.UTF_8));
test(testStringReturn, testValue);

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.linguafranca.pwdb;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

import static org.junit.Assert.*;
import static org.linguafranca.util.PropertyValueUtil.genericTest;
import static org.linguafranca.pwdb.util.PropertyValueUtil.genericTest;

public class PropertyValueTest {

public static final String ANOTHER_SECRET = "password with accents àéç";
Logger logger = LoggerFactory.getLogger(PropertyValueTest.class);

@Test
public void bytesTest() {
genericTest(PropertyValue.BytesStore.getFactory());}
genericTest(PropertyValue.BytesStore.getFactory());
}

@Test
public void sealedObjectTest() {
genericTest(PropertyValue.SealedStore.getFactory());
}

@Test
public void stringTest() {
genericTest(PropertyValue.StringStore.getFactory());
Expand All @@ -32,6 +31,5 @@ public void sealedScriptTest(){
PropertyValue.SealedStore sealed = PropertyValue.SealedStore.getFactory().of(ANOTHER_SECRET);
byte[] bytes = sealed.getValueAsBytes();
assertArrayEquals(ANOTHER_SECRET.getBytes(StandardCharsets.UTF_8), bytes);

}
}
19 changes: 14 additions & 5 deletions example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,25 @@
<artifactId>KeePassJava2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.github.novacrypto</groupId>
<artifactId>SecureString</artifactId>
<version>2022.01.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>
<dependency>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>
</dependencies>


Expand Down
14 changes: 3 additions & 11 deletions example/src/main/java/org/linguafranca/pwdb/kdbx/QuickStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.linguafranca.pwdb.kdbx;

import org.junit.BeforeClass;
import org.linguafranca.pwdb.*;
import org.linguafranca.pwdb.format.KdbxCreds;
import org.linguafranca.pwdb.format.KdbxHeader;
Expand All @@ -27,11 +26,8 @@
import org.linguafranca.pwdb.security.Encryption;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.linguafranca.util.TestUtil.getTestPrintStream;

/**
Expand All @@ -42,17 +38,13 @@
@SuppressWarnings("WeakerAccess")
public abstract class QuickStart {

public static final String TEST_OUTPUT_DIR = "testOutput";
static PrintStream printStream = getTestPrintStream();


public abstract Database getDatabase();
public abstract Database loadDatabase(Credentials creds, InputStream inputStream);

@BeforeClass
public static void ensureOutputDir() throws IOException {
Files.createDirectories(Paths.get("testOutput"));
}

/**
* Load KDBX
*/
Expand Down Expand Up @@ -90,7 +82,7 @@ public void saveKdbx() throws IOException {
}

// save to a file with password "123"
try (FileOutputStream outputStream = new FileOutputStream("testOutput/test.kdbx")) {
try (FileOutputStream outputStream = new FileOutputStream(TEST_OUTPUT_DIR + "/test.kdbx")) {
database.save(new KdbxCreds("123".getBytes()), outputStream);
}
}
Expand Down Expand Up @@ -138,7 +130,7 @@ public void loadKdb() throws IOException {
// deep copy from group (not including source group, KDB database has simulated root)
kdbxDatabase.getRootGroup().copy(database.getRootGroup());
// save it
try (FileOutputStream f = new FileOutputStream("testOutput/migration.kdbx")) {
try (FileOutputStream f = new FileOutputStream(TEST_OUTPUT_DIR + "/migration.kdbx")) {
kdbxDatabase.save(new KdbxCreds("123".getBytes()), f);
}
}
Expand Down
1 change: 1 addition & 0 deletions example/src/main/java/org/linguafranca/pwdb/kdbx/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static void listDatabase(String resourceName, Credentials creds, OutputSt
public static void listDatabase(DatabaseLoader loader, String resourceName, Credentials creds, OutputStream outputStream) throws IOException {
Database database = loader.load(creds, Util.class.getClassLoader().getResourceAsStream(resourceName));
database.save(new StreamFormat.None(), new KdbxCreds.None(), outputStream);
outputStream.flush();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import org.junit.Test;

import static org.junit.Assert.*;
import static org.linguafranca.util.PropertyValueUtil.genericTest;
import static org.linguafranca.pwdb.util.PropertyValueUtil.genericTest;

public class SecureCharBufferPropertyValueTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.linguafranca.pwdb.kdbx;

import org.junit.BeforeClass;
import org.junit.Test;
import org.linguafranca.pwdb.Credentials;
import org.linguafranca.pwdb.format.KdbxCreds;
Expand Down Expand Up @@ -53,6 +54,11 @@ public KdbxDatabase loadDatabase(Credentials credentials, InputStream inputStrea
}
}

@BeforeClass
public static void ensureOutputDir() throws IOException {
Files.createDirectories(Paths.get(TEST_OUTPUT_DIR));
}

@Test
public void saveTest() throws IOException {
super.saveKdbx();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ public class UtilTest {
@Test
public void listXmlTest() throws IOException {
PrintWriter writer = new PrintWriter(outputStream);
listXml("V3-CustomIcon.kdbx", new KdbxCreds("123".getBytes()), writer);
listXml("Database-4.1-123.kdbx", new KdbxCreds("123".getBytes()), writer);
writer.flush();
}

@Test
public void listXmlTest2() throws IOException {
PrintWriter writer = new PrintWriter(outputStream);
listXml("issue-70/test2.kdbx", new KdbxCreds("KeePassJava2".getBytes()), writer);
writer.flush();
}
/**
* List Database Encryption Characteristics
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.linguafranca.pwdb.kdbx.validation;

import org.junit.Test;
import org.linguafranca.pwdb.format.KdbxCreds;
import org.linguafranca.pwdb.kdbx.jackson.KdbxDatabase;

import java.io.IOException;
import java.io.InputStream;

/**
* Review Issue-70 https://github.com/jorabin/KeePassJava2/issues/70
*/
public class Issue70Test {

public static final String TEST_RESOURCE1 = "issue-70/test2.kdbx";
public static final KdbxCreds CREDENTIALS1 = new KdbxCreds("KeePassJava2".getBytes());
public static final String TEST_RESOURCE2 = "Database-4.1-123.kdbx";
public static final KdbxCreds CREDENTIALS2 = new KdbxCreds("123".getBytes());

@Test
public void testCustomData() throws IOException {
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(TEST_RESOURCE1)) {
KdbxDatabase database = KdbxDatabase.load(CREDENTIALS1, inputStream);
}
}

@Test
public void testFileFormat_4_1() throws IOException {
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(TEST_RESOURCE2)){
KdbxDatabase database = KdbxDatabase.load(CREDENTIALS2, inputStream);
}
}
}
5 changes: 1 addition & 4 deletions kdb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
<artifactId>database</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
11 changes: 6 additions & 5 deletions kdbx-database/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ public static KdbxEntry createEntry(KdbxDatabase database) {

@JacksonXmlProperty(localName = "OverrideURL")
protected String overrideURL;


@JacksonXmlProperty(localName = "PreviousParentGroup")
@JsonDeserialize(converter = Base64ToUUIDConverter.class)
@JsonSerialize(converter = UUIDToBase64Converter.class)
protected UUID previousParentGroup;

@JacksonXmlProperty(localName = "Tags")
protected String tags;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public class KdbxGroup extends AbstractGroup{
@JsonSerialize(converter = UUIDToBase64Converter.class)
protected UUID lastTopVisibleEntry;

@JacksonXmlProperty(localName = "PreviousParentGroup")
@JsonDeserialize(converter = Base64ToUUIDConverter.class)
@JsonSerialize(converter = UUIDToBase64Converter.class)
protected UUID previousParentGroup;

@JacksonXmlProperty(localName = "Tags")
protected String tags;

@JacksonXmlProperty(localName = "Entry") /* Workaround jackson */
@JacksonXmlElementWrapper(useWrapping = false)
protected List<KdbxEntry> entries;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,17 +339,30 @@ public String getName() {

public static class CustomData {

public CustomData() {
public static class CustomDataItem {
@JacksonXmlProperty(localName = "Key")
public String key;
@JacksonXmlProperty(localName = "Value")
public String value;
@JacksonXmlProperty(localName = "LastModificationTime")
@JsonDeserialize(converter = StringToDateConverter.class)
@JsonSerialize(converter = DateToStringConverter.class)
public Date lastModificationTime;
}

public List<Object> getAny() {
return any;
@JacksonXmlProperty(localName = "Item")
@JacksonXmlElementWrapper(useWrapping = false)
protected List<CustomDataItem> items;

public CustomData() {
}

public void setAny(List<Object> any) {
this.any = any;
public List<CustomDataItem> getItems() {
return items;
}

protected List<Object> any;
public void setItems(List<CustomDataItem> items) {
this.items = items;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.linguafranca.pwdb;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand Down
10 changes: 1 addition & 9 deletions kdbx-io/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,11 @@
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>org.linguafranca.pwdb</groupId>
<artifactId>test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Loading

0 comments on commit 0fddc53

Please sign in to comment.