Skip to content

Commit cdf3814

Browse files
committed
Add some unit tests
1 parent a4627ed commit cdf3814

File tree

5 files changed

+113
-16
lines changed

5 files changed

+113
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Concerning the SSL validation:
118118

119119
Warning: tests require a remote SNS appliance.
120120

121-
`$ APPLIANCE=10.0.0.254 python3 setup.py test`
121+
`$ PASSWORD=password APPLIANCE=10.0.0.254 python3 setup.py test`
122122

123123

124124
To run `snscli` from the source folder without install:

tests/test_auth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python
2+
3+
import os
4+
import unittest
5+
from stormshield.sns.sslclient import SSLClient
6+
7+
APPLIANCE=os.getenv('APPLIANCE', "")
8+
SERIAL = os.getenv('SERIAL', "")
9+
PASSWORD = os.getenv('PASSWORD', "")
10+
11+
@unittest.skipIf(APPLIANCE=="", "APPLIANCE env var must be set to the ip/hostname of a running SNS appliance")
12+
@unittest.skipIf(SERIAL=="", "SERIAL env var must be set to the firewall serial number")
13+
@unittest.skipIf(PASSWORD=="", "PASSWORD env var must be set to the firewall password")
14+
class TestAuth(unittest.TestCase):
15+
""" Test authentication options """
16+
17+
def test_sslverifyhost(self):
18+
""" Test sslverifyhost option """
19+
20+
try:
21+
client = SSLClient(host=SERIAL, ip=APPLIANCE, user='admin', password=PASSWORD, sslverifyhost=True)
22+
self.assertTrue(1==1, "SSLClient connects with sslverifyhost=True")
23+
except:
24+
self.fail("SSLClient did not connect")
25+
26+
response = client.send_command('LIST')
27+
self.assertEqual(response.ret, 100)
28+
29+
client.disconnect()

tests/test_cert.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/python
2+
3+
import os
4+
import unittest
5+
from stormshield.sns.sslclient import SSLClient
6+
7+
APPLIANCE=os.getenv('APPLIANCE', "")
8+
FQDN = os.getenv('FQDN', "")
9+
PASSWORD = os.getenv('PASSWORD', "")
10+
CABUNDLE = os.getenv('CABUNDLE', "")
11+
CERT = os.getenv('CERT', "")
12+
13+
@unittest.skipIf(APPLIANCE=="", "APPLIANCE env var must be set to the ip/hostname of a running SNS appliance")
14+
@unittest.skipIf(FQDN=="", "FQDN env var must be set to the firewall fqdn")
15+
@unittest.skipIf(PASSWORD=="", "PASSWORD env var must be set to the firewall password")
16+
@unittest.skipIf(CABUNDLE=="", "CABUNDLE env var must be set to the CA bundle file")
17+
@unittest.skipIf(CERT=="", "CERT env var must be set to the certificate file")
18+
class TestCert(unittest.TestCase):
19+
""" Test cabundle / certificate authentication options """
20+
21+
def test_sslverifypeer(self):
22+
""" Test sslverifypeer option """
23+
24+
# by default sslverifypeer is True
25+
try:
26+
client = SSLClient(host=APPLIANCE, user='admin', password=PASSWORD)
27+
self.fail("SSLClient should have failed (untrusted CA)")
28+
except Exception as exception:
29+
self.assertTrue(1==1, "SSLClient did not connect (untrusted CA)")
30+
31+
try:
32+
client = SSLClient(host=APPLIANCE, user='admin', password=PASSWORD, sslverifypeer=False)
33+
self.assertTrue(1==1, "SSLClient connects with sslverifypeer=True")
34+
except Exception as exception:
35+
print(exception)
36+
self.fail("SSLClient did not connect")
37+
38+
response = client.send_command('LIST')
39+
self.assertEqual(response.ret, 100)
40+
41+
client.disconnect()
42+
43+
def test_cabundle(self):
44+
""" Test cabundle option """
45+
46+
try:
47+
client = SSLClient(host=FQDN, ip=APPLIANCE, user='admin', password=PASSWORD, sslverifyhost=True, cabundle=CABUNDLE)
48+
self.assertTrue(1==1, "SSLClient connects with cabundle")
49+
except Exception as exception:
50+
print(exception)
51+
self.fail("SSLClient did not connect")
52+
53+
response = client.send_command('LIST')
54+
self.assertEqual(response.ret, 100)
55+
56+
client.disconnect()
57+
58+
def test_cert(self):
59+
""" Test user certificate authentication """
60+
61+
try:
62+
client = SSLClient(host=FQDN, ip=APPLIANCE, usercert=CERT, sslverifyhost=True, cabundle=CABUNDLE)
63+
self.assertTrue(1==1, "SSLClient connects with cabundle")
64+
except Exception as exception:
65+
print(exception)
66+
self.fail("SSLClient did not connect")
67+
68+
response = client.send_command('LIST')
69+
self.assertEqual(response.ret, 100)
70+
71+
client.disconnect()

tests/test_file.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88
import unittest
99
from stormshield.sns.sslclient import SSLClient
1010

11-
# APPLIANCE env var must be set to the ip/hostname of a running SNS appliance
12-
if 'APPLIANCE' not in os.environ:
13-
APPLIANCE=""
14-
else:
15-
APPLIANCE=os.environ['APPLIANCE']
11+
APPLIANCE=os.getenv('APPLIANCE', "")
12+
PASSWORD = os.getenv('PASSWORD', "")
1613

1714
@unittest.skipIf(APPLIANCE=="", "APPLIANCE env var must be set to the ip/hostname of a running SNS appliance")
15+
@unittest.skipIf(PASSWORD=="", "PASSWORD env var must be set to the firewall password")
1816
class TestFormatIni(unittest.TestCase):
1917
""" Test file upload & download """
2018

2119
def setUp(self):
22-
self.client = SSLClient(host=APPLIANCE, user='admin', password='adminadmin', sslverifyhost=False)
20+
self.client = SSLClient(host=APPLIANCE, user='admin', password=PASSWORD, sslverifyhost=False)
2321

2422
self.tmpdir = tempfile.TemporaryDirectory()
2523
self.upload = os.path.join(self.tmpdir.name, 'upload')
@@ -30,7 +28,7 @@ def tearDown(self):
3028
self.tmpdir.cleanup()
3129

3230
def test_upload_download(self):
33-
""" upload / download """
31+
""" Test file upload and download """
3432

3533
#generate a random file
3634
content = "".join( [random.choice(string.ascii_letters) for i in range(15)] )

tests/test_format_ini.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@
66

77
from stormshield.sns.sslclient import SSLClient
88

9-
# APPLIANCE env var must be set to the ip/hostname of a running SNS appliance
10-
if 'APPLIANCE' not in os.environ:
11-
APPLIANCE=""
12-
else:
13-
APPLIANCE=os.environ['APPLIANCE']
9+
APPLIANCE=os.getenv('APPLIANCE', "")
10+
PASSWORD = os.getenv('PASSWORD', "")
1411

1512
@unittest.skipIf(APPLIANCE=="", "APPLIANCE env var must be set to the ip/hostname of a running SNS appliance")
13+
@unittest.skipIf(PASSWORD=="", "PASSWORD env var must be set to the firewall password")
1614
class TestFormatIni(unittest.TestCase):
1715
""" Test INI format """
1816

1917
def setUp(self):
20-
self.client = SSLClient(host=APPLIANCE, user='admin', password='adminadmin', sslverifyhost=False)
18+
self.client = SSLClient(host=APPLIANCE, user='admin', password=PASSWORD, sslverifyhost=False)
2119

2220
self.maxDiff = 5000
2321

@@ -57,7 +55,7 @@ def test_section(self):
5755

5856
expected = """101 code=00a01000 msg="Begin" format="section"
5957
[Global]
60-
State=1
58+
State=0
6159
RiskHalfLife=21600
6260
RiskTTL=86400
6361
[Alarm]
@@ -103,7 +101,8 @@ def test_list(self):
103101

104102
expected = """101 code=00a01000 msg="Begin" format="list"
105103
[Result]
106-
any
104+
labo_network
105+
Network_internals
107106
100 code=00a00100 msg="Ok\""""
108107

109108
response = self.client.send_command('CONFIG WEBADMIN ACCESS SHOW')

0 commit comments

Comments
 (0)