Skip to content

Commit cf62860

Browse files
committed
(improvement) add some test for new methods
1 parent 24d5cd8 commit cf62860

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

src/Solnet.Programs/Models/NameService/TokenData.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public static TokenData Deserialize(byte[] input)
5353
var data = new ReadOnlySpan<byte>(input, 96, input.Length - 96);
5454
int offset = 0;
5555

56-
offset += data.GetBorshString(0, out var name);
57-
offset += data.GetBorshString(offset, out var ticker);
56+
offset += data.GetBorshStringSize(0, out var name);
57+
offset += data.GetBorshStringSize(offset, out var ticker);
5858

5959
var mint = data.GetPubKey(offset);
6060
offset += 32;
@@ -64,7 +64,7 @@ public static TokenData Deserialize(byte[] input)
6464
string website = null, logo = null;
6565

6666
if (data.GetBool(offset++))
67-
offset += data.GetBorshString(offset, out website);
67+
offset += data.GetBorshStringSize(offset, out website);
6868

6969
if (data.GetBool(offset++))
7070
data.GetBorshString(offset, out logo);

src/Solnet.Programs/Utilities/Deserialization.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,32 @@ public static (string EncodedString, int Length) DecodeBincodeString(this ReadOn
242242
/// <param name="result">The decoded data./>.</param>
243243
/// <returns>The length in bytes that was read from the original buffer, including the</returns>
244244
/// <exception cref="ArgumentOutOfRangeException">Thrown when the offset is too big for the span.</exception>
245-
public static int GetBorshString(this ReadOnlySpan<byte> data, int offset, out string result)
245+
public static string GetBorshString(this ReadOnlySpan<byte> data, int offset, out string result)
246246
{
247247
if (offset + sizeof(uint) > data.Length)
248248
throw new ArgumentOutOfRangeException(nameof(offset));
249-
250249
int stringLength = (int)data.GetU32(offset);
251250
byte[] stringBytes = data.GetSpan(offset + sizeof(uint), stringLength).ToArray();
252251
result = Encoding.UTF8.GetString(stringBytes);
253252

253+
return result;
254+
}
255+
/// <summary>
256+
/// Get size of the Borsh string
257+
/// </summary>
258+
/// <param name="data"></param>
259+
/// <param name="offset"></param>
260+
/// <param name="result"></param>
261+
/// <returns></returns>
262+
/// <exception cref="ArgumentOutOfRangeException"></exception>
263+
public static int GetBorshStringSize(this ReadOnlySpan<byte> data, int offset, out string result)
264+
{
265+
if (offset + sizeof(uint) > data.Length)
266+
throw new ArgumentOutOfRangeException(nameof(offset));
267+
268+
int stringLength = (int)data.GetU32(offset);
269+
byte[] stringBytes = data.GetSpan(offset + sizeof(uint), stringLength).ToArray();
270+
result = Encoding.UTF8.GetString(stringBytes);
254271
return stringLength + sizeof(uint);
255272
}
256273

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Solnet.Programs.Utilities;
3+
using System;
4+
5+
namespace Solnet.Programs.Test;
6+
7+
8+
[TestClass]
9+
public class BpfLoaderProgramTest
10+
{
11+
private string _expectedBorshStringResult =
12+
"0B00000068656C6C6F20776F726C6400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
13+
private string _expectedBorshStringResult2 =
14+
"000B00000068656C6C6F20776F726C64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
15+
[TestMethod]
16+
public void BorshStringSerializationTest()
17+
{
18+
var sampleString = "hello world";
19+
var buffer = new byte[100];
20+
buffer.WriteBorshString( sampleString, 0);
21+
Assert.AreEqual( Convert.ToHexString(buffer) ,_expectedBorshStringResult );
22+
buffer = new byte[100];
23+
buffer.WriteBorshString( sampleString, 1);
24+
Assert.AreEqual( Convert.ToHexString(buffer) ,_expectedBorshStringResult2 );
25+
}
26+
27+
[TestMethod]
28+
public void BorshStringDeserializationTest()
29+
{
30+
var sampleString = "hello world";
31+
var sample1 = new ReadOnlySpan<byte>(Convert.FromHexString(_expectedBorshStringResult)) ;
32+
sample1.GetBorshString(0, out var result);
33+
Assert.AreEqual(sampleString,result );
34+
sample1 = new ReadOnlySpan<byte>(Convert.FromHexString(_expectedBorshStringResult2)) ;
35+
sample1.GetBorshString(1, out var result2);
36+
Assert.AreEqual(sampleString,result2 );
37+
}
38+
}

test/Solnet.Programs.Test/Solnet.Programs.Test.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<TargetFramework>net6.0</TargetFramework>
55

66
<IsPackable>false</IsPackable>
7+
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
79
</PropertyGroup>
810

911
<ItemGroup>

0 commit comments

Comments
 (0)