示例#1
0
def test_ovpnconfig_constructor() -> None:
    """Test OVPNConfig's constructor."""
    config = profile_parser.OVPNConfig()
    assert config.params == []

    params = [
        profile_parser.Parameter("cipher", "AES-256-CBC"),
        profile_parser.Parameter("hash", "sha256")
    ]
    config = profile_parser.OVPNConfig(params)
    assert config.params == params
示例#2
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
示例#3
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"))
示例#4
0
def test_ovpnconfig_cipher_strength() -> None:
    """Test OVPNConfig.cipher_strength()."""
    config = profile_parser.OVPNConfig(
        [profile_parser.Parameter("cipher", "AES-256-CBC")])
    assert config.cipher_strength() == types.CipherStrength.STRONG

    config = profile_parser.OVPNConfig(
        [profile_parser.Parameter("cipher", "AES-192-CBC")])
    assert config.cipher_strength() == types.CipherStrength.MEDIUM

    config = profile_parser.OVPNConfig(
        [profile_parser.Parameter("cipher", "AES-128-CBC")])
    assert config.cipher_strength() == types.CipherStrength.ACCEPTABLE

    config = profile_parser.OVPNConfig(
        [profile_parser.Parameter("cipher", "BF-CBC")])
    assert config.cipher_strength() == types.CipherStrength.WEAK
示例#5
0
def test_ovpnconfig___contains__() -> None:
    """Test OVPNConfig.__contains__()."""
    config = profile_parser.OVPNConfig(
        [profile_parser.Parameter("cipher", "AES-256-CBC")])

    assert "cipher" in config
    assert "hash" not in config

    with pytest.raises(TypeError, match="key must be a str"):
        assert 0 not in config  # type: ignore
示例#6
0
def test_ovpnconfig_insert() -> None:
    """Test OVPNConfig.insert()."""
    config = profile_parser.OVPNConfig(
        [profile_parser.Parameter("cipher", "AES-256-CBC")])
    param = profile_parser.Parameter("hash", "sha256")

    config.insert(1, param)
    assert "hash" in config

    with pytest.raises(KeyError, match="hash already exists"):
        config.insert(2, param)
示例#7
0
def test_ovpnconfig___getitem__() -> None:
    """Test OVPNConfig.__getitem__()."""
    param = profile_parser.Parameter("cipher", "AES-256-CBC")
    config = profile_parser.OVPNConfig([param])

    assert config["cipher"] == config[0] == param

    with pytest.raises(KeyError, match="hash does not exist"):
        config["hash"]

    with pytest.raises(TypeError, match="Empty key not allowed"):
        config[None]  # type: ignore
示例#8
0
def test_ovpnconfig_index() -> None:
    """Test OVPNConfig.index()."""
    params = [
        profile_parser.Parameter("cipher", "AES-256-CBC"),
        profile_parser.Parameter("hash", "sha256")
    ]
    config = profile_parser.OVPNConfig(params)

    assert config.index("cipher") == 0

    with pytest.raises(KeyError, match="dummy does not exist"):
        config.index("dummy")

    with pytest.raises(TypeError, match="Empty key not allowed"):
        config.index(None)  # type: ignore
示例#9
0
def test_ovpnconfig___delitem__() -> None:
    """Test OVPNConfig.__delitem__()."""
    params = [
        profile_parser.Parameter("cipher", "AES-256-CBC"),
        profile_parser.Parameter("hash", "sha256")
    ]
    config = profile_parser.OVPNConfig(params)

    del config["hash"]
    assert "hash" not in config
    del config[0]
    assert "cipher" not in config

    with pytest.raises(KeyError, match="dummy does not exist"):
        del config["dummy"]

    with pytest.raises(TypeError, match="Empty key not allowed"):
        del config[None]  # type: ignore
示例#10
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")