def test_file_schema(self): c = { "files": [ { "path": "/tmp/hello.sh", "mode": "0644", "contents": "echo 'hello world'" } ] } # valid c['files'][0]['mode'] = '3555' o = OpenWrt(c) o.validate() # valid c['files'][0]['mode'] = '755' o = OpenWrt(c) o.validate() # too long c['files'][0]['mode'] = '00777' o = OpenWrt(c) with self.assertRaises(ValidationError): o.validate() # too short c['files'][0]['mode'] = '75' o = OpenWrt(c) with self.assertRaises(ValidationError): o.validate() # invalid c['files'][0]['mode'] = '0855' o = OpenWrt(c) with self.assertRaises(ValidationError): o.validate()
def test_mac_address_format(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "mac": "00:11:22:33:44:55" } ] }) o.validate() # too short o.config['interfaces'][0]['mac'] = '00:11:22:33:44' with self.assertRaises(ValidationError): o.validate() # valid o.config['interfaces'][0]['mac'] = '00-11-22-33-44-55' o.validate() # should not be valid o.config['interfaces'][0]['mac'] = '00:11:22:33:44:ZY' with self.assertRaises(ValidationError): o.validate() # empty is valid (will be ignored) o.config['interfaces'][0]['mac'] = '' o.validate()
def test_maclist_format(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "MyWifiAP", "macfilter": "deny", "maclist": [ "E8:94:F6:33:8C:1D", ] } } ] }) o.validate() # too short o.config['interfaces'][0]['wireless']['maclist'][0] = '00:11:22:33:44' with self.assertRaises(ValidationError): o.validate() # valid o.config['interfaces'][0]['wireless']['maclist'][0] = '00-11-22-33-44-55' o.validate() # should not be valid o.config['interfaces'][0]['wireless']['maclist'][0] = '00:11:22:33:44:ZY' with self.assertRaises(ValidationError): o.validate() # empty is not valid o.config['interfaces'][0]['wireless']['maclist'][0] = '' with self.assertRaises(ValidationError): o.validate()
def test_ifname_pattern(self): o = OpenWrt({"interfaces": [{"name": "eth 0", "type": "ethernet"}]}) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['name'] = 'e-t_h@=0.1' o.validate()
def test_igmp(self): o = OpenWrt( { "interfaces": [ { "name": "br-lan", "type": "bridge", "igmp_snooping": True, "bridge_members": ["eth0", "eth1"], } ] } ) expected = self._tabs( """package network config interface 'br_lan' option ifname 'eth0 eth1' option igmp_snooping '1' option proto 'none' option type 'bridge' """ ) self.assertEqual(o.render(), expected) # try entering an invalid value o.config['interfaces'][0]['igmp_snooping'] = 'wrong' with self.assertRaises(ValidationError): o.validate()
def test_interface_ipv6(self): o = OpenWrt( { "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "family": "ipv6", "proto": "static", "address": "fe80::ba27:ebff:fe1c:5477", "mask": 64, } ], } ] } ) o.validate() # invalid ipv6 o.config['interfaces'][0]['addresses'][0][ 'address' ] = 'fe80::ba27.ebff_fe1c:5477' with self.assertRaises(ValidationError): o.validate()
def test_ifname_length(self): o = OpenWrt({"interfaces": [{"name": "ifname0123456789", "type": "ethernet"}]}) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['name'] = 'ifname0' o.validate()
def test_ntp_servers_hostname(self): o = OpenWrt({"ntp": {"enabled": True, "server": ["0.openwrt.pool.ntp.org"]}}) o.validate() # invalid hostname o.config['ntp']['server'][0] = 'totally/wrong' with self.assertRaises(ValidationError): o.validate()
def test_dns_search_hostname(self): o = OpenWrt({"dns_search": ['openwisp.org', 'test.netjson.org']}) o.validate() # invalid hostname o.config['dns_search'].append('very/wrong') with self.assertRaises(ValidationError): o.validate()
def test_bssid_format(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio1", "mode": "adhoc", "ssid": "adhoc-ssid", "bssid": "00:11:22:33:44:55" } } ] }) o.validate() # too short o.config['interfaces'][0]['wireless']['bssid'] = '00:11:22:33:44' with self.assertRaises(ValidationError): o.validate() # valid o.config['interfaces'][0]['wireless']['bssid'] = '00-11-22-33-44-55' o.validate() # should not be valid o.config['interfaces'][0]['wireless']['bssid'] = '00:11:22:33:44:ZY' with self.assertRaises(ValidationError): o.validate() # empty is not valid o.config['interfaces'][0]['wireless']['bssid'] = '' with self.assertRaises(ValidationError): o.validate()
def test_file_schema(self): c = { "files": [{ "path": "/tmp/hello.sh", "mode": "0644", "contents": "echo 'hello world'", }] } # valid c['files'][0]['mode'] = '3555' o = OpenWrt(c) o.validate() # valid c['files'][0]['mode'] = '755' o = OpenWrt(c) o.validate() # too long c['files'][0]['mode'] = '00777' o = OpenWrt(c) with self.assertRaises(ValidationError): o.validate() # too short c['files'][0]['mode'] = '75' o = OpenWrt(c) with self.assertRaises(ValidationError): o.validate() # invalid c['files'][0]['mode'] = '0855' o = OpenWrt(c) with self.assertRaises(ValidationError): o.validate()
def test_system_invalid_timezone(self): o = OpenWrt( {"general": { "hostname": "test_system", "timezone": "WRONG" }}) with self.assertRaises(ValidationError): o.validate()
def test_error_str(self): o = OpenWrt({'interfaces': [{'wrong': True}]}) try: o.validate() except Exception as e: self.assertIn('Against schema', str(e)) else: self.fail('ValidationError not raised')
def test_system_invalid_timezone(self): o = OpenWrt({ "general": { "hostname": "test_system", "timezone": "WRONG", } }) with self.assertRaises(ValidationError): o.validate()
def test_dns_search_hostname(self): o = OpenWrt({ "dns_search": ['openwisp.org', 'test.netjson.org'] }) o.validate() # invalid hostname o.config['dns_search'].append('very/wrong') with self.assertRaises(ValidationError): o.validate()
def test_led_schema_validation(self): o = OpenWrt({ "led": [ { "invalid": True } ] }) with self.assertRaises(ValidationError): o.validate()
def test_bssid_adhoc(self): o = OpenWrt(self._bssid_netjson) # bssid is required del o.config['interfaces'][0]['wireless']['bssid'] with self.assertRaises(ValidationError): o.validate() # empty is not valid o.config['interfaces'][0]['wireless']['bssid'] = '' with self.assertRaises(ValidationError): o.validate()
def test_ntp_servers_hostname(self): o = OpenWrt({ "ntp": { "enabled": True, "server": ["0.openwrt.pool.ntp.org"] } }) o.validate() # invalid hostname o.config['ntp']['server'][0] = 'totally/wrong' with self.assertRaises(ValidationError): o.validate()
def test_network_maxlength(self): o = OpenWrt({ "interfaces": [{ "name": "eth0", "network": "lan0123456789012345", "type": "ethernet", }] }) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['network'] = 'lan' o.validate()
def test_bridge_members_unique(self): o = OpenWrt({ "interfaces": [{ "name": "lan", "type": "bridge", "bridge_members": ["eth0", "eth0"], }] }) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['bridge_members'][0] = 'eth1' o.validate()
def test_ifname_pattern(self): o = OpenWrt({ "interfaces": [ { "name": "eth 0", "type": "ethernet" } ] }) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['name'] = 'e-t_h@=0.1' o.validate()
def test_ifname_length(self): o = OpenWrt({ "interfaces": [ { "name": "ifname0123456789", "type": "ethernet" } ] }) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['name'] = 'ifname0' o.validate()
def test_schema_radio_wrong_protocol(self): o = OpenWrt({ "radios": [{ "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11ad", # ad is not supported by OpenWRT yet "channel": 132, "channel_width": 80, "tx_power": 8, }] }) with self.assertRaises(ValidationError): o.validate()
def test_schema_radio_wrong_driver(self): o = OpenWrt({ "radios": [{ "name": "radio0", "phy": "phy0", "driver": "iamwrong", "protocol": "802.11ac", "channel": 132, "channel_width": 80, "tx_power": 8, }] }) with self.assertRaises(ValidationError): o.validate()
def test_bridge_members_unique(self): o = OpenWrt({ "interfaces": [ { "name": "lan", "type": "bridge", "bridge_members": ["eth0", "eth0"] } ] }) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['bridge_members'][0] = 'eth1' o.validate()
def test_network_maxlength(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "network": "lan0123456789", "type": "ethernet" } ] }) with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['network'] = 'lan' o.validate()
def test_schema_radio_wrong_driver(self): o = OpenWrt({ "radios": [ { "name": "radio0", "phy": "phy0", "driver": "iamwrong", "protocol": "802.11ac", "channel": 132, "channel_width": 80, "tx_power": 8 } ] }) with self.assertRaises(ValidationError): o.validate()
def test_schema_radio_wrong_protocol(self): o = OpenWrt({ "radios": [ { "name": "radio0", "phy": "phy0", "driver": "mac80211", "protocol": "802.11ad", # ad is not supported by OpenWRT yet "channel": 132, "channel_width": 80, "tx_power": 8 } ] }) with self.assertRaises(ValidationError): o.validate()
def test_isolate(self): o = OpenWrt({ "interfaces": [{ "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "isolate": True, }, }] }) self.assertIn("option isolate '1'", o.render()) # try entering an invalid value o.config['interfaces'][0]['wireless']['isolate'] = 'wrong' with self.assertRaises(ValidationError): o.validate()
def test_isolate(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "isolate": True } } ] }) self.assertIn("option isolate '1'", o.render()) # try entering an invalid value o.config['interfaces'][0]['wireless']['isolate'] = 'wrong' with self.assertRaises(ValidationError): o.validate()
def test_interface_ipv4(self): o = OpenWrt({ "interfaces": [{ "name": "eth0", "type": "ethernet", "addresses": [{ "family": "ipv4", "proto": "static", "address": "10.0.0.1", "mask": 28 }] }] }) o.validate() # invalid ipv4 o.config['interfaces'][0]['addresses'][0]['address'] = '127_0_0_1' with self.assertRaises(ValidationError): o.validate()
def test_wireless_network_attr_validation(self): o = OpenWrt({ "interfaces": [ { "name": "wlan0", "type": "wireless", "wireless": { "radio": "radio0", "mode": "access_point", "ssid": "open", "network": "lan 0" } } ] }) # pattern does not validate with self.assertRaises(ValidationError): o.validate() # maxLength does not validate o.config['interfaces'][0]['wireless']['network'] = ['lan0123456789012345'] with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['wireless']['network'] = ['lan'] o.validate()
def test_bssid_format(self): o = OpenWrt(self._bssid_netjson) o.validate() # too short o.config['interfaces'][0]['wireless']['bssid'] = '00:11:22:33:44' with self.assertRaises(ValidationError): o.validate() # valid o.config['interfaces'][0]['wireless']['bssid'] = '00-11-22-33-44-55' o.validate() # should not be valid o.config['interfaces'][0]['wireless']['bssid'] = '00:11:22:33:44:ZY' with self.assertRaises(ValidationError): o.validate()
def test_interface_ipv4(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "family": "ipv4", "proto": "static", "address": "10.0.0.1", "mask": 28 } ] } ] }) o.validate() # invalid ipv4 o.config['interfaces'][0]['addresses'][0]['address'] = '127_0_0_1' with self.assertRaises(ValidationError): o.validate()
def test_interface_ipv6(self): o = OpenWrt({ "interfaces": [ { "name": "eth0", "type": "ethernet", "addresses": [ { "family": "ipv6", "proto": "static", "address": "fe80::ba27:ebff:fe1c:5477", "mask": 64 } ] } ] }) o.validate() # invalid ipv6 o.config['interfaces'][0]['addresses'][0]['address'] = 'fe80::ba27.ebff_fe1c:5477' with self.assertRaises(ValidationError): o.validate()
def test_igmp(self): o = OpenWrt({ "interfaces": [ { "name": "br-lan", "type": "bridge", "igmp_snooping": True, "bridge_members": ["eth0", "eth1"] } ] }) expected = self._tabs("""package network config interface 'br_lan' option ifname 'eth0 eth1' option igmp_snooping '1' option proto 'none' option type 'bridge' """) self.assertEqual(o.render(), expected) # try entering an invalid value o.config['interfaces'][0]['igmp_snooping'] = 'wrong' with self.assertRaises(ValidationError): o.validate()
def test_bssid_station(self): o = OpenWrt(self._bssid_netjson) o.config['interfaces'][0]['wireless']['mode'] = 'station' o.validate() # bssid is not required del o.config['interfaces'][0]['wireless']['bssid'] o.validate() # empty is valid o.config['interfaces'][0]['wireless']['bssid'] = '' o.validate()
def test_bridge_members_schema(self): o = OpenWrt({"interfaces": [{"name": "lan", "type": "bridge"}]}) with self.assertRaises(ValidationError): o.validate() o.config['interfaces'][0]['bridge_members'] = [3] with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['bridge_members'] = ['eth0', 'wlan0'] o.validate()
def test_network_pattern(self): o = OpenWrt( {"interfaces": [{"name": "eth0", "network": "lan 0", "type": "ethernet"}]} ) with self.assertRaises(ValidationError): o.validate() o.config['interfaces'][0]['network'] = 'lan/0' with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['network'] = 'lan' o.validate()
def test_validate(self): o = OpenWrt({'interfaces': 'WRONG'}) with self.assertRaises(ValidationError): o.validate() o = OpenWrt({'interfaces': []}) o.validate() o.config['interfaces'] = 'CHANGED' try: o.validate() except ValidationError as e: self.assertEqual(e.details.instance, 'CHANGED') else: self.fail('ValidationError not raised')
def test_bridge_members_schema(self): o = OpenWrt({ "interfaces": [ { "name": "lan", "type": "bridge" } ] }) with self.assertRaises(ValidationError): o.validate() o.config['interfaces'][0]['bridge_members'] = [3] with self.assertRaises(ValidationError): o.validate() # ensure fix works o.config['interfaces'][0]['bridge_members'] = ['eth0', 'wlan0'] o.validate()
def test_netjson_example(self): o = OpenWrt(netjson_example) o.validate()
def test_find_bridge_skip_error(self): o = OpenWrt({'interfaces': ['WRONG']}) with self.assertRaises(ValidationError): o.validate()
def test_config_copy(self): config = {'interfaces': []} o = OpenWrt(config) o.validate() self.assertDictEqual(config, {'interfaces': []})
def test_general_hostname(self): o = OpenWrt({"general": {"hostname": "invalid hostname"}}) with self.assertRaises(ValidationError): o.validate() o.config['general']['hostname'] = 'valid' o.validate()
def test_led_schema_validation(self): o = OpenWrt({"led": [{"invalid": True}]}) with self.assertRaises(ValidationError): o.validate()