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"))
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)
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
def test_parameter_write() -> None: """Test Parameter.write().""" solo_param = profile_parser.Parameter("client") dummy_io = io.StringIO() assert solo_param.write(dummy_io) == 1 assert dummy_io.getvalue() == "client\n" double_param = profile_parser.Parameter("dev", "tun") dummy_io = io.StringIO() assert double_param.write(dummy_io) == 1 assert dummy_io.getvalue() == "dev tun\n"
def test_parameter_constructor() -> None: """Test Parameter's constructor.""" solo_param = profile_parser.Parameter("client") assert solo_param.name == "client" assert solo_param.value is None double_param = profile_parser.Parameter("dev", "tun") assert double_param.name == "dev" assert double_param.value == "tun"
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
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
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
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
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")
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
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)
def test_parameter___len__() -> None: """Test Parameter.__len__().""" param = profile_parser.Parameter("client") assert len(param) == 1