mercredi 23 septembre 2015

C# mutantion testing - change method runtime with il code

You can skip to my approach if you don't mind what I'm actually trying to do.

What I'm trying to do
Hey I'm trying to make mutant testing,
inspired by the talk
http://ift.tt/1iMb6qv

But I'm using c# and the best mutant libraries are made in java such as
http://pitest.org/

There are some frameworks for c# such as ninjaturtles and visualmutator, but they both doesn't work in my computer for some reason(I get a weird error). and also I thought it would be interesting creating my own.

About mutant testing
For those who doesn't know what is mutant testing, it's a testing for the tests, most people use code coverage to check that their test cover all the scenarios, but it's not enough, cause just because it gets to a piece of code doesn't mean it tests it. It changes a piece of code, and if the tests still pass it means you didn't tested the piece of code.

My approach
So I've tried starting with a simple code that gets the il codes of a method.

        var classType = typeof(MethodClass);
        var methodInfo = classType.GetMethod("ExecuteMethod", BindingFlags.NonPublic | BindingFlags.Static);

        byte[] ilCodes = methodInfo.GetMethodBody().GetILAsByteArray();

this is the MethodClass I'm trying to change:

public class MethodClass
{
    private static int ExecuteMethod()
    {
        var i = 0;
        i += 5;
        if (i >= 5)
        {
            i = 2;
        }
        return i;
    }
}

now I'm trying to replace the ils

        for (int i = 0; i < ilCodes.Length; i++)
        {
            if (ilCodes[i] == OpCodes.Add.Value)
            {
                ilCodes[i] = (byte)OpCodes.Sub.Value;
            }
        }

but then I'm not sure how to update my function to work with the new il codes.

I've tried using

        var dynamicFunction = new DynamicMethod("newmethod", typeof(int), null);
        var ilGenerator = dynamicFunction.GetILGenerator();

and then the il generator has a function emit, that gets operator and value, so I could use this. but I don't have the value to put in the emit..

Does anybody know how to do it?





Aucun commentaire:

Enregistrer un commentaire