Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test large overflows and underflows for EnsureIntegerInRange #1170

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/Neo.Compiler.CSharp.TestContracts/Contract_Overflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Neo.Compiler.CSharp.TestContracts
{
public class Contract_Overflow : SmartContract.Framework.SmartContract
{
public static int Add(int a, int b) => a + b;
public static int Mul(int a, int b) => a * b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Neo.Cryptography.ECC;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Numerics;

namespace Neo.SmartContract.Testing;

public abstract class Contract_Overflow(Neo.SmartContract.Testing.SmartContractInitialize initialize) : Neo.SmartContract.Testing.SmartContract(initialize), IContractInfo
{
#region Compiled data

public static Neo.SmartContract.Manifest.ContractManifest Manifest => Neo.SmartContract.Manifest.ContractManifest.Parse(@"{""name"":""Contract_Overflow"",""groups"":[],""features"":{},""supportedstandards"":[],""abi"":{""methods"":[{""name"":""add"",""parameters"":[{""name"":""a"",""type"":""Integer""},{""name"":""b"",""type"":""Integer""}],""returntype"":""Integer"",""offset"":0,""safe"":false},{""name"":""mul"",""parameters"":[{""name"":""a"",""type"":""Integer""},{""name"":""b"",""type"":""Integer""}],""returntype"":""Integer"",""offset"":53,""safe"":false}],""events"":[]},""permissions"":[],""trusts"":[],""extra"":{""nef"":{""optimization"":""All""}}}");

/// <summary>
/// Optimization: "All"
/// </summary>
public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable<Neo.SmartContract.NefFile>(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGpXAAJ4eZ5KAgAAAIAuBCIKSgL///9/Mh4D/////wAAAACRSgL///9/MgwDAAAAAAEAAACfQFcAAnh5oEoCAAAAgC4EIgpKAv///38yHgP/////AAAAAJFKAv///38yDAMAAAAAAQAAAJ9AhqjPTQ=="));

#endregion

#region Unsafe methods

/// <summary>
/// Unsafe method
/// </summary>
[DisplayName("add")]
public abstract BigInteger? Add(BigInteger? a, BigInteger? b);

/// <summary>
/// Unsafe method
/// </summary>
[DisplayName("mul")]
public abstract BigInteger? Mul(BigInteger? a, BigInteger? b);

#endregion

}
32 changes: 32 additions & 0 deletions tests/Neo.Compiler.CSharp.UnitTests/UnitTest_Overflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Neo.SmartContract.Testing;
using Neo.SmartContract.Testing.Exceptions;
using System.Numerics;

namespace Neo.Compiler.CSharp.UnitTests
{
[TestClass]
public class UnitTest_Overflow : DebugAndTestBase<Contract_Overflow>
{
[TestMethod]
public void Test_Add()
{
Assert.AreEqual(unchecked(int.MinValue - 1), Contract.Add(int.MinValue, -1));
Assert.AreEqual(unchecked(int.MaxValue + 1), Contract.Add(int.MaxValue, 1));
Assert.AreEqual(unchecked(int.MinValue - int.MaxValue), Contract.Add(int.MinValue, -int.MaxValue));
Assert.AreEqual(unchecked(int.MaxValue - int.MinValue), Contract.Add(int.MaxValue, unchecked(-int.MinValue)));
}

[TestMethod]
public void Test_Mul()
{
Assert.AreEqual(unchecked(int.MinValue * 2), Contract.Mul(int.MinValue, 2));
Assert.AreEqual(unchecked(int.MinValue * (-2)), Contract.Mul(int.MinValue, -2));
Assert.AreEqual(unchecked(int.MaxValue * 2), Contract.Mul(int.MaxValue, 2));
Assert.AreEqual(unchecked(int.MaxValue * (-2)), Contract.Mul(int.MaxValue, -2));
Assert.AreEqual(unchecked(int.MinValue * int.MaxValue), Contract.Mul(int.MinValue, int.MaxValue));
Assert.AreEqual(unchecked(int.MinValue * (-int.MaxValue)), Contract.Mul(int.MinValue, -int.MaxValue));
Assert.AreEqual(unchecked((-int.MinValue) * int.MaxValue), Contract.Mul(unchecked(-int.MinValue), int.MaxValue));
}
}
}
Loading