3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-02-14 19:25:51 +00:00

Move some tests from ScriptTest to ScriptBuilderTest.

This commit is contained in:
Andreas Schildbach 2018-01-02 16:41:11 +01:00
parent 3b80b707a5
commit 3f2a8f11c5
2 changed files with 72 additions and 71 deletions

View File

@ -19,6 +19,7 @@ package org.bitcoinj.script;
import static org.bitcoinj.script.ScriptOpCodes.OP_FALSE;
import static org.bitcoinj.script.ScriptOpCodes.OP_TRUE;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
@ -35,6 +36,77 @@ public class ScriptBuilderTest {
}
}
@Test
public void numberBuilderZero() {
// Test encoding of zero, which should result in an opcode
final ScriptBuilder builder = new ScriptBuilder();
// 0 should encode directly to 0
builder.number(0);
assertArrayEquals(new byte[] {
0x00 // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilderPositiveOpCode() {
final ScriptBuilder builder = new ScriptBuilder();
builder.number(5);
assertArrayEquals(new byte[] {
0x55 // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilderBigNum() {
ScriptBuilder builder = new ScriptBuilder();
// 21066 should take up three bytes including the length byte
// at the start
builder.number(0x524a);
assertArrayEquals(new byte[] {
0x02, // Length of the pushed data
0x4a, 0x52 // Pushed data
}, builder.build().getProgram());
// Test the trimming code ignores zeroes in the middle
builder = new ScriptBuilder();
builder.number(0x110011);
assertEquals(4, builder.build().getProgram().length);
// Check encoding of a value where signed/unsigned encoding differs
// because the most significant byte is 0x80, and therefore a
// sign byte has to be added to the end for the signed encoding.
builder = new ScriptBuilder();
builder.number(0x8000);
assertArrayEquals(new byte[] {
0x03, // Length of the pushed data
0x00, (byte) 0x80, 0x00 // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilderNegative() {
// Check encoding of a negative value
final ScriptBuilder builder = new ScriptBuilder();
builder.number(-5);
assertArrayEquals(new byte[] {
0x01, // Length of the pushed data
((byte) 133) // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilder16() {
ScriptBuilder builder = new ScriptBuilder();
// Numbers greater than 16 must be encoded with PUSHDATA
builder.number(15).number(16).number(17);
builder.number(0, 17).number(1, 16).number(2, 15);
Script script = builder.build();
assertEquals("PUSHDATA(1)[11] 16 15 15 16 PUSHDATA(1)[11]", script.toString());
}
@Test
public void testOpTrue() {
byte[] expected = new byte[] { OP_TRUE };

View File

@ -454,75 +454,4 @@ public class ScriptTest {
public void getToAddressNoPubKey() throws Exception {
ScriptBuilder.createOutputScript(new ECKey()).getToAddress(PARAMS, false);
}
/** Test encoding of zero, which should result in an opcode */
@Test
public void numberBuilderZero() {
final ScriptBuilder builder = new ScriptBuilder();
// 0 should encode directly to 0
builder.number(0);
assertArrayEquals(new byte[] {
0x00 // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilderPositiveOpCode() {
final ScriptBuilder builder = new ScriptBuilder();
builder.number(5);
assertArrayEquals(new byte[] {
0x55 // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilderBigNum() {
ScriptBuilder builder = new ScriptBuilder();
// 21066 should take up three bytes including the length byte
// at the start
builder.number(0x524a);
assertArrayEquals(new byte[] {
0x02, // Length of the pushed data
0x4a, 0x52 // Pushed data
}, builder.build().getProgram());
// Test the trimming code ignores zeroes in the middle
builder = new ScriptBuilder();
builder.number(0x110011);
assertEquals(4, builder.build().getProgram().length);
// Check encoding of a value where signed/unsigned encoding differs
// because the most significant byte is 0x80, and therefore a
// sign byte has to be added to the end for the signed encoding.
builder = new ScriptBuilder();
builder.number(0x8000);
assertArrayEquals(new byte[] {
0x03, // Length of the pushed data
0x00, (byte) 0x80, 0x00 // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilderNegative() {
// Check encoding of a negative value
final ScriptBuilder builder = new ScriptBuilder();
builder.number(-5);
assertArrayEquals(new byte[] {
0x01, // Length of the pushed data
((byte) 133) // Pushed data
}, builder.build().getProgram());
}
@Test
public void numberBuilder16() {
ScriptBuilder builder = new ScriptBuilder();
// Numbers greater than 16 must be encoded with PUSHDATA
builder.number(15).number(16).number(17);
builder.number(0, 17).number(1, 16).number(2, 15);
Script script = builder.build();
assertEquals("PUSHDATA(1)[11] 16 15 15 16 PUSHDATA(1)[11]", script.toString());
}
}