Moving "Security" to a separate port, so there can be a Bouncycastle and a Spongycastle implementation. (BC doesn't work on Android, SC can't be used on Oracle's JVM)

This commit is contained in:
2015-08-05 19:52:18 +02:00
parent 6542bd1451
commit b8546e28af
60 changed files with 1168 additions and 641 deletions

View File

@ -12,7 +12,9 @@ uploadArchives {
dependencies {
compile project(':domain')
compile 'com.h2database:h2:1.4.187'
compile 'org.flywaydb:flyway-core:3.2.1'
testCompile 'junit:junit:4.11'
testCompile 'com.h2database:h2:1.4.187'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile project(':security-bc')
}

View File

@ -28,9 +28,9 @@ import java.sql.SQLException;
*/
public class JdbcConfig {
protected final Flyway flyway;
private final String dbUrl;
private final String dbUser;
private final String dbPassword;
protected final String dbUrl;
protected final String dbUser;
protected final String dbPassword;
public JdbcConfig(String dbUrl, String dbUser, String dbPassword) {
this.dbUrl = dbUrl;

View File

@ -18,14 +18,15 @@ package ch.dissem.bitmessage.repository;
import ch.dissem.bitmessage.entity.Streamable;
import ch.dissem.bitmessage.entity.payload.ObjectType;
import org.flywaydb.core.Flyway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.sql.*;
import java.sql.Blob;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import static ch.dissem.bitmessage.utils.Strings.hex;
@ -81,8 +82,8 @@ abstract class JdbcHelper {
if (data != null) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
data.write(os);
ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
ps.setBlob(parameterIndex, is);
byte[] bytes = os.toByteArray();
ps.setBinaryStream(parameterIndex, new ByteArrayInputStream(bytes), bytes.length);
} else {
ps.setBlob(parameterIndex, (Blob) null);
}

View File

@ -25,7 +25,7 @@ import java.util.List;
import static org.junit.Assert.*;
public class JdbcAddressRepositoryTest {
public class JdbcAddressRepositoryTest extends TestBase {
public static final String CONTACT_A = "BM-2cW7cD5cDQJDNkE7ibmyTxfvGAmnPqa9Vt";
public static final String CONTACT_B = "BM-2cTtkBnb4BUYDndTKun6D9PjtueP2h1bQj";
public static final String CONTACT_C = "BM-2cV5f9EpzaYARxtoruSpa6pDoucSf9ZNke";

View File

@ -34,7 +34,7 @@ import static ch.dissem.bitmessage.utils.UnixTime.DAY;
import static ch.dissem.bitmessage.utils.UnixTime.now;
import static org.junit.Assert.*;
public class JdbcInventoryTest {
public class JdbcInventoryTest extends TestBase {
private TestJdbcConfig config;
private Inventory inventory;

View File

@ -25,7 +25,6 @@ import ch.dissem.bitmessage.entity.valueobject.Label;
import ch.dissem.bitmessage.entity.valueobject.PrivateKey;
import ch.dissem.bitmessage.ports.AddressRepository;
import ch.dissem.bitmessage.ports.MessageRepository;
import ch.dissem.bitmessage.utils.Security;
import org.junit.Before;
import org.junit.Test;
@ -33,10 +32,11 @@ import java.util.Arrays;
import java.util.List;
import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG;
import static ch.dissem.bitmessage.utils.Singleton.security;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
public class JdbcMessageRepositoryTest {
public class JdbcMessageRepositoryTest extends TestBase {
private BitmessageAddress contactA;
private BitmessageAddress contactB;
private BitmessageAddress identity;
@ -54,7 +54,11 @@ public class JdbcMessageRepositoryTest {
config.reset();
addressRepo = new JdbcAddressRepository(config);
repo = new JdbcMessageRepository(config);
new InternalContext(new BitmessageContext.Builder().addressRepo(addressRepo).messageRepo(repo));
new InternalContext(new BitmessageContext.Builder()
.security(security())
.addressRepo(addressRepo)
.messageRepo(repo)
);
BitmessageAddress tmp = new BitmessageAddress(new PrivateKey(false, 1, 1000, 1000));
contactA = new BitmessageAddress(tmp.getAddress());
@ -120,7 +124,7 @@ public class JdbcMessageRepositoryTest {
@Test
public void testSave() throws Exception {
Plaintext message = new Plaintext.Builder(MSG)
.IV(new InventoryVector(Security.randomBytes(32)))
.IV(new InventoryVector(security().randomBytes(32)))
.from(identity)
.to(contactA)
.message("Subject", "Message")
@ -143,7 +147,7 @@ public class JdbcMessageRepositoryTest {
public void testUpdate() throws Exception {
List<Plaintext> messages = repo.findMessages(Plaintext.Status.DRAFT, contactA);
Plaintext message = messages.get(0);
message.setInventoryVector(new InventoryVector(Security.randomBytes(32)));
message.setInventoryVector(new InventoryVector(security().randomBytes(32)));
repo.save(message);
messages = repo.findMessages(Plaintext.Status.DRAFT, contactA);

View File

@ -27,7 +27,7 @@ import java.util.List;
import static ch.dissem.bitmessage.utils.UnixTime.now;
import static org.junit.Assert.assertEquals;
public class JdbcNodeRegistryTest {
public class JdbcNodeRegistryTest extends TestBase {
private TestJdbcConfig config;
private NodeRegistry registry;

View File

@ -0,0 +1,38 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.repository;
import ch.dissem.bitmessage.InternalContext;
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
import ch.dissem.bitmessage.security.bc.BouncySecurity;
import ch.dissem.bitmessage.utils.Singleton;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Created by chris on 20.07.15.
*/
public class TestBase {
static {
BouncySecurity security = new BouncySecurity();
Singleton.initialize(security);
InternalContext ctx = mock(InternalContext.class);
when(ctx.getProofOfWorkEngine()).thenReturn(new MultiThreadedPOWEngine());
security.setContext(ctx);
}
}

View File

@ -17,7 +17,8 @@
package ch.dissem.bitmessage.repository;
/**
* Created by chris on 02.06.15.
* JdbcConfig to be used for tests. Uses an in-memory database and adds a useful {@link #reset()} method resetting
* the database.
*/
public class TestJdbcConfig extends JdbcConfig {
public TestJdbcConfig() {