如何测试 AccessViolationException 的处理 [英] How to test handling of AccessViolationException

查看:39
本文介绍了如何测试 AccessViolationException 的处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个测试来验证我的代码是否可以处理 AccessViolationException(或任何其他 WIN32 损坏状态异常 - CSE),该异常发生在不安全的上下文中,通常是通过调用 3rd 方库.这一切都应该在 .net 4.0 上使用 C# 完成.

I need to write a test which verifies that my code can handle an AccessViolationException (or any other WIN32 Corrupted State Exception - CSE), which occours in an unsafe context, typically by calling a 3rd party lib. This should all be done using C# on .net 4.0.

我发现了这个相关问题如何处理 AccessViolationException 和这篇相关文章 http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx,解释了如何捕捉这些 CSE 及其背景.

I found this related question How to handle AccessViolationException and this related article http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx, which explains how to catch these CSE's and their background.

所以我想在测试中激发一个 WIN32 CSE,以确保在我的应用程序中正确处理.类似的东西:

So i would like to provoke a WIN32 CSE in a test, to ensure correct handling in my application. Something like:

一些要测试的示例类:

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
        }
    }
}

测试:

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());
    }
}

有没有人建议如何在不做太多工作的情况下实现这一目标(例如,编写导致此异常的不安全库).

Does anyone have a suggestion of how to achive this without to much work (ex. writting a unsafe lib which causes this exception).

亲切的问候

更新:为了匹配我的最终解决方案,感谢 JaredPar.

UPDATED: To match my final solution, thanks to 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
        }
    }
}

提示:要手动验证这一点,请使用一个简单的控制台应用程序,从命令行:

TIP: To verify this manually use a simple console app, from the commandline:

class Program
{
    static void Main(string[] args)
    {
        var handler = new MyExceptionHandler();

        if (args.Length > 1)
        {
            handler.HandleCorruptedStateException();
        }
        else
        {
            handler.DoesNotHandleCorruptedStateException();
        }
    }
}

推荐答案

试试下面的

var ptr = new IntPtr(42);
Marshal.StructureToPtr(42, ptr, true);

CLI 规范不保证这会抛出 AccessViolationException,但它会在我知道的每个平台上

This isn't guaranteed to throw an AccessViolationException by the CLI spec but it will on every platform I'm aware of

这篇关于如何测试 AccessViolationException 的处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆