Skip to content

Commit

Permalink
Ensure UIntX length (#2655)
Browse files Browse the repository at this point in the history
* Ensure length

* Fix UT and change to FormatException
  • Loading branch information
shargon authored Jan 28, 2022
1 parent abd53e8 commit 1e44918
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/neo/UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public UInt160()
/// <param name="value">The value of the <see cref="UInt160"/>.</param>
public unsafe UInt160(ReadOnlySpan<byte> value)
{
if (value.Length != Length) throw new FormatException();
fixed (ulong* p = &value1)
{
Span<byte> dst = new(p, Length);
Expand Down
1 change: 1 addition & 0 deletions src/neo/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public UInt256()
/// <param name="value">The value of the <see cref="UInt256"/>.</param>
public unsafe UInt256(ReadOnlySpan<byte> value)
{
if (value.Length != Length) throw new FormatException();
fixed (ulong* p = &value1)
{
Span<byte> dst = new(p, Length);
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/Native/UT_GasToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ await Assert.ThrowsExceptionAsync<InvalidOperationException>(async () =>
// Bad inputs

Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.GAS.Transfer(snapshot, from, to, BigInteger.MinusOne, true, persistingBlock));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.GAS.Transfer(snapshot, new byte[19], to, BigInteger.One, false, persistingBlock));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.GAS.Transfer(snapshot, from, new byte[19], BigInteger.One, false, persistingBlock));
Assert.ThrowsException<FormatException>(() => NativeContract.GAS.Transfer(snapshot, new byte[19], to, BigInteger.One, false, persistingBlock));
Assert.ThrowsException<FormatException>(() => NativeContract.GAS.Transfer(snapshot, from, new byte[19], BigInteger.One, false, persistingBlock));
}

internal static StorageKey CreateStorageKey(byte prefix, uint key)
Expand Down
4 changes: 2 additions & 2 deletions tests/neo.UnitTests/SmartContract/Native/UT_NeoToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ public void Check_Transfer()
// Bad inputs

Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.NEO.Transfer(snapshot, from, to, BigInteger.MinusOne, true, persistingBlock));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.NEO.Transfer(snapshot, new byte[19], to, BigInteger.One, false, persistingBlock));
Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.NEO.Transfer(snapshot, from, new byte[19], BigInteger.One, false, persistingBlock));
Assert.ThrowsException<FormatException>(() => NativeContract.NEO.Transfer(snapshot, new byte[19], to, BigInteger.One, false, persistingBlock));
Assert.ThrowsException<FormatException>(() => NativeContract.NEO.Transfer(snapshot, from, new byte[19], BigInteger.One, false, persistingBlock));

// More than balance

Expand Down
4 changes: 1 addition & 3 deletions tests/neo.UnitTests/SmartContract/UT_InteropService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ public void TestBlockchain_GetBlock()
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
NativeContract.Ledger.GetBlock(engine.Snapshot, new UInt256(data1)).Should().BeNull();

byte[] data2 = new byte[] { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 };
Assert.ThrowsException<ArgumentOutOfRangeException>(() => NativeContract.Ledger.GetBlock(engine.Snapshot, new UInt256(data2)));
NativeContract.Ledger.GetBlock(engine.Snapshot, TestBlockchain.TheNeoSystem.GenesisBlock.Hash).Should().NotBeNull();
}

[TestMethod]
Expand Down
6 changes: 6 additions & 0 deletions tests/neo.UnitTests/UT_UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace Neo.UnitTests.IO
[TestClass]
public class UT_UInt160
{
[TestMethod]
public void TestFail()
{
Assert.ThrowsException<FormatException>(() => new UInt160(new byte[UInt160.Length + 1]));
}

[TestMethod]
public void TestGernerator1()
{
Expand Down
6 changes: 6 additions & 0 deletions tests/neo.UnitTests/UT_UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ namespace Neo.UnitTests.IO
[TestClass]
public class UT_UInt256
{
[TestMethod]
public void TestFail()
{
Assert.ThrowsException<FormatException>(() => new UInt256(new byte[UInt256.Length + 1]));
}

[TestMethod]
public void TestGernerator1()
{
Expand Down

0 comments on commit 1e44918

Please sign in to comment.