def test_do_command_bye(self):
        """ Test executing the bye command. """
        command = "bye"

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
    
        self.assertEquals(u"terminating", restricted_handler.do_command(command))
    def test_parse_body_empty_body(self):
        """ Ensure proper behavior on a None command. """
        self.mox.StubOutWithMock(RestrictedCommandHandler, "__init__")

        RestrictedCommandHandler.__init__()

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
        self.assertEquals(None, restricted_handler.parse_body(None))
    def test_parse_body_help_existing_command(self):
        """ Test the help function for an existing command. """
        command = "foobar"
        args = "some command arguments"
        hlp = "command help"

        command_set = [(command, args, hlp)]

        self.mox.StubOutWithMock(RestrictedCommandHandler, "__init__")
        self.mox.StubOutWithMock(configuration.commands, "restricted_set")

        RestrictedCommandHandler.__init__()
        configuration.commands.restricted_set().AndReturn(command_set)
        configuration.commands.restricted_set().AndReturn(command_set)
        configuration.commands.restricted_set().AndReturn(command_set)
        configuration.commands.restricted_set().AndReturn(command_set)

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
        self.assertEquals("foobar - command help",
                restricted_handler.parse_body("help foobar"))
        self.assertEquals("foobar - command help",
                restricted_handler.parse_body("help	foobar"))
        self.assertEquals("foobar - command help",
                restricted_handler.parse_body("help	foobar	"))
        self.assertEquals("foobar - command help",
                restricted_handler.parse_body("help foobar "))
    def test_do_command_no_args(self):
        """ Test executing a command without arguments. """
        command = "ls"
        args = None

        method_args = [command]

        response = "foobar"

        self.mox.StubOutWithMock(RestrictedCommandHandler, "make_syscall")

        RestrictedCommandHandler.make_syscall(method_args).AndReturn(response)

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
        self.assertEquals(response,
                restricted_handler.do_command(command, args))
    def test_do_command_popen_raises(self):
        """ Ensure proper behavior when Popen raises an assertion upon command
            execution. """
        command = "ls"
        args = "-al"

        method_args = [command]
        method_args.extend(args)

        self.mox.StubOutWithMock(RestrictedCommandHandler, "make_syscall")

        RestrictedCommandHandler.make_syscall(method_args).AndRaise(
                OSError(2, None, 'File not found'))

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
        self.assertEqual(type(""),
                type(restricted_handler.do_command(command, args)))
    def test_parse_body_disallowed_command(self):
        """ Test for a disallowed command with some arguments. """
        command = "foobar"
        args = "some command arguments"
        hlp = "command help"

        command_set = [(command, args, hlp)]

        self.mox.StubOutWithMock(RestrictedCommandHandler, "__init__")
        self.mox.StubOutWithMock(configuration.commands, "restricted_set")

        RestrictedCommandHandler.__init__()
        configuration.commands.restricted_set().AndReturn(command_set)

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
        self.assertEquals(None,
                restricted_handler.parse_body("disallowed command"))
    def test_parse_body_command_in_set_args(self):
        """ Test for an allowed command with arguments. """
        command = "foobar"
        args = "some command arguments"
        hlp = "command help"

        command_set = [(command, args, hlp)]

        response = "a response"

        self.mox.StubOutWithMock(RestrictedCommandHandler, "__init__")
        self.mox.StubOutWithMock(RestrictedCommandHandler, "do_command")
        self.mox.StubOutWithMock(configuration.commands, "restricted_set")

        RestrictedCommandHandler.__init__()
        configuration.commands.restricted_set().AndReturn(command_set)
        RestrictedCommandHandler.do_command(command, args).AndReturn(response)

        self.mox.ReplayAll()

        restricted_handler = RestrictedCommandHandler()
        self.assertEquals(response, restricted_handler.parse_body(command))