|
| 1 | +#!/usr/bin/env python |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +import asyncio |
| 4 | +import contextlib |
| 5 | +import sdbus |
| 6 | +import pytest |
| 7 | +from sdbus_async.networkmanager import ( |
| 8 | + ConnectionProfile, |
| 9 | + NetworkConnectionSettings, |
| 10 | + NetworkManagerSettings as SettingsManager, |
| 11 | + NmSettingsInvalidConnectionError, |
| 12 | +) |
| 13 | + |
| 14 | +# All test coroutines will be treated as marked. |
| 15 | + |
| 16 | + |
| 17 | +def test_wifi_wpa_psk_simple_from_dict() -> ConnectionProfile: |
| 18 | + """Parse connection and ipv4 settings from dbus using ConnectionProfile""" |
| 19 | + profile = ConnectionProfile.from_settings_dict( |
| 20 | + { |
| 21 | + "connection": { |
| 22 | + "id": "WirelessWpaPskConnection", |
| 23 | + "type": "802-11-wireless", |
| 24 | + "uuid": "16ea7af1-0e35-4036-831e-ced975f48510", |
| 25 | + "autoconnect": False, |
| 26 | + }, |
| 27 | + "ipv4": {"method": "auto"}, |
| 28 | + "ipv6": {"method": "disabled"}, |
| 29 | + "802-11-wireless": { |
| 30 | + "security": "802-11-wireless-security", |
| 31 | + "ssid": b"CafeSSID", |
| 32 | + }, |
| 33 | + "802-11-wireless-security": {"key-mgmt": "wpa-psk"}, |
| 34 | + } |
| 35 | + ) |
| 36 | + assert profile.connection.connection_id == "WirelessWpaPskConnection" |
| 37 | + assert profile.connection.uuid == "16ea7af1-0e35-4036-831e-ced975f48510" |
| 38 | + assert profile.connection.connection_type == "802-11-wireless" |
| 39 | + assert profile.connection.autoconnect is False |
| 40 | + assert profile.ipv4 |
| 41 | + assert profile.ipv4.method == "auto" |
| 42 | + assert profile.ipv4.address_data is None |
| 43 | + assert profile.ipv6 |
| 44 | + assert profile.ipv6.method == "disabled" |
| 45 | + return profile |
| 46 | + |
| 47 | + |
| 48 | +async def delete_connection_by_uuid(nmset: SettingsManager, uuid: str) -> None: |
| 49 | + dpath = await nmset.get_connection_by_uuid(uuid) |
| 50 | + connection_settings = NetworkConnectionSettings(dpath) |
| 51 | + await connection_settings.delete() |
| 52 | + |
| 53 | + |
| 54 | +# @pytest_asyncio.fixture |
| 55 | +@pytest.mark.asyncio |
| 56 | +async def test_add_wifi_wpa_psk_simple_from_dict() -> None: |
| 57 | + """Test adding a wireless WPA PSK connection profile(unsaved) from dict""" |
| 58 | + profile = test_wifi_wpa_psk_simple_from_dict() |
| 59 | + |
| 60 | + # If we add many connections passing the same id, things get messy. Check: |
| 61 | + sdbus.set_default_bus(sdbus.sd_bus_open_system()) |
| 62 | + settings = SettingsManager() |
| 63 | + assert profile.connection.uuid |
| 64 | + with contextlib.suppress(NmSettingsInvalidConnectionError): |
| 65 | + await settings.get_connection_by_uuid(profile.connection.uuid) |
| 66 | + print(f"Deleting existing connection with {profile.connection.uuid}") |
| 67 | + await delete_connection_by_uuid(settings, profile.connection.uuid) |
| 68 | + await settings.add_connection_unsaved(profile.to_dbus()) |
| 69 | + await delete_connection_by_uuid(settings, profile.connection.uuid) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + """The tests can be run by pytest (and from IDEs by running this module)""" |
| 74 | + test_wifi_wpa_psk_simple_from_dict() |
| 75 | + |
| 76 | + # Test using ConnectionProfile.from_dict() to create a ConnectionProfile |
| 77 | + # which can be used to add Wireless WPA PSK connection profile from dict |
| 78 | + # to a running NetworkManager. Requires access and permissions to access |
| 79 | + # a running NetworkManager. The added connection is not saved and deleted |
| 80 | + # immediately (and does not have autoconnect enabled): |
| 81 | + asyncio.run(test_add_wifi_wpa_psk_simple_from_dict()) |
0 commit comments