def test_create_host_info(self):
        serializer = Serializer()

        foreground_color = Color(value=Color.CYAN)
        background_color = Color(value=Color.RED)
        cursor_position = Coordinates(x=1, y=2)
        window_position = Coordinates(x=3, y=4)
        cursor_size = 10
        buffer_size = Size(height=10, width=20)
        window_size = Size(height=30, width=40)
        max_window_size = Size(height=50, width=60)
        max_physical_window_size = Size(height=70, width=80)
        window_title = "Random Window Title"

        ps_raw_ui = PSHostRawUserInterface(
            window_title, cursor_size, foreground_color, background_color,
            cursor_position, window_position, buffer_size,
            max_physical_window_size, max_window_size, window_size
        )
        ps_ui = PSHostUserInterface(raw_ui=ps_raw_ui)
        ps_host = PSHost(None, None, False, None, None, ps_ui, None)

        host_info = HostInfo(host=ps_host)
        expected_xml = normalise_xml(self.HOST_XML)

        actual = serializer.serialize(host_info)
        actual_xml = normalise_xml(ET.tostring(actual))
        assert_xml_diff(actual_xml, expected_xml)
Exemple #2
0
    def SetWindowSize(self, runspace, pipeline, size):
        """
        MI: 40
        SHOULD set the view window size based on the size specified.

        :param runspace: The runspace the host call relates to
        :param pipeline: The pipeline (if any) that the call relates to
        :param size: A GenericComplexObject that contains the extended
            properties for the size
        """
        obj = Size(height=size.extended_properties['height'],
                   width=size.extended_properties['width'])
        self.window_size = obj
Exemple #3
0
    def SetBufferSize(self, runspace, pipeline, size):
        """
        MI: 38
        SHOULD set the size of the screen buffer with the specified size in
        character cells.

        :param runspace: The runspace the host call relates to
        :param pipeline: The pipeline (if any) that the call relates to
        :param size: A GenericComplexObject that contains the extended
            properties for the size
        """
        obj = Size(height=size.extended_properties['height'],
                   width=size.extended_properties['width'])
        self.buffer_size = obj
Exemple #4
0
    def SetWindowSize(
        self,
        runspace: RunspacePool,
        pipeline: typing.Optional[PowerShell],
        size: GenericComplexObject,
    ) -> None:
        """
        MI: 40
        SHOULD set the view window size based on the size specified.

        :param runspace: The runspace the host call relates to
        :param pipeline: The pipeline (if any) that the call relates to
        :param size: A GenericComplexObject that contains the extended
            properties for the size
        """
        obj = Size(height=size.extended_properties["height"],
                   width=size.extended_properties["width"])
        self.window_size = obj
Exemple #5
0
    def SetBufferSize(
        self,
        runspace: RunspacePool,
        pipeline: typing.Optional[PowerShell],
        size: GenericComplexObject,
    ) -> None:
        """
        MI: 38
        SHOULD set the size of the screen buffer with the specified size in
        character cells.

        :param runspace: The runspace the host call relates to
        :param pipeline: The pipeline (if any) that the call relates to
        :param size: A GenericComplexObject that contains the extended
            properties for the size
        """
        obj = Size(height=size.extended_properties["height"],
                   width=size.extended_properties["width"])
        self.buffer_size = obj
Exemple #6
0
    def test_ps_host_raw_ui_method(self):
        window_title = "pypsrp window"
        cursor_size = 50
        foreground_color = Color(value=Color.WHITE)
        background_color = Color(value=Color.BLUE)
        cursor_position = Coordinates(x=1, y=2)
        window_position = Coordinates(x=3, y=4)
        buffer_size = Size(width=80, height=80)
        max_physical_window_size = Size(width=80, height=80)
        max_window_size = Size(width=80, height=80)
        window_size = Size(width=80, height=80)

        raw_ui = PSHostRawUserInterface(
            window_title, cursor_size, foreground_color, background_color,
            cursor_position, window_position, buffer_size,
            max_physical_window_size, max_window_size, window_size
        )

        actual_foreground_color = raw_ui.GetForegroundColor(None, None)
        assert actual_foreground_color == foreground_color
        raw_ui.SetForegroundColor(None, None, Color.MAGENTA)
        assert isinstance(raw_ui.foreground_color, Color)
        assert raw_ui.foreground_color.value == Color.MAGENTA

        actual_background_color = raw_ui.GetBackgroundColor(None, None)
        assert actual_background_color == background_color
        raw_ui.SetBackgroundColor(None, None, Color.DARK_MAGENTA)
        assert isinstance(raw_ui.background_color, Color)
        assert raw_ui.background_color.value == Color.DARK_MAGENTA

        coordinates = GenericComplexObject()
        coordinates.extended_properties['x'] = 11
        coordinates.extended_properties['y'] = 12

        actual_cursor_position = raw_ui.GetCursorPosition(None, None)
        assert actual_cursor_position == cursor_position
        raw_ui.SetCursorPosition(None, None, coordinates)
        assert isinstance(raw_ui.cursor_position, Coordinates)
        assert raw_ui.cursor_position.x == 11
        assert raw_ui.cursor_position.y == 12

        actual_window_position = raw_ui.GetWindowPosition(None, None)
        assert actual_window_position == window_position
        raw_ui.SetWindowPosition(None, None, coordinates)
        assert isinstance(raw_ui.window_position, Coordinates)
        assert raw_ui.window_position.x == 11
        assert raw_ui.window_position.y == 12

        actual_cursor_size = raw_ui.GetCursorSize(None, None)
        assert actual_cursor_size == cursor_size
        raw_ui.SetCursorSize(None, None, 25)
        assert raw_ui.cursor_size == 25

        size = GenericComplexObject()
        size.extended_properties['height'] = 160
        size.extended_properties['width'] = 160

        actual_buffer_size = raw_ui.GetBufferSize(None, None)
        assert actual_buffer_size == buffer_size
        raw_ui.SetBufferSize(None, None, size)
        assert isinstance(raw_ui.buffer_size, Size)
        assert raw_ui.buffer_size.height == 160
        assert raw_ui.buffer_size.width == 160

        actual_window_size = raw_ui.GetWindowSize(None, None)
        assert actual_window_size == window_size
        raw_ui.SetWindowSize(None, None, size)
        assert isinstance(raw_ui.window_size, Size)
        assert raw_ui.window_size.height == 160
        assert raw_ui.window_size.width == 160

        actual_window_title = raw_ui.GetWindowTitle(None, None)
        assert actual_window_title == window_title
        raw_ui.SetWindowTitle(None, None, "new title")
        assert raw_ui.window_title == "new title"

        actual_max_window_size = raw_ui.GetMaxWindowSize(None, None)
        assert actual_max_window_size == max_window_size

        actual_physical_window_size = raw_ui.GetMaxPhysicalWindowSize(None,
                                                                      None)
        assert actual_physical_window_size == max_physical_window_size

        raw_ui.key_available = True
        actual_key_available = raw_ui.GetKeyAvailable(None, None)
        assert actual_key_available is True

        raw_ui.FlushInputBuffer(None, None)

        rectangle = GenericComplexObject()
        rectangle.extended_properties['left'] = 1
        rectangle.extended_properties['top'] = 2
        rectangle.extended_properties['right'] = 3
        rectangle.extended_properties['bottom'] = 4

        fill = GenericComplexObject()
        fill.extended_properties['character'] = "A"
        fill.extended_properties['foregroundColor'] = 12
        fill.extended_properties['backgroundColor'] = 10
        fill.extended_properties['bufferCellType'] = 0

        contents = GenericComplexObject()
        contents.extended_properties['mal'] = [2, 2]
        contents.extended_properties['mae'] = [[fill, fill], [fill, fill]]

        raw_ui.SetBufferContents1(None, None, rectangle, fill)
        raw_ui.SetBufferContents2(None, None, coordinates, contents)
        raw_ui.ScrollBufferContents(None, None, rectangle, coordinates,
                                    rectangle, fill)
Exemple #7
0
    def test_psrp_pshost_raw_ui_mocked_methods(self, wsman_conn,
                                               monkeypatch):
        # in a mocked context the calculated size differs on a few variables
        # we will mock out that call and return the ones used in our existing
        # responses
        mock_calc = MagicMock()
        mock_calc.side_effect = [113955, 382750]

        key_info = KeyInfo(code=65, character="a",
                           state=ControlKeyState.CapsLockOn, key_down=True)

        set_foreground_color = MagicMock(return_value=None)
        set_background_color = MagicMock(return_value=None)
        set_cursor_position = MagicMock(return_value=None)
        set_window_position = MagicMock(return_value=None)
        set_cursor_size = MagicMock(return_value=None)
        set_buffer_size = MagicMock(return_value=None)
        set_window_size = MagicMock(return_value=None)
        set_window_title = MagicMock(return_value=None)
        read_key = MagicMock(return_value=key_info)
        flush_input = MagicMock(return_value=None)
        set_buffer1 = MagicMock(return_value=None)
        set_buffer2 = MagicMock(return_value=None)
        scroll_buffer = MagicMock(return_value=None)

        window_title = "pypsrp window"
        cursor_size = 50
        foreground_color = Color(value=Color.WHITE)
        background_color = Color(value=Color.BLUE)
        cursor_position = Coordinates(x=1, y=2)
        window_position = Coordinates(x=3, y=4)
        buffer_size = Size(width=80, height=80)
        max_physical_window_size = Size(width=80, height=80)
        max_window_size = Size(width=80, height=80)
        window_size = Size(width=80, height=80)

        host_raw_ui = PSHostRawUserInterface(window_title, cursor_size,
                                             foreground_color,
                                             background_color, cursor_position,
                                             window_position, buffer_size,
                                             max_physical_window_size,
                                             max_window_size, window_size)
        host_raw_ui.SetForegroundColor = set_foreground_color
        host_raw_ui.SetBackgroundColor = set_background_color
        host_raw_ui.SetCursorPosition = set_cursor_position
        host_raw_ui.SetWindowPosition = set_window_position
        host_raw_ui.SetCursorSize = set_cursor_size
        host_raw_ui.SetBufferSize = set_buffer_size
        host_raw_ui.SetWindowSize = set_window_size
        host_raw_ui.SetWindowTitle = set_window_title
        host_raw_ui.ReadKey = read_key
        host_raw_ui.FlushInputBuffer = flush_input
        host_raw_ui.SetBufferContents1 = set_buffer1
        host_raw_ui.SetBufferContents2 = set_buffer2
        host_raw_ui.ScrollBufferContents = scroll_buffer

        host_ui = PSHostUserInterface(host_raw_ui)
        host = PSHost(None, None, False, None, None, host_ui, None)

        with RunspacePool(wsman_conn, host=host) as pool:
            ps = PowerShell(pool)
            ps.add_script('''$host.UI.RawUI.ForegroundColor
$host.UI.RawUI.ForegroundColor = [System.ConsoleColor]::Green
$host.UI.RawUI.ForegroundColor

$host.UI.RawUI.BackgroundColor
$host.UI.RawUI.BackgroundColor = [System.ConsoleColor]::Red
$host.UI.RawUI.BackgroundColor

$host.UI.RawUI.CursorPosition
$host.UI.RawUI.CursorPosition = (New-Object -TypeName System.Management.Automation.Host.Coordinates -ArgumentList 11, 12)
$host.UI.RawUI.CursorPosition

$host.UI.RawUI.WindowPosition
$host.UI.RawUI.WindowPosition = (New-Object -TypeName System.Management.Automation.Host.Coordinates -ArgumentList 13, 14)
$host.UI.RawUI.WindowPosition

$host.UI.RawUI.CursorSize
$host.UI.RawUI.CursorSize = 25
$host.UI.RawUI.CursorSize

$host.UI.RawUI.BufferSize
$host.UI.RawUI.BufferSize = (New-Object -TypeName System.Management.Automation.Host.Size -ArgumentList 8, 9)
$host.UI.RawUI.BufferSize

$host.UI.RawUI.WindowSize
$host.UI.RawUI.WindowSize = (New-Object -TypeName System.Management.Automation.Host.Size -ArgumentList 8, 9)
$host.UI.RawUI.WindowSize

$host.UI.RawUI.WindowTitle
$host.UI.RawUI.WindowTitle = "New Window Title"
$host.UI.RawUI.WindowTitle

$host.UI.RawUI.ReadKey()

$host.UI.RawUI.FlushInputBuffer()

$buffer_cell = New-Object -TypeName System.Management.Automation.Host.BufferCell -ArgumentList "Z", ([System.ConsoleColor]::Red), ([System.ConsoleColor]::Green), ([System.Management.Automation.Host.BufferCellType]::Complete)
$rectangle = New-Object -TypeName System.Management.Automation.Host.Rectangle -ArgumentList 1, 2, 3, 4
$host.UI.RawUI.SetBufferContents($rectangle, $buffer_cell)

$coordinates = New-Object -TypeName System.Management.Automation.Host.Coordinates -ArgumentList 15, 15

$buffer_cell1_1 = New-Object -TypeName System.Management.Automation.Host.BufferCell -ArgumentList "A", ([System.ConsoleColor]::Black), ([System.ConsoleColor]::White), ([System.Management.Automation.Host.BufferCellType]::Leading)
$buffer_cell1_2 = New-Object -TypeName System.Management.Automation.Host.BufferCell -ArgumentList "B", ([System.ConsoleColor]::Black), ([System.ConsoleColor]::White), ([System.Management.Automation.Host.BufferCellType]::Trailing)
$buffer_cell2_1 = New-Object -TypeName System.Management.Automation.Host.BufferCell -ArgumentList "C", ([System.ConsoleColor]::Black), ([System.ConsoleColor]::White), ([System.Management.Automation.Host.BufferCellType]::Leading)
$buffer_cell2_2 = New-Object -TypeName System.Management.Automation.Host.BufferCell -ArgumentList "D", ([System.ConsoleColor]::Black), ([System.ConsoleColor]::White), ([System.Management.Automation.Host.BufferCellType]::Trailing)

$cells = New-Object -TypeName "System.Management.Automation.Host.BufferCell[,]" -ArgumentList 2,2
$cells[0,0] = $buffer_cell1_1
$cells[0,1] = $buffer_cell1_2
$cells[1,1] = $buffer_cell2_2
$cells[1,0] = $buffer_cell2_1
$host.UI.RawUI.SetBufferContents($coordinates, $cells)

$host.UI.RawUI.ScrollBufferContents($rectangle, $coordinates, $rectangle, $buffer_cell)''')
            actual = ps.invoke()

        assert len(actual) == 17

        assert str(actual[0]) == "White"
        assert str(actual[1]) == "Green"
        assert set_foreground_color.call_count == 1
        assert isinstance(set_foreground_color.call_args[0][0], RunspacePool)
        assert isinstance(set_foreground_color.call_args[0][1], PowerShell)
        assert set_foreground_color.call_args[0][2] == Color.GREEN

        assert str(actual[2]) == "Blue"
        assert str(actual[3]) == "Red"
        assert set_background_color.call_count == 1
        assert isinstance(set_background_color.call_args[0][0], RunspacePool)
        assert isinstance(set_background_color.call_args[0][1], PowerShell)
        assert set_background_color.call_args[0][2] == Color.RED

        assert str(actual[4]) == "1,2"
        assert str(actual[5]) == "11,12"
        assert set_cursor_position.call_count == 1
        assert isinstance(set_cursor_position.call_args[0][0], RunspacePool)
        assert isinstance(set_cursor_position.call_args[0][1], PowerShell)
        assert set_cursor_position.call_args[0][2].extended_properties['x'] \
            == 11
        assert set_cursor_position.call_args[0][2].extended_properties['y'] \
            == 12

        assert str(actual[6]) == "3,4"
        assert str(actual[7]) == "13,14"
        assert set_window_position.call_count == 1
        assert isinstance(set_window_position.call_args[0][0], RunspacePool)
        assert isinstance(set_window_position.call_args[0][1], PowerShell)
        assert set_window_position.call_args[0][2].extended_properties['x'] \
            == 13
        assert set_window_position.call_args[0][2].extended_properties['y'] \
            == 14

        assert actual[8] == 50
        assert actual[9] == 25
        assert set_cursor_size.call_count == 1
        assert isinstance(set_cursor_size.call_args[0][0], RunspacePool)
        assert isinstance(set_cursor_size.call_args[0][1], PowerShell)
        assert set_cursor_size.call_args[0][2] == 25

        assert str(actual[10]) == "80,80"
        assert str(actual[11]) == "8,9"
        assert set_buffer_size.call_count == 1
        assert isinstance(set_buffer_size.call_args[0][0], RunspacePool)
        assert isinstance(set_buffer_size.call_args[0][1], PowerShell)
        assert isinstance(set_buffer_size.call_args[0][2],
                          GenericComplexObject)
        assert set_buffer_size.call_args[0][2].extended_properties['width'] \
            == 8
        assert set_buffer_size.call_args[0][2].extended_properties['height'] \
            == 9

        assert str(actual[12]) == "80,80"
        assert str(actual[13]) == "8,9"
        assert set_window_size.call_count == 1
        assert isinstance(set_window_size.call_args[0][0], RunspacePool)
        assert isinstance(set_window_size.call_args[0][1], PowerShell)
        assert isinstance(set_window_size.call_args[0][2],
                          GenericComplexObject)
        assert set_window_size.call_args[0][2].extended_properties['width'] \
            == 8
        assert set_window_size.call_args[0][2].extended_properties['height'] \
            == 9

        assert actual[14] == "pypsrp window"
        assert actual[15] == "New Window Title"
        assert set_window_title.call_count == 1
        assert isinstance(set_window_title.call_args[0][0], RunspacePool)
        assert isinstance(set_window_title.call_args[0][1], PowerShell)
        assert set_window_title.call_args[0][2] == "New Window Title"

        assert str(actual[16]) == "65,a,CapsLockOn,True"
        assert isinstance(actual[16], KeyInfoDotNet)
        assert read_key.call_count == 1
        assert isinstance(read_key.call_args[0][0], RunspacePool)
        assert isinstance(read_key.call_args[0][1], PowerShell)
        assert read_key.call_args[0][2] == 4

        assert flush_input.call_count == 1
        assert isinstance(flush_input.call_args[0][0], RunspacePool)
        assert isinstance(flush_input.call_args[0][1], PowerShell)

        assert set_buffer1.call_count == 1
        assert isinstance(set_buffer1.call_args[0][0], RunspacePool)
        assert isinstance(set_buffer1.call_args[0][1], PowerShell)
        assert isinstance(set_buffer1.call_args[0][2], GenericComplexObject)
        assert set_buffer1.call_args[0][2].extended_properties['left'] == 1
        assert set_buffer1.call_args[0][2].extended_properties['top'] == 2
        assert set_buffer1.call_args[0][2].extended_properties['right'] == 3
        assert set_buffer1.call_args[0][2].extended_properties['bottom'] == 4
        fill = set_buffer1.call_args[0][3]
        assert isinstance(fill, GenericComplexObject)
        assert fill.extended_properties['character'] == "Z"
        assert fill.extended_properties['foregroundColor'] == 12
        assert fill.extended_properties['backgroundColor'] == 10
        assert fill.extended_properties['bufferCellType'] == 0

        assert set_buffer2.call_count == 1
        assert isinstance(set_buffer2.call_args[0][0], RunspacePool)
        assert isinstance(set_buffer2.call_args[0][1], PowerShell)
        assert isinstance(set_buffer2.call_args[0][2], GenericComplexObject)
        assert set_buffer2.call_args[0][2].extended_properties['x'] == 15
        assert set_buffer2.call_args[0][2].extended_properties['y'] == 15
        assert isinstance(set_buffer2.call_args[0][3], GenericComplexObject)
        assert set_buffer2.call_args[0][3].extended_properties['mal'] == [2, 2]
        set_contents = set_buffer2.call_args[0][3].extended_properties['mae']
        assert len(set_contents) == 4
        assert set_contents[0].extended_properties['character'] == "A"
        assert set_contents[0].extended_properties['foregroundColor'] == 0
        assert set_contents[0].extended_properties['backgroundColor'] == 15
        assert set_contents[0].extended_properties['bufferCellType'] == 1
        assert set_contents[1].extended_properties['character'] == "B"
        assert set_contents[1].extended_properties['foregroundColor'] == 0
        assert set_contents[1].extended_properties['backgroundColor'] == 15
        assert set_contents[1].extended_properties['bufferCellType'] == 2
        assert set_contents[2].extended_properties['character'] == "C"
        assert set_contents[2].extended_properties['foregroundColor'] == 0
        assert set_contents[2].extended_properties['backgroundColor'] == 15
        assert set_contents[2].extended_properties['bufferCellType'] == 1
        assert set_contents[3].extended_properties['character'] == "D"
        assert set_contents[3].extended_properties['foregroundColor'] == 0
        assert set_contents[3].extended_properties['backgroundColor'] == 15
        assert set_contents[3].extended_properties['bufferCellType'] == 2

        assert scroll_buffer.call_count == 1
        assert isinstance(scroll_buffer.call_args[0][0], RunspacePool)
        assert isinstance(scroll_buffer.call_args[0][1], PowerShell)
        source = scroll_buffer.call_args[0][2]
        assert isinstance(source, GenericComplexObject)
        assert source.extended_properties['left'] == 1
        assert source.extended_properties['top'] == 2
        assert source.extended_properties['right'] == 3
        assert source.extended_properties['bottom'] == 4
        destination = scroll_buffer.call_args[0][3]
        assert isinstance(destination, GenericComplexObject)
        assert destination.extended_properties['x'] == 15
        assert destination.extended_properties['y'] == 15
        clip = scroll_buffer.call_args[0][4]
        assert isinstance(clip, GenericComplexObject)
        assert clip.extended_properties['left'] == 1
        assert clip.extended_properties['top'] == 2
        assert clip.extended_properties['right'] == 3
        assert clip.extended_properties['bottom'] == 4
        fill = scroll_buffer.call_args[0][5]
        assert isinstance(fill, GenericComplexObject)
        assert fill.extended_properties['character'] == "Z"
        assert fill.extended_properties['foregroundColor'] == 12
        assert fill.extended_properties['backgroundColor'] == 10
        assert fill.extended_properties['bufferCellType'] == 0