如何用 xUnit 测试 byte[] 数组?

How to test byte[] array with xUnit?

我是 C# 单元测试和 xUnit 的新手。测试 byte[] 数组的正确方法是什么?

我正在尝试测试原始数据包以再次反对原始数据包。

[Fact]
public void DiscoverTest()
{
    // DHCP Discover packet (#1) from 
    // https://wiki.wireshark.org/DHCP
    // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=view&target=dhcp.pcap
    // Ethernet and UDP metadata stripped
    byte[] b = new byte[]
    {
       0x01, 0x01, 0x06, 0x00, 0x00, 0x00, 0x3d, 0x1d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x0b, 0x82, 0x01, 0xfc, 0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82,
       0x53, 0x63, 0x35, 0x01, 0x01, 0x3d, 0x07, 0x01, 0x00, 0x0b, 0x82, 0x01, 0xfc, 0x42,
       0x32, 0x04, 0x00, 0x00, 0x00, 0x00, 0x37, 0x04, 0x01, 0x03, 0x06, 0x2a, 0xff, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0x00
    };

    // raw byte[] to object's properties etc
    DHCPPacket p = new DHCPPacket(b);

    // p.GetRawBytes() object properties etc to raw byte[]
    Assert.Equal<byte[]>(b, p.GetRawBytes());
}

这给出:

Assert.Equal() Failure
Expected: Byte[] [1, 1, 6, 0, 0, ...]
Actual:   Byte[] [1, 1, 6, 0, 0, ...]

所以我看不出到底是哪个offset数据错了。

正确的方法是什么?

我正在使用 xUnit 2.2.0。

XUnit 中的单元测试在第一个断言时失败。如果你首先声明长度,你将能够分辨出哪个长度是关闭的。

Assert.Equal(b.Length, p.GetRawBytes().Length);
Assert.Equal<byte[]>(b, p.GetRawBytes());

Assert.Equal 还有一个可选的第三个参数,当测试失败时打印。所以你可以说:

Assert.Equal(b.Length, p.GetRawBytes().Length, $"Byte array lengths different. Expected: {b.length}, Actual: {p.GetRawBytes().Length}");

因此正如评论中指出的那样,数组必须迭代:

byte[] pb = new DHCPPacket(b).GetRawBytes();

for (int i = 0; i < b.Length; i++)
{
    byte expected = b[i];
    byte actual = pb[i];

    Assert.True(expected == actual, 
        String.Format("Expected: '{0}', Actual: '{1}' at offset {2}.", (byte)expected, (byte)actual, i)
    );
}