From 00bd6a08b7fd9c2ecbc75d5886b3294b58e3778b Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Tue, 28 Apr 2015 18:36:07 +0200 Subject: [PATCH] Added SerializationTest for object payload (which is probably the most important) --- .../bitmessage/entity/ObjectMessage.java | 4 + .../bitmessage/entity/payload/V3Pubkey.java | 1 + .../ch/dissem/bitmessage/ports/Inventory.java | 2 +- .../bitmessage/entity/SerializationTest.java | 92 ++++++++++++++++++ domain/src/test/resources/V1Msg.payload | Bin 0 -> 1324 bytes domain/src/test/resources/V2GetPubkey.payload | Bin 0 -> 42 bytes domain/src/test/resources/V2Pubkey.payload | Bin 0 -> 154 bytes domain/src/test/resources/V3GetPubkey.payload | Bin 0 -> 42 bytes domain/src/test/resources/V3Pubkey.payload | Bin 0 -> 232 bytes domain/src/test/resources/V4Broadcast.payload | Bin 0 -> 1356 bytes domain/src/test/resources/V4GetPubkey.payload | Bin 0 -> 54 bytes domain/src/test/resources/V4Pubkey.payload | Bin 0 -> 396 bytes domain/src/test/resources/V5Broadcast.payload | Bin 0 -> 668 bytes .../bitmessage/inventory/JdbcHelper.java | 2 +- .../bitmessage/inventory/JdbcInventory.java | 4 +- .../bitmessage/inventory/SimpleInventory.java | 2 +- .../V1.1__Create_inventory_table.sql | 2 +- .../bitmessage/networking/Connection.java | 2 +- 18 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 domain/src/test/java/ch/dissem/bitmessage/entity/SerializationTest.java create mode 100644 domain/src/test/resources/V1Msg.payload create mode 100644 domain/src/test/resources/V2GetPubkey.payload create mode 100644 domain/src/test/resources/V2Pubkey.payload create mode 100644 domain/src/test/resources/V3GetPubkey.payload create mode 100644 domain/src/test/resources/V3Pubkey.payload create mode 100644 domain/src/test/resources/V4Broadcast.payload create mode 100644 domain/src/test/resources/V4GetPubkey.payload create mode 100644 domain/src/test/resources/V4Pubkey.payload create mode 100644 domain/src/test/resources/V5Broadcast.payload 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 0000000000000000000000000000000000000000..22da6c8dc2429276862e488590c4f5c759f18367 GIT binary patch literal 1324 zcmV+{1=IQf00005**ww!00010Pol;E000630id{h@GOM9KUK0%7!OwZbppx&ATd~h z{5~PK&3w-8PGgXf$wsLSTOFO05sC+F%^;_u%K#u>V9AswCu8!1*p%Kt8WD3ha7q`J z@=G^WeD_FEUQB3b$XJN53iLmZwLPEExwsFx34HHn%sA_y@w4{L4LXdK6D z#J6~)7|rv~R2Liotg@g&)YDWi`UBO5`k?^i$yWgdY}9>^>a#iH9StUR($SU5H_C^h zJXSI5gCP^%iZ6INyX3YGCe>mxL4HdYLZ~@ZB3--2qQN!^T8O&*C;pZeW;@_*@}sA% zpUukuV>8f-Q%dG{mmT2$<3>S^a;(phnQovh&5Xij(dR8D5nk2;Se(%{cru{^12&4Z zp|?%Y67c+q?IUO1R(1VFyy9Q`oCtqRnpWlHTRzj~)wYDmmubFeGtxfN%0q>~#-%bQQ#grv-zlv>{b9PjnI zXHJpYTSaGuu6segoM%Sife>m8v#?Lu2OrJlueyp7d-v|Nf=|by89&PT3bO9Ow5We4 zqg8P}MG0oc%kmRtl~rcPnu`pEu*h#vW@JoTWj)+KO{)8oGu+-`PW#pOY|(@|2I}~` zTsXdSNZV!#HAgLjd2iNb(*sIIL9bmGcHO8E`H5#k&>_5bqpRbFgHUQ256)Mg!;T-sZ=B#2`9A*sv%A=^ iaQ6{HefhIHKB`d^Z&NBVo}5s?88hnr>1C8>%GjysyM4s~ literal 0 HcmV?d00001 diff --git a/domain/src/test/resources/V2GetPubkey.payload b/domain/src/test/resources/V2GetPubkey.payload new file mode 100644 index 0000000000000000000000000000000000000000..1f14d0b26db9e6bf3e009d158d494f197e7faf2a GIT binary patch literal 42 scmZQz00Ni)lR;Fdqr`R)&BVySQ2v|2_kYuqOov4MiA-A_BpwUXVbpsOs*-(>Bk-RL>oGZI9%gzdb_OZ zx}V;qhYp6OPyIF)O1%iv?+P{LznF4k+y1jF9^BevbVqoNMajmm?mJeP#mgFO?wVYn z$XLFkl6TTU$;wBr=2J^#4nK^#R_Je{u2|^3$Vm3(flKQpcFmptYT{G|6EDYa>e)Ay FO#!@VKGpyL literal 0 HcmV?d00001 diff --git a/domain/src/test/resources/V3GetPubkey.payload b/domain/src/test/resources/V3GetPubkey.payload new file mode 100644 index 0000000000000000000000000000000000000000..a04466a97f02612fdc909a0c390899da3c712889 GIT binary patch literal 42 ucmZQzU|?Vr`=kM+7(xSD5yVyNaGx}V(QP?x%UCWx!3n68R`0T8VA~5e`^H* literal 0 HcmV?d00001 diff --git a/domain/src/test/resources/V4Broadcast.payload b/domain/src/test/resources/V4Broadcast.payload new file mode 100644 index 0000000000000000000000000000000000000000..2e42aa96dc03ba6d1cdc115af0af132543cca000 GIT binary patch literal 1356 zcmV-S1+)4900000Rgw7s00010Kw1_6000970aD3G&TE&3*VlpIklIgcbOOo%Anl~R zJh&z2ICRf8FgUk&bx8s)MN&hM)R$UNvA-^sTa`X{&oPHeq6^x`+0lrCYr=WZ_Avlb9ATz7H%3mpt)(~EWAu{)6a}K&3(Aza6x0;oYk64rGURn%wdSi^V zpll8Wu3}oJLognt6Rdo^-qyVcU~+ZgWB%!3m?@zqoII~l-1N89jJrALXQ}$#rH)ct z6N85&EZhW@NkD%FfYiRg9=oA)h8X+EK1@hSok%ZRCOa;Yx|@29LRBD(oZb|_Gxj@C zSTo=Mv;R8XsM;$bg>TFeGiSaP=PM(@922Et0Ih#En`JZSc>fgz%Md8gEWC7V7rf$S zH~$64FcZM!MpZRf;nxeKE9`j4;If!_>4=DC;;gBlAx5Oo*5-Sa5hC4azPWneqH)r? znOtUrgS23l`fVL$Wi}}>LGmEO4};NM?Cx&V-u!K=_%mD*bj8_`jCWR&8W^-nULyMx zm(&|6!IB{?ach}8f*V(rJ;luENxbSR)}0QQoJ9O@!%kEF4dTVzZ`pEP5&X`|WM8rZ z4Gj9~?dSKx8w3$}7*FaYpg*}S81xMaE9cLdAVQ+5#QMR_f^EQ1Nijn*Q|-@movHp6 zU7ayCWi4#y#S!+`H~+;MzW#Y(`lLINkd(fbCjxeW2;RoDKU9enQr6G}7e&;{m_AB}?+b6?5%ZRNPAWS@={;q%)l$a_oAufL_ss!KdVwJ(nW7z#H}GtY{0IJ$fL3OU3(1> zp8)$n@jG4S{3*q4$z!!(v-1+}{BH~0L<_e2vTvue6$A9KL>Di^FbhqUVhN*F5qq~9 zShnZzVS$Tu-5pgeb^Iz-&s1RqYTDIb!(b}6%X@PEy8xoB-ST7D%ik42Sie$<^1C0j ztc#n@o_YkoA}gPIOr84A1t8q?1+xFlheMv{hiP>j`S3XLlY6VNad~VBR z$5iSl72bKQvF2z)k=iv<6hq6Mlbp~u+%1tGpQoJi3$ycXDDiYQEy<}C2=$o3;I;nj zlvkvzdB9LdhZc!MzMaf)xUV)8D4c z)wsb8z%y8{dVw)rjpq7#H%YYT)fbKk8hAqJ1brKz}(hN+Uu~?^f!nYv% z#F^S|zC`U%UR?Mj3iVD^)+8FU5}FM1VIQ?obkrL4{mQ`Dk0tyY2{0+5>Uq-h!m38$ O@6{o1@%61UbGrNm^`40U literal 0 HcmV?d00001 diff --git a/domain/src/test/resources/V4GetPubkey.payload b/domain/src/test/resources/V4GetPubkey.payload new file mode 100644 index 0000000000000000000000000000000000000000..c17ce0eba3ad4cec86aa63206f826957fd2216fd GIT binary patch literal 54 zcmZQz00KweKOid9!9NE?voJd4wWS;JO+DtXwam9jA-u4HQz+p|S5%3q5zG+lUG@d+W#B?8I-AmE7M`4N#f_>l5sBGqyTKB8TGRakg9 z{PNyG)E0p#TL2(2Bc#SVza5G}4(Qc1boXpiS1KmQ0u>Y9m(kj~dz#2#IRo_+of09u zM}mU0(-->0q6&Wzu^mhK5O~d~?-}~qaS;1T@P^>uT->>%=hNd0q;kp3jfo z4Rr)2gw4(EA}u_w;Zs;DlTRM4@IEdI0j{@Htc6m|5sM7<_`ZPbu=B%aeH!OFZLGvp q0esO1m&p6{H$wn}ivBFpR<0sS)&!?lDlgF literal 0 HcmV?d00001 diff --git a/domain/src/test/resources/V5Broadcast.payload b/domain/src/test/resources/V5Broadcast.payload new file mode 100644 index 0000000000000000000000000000000000000000..8d508501bcb226b60893b8df85a3fdb45aa59d06 GIT binary patch literal 668 zcmV;N0%QFE00002;2zTe00010Mm1sp000980X-GPNShv2W7BT(qtBYfH~lKR5(a?g z5F9#I$Ncy9RFJD(@*)u)5Umu+fHg>D*P-6*;uCKmW;7vt4Ge*`Ru zP|1xX(Gf||GS`0m2;$owUsz!1jrHrxbhg21sHgU{lyv?qQybVs%=BsGUGL!IuIj)( zPgjRiHGuMwFl3e?O|$QOXqKBjUIlW}8j53%spiMEFlN5^UJ%DEs>>6ysx(+gPwU$4 zm(Bz@Z$Sw2OX%{AvEi??6!%$MSRKAF_Zf{ny@Tsry6TK#FfX1-O~vg5Bo2cX&Ln^5 zWU7OHlK5X9dk#4q3Xycmkp81Lr$!w&^X|_KGn7D=dAtHF^J^{gl(<{k_C#5XQVn2R zUYufw5_n(r!V?*4qwWQ*{XLBILHy2cbdZ22l`Qw)MQeOePT`T CXhh!t literal 0 HcmV?d00001 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); }