2020-04-02 15:08:14 +02:00
|
|
|
import unittest
|
|
|
|
import string
|
|
|
|
import random
|
2020-04-02 16:16:58 +02:00
|
|
|
import requests
|
2020-04-02 15:08:14 +02:00
|
|
|
import re
|
|
|
|
import json
|
2020-04-02 16:16:58 +02:00
|
|
|
import sys
|
2020-04-02 15:08:14 +02:00
|
|
|
|
|
|
|
class PhpTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def randomString(length):
|
|
|
|
letters = string.ascii_lowercase + string.ascii_uppercase + string.digits
|
|
|
|
return ''.join(random.choice(letters) for i in range(length))
|
|
|
|
|
|
|
|
ADMIN_USERNAME = "Administrator"
|
|
|
|
ADMIN_PASSWORD = randomString(16)
|
|
|
|
|
2020-04-02 16:16:58 +02:00
|
|
|
def __init__(self, methods):
|
|
|
|
super().__init__("test_methods")
|
2020-04-02 15:08:14 +02:00
|
|
|
keywords = ["Fatal error", "Warning", "Notice", "Parse error", "Deprecated"]
|
2020-04-02 16:16:58 +02:00
|
|
|
self.methods = methods
|
2020-04-02 15:08:14 +02:00
|
|
|
self.phpPattern = re.compile("<b>(%s)</b>:" % ("|".join(keywords)))
|
2020-04-02 16:16:58 +02:00
|
|
|
self.url = "http://localhost"
|
|
|
|
self.session = requests.Session()
|
2020-04-02 15:08:14 +02:00
|
|
|
|
|
|
|
def httpError(self, res):
|
|
|
|
return "Server returned: %d %s" % (res.status_code, res.reason)
|
|
|
|
|
|
|
|
def getPhpErrors(self, res):
|
|
|
|
return [line for line in res.text.split("\n") if self.phpPattern.search(line)]
|
|
|
|
|
2020-04-02 16:16:58 +02:00
|
|
|
def httpGet(self, target="/"):
|
|
|
|
url = self.url + target
|
|
|
|
res = self.session.get(url)
|
|
|
|
self.assertEquals(200, res.status_code, self.httpError(res))
|
|
|
|
self.assertEquals([], self.getPhpErrors(res))
|
|
|
|
return res
|
|
|
|
|
|
|
|
def httpPost(self, target="/", data={}):
|
|
|
|
url = self.url + target
|
|
|
|
res = self.session.post(url, data=data)
|
|
|
|
self.assertEquals(200, res.status_code, self.httpError(res))
|
|
|
|
self.assertEquals([], self.getPhpErrors(res))
|
|
|
|
obj = self.getJson(res)
|
|
|
|
return obj
|
|
|
|
|
2020-04-02 15:08:14 +02:00
|
|
|
def getJson(self, res):
|
|
|
|
obj = None
|
|
|
|
try:
|
|
|
|
obj = json.loads(res.text)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
self.assertTrue(isinstance(obj, dict), res.text)
|
|
|
|
return obj
|
2020-04-02 16:16:58 +02:00
|
|
|
|
|
|
|
def test_methods(self):
|
|
|
|
print()
|
|
|
|
print("Running Tests in %s" % self.__class__.__name__)
|
|
|
|
for msg, method in self.methods.items():
|
|
|
|
self.test_method(msg, method)
|
|
|
|
|
|
|
|
def test_method(self, msg, method):
|
|
|
|
sys.stdout.write("[ ] %s" % msg)
|
|
|
|
method()
|
|
|
|
sys.stdout.write("\r[+] %s\n" % msg)
|
|
|
|
sys.stdout.flush()
|