From a9fa671988dc38b2010ec047ee27516d992444cc Mon Sep 17 00:00:00 2001 From: JeremyRand Date: Wed, 29 Jun 2016 23:55:56 +0000 Subject: [PATCH] Added tests for parsing name scripts. --- .../org/libdohj/script/NameScriptTest.java | 355 ++++++++++++++++++ .../namecoin_name_firstupdate_d_bitcoin.bin | Bin 0 -> 476 bytes .../script/namecoin_name_new_d_bitcoin.bin | Bin 0 -> 257 bytes .../script/namecoin_name_update_d_bitcoin.bin | Bin 0 -> 635 bytes .../org/libdohj/script/namecoin_p2pkh.bin | Bin 0 -> 193 bytes 5 files changed, 355 insertions(+) create mode 100644 core/src/test/java/org/libdohj/script/NameScriptTest.java create mode 100644 core/src/test/resources/org/libdohj/script/namecoin_name_firstupdate_d_bitcoin.bin create mode 100644 core/src/test/resources/org/libdohj/script/namecoin_name_new_d_bitcoin.bin create mode 100644 core/src/test/resources/org/libdohj/script/namecoin_name_update_d_bitcoin.bin create mode 100644 core/src/test/resources/org/libdohj/script/namecoin_p2pkh.bin diff --git a/core/src/test/java/org/libdohj/script/NameScriptTest.java b/core/src/test/java/org/libdohj/script/NameScriptTest.java new file mode 100644 index 00000000..1b216e95 --- /dev/null +++ b/core/src/test/java/org/libdohj/script/NameScriptTest.java @@ -0,0 +1,355 @@ +/* + * Copyright 2016 Jeremy Rand + * + * 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 org.libdohj.script; + +import org.libdohj.params.AbstractNamecoinParams; +import org.libdohj.params.NamecoinMainNetParams; + +import org.bitcoinj.core.Context; +import org.bitcoinj.core.Transaction; +import org.bitcoinj.core.TransactionOutput; +import org.bitcoinj.core.Util; +import org.bitcoinj.script.Script; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import org.junit.rules.ExpectedException; + +import java.io.IOException; + +/** + * + * @author Jeremy Rand + */ +public class NameScriptTest { + private static final AbstractNamecoinParams params = NamecoinMainNetParams.get(); + + @Before + public void setUp() throws Exception { + Context context = new Context(params); + } + + @Rule + public ExpectedException expectedEx = ExpectedException.none(); + + @Test + public void nameNewIsNameOp() throws IOException { + final NameScript ns = getNameNewNameScript(); + + assertTrue(ns.isNameOp()); + } + + @Test + public void nameNewGetAddress() throws IOException { + final NameScript ns = getNameNewNameScript(); + + assertEquals("MyVbKbD4MYNUMEpdNAm3Jd3nbr5t8djALC", ns.getAddress().getToAddress(params).toString()); + } + + // TODO: getNameOp when it's implemented + + @Test + public void nameNewIsAnyUpdate() throws IOException { + final NameScript ns = getNameNewNameScript(); + + assertFalse(ns.isAnyUpdate()); + } + + @Test + public void nameNewGetOpName() throws IOException { + final NameScript ns = getNameNewNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not an AnyUpdate op"); + ns.getOpName(); + } + + @Test + public void nameNewGetOpValue() throws IOException { + final NameScript ns = getNameNewNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not an AnyUpdate op"); + ns.getOpValue(); + + } + + // TODO: getOpRand, getOpHash, isNameScript when they're implemented + + @Test + public void nameFirstUpdateIsNameOp() throws IOException { + final NameScript ns = getNameFirstUpdateNameScript(); + + assertTrue(ns.isNameOp()); + } + + @Test + public void nameFirstUpdateGetAddress() throws IOException { + final NameScript ns = getNameFirstUpdateNameScript(); + + assertEquals("NGcTVLgw6cgdavaE7C9QvWaY7gKiWbLrjP", ns.getAddress().getToAddress(params).toString()); + } + + // TODO: getNameOp when it's implemented + + @Test + public void nameFirstUpdateIsAnyUpdate() throws IOException { + final NameScript ns = getNameFirstUpdateNameScript(); + + assertTrue(ns.isAnyUpdate()); + } + + @Test + public void nameFirstUpdateGetOpName() throws IOException { + final NameScript ns = getNameFirstUpdateNameScript(); + + assertEquals("d/bitcoin", new String(ns.getOpName().data, "ISO-8859-1")); + } + + @Test + public void nameFirstUpdateGetOpValue() throws IOException { + final NameScript ns = getNameFirstUpdateNameScript(); + + assertEquals("webpagedeveloper.me/namecoin", new String(ns.getOpValue().data, "ISO-8859-1")); + } + + // TODO: getOpRand, getOpHash, isNameScript when they're implemented + + @Test + public void nameUpdateIsNameOp() throws IOException { + final NameScript ns = getNameUpdateNameScript(); + + assertTrue(ns.isNameOp()); + } + + @Test + public void nameUpdateGetAddress() throws IOException { + final NameScript ns = getNameUpdateNameScript(); + + assertEquals("N9dLs1zHRfZr5cJNjSrvhWrrUcmNSthdmz", ns.getAddress().getToAddress(params).toString()); + } + + // TODO: getNameOp when it's implemented + + @Test + public void nameUpdateIsAnyUpdate() throws IOException { + final NameScript ns = getNameUpdateNameScript(); + + assertTrue(ns.isAnyUpdate()); + } + + @Test + public void nameUpdateGetOpName() throws IOException { + final NameScript ns = getNameUpdateNameScript(); + + assertEquals("d/bitcoin", new String(ns.getOpName().data, "ISO-8859-1")); + } + + @Test + public void nameUpdateGetOpValue() throws IOException { + final NameScript ns = getNameUpdateNameScript(); + + assertEquals("{\"info\":{\"registrar\":\"http://register.dot-bit.org\"},\"email\": \"register@dot-bit.org\",\"ns\":[\"ns0.web-sweet-web.net\",\"ns1.web-sweet-web.net\"],\"map\":{\"\":{\"ns\":[\"ns0.web-sweet-web.net\",\"ns1.web-sweet-web.net\"]}}}", new String(ns.getOpValue().data, "ISO-8859-1")); + } + + // TODO: getOpRand, getOpHash, isNameScript when they're implemented + + @Test + public void currencyIsNameOp() throws IOException { + final NameScript ns = getCurrencyNameScript(); + + assertFalse(ns.isNameOp()); + } + + @Test + public void currencyGetAddress() throws IOException { + final NameScript ns = getCurrencyNameScript(); + + assertEquals("NCMmrGC7uaJ3uv8feLgBTtwGLQSWfmxMCk", ns.getAddress().getToAddress(params).toString()); + } + + // TODO: getNameOp when it's implemented + + @Test + public void currencyIsAnyUpdate() throws IOException { + final NameScript ns = getCurrencyNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not a name op"); + ns.isAnyUpdate(); + } + + @Test + public void currencyGetOpName() throws IOException { + final NameScript ns = getCurrencyNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not an AnyUpdate op"); + ns.getOpName(); + } + + @Test + public void currencyGetOpValue() throws IOException { + final NameScript ns = getCurrencyNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not an AnyUpdate op"); + ns.getOpValue(); + } + + // TODO: getOpRand, getOpHash, isNameScript when they're implemented + + @Test + public void returnIsNameOp() throws IOException { + final NameScript ns = getReturnNameScript(); + + assertFalse(ns.isNameOp()); + } + + @Test + public void returnGetAddress() throws IOException { + final NameScript ns = getReturnNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Cannot cast this script to a pay-to-address type"); + ns.getAddress().getToAddress(params).toString(); + } + + // TODO: getNameOp when it's implemented + + @Test + public void returnIsAnyUpdate() throws IOException { + final NameScript ns = getReturnNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not a name op"); + ns.isAnyUpdate(); + } + + @Test + public void returnGetOpName() throws IOException { + final NameScript ns = getReturnNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not an AnyUpdate op"); + ns.getOpName(); + } + + @Test + public void returnGetOpValue() throws IOException { + final NameScript ns = getReturnNameScript(); + + expectedEx.expect(org.bitcoinj.core.ScriptException.class); + expectedEx.expectMessage("Not an AnyUpdate op"); + ns.getOpValue(); + } + + // TODO: getOpRand, getOpHash, isNameScript when they're implemented + + NameScript getNameNewNameScript() throws IOException { + byte[] payload; + final Transaction tx; + final TransactionOutput out; + final Script outScript; + final NameScript ns; + + // https://namecoin.webbtc.com/tx/6047ce28a076118403aa960909c9c4d0056f97ee0da4d37d109515f8367e2ccb + + payload = Util.getBytes(getClass().getResourceAsStream("namecoin_name_new_d_bitcoin.bin")); + tx = new Transaction(params, payload); + out = tx.getOutputs().get(1); + outScript = out.getScriptPubKey(); + ns = new NameScript(outScript); + + return ns; + } + + NameScript getNameFirstUpdateNameScript() throws IOException { + byte[] payload; + final Transaction tx; + final TransactionOutput out; + final Script outScript; + final NameScript ns; + + // https://namecoin.webbtc.com/tx/ab1207bd605af57ed0b5325ac94d19578cff3bce668ebe8dda2f42a00b001f5d + + payload = Util.getBytes(getClass().getResourceAsStream("namecoin_name_firstupdate_d_bitcoin.bin")); + tx = new Transaction(params, payload); + out = tx.getOutputs().get(1); + outScript = out.getScriptPubKey(); + ns = new NameScript(outScript); + + return ns; + } + + NameScript getNameUpdateNameScript() throws IOException { + byte[] payload; + final Transaction tx; + final TransactionOutput out; + final Script outScript; + final NameScript ns; + + // https://namecoin.webbtc.com/tx/3376c5e0e5b69d0a104863de8432d7c13f891065e7628a72487b770c6418d397 + + payload = Util.getBytes(getClass().getResourceAsStream("namecoin_name_update_d_bitcoin.bin")); + tx = new Transaction(params, payload); + out = tx.getOutputs().get(1); + outScript = out.getScriptPubKey(); + ns = new NameScript(outScript); + + return ns; + } + + NameScript getCurrencyNameScript() throws IOException { + byte[] payload; + final Transaction tx; + final TransactionOutput out; + final Script outScript; + final NameScript ns; + + // https://namecoin.webbtc.com/tx/4ea5d679d63ef46449a44ca056584a986412676641bdaf13d44a7c7c2e32cca1 + + payload = Util.getBytes(getClass().getResourceAsStream("namecoin_p2pkh.bin")); + tx = new Transaction(params, payload); + out = tx.getOutputs().get(0); + outScript = out.getScriptPubKey(); + ns = new NameScript(outScript); + + return ns; + } + + NameScript getReturnNameScript() throws IOException { + byte[] payload; + final Transaction tx; + final TransactionOutput out; + final Script outScript; + final NameScript ns; + + // https://namecoin.webbtc.com/tx/ab1207bd605af57ed0b5325ac94d19578cff3bce668ebe8dda2f42a00b001f5d + + payload = Util.getBytes(getClass().getResourceAsStream("namecoin_name_firstupdate_d_bitcoin.bin")); + tx = new Transaction(params, payload); + out = tx.getOutputs().get(2); + outScript = out.getScriptPubKey(); + ns = new NameScript(outScript); + + return ns; + } +} diff --git a/core/src/test/resources/org/libdohj/script/namecoin_name_firstupdate_d_bitcoin.bin b/core/src/test/resources/org/libdohj/script/namecoin_name_firstupdate_d_bitcoin.bin new file mode 100644 index 0000000000000000000000000000000000000000..b58e7ffdfc8b5bf3f607f1c628151079982d41bd GIT binary patch literal 476 zcmV<20VDnZaR2}U%Pf92_!X59ebb~3?w4-`(8S3J36`n@gb{Y2D9%S<0RR91j7cy? z0wDnF!ukV6Otc=?+wMJ0gv8#|M6duz2lo!&V1X)l`sApoYY&C}OOFSh;IZ=`SH zbDuhE>`JG$c9G?ye3Ag1|9AmG1P1deXm-?J8pT3WzVq z;aC8z_%|NjF3Q6&NZ z0000(K?E?InH*2H!%a6L%6E2o3{YyvV$^CYsurHHPT0wy$-SA6SAo033H^ zVsK$+Wn^V`Wo&P7WpXZUWiM`FZDnI`X>M(8cBvHG$HMy1nSe-rq=et>V>zEW0!>u*UBYRA;6%R;ea!47> zVU3MlWX*N_q2)47etj-)Mn{%QNlmV2|KyyHD$_NvUa^q-s&8b?-_sw~SSas$mbN&1 zyUSabMHc=}L2>3=H;VEED;9RJ9r)=u!PzXL_nOL=bCdrA!Or76Pj>Hr+P}0VZ)X(C zD)WuIKcA6OeYxLuW+G$57RFCN7c+Qz7`QSiq|MGaw0*1JDrKjT<@=c(7(FchT-zY~ zXT>Cj)gn_KZNJW>$gqWpIlDQ@Yr>~@-!@+^TF=vY*2Ll*Pwc;f(`SWRk}jNK1lh<$URu7A zRkc!4YIrS4gh$Zt&)KswXsS{t=)GAOHY$tW6UD literal 0 HcmV?d00001