def test_automaticDynamicPowerDown(self): """ Powerups with '__getPowerupInterfaces__' methods can be powered down automatically on the interfaces they specify. """ s = Store() p = PlusOneTimesFour(store=s) s.powerUp(p) s.powerDown(p) self.assertEqual(len(list(s.powerupsFor(IValueHaver))), 0) self.assertEqual(len(list(s.powerupsFor(IScalingFactor))), 0)
def test_automaticDynamicPowerDown(self): """ Powerups with '__getPowerupInterfaces__' methods can be powered down automatically on the interfaces they specify. """ s = Store() p = PlusOneTimesFour(store=s) s.powerUp(p) s.powerDown(p) self.assertEquals(len(list(s.powerupsFor(IValueHaver))), 0) self.assertEquals(len(list(s.powerupsFor(IScalingFactor))), 0)
def test_powerItUp(self): """ Powering up a store with an C{AboutPlugin} results in it being installed as an L{ISiteRootPlugin} powerup. """ s = Store() ap = self.aboutpage.AboutPlugin(store=s) s.powerUp(ap) self.assertEquals([ap], list(s.powerupsFor(ISiteRootPlugin)))
def test_factoryPowerup(self): """ When installed, C{self.factoryClass} is a powerup for L{IBoxReceiverFactory}. """ store = Store() factory = self.factoryClass(store=store) installOn(factory, store) self.assertEqual(list(store.powerupsFor(IBoxReceiverFactory)), [factory])
class AMPConfigurationTests(TestCase): """ Tests for L{xmantissa.ampserver.AMPConfiguration} which defines how to create an L{AMP} server. """ def setUp(self): """ Create an in-memory L{Store} with an L{AMPConfiguration} in it. """ self.store = Store() self.conf = AMPConfiguration(store=self.store) installOn(self.conf, self.store) def test_interfaces(self): """ L{AMPConfiguration} implements L{IProtocolFactoryFactory}. """ self.assertTrue(verifyObject(IProtocolFactoryFactory, self.conf)) def test_powerup(self): """ L{ionstallOn} powers up the target for L{IProtocolFactoryFactory} with L{AMPConfiguration}. """ self.assertIn(self.conf, list(self.store.powerupsFor(IProtocolFactoryFactory))) def test_getFactory(self): """ L{AMPConfiguration.getFactory} returns a L{ServerFactory} instance which returns L{CredReceiver} instances from its C{buildProtocol} method. """ factory = self.conf.getFactory() self.assertTrue(isinstance(factory, ServerFactory)) protocol = factory.buildProtocol(None) self.assertTrue(isinstance(protocol, CredReceiver)) def test_generateOneTimePad(self): """ L{AMPConfiguration.generateOneTimePad} returns a one-time pad. """ object.__setattr__(self.conf, 'callLater', lambda x, y: None) pad = self.conf.generateOneTimePad(self.store) self.assertNotEqual(pad, self.conf.generateOneTimePad(self.store)) def test_oneTimePadExpires(self): """ L{AMPConfiguration.generateOneTimePad} should expire its pad. """ def callLater(seconds, f): self.assertEqual(seconds, self.conf.ONE_TIME_PAD_DURATION) f() object.__setattr__(self.conf, 'callLater', callLater) pad = self.conf.generateOneTimePad(self.store) self.assertFalse(pad in self.conf._oneTimePads)
def test_powerUp(self): """ L{Calendar} is an L{IMessageReceiver} powerup. """ store = Store() calendar = self.cal.Calendar(store=store) self.assertTrue(verifyObject(IMessageReceiver, calendar)) store.powerUp(calendar) self.assertEquals(list(store.powerupsFor(IMessageReceiver)), [calendar])
def test_automaticPowerDown(self): """ Powerups with 'powerupInterfaces' attributes can be powered down automatically on the interfaces they specify. """ s = Store() p = PlusTwo(store=s) s.powerUp(p) s.powerDown(p) self.assertEquals(len(list(s.powerupsFor(IValueHaver))), 0)
def test_factoryPowerup(self): """ When installed, C{self.factoryClass} is a powerup for L{IBoxReceiverFactory}. """ store = Store() factory = self.factoryClass(store=store) installOn(factory, store) self.assertEqual( list(store.powerupsFor(IBoxReceiverFactory)), [factory])
def test_powerUp(self): """ L{Calendar} is an L{IMessageReceiver} powerup. """ store = Store() calendar = self.cal.Calendar(store=store) self.assertTrue(verifyObject(IMessageReceiver, calendar)) store.powerUp(calendar) self.assertEquals( list(store.powerupsFor(IMessageReceiver)), [calendar])
def test_automaticPowerDown(self): """ Powerups with 'powerupInterfaces' attributes can be powered down automatically on the interfaces they specify. """ s = Store() p = PlusTwo(store=s) s.powerUp(p) s.powerDown(p) self.assertEqual(len(list(s.powerupsFor(IValueHaver))), 0)
def testNoIndirectedIndirection(self): """ Because it is a special interface in the powerup system, you can't have powerups for IPowerupIndirector; there's no sensible thing that could mean other than an infinite loop. Let's make sure that both looking for IPowerupIndirector and attempting to install a powerup for it will fail appropriately. """ s = Store() s3 = SubtractThree(store=s) self.assertRaises(TypeError, s.powerUp, s3, IPowerupIndirector) self.assertEqual(list(s.powerupsFor(IPowerupIndirector)), [])
def _siteRequirementTest(self, offering, cls): """ Verify that installing C{offering} results in an instance of the given Item subclass being installed as a powerup for IProtocolFactoryFactory. """ store = Store() ixmantissa.IOfferingTechnician(store).installOffering(offering) factories = list(store.powerupsFor(ixmantissa.IProtocolFactoryFactory)) for factory in factories: if isinstance(factory, cls): break else: self.fail("No instance of %r in %r" % (cls, factories))
class PortTestsMixin: """ Test method-defining mixin class for port types with C{portNumber} and C{factory} attributes. Included are tests for various persistence-related behaviors as well as the L{IService} implementation which all ports should have. @ivar portType: The L{Item} subclass which will be tested. @ivar lowPortNumber: A port number which requires privileges to bind on POSIX. Used to test L{privilegedStartService}. @ivar highPortNumber: A port number which does not require privileges to bind on POSIX. Used to test the interaction between L{privilegedStartService} and L{startService}. @ivar dbdir: The path at which to create the test L{Store}. This must be bound before L{setUp} is run, since that is the only method which examines its value. @ivar ports: A list of ports which have been bound using L{listen}. created in L{setUp}. """ portType = None lowPortNumber = 123 highPortNumber = 1234 someInterface = u'127.0.0.1' def port(self, **kw): """ Create and return a new port instance with the given attribute values. """ return self.portType(**kw) def listen(self, *a, **kw): """ Pretend to bind a port. Used as a stub implementation of a reactor listen method. Subclasses should override and implement to append useful information to C{self.ports}. """ raise NotImplementedError() def checkPort(self, port, alternatePort=None): """ Assert that the given port has been properly created. @type port: L{DummyPort} @param port: A port which has been created by the code being tested. @type alternatePort: C{int} @param alternatePort: If not C{None}, the port number on which C{port} should be listening. """ raise NotImplementedError() def setUp(self): self.filesdir = self.mktemp() self.store = Store(filesdir=self.filesdir) self.realFactory = ServerFactory() self.factory = DummyFactory(store=self.store, realFactory=self.realFactory) self.ports = [] def test_portNumberAttribute(self): """ Test that C{self.portType} remembers the port number it is told to listen on. """ port = self.port(store=self.store, portNumber=self.lowPortNumber) self.assertEqual(port.portNumber, self.lowPortNumber) def test_interfaceAttribute(self): """ Test that C{self.portType} remembers the interface it is told to listen on. """ port = self.port(store=self.store, interface=self.someInterface) self.assertEqual(port.interface, self.someInterface) def test_factoryAttribute(self): """ Test that C{self.portType} remembers the factory it is given to associate with its port. """ port = self.port(store=self.store, factory=self.factory) self.assertIdentical(port.factory, self.factory) def test_service(self): """ Test that C{self.portType} becomes a service on the store it is installed on. """ port = self.port(store=self.store) installOn(port, self.store) self.assertEqual( list(self.store.powerupsFor(IService)), [port]) def test_setServiceParent(self): """ Test that the C{self.portType.setServiceParent} method adds the C{self.portType} to the Axiom Store Service as a child. """ port = self.port(store=self.store) port.setServiceParent(self.store) self.failUnlessIn(port, list(IService(self.store))) def test_disownServiceParent(self): """ Test that the C{self.portType.disownServiceParent} method removes the C{self.portType} from the Axiom Store Service. """ port = self.port(store=self.store) port.setServiceParent(self.store) port.disownServiceParent() self.failIfIn(port, list(IService(self.store))) def test_serviceParent(self): """ Test that C{self.portType} is a child of the store service after it is installed. """ port = self.port(store=self.store) installOn(port, self.store) service = IServiceCollection(self.store) self.failUnlessIn(port, list(service)) def _start(self, portNumber, methodName): port = self.port(store=self.store, portNumber=portNumber, factory=self.factory) port._listen = self.listen getattr(port, methodName)() return self.ports def _privilegedStartService(self, portNumber): return self._start(portNumber, 'privilegedStartService') def _startService(self, portNumber): return self._start(portNumber, 'startService') def test_startPrivilegedService(self): """ Test that C{self.portType} binds a low-numbered port with the reactor when it is started with privilege. """ ports = self._privilegedStartService(self.lowPortNumber) self.assertEqual(len(ports), 1) self.checkPort(ports[0]) def test_dontStartPrivilegedService(self): """ Test that C{self.portType} doesn't bind a high-numbered port with the reactor when it is started with privilege. """ ports = self._privilegedStartService(self.highPortNumber) self.assertEqual(ports, []) def test_startServiceLow(self): """ Test that C{self.portType} binds a low-numbered port with the reactor when it is started without privilege. """ ports = self._startService(self.lowPortNumber) self.assertEqual(len(ports), 1) self.checkPort(ports[0]) def test_startServiceHigh(self): """ Test that C{self.portType} binds a high-numbered port with the reactor when it is started without privilege. """ ports = self._startService(self.highPortNumber) self.assertEqual(len(ports), 1) self.checkPort(ports[0], self.highPortNumber) def test_startServiceNoInterface(self): """ Test that C{self.portType} binds to all interfaces if no interface is explicitly specified. """ port = self.port(store=self.store, portNumber=self.highPortNumber, factory=self.factory) port._listen = self.listen port.startService() self.assertEqual(self.ports[0].interface, '') def test_startServiceInterface(self): """ Test that C{self.portType} binds to only the specified interface when instructed to. """ port = self.port(store=self.store, portNumber=self.highPortNumber, factory=self.factory, interface=self.someInterface) port._listen = self.listen port.startService() self.assertEqual(self.ports[0].interface, self.someInterface) def test_startedOnce(self): """ Test that C{self.portType} only binds one network port when C{privilegedStartService} and C{startService} are both called. """ port = self.port(store=self.store, portNumber=self.lowPortNumber, factory=self.factory) port._listen = self.listen port.privilegedStartService() self.assertEqual(len(self.ports), 1) self.checkPort(self.ports[0]) port.startService() self.assertEqual(len(self.ports), 1) def test_stopService(self): """ Test that C{self.portType} cleans up its listening port when it is stopped. """ port = self.port(store=self.store, portNumber=self.lowPortNumber, factory=self.factory) port._listen = self.listen port.startService() stopped = port.stopService() stopping = self.ports[0].stopping self.failIfIdentical(stopping, None) self.assertIdentical(stopped, stopping) def test_deletedFactory(self): """ Test that the deletion of a C{self.portType}'s factory item results in the C{self.portType} being deleted. """ port = self.port(store=self.store, portNumber=self.lowPortNumber, factory=self.factory) self.factory.deleteFromStore() self.assertEqual(list(self.store.query(self.portType)), []) def test_deletionDisownsParent(self): """ Test that a deleted C{self.portType} no longer shows up in the children list of the service which used to be its parent. """ port = self.port(store=self.store, portNumber=self.lowPortNumber, factory=self.factory) port.setServiceParent(self.store) port.deleteFromStore() service = IServiceCollection(self.store) self.failIfIn(port, list(service))
class SecureShellConfigurationTests(TestCase): """ Tests for L{xmantissa.shell.SecureShellConfiguration} which defines how to create an SSH server. """ _hostKey = ( "-----BEGIN RSA PRIVATE KEY-----\n" "MIIByAIBAAJhAM/dftm59mJJ1JVy0bsq8J7fp4WUecgaJukRyf637d76ywxRYGdw\n" "47hkBiJaDYgaE9HMlh2eSow3b2YCyom4FLlh/7Buq58A9IofR7ZiNVYv0ZDppbDg\n" "FN+Gl2ZFLFB3dwIBIwJgC+DFa4b4It+lv2Wllaquqf4m1G7iYzSxxCzm+JzLw5lN\n" "bmsM0rX+Yk7bx3LcM6m34vyvhY6p/kQyjHo7/CkpaSQg4bnpOcqEq3oMf8E0c0lp\n" "TQ1TdtfnKKrZZPTaVr7rAjEA7O19/tSLK6by1BpE1cb6W07GK1WcafYLxQLT64o+\n" "GKxbrlsossc8gWJ8GDRjE2S5AjEA4JkYfYkgfucH941r9yDFrhr6FuOdwbLXDESZ\n" "DyLhW/7DHiVIXlaLFnY+51PcTwWvAjBzFESDFsdBFpMz0j6w+j8WaBccXMhQuVYs\n" "fbdjxs20NnWsdWuKCQAhljxGRVSxpfMCMBmrGL3jyTMTFtp2j/78bl0KZbl5GVf3\n" "LoUPJ29xs1r4i1PnAPTWsM9d+I93TGDNcwIxAMRz4KO02tiLXG2igwDw/WWszrkr\n" "r4ggaFDlt4QqoNz0l4tayqzbDV1XceLgP4cXcQ==\n" "-----END RSA PRIVATE KEY-----\n") def setUp(self): """ Create an in-memory L{Store} with a L{SecureShellConfiguration} in it. """ self.store = Store() self.shell = SecureShellConfiguration(store=self.store, hostKey=self._hostKey) installOn(self.shell, self.store) def test_interfaces(self): """ L{SecureShellConfiguration} implements L{IProtocolFactoryFactory}. """ self.assertTrue(verifyObject(IProtocolFactoryFactory, self.shell)) def test_powerup(self): """ L{installOn} powers up the target for L{IProtocolFactoryFactory} with L{SecureShellConfiguration}. """ self.assertIn(self.shell, list(self.store.powerupsFor(IProtocolFactoryFactory))) def test_repr(self): """ The result of C{repr} on a L{SecureShellConfiguration} instance includes only a fingerprint of the private key, not the entire value. """ self.assertEqual( repr(self.shell), "SecureShellConfiguration(storeID=%d, " % (self.shell.storeID, ) + "hostKeyFingerprint='68cc7060bb6394060672467e7c4d8f3b')") def assertHostKey(self, shell, factory): """ Assert that the public and private keys provided by C{factory} match those specified by C{shell} and that they are L{Key} instances. """ privateKey = Key.fromString(shell.hostKey) self.assertEqual(factory.publicKeys, {'ssh-rsa': privateKey.public()}) self.assertEqual(factory.privateKeys, {'ssh-rsa': privateKey}) def test_getFactory(self): """ L{SecureShellConfiguration.getFactory} returns an L{SSHFactory} with keys from L{SecureShellConfiguration.hostKey}. """ factory = self.shell.getFactory() self.assertHostKey(self.shell, factory) def test_keyGeneration(self): """ L{SecureShellConfiguration} generates its own key pair if one is not supplied to C{__init__}. """ store = Store() shell = SecureShellConfiguration(store=store) installOn(shell, store) factory = shell.getFactory() self.assertHostKey(shell, factory) def test_keyRotation(self): """ L{SecureShellConfiguration.rotate} generates a new key pair replacing the old one. """ oldKey = self.shell.hostKey self.shell.rotate() newKey = self.shell.hostKey self.assertNotEqual(oldKey, newKey) factory = self.shell.getFactory() self.assertHostKey(self.shell, factory) def test_portal(self): """ The factory returned by L{SecureShellConfiguration.getFactory} has a C{portal} attribute which allows logins authenticated in the usual L{axiom.userbase} manner. """ localpart = u'foo bar' domain = u'example.com' password = u'baz quux' loginSystem = self.store.findUnique(LoginSystem) account = loginSystem.addAccount(localpart, domain, password, internal=True) subStore = account.avatars.open() avatar = object() subStore.inMemoryPowerUp(avatar, IConchUser) factory = self.shell.getFactory() login = factory.portal.login( UsernamePassword( '%s@%s' % (localpart.encode('ascii'), domain.encode('ascii')), password), None, IConchUser) def cbLoggedIn(result): self.assertIdentical(IConchUser, result[0]) self.assertIdentical(avatar, result[1]) login.addCallback(cbLoggedIn) return login
class SecureShellConfigurationTests(TestCase): """ Tests for L{xmantissa.shell.SecureShellConfiguration} which defines how to create an SSH server. """ _hostKey = ( "-----BEGIN RSA PRIVATE KEY-----\n" "MIIByAIBAAJhAM/dftm59mJJ1JVy0bsq8J7fp4WUecgaJukRyf637d76ywxRYGdw\n" "47hkBiJaDYgaE9HMlh2eSow3b2YCyom4FLlh/7Buq58A9IofR7ZiNVYv0ZDppbDg\n" "FN+Gl2ZFLFB3dwIBIwJgC+DFa4b4It+lv2Wllaquqf4m1G7iYzSxxCzm+JzLw5lN\n" "bmsM0rX+Yk7bx3LcM6m34vyvhY6p/kQyjHo7/CkpaSQg4bnpOcqEq3oMf8E0c0lp\n" "TQ1TdtfnKKrZZPTaVr7rAjEA7O19/tSLK6by1BpE1cb6W07GK1WcafYLxQLT64o+\n" "GKxbrlsossc8gWJ8GDRjE2S5AjEA4JkYfYkgfucH941r9yDFrhr6FuOdwbLXDESZ\n" "DyLhW/7DHiVIXlaLFnY+51PcTwWvAjBzFESDFsdBFpMz0j6w+j8WaBccXMhQuVYs\n" "fbdjxs20NnWsdWuKCQAhljxGRVSxpfMCMBmrGL3jyTMTFtp2j/78bl0KZbl5GVf3\n" "LoUPJ29xs1r4i1PnAPTWsM9d+I93TGDNcwIxAMRz4KO02tiLXG2igwDw/WWszrkr\n" "r4ggaFDlt4QqoNz0l4tayqzbDV1XceLgP4cXcQ==\n" "-----END RSA PRIVATE KEY-----\n") def setUp(self): """ Create an in-memory L{Store} with a L{SecureShellConfiguration} in it. """ self.store = Store() self.shell = SecureShellConfiguration( store=self.store, hostKey=self._hostKey) installOn(self.shell, self.store) def test_interfaces(self): """ L{SecureShellConfiguration} implements L{IProtocolFactoryFactory}. """ self.assertTrue(verifyObject(IProtocolFactoryFactory, self.shell)) def test_powerup(self): """ L{installOn} powers up the target for L{IProtocolFactoryFactory} with L{SecureShellConfiguration}. """ self.assertIn( self.shell, list(self.store.powerupsFor(IProtocolFactoryFactory))) def test_repr(self): """ The result of C{repr} on a L{SecureShellConfiguration} instance includes only a fingerprint of the private key, not the entire value. """ self.assertEqual( repr(self.shell), "SecureShellConfiguration(storeID=%d, " % (self.shell.storeID,) + "hostKeyFingerprint='68cc7060bb6394060672467e7c4d8f3b')") def assertHostKey(self, shell, factory): """ Assert that the public and private keys provided by C{factory} match those specified by C{shell} and that they are L{Key} instances. """ privateKey = Key.fromString(shell.hostKey) self.assertEqual( factory.publicKeys, {'ssh-rsa': privateKey.public()}) self.assertEqual(factory.privateKeys, {'ssh-rsa': privateKey}) def test_getFactory(self): """ L{SecureShellConfiguration.getFactory} returns an L{SSHFactory} with keys from L{SecureShellConfiguration.hostKey}. """ factory = self.shell.getFactory() self.assertHostKey(self.shell, factory) def test_keyGeneration(self): """ L{SecureShellConfiguration} generates its own key pair if one is not supplied to C{__init__}. """ store = Store() shell = SecureShellConfiguration(store=store) installOn(shell, store) factory = shell.getFactory() self.assertHostKey(shell, factory) def test_portal(self): """ The factory returned by L{SecureShellConfiguration.getFactory} has a C{portal} attribute which allows logins authenticated in the usual L{axiom.userbase} manner. """ localpart = u'foo bar' domain = u'example.com' password = u'baz quux' loginSystem = self.store.findUnique(LoginSystem) account = loginSystem.addAccount( localpart, domain, password, internal=True) subStore = account.avatars.open() avatar = object() subStore.inMemoryPowerUp(avatar, IConchUser) factory = self.shell.getFactory() login = factory.portal.login( UsernamePassword( '%s@%s' % (localpart.encode('ascii'), domain.encode('ascii')), password), None, IConchUser) def cbLoggedIn(result): self.assertIdentical(IConchUser, result[0]) self.assertIdentical(avatar, result[1]) login.addCallback(cbLoggedIn) return login
class AMPConfigurationTests(TestCase): """ Tests for L{xmantissa.ampserver.AMPConfiguration} which defines how to create an L{AMP} server. """ def setUp(self): """ Create an in-memory L{Store} with an L{AMPConfiguration} in it. """ self.store = Store() self.conf = AMPConfiguration(store=self.store) installOn(self.conf, self.store) def test_interfaces(self): """ L{AMPConfiguration} implements L{IProtocolFactoryFactory}. """ self.assertTrue(verifyObject(IProtocolFactoryFactory, self.conf)) def test_powerup(self): """ L{ionstallOn} powers up the target for L{IProtocolFactoryFactory} with L{AMPConfiguration}. """ self.assertIn( self.conf, list(self.store.powerupsFor(IProtocolFactoryFactory))) def test_getFactory(self): """ L{AMPConfiguration.getFactory} returns a L{ServerFactory} instance which returns L{CredReceiver} instances from its C{buildProtocol} method. """ factory = self.conf.getFactory() self.assertTrue(isinstance(factory, ServerFactory)) protocol = factory.buildProtocol(None) self.assertTrue(isinstance(protocol, CredReceiver)) def test_generateOneTimePad(self): """ L{AMPConfiguration.generateOneTimePad} returns a one-time pad. """ object.__setattr__(self.conf, 'callLater', lambda x, y: None) pad = self.conf.generateOneTimePad(self.store) self.assertNotEqual( pad, self.conf.generateOneTimePad(self.store)) def test_oneTimePadExpires(self): """ L{AMPConfiguration.generateOneTimePad} should expire its pad. """ def callLater(seconds, f): self.assertEqual( seconds, self.conf.ONE_TIME_PAD_DURATION) f() object.__setattr__(self.conf, 'callLater', callLater) pad = self.conf.generateOneTimePad(self.store) self.assertFalse(pad in self.conf._oneTimePads)