diff --git a/netjsonconfig/backends/openwrt/converters/interfaces.py b/netjsonconfig/backends/openwrt/converters/interfaces.py index 60dfd9192..6f1a838cc 100644 --- a/netjsonconfig/backends/openwrt/converters/interfaces.py +++ b/netjsonconfig/backends/openwrt/converters/interfaces.py @@ -52,7 +52,6 @@ def __set_dsa_interface(self, interface): ) def to_intermediate_loop(self, block, result, index=None): - result.setdefault('network', []) uci_name = self._get_uci_name(block.get('network') or block['name']) address_list = self.__intermediate_addresses(block) interface = self.__intermediate_interface(block, uci_name) @@ -60,6 +59,7 @@ def to_intermediate_loop(self, block, result, index=None): if self.dsa_interface: uci_device = self.__intermediate_device(interface) if uci_device: + result.setdefault('network', []) result['network'].append(self.sorted_dict(uci_device)) # create one or more "config interface" UCI blocks i = 1 @@ -95,6 +95,9 @@ def __intermediate_addresses(self, interface): if interface.get('type') == 'wireguard': return self.__intermediate_wireguard_addresses(interface) address_list = self.get_copy(interface, 'addresses') + # ignore wireless interfaces without addresses + if not address_list and interface['type'] == 'wireless': + return [] # do not ignore interfaces if they do not contain any address if not address_list: return [{'proto': 'none'}] @@ -117,21 +120,24 @@ def __intermediate_addresses(self, interface): static[address_key].append('{address}/{mask}'.format(**address)) static.update(self.__intermediate_address(address)) if static: - # do not use CIDR notation when using a single ipv4 - # see https://github.com/openwisp/netjsonconfig/issues/54 - if len(static.get('ipaddr', [])) == 1: - network = ip_interface(static['ipaddr'][0]) - static['ipaddr'] = str(network.ip) - static['netmask'] = str(network.netmask) - # do not use lists when using a single ipv6 address - # (avoids to change output of existing configuration) - if len(static.get('ip6addr', [])) == 1: - static['ip6addr'] = static['ip6addr'][0] - result.append(static) + result.append(self.__intermediate_static_address(static)) if dhcp: result += dhcp return result + def __intermediate_static_address(self, uci): + # do not use CIDR notation when using a single ipv4 + # see https://github.com/openwisp/netjsonconfig/issues/54 + if len(uci.get('ipaddr', [])) == 1: + network = ip_interface(uci['ipaddr'][0]) + uci['ipaddr'] = str(network.ip) + uci['netmask'] = str(network.netmask) + # do not use lists when using a single ipv6 address + # (avoids to change output of existing configuration) + if len(uci.get('ip6addr', [])) == 1: + uci['ip6addr'] = uci['ip6addr'][0] + return uci + def __intermediate_wireguard_addresses(self, interface): addresses = interface.pop('addresses') address_list = [] @@ -210,8 +216,10 @@ def __intermediate_device(self, interface): data structure compatible with new syntax introduced in OpenWrt 21.02. """ - device = {} + # ignore wireless interfaces + if interface['type'] == 'wireless': + return device # Add L2 options (needed for > OpenWrt 21.02) self._add_l2_options(device, interface) device.update( diff --git a/netjsonconfig/backends/openwrt/converters/wireless.py b/netjsonconfig/backends/openwrt/converters/wireless.py index 6679c39fd..17cae4bcd 100644 --- a/netjsonconfig/backends/openwrt/converters/wireless.py +++ b/netjsonconfig/backends/openwrt/converters/wireless.py @@ -20,6 +20,7 @@ def to_intermediate_loop(self, block, result, index=None): def __intermediate_wireless(self, interface): if 'wireless' not in interface: return + interface.pop('network', None) wireless = interface['wireless'] # inherit "disabled" attribute from interface if present wireless['disabled'] = interface.get('disabled') @@ -72,8 +73,8 @@ def __intermediate_wireless(self, interface): try: bridges = self._bridged_wifi[interface['name']] except KeyError: - # default to the value of "network" or inteface name - network = [interface.get('network', interface['name'])] + # don't bridge to anything unless explicitly specified + network = [] else: network = bridges wireless['network'] = network diff --git a/tests/openwrt/test_backend.py b/tests/openwrt/test_backend.py index 8d0f3094d..4562b7ae7 100644 --- a/tests/openwrt/test_backend.py +++ b/tests/openwrt/test_backend.py @@ -156,11 +156,7 @@ def test_generate(self): network = tar.getmember('etc/config/network') contents = tar.extractfile(network).read().decode() expected = self._tabs( - """config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' + """config interface 'wlan0' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option proto 'static' @@ -185,7 +181,6 @@ def test_generate(self): option hidden '1' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """ ) diff --git a/tests/openwrt/test_encryption.py b/tests/openwrt/test_encryption.py index ff81110ee..6782edb9f 100644 --- a/tests/openwrt/test_encryption.py +++ b/tests/openwrt/test_encryption.py @@ -27,16 +27,7 @@ class TestEncryption(unittest.TestCase, _TabsMixin): } ] } - _wpa3_personal_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa3_personal_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -45,7 +36,6 @@ class TestEncryption(unittest.TestCase, _TabsMixin): option ifname 'wlan0' option key 'passphrase012345' option mode 'ap' - option network 'wlan0' option ssid 'wpa3-personal' """ @@ -56,7 +46,7 @@ def test_render_wpa3_personal(self): def test_parse_wpa3_personal(self): o = OpenWrt(native=self._wpa3_personal_uci) - self.assertEqual(o.config, self._wpa3_personal_netjson) + self.assertDictEqual(o.config, self._wpa3_personal_netjson) _wpa2_personal_mixed_netjson = { "interfaces": [ @@ -77,16 +67,7 @@ def test_parse_wpa3_personal(self): } ] } - _wpa2_personal_mixed_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_personal_mixed_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -95,7 +76,6 @@ def test_parse_wpa3_personal(self): option ifname 'wlan0' option key 'passphrase012345' option mode 'ap' - option network 'wlan0' option ssid 'wpa2-3-personal-mixed' """ @@ -106,7 +86,7 @@ def test_render_wpa2_personal_mixed(self): def test_parse_wpa2_personal_mixed(self): o = OpenWrt(native=self._wpa2_personal_mixed_uci) - self.assertEqual(o.config, self._wpa2_personal_mixed_netjson) + self.assertDictEqual(o.config, self._wpa2_personal_mixed_netjson) _wpa2_personal_netjson = { "interfaces": [ @@ -127,16 +107,7 @@ def test_parse_wpa2_personal_mixed(self): } ] } - _wpa2_personal_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_personal_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -145,7 +116,6 @@ def test_parse_wpa2_personal_mixed(self): option ifname 'wlan0' option key 'passphrase012345' option mode 'ap' - option network 'wlan0' option ssid 'wpa2-personal' """ @@ -156,7 +126,7 @@ def test_render_wpa2_personal(self): def test_parse_wpa2_personal(self): o = OpenWrt(native=self._wpa2_personal_uci) - self.assertEqual(o.config, self._wpa2_personal_netjson) + self.assertDictEqual(o.config, self._wpa2_personal_netjson) _wpa_personal_mixed_netjson = { "interfaces": [ @@ -177,16 +147,7 @@ def test_parse_wpa2_personal(self): } ] } - _wpa_personal_mixed_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa_personal_mixed_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -195,7 +156,6 @@ def test_parse_wpa2_personal(self): option ifname 'wlan0' option key 'passphrase012345' option mode 'ap' - option network 'wlan0' option ssid 'wpa2-personal' """ @@ -206,7 +166,7 @@ def test_render_wpa_personal_mixed(self): def test_parse_wpa_personal_mixed(self): o = OpenWrt(native=self._wpa_personal_mixed_uci) - self.assertEqual(o.config, self._wpa_personal_mixed_netjson) + self.assertDictEqual(o.config, self._wpa_personal_mixed_netjson) _wpa_personal_netjson = { "interfaces": [ @@ -226,16 +186,7 @@ def test_parse_wpa_personal_mixed(self): } ] } - _wpa_personal_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa_personal_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -243,7 +194,6 @@ def test_parse_wpa_personal_mixed(self): option ifname 'wlan0' option key 'passphrase012345' option mode 'ap' - option network 'wlan0' option ssid 'wpa-personal' """ @@ -254,7 +204,7 @@ def test_render_wpa_personal(self): def test_parse_wpa_personal(self): o = OpenWrt(native=self._wpa_personal_uci) - self.assertEqual(o.config, self._wpa_personal_netjson) + self.assertDictEqual(o.config, self._wpa_personal_netjson) _wpa2_enterprise_mixed_ap_netjson = { "interfaces": [ @@ -276,16 +226,7 @@ def test_parse_wpa_personal(self): } ] } - _wpa2_enterprise_mixed_ap_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_enterprise_mixed_ap_uci = """package wireless config wifi-iface 'wifi_wlan0' option acct_secret 'radius_secret' @@ -297,7 +238,6 @@ def test_parse_wpa_personal(self): option ifname 'wlan0' option key 'radius_secret' option mode 'ap' - option network 'wlan0' option server '192.168.0.1' option ssid 'enterprise-mixed' """ @@ -309,7 +249,7 @@ def test_render_wpa2_enterprise_mixed_ap(self): def test_parse_wpa2_enterprise_mixed_ap(self): o = OpenWrt(native=self._wpa2_enterprise_mixed_ap_uci) - self.assertEqual(o.config, self._wpa2_enterprise_mixed_ap_netjson) + self.assertDictEqual(o.config, self._wpa2_enterprise_mixed_ap_netjson) _wpa3_enterprise_ap_netjson = { "interfaces": [ @@ -340,16 +280,7 @@ def test_parse_wpa2_enterprise_mixed_ap(self): } ] } - _wpa3_enterprise_ap_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa3_enterprise_ap_uci = """package wireless config wifi-iface 'wifi_wlan0' option acct_interval '300' @@ -369,7 +300,6 @@ def test_parse_wpa2_enterprise_mixed_ap(self): option key 'radius_secret' option mode 'ap' option nasid '2' - option network 'wlan0' option port '1812' option server '192.168.0.1' option ssid 'wpa3-enterprise' @@ -383,7 +313,7 @@ def test_render_wpa3_enterprise(self): def test_parse_wpa3_enterprise(self): o = OpenWrt(native=self._wpa3_enterprise_ap_uci) - self.assertEqual(o.config, self._wpa3_enterprise_ap_netjson) + self.assertDictEqual(o.config, self._wpa3_enterprise_ap_netjson) _wpa2_enterprise_ap_netjson = { "interfaces": [ @@ -409,16 +339,7 @@ def test_parse_wpa3_enterprise(self): } ] } - _wpa2_enterprise_ap_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_enterprise_ap_uci = """package wireless config wifi-iface 'wifi_wlan0' option acct_port '1813' @@ -433,7 +354,6 @@ def test_parse_wpa3_enterprise(self): option key 'radius_secret' option mode 'ap' option nasid '2' - option network 'wlan0' option port '1812' option server '192.168.0.1' option ssid 'wpa2-802.1x' @@ -447,7 +367,7 @@ def test_render_wpa2_enterprise(self): def test_parse_wpa2_enterprise(self): o = OpenWrt(native=self._wpa2_enterprise_ap_uci) - self.assertEqual(o.config, self._wpa2_enterprise_ap_netjson) + self.assertDictEqual(o.config, self._wpa2_enterprise_ap_netjson) _wpa_enterprise_mixed_ap_netjson = { "interfaces": [ @@ -468,16 +388,7 @@ def test_parse_wpa2_enterprise(self): } ] } - _wpa_enterprise_mixed_ap_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa_enterprise_mixed_ap_uci = """package wireless config wifi-iface 'wifi_wlan0' option acct_secret 'radius_secret' @@ -488,7 +399,6 @@ def test_parse_wpa2_enterprise(self): option ifname 'wlan0' option key 'radius_secret' option mode 'ap' - option network 'wlan0' option server '192.168.0.1' option ssid 'enterprise-mixed' """ @@ -500,7 +410,7 @@ def test_render_wpa_enterprise_mixed_ap(self): def test_parse_wpa_enterprise_mixed_ap(self): o = OpenWrt(native=self._wpa_enterprise_mixed_ap_uci) - self.assertEqual(o.config, self._wpa_enterprise_mixed_ap_netjson) + self.assertDictEqual(o.config, self._wpa_enterprise_mixed_ap_netjson) _wpa_enterprise_ap_netjson = { "interfaces": [ @@ -521,16 +431,7 @@ def test_parse_wpa_enterprise_mixed_ap(self): } ] } - _wpa_enterprise_ap_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa_enterprise_ap_uci = """package wireless config wifi-iface 'wifi_wlan0' option acct_secret 'radius_secret' @@ -541,7 +442,6 @@ def test_parse_wpa_enterprise_mixed_ap(self): option ifname 'wlan0' option key 'radius_secret' option mode 'ap' - option network 'wlan0' option server '192.168.0.1' option ssid 'enterprise' """ @@ -553,7 +453,7 @@ def test_render_wpa_enterprise_ap(self): def test_parse_wpa_enterprise_ap(self): o = OpenWrt(native=self._wpa_enterprise_ap_uci) - self.assertEqual(o.config, self._wpa_enterprise_ap_netjson) + self.assertDictEqual(o.config, self._wpa_enterprise_ap_netjson) _wpa3_enterprise_client_netjson = { "interfaces": [ @@ -578,16 +478,7 @@ def test_parse_wpa_enterprise_ap(self): ] } - _wpa3_enterprise_client_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa3_enterprise_client_uci = """package wireless config wifi-iface 'wifi_wlan0' option bssid '00:26:b9:20:5f:09' @@ -598,7 +489,6 @@ def test_parse_wpa_enterprise_ap(self): option ieee80211w '2' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option password 'test-password' option ssid 'enterprise-client' """ @@ -610,7 +500,7 @@ def test_render_wpa3_enterprise_client(self): def test_parse_wpa3_enterprise_client(self): o = OpenWrt(native=self._wpa3_enterprise_client_uci) - self.assertEqual(o.config, self._wpa3_enterprise_client_netjson) + self.assertDictEqual(o.config, self._wpa3_enterprise_client_netjson) _wpa2_enterprise_tls_client_netjson = { "interfaces": [ @@ -634,16 +524,7 @@ def test_parse_wpa3_enterprise_client(self): } ] } - _wpa2_enterprise_client_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_enterprise_client_uci = """package wireless config wifi-iface 'wifi_wlan0' option bssid '00:26:b9:20:5f:09' @@ -654,7 +535,6 @@ def test_parse_wpa3_enterprise_client(self): option ieee80211w '1' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option password 'test-password' option ssid 'enterprise-client' """ @@ -666,7 +546,7 @@ def test_render_wpa2_enterprise_client(self): def test_parse_wpa2_enterprise_client(self): o = OpenWrt(native=self._wpa2_enterprise_client_uci) - self.assertEqual(o.config, self._wpa2_enterprise_tls_client_netjson) + self.assertDictEqual(o.config, self._wpa2_enterprise_tls_client_netjson) _wpa2_enterprise_ttls_client_netjson = { "interfaces": [ @@ -690,16 +570,7 @@ def test_parse_wpa2_enterprise_client(self): } ] } - _wpa2_enterprise_ttls_client_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_enterprise_ttls_client_uci = """package wireless config wifi-iface 'wifi_wlan0' option auth 'MSCHAPV2' @@ -710,7 +581,6 @@ def test_parse_wpa2_enterprise_client(self): option identity 'test-identity' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option password 'test-password' option ssid 'enterprise-client' """ @@ -722,7 +592,7 @@ def test_render_wpa2_enterprise_ttls_client(self): def test_parse_wpa2_enterprise_ttls_client(self): o = OpenWrt(native=self._wpa2_enterprise_ttls_client_uci) - self.assertEqual(o.config, self._wpa2_enterprise_ttls_client_netjson) + self.assertDictEqual(o.config, self._wpa2_enterprise_ttls_client_netjson) _wpa2_enterprise_peap_client_netjson = { "interfaces": [ @@ -746,16 +616,7 @@ def test_parse_wpa2_enterprise_ttls_client(self): } ] } - _wpa2_enterprise_peap_client_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_enterprise_peap_client_uci = """package wireless config wifi-iface 'wifi_wlan0' option auth 'EAP-MSCHAPV2' @@ -766,7 +627,6 @@ def test_parse_wpa2_enterprise_ttls_client(self): option identity 'test-identity' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option password 'test-password' option ssid 'enterprise-client' """ @@ -778,7 +638,7 @@ def test_render_wpa2_enterprise_peap_client(self): def test_parse_wpa2_enterprise_peap_client(self): o = OpenWrt(native=self._wpa2_enterprise_peap_client_uci) - self.assertEqual(o.config, self._wpa2_enterprise_peap_client_netjson) + self.assertDictEqual(o.config, self._wpa2_enterprise_peap_client_netjson) _wpa2_enterprise_tls_client_auth_netjson = { "interfaces": [ @@ -803,16 +663,7 @@ def test_parse_wpa2_enterprise_peap_client(self): ] } - _wpa2_enterprise_tls_client_auth_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_enterprise_tls_client_auth_uci = """package wireless config wifi-iface 'wifi_wlan0' option bssid '00:26:b9:20:5f:09' @@ -822,7 +673,6 @@ def test_parse_wpa2_enterprise_peap_client(self): option identity 'test-identity' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option password 'test-password' option ssid 'enterprise-client' """ @@ -850,16 +700,7 @@ def test_render_wpa2_enterprise_tls_client_auth(self): } ] } - _wep_open_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wep_open_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -868,7 +709,6 @@ def test_render_wpa2_enterprise_tls_client_auth(self): option key '1' option key1 's:wepkey1234567' option mode 'ap' - option network 'wlan0' option ssid 'wep' """ @@ -879,7 +719,7 @@ def test_render_wep_open(self): def test_parse_wep_open(self): o = OpenWrt(native=self._wep_open_uci) - self.assertEqual(o.config, self._wep_open_netjson) + self.assertDictEqual(o.config, self._wep_open_netjson) _wep_shared_netjson = { "interfaces": [ @@ -899,16 +739,7 @@ def test_parse_wep_open(self): } ] } - _wep_shared_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wep_shared_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -917,7 +748,6 @@ def test_parse_wep_open(self): option key '1' option key1 'wepkey1234567' option mode 'ap' - option network 'wlan0' option ssid 'wep' """ @@ -928,7 +758,7 @@ def test_render_wep_shared(self): def test_parse_wep_shared(self): o = OpenWrt(native=self._wep_shared_uci) - self.assertEqual(o.config, self._wep_shared_netjson) + self.assertDictEqual(o.config, self._wep_shared_netjson) def test_encryption_disabled(self): o = OpenWrt( @@ -953,23 +783,13 @@ def test_encryption_disabled(self): } ) expected = self._tabs( - """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option encryption 'none' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyNetwork' """ ) @@ -989,23 +809,13 @@ def test_encryption_disabled(self): } ] } - _no_encryption_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _no_encryption_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option encryption 'none' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'open' """ @@ -1016,7 +826,7 @@ def test_render_no_encryption(self): def test_parse_no_encryption(self): o = OpenWrt(native=self._no_encryption_uci) - self.assertEqual(o.config, self._no_encryption_netjson) + self.assertDictEqual(o.config, self._no_encryption_netjson) _wpa2_80211s_netjson = { "interfaces": [ @@ -1036,16 +846,7 @@ def test_parse_no_encryption(self): } ] } - _wpa2_80211s_uci = """package network - -config device 'device_mesh0' - option name 'mesh0' - -config interface 'mesh0' - option device 'mesh0' - option proto 'none' - -package wireless + _wpa2_80211s_uci = """package wireless config wifi-iface 'wifi_mesh0' option device 'radio0' @@ -1054,7 +855,6 @@ def test_parse_no_encryption(self): option key 'passphrase012345' option mesh_id 'encrypted-mesh' option mode 'mesh' - option network 'mesh0' """ def test_render_wpa2_80211s(self): @@ -1064,7 +864,7 @@ def test_render_wpa2_80211s(self): def test_parse_wpa2_80211s(self): o = OpenWrt(native=self._wpa2_80211s_uci) - self.assertEqual(o.config, self._wpa2_80211s_netjson) + self.assertDictEqual(o.config, self._wpa2_80211s_netjson) _wpa3_80211s_netjson = { "interfaces": [ @@ -1085,16 +885,7 @@ def test_parse_wpa2_80211s(self): } ] } - _wpa3_80211s_uci = """package network - -config device 'device_mesh0' - option name 'mesh0' - -config interface 'mesh0' - option device 'mesh0' - option proto 'none' - -package wireless + _wpa3_80211s_uci = """package wireless config wifi-iface 'wifi_mesh0' option device 'radio0' @@ -1104,7 +895,6 @@ def test_parse_wpa2_80211s(self): option key 'passphrase012345' option mesh_id 'encrypted-mesh' option mode 'mesh' - option network 'mesh0' """ def test_render_wpa3_80211s(self): @@ -1114,7 +904,7 @@ def test_render_wpa3_80211s(self): def test_parse_wpa3_80211s(self): o = OpenWrt(native=self._wpa3_80211s_uci) - self.assertEqual(o.config, self._wpa3_80211s_netjson) + self.assertDictEqual(o.config, self._wpa3_80211s_netjson) _wpa2_adhoc_netjson = { "interfaces": [ @@ -1135,16 +925,7 @@ def test_parse_wpa3_80211s(self): } ] } - _wpa2_adhoc_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wpa2_adhoc_uci = """package wireless config wifi-iface 'wifi_wlan0' option bssid '00:26:b9:20:5f:09' @@ -1153,7 +934,6 @@ def test_parse_wpa3_80211s(self): option ifname 'wlan0' option key 'passphrase012345' option mode 'adhoc' - option network 'wlan0' option ssid 'encrypted-adhoc' """ @@ -1164,7 +944,7 @@ def test_render_wpa2_adhoc(self): def test_parse_wpa2_adhoc(self): o = OpenWrt(native=self._wpa2_adhoc_uci) - self.assertEqual(o.config, self._wpa2_adhoc_netjson) + self.assertDictEqual(o.config, self._wpa2_adhoc_netjson) _wps_ap_netjson = { "interfaces": [ @@ -1185,23 +965,13 @@ def test_parse_wpa2_adhoc(self): } ] } - _wps_ap_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wps_ap_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option encryption 'psk' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'wps-ssid' option wps_label '0' option wps_pin 'pin1234' @@ -1215,7 +985,7 @@ def test_render_wps_ap(self): def test_parse_wps_ap(self): o = OpenWrt(native=self._wps_ap_uci) - self.assertEqual(o.config, self._wps_ap_netjson) + self.assertDictEqual(o.config, self._wps_ap_netjson) def test_render_ieee80211w(self): _netjson_wpa3_personal_cipher_tkip = { @@ -1337,16 +1107,7 @@ def test_render_ieee80211w(self): ] } - _owe_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _owe_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -1354,7 +1115,6 @@ def test_render_ieee80211w(self): option ieee80211w '1' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'owe_ssid' """ @@ -1365,4 +1125,4 @@ def test_render_owe(self): def test_parse_owe(self): o = OpenWrt(native=self._owe_uci) - self.assertEqual(o.config, self._owe_netjson) + self.assertDictEqual(o.config, self._owe_netjson) diff --git a/tests/openwrt/test_wireless.py b/tests/openwrt/test_wireless.py index 54d33ec5e..0943dc3a7 100644 --- a/tests/openwrt/test_wireless.py +++ b/tests/openwrt/test_wireless.py @@ -37,11 +37,7 @@ class TestWireless(unittest.TestCase, _TabsMixin): } _wifi_uci = """package network -config device 'device_wlan0' - option name 'wlan0' - config interface 'wlan0' - option device 'wlan0' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option proto 'static' @@ -56,7 +52,6 @@ class TestWireless(unittest.TestCase, _TabsMixin): option ifname 'wlan0' option isolate '1' option mode 'ap' - option network 'wlan0' option rts '1300' option ssid 'MyWifiAP' """ @@ -68,11 +63,11 @@ def test_render_wifi_interface(self): def test_parse_wifi_interface(self): o = OpenWrt(native=self._wifi_uci) - self.assertEqual(o.config, self._wifi_netjson) + self.assertDictEqual(o.config, self._wifi_netjson) def test_parse_wifi_interface_without_ifname(self): o = OpenWrt(native=self._wifi_uci.replace(" option ifname 'wlan0'\n", '')) - self.assertEqual(o.config, self._wifi_netjson) + self.assertDictEqual(o.config, self._wifi_netjson) def test_parse_wifi_interface_partial(self): o = OpenWrt( @@ -82,7 +77,6 @@ def test_parse_wifi_interface_partial(self): option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """ ) @@ -99,7 +93,7 @@ def test_parse_wifi_interface_partial(self): } ] } - self.assertEqual(o.config, expected) + self.assertDictEqual(o.config, expected) _multiple_wifi_netjson = { "interfaces": [ @@ -128,18 +122,10 @@ def test_parse_wifi_interface_partial(self): } _multiple_wifi_uci = """package network -config device 'device_wlan0' - option name 'wlan0' - config interface 'wlan0' - option device 'wlan0' option proto 'dhcp' -config device 'device_wlan1' - option name 'wlan1' - config interface 'wlan1' - option device 'wlan1' option proto 'dhcp' package wireless @@ -148,7 +134,6 @@ def test_parse_wifi_interface_partial(self): option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'ap-ssid' config wifi-iface 'wifi_wlan1' @@ -156,7 +141,6 @@ def test_parse_wifi_interface_partial(self): option device 'radio1' option ifname 'wlan1' option mode 'adhoc' - option network 'wlan1' option ssid 'adhoc-ssid' """ @@ -167,16 +151,11 @@ def test_render_multiple_wifi(self): def test_parse_multiple_wifi(self): o = OpenWrt(native=self._multiple_wifi_uci) - self.assertEqual(o.config, self._multiple_wifi_netjson) + self.assertDictEqual(o.config, self._multiple_wifi_netjson) _wifi_bridge_netjson = { "interfaces": [ {"name": "eth0.1", "type": "ethernet"}, - { - "name": "wlan0", - "type": "wireless", - "wireless": {"radio": "radio0", "mode": "access_point", "ssid": "open"}, - }, { "name": "br-lan", "type": "bridge", @@ -190,6 +169,11 @@ def test_parse_multiple_wifi(self): } ], }, + { + "name": "wlan0", + "type": "wireless", + "wireless": {"radio": "radio0", "mode": "access_point", "ssid": "open"}, + }, ] } _wifi_bridge_uci = """package network @@ -201,13 +185,6 @@ def test_parse_multiple_wifi(self): option device 'eth0.1' option proto 'none' -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - config device 'device_br_lan' option name 'br-lan' list ports 'eth0.1' @@ -237,59 +214,46 @@ def test_render_wifi_bridge(self): def test_parse_wifi_bridge(self): o = OpenWrt(native=self._wifi_bridge_uci) - wifi_bridge_netjson = self._wifi_bridge_netjson.copy() - self.assertEqual(o.config, wifi_bridge_netjson) + self.assertDictEqual(o.config, self._wifi_bridge_netjson) - _wifi_networks_netjson = { + _wifi_station_dhcp = { "interfaces": [ - {"name": "eth0", "type": "ethernet"}, { - "name": "wlan0", + "name": "wsta0", "type": "wireless", "addresses": [{"proto": "dhcp", "family": "ipv4"}], "wireless": { "radio": "radio0", - "mode": "access_point", + "mode": "station", "ssid": "open", - "network": ["wlan0", "eth0"], + "wds": False, }, }, ] } _wifi_networks_uci = """package network -config device 'device_eth0' - option name 'eth0' - -config interface 'eth0' - option device 'eth0' - option proto 'none' - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' +config interface 'wsta0' option proto 'dhcp' package wireless -config wifi-iface 'wifi_wlan0' +config wifi-iface 'wifi_wsta0' option device 'radio0' - option ifname 'wlan0' - option mode 'ap' - option network 'wlan0 eth0' + option ifname 'wsta0' + option mode 'sta' option ssid 'open' + option wds '0' """ - def test_render_wifi_networks(self): - o = OpenWrt(self._wifi_networks_netjson) + def test_render_wifi_station_dhcp(self): + o = OpenWrt(self._wifi_station_dhcp) expected = self._tabs(self._wifi_networks_uci) self.assertEqual(o.render(), expected) - def test_parse_wifi_network(self): + def test_parse_wifi_station_dhcp(self): o = OpenWrt(native=self._wifi_networks_uci) - self.assertEqual(o.config, self._wifi_networks_netjson) + self.assertDictEqual(o.config, self._wifi_station_dhcp) def test_render_wireless_empty_network_attr(self): o = OpenWrt( @@ -309,22 +273,12 @@ def test_render_wireless_empty_network_attr(self): } ) expected = self._tabs( - """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'open' """ ) @@ -363,27 +317,16 @@ def test_wireless_network_attr_validation(self): { "name": "wlan0", "type": "wireless", - "network": "guests", "wireless": {"radio": "radio0", "mode": "access_point", "ssid": "open"}, } ] } - _interface_network_uci = """package network - -config device 'device_guests' - option name 'wlan0' - -config interface 'guests' - option device 'wlan0' - option proto 'none' - -package wireless + _interface_network_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'guests' option ssid 'open' """ @@ -394,7 +337,7 @@ def test_render_interface_network(self): def test_parse_interface_network(self): o = OpenWrt(native=self._interface_network_uci) - self.assertEqual(o.config, self._interface_network_netjson) + self.assertDictEqual(o.config, self._interface_network_netjson) def test_network_dot_conversion(self): o = OpenWrt( @@ -414,16 +357,7 @@ def test_network_dot_conversion(self): } ) expected = self._tabs( - """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -453,16 +387,7 @@ def test_network_dash_conversion(self): } ) expected = self._tabs( - """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -489,17 +414,7 @@ def test_network_dash_conversion(self): } ] } - _disabled_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option enabled '0' - option proto 'none' - -package wireless + _disabled_uci = """package wireless config wifi-iface 'wifi_wlan0' option bssid '00:11:22:33:44:55' @@ -507,7 +422,6 @@ def test_network_dash_conversion(self): option disabled '1' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option ssid 'mywifi' """ @@ -522,7 +436,7 @@ def test_render_interface_disabled(self): def test_parse_interface_disabled_full(self): o = OpenWrt(native=self._disabled_uci) - self.assertEqual(o.config, self._disabled_netjson) + self.assertDictEqual(o.config, self._disabled_netjson) def test_parse_interface_disabled_partial(self): o = OpenWrt( @@ -538,7 +452,7 @@ def test_parse_interface_disabled_partial(self): option ssid 'mywifi' """ ) - self.assertEqual(o.config, self._disabled_netjson) + self.assertDictEqual(o.config, self._disabled_netjson) _wds_netjson = { "interfaces": [ @@ -554,22 +468,12 @@ def test_parse_interface_disabled_partial(self): } ] } - _wds_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _wds_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWdsAp' option wds '1' """ @@ -581,7 +485,7 @@ def test_render_wds_ap(self): def test_parse_wds_ap(self): o = OpenWrt(native=self._wds_uci) - self.assertEqual(o.config, self._wds_netjson) + self.assertDictEqual(o.config, self._wds_netjson) def test_wifi_options_zero(self): """ @@ -608,22 +512,12 @@ def test_wifi_options_zero(self): } ) expected = self._tabs( - """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' option wmm '1' """ @@ -645,16 +539,7 @@ def test_wifi_options_zero(self): } ] } - _macfilter_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _macfilter_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -663,7 +548,6 @@ def test_wifi_options_zero(self): list maclist 'E8:94:F6:33:8C:1D' list maclist '42:6c:8f:95:0f:00' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """ @@ -674,7 +558,7 @@ def test_render_macfilter(self): def test_parse_macfilter(self): o = OpenWrt(native=self._macfilter_uci) - self.assertEqual(o.config, self._macfilter_netjson) + self.assertDictEqual(o.config, self._macfilter_netjson) def test_maclist_format(self): o = OpenWrt( @@ -713,6 +597,14 @@ def test_maclist_format(self): _wds_bridge_netjson = { "interfaces": [ + # WDS bridge + { + "name": "br-wds_bridge", + "network": "wds_bridge", + "type": "bridge", + "addresses": [{"proto": "dhcp", "family": "ipv4"}], + "bridge_members": ["wlan0", "wlan1"], + }, # client { "name": "wlan0", @@ -735,32 +627,10 @@ def test_maclist_format(self): "ssid": "FreeRomaWifi", }, }, - # WDS bridge - { - "name": "br-wds_bridge", - "network": "wds_bridge", - "type": "bridge", - "addresses": [{"proto": "dhcp", "family": "ipv4"}], - "bridge_members": ["wlan0", "wlan1"], - }, ] } _wds_bridge_uci = """package network -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -config device 'device_wlan1' - option name 'wlan1' - -config interface 'wlan1' - option device 'wlan1' - option proto 'none' - config device 'device_wds_bridge' option name 'br-wds_bridge' list ports 'wlan0' @@ -797,7 +667,7 @@ def test_render_wds_bridge(self): def test_parse_wds_bridge(self): o = OpenWrt(native=self._wds_bridge_uci) - self.assertEqual(o.config, self._wds_bridge_netjson) + self.assertDictEqual(o.config, self._wds_bridge_netjson) _80211r_netjson = { "interfaces": [ @@ -818,16 +688,7 @@ def test_parse_wds_bridge(self): } ] } - _80211r_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _80211r_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -849,7 +710,7 @@ def test_render_access_point_80211r(self): def test_parse_access_point_80211r(self): o = OpenWrt(native=self._80211r_uci) - self.assertEqual(o.config, self._80211r_netjson) + self.assertDictEqual(o.config, self._80211r_netjson) with self.subTest('ignore bogus reassociation_deadline'): bogus_uci = self._80211r_uci @@ -859,7 +720,7 @@ def test_parse_access_point_80211r(self): o = OpenWrt(native=bogus_uci) netjson_80211r = deepcopy(self._80211r_netjson) del netjson_80211r['interfaces'][0]['wireless']['reassociation_deadline'] - self.assertEqual(o.config, netjson_80211r) + self.assertDictEqual(o.config, netjson_80211r) _80211r_false_netjson = { "interfaces": [ @@ -881,16 +742,7 @@ def test_parse_access_point_80211r(self): } ] } - _80211r_false_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _80211r_false_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -909,15 +761,6 @@ def test_render_access_point_80211r_false(self): _80211s_netjson = { "interfaces": [ - { - "name": "mesh0", - "type": "wireless", - "wireless": { - "radio": "radio0", - "mode": "802.11s", - "mesh_id": "ninux", - }, - }, { "name": "br-lan", "network": "lan", @@ -932,17 +775,19 @@ def test_render_access_point_80211r_false(self): } ], }, + { + "name": "mesh0", + "type": "wireless", + "wireless": { + "radio": "radio0", + "mode": "802.11s", + "mesh_id": "ninux", + }, + }, ] } _80211s_uci = """package network -config device 'device_mesh0' - option name 'mesh0' - -config interface 'mesh0' - option device 'mesh0' - option proto 'none' - config device 'device_lan' option name 'br-lan' list ports 'mesh0' @@ -971,7 +816,7 @@ def test_render_mesh_80211s(self): def test_parse_mesh_80211s(self): o = OpenWrt(native=self._80211s_uci) - self.assertEqual(o.config, self._80211s_netjson) + self.assertDictEqual(o.config, self._80211s_netjson) _bssid_netjson = { "interfaces": [ @@ -1039,16 +884,7 @@ def test_bssid_station(self): } ] } - _list_option_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _list_option_uci = """package wireless config wifi-iface 'wifi_wlan0' list basic_rate '6000' @@ -1056,7 +892,6 @@ def test_bssid_station(self): option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'open' """ @@ -1067,7 +902,7 @@ def test_render_list_option(self): def test_parse_list_option(self): o = OpenWrt(native=self._list_option_uci) - self.assertEqual(o.config, self._list_option_netjson) + self.assertDictEqual(o.config, self._list_option_netjson) def test_isolate(self): o = OpenWrt( @@ -1102,23 +937,13 @@ def test_isolate(self): } ] } - _macaddr_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _macaddr_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option macaddr 'E8:94:F6:33:8C:00' option mode 'ap' - option network 'wlan0' option ssid 'open' """ @@ -1129,7 +954,7 @@ def test_render_macaddr_override(self): def test_parse_macaddr_override(self): o = OpenWrt(native=self._macaddr_uci) - self.assertEqual(o.config, self._macaddr_netjson) + self.assertDictEqual(o.config, self._macaddr_netjson) _custom_id_netjson = { "interfaces": [ @@ -1145,22 +970,12 @@ def test_parse_macaddr_override(self): } ] } - _custom_id_uci = """package network - -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - -package wireless + _custom_id_uci = """package wireless config wifi-iface 'arbitrary_id' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """ @@ -1171,7 +986,7 @@ def test_render_wifi_custom_id(self): def test_parse_wifi_custom_id(self): o = OpenWrt(native=self._custom_id_uci) - self.assertEqual(o.config, self._custom_id_netjson) + self.assertDictEqual(o.config, self._custom_id_netjson) _wifi_simplified_bridge_netjson = { "interfaces": [ @@ -1204,13 +1019,6 @@ def test_parse_wifi_custom_id(self): option device 'br-lan' option proto 'none' -config device 'device_wlan0' - option name 'wlan0' - -config interface 'wlan0' - option device 'wlan0' - option proto 'none' - package wireless config wifi-iface 'wifi_wlan0' @@ -1228,10 +1036,17 @@ def test_render_simplified_wifi_bridge(self): def test_parse_simplified_wifi_bridge(self): o = OpenWrt(native=self._wifi_simplified_bridge_uci) - self.assertEqual(o.config, self._wifi_simplified_bridge_netjson) + self.assertDictEqual(o.config, self._wifi_simplified_bridge_netjson) _wireless_interface_netjson = { "interfaces": [ + # bridge + { + "name": "br-cpwifi", + "network": "cpwifi", + "type": "bridge", + "bridge_members": ["cpwlan0", "cpwlan1"], + }, # client { "name": "cpwlan0", @@ -1257,31 +1072,10 @@ def test_parse_simplified_wifi_bridge(self): "isolate": True, }, }, - # bridge - { - "name": "br-cpwifi", - "network": "cpwifi", - "type": "bridge", - "bridge_members": ["cpwlan0", "cpwlan1"], - }, ] } _wireless_interface_uci = """package network -config device 'device_cpwlan0' - option name 'cpwlan0' - -config interface 'cpwlan0' - option device 'cpwlan0' - option proto 'none' - -config device 'device_cpwlan1' - option name 'cpwlan1' - -config interface 'cpwlan1' - option device 'cpwlan1' - option proto 'none' - config device 'device_cpwifi' option name 'br-cpwifi' list ports 'cpwlan0' @@ -1322,4 +1116,54 @@ def test_render_wireless_interface_name(self): def test_parse_wireless_interface_name(self): o = OpenWrt(native=self._wireless_interface_uci) - self.assertEqual(o.config, self._wireless_interface_netjson) + self.assertDictEqual(o.config, self._wireless_interface_netjson) + + _wireless_only_netjson = { + "interfaces": [ + { + "name": "wlan0", + "type": "wireless", + "wireless": { + "radio": "radio0", + "mode": "access_point", + "ssid": "WifiVLAN10", + "network": ["vlan10"], + }, + }, + { + "name": "wlan1", + "type": "wireless", + "wireless": { + "radio": "radio0", + "mode": "access_point", + "ssid": "WifiVLAN20", + "network": ["vlan20"], + }, + }, + ] + } + _wireless_only_uci = """package wireless + +config wifi-iface 'wifi_wlan0' + option device 'radio0' + option ifname 'wlan0' + option mode 'ap' + option network 'vlan10' + option ssid 'WifiVLAN10' + +config wifi-iface 'wifi_wlan1' + option device 'radio0' + option ifname 'wlan1' + option mode 'ap' + option network 'vlan20' + option ssid 'WifiVLAN20' +""" + + def test_render_wireless_only(self): + o = OpenWrt(self._wireless_only_netjson) + expected = self._tabs(self._wireless_only_uci) + self.assertEqual(o.render(), expected) + + def test_parse_wireless_only(self): + o = OpenWrt(native=self._wireless_only_uci) + self.assertDictEqual(o.config, self._wireless_only_netjson) diff --git a/tests/openwrt/test_wireless_legacy.py b/tests/openwrt/test_wireless_legacy.py index b5c814de1..d93f1a0b1 100644 --- a/tests/openwrt/test_wireless_legacy.py +++ b/tests/openwrt/test_wireless_legacy.py @@ -51,7 +51,6 @@ class TestWireless(unittest.TestCase, _TabsMixin): option hidden '1' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option rts '1300' option ssid 'MyWifiAP' """ @@ -63,7 +62,7 @@ def test_render_wifi_interface(self): def test_parse_wifi_interface(self): o = OpenWrt(native=self._wifi_uci, dsa=False) - self.assertEqual(o.config, self._wifi_netjson) + self.assertDictEqual(o.config, self._wifi_netjson) def test_parse_wifi_interface_partial(self): o = OpenWrt( @@ -74,7 +73,6 @@ def test_parse_wifi_interface_partial(self): option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """, ) @@ -91,7 +89,7 @@ def test_parse_wifi_interface_partial(self): } ] } - self.assertEqual(o.config, expected) + self.assertDictEqual(o.config, expected) _multiple_wifi_netjson = { "interfaces": [ @@ -134,7 +132,6 @@ def test_parse_wifi_interface_partial(self): option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'ap-ssid' config wifi-iface 'wifi_wlan1' @@ -142,7 +139,6 @@ def test_parse_wifi_interface_partial(self): option device 'radio1' option ifname 'wlan1' option mode 'adhoc' - option network 'wlan1' option ssid 'adhoc-ssid' """ @@ -153,16 +149,11 @@ def test_render_multiple_wifi(self): def test_parse_multiple_wifi(self): o = OpenWrt(native=self._multiple_wifi_uci, dsa=False) - self.assertEqual(o.config, self._multiple_wifi_netjson) + self.assertDictEqual(o.config, self._multiple_wifi_netjson) _wifi_bridge_netjson = { "interfaces": [ {"name": "eth0.1", "type": "ethernet"}, - { - "name": "wlan0", - "type": "wireless", - "wireless": {"radio": "radio0", "mode": "access_point", "ssid": "open"}, - }, { "name": "br-lan", "type": "bridge", @@ -176,6 +167,11 @@ def test_parse_multiple_wifi(self): } ], }, + { + "name": "wlan0", + "type": "wireless", + "wireless": {"radio": "radio0", "mode": "access_point", "ssid": "open"}, + }, ] } _wifi_bridge_uci = """package network @@ -184,10 +180,6 @@ def test_parse_multiple_wifi(self): option ifname 'eth0.1' option proto 'none' -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - config interface 'br_lan' option ifname 'eth0.1 wlan0' option ipaddr '192.168.1.1' @@ -213,7 +205,7 @@ def test_render_wifi_bridge(self): def test_parse_wifi_bridge(self): o = OpenWrt(native=self._wifi_bridge_uci, dsa=False) wifi_bridge_netjson = self._wifi_bridge_netjson.copy() - self.assertEqual(o.config, wifi_bridge_netjson) + self.assertDictEqual(o.config, wifi_bridge_netjson) _wifi_networks_netjson = { "interfaces": [ @@ -258,7 +250,7 @@ def test_render_wifi_networks(self): def test_parse_wifi_network(self): o = OpenWrt(native=self._wifi_networks_uci, dsa=False) - self.assertEqual(o.config, self._wifi_networks_netjson) + self.assertDictEqual(o.config, self._wifi_networks_netjson) def test_render_wireless_empty_network_attr(self): o = OpenWrt( @@ -279,19 +271,12 @@ def test_render_wireless_empty_network_attr(self): dsa=False, ) expected = self._tabs( - """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'open' """ ) @@ -330,24 +315,16 @@ def test_wireless_network_attr_validation(self): { "name": "wlan0", "type": "wireless", - "network": "guests", "wireless": {"radio": "radio0", "mode": "access_point", "ssid": "open"}, } ] } - _interface_network_uci = """package network - -config interface 'guests' - option ifname 'wlan0' - option proto 'none' - -package wireless + _interface_network_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'guests' option ssid 'open' """ @@ -358,7 +335,34 @@ def test_render_interface_network(self): def test_parse_interface_network(self): o = OpenWrt(native=self._interface_network_uci, dsa=False) - self.assertEqual(o.config, self._interface_network_netjson) + self.assertDictEqual(o.config, self._interface_network_netjson) + + def test_render_ignores_redundant_network(self): + netjson = { + "interfaces": [ + { + "name": "wlan0", + "type": "wireless", + "network": "guests", + "wireless": { + "radio": "radio0", + "mode": "access_point", + "ssid": "open", + }, + } + ] + } + uci = """package wireless + +config wifi-iface 'wifi_wlan0' + option device 'radio0' + option ifname 'wlan0' + option mode 'ap' + option ssid 'open' +""" + o = OpenWrt(netjson, dsa=False) + expected = self._tabs(uci) + self.assertEqual(o.render(), expected) def test_network_dot_conversion(self): o = OpenWrt( @@ -379,13 +383,7 @@ def test_network_dot_conversion(self): dsa=False, ) expected = self._tabs( - """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -416,13 +414,7 @@ def test_network_dash_conversion(self): dsa=False, ) expected = self._tabs( - """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -449,14 +441,7 @@ def test_network_dash_conversion(self): } ] } - _disabled_uci = """package network - -config interface 'wlan0' - option enabled '0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _disabled_uci = """package wireless config wifi-iface 'wifi_wlan0' option bssid '00:11:22:33:44:55' @@ -464,7 +449,6 @@ def test_network_dash_conversion(self): option disabled '1' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option ssid 'mywifi' """ @@ -479,7 +463,7 @@ def test_render_interface_disabled(self): def test_parse_interface_disabled_full(self): o = OpenWrt(native=self._disabled_uci, dsa=False) - self.assertEqual(o.config, self._disabled_netjson) + self.assertDictEqual(o.config, self._disabled_netjson) def test_parse_interface_disabled_partial(self): o = OpenWrt( @@ -492,11 +476,10 @@ def test_parse_interface_disabled_partial(self): option disabled '1' option ifname 'wlan0' option mode 'sta' - option network 'wlan0' option ssid 'mywifi' """, ) - self.assertEqual(o.config, self._disabled_netjson) + self.assertDictEqual(o.config, self._disabled_netjson) _wds_netjson = { "interfaces": [ @@ -512,19 +495,12 @@ def test_parse_interface_disabled_partial(self): } ] } - _wds_uci = """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _wds_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWdsAp' option wds '1' """ @@ -536,7 +512,7 @@ def test_render_wds_ap(self): def test_parse_wds_ap(self): o = OpenWrt(native=self._wds_uci, dsa=False) - self.assertEqual(o.config, self._wds_netjson) + self.assertDictEqual(o.config, self._wds_netjson) def test_wifi_options_zero(self): """ @@ -564,19 +540,12 @@ def test_wifi_options_zero(self): dsa=False, ) expected = self._tabs( - """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' option wmm '1' """ @@ -598,13 +567,7 @@ def test_wifi_options_zero(self): } ] } - _macfilter_uci = """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _macfilter_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -613,7 +576,6 @@ def test_wifi_options_zero(self): list maclist 'E8:94:F6:33:8C:1D' list maclist '42:6c:8f:95:0f:00' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """ @@ -624,7 +586,7 @@ def test_render_macfilter(self): def test_parse_macfilter(self): o = OpenWrt(native=self._macfilter_uci, dsa=False) - self.assertEqual(o.config, self._macfilter_netjson) + self.assertDictEqual(o.config, self._macfilter_netjson) def test_maclist_format(self): o = OpenWrt( @@ -664,6 +626,14 @@ def test_maclist_format(self): _wds_bridge_netjson = { "interfaces": [ + # WDS bridge + { + "name": "br-wds_bridge", + "network": "wds_bridge", + "type": "bridge", + "addresses": [{"proto": "dhcp", "family": "ipv4"}], + "bridge_members": ["wlan0", "wlan1"], + }, # client { "name": "wlan0", @@ -686,26 +656,10 @@ def test_maclist_format(self): "ssid": "FreeRomaWifi", }, }, - # WDS bridge - { - "name": "br-wds_bridge", - "network": "wds_bridge", - "type": "bridge", - "addresses": [{"proto": "dhcp", "family": "ipv4"}], - "bridge_members": ["wlan0", "wlan1"], - }, ] } _wds_bridge_uci = """package network -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -config interface 'wlan1' - option ifname 'wlan1' - option proto 'none' - config interface 'wds_bridge' option ifname 'wlan0 wlan1' option proto 'dhcp' @@ -737,7 +691,7 @@ def test_render_wds_bridge(self): def test_parse_wds_bridge(self): o = OpenWrt(native=self._wds_bridge_uci, dsa=False) - self.assertEqual(o.config, self._wds_bridge_netjson) + self.assertDictEqual(o.config, self._wds_bridge_netjson) _80211r_netjson = { "interfaces": [ @@ -758,13 +712,7 @@ def test_parse_wds_bridge(self): } ] } - _80211r_uci = """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _80211r_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' @@ -786,7 +734,7 @@ def test_render_access_point_80211r(self): def test_parse_access_point_80211r(self): o = OpenWrt(native=self._80211r_uci, dsa=False) - self.assertEqual(o.config, self._80211r_netjson) + self.assertDictEqual(o.config, self._80211r_netjson) with self.subTest('ignore bogus reassociation_deadline'): bogus_uci = self._80211r_uci @@ -796,19 +744,10 @@ def test_parse_access_point_80211r(self): o = OpenWrt(native=bogus_uci, dsa=False) netjson_80211r = deepcopy(self._80211r_netjson) del netjson_80211r['interfaces'][0]['wireless']['reassociation_deadline'] - self.assertEqual(o.config, netjson_80211r) + self.assertDictEqual(o.config, netjson_80211r) _80211s_netjson = { "interfaces": [ - { - "name": "mesh0", - "type": "wireless", - "wireless": { - "radio": "radio0", - "mode": "802.11s", - "mesh_id": "ninux", - }, - }, { "name": "br-lan", "network": "lan", @@ -823,14 +762,19 @@ def test_parse_access_point_80211r(self): } ], }, + { + "name": "mesh0", + "type": "wireless", + "wireless": { + "radio": "radio0", + "mode": "802.11s", + "mesh_id": "ninux", + }, + }, ] } _80211s_uci = """package network -config interface 'mesh0' - option ifname 'mesh0' - option proto 'none' - config interface 'lan' option ifname 'mesh0' option ipaddr '192.168.0.1' @@ -855,7 +799,7 @@ def test_render_mesh_80211s(self): def test_parse_mesh_80211s(self): o = OpenWrt(native=self._80211s_uci, dsa=False) - self.assertEqual(o.config, self._80211s_netjson) + self.assertDictEqual(o.config, self._80211s_netjson) _bssid_netjson = { "interfaces": [ @@ -923,13 +867,7 @@ def test_bssid_station(self): } ] } - _list_option_uci = """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _list_option_uci = """package wireless config wifi-iface 'wifi_wlan0' list basic_rate '6000' @@ -937,7 +875,6 @@ def test_bssid_station(self): option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'open' """ @@ -948,7 +885,7 @@ def test_render_list_option(self): def test_parse_list_option(self): o = OpenWrt(native=self._list_option_uci, dsa=False) - self.assertEqual(o.config, self._list_option_netjson) + self.assertDictEqual(o.config, self._list_option_netjson) def test_isolate(self): o = OpenWrt( @@ -984,20 +921,13 @@ def test_isolate(self): } ] } - _macaddr_uci = """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _macaddr_uci = """package wireless config wifi-iface 'wifi_wlan0' option device 'radio0' option ifname 'wlan0' option macaddr 'E8:94:F6:33:8C:00' option mode 'ap' - option network 'wlan0' option ssid 'open' """ @@ -1008,7 +938,7 @@ def test_render_macaddr_override(self): def test_parse_macaddr_override(self): o = OpenWrt(native=self._macaddr_uci, dsa=False) - self.assertEqual(o.config, self._macaddr_netjson) + self.assertDictEqual(o.config, self._macaddr_netjson) _custom_id_netjson = { "interfaces": [ @@ -1024,19 +954,12 @@ def test_parse_macaddr_override(self): } ] } - _custom_id_uci = """package network - -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - -package wireless + _custom_id_uci = """package wireless config wifi-iface 'arbitrary_id' option device 'radio0' option ifname 'wlan0' option mode 'ap' - option network 'wlan0' option ssid 'MyWifiAP' """ @@ -1047,7 +970,7 @@ def test_render_wifi_custom_id(self): def test_parse_wifi_custom_id(self): o = OpenWrt(native=self._custom_id_uci, dsa=False) - self.assertEqual(o.config, self._custom_id_netjson) + self.assertDictEqual(o.config, self._custom_id_netjson) _wifi_simplified_bridge_netjson = { "interfaces": [ @@ -1075,10 +998,6 @@ def test_parse_wifi_custom_id(self): option proto 'none' option type 'bridge' -config interface 'wlan0' - option ifname 'wlan0' - option proto 'none' - package wireless config wifi-iface 'wifi_wlan0' @@ -1096,4 +1015,4 @@ def test_render_simplified_wifi_bridge(self): def test_parse_simplified_wifi_bridge(self): o = OpenWrt(native=self._wifi_simplified_bridge_uci, dsa=False) - self.assertEqual(o.config, self._wifi_simplified_bridge_netjson) + self.assertDictEqual(o.config, self._wifi_simplified_bridge_netjson)