简体   繁体   English

隧道接口单元测试

[英]Tunnel interface unit-testing

I've made a Tun class that wraps pytun.TunTapDevice : 我做了一个包装pytun.TunTapDevice的Tun类:

from pytun import TunTapDevice

class Tun(object):
    def __init__(self,name='tun',addr=None,dstaddr=None,netmask=None,mtu=None):

        tun = TunTapDevice(name=name)
        if addr     : tun.addr      = addr
        if dstaddr  : tun.dstaddr   = dstaddr
        if netmask  : tun.netmask   = netmask
        if mtu      : tun.mtu       = mtu
        self._tun = tun
        self.up = self._tun.up
        self.down = self._tun.down
        self.read = self._tun.read
        self.write = self._tun.write
        self.close = self._tun.close


    @property
    def name(self):
        return self._tun.name

    @property
    def mtu(self):
        return self._tun.mtu

The question is not about how to write a tunnel, but about how to write a test-case to ensure it works properly in unix-like oses using python unit-testing. 问题不是关于如何编写隧道,而是关于如何编写测试用例,以确保使用Python单元测试在类似Unix的oses中正常工作。

What should I write out to it to ensure is works? 我应该写些什么以确保其正常工作? Maybe an ARP request, ICMP, DNS packet or anything else: 可能是ARP请求,ICMP,DNS数据包或其他任何内容:

class TestTunnel(unittest.TestCase):

    def setUp(self):
        self.tun = Tun(name='tun0', addr='192.168.0.23', netmask='255.255.255.0',mtu=1500)

    def test_tunnel(self):
        self.tun.write(?????)
        self.assertEqual(self.tun.read(),????)

EDIT 1: 编辑1:

finally i got it by this code: 最终我通过以下代码得到了它:

from select import select
import dpkt
import struct

class TunnelTestCase( unittest.TestCase):

    def setUp(self):

        self.tun = Tun(name='testtun',
                       addr='192.168.6.92',
                       dstaddr='192.168.6.93',
                       netmask='255.255.255.0', 
                       mtu=1500)
        self.tun.up()

    def _ip2str(self,ip):
        return '.'.join([str(i) for i in struct.unpack('!BBBB',ip)])

    def test_echo(self):
        reqPack = dpkt.ip.IP('E\x00\x00T\x00\x00@\x00@\x01\xac\x9f\xc0\xa8\x06]\xc0\xa8\x06\\\x08\x00\x1c\xae\t\xc7\x00\x01\x0f\x8adQq\xab\x01\x00\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./01234567')
        self.tun.write(reqPack.pack())

        r,_w,_ex = select([self.tun],[],[],4)
        if len(r) and r[0] == self.tun:
            replyPack = dpkt.ip.IP(self.tun.read())
            self.assertEqual(self._ip2str(replyPack.src), self.tun.addr)
            self.assertEqual(self._ip2str(replyPack.dst), self.tun.dstaddr)
            return

        self.assert_(False, 'ICMP Echo timeout, the tunnel interface may not works properly in your system.')

You may simply be able to borrow the test cases from pytun and simplify them. 您可能仅能从pytun借用测试用例并简化它们。 In fact, I think that testing actual connectivity is done in their package, so unless you are trying to test something significantly different, you might be able to get away with just running there tests without modification. 实际上,我认为测试实际的连通性是在他们的程序包中完成的,因此,除非您尝试测试明显不同的东西,否则您可能无需进行任何修改就可以运行那里的测试。 https://github.com/montag451/pytun/tree/master/test https://github.com/montag451/pytun/tree/master/test

Since it's a raw socket, you can simply try to send a plain ascii message from the client and verify it is received by the server, and then have the server send back an acknowledgement, which you can assert against. 由于它是原始套接字,因此您可以简单地尝试从客户端发送纯ascii消息并验证它是否已被服务器接收,然后让服务器发送回确认,您可以对此进行断言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM