Skip to content

Commit 0d75b9d

Browse files
committed
[schema] Made bssid not required for wireless stations #94
It's still required for adhoc interfaces. Fixes #94 and closes #95
1 parent 4373b24 commit 0d75b9d

2 files changed

Lines changed: 48 additions & 19 deletions

File tree

netjsonconfig/schema.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from .countries import countries
88

99
DEFAULT_FILE_MODE = '0644'
10+
MAC_PATTERN = '([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})'
11+
MAC_PATTERN_BLANK = '^({0}|)$'.format(MAC_PATTERN)
1012

1113
schema = {
1214
"$schema": "http://json-schema.org/draft-04/schema#",
@@ -157,7 +159,7 @@
157159
"type": "string",
158160
"title": "MAC address",
159161
"description": "if specified overrides default macaddress for this interface",
160-
"pattern": "^(([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|)$", # can be empty
162+
"pattern": MAC_PATTERN_BLANK, # can be empty
161163
"maxLength": 17,
162164
"propertyOrder": 3,
163165
},
@@ -348,13 +350,11 @@
348350
}
349351
},
350352
"bssid_wireless_property": {
351-
"required": ["bssid"],
352353
"properties": {
353354
"bssid": {
354355
"type": "string",
355356
"title": "BSSID",
356-
"pattern": "^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$",
357-
"minLength": 17,
357+
"pattern": MAC_PATTERN_BLANK,
358358
"maxLength": 17,
359359
"propertyOrder": 4,
360360
},
@@ -715,7 +715,16 @@
715715
"adhoc_wireless_settings": {
716716
"title": "Adhoc",
717717
"allOf": [
718-
{"properties": {"mode": {"enum": ["adhoc"]}}},
718+
{
719+
"required": ["bssid"],
720+
"properties": {
721+
"mode": {"enum": ["adhoc"]},
722+
"bssid": {
723+
"pattern": MAC_PATTERN,
724+
"minLength": 17,
725+
}
726+
}
727+
},
719728
{"$ref": "#/definitions/base_wireless_settings"},
720729
{"$ref": "#/definitions/ssid_wireless_property"},
721730
{"$ref": "#/definitions/bssid_wireless_property"},

tests/openwrt/test_wireless.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -813,21 +813,23 @@ def test_parse_mesh_80211s(self):
813813
o = OpenWrt(native=self._80211s_uci)
814814
self.assertEqual(o.config, self._80211s_netjson)
815815

816-
def test_bssid_format(self):
817-
o = OpenWrt({
818-
"interfaces": [
819-
{
820-
"name": "wlan0",
821-
"type": "wireless",
822-
"wireless": {
823-
"radio": "radio1",
824-
"mode": "adhoc",
825-
"ssid": "adhoc-ssid",
826-
"bssid": "00:11:22:33:44:55"
827-
}
816+
_bssid_netjson = {
817+
"interfaces": [
818+
{
819+
"name": "wlan0",
820+
"type": "wireless",
821+
"wireless": {
822+
"radio": "radio0",
823+
"mode": "adhoc",
824+
"ssid": "bssid-test",
825+
"bssid": "00:11:22:33:44:55"
828826
}
829-
]
830-
})
827+
}
828+
]
829+
}
830+
831+
def test_bssid_format(self):
832+
o = OpenWrt(self._bssid_netjson)
831833
o.validate()
832834
# too short
833835
o.config['interfaces'][0]['wireless']['bssid'] = '00:11:22:33:44'
@@ -840,11 +842,29 @@ def test_bssid_format(self):
840842
o.config['interfaces'][0]['wireless']['bssid'] = '00:11:22:33:44:ZY'
841843
with self.assertRaises(ValidationError):
842844
o.validate()
845+
846+
def test_bssid_adhoc(self):
847+
o = OpenWrt(self._bssid_netjson)
848+
# bssid is required
849+
del o.config['interfaces'][0]['wireless']['bssid']
850+
with self.assertRaises(ValidationError):
851+
o.validate()
843852
# empty is not valid
844853
o.config['interfaces'][0]['wireless']['bssid'] = ''
845854
with self.assertRaises(ValidationError):
846855
o.validate()
847856

857+
def test_bssid_station(self):
858+
o = OpenWrt(self._bssid_netjson)
859+
o.config['interfaces'][0]['wireless']['mode'] = 'station'
860+
o.validate()
861+
# bssid is not required
862+
del o.config['interfaces'][0]['wireless']['bssid']
863+
o.validate()
864+
# empty is valid
865+
o.config['interfaces'][0]['wireless']['bssid'] = ''
866+
o.validate()
867+
848868
_list_option_netjson = {
849869
"interfaces": [
850870
{

0 commit comments

Comments
 (0)