Example #1
0
def test_ovpnconfig_add() -> None:
    """Test OVPNConfig.add()."""
    cipher = profile_parser.Parameter("cipher", "AES-256-CBC")
    config = profile_parser.OVPNConfig()
    config.add(cipher)

    assert config.params == [cipher]

    # Test replacement
    cipher2 = profile_parser.Parameter("cipher", "AES-128-CBC")
    config.add(cipher2, exist_ok=True)

    assert config.params == [cipher2]

    # Test error when duplicate key inserted
    with pytest.raises(KeyError, match="cipher already present in config"):
        config.add(cipher2)

    # Duplicate blank lines should be fine
    config.add(profile_parser.BlankLine())
    config.add(profile_parser.BlankLine())

    # Duplicate comments should be fine
    config.add(profile_parser.Comment("# Comment"))
    config.add(profile_parser.Comment("# Comment"))
Example #2
0
def test_comment_write() -> None:
    """Test Comment.write()."""
    comment = profile_parser.Comment("# This is a comment")

    dummy_io = io.StringIO()
    assert comment.write(dummy_io) == 1
    assert dummy_io.getvalue() == "# This is a comment\n"
Example #3
0
def test_ovpnconfig_last_before_inline() -> None:
    """Test OVPNConfig.last_before_inline()."""
    contents = ["Line 1", "Line 2\n"]
    inline = profile_parser.Inline("<ca>", contents)
    comment = profile_parser.Comment("# This is the first line")

    config = profile_parser.OVPNConfig([comment, inline])
    assert config.last_before_inline() == 1

    config = profile_parser.OVPNConfig([comment])
    assert config.last_before_inline() == 1
Example #4
0
def test_ovpnconfig_write(tmpdir: py.path.local) -> None:
    """Test OVPNConfig.write()."""
    config = profile_parser.OVPNConfig([
        profile_parser.Comment("# First line"),
        profile_parser.BlankLine(),
        profile_parser.Parameter("cipher", "AES-256-CBC"),
        profile_parser.Parameter("hash", "sha256"),
    ])

    out_file = Path(tmpdir / "test.ovpn")
    config.write(out_file)

    with out_file.open("rt") as f_in:
        contents = f_in.read()

    assert contents == ("# First line\n"
                        "\n"
                        "cipher AES-256-CBC\n"
                        "hash sha256\n")
Example #5
0
def test_ovpnconfig_read(tmpdir: py.path.local) -> None:
    """Test OVPNConfig.read()."""
    simple_config = [
        profile_parser.Comment("# Line 1"),
        profile_parser.Parameter("client")
    ]

    config_lines = ["# Line 1", "client"]

    temp_file = Path(tmpdir / "temp.ovpn")
    with temp_file.open("wt") as f_out:
        f_out.write("\n".join(config_lines))

    config = profile_parser.OVPNConfig.read(temp_file)
    assert config.params == simple_config

    config_lines.append("-bogus-")

    temp_file = Path(tmpdir / "temp.ovpn")
    with temp_file.open("wt") as f_out:
        f_out.write("\n".join(config_lines))

    with pytest.raises(ValueError, match="Unknown config file line"):
        profile_parser.OVPNConfig.read(temp_file)
Example #6
0
def test_comment___len__() -> None:
    """Test Comment.__len__()."""
    comment = profile_parser.Comment("# This is a comment")
    assert len(comment) == 1
Example #7
0
def test_comment_constructor() -> None:
    """Test Comments's constructor."""
    comment = profile_parser.Comment("# This is a comment")

    assert comment.name == "# This is a comment"
    assert comment.value is None
Example #8
0
def test_ovpnconfigparam_cmp_error() -> None:
    """Test OVPNParam.__eq__()."""
    comment = profile_parser.Comment("# This is the first line")

    assert comment != 0