Added SerializationTest for object payload (which is probably the most important)
This commit is contained in:
parent
407c432f3c
commit
00bd6a08b7
@ -76,6 +76,10 @@ public class ObjectMessage implements MessagePayload {
|
||||
return payload;
|
||||
}
|
||||
|
||||
public long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public long getStream() {
|
||||
return stream;
|
||||
}
|
||||
|
@ -118,6 +118,7 @@ public class V3Pubkey extends V2Pubkey {
|
||||
}
|
||||
|
||||
public V3Pubkey build() {
|
||||
// TODO: check signature
|
||||
return new V3Pubkey(this);
|
||||
}
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public interface Inventory {
|
||||
|
||||
ObjectMessage getObject(InventoryVector vector);
|
||||
|
||||
void storeObject(int version, ObjectMessage object);
|
||||
void storeObject(ObjectMessage object);
|
||||
|
||||
void cleanup();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
BIN
domain/src/test/resources/V1Msg.payload
Normal file
BIN
domain/src/test/resources/V1Msg.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V2GetPubkey.payload
Normal file
BIN
domain/src/test/resources/V2GetPubkey.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V2Pubkey.payload
Normal file
BIN
domain/src/test/resources/V2Pubkey.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V3GetPubkey.payload
Normal file
BIN
domain/src/test/resources/V3GetPubkey.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V3Pubkey.payload
Normal file
BIN
domain/src/test/resources/V3Pubkey.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V4Broadcast.payload
Normal file
BIN
domain/src/test/resources/V4Broadcast.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V4GetPubkey.payload
Normal file
BIN
domain/src/test/resources/V4GetPubkey.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V4Pubkey.payload
Normal file
BIN
domain/src/test/resources/V4Pubkey.payload
Normal file
Binary file not shown.
BIN
domain/src/test/resources/V5Broadcast.payload
Normal file
BIN
domain/src/test/resources/V5Broadcast.payload
Normal file
Binary file not shown.
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
);
|
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user