|
| 1 | +#!/usr/bin/env python |
| 2 | +# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | +import asyncio |
| 4 | +import unittest |
| 5 | +import pytest |
| 6 | +import sdbus |
| 7 | +from sdbus_async.networkmanager import ( |
| 8 | + ConnectionProfile, |
| 9 | + NetworkManagerSettings as SettingsManager, |
| 10 | + NetworkManagerConnectionProperties as ConnectionProfileDict, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +def profile_with_autoconnect_set_to(autoconnect: bool) -> ConnectionProfile: |
| 15 | + dummy_profile: ConnectionProfileDict = { |
| 16 | + "connection": { |
| 17 | + "id": ("s", 'DummyConnection'), |
| 18 | + "uuid": ("s", '16ea7af1-0e35-4036-831e-ced975f48510'), |
| 19 | + "type": ("s", "dummy"), |
| 20 | + "autoconnect": ("b", autoconnect), |
| 21 | + "interface-name": ("s", "dummy-notused0"), |
| 22 | + }, |
| 23 | + } |
| 24 | + profile_from_dbus_dict = ConnectionProfile.from_dbus(dummy_profile) |
| 25 | + # Assert that ConnectionProfile.from_dbus parsed autoconnect correctly: |
| 26 | + assert profile_from_dbus_dict.connection.autoconnect is autoconnect |
| 27 | + return profile_from_dbus_dict |
| 28 | + |
| 29 | + |
| 30 | +def set_sdbus_default_bus() -> None: |
| 31 | + sdbus.set_default_bus(sdbus.sd_bus_open_system()) |
| 32 | + |
| 33 | + |
| 34 | +async def get_settings_by_networkmanager( |
| 35 | + profile: ConnectionProfileDict, |
| 36 | +) -> ConnectionProfileDict: |
| 37 | + """Add a connection and return the settings of NetworkManager |
| 38 | + Uses the helper method get_gettings_by_uuid() which uses get_settings() |
| 39 | +
|
| 40 | + Requires access and permissions to access a running NetworkManager. |
| 41 | + The added (dummy) connection is not saved and deleted immediately""" |
| 42 | + manager = SettingsManager() |
| 43 | + |
| 44 | + # Temporarily add the new Wifi connection to NetworkManager: |
| 45 | + await manager.add_connection_unsaved(profile) |
| 46 | + |
| 47 | + # Get the settings of the temporary connection from NetworkManager: |
| 48 | + uuid = profile["connection"]["uuid"][1] |
| 49 | + connection_profile_dict = await manager.get_settings_by_uuid(uuid) |
| 50 | + |
| 51 | + # Remove the temporay connection (now that we have read its settings): |
| 52 | + await manager.delete_connection_by_uuid(uuid) |
| 53 | + return connection_profile_dict |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_autoconnect_false_returned_by_networkmanager() -> None: |
| 58 | + """Test get_settings_by_uuid() and check that autoconnect = False is set""" |
| 59 | + set_sdbus_default_bus() |
| 60 | + |
| 61 | + # Set autoconnect = ("b", False) and expect it to be returned by NM: |
| 62 | + profile_test = profile_with_autoconnect_set_to(autoconnect=False).to_dbus() |
| 63 | + profile_from_manager = await get_settings_by_networkmanager(profile_test) |
| 64 | + # Assert that NetworkManager did return autoconnect=False (default is True) |
| 65 | + unittest.TestCase().assertTupleEqual( |
| 66 | + profile_from_manager["connection"]["autoconnect"], ("b", False) |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.asyncio |
| 71 | +async def test_autoconnect_true_not_returned_by_networkmanager() -> None: |
| 72 | + """Check autoconnect=True (is default) is not returned by get_settings()""" |
| 73 | + set_sdbus_default_bus() |
| 74 | + |
| 75 | + # Set autoconnect = ("b", True) and expect to not be returned by NM: |
| 76 | + # (NetworkManager does not return a property if it has the default value) |
| 77 | + profile_test = profile_with_autoconnect_set_to(autoconnect=True).to_dbus() |
| 78 | + unittest.TestCase().assertTupleEqual( |
| 79 | + profile_test["connection"]["autoconnect"], ("b", True) |
| 80 | + ) |
| 81 | + # Assert that NetworkManager did not return autoconnect (default is True) |
| 82 | + profile_from_manager = await get_settings_by_networkmanager(profile_test) |
| 83 | + assert "autoconnect" not in profile_from_manager["connection"] |
| 84 | + |
| 85 | + |
| 86 | +def test_autoconnect_true_returned_by_settings_dict_when_requested() -> None: |
| 87 | + """to_settings_dict(defaults=True) returns autoconnect=True (is default)""" |
| 88 | + profile_from_dbus_dict = profile_with_autoconnect_set_to(True) |
| 89 | + settings_dict = profile_from_dbus_dict.to_settings_dict(defaults=True) |
| 90 | + assert settings_dict["connection"]["autoconnect"] is True |
| 91 | + |
| 92 | + |
| 93 | +def test_autoconnect_true_not_returned_by_to_settings_dict() -> None: |
| 94 | + """to_settings_dict(defaults=False) does not return autoconnect=True""" |
| 95 | + |
| 96 | + # Like checked by test_autoconnect_true_not_returned_by_networkmanager() |
| 97 | + # above (which checks that get_settings) does not return autoconnect=True |
| 98 | + # (because it is the default), test the same for .to_settings_dict(): |
| 99 | + profile_from_dbus_dict = profile_with_autoconnect_set_to(True) |
| 100 | + settings_dict = profile_from_dbus_dict.to_settings_dict(defaults=False) |
| 101 | + |
| 102 | + # Assert that networkmanager did not return autoconnect (default is True) |
| 103 | + assert "autoconnect" not in settings_dict["connection"] |
| 104 | + |
| 105 | + |
| 106 | +async def check_to_settings_dict_profile_eqal_to_from_settings_dict() -> None: |
| 107 | + """Async main function to run all tests when not run by pytest""" |
| 108 | + """The tests can be run by pytest (and from IDEs by running this module)""" |
| 109 | + set_sdbus_default_bus() |
| 110 | + await test_autoconnect_false_returned_by_networkmanager() |
| 111 | + await test_autoconnect_true_not_returned_by_networkmanager() |
| 112 | + test_autoconnect_true_returned_by_settings_dict_when_requested() |
| 113 | + test_autoconnect_true_not_returned_by_to_settings_dict() |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + """Main function to run all tests when not run by pytest""" |
| 118 | + asyncio.run(check_to_settings_dict_profile_eqal_to_from_settings_dict()) |
0 commit comments