<s> from ipaddress import ip_network <EOL> from twisted . internet . protocol import ServerFactory <EOL> from twisted . internet . protocol import Protocol <EOL> from twisted . internet . address import IPv4Address <EOL> from twisted . python import log as twisted_log <EOL> from twisted . trial import unittest <EOL> from comet . utility import WhitelistingFactory <EOL> from comet . testutils import DummyLogObserver <EOL> class TestFactory ( ServerFactory ) : <EOL> protocol = Protocol <EOL> class WhitelistingFactoryTestCase ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . observer = DummyLogObserver ( ) <EOL> twisted_log . addObserver ( self . observer ) <EOL> def tearDown ( self ) : <EOL> twisted_log . removeObserver ( self . observer ) <EOL> def test_empty_whitelist ( self ) : <EOL> factory = WhitelistingFactory ( TestFactory ( ) , [ ] ) <EOL> self . assertEqual ( <EOL> factory . buildProtocol ( IPv4Address ( '<STR_LIT>' , '<STR_LIT:127.0.0.1>' , <NUM_LIT:0> ) ) , <EOL> None <EOL> ) <EOL> self . assertEqual ( len ( self . observer . messages ) , <NUM_LIT:1> ) <EOL> self . assertTrue ( "<STR_LIT>" in self . observer . messages [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> def test_in_whitelist ( self ) : <EOL> factory = WhitelistingFactory ( TestFactory ( ) , [ ip_network ( '<STR_LIT>' ) ] ) <EOL> self . assertIsInstance ( <EOL> factory . buildProtocol ( IPv4Address ( '<STR_LIT>' , '<STR_LIT:127.0.0.1>' , <NUM_LIT:0> ) ) , <EOL> Protocol <EOL> ) <EOL> self . assertEqual ( len ( self . observer . messages ) , <NUM_LIT:0> ) <EOL> def test_not_in_whitelist ( self ) : <EOL> factory = WhitelistingFactory ( TestFactory ( ) , [ ip_network ( '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( <EOL> factory . buildProtocol ( IPv4Address ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ) ) , <EOL> None <EOL> ) <EOL> def test_log_message ( self ) : <EOL> TEST_STRING = "<STR_LIT>" <EOL> factory = WhitelistingFactory ( <EOL> TestFactory ( ) , [ ip_network ( '<STR_LIT>' ) ] , TEST_STRING <EOL> ) <EOL> self . assertEqual ( <EOL> factory . buildProtocol ( IPv4Address ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:0> ) ) , <EOL> None <EOL> ) <EOL> self . assertFalse ( "<STR_LIT>" in self . observer . messages [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( TEST_STRING in self . observer . messages [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) </s>
