Jelajahi Sumber

Additional negative tests

Roey Regev 6 tahun lalu
induk
melakukan
1076878092

+ 5 - 2
code/Test/test_NamedQueries.py

@@ -1,4 +1,4 @@
-import unittest
+import unittest, pyparsing
 
 from definitions import ROOT_DIR
 import Core.Controller as Ctrl
@@ -50,4 +50,7 @@ class UnitTestPyparsing(unittest.TestCase):
                           '172.217.23.174','192.168.33.254','204.79.197.200','23.51.123.27','35.161.3.50',
                           '52.11.17.245','52.34.37.177','52.39.210.199','52.41.250.141','52.85.173.182',
                           '54.149.74.139','54.187.98.195','54.192.44.108','54.192.44.177','72.247.178.113',
-                          '72.247.178.67','93.184.220.29'])
+                          '72.247.178.67','93.184.220.29'])
+        # undefined Query
+        with self.assertRaises(pyparsing.ParseException):
+            controller.statistics.process_db_query('avg(notDefinedQuery)')

+ 9 - 2
code/Test/test_NestedNamedQueries.py

@@ -1,4 +1,4 @@
-import unittest
+import unittest, sqlite3, pyparsing
 
 from definitions import ROOT_DIR
 import Core.Controller as Ctrl
@@ -38,4 +38,11 @@ class UnitTestPyparsing(unittest.TestCase):
                           '54.192.44.177',
                           '72.247.178.113',
                           '72.247.178.67',
-                          '93.184.220.29'])
+                          '93.184.220.29'])
+        # semantically incorrect query
+        with self.assertRaises(sqlite3.OperationalError):
+            controller.statistics.process_db_query('ipaddress(ipaddress in most_used(macaddress))')
+        # syntactically incorrect query
+        with self.assertRaises(pyparsing.ParseException):
+            controller.statistics.process_db_query('ipaddress(macaddress in most_used(macaddress(ipaddress in'
+                                                   ' least_used(ipaddress))))')

+ 19 - 2
code/Test/test_SQLQueries.py

@@ -1,4 +1,4 @@
-import unittest
+import unittest, sqlite3
 
 from definitions import ROOT_DIR
 import Core.Controller as Ctrl
@@ -36,4 +36,21 @@ class UnitTestPyparsing(unittest.TestCase):
         self.assertEqual("72.247.178.113", controller.statistics.stats_db.process_db_query(query2))
         self.assertEqual(["72.247.178.67", "72.247.178.113"], controller.statistics.stats_db.process_db_query(query3))
         self.assertEqual("10.0.2.15", controller.statistics.stats_db.process_db_query(query4))
-        self.assertEqual(["10.0.2.15", "172.217.23.174"], controller.statistics.stats_db.process_db_query(query5))
+        self.assertEqual(["10.0.2.15", "172.217.23.174"], controller.statistics.stats_db.process_db_query(query5))
+
+        # too long query (stackoverflow)
+        with self.assertRaises(sqlite3.OperationalError):
+            query = "Select ipAddress from ip_statistics where pktsSent = "
+            i = 0
+            while (i < 15):
+                query += "(Select pktsSent from ip_statistics where pktsSent ="
+                i += 1
+            query += "5"
+            while (i > 0):
+                query += ")"
+                i -= 1
+            controller.statistics.stats_db.process_db_query(query)
+        # compare of tables with different dimension
+        with self.assertRaises(sqlite3.OperationalError):
+            controller.statistics.stats_db.process_db_query('Select ipAddress from ip_Statistics where pktsSent'
+                                                            '= (Select * from ip_Statistics)')

+ 5 - 1
code/Test/test_pyparsing_functionality.py

@@ -50,4 +50,8 @@ class UnitTestPyparsing(unittest.TestCase):
         self.assertEqual(controller.statistics.get_ip_address_from_mac('08:00:27:a3:83:43'), '10.0.2.15')
         self.assertEqual(controller.statistics.get_mac_address('10.0.2.15'), '08:00:27:a3:83:43')
         self.assertEqual(controller.statistics.get_most_used_mss('10.0.2.15'), 1460)
-        self.assertEqual(controller.statistics.get_most_used_ttl('10.0.2.15'), 128)
+        self.assertEqual(controller.statistics.get_most_used_ttl('10.0.2.15'), 128)
+
+        # wrong input parameter
+        with self.assertRaises(TypeError):
+            self.assertEqual(controller.statistics.get_pps_sent('08:00:27:a3:83:43'), 32)