add unit tests for null handling, fix assertion in rust code

This commit is contained in:
Vinzenz Schroeter 2024-10-20 13:33:12 +02:00
parent 9cdb5e0dbf
commit 66823d0676
6 changed files with 71 additions and 2 deletions

View file

@ -70,7 +70,7 @@ pub unsafe extern "C" fn sp_cp437_grid_load(
data: *const u8,
data_length: usize,
) -> NonNull<SPCp437Grid> {
assert!(data.is_null());
assert!(!data.is_null());
let data = std::slice::from_raw_parts(data, data_length);
let result = Box::new(SPCp437Grid(servicepoint::Cp437Grid::load(
width, height, data,

View file

@ -0,0 +1,12 @@
namespace ServicePoint.Tests;
public class BitVecTests
{
[Fact]
public void UseAfterFree()
{
var bitvec = new BitVec(8);
_ = Command.BitmapLinear(0, bitvec, CompressionCode.Uncompressed);
Assert.Throws<NullReferenceException>(() => _ = Command.BitmapLinear(0, bitvec, CompressionCode.Uncompressed));
}
}

View file

@ -0,0 +1,12 @@
namespace ServicePoint.Tests;
public class BitmapTests
{
[Fact]
public void UseAfterFree()
{
var bitmap = Bitmap.NewScreenSized();
_ = Command.BitmapLinearWin(0, 0, bitmap, CompressionCode.Uncompressed);
Assert.Throws<NullReferenceException>(() => _ = Command.BitmapLinearWin(0, 0, bitmap, CompressionCode.Uncompressed));
}
}

View file

@ -0,0 +1,12 @@
namespace ServicePoint.Tests;
public class BBrightnessGridTests
{
[Fact]
public void UseAfterFree()
{
var grid = new BrightnessGrid(23, 42);
_ = Command.CharBrightness(0, 0, grid);
Assert.Throws<NullReferenceException>(() => _ = Command.CharBrightness(0, 0, grid));
}
}

View file

@ -5,10 +5,11 @@ public class CommandTests
private Connection _fakeConnection = Connection.Fake();
[Fact]
public void Test1()
public void UseAfterSend()
{
var command = Command.Clear();
_fakeConnection.Send(command);
Assert.Throws<NullReferenceException>(() => _fakeConnection.Send(command));
_fakeConnection.Send(Command.Clear());
}
}

View file

@ -0,0 +1,32 @@
using System.Runtime.CompilerServices;
namespace ServicePoint.Tests;
public class Cp437GridTests
{
[Fact]
public void UseAfterFree()
{
var grid = new Cp437Grid(2, 3);
_ = Command.Cp437Data(0, 0, grid.Clone());
_ = Command.Cp437Data(0, 0, grid);
Assert.Throws<NullReferenceException>(() => _ = Command.Cp437Data(0, 0, grid));
}
[Fact]
public void ReadAndWriteString()
{
var grid = new Cp437Grid(3, 2);
grid[1] = "abc";
Assert.Equal("abc", grid[1]);
}
[Fact]
public void LoadSpan()
{
var ascii_str = "abc123"u8;
var grid = Cp437Grid.Load(3, 2, ascii_str);
Assert.Equal("abc", grid[0]);
Assert.Equal("123", grid[1]);
}
}