Ejemplo n.º 1
0
 def _create_dummy_and_eat_banner(self, device, port = None):
     protocol = Dummy(device = device)
     protocol.connect(device.hostname, port)
     self.assertEqual(str(protocol.buffer), '')
     self.assertEqual(protocol.response, None)
     protocol.expect(re.compile(re.escape(self.banner)))
     self.assertEqual(protocol.response, '')
     return protocol
Ejemplo n.º 2
0
    def testDummy(self):
        # Test simple instance with banner.
        protocol = Dummy(device = self.device)
        protocol.connect('testhost')
        self.assertEqual(str(protocol.buffer), '')
        self.assertEqual(protocol.response, None)
        protocol.close()

        # Test login.
        protocol = Dummy(device = self.device)
        protocol.connect('testhost')
        self.assertEqual(str(protocol.buffer), '')
        self.assertEqual(protocol.response, None)
        protocol.login(self.account, flush = False)
        self.assertEqual(protocol.buffer.tail(len(self.prompt)), self.prompt)
        protocol.close()

        # Test login with user prompt.
        device = VirtualDevice(self.hostname,
                               echo       = True,
                               login_type = VirtualDevice.LOGIN_TYPE_USERONLY)
        protocol = self._create_dummy_and_eat_banner(device)
        self.assertEqual(str(protocol.buffer), 'User: '******'Password: '******'User: '******'')
        self.assertEqual(protocol.response, self.user + '\r')
        protocol.close()

        # Test login with password prompt and wait parameter.
        device = VirtualDevice(self.hostname,
                               echo       = True,
                               login_type = VirtualDevice.LOGIN_TYPE_PASSWORDONLY)
        protocol = self._create_dummy_and_eat_banner(device)
        self.assertEqual(str(protocol.buffer), 'Password: '******'')
        self.assertEqual(protocol.response, self.password + '\r')
        protocol.close()

        # Test login with port number.
        protocol = self._create_dummy_and_eat_banner(device, 1234)
        self.assertEqual(str(protocol.buffer), 'Password: '******'')
        self.assertEqual(protocol.response, self.password + '\r')
        protocol.close()

        # Test a custom response.
        device = VirtualDevice(self.hostname,
                               echo       = True,
                               login_type = VirtualDevice.LOGIN_TYPE_NONE)
        protocol = Dummy(device = device)
        command  = re.compile(r'testcommand')
        response = 'hello world\r\n%s> ' % self.hostname
        device.add_command(command, response, prompt = False)
        protocol.set_prompt(re.compile(r'> $'))
        protocol.connect('testhost')
        protocol.expect(re.compile(re.escape(self.banner)))
        self.assertEqual(protocol.response, '')
        self.assertEqual(str(protocol.buffer), self.prompt)
        protocol.expect_prompt()
        self.assertEqual(str(protocol.buffer), '')
        self.assertEqual(protocol.response, self.hostname)
        protocol.execute('testcommand')
        expected = 'testcommand\rhello world\r\n' + self.hostname
        self.assertEqual(protocol.response, expected)
        self.assertEqual(str(protocol.buffer), '')
        protocol.close()