2010-09-15 17:28:56 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
|
|
|
$Id$
|
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2010-09-15 17:28:56 +04:00
|
|
|
"""
|
|
|
|
|
2010-09-27 17:26:46 +04:00
|
|
|
import doctest
|
2010-09-26 18:02:13 +04:00
|
|
|
import logging
|
2010-09-15 17:28:56 +04:00
|
|
|
import os
|
2010-09-26 18:02:13 +04:00
|
|
|
import re
|
2010-09-27 15:20:48 +04:00
|
|
|
import shutil
|
2010-09-15 17:28:56 +04:00
|
|
|
import sys
|
2010-09-26 18:02:13 +04:00
|
|
|
import tempfile
|
2010-09-26 14:47:04 +04:00
|
|
|
import time
|
2010-09-15 17:28:56 +04:00
|
|
|
|
2010-09-26 18:02:13 +04:00
|
|
|
from lib.controller.controller import start
|
2010-11-24 00:00:42 +03:00
|
|
|
from lib.core.common import clearConsoleLine
|
2010-09-26 14:47:04 +04:00
|
|
|
from lib.core.common import dataToStdout
|
2010-09-26 18:02:13 +04:00
|
|
|
from lib.core.common import getCompiledRegex
|
2010-10-07 02:43:04 +04:00
|
|
|
from lib.core.common import readXmlFile
|
2010-09-15 17:28:56 +04:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import logger
|
|
|
|
from lib.core.data import paths
|
2010-09-26 18:02:13 +04:00
|
|
|
from lib.core.option import init
|
2010-09-26 18:56:55 +04:00
|
|
|
from lib.core.option import __setVerbosity
|
2010-09-27 17:26:46 +04:00
|
|
|
from lib.core.optiondict import optDict
|
2010-09-26 18:02:13 +04:00
|
|
|
from lib.parse.cmdline import cmdLineParser
|
2010-09-15 17:28:56 +04:00
|
|
|
|
|
|
|
def smokeTest():
|
|
|
|
"""
|
|
|
|
This will run the basic smoke testing of a program
|
|
|
|
"""
|
|
|
|
retVal = True
|
2010-09-26 14:47:04 +04:00
|
|
|
count, length = 0, 0
|
2010-09-27 15:20:48 +04:00
|
|
|
|
2010-10-15 14:29:24 +04:00
|
|
|
for _, _, files in os.walk(paths.SQLMAP_ROOT_PATH):
|
|
|
|
for ifile in files:
|
2010-09-26 14:47:04 +04:00
|
|
|
length += 1
|
2010-09-27 15:20:48 +04:00
|
|
|
|
2010-09-15 17:28:56 +04:00
|
|
|
for root, _, files in os.walk(paths.SQLMAP_ROOT_PATH):
|
2010-10-15 14:29:24 +04:00
|
|
|
for ifile in files:
|
|
|
|
if os.path.splitext(ifile)[1].lower() == '.py' and ifile != '__init__.py':
|
|
|
|
path = os.path.join(root, os.path.splitext(ifile)[0])
|
2010-09-15 17:28:56 +04:00
|
|
|
path = path.replace(paths.SQLMAP_ROOT_PATH, '.')
|
|
|
|
path = path.replace(os.sep, '.').lstrip('.')
|
|
|
|
try:
|
|
|
|
__import__(path)
|
|
|
|
module = sys.modules[path]
|
|
|
|
except Exception, msg:
|
|
|
|
retVal = False
|
2010-09-26 14:47:04 +04:00
|
|
|
dataToStdout("\r")
|
2010-10-15 14:29:24 +04:00
|
|
|
errMsg = "smoke test failed at importing module '%s' (%s):\n%s" % (path, os.path.join(paths.SQLMAP_ROOT_PATH, ifile), msg)
|
2010-09-15 17:28:56 +04:00
|
|
|
logger.error(errMsg)
|
|
|
|
else:
|
|
|
|
# Run doc tests
|
|
|
|
# Reference: http://docs.python.org/library/doctest.html
|
|
|
|
(failure_count, test_count) = doctest.testmod(module)
|
|
|
|
if failure_count > 0:
|
|
|
|
retVal = False
|
|
|
|
|
2010-09-26 14:47:04 +04:00
|
|
|
count += 1
|
2010-10-15 14:29:24 +04:00
|
|
|
status = '%d/%d (%d%s) ' % (count, length, round(100.0*count/length), '%')
|
2010-09-26 14:47:04 +04:00
|
|
|
dataToStdout("\r[%s] [INFO] complete: %s" % (time.strftime("%X"), status))
|
|
|
|
|
2010-11-24 00:00:42 +03:00
|
|
|
clearConsoleLine()
|
2010-09-15 17:28:56 +04:00
|
|
|
if retVal:
|
2010-09-27 15:20:48 +04:00
|
|
|
logger.info("smoke test final result: PASSED")
|
2010-09-15 17:28:56 +04:00
|
|
|
else:
|
2010-09-27 15:20:48 +04:00
|
|
|
logger.error("smoke test final result: FAILED")
|
|
|
|
|
2010-09-15 17:28:56 +04:00
|
|
|
return retVal
|
2010-09-15 17:32:42 +04:00
|
|
|
|
2010-09-27 17:26:46 +04:00
|
|
|
def adjustValueType(tagName, value):
|
|
|
|
for family in optDict.keys():
|
|
|
|
for name, type_ in optDict[family].items():
|
|
|
|
if type(type_) == tuple:
|
|
|
|
type_ = type_[0]
|
|
|
|
if tagName == name:
|
|
|
|
if type_ == "boolean":
|
|
|
|
value = (value == "True")
|
|
|
|
elif type_ == "integer":
|
|
|
|
value = int(value)
|
|
|
|
elif type_ == "float":
|
|
|
|
value = float(value)
|
|
|
|
break
|
|
|
|
return value
|
|
|
|
|
2010-09-15 17:32:42 +04:00
|
|
|
def liveTest():
|
|
|
|
"""
|
2010-09-15 17:55:28 +04:00
|
|
|
This will run the test of a program against the live testing environment
|
2010-09-15 17:32:42 +04:00
|
|
|
"""
|
2010-09-26 18:56:55 +04:00
|
|
|
retVal = True
|
|
|
|
count = 0
|
2010-09-27 15:20:48 +04:00
|
|
|
global_ = {}
|
|
|
|
vars_ = {}
|
2010-10-07 02:43:04 +04:00
|
|
|
livetests = readXmlFile(paths.LIVE_TESTS_XML)
|
2010-09-27 15:20:48 +04:00
|
|
|
length = len(livetests.getElementsByTagName("case"))
|
|
|
|
|
|
|
|
element = livetests.getElementsByTagName("global")
|
|
|
|
if element:
|
|
|
|
for item in element:
|
2010-09-26 18:02:13 +04:00
|
|
|
for child in item.childNodes:
|
|
|
|
if child.nodeType == child.ELEMENT_NODE and child.hasAttribute("value"):
|
2010-09-27 17:26:46 +04:00
|
|
|
global_[child.tagName] = adjustValueType(child.tagName, child.getAttribute("value"))
|
2010-09-27 15:20:48 +04:00
|
|
|
|
|
|
|
element = livetests.getElementsByTagName("vars")
|
|
|
|
if element:
|
|
|
|
for item in element:
|
|
|
|
for child in item.childNodes:
|
|
|
|
if child.nodeType == child.ELEMENT_NODE and child.hasAttribute("value"):
|
|
|
|
vars_[child.tagName] = child.getAttribute("value")
|
2010-09-26 18:02:13 +04:00
|
|
|
|
|
|
|
for case in livetests.getElementsByTagName("case"):
|
2010-09-27 15:20:48 +04:00
|
|
|
name = None
|
2010-09-26 18:02:13 +04:00
|
|
|
log = []
|
|
|
|
session = []
|
2010-09-27 15:20:48 +04:00
|
|
|
switches = dict(global_)
|
|
|
|
|
|
|
|
if case.hasAttribute("name"):
|
|
|
|
name = case.getAttribute("name")
|
2010-09-26 18:02:13 +04:00
|
|
|
|
|
|
|
if case.getElementsByTagName("switches"):
|
|
|
|
for child in case.getElementsByTagName("switches")[0].childNodes:
|
|
|
|
if child.nodeType == child.ELEMENT_NODE and child.hasAttribute("value"):
|
2010-09-27 17:26:46 +04:00
|
|
|
value = replaceVars(child.getAttribute("value"), vars_)
|
|
|
|
switches[child.tagName] = adjustValueType(child.tagName, value)
|
2010-09-26 18:02:13 +04:00
|
|
|
|
|
|
|
if case.getElementsByTagName("log"):
|
|
|
|
for item in case.getElementsByTagName("log")[0].getElementsByTagName("item"):
|
|
|
|
if item.hasAttribute("value"):
|
2010-09-27 15:20:48 +04:00
|
|
|
log.append(replaceVars(item.getAttribute("value"), vars_))
|
2010-09-26 18:02:13 +04:00
|
|
|
|
|
|
|
if case.getElementsByTagName("session"):
|
|
|
|
for item in case.getElementsByTagName("session")[0].getElementsByTagName("item"):
|
|
|
|
if item.hasAttribute("value"):
|
2010-09-27 15:20:48 +04:00
|
|
|
session.append(replaceVars(item.getAttribute("value"), vars_))
|
2010-09-26 18:56:55 +04:00
|
|
|
|
2010-09-27 15:20:48 +04:00
|
|
|
count += 1
|
|
|
|
msg = "running live test case '%s' (%d/%d)" % (name, count, length)
|
|
|
|
logger.info(msg)
|
|
|
|
result = runCase(name, switches, log, session)
|
|
|
|
if result:
|
|
|
|
logger.info("test passed")
|
|
|
|
else:
|
|
|
|
logger.error("test failed")
|
|
|
|
retVal &= result
|
|
|
|
|
|
|
|
dataToStdout("\n")
|
2010-09-26 18:56:55 +04:00
|
|
|
if retVal:
|
2010-09-27 15:20:48 +04:00
|
|
|
logger.info("live test final result: PASSED")
|
2010-09-26 18:56:55 +04:00
|
|
|
else:
|
2010-09-27 15:20:48 +04:00
|
|
|
logger.error("live test final result: FAILED")
|
|
|
|
|
2010-09-26 18:56:55 +04:00
|
|
|
return retVal
|
2010-09-26 18:02:13 +04:00
|
|
|
|
2010-09-27 15:20:48 +04:00
|
|
|
def initCase(switches=None):
|
2010-09-26 18:02:13 +04:00
|
|
|
paths.SQLMAP_OUTPUT_PATH = tempfile.mkdtemp()
|
|
|
|
paths.SQLMAP_DUMP_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "dump")
|
|
|
|
paths.SQLMAP_FILES_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "files")
|
|
|
|
cmdLineOptions = cmdLineParser()
|
|
|
|
cmdLineOptions.liveTest = cmdLineOptions.smokeTest = False
|
2010-09-27 15:20:48 +04:00
|
|
|
|
|
|
|
if switches:
|
|
|
|
for key, value in switches.items():
|
2010-09-27 17:26:46 +04:00
|
|
|
if key in cmdLineOptions.__dict__:
|
|
|
|
cmdLineOptions.__dict__[key] = value
|
2010-09-27 15:20:48 +04:00
|
|
|
|
|
|
|
conf.sessionFile = None
|
2010-09-26 18:02:13 +04:00
|
|
|
init(cmdLineOptions)
|
2010-09-26 18:56:55 +04:00
|
|
|
__setVerbosity()
|
|
|
|
|
|
|
|
def cleanCase():
|
2010-09-27 15:20:48 +04:00
|
|
|
shutil.rmtree(paths.SQLMAP_OUTPUT_PATH, True)
|
2010-09-26 18:56:55 +04:00
|
|
|
paths.SQLMAP_OUTPUT_PATH = os.path.join(paths.SQLMAP_ROOT_PATH, "output")
|
|
|
|
paths.SQLMAP_DUMP_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "dump")
|
|
|
|
paths.SQLMAP_FILES_PATH = os.path.join(paths.SQLMAP_OUTPUT_PATH, "%s", "files")
|
|
|
|
conf.verbose = 1
|
|
|
|
__setVerbosity()
|
2010-09-26 18:02:13 +04:00
|
|
|
|
2010-09-27 15:20:48 +04:00
|
|
|
def runCase(name=None, switches=None, log=None, session=None):
|
2010-09-26 18:56:55 +04:00
|
|
|
retVal = True
|
2010-09-27 15:20:48 +04:00
|
|
|
initCase(switches)
|
2010-09-26 18:56:55 +04:00
|
|
|
|
|
|
|
result = start()
|
|
|
|
if result == False: #if None ignore
|
|
|
|
retVal = False
|
|
|
|
|
|
|
|
if session and retVal:
|
2010-10-15 14:29:24 +04:00
|
|
|
ifile = open(conf.sessionFile, 'r')
|
|
|
|
content = ifile.read()
|
|
|
|
ifile.close()
|
2010-09-26 18:56:55 +04:00
|
|
|
for item in session:
|
2010-09-27 15:20:48 +04:00
|
|
|
if item.startswith("r'") and item.endswith("'"):
|
|
|
|
if not re.search(item[2:-1], content):
|
|
|
|
retVal = False
|
|
|
|
break
|
|
|
|
elif content.find(item) < 0:
|
2010-09-26 18:56:55 +04:00
|
|
|
retVal = False
|
|
|
|
break
|
|
|
|
|
|
|
|
if log and retVal:
|
2010-10-15 14:29:24 +04:00
|
|
|
ifile = open(conf.dumper.getOutputFile(), 'r')
|
|
|
|
content = ifile.read()
|
|
|
|
ifile.close()
|
2010-09-26 18:56:55 +04:00
|
|
|
for item in log:
|
2010-09-27 15:20:48 +04:00
|
|
|
if item.startswith("r'") and item.endswith("'"):
|
|
|
|
if not re.search(item[2:-1], content):
|
|
|
|
retVal = False
|
|
|
|
break
|
|
|
|
elif content.find(item) < 0:
|
2010-09-26 18:56:55 +04:00
|
|
|
retVal = False
|
|
|
|
break
|
|
|
|
|
|
|
|
cleanCase()
|
|
|
|
return retVal
|
2010-09-26 18:02:13 +04:00
|
|
|
|
2010-09-27 15:20:48 +04:00
|
|
|
def replaceVars(item, vars_):
|
2010-09-26 18:02:13 +04:00
|
|
|
retVal = item
|
2010-09-27 15:20:48 +04:00
|
|
|
if item and vars_:
|
2010-09-26 18:02:13 +04:00
|
|
|
for var in re.findall(getCompiledRegex("\$\{([^}]+)\}"), item):
|
2010-09-27 15:20:48 +04:00
|
|
|
if var in vars_:
|
|
|
|
retVal = retVal.replace("${%s}" % var, vars_[var])
|
|
|
|
return retVal
|