Tunnel interface unit-testing
I've made a Tun class that wraps pytun.TunTapDevice :
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.
What should I write out to it to ensure is works? Maybe an ARP request, ICMP, DNS packet or anything else:
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(),????)
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 == 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.')
Asked by: Sydney801 | Posted: 06-12-2021
You may simply be able to borrow the test cases from pytun and simplify them. 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
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.Answered by: Julian260 | Posted: 07-01-2022
Python, unit-testing and mocking imports
I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests? As an example: The file with the functions I want to test, imports ten other files which is part of our software and not pytho...
Python unit-testing with nose: Making sequential tests
I am just learning how to do unit-testing. I'm on Python / nose / Wing IDE. (The project that I'm writing tests for is a simulations framework, and among other things it lets you run simulations both synchronously and asynchronously, and the results of the simulation should be the same in both.) The thing is, I want some of my tests to use simulat...
python - Unit-testing extensions for an 'external' program with pyunit
I'm struggling to know where to start with unittest, having read the dive-into-python tutorial and looked at http://pyunit.sourceforge.net/. I've got a piece of analysis software (call it 'prog.exe') which uses python for its input decks. I've started writing a python module which I'm going to import from that input deck to provide some useful functionali...
python - Flask blueprint unit-testing
Is there a good practice to unit-test a flask blueprint? http://flask.pocoo.org/docs/testing/ I didn't found something that helped me or that is simple enough. // Edit Here are my code: # -*- coding: utf-8 -*- import sys import os import unittest import flask sys.path = [os.path.abspath('')] + sys.path from ap...
python - Django unit-testing client.login not working
I am using django 1.6 and I have the following test code: def tes_stuff(self): new_user=EndUser.objects.create(username="test", firstname="test", email="firstname.lastname@example.org", password="test") self.assertTrue(self.client.login(username="test", password="test")) When I run it I get an arror that says: AssertionError: False is not true I am unsure why it is not l...
Is there a preferred BDD style unit-testing framework for Python?
I was wondering if there are any BDD-style 'describe-it' unit-testing frameworks for Python that are maintained and production ready. I have found describe, but it doesn't seem to be maintained and has no documentation. I've also found sure which reached 1.0, but it seems to just add syntactic sugar i...
Python Unit-Testing All Test Cases
I am running unit tests on pycharm but some how am discovering something weird. When I started testing a class, more specifically its methods, I simply wrote test cases like the following: # hey.py class hey: def hello(self): return True def bye(self): return 'Bye' # test_hey.py from unittest import TestCase class TestHey(TestCase): def test_hello(self): self.fail() ...
python - Unit-Testing Flask application views & dialogs
I am new to Flask and have recently started working on a project. After I've fixed some issues related to dialogs and views the client asked to write unittests to verify the resolutions. I've gone through some already written tests but they mostly just check the status code of http response, like this: def test_home_page(self): rv = self.client.get('/home') self.assertEqual(rv._status_code, 200, rv....
What's the right way of unit-testing a python class?
I'm not sure what's the best way to build independent unit tests on methods of a python class. Here is a trivial example: class MyClass(object): def __init__(self, data): self.data = data def myMethod(self): #do something return True I'm building unit tests like this: class TestMyClass(unittest.TestCase): def test_init(self): mydata = mock.Mock() ...
django - Unit-testing Python: Mocking function calls inside function
I have a django view like this # Django view from some_module import f2 def f1(request, version): # some code f2(**kargs) # more code return HTTPResponse(response) The function f2 is in another module # some_module def f2(**kargs): # some code The Django view is part of an API so, the request and response are in json How do I...