Skip to content

Commit

Permalink
run tests on net7.0 netstandard2.0
Browse files Browse the repository at this point in the history
Signed-off-by: David Hernando <[email protected]>
  • Loading branch information
davidatwhiletrue committed Apr 12, 2024
1 parent bbcea99 commit a955ef7
Show file tree
Hide file tree
Showing 19 changed files with 134 additions and 43 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and Test

on:
push:
branches: [ master ]
branches: [ master, netstandard-2-0-target ]
pull_request:
branches: [ master ]

Expand All @@ -11,6 +11,13 @@ jobs:

runs-on: ubuntu-latest

strategy:
matrix:
include:
- name: net7.0
framework: net7.0
- name: netstandard2.0
framework: netstandard2.0
steps:
- uses: actions/checkout@v2
- name: Setup .NET
Expand All @@ -22,4 +29,4 @@ jobs:
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal --filter "TestCategory!~NCTL"
run: TEST_FRAMEWORK ${{ matrix.framework }} dotnet test --no-build --verbosity normal --filter "TestCategory!~NCTL"
5 changes: 2 additions & 3 deletions Casper.Network.SDK.Test/CLValueJsonDeserializerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Text.Json.Serialization;
using Casper.Network.SDK.Converters;
using Casper.Network.SDK.Types;
using Newtonsoft.Json.Linq;
using NUnit.Framework;
using Org.BouncyCastle.Utilities.Encoders;

Expand Down Expand Up @@ -148,7 +147,7 @@ public void DeserializeI32CLValue()
var json = @"{""cl_type"":""I32"",""bytes"":""00000080"",""parsed"":-2147483648}";
var clValue = JsonSerializer.Deserialize<CLValue>(json);
Assert.IsNotNull(clValue);
Assert.AreEqual(int.MinValue, BitConverter.ToInt32(clValue.Bytes));
Assert.AreEqual(int.MinValue, BitConverterExtensions.ToInt32(clValue.Bytes));
Assert.AreEqual(int.MinValue, clValue.Parsed);
}

Expand All @@ -168,7 +167,7 @@ public void DeserializeU64CLValue()
var clValue = JsonSerializer.Deserialize<CLValue>(json);
Assert.IsNotNull(clValue);
Assert.AreEqual(CLType.U64, clValue.TypeInfo.Type);
Assert.AreEqual(ulong.MaxValue, BitConverter.ToUInt64(clValue.Bytes));
Assert.AreEqual(ulong.MaxValue, BitConverterExtensions.ToUInt64(clValue.Bytes));
Assert.AreEqual(ulong.MaxValue, clValue.Parsed);
}

Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/CLValueToTypeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public void CLValueToBigInteger()
Assert.AreEqual(new BigInteger(5123456789012), clValue.ToBigInteger());
Assert.AreEqual(new BigInteger(5123456789012), (BigInteger) clValue);

var bigInt = new BigInteger(Hex.Decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"), true);
var bigInt = BigIntegerCompat.Create(Hex.Decode("F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF"), true);
clValue = CLValue.U128(bigInt);
Assert.AreEqual(bigInt, clValue.ToBigInteger());
Assert.AreEqual(bigInt, (BigInteger) clValue);
Expand Down
18 changes: 13 additions & 5 deletions Casper.Network.SDK.Test/Casper.Network.SDK.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>

<LangVersion>9.0</LangVersion>
<IsPackable>false</IsPackable>

<RootNamespace>NetCasperTest</RootNamespace>
</PropertyGroup>

Expand All @@ -15,10 +14,19 @@
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Casper.Network.SDK\Casper.Network.SDK.csproj" />
<!-- Define a property to read from an environment variable -->
<PropertyGroup>
<CustomTargetFramework Condition="'$(TEST_FRAMEWORK)' != ''">$(TEST_FRAMEWORK)</CustomTargetFramework>
<!-- Fallback to a default framework if the environment variable is not set -->
<CustomTargetFramework Condition="'$(CustomTargetFramework)' == ''">net7.0</CustomTargetFramework>
</PropertyGroup>

<ItemGroup Condition="'$(CustomTargetFramework)' == 'net7.0'">
<ProjectReference Include="..\Casper.Network.SDK\Casper.Network.SDK.csproj" Properties="TargetFramework=net7.0" />
</ItemGroup>
<ItemGroup Condition="'$(CustomTargetFramework)' == 'netstandard2.0'">
<ProjectReference Include="..\Casper.Network.SDK\Casper.Network.SDK.csproj" Properties="TargetFramework=netstandard2.0" />
</ItemGroup>

<ItemGroup>
<Content Include="TestData\**\*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
Expand Down
18 changes: 9 additions & 9 deletions Casper.Network.SDK.Test/KeysTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ public void TestValidBlakeEd25519()
var publicKey = PublicKey.FromHexString(ED25519publicKey);
Assert.AreEqual(KeyAlgo.ED25519, publicKey.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, publicKey.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(publicKey.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(publicKey.RawBytes));

var hash = publicKey.GetAccountHash();
Assert.AreEqual(ED25519hash, hash.Substring("account-hash-".Length), "Unexpected ED25519 hash value");

var pk2 = PublicKey.FromBytes(Hex.Decode(ED25519publicKey));
Assert.AreEqual(KeyAlgo.ED25519, pk2.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, pk2.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(pk2.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(pk2.RawBytes));

var pk3 = PublicKey.FromRawBytes(Hex.Decode(ED25519publicKey)[1..], KeyAlgo.ED25519);
var pk3 = PublicKey.FromRawBytes(Hex.Decode(ED25519publicKey).Slice(1), KeyAlgo.ED25519);
Assert.AreEqual(KeyAlgo.ED25519, pk3.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, pk3.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(pk3.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(pk3.RawBytes));

var pemfile = TestContext.CurrentContext.TestDirectory +
"/TestData/test-ed25519-pk.pem";
var pk4 = PublicKey.FromPem(pemfile);
Assert.AreEqual(KeyAlgo.ED25519, pk4.KeyAlgorithm);
Assert.AreEqual(ED25519publicKey, pk4.ToAccountHex());
Assert.IsTrue(Hex.Decode(ED25519publicKey)[1..].SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).Slice(1).SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(ED25519publicKey).SequenceEqual(pk4.GetBytes()));

var hash3 = pk4.GetAccountHash();
Expand All @@ -60,19 +60,19 @@ public void TestValidBlakeSecp256k1()
var pk2 = PublicKey.FromBytes(Hex.Decode(SECP256K1publicKey));
Assert.AreEqual(KeyAlgo.SECP256K1, pk2.KeyAlgorithm);
Assert.AreEqual(SECP256K1publicKey, pk2.ToAccountHex());
Assert.IsTrue(Hex.Decode(SECP256K1publicKey)[1..].SequenceEqual(pk2.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).Slice(1).SequenceEqual(pk2.RawBytes));

var pk3 = PublicKey.FromRawBytes(Hex.Decode(SECP256K1publicKey)[1..], KeyAlgo.SECP256K1);
var pk3 = PublicKey.FromRawBytes(Hex.Decode(SECP256K1publicKey).Slice(1), KeyAlgo.SECP256K1);
Assert.AreEqual(KeyAlgo.SECP256K1, pk3.KeyAlgorithm);
Assert.AreEqual(SECP256K1publicKey, pk3.ToAccountHex());
Assert.IsTrue(Hex.Decode(SECP256K1publicKey)[1..].SequenceEqual(pk3.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).Slice(1).SequenceEqual(pk3.RawBytes));

var pemfile = TestContext.CurrentContext.TestDirectory +
"/TestData/test-secp256k1-pk.pem";
var pk4 = PublicKey.FromPem(pemfile);
Assert.AreEqual(KeyAlgo.SECP256K1, pk4.KeyAlgorithm);
Assert.AreEqual(SECP256K1publicKey, pk4.ToAccountHex());
Assert.IsTrue(Hex.Decode(SECP256K1publicKey)[1..].SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).Slice(1).SequenceEqual(pk4.RawBytes));
Assert.IsTrue(Hex.Decode(SECP256K1publicKey).SequenceEqual(pk4.GetBytes()));

var hash3 = pk4.GetAccountHash();
Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/NctlContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NctlContractTest : NctlBase
[Test, Order(1)]
public async Task DeployContractTest()
{
var wasmBytes = await File.ReadAllBytesAsync(_wasmFile);
var wasmBytes = await FileExtensions.ReadAllBytesAsync(_wasmFile);

var deploy = DeployTemplates.ContractDeploy(
wasmBytes,
Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/NctlMyDictContractTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class NctlMyDictContractTest : NctlBase
[Test, Order(1)]
public async Task DeployContractTest()
{
var wasmBytes = await File.ReadAllBytesAsync(_wasmFile);
var wasmBytes = await FileExtensions.ReadAllBytesAsync(_wasmFile);

var deploy = DeployTemplates.ContractDeploy(
wasmBytes,
Expand Down
2 changes: 1 addition & 1 deletion Casper.Network.SDK.Test/NctlSpeculativeExecutionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Setup()
[Test, Order(1)]
public async Task SpeculativeExecutionTest()
{
var wasmBytes = await File.ReadAllBytesAsync(_wasmFile);
var wasmBytes = await FileExtensions.ReadAllBytesAsync(_wasmFile);

var deploy = DeployTemplates.ContractDeploy(
wasmBytes,
Expand Down
4 changes: 3 additions & 1 deletion Casper.Network.SDK.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\d\AppData\Local\JetBrains\Rider2021.3\resharper-host\temp\Rider\vAny\CoverageData\_Casper.Network.SDK.-947408436\Snapshot\snapshot.utdcvr</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=240e6887_002D6585_002D487e_002D89bf_002D0b306f497b30/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
&lt;Solution /&gt;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
3 changes: 2 additions & 1 deletion Casper.Network.SDK/Casper.Network.SDK.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0;netstandard2.0</TargetFramework>
<TargetFrameworks>net7.0;netstandard2.0</TargetFrameworks>
<LangVersion>9.0</LangVersion>
<NoWarn>CS1591</NoWarn>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0</FileVersion>
<PackageVersion>2.2.0</PackageVersion>
Expand Down
7 changes: 4 additions & 3 deletions Casper.Network.SDK/Compat/BigIntegerCompat.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#if NETSTANDARD2_0

using System;
using System.Numerics;

public class BigIntegerCompat
{
public static BigInteger Create(byte[] value, bool isUnsigned = false, bool isBigEndian = false)
{
#if NET7_0_OR_GREATER
return new BigInteger(value, true, false);
#elif NETSTANDARD2_0
if (value == null)
{
throw new ArgumentNullException(nameof(value));
Expand All @@ -28,7 +29,7 @@ public static BigInteger Create(byte[] value, bool isUnsigned = false, bool isBi
}

return new BigInteger(value);
#endif
}
}

#endif
30 changes: 30 additions & 0 deletions Casper.Network.SDK/Compat/BigIntegerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@ public static byte[] ToByteArray(this BigInteger value, bool isUnsigned = false)

return bytes;
}

public static int GetBitLength(this BigInteger value)
{
// If the value is zero, its bit length is 0
if (value == BigInteger.Zero)
return 0;

// Get the byte array without the sign
byte[] bytes = value.ToByteArray();

// Find the highest-order byte with a non-zero value
int highestByte = bytes.Length - 1;
while (highestByte > 0 && bytes[highestByte] == 0)
{
highestByte--;
}

// Count the bits in the last used byte
int bitsInLastByte = 0;
byte lastByte = bytes[highestByte];
while (lastByte != 0)
{
bitsInLastByte++;
lastByte >>= 1;
}

// Calculate the total bit length
// (Number of full bytes minus 1) * 8 + bits in last byte
return highestByte * 8 + bitsInLastByte;
}
}

#endif
2 changes: 0 additions & 2 deletions Casper.Network.SDK/Compat/BitConverterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if NETSTANDARD2_0

using System;

Expand All @@ -25,4 +24,3 @@ public static UInt32 ToUInt32(byte[] value, int startIndex = 0)
}
}

#endif
2 changes: 0 additions & 2 deletions Casper.Network.SDK/Compat/BytesExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#if NETSTANDARD2_0

using System;

Expand All @@ -22,4 +21,3 @@ public static byte[] Slice(this byte[] bytes, int from, int? to = null)
}
}

#endif
7 changes: 4 additions & 3 deletions Casper.Network.SDK/Compat/DateTimeCompat.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#if NETSTANDARD2_0

using System;
using System.Globalization;

public static class DateTimeCompat
{
#if NET7_0_OR_GREATER
public static readonly DateTime UnixEpoch = DateTime.UnixEpoch;
#elif NETSTANDARD2_0
public static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
#endif
}

#endif
4 changes: 0 additions & 4 deletions Casper.Network.SDK/Compat/EnumCompat.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#if NETSTANDARD2_0

using System;
using System.Linq;

Expand All @@ -15,5 +13,3 @@ public static string GetName<TEnum>(TEnum value) where TEnum : Enum
return Enum.GetName(typeof(TEnum), value);
}
}

#endif
38 changes: 38 additions & 0 deletions Casper.Network.SDK/Compat/FileExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.IO;
using System.Threading.Tasks;

public static class FileExtensions
{
public static async Task<byte[]> ReadAllBytesAsync(string path)
{
#if NET7_0_OR_GREATER
return await File.ReadAllBytesAsync(path);
#elif NETSTANDARD2_0

// Using FileStream with asynchronous flag
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096,
FileOptions.Asynchronous))
{
// Allocate memory for the file's content
byte[] bytes = new byte[stream.Length];
// Read the entire file asynchronously
int numBytesToRead = (int)stream.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = await stream.ReadAsync(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;

numBytesRead += n;
numBytesToRead -= n;
}

return bytes;
}
#endif
}
}

8 changes: 4 additions & 4 deletions Casper.Network.SDK/Types/CLValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ public Dictionary<TKey, TValue> ToDictionary<TKey, TValue>()
}

/// <summary>
/// Converts Tuple1 CLValue to Tuple&ltT1;&gt;.
/// Converts Tuple1 CLValue to Tuple&lt;T1&gt;.
/// </summary>
public Tuple<T1> ToTuple1<T1>()
{
Expand All @@ -969,7 +969,7 @@ public Tuple<T1> ToTuple1<T1>()
}

/// <summary>
/// Converts Tuple2 CLValue to Tuple&ltT1,T2;&gt;.
/// Converts Tuple2 CLValue to Tuple&lt;T1,T2&gt;.
/// </summary>
public Tuple<T1, T2> ToTuple2<T1, T2>()
{
Expand All @@ -992,7 +992,7 @@ public Tuple<T1, T2> ToTuple2<T1, T2>()
}

/// <summary>
/// Converts Tuple2 CLValue to Tuple&ltT1,T2,T3;&gt;.
/// Converts Tuple2 CLValue to Tuple&lt;T1,T2,T3&gt;.
/// </summary>
public Tuple<T1, T2, T3> ToTuple3<T1, T2, T3>()
{
Expand All @@ -1015,7 +1015,7 @@ public Tuple<T1, T2, T3> ToTuple3<T1, T2, T3>()
}

/// <summary>
/// Converts Result CLValue to Result&ltTOk,TErr;&gt;.
/// Converts Result CLValue to Result&lt;TOk,TErr&gt;.
/// </summary>
public Result<TOk, TErr> ToResult<TOk, TErr>()
{
Expand Down
Loading

0 comments on commit a955ef7

Please sign in to comment.