Esempio n. 1
0
def test_init_kernel32_unique():
    """Make sure function doesn't override other LibraryLoaders."""
    k32_a = ctypes.LibraryLoader(ctypes.WinDLL).kernel32
    k32_a.GetStdHandle.argtypes = [ctypes.c_void_p]
    k32_a.GetStdHandle.restype = ctypes.c_ulong

    k32_b, stderr_b, stdout_b = windows.init_kernel32()

    k32_c = ctypes.LibraryLoader(ctypes.WinDLL).kernel32
    k32_c.GetStdHandle.argtypes = [ctypes.c_long]
    k32_c.GetStdHandle.restype = ctypes.c_short

    k32_d, stderr_d, stdout_d = windows.init_kernel32()

    # Verify external.
    assert k32_a.GetStdHandle.argtypes == [ctypes.c_void_p]
    assert k32_a.GetStdHandle.restype == ctypes.c_ulong
    assert k32_c.GetStdHandle.argtypes == [ctypes.c_long]
    assert k32_c.GetStdHandle.restype == ctypes.c_short

    # Verify ours.
    assert k32_b.GetStdHandle.argtypes == [ctypes.c_ulong]
    assert k32_b.GetStdHandle.restype == ctypes.c_void_p
    assert k32_d.GetStdHandle.argtypes == [ctypes.c_ulong]
    assert k32_d.GetStdHandle.restype == ctypes.c_void_p
    assert stderr_b == stderr_d
    assert stdout_b == stdout_d
Esempio n. 2
0
def test_init_kernel32_unique():
    """Make sure function doesn't override other LibraryLoaders."""
    k32_a = ctypes.LibraryLoader(ctypes.WinDLL).kernel32
    k32_a.GetStdHandle.argtypes = [ctypes.c_void_p]
    k32_a.GetStdHandle.restype = ctypes.c_ulong

    k32_b, stderr_b, stdout_b = windows.init_kernel32()

    k32_c = ctypes.LibraryLoader(ctypes.WinDLL).kernel32
    k32_c.GetStdHandle.argtypes = [ctypes.c_long]
    k32_c.GetStdHandle.restype = ctypes.c_short

    k32_d, stderr_d, stdout_d = windows.init_kernel32()

    # Verify external.
    assert k32_a.GetStdHandle.argtypes == [ctypes.c_void_p]
    assert k32_a.GetStdHandle.restype == ctypes.c_ulong
    assert k32_c.GetStdHandle.argtypes == [ctypes.c_long]
    assert k32_c.GetStdHandle.restype == ctypes.c_short

    # Verify ours.
    assert k32_b.GetStdHandle.argtypes == [ctypes.c_ulong]
    assert k32_b.GetStdHandle.restype == ctypes.c_void_p
    assert k32_d.GetStdHandle.argtypes == [ctypes.c_ulong]
    assert k32_d.GetStdHandle.restype == ctypes.c_void_p
    assert stderr_b == stderr_d
    assert stdout_b == stdout_d
Esempio n. 3
0
def test_get_console_info():
    """Test function."""
    # Test error.
    if windows.IS_WINDOWS:
        with pytest.raises(OSError):
            windows.get_console_info(windows.init_kernel32()[0],
                                     windows.INVALID_HANDLE_VALUE)

    # Test no error with mock methods.
    kernel32 = MockKernel32()
    fg_color, bg_color, native_ansi = windows.get_console_info(
        kernel32, windows.INVALID_HANDLE_VALUE)
    assert fg_color == 7
    assert bg_color == 0
    assert native_ansi is False

    # Test different console modes.
    for not_native in (0x0, 0x1, 0x2, 0x1 | 0x2):
        kernel32.set_mode = not_native
        assert not windows.get_console_info(kernel32,
                                            windows.INVALID_HANDLE_VALUE)[-1]
    for native in (i | 0x4 for i in (0x0, 0x1, 0x2, 0x1 | 0x2)):
        kernel32.set_mode = native
        assert windows.get_console_info(kernel32,
                                        windows.INVALID_HANDLE_VALUE)[-1]
Esempio n. 4
0
def test_init_kernel32_valid_handle(monkeypatch, stderr_invalid,
                                    stdout_invalid):
    """Test valid/invalid handle handling.

    :param monkeypatch: pytest fixture.
    :param bool stderr_invalid: Mock stderr is valid.
    :param bool stdout_invalid: Mock stdout is valid.
    """
    mock_sys = MockSys()
    monkeypatch.setattr(windows, 'sys', mock_sys)
    if stderr_invalid:
        setattr(mock_sys.stderr, '_original_stream', True)
    if stdout_invalid:
        setattr(mock_sys.stdout, '_original_stream', True)

    stderr, stdout = windows.init_kernel32(MockKernel32(stderr=100,
                                                        stdout=200))[1:]

    if stderr_invalid and stdout_invalid:
        assert stderr == windows.INVALID_HANDLE_VALUE
        assert stdout == windows.INVALID_HANDLE_VALUE
    elif stdout_invalid:
        assert stderr == 100
        assert stdout == windows.INVALID_HANDLE_VALUE
    elif stderr_invalid:
        assert stderr == windows.INVALID_HANDLE_VALUE
        assert stdout == 200
    else:
        assert stderr == 100
        assert stdout == 200
Esempio n. 5
0
def test_init_kernel32_valid_handle(monkeypatch, stderr_invalid, stdout_invalid):
    """Test valid/invalid handle handling.

    :param monkeypatch: pytest fixture.
    :param bool stderr_invalid: Mock stderr is valid.
    :param bool stdout_invalid: Mock stdout is valid.
    """
    mock_sys = MockSys()
    monkeypatch.setattr(windows, 'sys', mock_sys)
    if stderr_invalid:
        setattr(mock_sys.stderr, '_original_stream', True)
    if stdout_invalid:
        setattr(mock_sys.stdout, '_original_stream', True)

    stderr, stdout = windows.init_kernel32(MockKernel32(stderr=100, stdout=200))[1:]

    if stderr_invalid and stdout_invalid:
        assert stderr == windows.INVALID_HANDLE_VALUE
        assert stdout == windows.INVALID_HANDLE_VALUE
    elif stdout_invalid:
        assert stderr == 100
        assert stdout == windows.INVALID_HANDLE_VALUE
    elif stderr_invalid:
        assert stderr == windows.INVALID_HANDLE_VALUE
        assert stdout == 200
    else:
        assert stderr == 100
        assert stdout == 200
Esempio n. 6
0
def test_windows_stream():
    """Test class."""
    # Test error.
    if windows.IS_WINDOWS:
        stream = windows.WindowsStream(windows.init_kernel32()[0], windows.INVALID_HANDLE_VALUE, StringIO())
        assert stream.colors == (windows.WINDOWS_CODES['white'], windows.WINDOWS_CODES['black'])
        stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES['bgblue']  # No exception, just ignore.
        assert stream.colors == (windows.WINDOWS_CODES['white'], windows.WINDOWS_CODES['black'])

    # Test __getattr__() and color resetting.
    original_stream = StringIO()
    stream = windows.WindowsStream(MockKernel32(), windows.INVALID_HANDLE_VALUE, original_stream)
    assert stream.writelines == original_stream.writelines  # Test __getattr__().
    assert stream.colors == (windows.WINDOWS_CODES['white'], windows.WINDOWS_CODES['black'])
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES['bgblue']
    assert stream.colors == (windows.WINDOWS_CODES['red'], windows.WINDOWS_CODES['bgblue'])
    stream.colors = None  # Resets colors to original.
    assert stream.colors == (windows.WINDOWS_CODES['white'], windows.WINDOWS_CODES['black'])

    # Test special negative codes.
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES['bgblue']
    stream.colors = windows.WINDOWS_CODES['/fg']
    assert stream.colors == (windows.WINDOWS_CODES['white'], windows.WINDOWS_CODES['bgblue'])
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES['bgblue']
    stream.colors = windows.WINDOWS_CODES['/bg']
    assert stream.colors == (windows.WINDOWS_CODES['red'], windows.WINDOWS_CODES['black'])
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES['bgblue']
    stream.colors = windows.WINDOWS_CODES['bgblack']
    assert stream.colors == (windows.WINDOWS_CODES['red'], windows.WINDOWS_CODES['black'])

    # Test write.
    stream.write(Color('{/all}A{red}B{bgblue}C'))
    original_stream.seek(0)
    assert original_stream.read() == 'ABC'
    assert stream.colors == (windows.WINDOWS_CODES['red'], windows.WINDOWS_CODES['bgblue'])

    # Test ignore invalid code.
    original_stream.seek(0)
    original_stream.truncate()
    stream.write('\x1b[0mA\x1b[31mB\x1b[44;999mC')
    original_stream.seek(0)
    assert original_stream.read() == 'ABC'
    assert stream.colors == (windows.WINDOWS_CODES['red'], windows.WINDOWS_CODES['bgblue'])
Esempio n. 7
0
def test_get_console_info():
    """Test function."""
    # Test error.
    if windows.IS_WINDOWS:
        with pytest.raises(OSError):
            windows.get_console_info(windows.init_kernel32()[0], windows.INVALID_HANDLE_VALUE)

    # Test no error with mock methods.
    kernel32 = MockKernel32()
    fg_color, bg_color, native_ansi = windows.get_console_info(kernel32, windows.INVALID_HANDLE_VALUE)
    assert fg_color == 7
    assert bg_color == 0
    assert native_ansi is False

    # Test different console modes.
    for not_native in (0x0, 0x1, 0x2, 0x1 | 0x2):
        kernel32.set_mode = not_native
        assert not windows.get_console_info(kernel32, windows.INVALID_HANDLE_VALUE)[-1]
    for native in (i | 0x4 for i in (0x0, 0x1, 0x2, 0x1 | 0x2)):
        kernel32.set_mode = native
        assert windows.get_console_info(kernel32, windows.INVALID_HANDLE_VALUE)[-1]
Esempio n. 8
0
def test_windows_stream():
    """Test class."""
    # Test error.
    if windows.IS_WINDOWS:
        stream = windows.WindowsStream(windows.init_kernel32()[0],
                                       windows.INVALID_HANDLE_VALUE,
                                       StringIO())
        assert stream.colors == (windows.WINDOWS_CODES['white'],
                                 windows.WINDOWS_CODES['black'])
        stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES[
            'bgblue']  # No exception, just ignore.
        assert stream.colors == (windows.WINDOWS_CODES['white'],
                                 windows.WINDOWS_CODES['black'])

    # Test __getattr__() and color resetting.
    original_stream = StringIO()
    stream = windows.WindowsStream(MockKernel32(),
                                   windows.INVALID_HANDLE_VALUE,
                                   original_stream)
    assert stream.writelines == original_stream.writelines  # Test __getattr__().
    assert stream.colors == (windows.WINDOWS_CODES['white'],
                             windows.WINDOWS_CODES['black'])
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES[
        'bgblue']
    assert stream.colors == (windows.WINDOWS_CODES['red'],
                             windows.WINDOWS_CODES['bgblue'])
    stream.colors = None  # Resets colors to original.
    assert stream.colors == (windows.WINDOWS_CODES['white'],
                             windows.WINDOWS_CODES['black'])

    # Test special negative codes.
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES[
        'bgblue']
    stream.colors = windows.WINDOWS_CODES['/fg']
    assert stream.colors == (windows.WINDOWS_CODES['white'],
                             windows.WINDOWS_CODES['bgblue'])
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES[
        'bgblue']
    stream.colors = windows.WINDOWS_CODES['/bg']
    assert stream.colors == (windows.WINDOWS_CODES['red'],
                             windows.WINDOWS_CODES['black'])
    stream.colors = windows.WINDOWS_CODES['red'] | windows.WINDOWS_CODES[
        'bgblue']
    stream.colors = windows.WINDOWS_CODES['bgblack']
    assert stream.colors == (windows.WINDOWS_CODES['red'],
                             windows.WINDOWS_CODES['black'])

    # Test write.
    stream.write(Color('{/all}A{red}B{bgblue}C'))
    original_stream.seek(0)
    assert original_stream.read() == 'ABC'
    assert stream.colors == (windows.WINDOWS_CODES['red'],
                             windows.WINDOWS_CODES['bgblue'])

    # Test ignore invalid code.
    original_stream.seek(0)
    original_stream.truncate()
    stream.write('\x1b[0mA\x1b[31mB\x1b[44;999mC')
    original_stream.seek(0)
    assert original_stream.read() == 'ABC'
    assert stream.colors == (windows.WINDOWS_CODES['red'],
                             windows.WINDOWS_CODES['bgblue'])