Ho bisogno di scrivere un test che verifica che il mio codice possa gestire una AccessViolationException (o qualsiasi altra eccezione di stato corrotta WIN32 - CSE), che si verifica in un contesto non sicuro, in genere chiamando una terza parte lib . Questo dovrebbe essere fatto usando C# su .net 4.0.Come testare la gestione di AccessViolationException
Ho trovato questa domanda correlata How to handle AccessViolationException e questo articolo correlato http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx, che spiega come catturare questi CSE e il loro background.
Quindi mi piacerebbe provocare un CSE WIN32 in un test, per garantire la corretta gestione della mia applicazione. Qualcosa di simile:
Qualche esempio di classe di prova:
public class MyExceptionHandler
{
[HandleProcessCorruptedStateExceptions]
public void HandleCorruptedStateException()
{
try
{
//Force genuine unsafe AccessViolationException
//not just a throw new AccessViolationException
}
catch(Exception e)
{
//Log/cleanup/other
}
}
public void DoesNotHandleCorruptedStateException()
{
try
{
//Force genuine unsafe AccessViolationException
//not just a throw new AccessViolationException
}
catch (Exception e)
{
//Log/cleanup/other
}
}
}
A Test:
class MyTest
{
[Test]
public void ShouldVerifyThatAnAccessViolationExceptionIsHandledCorrectly()
{
var handler = new MyExceptionHandler();
Assert.DoesNotThrow(() => handler.HandleCorruptedStateException());
}
[Test]
public void ShouldVerifyThatAnAccessViolationExceptionIsNotHandledCorrectly()
{
var handler = new MyExceptionHandler();
Assert.Throws<AccessViolationException>(() => handler.DoesNotHandleCorruptedStateException());
}
}
Qualcuno ha un suggerimento su come raggiungere questo obiettivo, senza molto lavoro (ex scrivendo un pericoloso. lib che causa questa eccezione).
Cordiali saluti
aggiornamento: Per abbinare la mia soluzione finale, grazie alla JaredPar.
public class MyExceptionHandler
{
[HandleProcessCorruptedStateExceptions]
public void HandleCorruptedStateException()
{
try
{
var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);
}
catch(Exception e)
{
//Log/cleanup/other
}
}
public void DoesNotHandleCorruptedStateException()
{
try
{
var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);
}
catch (Exception e)
{
//Log/cleanup/other
}
}
}
SUGGERIMENTO: per verificare questo utilizzare manualmente una semplice console app, dalla linea di comando:
class Program
{
static void Main(string[] args)
{
var handler = new MyExceptionHandler();
if (args.Length > 1)
{
handler.HandleCorruptedStateException();
}
else
{
handler.DoesNotHandleCorruptedStateException();
}
}
}
Grazie, ma questo non genera un CSE, solo un ArgumentNullException. Deve essere un vero CSE Win32 che causerà l'arresto anomalo dell'applicazione se non viene gestito in try catch + è contrassegnato con l'attributo [HandleProcessCorruptedStateExceptions], ecc. Come descritto nell'articolo. –
Sono stato corretto. Questo funziona :-D. Ho fatto un errore nel mio test (tipico). Grazie mille :). –