Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
routersploit
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
czos-dpend
routersploit
Commits
f23a4f65
Commit
f23a4f65
authored
Jun 06, 2016
by
fwkz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stripping scheme in validators.ipv4.
Covering validators.ipv4 with tests.
parent
bb9a125a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
3 deletions
+71
-3
test_validators.py
routersploit/test/test_validators.py
+62
-0
validators.py
routersploit/validators.py
+9
-3
No files found.
routersploit/test/test_validators.py
View file @
f23a4f65
import
unittest
import
unittest
import
socket
try
:
try
:
import
unittest.mock
as
mock
import
unittest.mock
as
mock
...
@@ -7,6 +8,7 @@ except ImportError:
...
@@ -7,6 +8,7 @@ except ImportError:
from
routersploit.test
import
RoutersploitTestCase
from
routersploit.test
import
RoutersploitTestCase
from
routersploit
import
validators
from
routersploit
import
validators
from
routersploit.exceptions
import
OptionValidationError
class
ValidatorsTest
(
RoutersploitTestCase
):
class
ValidatorsTest
(
RoutersploitTestCase
):
...
@@ -19,6 +21,66 @@ class ValidatorsTest(RoutersploitTestCase):
...
@@ -19,6 +21,66 @@ class ValidatorsTest(RoutersploitTestCase):
def
test_url_already_with_https_prefix
(
self
):
def
test_url_already_with_https_prefix
(
self
):
self
.
assertEqual
(
validators
.
url
(
"https://127.0.0.1"
),
"https://127.0.0.1"
)
self
.
assertEqual
(
validators
.
url
(
"https://127.0.0.1"
),
"https://127.0.0.1"
)
def
test_ipv4_valid_address
(
self
):
address
=
"127.0.0.1"
self
.
assertEqual
(
validators
.
ipv4
(
address
),
address
)
def
test_ipv4_invalid_address_1
(
self
):
""" IP address with segment out of range. """
address
=
"127.256.0.1"
with
self
.
assertRaises
(
OptionValidationError
):
validators
.
ipv4
(
address
)
def
test_ipv4_invalid_address_2
(
self
):
""" IP address with 4 digit segment. """
address
=
"127.0.0.1234"
with
self
.
assertRaises
(
OptionValidationError
):
validators
.
ipv4
(
address
)
def
test_ipv4_invalid_address_3
(
self
):
""" IP address with extra segment """
address
=
"127.0.0.123.123"
with
self
.
assertRaises
(
OptionValidationError
):
validators
.
ipv4
(
address
)
@mock.patch
(
"socket.inet_pton"
)
def
test_ipv4_no_inet_pton_valid_address
(
self
,
mock_inet_pton
):
address
=
"127.0.0.1"
mock_inet_pton
.
side_effect
=
AttributeError
self
.
assertEqual
(
validators
.
ipv4
(
address
),
"127.0.0.1"
)
@mock.patch
(
"socket.inet_pton"
)
def
test_ipv4_no_inet_pton_invalid_address_1
(
self
,
mock_inet_pton
):
""" IP address with segment out of range. """
address
=
"127.256.0.1"
mock_inet_pton
.
side_effect
=
AttributeError
with
self
.
assertRaises
(
OptionValidationError
):
validators
.
ipv4
(
address
)
@mock.patch
(
"socket.inet_pton"
)
def
test_ipv4_no_inet_pton_invalid_address_2
(
self
,
mock_inet_pton
):
""" IP address with 4 digit segment. """
address
=
"127.0.0.1234"
mock_inet_pton
.
side_effect
=
AttributeError
with
self
.
assertRaises
(
OptionValidationError
):
validators
.
ipv4
(
address
)
@mock.patch
(
"socket.inet_pton"
)
def
test_ipv4_no_inet_pton_invalid_address_3
(
self
,
mock_inet_pton
):
""" IP address with extra segment """
address
=
"127.0.0.123.123"
mock_inet_pton
.
side_effect
=
AttributeError
with
self
.
assertRaises
(
OptionValidationError
):
validators
.
ipv4
(
address
)
def
test_ipv4_strip_scheme_1
(
self
):
address
=
"http://127.0.0.1"
self
.
assertEqual
(
validators
.
ipv4
(
address
),
"127.0.0.1"
)
def
test_ipv4_strip_scheme_2
(
self
):
address
=
"ftp://127.0.0.1"
self
.
assertEqual
(
validators
.
ipv4
(
address
),
"127.0.0.1"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
routersploit/validators.py
View file @
f23a4f65
import
socket
import
socket
import
urlparse
from
.exceptions
import
OptionValidationError
from
.exceptions
import
OptionValidationError
...
@@ -15,6 +16,8 @@ def url(address):
...
@@ -15,6 +16,8 @@ def url(address):
def
ipv4
(
address
):
def
ipv4
(
address
):
address
=
urlparse
.
urlsplit
(
address
)
address
=
address
.
netloc
or
address
.
path
try
:
try
:
socket
.
inet_pton
(
socket
.
AF_INET
,
address
)
socket
.
inet_pton
(
socket
.
AF_INET
,
address
)
except
AttributeError
:
except
AttributeError
:
...
@@ -22,8 +25,12 @@ def ipv4(address):
...
@@ -22,8 +25,12 @@ def ipv4(address):
socket
.
inet_aton
(
address
)
socket
.
inet_aton
(
address
)
except
socket
.
error
:
except
socket
.
error
:
raise
OptionValidationError
(
"Option have to be valid IP address."
)
raise
OptionValidationError
(
"Option have to be valid IP address."
)
return
address
.
count
(
'.'
)
==
3
if
address
.
count
(
'.'
)
==
3
:
return
address
else
:
raise
OptionValidationError
(
"Option have to be valid IP address."
)
except
socket
.
error
:
except
socket
.
error
:
raise
OptionValidationError
(
"Option have to be valid IP address."
)
raise
OptionValidationError
(
"Option have to be valid IP address."
)
return
address
return
address
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment