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

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

问题描述

我需要编写一个测试来验证我的代码可以处理AccessViolationException(或任何其他WIN32损坏状态异常-CSE),该错误通常在不安全的上下文中发生,通常是通过调用第三方lib。
这应该全部在.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 ,其中介绍了如何

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

A测试:

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

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

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天全站免登陆