diff --git a/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java b/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java index e595bef..65c40ec 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java +++ b/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java @@ -76,6 +76,10 @@ public class ObjectMessage implements MessagePayload { return payload; } + public long getVersion() { + return version; + } + public long getStream() { return stream; } diff --git a/domain/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java b/domain/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java index 829b5e5..b30c25e 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java +++ b/domain/src/main/java/ch/dissem/bitmessage/entity/payload/V3Pubkey.java @@ -118,6 +118,7 @@ public class V3Pubkey extends V2Pubkey { } public V3Pubkey build() { + // TODO: check signature return new V3Pubkey(this); } } diff --git a/domain/src/main/java/ch/dissem/bitmessage/ports/Inventory.java b/domain/src/main/java/ch/dissem/bitmessage/ports/Inventory.java index 837e865..b0b1a86 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/ports/Inventory.java +++ b/domain/src/main/java/ch/dissem/bitmessage/ports/Inventory.java @@ -31,7 +31,7 @@ public interface Inventory { ObjectMessage getObject(InventoryVector vector); - void storeObject(int version, ObjectMessage object); + void storeObject(ObjectMessage object); void cleanup(); } diff --git a/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java b/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java new file mode 100644 index 0000000..a3163f2 --- /dev/null +++ b/domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java @@ -0,0 +1,92 @@ +/* + * 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.entity; + +import ch.dissem.bitmessage.entity.payload.*; +import ch.dissem.bitmessage.factory.Factory; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * Created by chris on 28.04.15. + */ +public class SerializationTest { + @Test + public void ensureGetPubkeyIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V2GetPubkey.payload", 2, GetPubkey.class); + doTest("V3GetPubkey.payload", 2, GetPubkey.class); + doTest("V4GetPubkey.payload", 2, GetPubkey.class); + } + + @Test + public void ensureV2PubkeyIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V2Pubkey.payload", 2, V2Pubkey.class); + } + + @Test + public void ensureV3PubkeyIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V3Pubkey.payload", 3, V3Pubkey.class); + } + + @Test + public void ensureV4PubkeyIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V4Pubkey.payload", 4, V4Pubkey.class); + } + + @Test + public void ensureV1MsgIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V1Msg.payload", 1, Msg.class); + } + @Test + public void ensureV4BroadcastIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V4Broadcast.payload", 4, V4Broadcast.class); + } + @Test + public void ensureV5BroadcastIsDeserializedAndSerializedCorrectly() throws IOException { + doTest("V5Broadcast.payload", 5, V5Broadcast.class); + } + + private void doTest(String resourceName, int version, Class expectedPayloadType) throws IOException { + byte[] data = getBytes(resourceName); + InputStream in = new ByteArrayInputStream(data); + ObjectMessage object = Factory.getObjectMessage(version, in, data.length); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + object.write(out); + assertArrayEquals(data, out.toByteArray()); + assertEquals(expectedPayloadType.getCanonicalName(), object.getPayload().getClass().getCanonicalName()); + } + + private byte[] getBytes(String resourceName) throws IOException { + InputStream in = getClass().getClassLoader().getResourceAsStream(resourceName); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int len = in.read(buffer); + while (len != -1) { + out.write(buffer, 0, len); + len = in.read(buffer); + } + return out.toByteArray(); + } +} diff --git a/domain/src/test/resources/V1Msg.payload b/domain/src/test/resources/V1Msg.payload new file mode 100644 index 0000000..22da6c8 Binary files /dev/null and b/domain/src/test/resources/V1Msg.payload differ diff --git a/domain/src/test/resources/V2GetPubkey.payload b/domain/src/test/resources/V2GetPubkey.payload new file mode 100644 index 0000000..1f14d0b Binary files /dev/null and b/domain/src/test/resources/V2GetPubkey.payload differ diff --git a/domain/src/test/resources/V2Pubkey.payload b/domain/src/test/resources/V2Pubkey.payload new file mode 100644 index 0000000..2cb7203 Binary files /dev/null and b/domain/src/test/resources/V2Pubkey.payload differ diff --git a/domain/src/test/resources/V3GetPubkey.payload b/domain/src/test/resources/V3GetPubkey.payload new file mode 100644 index 0000000..a04466a Binary files /dev/null and b/domain/src/test/resources/V3GetPubkey.payload differ diff --git a/domain/src/test/resources/V3Pubkey.payload b/domain/src/test/resources/V3Pubkey.payload new file mode 100644 index 0000000..0f47400 Binary files /dev/null and b/domain/src/test/resources/V3Pubkey.payload differ diff --git a/domain/src/test/resources/V4Broadcast.payload b/domain/src/test/resources/V4Broadcast.payload new file mode 100644 index 0000000..2e42aa9 Binary files /dev/null and b/domain/src/test/resources/V4Broadcast.payload differ diff --git a/domain/src/test/resources/V4GetPubkey.payload b/domain/src/test/resources/V4GetPubkey.payload new file mode 100644 index 0000000..c17ce0e Binary files /dev/null and b/domain/src/test/resources/V4GetPubkey.payload differ diff --git a/domain/src/test/resources/V4Pubkey.payload b/domain/src/test/resources/V4Pubkey.payload new file mode 100644 index 0000000..b9b1c59 Binary files /dev/null and b/domain/src/test/resources/V4Pubkey.payload differ diff --git a/domain/src/test/resources/V5Broadcast.payload b/domain/src/test/resources/V5Broadcast.payload new file mode 100644 index 0000000..8d50850 Binary files /dev/null and b/domain/src/test/resources/V5Broadcast.payload differ diff --git a/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcHelper.java b/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcHelper.java index 2403302..624e180 100644 --- a/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcHelper.java +++ b/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcHelper.java @@ -42,7 +42,7 @@ abstract class JdbcHelper { static { Flyway flyway = new Flyway(); - flyway.setDataSource(DB_URL, DB_USER, null); + flyway.setDataSource(DB_URL, DB_USER, DB_PWD); flyway.migrate(); } diff --git a/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcInventory.java b/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcInventory.java index 02db760..c0bcb18 100644 --- a/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcInventory.java +++ b/inventory/src/main/java/ch/dissem/bitmessage/inventory/JdbcInventory.java @@ -72,7 +72,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory { } @Override - public void storeObject(int version, ObjectMessage object) { + public void storeObject(ObjectMessage object) { try { PreparedStatement ps = getConnection().prepareStatement("INSERT INTO Inventory (hash, stream, expires, data, type, version) VALUES (?, ?, ?, ?, ?, ?)"); InventoryVector iv = object.getInventoryVector(); @@ -82,7 +82,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory { ps.setLong(3, object.getExpiresTime()); writeBlob(ps, 4, object); ps.setLong(5, object.getType()); - ps.setInt(6, version); + ps.setLong(6, object.getVersion()); ps.executeUpdate(); } catch (SQLException e) { LOG.error("Error storing object of type " + object.getPayload().getClass().getSimpleName(), e); diff --git a/inventory/src/main/java/ch/dissem/bitmessage/inventory/SimpleInventory.java b/inventory/src/main/java/ch/dissem/bitmessage/inventory/SimpleInventory.java index d0efec7..2574d68 100644 --- a/inventory/src/main/java/ch/dissem/bitmessage/inventory/SimpleInventory.java +++ b/inventory/src/main/java/ch/dissem/bitmessage/inventory/SimpleInventory.java @@ -44,7 +44,7 @@ public class SimpleInventory implements Inventory { } @Override - public void storeObject(int version, ObjectMessage object) { + public void storeObject(ObjectMessage object) { throw new NotImplementedException(); } diff --git a/inventory/src/main/resources/db/migration/V1.1__Create_inventory_table.sql b/inventory/src/main/resources/db/migration/V1.1__Create_inventory_table.sql index 5f3855f..83653bf 100644 --- a/inventory/src/main/resources/db/migration/V1.1__Create_inventory_table.sql +++ b/inventory/src/main/resources/db/migration/V1.1__Create_inventory_table.sql @@ -4,5 +4,5 @@ CREATE TABLE Inventory ( expires BIGINT NOT NULL, data BLOB NOT NULL, type BIGINT NOT NULL, - version INT NOT NULL + version BIGINT NOT NULL ); \ No newline at end of file diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index e6cada4..443e356 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -151,7 +151,7 @@ public class Connection implements Runnable { try { LOG.debug("Received object " + objectMessage.getInventoryVector()); Security.checkProofOfWork(objectMessage, ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes()); - ctx.getInventory().storeObject(version, objectMessage); + ctx.getInventory().storeObject(objectMessage); } catch (IOException e) { LOG.debug(e.getMessage(), e); }