Skip to content

Commit

Permalink
Merge cadb36b into dfd7c78
Browse files Browse the repository at this point in the history
  • Loading branch information
NoumbissiValere committed Jul 13, 2020
2 parents dfd7c78 + cadb36b commit f9e65d2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ target/

# editors
*.komodoproject
.vscode

# other
*.DS_Store*
Expand Down
9 changes: 9 additions & 0 deletions netjsonconfig/backends/base/backend.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import gzip
import ipaddress
import json
import tarfile
from collections import OrderedDict
Expand Down Expand Up @@ -122,6 +123,14 @@ def _deduplicate_files(self):
files_dict[file['path']] = file
self.config['files'] = list(files_dict.values())

@draft4_format_checker.checks('cidr', AssertionError)
def _cidr_notation(value):
try:
ipaddress.ip_network(value)
except ValueError as e:
assert False, str(e)
return True

def validate(self):
try:
Draft4Validator(self.schema, format_checker=draft4_format_checker).validate(
Expand Down
2 changes: 2 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,14 @@
"title": "source subnet",
"description": "(CIDR notation)",
"propertyOrder": 3,
"format": "cidr",
},
"dest": {
"type": "string",
"title": "destination subnet",
"description": "(CIDR notation)",
"propertyOrder": 4,
"format": "cidr",
},
"tos": {
"type": "integer",
Expand Down
41 changes: 41 additions & 0 deletions tests/openwrt/test_network.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from netjsonconfig import OpenWrt
from netjsonconfig.exceptions import ValidationError
from netjsonconfig.utils import _TabsMixin


Expand Down Expand Up @@ -210,6 +211,46 @@ def test_rules_no_src_dest(self):
)
self.assertEqual(o.render(), expected)

def test_render_rule_wrong(self):
rule = {
"ip_rules": [
{
"in": "eth0",
"out": "eth1",
"src": "wrong",
"dest": "wrong1",
"tos": 2,
"action": "blackhole",
}
]
}
o = OpenWrt(rule)
with self.assertRaisesRegex(ValidationError, "'wrong' is not a 'cidr'"):
o.validate()
rule['ip_rules'][0]['src'] = '192.168.1.0/24'
o = OpenWrt(rule)
with self.assertRaisesRegexp(ValidationError, "'wrong1' is not a 'cidr'"):
o.validate()
# fix 'dest' and expect no ValidationError raised
rule['ip_rules'][0]['dest'] = '192.168.1.0/24'
o = OpenWrt(rule)
o.validate()

def test_parse_rules_zone(self):
with self.assertRaisesRegexp(ValidationError, "'wrong' is not a 'cidr'"):
OpenWrt(
native="""package network
config rule 'rule1'
option action 'blackhole'
option dest 'wrong'
option in 'eth0'
option out 'eth1'
option src 'wrong'
option tos '2'
"""
)

_switch_netjson = {
"switch": [
{
Expand Down

0 comments on commit f9e65d2

Please sign in to comment.