hydrus/include/TestHydrusNATPunch.py

43 lines
1.7 KiB
Python
Raw Normal View History

2019-01-09 22:59:03 +00:00
from . import ClientConstants as CC
from . import HydrusConstants as HC
from . import HydrusNATPunch
2014-04-16 20:31:59 +00:00
import os
2016-03-30 22:56:50 +00:00
import random
2014-04-16 20:31:59 +00:00
import time
import unittest
class TestNATPunch( unittest.TestCase ):
2014-04-23 20:56:12 +00:00
def test_upnp( self ):
2014-04-16 20:31:59 +00:00
2014-04-23 20:56:12 +00:00
internal_client = HydrusNATPunch.GetLocalIP()
2016-03-30 22:56:50 +00:00
internal_port = random.randint( 1000, 1500 )
2014-04-23 20:56:12 +00:00
2016-03-30 22:56:50 +00:00
external_port = random.randint( 1000, 1500 )
2014-04-23 20:56:12 +00:00
description_tcp = 'hydrus test tcp'
description_udp = 'hydrus test udp'
HydrusNATPunch.AddUPnPMapping( internal_client, internal_port, external_port, 'TCP', description_tcp )
HydrusNATPunch.AddUPnPMapping( internal_client, internal_port, external_port, 'UDP', description_udp )
mappings = HydrusNATPunch.GetUPnPMappings()
mappings_without_lease_times = [ mapping[:-1] for mapping in mappings ]
2019-05-22 22:35:06 +00:00
self.assertIn( ( description_tcp, internal_client, internal_port, external_port, 'TCP' ), mappings_without_lease_times )
self.assertIn( ( description_udp, internal_client, internal_port, external_port, 'UDP' ), mappings_without_lease_times )
2014-04-23 20:56:12 +00:00
HydrusNATPunch.RemoveUPnPMapping( external_port, 'TCP' )
HydrusNATPunch.RemoveUPnPMapping( external_port, 'UDP' )
mappings = HydrusNATPunch.GetUPnPMappings()
mappings_without_lease_times = [ mapping[:-1] for mapping in mappings ]
2019-05-22 22:35:06 +00:00
self.assertNotIn( ( description_tcp, internal_client, internal_port, external_port, 'TCP' ), mappings_without_lease_times )
self.assertNotIn( ( description_udp, internal_client, internal_port, external_port, 'UDP' ), mappings_without_lease_times )
2014-04-23 20:56:12 +00:00
2019-01-09 22:59:03 +00:00